1
1
package com .relogiclabs .jschema .internal .script ;
2
2
3
3
import com .relogiclabs .jschema .internal .engine .Evaluator ;
4
- import com .relogiclabs .jschema .internal .engine .ScopeContext ;
4
+ import com .relogiclabs .jschema .internal .engine .ScriptScope ;
5
5
import com .relogiclabs .jschema .type .EValue ;
6
6
import lombok .Getter ;
7
7
import lombok .RequiredArgsConstructor ;
11
11
import static com .relogiclabs .jschema .internal .engine .ScriptTreeHelper .areCompatible ;
12
12
import static com .relogiclabs .jschema .internal .script .RFunction .hasVariadic ;
13
13
import static com .relogiclabs .jschema .internal .util .CollectionHelper .subList ;
14
- import static com .relogiclabs .jschema .internal .util .MiscellaneousHelper .hasFlag ;
15
- import static com .relogiclabs .jschema .message .ErrorCode .FUNS05 ;
14
+ import static com .relogiclabs .jschema .internal .util .CommonHelper .hasFlag ;
15
+ import static com .relogiclabs .jschema .message .ErrorCode .FNVK02 ;
16
16
17
17
@ Getter
18
18
@ RequiredArgsConstructor
19
19
public final class GFunction implements RFunction {
20
- public static final String CONSTRAINT_PREFIX = "@" ;
21
20
public static final int CONSTRAINT_MODE = 1 ;
22
21
public static final int FUTURE_MODE = 3 ;
23
22
public static final int SUBROUTINE_MODE = 4 ;
@@ -35,33 +34,35 @@ public GFunction(GParameter[] parameters, Evaluator body, int mode) {
35
34
}
36
35
37
36
@ Override
38
- public EValue invoke (ScopeContext functionScope , List <EValue > arguments ) {
37
+ public ScriptScope bind (ScriptScope parentScope , List <EValue > arguments ) {
38
+ areCompatible (parameters , arguments , FNVK02 );
39
+ var scope = new ScriptScope (parentScope );
40
+ var i = 0 ;
41
+ for (var p : parameters ) scope .addVariable (p .getName (), p .isVariadic ()
42
+ ? new GArray (subList (arguments , i )) : arguments .get (i ++));
43
+ return scope ;
44
+ }
45
+
46
+ @ Override
47
+ public EValue invoke (ScriptScope functionScope , List <EValue > arguments ) {
39
48
return invoke (functionScope );
40
49
}
41
50
42
- public EValue invoke (ScopeContext functionScope ) {
51
+ public EValue invoke (ScriptScope functionScope ) {
43
52
var result = getBody ().evaluate (functionScope );
44
53
if (result instanceof GControl ctrl ) return ctrl .getValue ();
45
54
return VOID ;
46
55
}
47
56
48
- @ Override
49
- public ScopeContext bind (ScopeContext parentScope , List <EValue > arguments ) {
50
- areCompatible (parameters , arguments , FUNS05 );
51
- var scope = new ScopeContext (parentScope );
52
- var i = 0 ;
53
- for (var p : parameters ) {
54
- if (p .isVariadic ()) scope .addVariable (p .getName (), new GArray (subList (arguments , i )));
55
- else scope .addVariable (p .getName (), arguments .get (i ++));
56
- }
57
- return scope ;
57
+ public boolean isConstraint () {
58
+ return hasFlag (mode , CONSTRAINT_MODE );
58
59
}
59
60
60
61
public boolean isFuture () {
61
62
return hasFlag (mode , FUTURE_MODE );
62
63
}
63
64
64
- public static boolean isConstraint ( int mode ) {
65
- return hasFlag (mode , CONSTRAINT_MODE );
65
+ public boolean isSubroutine ( ) {
66
+ return hasFlag (mode , SUBROUTINE_MODE );
66
67
}
67
68
}
0 commit comments