Skip to content

Commit

Permalink
Merge pull request #1964 from lf-lang/modes-statevar-reset-time
Browse files Browse the repository at this point in the history
Fix compilation error in code for reset state variables with time type
  • Loading branch information
lhstrh authored Aug 27, 2023
2 parents 8bd0390 + 9ec756d commit 76c0cf5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ private static String generateModalReset(
+ "]";
var type = types.getTargetType(instance.tpr.resolveType(ASTUtils.getInferredType(stateVar)));

if (ASTUtils.isOfTimeType(stateVar)
|| ASTUtils.isParameterized(stateVar) && !stateVar.getInit().getExprs().isEmpty()) {
if (ASTUtils.isParameterized(stateVar) && !stateVar.getInit().getExprs().isEmpty()) {
return CModesGenerator.generateStateResetStructure(
instance, modeRef, selfRef, stateVar.getName(), initExpr, type);
} else {
Expand Down
42 changes: 42 additions & 0 deletions test/C/src/modal_models/ResetStateVariableOfTypeTime.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Modal Reactor Test. Tests state variable with type time. Model by Edward Lee.
* https://github.com/lf-lang/lingua-franca/issues/1938
*/
target C {
timeout: 3 s,
fast: true
}

reactor C {
input trigger: bool
reset state t: time = 0 s

reaction(trigger) {=
lf_print("t = %ld", self->t);
if (self->t != SEC(0)) {
lf_print("Error: Missing reset");
}

self->t = lf_time_logical();
=}
}

main reactor {
timer t(0, 1 s)

initial mode A {
c = new C()
reaction(t) -> reset(B), c.trigger {=
lf_print("In A");
lf_set(c.trigger, true);
lf_set_mode(B);
=}
}

mode B {
reaction(t) -> reset(A) {=
lf_print("In B");
lf_set_mode(A);
=}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Modal Reactor Test. Tests state variable initialized via parameter. Model by Edward Lee.
* https://github.com/lf-lang/lingua-franca/issues/1938
*/
target C {
timeout: 3 s,
fast: true
}

reactor C(init: int = 0) {
input trigger: bool
reset state i: int = init

reaction(trigger) {=
lf_print("i = %d", self->i);
if (self->i != -1) {
lf_print("Error: Missing reset");
}

self->i += 10;
=}
}

main reactor {
timer t(0, 1 s)

initial mode A {
c = new C(init=-1)
reaction(t) -> reset(B), c.trigger {=
lf_print("In A");
lf_set(c.trigger, true);
lf_set_mode(B);
=}
}

mode B {
reaction(t) -> reset(A) {=
lf_print("In B");
lf_set_mode(A);
=}
}
}

0 comments on commit 76c0cf5

Please sign in to comment.