|
16 | 16 | import liquidjava.rj_language.ast.FunctionInvocation; |
17 | 17 | import liquidjava.rj_language.ast.UnaryExpression; |
18 | 18 | import liquidjava.rj_language.ast.Var; |
| 19 | +import liquidjava.utils.Utils; |
| 20 | +import liquidjava.utils.constants.Formats; |
19 | 21 |
|
20 | 22 | /** |
21 | 23 | * Reads a state written as a value of its state set, {@code phase(e) == open}, as the state shorthand {@code open(e)}, |
22 | 24 | * so that everything that reads states sees one form |
23 | 25 | */ |
24 | 26 | public final class StateValues { |
25 | 27 |
|
| 28 | + /** A name {@link Formats#STATE_SET} generates */ |
| 29 | + private static final String GENERATED_SET_NAME = "state\\d+"; |
| 30 | + |
26 | 31 | private final List<GhostState> states; |
27 | 32 | private final Predicate<String> isVariable; |
28 | 33 | private final BiPredicate<Expression, GhostFunction> appliesTo; |
@@ -90,6 +95,43 @@ private Optional<FunctionInvocation> stateOf(Expression set, Expression name) th |
90 | 95 | return Optional.empty(); |
91 | 96 | } |
92 | 97 |
|
| 98 | + /** |
| 99 | + * {@code expression} with each state of a named set, {@code open(e)}, written as a value of its set, |
| 100 | + * {@code phase(e) == open}, and {@code !open(e)} as {@code phase(e) != open}, for display. A state keeps the |
| 101 | + * shorthand when its set has a generated name, such as {@code state1}, or when sets with different names have a |
| 102 | + * state of that name |
| 103 | + */ |
| 104 | + public static Expression toSetForm(Expression expression, List<GhostState> states) { |
| 105 | + return rewriteToSetForm(expression.clone(), states); |
| 106 | + } |
| 107 | + |
| 108 | + private static Expression rewriteToSetForm(Expression e, List<GhostState> states) { |
| 109 | + if (e instanceof UnaryExpression u && u.getOp().equals("!")) { |
| 110 | + Optional<BinaryExpression> value = setValue(u.getExpression(), "!=", states); |
| 111 | + if (value.isPresent()) |
| 112 | + return value.get(); |
| 113 | + } |
| 114 | + Optional<BinaryExpression> value = setValue(e, "==", states); |
| 115 | + if (value.isPresent()) |
| 116 | + return value.get(); |
| 117 | + for (int i = 0; i < e.getChildren().size(); i++) |
| 118 | + e.setChild(i, rewriteToSetForm(e.getChildren().get(i), states)); |
| 119 | + return e; |
| 120 | + } |
| 121 | + |
| 122 | + /** {@code set(e) op state} when {@code e} is the state {@code state(e)} of a set with one, given name */ |
| 123 | + private static Optional<BinaryExpression> setValue(Expression e, String op, List<GhostState> states) { |
| 124 | + if (!(e instanceof FunctionInvocation invocation && invocation.getArgs().size() == 1)) |
| 125 | + return Optional.empty(); |
| 126 | + List<String> setNames = states.stream().filter(s -> s.getParent() != null && s.matches(invocation.getName())) |
| 127 | + .map(s -> s.getParent().getName()).distinct().toList(); |
| 128 | + if (setNames.size() != 1 || setNames.get(0).matches(GENERATED_SET_NAME)) |
| 129 | + return Optional.empty(); |
| 130 | + FunctionInvocation set = new FunctionInvocation(qualifiedLike(invocation.getName(), setNames.get(0)), |
| 131 | + List.of(invocation.getArgs().get(0).clone())); |
| 132 | + return Optional.of(new BinaryExpression(set, op, new Var(Utils.getSimpleName(invocation.getName())))); |
| 133 | + } |
| 134 | + |
93 | 135 | /** The states of {@code set}, or only those named {@code name} when it is not null */ |
94 | 136 | private List<GhostState> statesOf(GhostFunction set, String name) { |
95 | 137 | return states.stream().filter(s -> s.getParent() == set && (name == null || s.getName().equals(name))).toList(); |
|
0 commit comments