Skip to content

Commit

Permalink
Do not squeeze reaction bodies onto one line.
Browse files Browse the repository at this point in the history
This adds state, but it is a private non-static member and the formatter
is not parallelized, so it should be OK. The technique employed here is
I think the most reasonable way to account for context-sensitive
formatting rules.
  • Loading branch information
petervdonovan committed Sep 2, 2023
1 parent 4444063 commit 90967d4
Show file tree
Hide file tree
Showing 454 changed files with 2,181 additions and 712 deletions.
17 changes: 17 additions & 0 deletions core/src/main/java/org/lflang/ast/ToLf.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lflang.ast;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -91,6 +92,12 @@ public class ToLf extends LfSwitch<MalleableString> {
/// public instance initialized when loading the class
public static final ToLf instance = new ToLf();

/**
* The eObjects in the syntax tree on the path from the root up to and including the current
* eObject.
*/
private final ArrayDeque<EObject> callStack = new ArrayDeque<>();

// private constructor
private ToLf() {
super();
Expand All @@ -104,6 +111,13 @@ public MalleableString caseArraySpec(ArraySpec spec) {

@Override
public MalleableString doSwitch(EObject eObject) {
callStack.push(eObject);
var ret = doSwitchHelper(eObject);
callStack.pop();
return ret;
}

private MalleableString doSwitchHelper(EObject eObject) {
ICompositeNode node = NodeModelUtils.findActualNodeFor(eObject);
if (node == null) return super.doSwitch(eObject);
var ancestorComments = getAncestorComments(node);
Expand Down Expand Up @@ -257,6 +271,9 @@ public MalleableString caseCode(Code code) {
if (content.lines().count() > 1 || content.contains("#") || content.contains("//")) {
return multilineRepresentation;
}
if (callStack.stream().anyMatch(it -> it instanceof Code) && !content.isBlank()) {
return MalleableString.anyOf(multilineRepresentation);
}
return MalleableString.anyOf(singleLineRepresentation, multilineRepresentation);
}

Expand Down
8 changes: 6 additions & 2 deletions test/C/src/ActionDelay.lf
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ reactor GeneratedDelay {
lf_schedule(act, MSEC(0));
=}

reaction(act) -> y_out {= lf_set(y_out, self->y_state); =}
reaction(act) -> y_out {=
lf_set(y_out, self->y_state);
=}
}

reactor Source {
output out: int

reaction(startup) -> out {= lf_set(out, 1); =}
reaction(startup) -> out {=
lf_set(out, 1);
=}
}

reactor Sink {
Expand Down
4 changes: 3 additions & 1 deletion test/C/src/ActionWithNoReaction.lf
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ main reactor ActionWithNoReaction {
timer t(0, 1 sec)
f.y -> p.x after 10 msec

reaction(t) -> f.x {= lf_set(f.x, 42); =}
reaction(t) -> f.x {=
lf_set(f.x, 42);
=}
}
8 changes: 6 additions & 2 deletions test/C/src/After.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ reactor foo {
input x: int
output y: int

reaction(x) -> y {= lf_set(y, 2*x->value); =}
reaction(x) -> y {=
lf_set(y, 2*x->value);
=}
}

reactor print {
Expand Down Expand Up @@ -47,5 +49,7 @@ main reactor After {
timer t(0, 1 sec)
f.y -> p.x after 10 msec

reaction(t) -> f.x {= lf_set(f.x, 42); =}
reaction(t) -> f.x {=
lf_set(f.x, 42);
=}
}
8 changes: 6 additions & 2 deletions test/C/src/AfterCycles.lf
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ target C
reactor Source {
output out: unsigned

reaction(startup) -> out {= lf_set(out, 1); =}
reaction(startup) -> out {=
lf_set(out, 1);
=}
}

reactor Work {
input in: unsigned
output out: unsigned

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

main reactor AfterCycles {
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/AfterZero.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ reactor foo {
input x: int
output y: int

reaction(x) -> y {= SET(y, 2*x->value); =}
reaction(x) -> y {=
SET(y, 2*x->value);
=}
}

reactor print {
Expand Down Expand Up @@ -52,5 +54,7 @@ main reactor {
timer t(0, 1 sec)
f.y -> p.x after 0

reaction(t) -> f.x {= SET(f.x, 42); =}
reaction(t) -> f.x {=
SET(f.x, 42);
=}
}
4 changes: 3 additions & 1 deletion test/C/src/Alignment.lf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ reactor Source {
state count: int = 1
timer t(0, 100 msec)

reaction(t) -> out {= lf_set(out, self->count++); =}
reaction(t) -> out {=
lf_set(out, self->count++);
=}
}

reactor Sieve {
Expand Down
4 changes: 3 additions & 1 deletion test/C/src/CompositionGain.lf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ reactor Wrapper {
main reactor CompositionGain {
wrapper = new Wrapper()

reaction(startup) -> wrapper.x {= lf_set(wrapper.x, 42); =}
reaction(startup) -> wrapper.x {=
lf_set(wrapper.x, 42);
=}

reaction(wrapper.y) {=
printf("Received %d\n", wrapper.y->value);
Expand Down
4 changes: 3 additions & 1 deletion test/C/src/DanglingOutput.lf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ reactor Source {
output out: int
timer t

reaction(t) -> out {= lf_set(out, 1); =}
reaction(t) -> out {=
lf_set(out, 1);
=}
}

reactor Gain {
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DeadlineAnytime.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ reactor A {
reaction(startup) -> a {=
self->i = 0;
while (!lf_check_deadline(self, true));
=} deadline(10 msec) {= lf_schedule(a, 0); =}
=} deadline(10 msec) {=
lf_schedule(a, 0);
=}

reaction(a) {= self->i = 42; =}
reaction(a) {=
self->i = 42;
=}

reaction(shutdown) {=
if (self->i == 42) {
Expand Down
12 changes: 9 additions & 3 deletions test/C/src/DeadlineInherited.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ target C {
threading: false
}

preamble {= extern int global_cnt; =}
preamble {=
extern int global_cnt;
=}

reactor NoDeadline {
preamble {= int global_cnt = 0; =}
preamble {=
int global_cnt = 0;
=}
timer t(0 msec, 100 msec)

reaction(t) {= global_cnt++; =}
reaction(t) {=
global_cnt++;
=}
}

reactor WithDeadline {
Expand Down
12 changes: 9 additions & 3 deletions test/C/src/DeadlinePriority.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ target C {
threading: false
}

preamble {= extern int global_cnt; =}
preamble {=
extern int global_cnt;
=}

reactor NoDeadline {
preamble {= int global_cnt = 0; =}
preamble {=
int global_cnt = 0;
=}
timer t(0 msec, 100 msec)

reaction(t) {= global_cnt++; =}
reaction(t) {=
global_cnt++;
=}
}

reactor WithDeadline {
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DeadlineWithAfterDelay.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ target C {
threading: false
}

preamble {= extern int global_cnt; =}
preamble {=
extern int global_cnt;
=}

reactor Source {
preamble {= int global_cnt = 0; =}
preamble {=
int global_cnt = 0;
=}

output out: int
timer t(0 msec, 100 msec)
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DeadlineWithBanks.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ target C {
build-type: Debug
}

preamble {= extern volatile int global_cnt; =}
preamble {=
extern volatile int global_cnt;
=}

reactor Bank(bank_index: int = 0) {
preamble {= volatile int global_cnt = 0; =}
preamble {=
volatile int global_cnt = 0;
=}

timer t(0, 100 msec)
output out: int
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DeadlineZero.lf
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ reactor Detector {
reaction(trigger) {=
printf("ERROR: failed to detect zero-duration deadline at iteration %d.\n", self->cnt);
exit(1);
=} deadline(0 msec) {= self->cnt++; =}
=} deadline(0 msec) {=
self->cnt++;
=}
}

reactor Generator {
output pulse: int
timer t(0, 100 msec)

reaction(t) -> pulse {= lf_set(pulse, 0); =}
reaction(t) -> pulse {=
lf_set(pulse, 0);
=}
}

main reactor {
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DelayArray.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ reactor DelayPointer(delay: time = 100 msec) {
output out: int[]
logical action a: int[]

reaction(in) -> a {= lf_schedule_token(a, self->delay, in->token); =}
reaction(in) -> a {=
lf_schedule_token(a, self->delay, in->token);
=}

reaction(a) -> out {= lf_set_token(out, a->token); =}
reaction(a) -> out {=
lf_set_token(out, a->token);
=}
}

reactor Source {
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DelayInt.lf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ reactor Delay(delay: time = 100 msec) {
output out: int
logical action a: int

reaction(a) -> out {= if (a->has_value && a->is_present) lf_set(out, a->value); =}
reaction(a) -> out {=
if (a->has_value && a->is_present) lf_set(out, a->value);
=}

reaction(in) -> a {=
// Use specialized form of schedule for integer payloads.
Expand Down Expand Up @@ -55,5 +57,7 @@ main reactor DelayInt {
t = new Test()
d.out -> t.in

reaction(startup) -> d.in {= lf_set(d.in, 42); =}
reaction(startup) -> d.in {=
lf_set(d.in, 42);
=}
}
8 changes: 6 additions & 2 deletions test/C/src/DelayString.lf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ reactor DelayString2(delay: time = 100 msec) {
output out: string
logical action a: string

reaction(a) -> out {= lf_set(out, a->value); =}
reaction(a) -> out {=
lf_set(out, a->value);
=}

reaction(in) -> a {=
// The following copies the char*, not the string.
Expand Down Expand Up @@ -48,5 +50,7 @@ main reactor {
t = new Test()
d.out -> t.in

reaction(startup) -> d.in {= lf_set(d.in, "Hello"); =}
reaction(startup) -> d.in {=
lf_set(d.in, "Hello");
=}
}
4 changes: 3 additions & 1 deletion test/C/src/DelayedAction.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ main reactor DelayedAction {
logical action a
state count: int = 0

reaction(t) -> a {= lf_schedule(a, MSEC(100)); =}
reaction(t) -> a {=
lf_schedule(a, MSEC(100));
=}

reaction(a) {=
interval_t elapsed = lf_time_logical_elapsed();
Expand Down
4 changes: 3 additions & 1 deletion test/C/src/DelayedReaction.lf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ reactor Source {
output out: int
timer t

reaction(t) -> out {= lf_set(out, 1); =}
reaction(t) -> out {=
lf_set(out, 1);
=}
}

reactor Sink {
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/Determinism.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ reactor Source {
output y: int
timer t

reaction(t) -> y {= lf_set(y, 1); =}
reaction(t) -> y {=
lf_set(y, 1);
=}
}

reactor Destination {
Expand All @@ -31,7 +33,9 @@ reactor Pass {
input x: int
output y: int

reaction(x) -> y {= lf_set(y, x->value); =}
reaction(x) -> y {=
lf_set(y, x->value);
=}
}

main reactor Determinism {
Expand Down
Loading

0 comments on commit 90967d4

Please sign in to comment.