Skip to content

Commit

Permalink
Merge pull request #2437 from lf-lang/method-macros
Browse files Browse the repository at this point in the history
Make macros available in methods
  • Loading branch information
erlingrj authored Nov 19, 2024
2 parents 58bb134 + 2a8e220 commit 8d614be
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/c-zephyr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ jobs:
--tests org.lflang.tests.runtime.CZephyrTest.buildZephyrThreaded* core:integrationTestCodeCoverageReport
./.github/scripts/run-zephyr-tests.sh test/C/src-gen
rm -rf test/C/src-gen
- name: Run basic tests
run: |
./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildBasic* core:integrationTestCodeCoverageReport
./.github/scripts/run-zephyr-tests.sh test/C/src-gen
rm -rf test/C/src-gen
- name: Run concurrent tests
run: |
./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildConcurrent* core:integrationTestCodeCoverageReport
./.github/scripts/run-zephyr-tests.sh test/C/src-gen
rm -rf test/C/src-gen
# - name: Run basic tests
# run: |
# ./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildBasic* core:integrationTestCodeCoverageReport
# ./.github/scripts/run-zephyr-tests.sh test/C/src-gen
# rm -rf test/C/src-gen
# - name: Run concurrent tests
# run: |
# ./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildConcurrent* core:integrationTestCodeCoverageReport
# ./.github/scripts/run-zephyr-tests.sh test/C/src-gen
# rm -rf test/C/src-gen
- name: Run Zephyr board tests
run: |
./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildZephyrBoards* core:integrationTestCodeCoverageReport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.lflang.generator.CodeBuilder;
import org.lflang.lf.Method;
import org.lflang.lf.Reactor;
import org.lflang.util.StringUtil;

/**
* Collection of functions to generate C code to declare methods.
Expand Down Expand Up @@ -60,6 +61,8 @@ public static String generateMethod(
var body = ASTUtils.toText(method.getCode());

code.prSourceLineNumber(method, suppressLineDirectives);
// Define macros for functions such as lf_tag(), lf_time_logical(), lf_set(), etc.
code.pr("#include " + StringUtil.addDoubleQuotes(CCoreFilesUtils.getCTargetSetHeader()));

code.prComment("Implementation of method " + method.getName() + "()");
code.pr(generateMethodSignature(method, tpr, types) + " {");
Expand All @@ -83,6 +86,7 @@ public static String generateMethod(
code.pr(body);
code.unindent();
code.pr("}");
code.pr("#include " + StringUtil.addDoubleQuotes(CCoreFilesUtils.getCTargetSetUndefHeader()));
code.prEndSourceLineNumber(suppressLineDirectives);
return code.toString();
}
Expand Down
48 changes: 48 additions & 0 deletions test/C/src/MethodMacros.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Test ability of methods to call each other and (recursively) themselves.
target C {
timeout: 10 ms
}

reactor Fib {
input in: int
output out: int
state foo: int = 2

// Return the n-th Fibonacci number.
// Type name convention for ports is _class_method_t, all lowercase.
method fib(out: _fib_out_t*, n: int): int {=
int result = 1;
if (n > 1) result = add(fib(NULL, n-1), fib(NULL, n-2));
if (out != NULL) {
lf_set(out, result);
lf_print("At elapsed time " PRINTF_TIME ", fib(%d) = %d", lf_time_logical_elapsed(), n, result);
}
return result;
=}

method add(x: int, y: int): int {=
return x + y;
=}

reaction(in) -> out {=
fib(out, in->value);
=}
}

main reactor {
state count: int = 0
timer t(0, 1 ms)
fib = new Fib()

reaction(t) -> fib.in {=
lf_set(fib.in, self->count);
=}

reaction(fib.out) {=
lf_print("fib(%d) = %d", self->count, fib.out->value);
int answers[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
if (fib.out->value != answers[self->count++]) {
lf_print_error_and_exit("Expected %d", answers[self->count-1]);
}
=}
}

0 comments on commit 8d614be

Please sign in to comment.