Skip to content

Commit

Permalink
Merge pull request #2369 from lf-lang/cpp-time-expressions
Browse files Browse the repository at this point in the history
Ability to use of target code expressions for time values in C++
  • Loading branch information
cmnrd authored Jul 16, 2024
2 parents 2b44dd9 + 5be133d commit 740ba74
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
26 changes: 16 additions & 10 deletions core/src/main/java/org/lflang/validation/LFValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.lflang.lf.BracketListExpression;
import org.lflang.lf.BuiltinTrigger;
import org.lflang.lf.BuiltinTriggerRef;
import org.lflang.lf.CodeExpr;
import org.lflang.lf.Connection;
import org.lflang.lf.Deadline;
import org.lflang.lf.Expression;
Expand Down Expand Up @@ -1603,16 +1604,21 @@ private void checkExpressionIsTime(Expression value, EStructuralFeature feature)
error("Missing time unit.", feature);
return;
}
} else if (target == Target.CPP && value instanceof ParenthesisListExpression) {
final var exprs = ((ParenthesisListExpression) value).getItems();
if (exprs.size() == 1) {
checkExpressionIsTime(exprs.get(0), feature);
return;
}
} else if (target == Target.CPP && value instanceof BracedListExpression) {
final var exprs = ((BracedListExpression) value).getItems();
if (exprs.size() == 1) {
checkExpressionIsTime(exprs.get(0), feature);
} else if (target == Target.CPP) {
if (value instanceof ParenthesisListExpression) {
final var exprs = ((ParenthesisListExpression) value).getItems();
if (exprs.size() == 1) {
checkExpressionIsTime(exprs.get(0), feature);
return;
}
} else if (value instanceof BracedListExpression) {
final var exprs = ((BracedListExpression) value).getItems();
if (exprs.size() == 1) {
checkExpressionIsTime(exprs.get(0), feature);
return;
}
} else if (value instanceof CodeExpr) {
// We leave checking of target code expressions to the target compiler
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class CppPreambleGenerator(
|#include "reactor-cpp/reactor-cpp.hh"
${" |"..includes.joinToString(separator = "\n", prefix = "// include the preambles from imported files \n")}
|
|using namespace std::chrono_literals;
|
${" |"..publicPreambles.joinToString(separator = "\n") { it.code.toText() }}
""".trimMargin()
}
Expand Down
53 changes: 53 additions & 0 deletions test/Cpp/src/target/TimeExpression.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
target Cpp {
timeout: 1 s
}

public preamble {=
inline reactor::Duration get_delay() { return 200ms; }
=}

reactor Foo(foo: time = {= get_delay() =}) {
timer t(0, foo)

state timer_triggered: bool = false

reaction(t) {=
timer_triggered = true;
if (get_elapsed_logical_time() % 200ms != reactor::Duration::zero()) {
std::cerr << "ERROR: timer triggered at an unexpected time\n";
exit(4);
}
=}

reaction(shutdown) {=
if (!timer_triggered) {
std::cerr << "ERROR: timer did not trigger\n";
exit(1);
}
=}
}

main reactor {
foo = new Foo()

state timer_triggered: bool = false

timer t(0, {= get_delay() + 200ms =})

reaction(t) {=
timer_triggered = true;
if (get_elapsed_logical_time() % 400ms != reactor::Duration::zero()) {
std::cerr << "ERROR: timer triggered at an unexpected time\n";
exit(4);
}
=}

reaction(shutdown) {=
if (!timer_triggered) {
std::cerr << "ERROR: timer did not trigger\n";
exit(2);
}

std::cout << "Success\n";
=}
}

0 comments on commit 740ba74

Please sign in to comment.