From 330e9d225d1bdd78e9c27f837989fc9bba70e42b Mon Sep 17 00:00:00 2001 From: erling Date: Mon, 2 Dec 2024 10:06:47 -0800 Subject: [PATCH] Fix the preamble generator (#146) * Fix the preamble generator * Also test imported preambles * Fix scoping --- .../lflang/generator/uc/UcPreambleGenerator.kt | 2 +- .../lflang/generator/uc/UcReactionGenerator.kt | 17 +++++++---------- .../lflang/generator/uc/UcReactorGenerator.kt | 2 +- test/lf/src/Preamble.lf | 8 ++++++++ test/lf/src/lib/ImportedPreamble.lf | 11 +++++++++++ 5 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 test/lf/src/lib/ImportedPreamble.lf diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcPreambleGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcPreambleGenerator.kt index 33ec22f1..23dd7afd 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcPreambleGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcPreambleGenerator.kt @@ -10,5 +10,5 @@ class UcPreambleGenerator( private val reactor: Reactor, ) { fun generateReactorPrivatePreamble() = reactor.preambles.joinToString(prefix= "// Private preambles\n", separator = "\n", postfix = "\n") { it.code.toText()} - fun generateReactorPublicPreamble() = resource.model.preambles.joinToString(prefix = "// Public preambles ", separator = "\n", postfix = "\n") {it.code.toText()} + fun generateReactorPublicPreamble() = resource.model.preambles.joinToString(prefix = "// Public preambles\n", separator = "\n", postfix = "\n") {it.code.toText()} } diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcReactionGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcReactionGenerator.kt index 7cab0ebf..e57bedac 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcReactionGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcReactionGenerator.kt @@ -48,18 +48,15 @@ class UcReactionGenerator(private val reactor: Reactor) { return res } - private val Reaction.allContainedEffectsTriggersAndSources get() = run { val res = mutableMapOf>() - for (effect in allContainedEffects) { - res.getOrPut(effect.container!!) { mutableListOf(effect) }.plus(effect) - } - for (trigger in allContainedTriggers) { - res.getOrPut((trigger as VarRef).container!!) { mutableListOf(trigger) }.plus(trigger) - } - for (source in allContainedSources) { - res.getOrPut((source as VarRef).container!!) { mutableListOf(source) }.plus(source) + for (varRef in allContainedEffects.plus(allContainedSources).plus(allContainedTriggers)) { + val varRef = varRef as VarRef + if (varRef.container!! !in res.keys) { + res[varRef.container] = mutableListOf() + } + res[varRef.container] = res[varRef.container]!!.plus(varRef) } res } @@ -238,7 +235,7 @@ class UcReactionGenerator(private val reactor: Reactor) { """| |// Generated struct providing access to ports of child reactor `${inst.name}` |struct _${inst.reactor.codeType}_${inst.name} { - ${"| "..triggers.joinToString { generateContainedTriggerInScope(it) }} + ${"| "..triggers.joinToString(separator = "\n") { generateContainedTriggerInScope(it) }} |}; |struct _${inst.reactor.codeType}_${inst.name} ${inst.name}; ${"|"..triggers.joinToString(separator = "\n") {generateContainedTriggerFieldInit("${inst.name}", it)}} diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcReactorGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcReactorGenerator.kt index 671b4412..547899fc 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcReactorGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcReactorGenerator.kt @@ -37,7 +37,7 @@ class UcReactorGenerator(private val reactor: Reactor, fileConfig: UcFileConfig, private val timers = UcTimerGenerator(reactor) private val actions = UcActionGenerator(reactor) private val reactions = UcReactionGenerator(reactor) - private val preambles = UcPreambleGenerator(fileConfig.resource, reactor) + private val preambles = UcPreambleGenerator(reactor.eResource(), reactor) private val instances = UcInstanceGenerator(reactor, parameters, ports, connections, reactions, fileConfig, messageReporter) diff --git a/test/lf/src/Preamble.lf b/test/lf/src/Preamble.lf index ef8da7bc..1195f0be 100644 --- a/test/lf/src/Preamble.lf +++ b/test/lf/src/Preamble.lf @@ -1,11 +1,19 @@ target uC +import ImportedPreamble from "./lib/ImportedPreamble.lf" +preamble {= + static int test2 = 32; +=} main reactor { + imported = new ImportedPreamble() + preamble {= static int test = 42; =} reaction(startup) {= validate(test == 42); + validate(test2 == 32); + validate(test3 == 22); =} } diff --git a/test/lf/src/lib/ImportedPreamble.lf b/test/lf/src/lib/ImportedPreamble.lf new file mode 100644 index 00000000..cfc11eee --- /dev/null +++ b/test/lf/src/lib/ImportedPreamble.lf @@ -0,0 +1,11 @@ +target uC + +preamble {= + static int test3 = 22; +=} + +reactor ImportedPreamble { + reaction(startup) {= + validate(test3 == 22); + =} +}