1
1
package com .relogiclabs .jschema .internal .engine ;
2
2
3
- import com .relogiclabs .jschema .internal .antlr .SchemaLexer ;
3
+ import com .relogiclabs .jschema .internal .antlr .SchemaParser ;
4
4
import com .relogiclabs .jschema .internal .script .GDouble ;
5
- import com .relogiclabs .jschema .internal .script .GFunction ;
6
5
import com .relogiclabs .jschema .internal .script .GInteger ;
6
+ import com .relogiclabs .jschema .internal .script .GLeftValue ;
7
7
import com .relogiclabs .jschema .internal .script .GObject ;
8
8
import com .relogiclabs .jschema .internal .script .GParameter ;
9
9
import com .relogiclabs .jschema .internal .script .GRange ;
10
- import com .relogiclabs .jschema .internal .script .GReference ;
11
10
import com .relogiclabs .jschema .tree .RuntimeContext ;
12
11
import com .relogiclabs .jschema .type .EArray ;
13
12
import com .relogiclabs .jschema .type .EBoolean ;
27
26
import java .util .stream .Collectors ;
28
27
29
28
import 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 ;
33
34
import static com .relogiclabs .jschema .internal .script .RFunction .hasVariadic ;
35
+ import static com .relogiclabs .jschema .internal .util .CommonHelper .hasFlag ;
34
36
import static com .relogiclabs .jschema .internal .util .StreamHelper .halt ;
35
37
import static java .util .stream .Collectors .toMap ;
36
38
@@ -39,7 +41,7 @@ public final class ScriptTreeHelper {
39
41
private static final String TRYOF_ERROR = "error" ;
40
42
41
43
private ScriptTreeHelper () {
42
- throw new UnsupportedOperationException ();
44
+ throw new UnsupportedOperationException ("This class is not intended for instantiation" );
43
45
}
44
46
45
47
public static boolean areEqual (EValue v1 , EValue v2 , RuntimeContext runtime ) {
@@ -55,7 +57,7 @@ public static boolean areEqual(EValue v1, EValue v2, RuntimeContext runtime) {
55
57
if (v1 instanceof EUndefined && v2 instanceof EUndefined ) return true ;
56
58
if (v1 instanceof EArray a1 && v2 instanceof EArray a2 ) {
57
59
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 ++)
59
61
if (!areEqual (a1 .get (i ), a2 .get (i ), runtime )) return false ;
60
62
return true ;
61
63
}
@@ -71,46 +73,44 @@ public static boolean areEqual(EValue v1, EValue v2, RuntimeContext runtime) {
71
73
}
72
74
73
75
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 ();
76
78
return value ;
77
79
}
78
80
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 );
81
86
}
82
87
83
88
private static int getFunctionMode (TerminalNode node ) {
84
89
if (node == null ) return 0 ;
85
90
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 ;
89
94
default -> 0 ;
90
95
};
91
96
}
92
97
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 );
101
100
}
102
101
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 ) ;
106
105
}
107
106
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 );
110
110
}
111
111
112
112
static GParameter [] toParameters (List <TerminalNode > identifiers ,
113
- TerminalNode ellipsis ) {
113
+ TerminalNode ellipsis ) {
114
114
identifiers .stream ().collect (toMap (ParseTree ::getText , Function .identity (),
115
115
(i1 , i2 ) -> halt (failOnDuplicateParameterName (i2 ))
116
116
));
@@ -122,8 +122,10 @@ static GParameter[] toParameters(List<TerminalNode> identifiers,
122
122
123
123
public static void areCompatible (GParameter [] parameters , List <EValue > arguments , String code ) {
124
124
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 );
127
129
}
128
130
129
131
public static String stringify (Object object ) {
@@ -132,9 +134,9 @@ public static String stringify(Object object) {
132
134
}
133
135
134
136
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 );
138
140
return object ;
139
141
}
140
142
0 commit comments