Skip to content

Commit

Permalink
Merge pull request #2353 from lf-lang/self-defined
Browse files Browse the repository at this point in the history
Define self variable so it can be used in instantiations
  • Loading branch information
edwardalee authored Jul 11, 2024
2 parents 76156a8 + a765dc1 commit 4865086
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,25 @@ public static String generatePythonClassInstantiations(
// setting parameter values.
code.pr("bank_index = " + PyUtil.bankIndexName(instance));
code.pr(generatePythonClassInstantiation(instance, className));
}

for (ReactorInstance child : instance.children) {
code.pr(generatePythonClassInstantiations(child, main));
if (!instance.children.isEmpty()) {
// Define self so that instantiations of contained reactors can refer to parameters.
// First, save the previous definition of self to restore after instantiating children.
// But do not do this for the main reactor.
if (instance.getParent() != null) {
code.pr("previous_self = self");
}
code.pr("self = " + PyUtil.reactorRef(instance));

for (ReactorInstance child : instance.children) {
code.pr(generatePythonClassInstantiations(child, main));
}
if (instance.getParent() != null) {
code.pr("self = previous_self");
}
}
code.unindent();
}
code.unindent();
return code.toString();
}

Expand Down
14 changes: 14 additions & 0 deletions test/Python/src/BankIndex.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
target Python

reactor A(bank_index=0, value=0) {
reaction(startup) {=
print("bank_index: {:d}, value: {:d}".format(self.bank_index, self.value))
if (self.value != 4 - self.bank_index):
sys.stderr.write("ERROR: Expected value to be 4 - bank_index.\n")
exit(1)
=}
}

main reactor(table = [4, 3, 2, 1]) {
a = new[4] A(value = {= self.table[bank_index] =})
}

0 comments on commit 4865086

Please sign in to comment.