@@ -15,6 +15,11 @@ class UcReactionGenerator(private val reactor: Reactor) {
1515 val Reaction .index
1616 get(): Int = priority - 1
1717
18+
19+
20+ private val Reaction .ctorDeadlineArg
21+ get() = if (deadline != null ) " , ${deadline.delay.toCCode()} " else " "
22+
1823 private val Reaction .allUncontainedTriggers
1924 get() = triggers.filterNot { it.isEffectOf(this ) || it.isContainedRef }
2025 private val Reaction .allUncontainedEffects
@@ -29,6 +34,8 @@ class UcReactionGenerator(private val reactor: Reactor) {
2934 private val Reaction .allContainedSources
3035 get() = sources.filter { ! it.isEffectOf(this ) && it.isContainedRef }
3136
37+ private val reactionsWithDeadline = reactor.allReactions.filter {it.deadline != null }
38+
3239 // Calculate the total number of effects, considering that we might write to
3340 // a contained input port
3441 private val Reaction .totalNumEffects
@@ -151,7 +158,7 @@ class UcReactionGenerator(private val reactor: Reactor) {
151158 }
152159
153160 private fun generateReactionCtor (reaction : Reaction ) =
154- " DEFINE_REACTION_CTOR(${reactor.codeType} , ${reaction.codeName} , ${reaction.index} );"
161+ " DEFINE_REACTION_CTOR(${reactor.codeType} , ${reaction.codeName} , ${reaction.index} ${reaction.ctorDeadlineArg} );"
155162
156163 private fun generateSelfStruct (reaction : Reaction ) =
157164 " DEFINE_REACTION_STRUCT(${reactor.codeType} , ${reaction.codeName} , ${reaction.totalNumEffects} );"
@@ -185,15 +192,37 @@ class UcReactionGenerator(private val reactor: Reactor) {
185192 postfix = " \n "
186193 ) { generateReactionBody(it) }
187194
188- private fun generateReactionBody (reaction : Reaction ) = with (PrependOperator ) {
189- """
190- |DEFINE_REACTION_BODY(${reactor.codeType} , ${reaction.codeName} ) {
191- | // Bring self struct, environment, triggers, effects and sources into scope.
195+ fun generateReactionDeadlineHandlers () =
196+ reactionsWithDeadline.joinToString(
197+ separator = " \n " ,
198+ prefix = " // Reaction deadline handlers\n " ,
199+ postfix= " \n "
200+ ) { generateReactionDeadlineHandler(it) }
201+
202+ private fun generateReactionScope (reaction : Reaction ) = with (PrependOperator ) {
203+ """ |// Bring self struct, environment, triggers, effects and sources into scope.
192204 | SCOPE_SELF(${reactor.codeType} );
193205 | SCOPE_ENV();
194206 ${" | " .. generateTriggersEffectsAndSourcesInScope(reaction)}
195207 ${" | " .. generateContainedTriggersAndSourcesInScope(reaction)}
208+ """ .trimMargin()
209+ }
210+
211+ private fun generateReactionDeadlineHandler (reaction : Reaction ) = with (PrependOperator ) {
212+ """
213+ |DEFINE_REACTION_DEADLINE_HANDLER(${reactor.codeType} , ${reaction.codeName} ) {
214+ ${" | " .. generateReactionScope(reaction)}
196215 | // Start of user-witten reaction body
216+ ${" | " .. reaction.deadline.code.toText()}
217+ |}
218+ """ .trimMargin()
219+ }
220+
221+ private fun generateReactionBody (reaction : Reaction ) = with (PrependOperator ) {
222+ """
223+ |DEFINE_REACTION_BODY(${reactor.codeType} , ${reaction.codeName} ) {
224+ ${" | " .. generateReactionScope(reaction)}
225+ | // Start of user-witten reaction deadline handler
197226 ${" | " .. reaction.code.toText()}
198227 |}
199228 """ .trimMargin()
0 commit comments