Skip to content

Commit

Permalink
Fix the preamble generator (#146)
Browse files Browse the repository at this point in the history
* Fix the preamble generator

* Also test imported preambles

* Fix scoping
  • Loading branch information
erlingrj authored Dec 2, 2024
1 parent a7d9dc4 commit 330e9d2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,15 @@ class UcReactionGenerator(private val reactor: Reactor) {
return res
}


private val Reaction.allContainedEffectsTriggersAndSources
get() = run {
val res = mutableMapOf<Instantiation, List<VarRef>>()
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
}
Expand Down Expand Up @@ -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)}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions test/lf/src/Preamble.lf
Original file line number Diff line number Diff line change
@@ -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);
=}
}
11 changes: 11 additions & 0 deletions test/lf/src/lib/ImportedPreamble.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
target uC

preamble {=
static int test3 = 22;
=}

reactor ImportedPreamble {
reaction(startup) {=
validate(test3 == 22);
=}
}

0 comments on commit 330e9d2

Please sign in to comment.