11package com .relogiclabs .jschema .internal .engine ;
22
3- import com .relogiclabs .jschema .internal .antlr .SchemaLexer ;
3+ import com .relogiclabs .jschema .internal .antlr .SchemaParser ;
44import com .relogiclabs .jschema .internal .script .GDouble ;
5- import com .relogiclabs .jschema .internal .script .GFunction ;
65import com .relogiclabs .jschema .internal .script .GInteger ;
6+ import com .relogiclabs .jschema .internal .script .GLeftValue ;
77import com .relogiclabs .jschema .internal .script .GObject ;
88import com .relogiclabs .jschema .internal .script .GParameter ;
99import com .relogiclabs .jschema .internal .script .GRange ;
10- import com .relogiclabs .jschema .internal .script .GReference ;
1110import com .relogiclabs .jschema .tree .RuntimeContext ;
1211import com .relogiclabs .jschema .type .EArray ;
1312import com .relogiclabs .jschema .type .EBoolean ;
2726import java .util .stream .Collectors ;
2827
2928import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnDuplicateParameterName ;
30- import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnFixedArgument ;
31- import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnVariadicArgument ;
32- import static com .relogiclabs .jschema .internal .script .GFunction .CONSTRAINT_PREFIX ;
29+ import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnFixedArity ;
30+ import static com .relogiclabs .jschema .internal .engine .ScriptErrorHelper .failOnVariadicArity ;
31+ import static com .relogiclabs .jschema .internal .script .GFunction .CONSTRAINT_MODE ;
32+ import static com .relogiclabs .jschema .internal .script .GFunction .FUTURE_MODE ;
33+ import static com .relogiclabs .jschema .internal .script .GFunction .SUBROUTINE_MODE ;
3334import static com .relogiclabs .jschema .internal .script .RFunction .hasVariadic ;
35+ import static com .relogiclabs .jschema .internal .util .CommonHelper .hasFlag ;
3436import static com .relogiclabs .jschema .internal .util .StreamHelper .halt ;
3537import static java .util .stream .Collectors .toMap ;
3638
@@ -39,7 +41,7 @@ public final class ScriptTreeHelper {
3941 private static final String TRYOF_ERROR = "error" ;
4042
4143 private ScriptTreeHelper () {
42- throw new UnsupportedOperationException ();
44+ throw new UnsupportedOperationException ("This class is not intended for instantiation" );
4345 }
4446
4547 public static boolean areEqual (EValue v1 , EValue v2 , RuntimeContext runtime ) {
@@ -55,7 +57,7 @@ public static boolean areEqual(EValue v1, EValue v2, RuntimeContext runtime) {
5557 if (v1 instanceof EUndefined && v2 instanceof EUndefined ) return true ;
5658 if (v1 instanceof EArray a1 && v2 instanceof EArray a2 ) {
5759 if (a1 .size () != a2 .size ()) return false ;
58- for (int i = 0 ; i < a1 .size (); i ++)
60+ for (var i = 0 ; i < a1 .size (); i ++)
5961 if (!areEqual (a1 .get (i ), a2 .get (i ), runtime )) return false ;
6062 return true ;
6163 }
@@ -71,46 +73,44 @@ public static boolean areEqual(EValue v1, EValue v2, RuntimeContext runtime) {
7173 }
7274
7375 public static EValue dereference (EValue value ) {
74- while (value instanceof GReference reference )
75- value = reference .getValue ();
76+ while (value instanceof GLeftValue lvalue )
77+ value = lvalue .getValue ();
7678 return value ;
7779 }
7880
79- static int getFunctionMode (TerminalNode constraint , TerminalNode future , TerminalNode subroutine ) {
80- return getFunctionMode (constraint ) | getFunctionMode (future ) | getFunctionMode (subroutine );
81+ static int getFunctionMode (TerminalNode constraint , TerminalNode future ,
82+ TerminalNode subroutine ) {
83+ return getFunctionMode (constraint )
84+ | getFunctionMode (future )
85+ | getFunctionMode (subroutine );
8186 }
8287
8388 private static int getFunctionMode (TerminalNode node ) {
8489 if (node == null ) return 0 ;
8590 return switch (node .getSymbol ().getType ()) {
86- case SchemaLexer .G_CONSTRAINT -> GFunction . CONSTRAINT_MODE ;
87- case SchemaLexer .G_FUTURE -> GFunction . FUTURE_MODE ;
88- case SchemaLexer .G_SUBROUTINE -> GFunction . SUBROUTINE_MODE ;
91+ case SchemaParser .G_CONSTRAINT -> CONSTRAINT_MODE ;
92+ case SchemaParser .G_FUTURE -> FUTURE_MODE ;
93+ case SchemaParser .G_SUBROUTINE -> SUBROUTINE_MODE ;
8994 default -> 0 ;
9095 };
9196 }
9297
93- static ENumber increment (ENumber number ) {
94- if (number instanceof EInteger i ) return GInteger .of (i .getValue () + 1 );
95- return GDouble .of (number .toDouble () + 1 );
96- }
97-
98- static ENumber decrement (ENumber number ) {
99- if (number instanceof EInteger i ) return GInteger .of (i .getValue () - 1 );
100- return GDouble .of (number .toDouble () - 1 );
98+ public static boolean isConstraint (int mode ) {
99+ return hasFlag (mode , CONSTRAINT_MODE );
101100 }
102101
103- static String formatFunctionName ( String baseName , GParameter [] parameters ) {
104- if (hasVariadic ( parameters )) return baseName + "#..." ;
105- return baseName + "#" + parameters . length ;
102+ static ENumber increment ( ENumber number ) {
103+ if (number instanceof EInteger i ) return GInteger . from ( i . getValue () + 1 ) ;
104+ return GDouble . from ( number . toDouble () + 1 ) ;
106105 }
107106
108- static String toConstraintName (String functionName ) {
109- return CONSTRAINT_PREFIX .concat (functionName );
107+ static ENumber decrement (ENumber number ) {
108+ if (number instanceof EInteger i ) return GInteger .from (i .getValue () - 1 );
109+ return GDouble .from (number .toDouble () - 1 );
110110 }
111111
112112 static GParameter [] toParameters (List <TerminalNode > identifiers ,
113- TerminalNode ellipsis ) {
113+ TerminalNode ellipsis ) {
114114 identifiers .stream ().collect (toMap (ParseTree ::getText , Function .identity (),
115115 (i1 , i2 ) -> halt (failOnDuplicateParameterName (i2 ))
116116 ));
@@ -122,8 +122,10 @@ static GParameter[] toParameters(List<TerminalNode> identifiers,
122122
123123 public static void areCompatible (GParameter [] parameters , List <EValue > arguments , String code ) {
124124 if (hasVariadic (parameters )) {
125- if (arguments .size () < parameters .length - 1 ) throw failOnVariadicArgument (code );
126- } else if (arguments .size () != parameters .length ) throw failOnFixedArgument (code );
125+ if (arguments .size () < parameters .length - 1 ) throw failOnVariadicArity (code );
126+ return ;
127+ }
128+ if (arguments .size () != parameters .length ) throw failOnFixedArity (code );
127129 }
128130
129131 public static String stringify (Object object ) {
@@ -132,9 +134,9 @@ public static String stringify(Object object) {
132134 }
133135
134136 static GObject createTryofMonad (EValue value , EValue error ) {
135- GObject object = new GObject (2 );
136- object .set (TRYOF_VALUE , value );
137- object .set (TRYOF_ERROR , error );
137+ var object = new GObject (2 );
138+ object .put (TRYOF_VALUE , value );
139+ object .put (TRYOF_ERROR , error );
138140 return object ;
139141 }
140142
0 commit comments