33import java .util .List ;
44import java .util .Objects ;
55import java .util .Optional ;
6+ import java .util .function .BiPredicate ;
67import java .util .function .Predicate ;
78import java .util .stream .Collectors ;
89
2223 */
2324public final class StateValues {
2425
25- private StateValues () {
26+ private final List <GhostState > states ;
27+ private final Predicate <String > isVariable ;
28+ private final BiPredicate <Expression , GhostFunction > appliesTo ;
29+
30+ /**
31+ * @param isVariable
32+ * whether a name is a variable where the spec is written; a variable keeps its meaning
33+ * @param appliesTo
34+ * whether a state set can be applied to an argument, by the argument's type; true when it is not known
35+ */
36+ public StateValues (List <GhostState > states , Predicate <String > isVariable ,
37+ BiPredicate <Expression , GhostFunction > appliesTo ) {
38+ this .states = states ;
39+ this .isVariable = isVariable ;
40+ this .appliesTo = appliesTo ;
2641 }
2742
2843 /**
2944 * {@code expression} with each {@code set(e) == state} read as {@code state(e)} and each {@code set(e) != state} as
30- * {@code !state(e)}. A name {@code isVariable} accepts keeps its meaning as a variable
45+ * {@code !state(e)}
3146 *
3247 * @throws CustomError
3348 * when the name is not a variable and is a state of another set of the same class
3449 */
35- public static Expression toShorthand (Expression expression , List <GhostState > states , Predicate <String > isVariable )
36- throws LJError {
37- return rewrite (expression .clone (), states , isVariable );
50+ public Expression toShorthand (Expression expression ) throws LJError {
51+ return rewrite (expression .clone ());
3852 }
3953
40- private static Expression rewrite (Expression e , List <GhostState > states , Predicate <String > isVariable )
41- throws LJError {
54+ private Expression rewrite (Expression e ) throws LJError {
4255 if (e instanceof BinaryExpression b && (b .getOperator ().equals ("==" ) || b .getOperator ().equals ("!=" ))) {
43- Optional <FunctionInvocation > state = stateOf (b .getFirstOperand (), b .getSecondOperand (), states , isVariable );
56+ Optional <FunctionInvocation > state = stateOf (b .getFirstOperand (), b .getSecondOperand ());
4457 if (state .isEmpty ())
45- state = stateOf (b .getSecondOperand (), b .getFirstOperand (), states , isVariable );
58+ state = stateOf (b .getSecondOperand (), b .getFirstOperand ());
4659 if (state .isPresent ())
4760 return b .getOperator ().equals ("==" ) ? state .get () : new UnaryExpression ("!" , state .get ());
4861 }
4962 for (int i = 0 ; i < e .getChildren ().size (); i ++)
50- e .setChild (i , rewrite (e .getChildren ().get (i ), states , isVariable ));
63+ e .setChild (i , rewrite (e .getChildren ().get (i )));
5164 return e ;
5265 }
5366
5467 /**
55- * {@code state(e)} when {@code set} is a state set applied to {@code e} and {@code name} one of its states, named
56- * as the shorthand written in the same place, so that it is resolved the same way, in a subclass too
68+ * {@code state(e)} when {@code set} is a state set applied to {@code e} and {@code name} a state of every set of
69+ * that name that applies to {@code e}. It is named as the shorthand written in the same place, so that it is
70+ * resolved the same way, in a subclass too. When only some of those sets have the state, nothing is read, and the
71+ * name stays an unknown variable
5772 */
58- private static Optional <FunctionInvocation > stateOf (Expression set , Expression name , List <GhostState > states ,
59- Predicate <String > isVariable ) throws LJError {
73+ private Optional <FunctionInvocation > stateOf (Expression set , Expression name ) throws LJError {
6074 if (!(set instanceof FunctionInvocation invocation && invocation .getArgs ().size () == 1
6175 && name instanceof Var var && !isVariable .test (var .getName ())))
6276 return Optional .empty ();
77+ Expression argument = invocation .getArgs ().get (0 );
6378 List <GhostFunction > sets = states .stream ().map (GhostState ::getParent ).filter (Objects ::nonNull ).distinct ()
64- .filter (s -> s .matches (invocation .getName ())).toList ();
65- if (sets .stream ().anyMatch (s -> !statesOf (s , states , var .getName ()).isEmpty ()))
79+ .filter (s -> s .matches (invocation .getName ()) && appliesTo .test (argument , s )).toList ();
80+ long withState = sets .stream ().filter (s -> !statesOf (s , var .getName ()).isEmpty ()).count ();
81+ if (withState > 0 && withState == sets .size ())
6682 return Optional .of (new FunctionInvocation (qualifiedLike (invocation .getName (), var .getName ()),
67- List .of (invocation .getArgs ().get (0 ).clone ())));
68- for (GhostFunction s : sets )
69- if (isStateOfAnotherSet (var .getName (), s , states ))
70- throw new CustomError (String .format ("State '%s' is not a state of '%s' (its states are %s)" ,
71- var .getName (), s .getName (),
72- statesOf (s , states , null ).stream ().map (GhostState ::getName ).collect (Collectors .joining (", " ))));
83+ List .of (argument .clone ())));
84+ if (withState == 0 )
85+ for (GhostFunction s : sets )
86+ if (isStateOfAnotherSet (var .getName (), s ))
87+ throw new CustomError (String .format ("State '%s' is not a state of '%s' (its states are %s)" ,
88+ var .getName (), s .getName (),
89+ statesOf (s , null ).stream ().map (GhostState ::getName ).collect (Collectors .joining (", " ))));
7390 return Optional .empty ();
7491 }
7592
7693 /** The states of {@code set}, or only those named {@code name} when it is not null */
77- private static List <GhostState > statesOf (GhostFunction set , List < GhostState > states , String name ) {
94+ private List <GhostState > statesOf (GhostFunction set , String name ) {
7895 return states .stream ().filter (s -> s .getParent () == set && (name == null || s .getName ().equals (name ))).toList ();
7996 }
8097
@@ -84,7 +101,7 @@ private static String qualifiedLike(String setName, String state) {
84101 return dot < 0 ? state : setName .substring (0 , dot + 1 ) + state ;
85102 }
86103
87- private static boolean isStateOfAnotherSet (String name , GhostFunction stateSet , List < GhostState > states ) {
104+ private boolean isStateOfAnotherSet (String name , GhostFunction stateSet ) {
88105 return states .stream ().anyMatch (s -> s .getName ().equals (name ) && s .getParent () != null
89106 && s .getParent () != stateSet && s .getParent ().getPrefix ().equals (stateSet .getPrefix ()));
90107 }
0 commit comments