diff --git a/core/src/main/java/org/lflang/generator/python/PythonReactorGenerator.java b/core/src/main/java/org/lflang/generator/python/PythonReactorGenerator.java index 8f2d3fd8ad..10c7e400df 100644 --- a/core/src/main/java/org/lflang/generator/python/PythonReactorGenerator.java +++ b/core/src/main/java/org/lflang/generator/python/PythonReactorGenerator.java @@ -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(); } diff --git a/test/Python/src/BankIndex.lf b/test/Python/src/BankIndex.lf new file mode 100644 index 0000000000..76494f5d93 --- /dev/null +++ b/test/Python/src/BankIndex.lf @@ -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] =}) +}