affinirum 1.5.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cache/arrow-preview-vWvB5K/affinirum-arrow-functions.patch +155 -0
- package/.cache/function-syntax-tests.log +20 -0
- package/README.md +925 -355
- package/dst/Affinirum.d.ts +29 -19
- package/dst/Affinirum.js +399 -334
- package/dst/CallFrame.d.ts +12 -3
- package/dst/CallFrame.js +49 -10
- package/dst/Constant.d.ts +1 -3
- package/dst/Constant.js +3 -8
- package/dst/LexicalScope.d.ts +11 -9
- package/dst/LexicalScope.js +10 -6
- package/dst/ParserSpan.d.ts +11 -0
- package/dst/{ScriptSpan.js → ParserSpan.js} +8 -5
- package/dst/ParserState.d.ts +28 -49
- package/dst/ParserState.js +256 -489
- package/dst/Type.d.ts +34 -17
- package/dst/Type.js +211 -115
- package/dst/TypeParser.d.ts +9 -0
- package/dst/TypeParser.js +127 -0
- package/dst/Value.d.ts +1 -1
- package/dst/Value.js +21 -10
- package/dst/Variable.d.ts +16 -5
- package/dst/Variable.js +32 -7
- package/dst/atom/ArrayAtom.d.ts +10 -10
- package/dst/atom/ArrayAtom.js +62 -55
- package/dst/atom/Atom.d.ts +7 -0
- package/dst/atom/FunctionAtom.d.ts +13 -15
- package/dst/atom/FunctionAtom.js +53 -104
- package/dst/atom/FunctionSignature.d.ts +23 -0
- package/dst/atom/FunctionSignature.js +124 -0
- package/dst/atom/ObjectAtom.d.ts +11 -9
- package/dst/atom/ObjectAtom.js +73 -33
- package/dst/atom/PrimitiveAtom.d.ts +6 -7
- package/dst/atom/PrimitiveAtom.js +7 -10
- package/dst/cjs/Affinirum.d.ts +29 -19
- package/dst/cjs/Affinirum.js +398 -333
- package/dst/cjs/CallFrame.d.ts +12 -3
- package/dst/cjs/CallFrame.js +49 -10
- package/dst/cjs/Constant.d.ts +1 -3
- package/dst/cjs/Constant.js +3 -8
- package/dst/cjs/LexicalScope.d.ts +11 -9
- package/dst/cjs/LexicalScope.js +10 -6
- package/dst/cjs/ParserSpan.d.ts +11 -0
- package/dst/cjs/{ScriptSpan.js → ParserSpan.js} +10 -7
- package/dst/cjs/ParserState.d.ts +28 -49
- package/dst/cjs/ParserState.js +255 -488
- package/dst/cjs/Type.d.ts +34 -17
- package/dst/cjs/Type.js +211 -115
- package/dst/cjs/TypeParser.d.ts +9 -0
- package/dst/cjs/TypeParser.js +131 -0
- package/dst/cjs/Value.d.ts +1 -1
- package/dst/cjs/Value.js +21 -10
- package/dst/cjs/Variable.d.ts +16 -5
- package/dst/cjs/Variable.js +36 -9
- package/dst/cjs/atom/ArrayAtom.d.ts +10 -10
- package/dst/cjs/atom/ArrayAtom.js +62 -55
- package/dst/cjs/atom/Atom.d.ts +7 -0
- package/dst/cjs/atom/FunctionAtom.d.ts +13 -15
- package/dst/cjs/atom/FunctionAtom.js +54 -105
- package/dst/cjs/atom/FunctionSignature.d.ts +23 -0
- package/dst/cjs/atom/FunctionSignature.js +128 -0
- package/dst/cjs/atom/ObjectAtom.d.ts +11 -9
- package/dst/cjs/atom/ObjectAtom.js +73 -33
- package/dst/cjs/atom/PrimitiveAtom.d.ts +6 -7
- package/dst/cjs/atom/PrimitiveAtom.js +7 -10
- package/dst/cjs/dictionary/Constants.d.ts +2 -0
- package/dst/cjs/dictionary/Constants.js +27 -0
- package/dst/cjs/dictionary/Functions.d.ts +2 -0
- package/dst/cjs/{Functions.js → dictionary/Functions.js} +27 -25
- package/dst/cjs/native/Aggregable.js +10 -2
- package/dst/cjs/native/Array.d.ts +2 -3
- package/dst/cjs/native/Array.js +38 -39
- package/dst/cjs/native/Boolean.d.ts +1 -1
- package/dst/cjs/native/Boolean.js +13 -10
- package/dst/cjs/native/Buffer.d.ts +5 -5
- package/dst/cjs/native/Buffer.js +70 -46
- package/dst/cjs/native/Composite.d.ts +5 -0
- package/dst/cjs/native/Composite.js +35 -0
- package/dst/cjs/native/Enumerable.js +20 -8
- package/dst/cjs/native/Float.d.ts +3 -1
- package/dst/cjs/native/Float.js +51 -12
- package/dst/cjs/native/Integer.d.ts +4 -2
- package/dst/cjs/native/Integer.js +60 -25
- package/dst/cjs/native/Iterable.d.ts +0 -2
- package/dst/cjs/native/Iterable.js +9 -28
- package/dst/cjs/native/Number.d.ts +0 -2
- package/dst/cjs/native/Number.js +34 -48
- package/dst/cjs/native/Object.d.ts +1 -1
- package/dst/cjs/native/Object.js +5 -6
- package/dst/cjs/native/String.d.ts +7 -6
- package/dst/cjs/native/String.js +28 -27
- package/dst/cjs/native/Timestamp.d.ts +3 -3
- package/dst/cjs/native/Timestamp.js +34 -36
- package/dst/cjs/native/Unknown.d.ts +3 -3
- package/dst/cjs/native/Unknown.js +48 -17
- package/dst/cjs/native/notation/AN.d.ts +1 -1
- package/dst/cjs/native/notation/AN.js +37 -35
- package/dst/cjs/native/notation/IP.d.ts +1 -1
- package/dst/cjs/native/notation/IP.js +11 -12
- package/dst/cjs/native/notation/JSON.d.ts +1 -1
- package/dst/cjs/native/notation/JSON.js +7 -6
- package/dst/cjs/node/AccessNode.d.ts +24 -0
- package/dst/cjs/node/AccessNode.js +193 -0
- package/dst/cjs/node/ArrayNode.d.ts +7 -4
- package/dst/cjs/node/ArrayNode.js +26 -15
- package/dst/cjs/node/AssignableNode.d.ts +16 -0
- package/dst/cjs/node/AssignableNode.js +18 -0
- package/dst/cjs/node/AssignmentNode.d.ts +20 -0
- package/dst/cjs/node/AssignmentNode.js +66 -0
- package/dst/cjs/node/BlockNode.d.ts +5 -4
- package/dst/cjs/node/BlockNode.js +18 -11
- package/dst/cjs/node/CallNode.d.ts +7 -5
- package/dst/cjs/node/CallNode.js +40 -41
- package/dst/cjs/node/CastNode.d.ts +5 -6
- package/dst/cjs/node/CastNode.js +16 -17
- package/dst/cjs/node/ConstantNode.d.ts +4 -6
- package/dst/cjs/node/ConstantNode.js +9 -17
- package/dst/cjs/node/FinalNode.d.ts +19 -0
- package/dst/cjs/node/FinalNode.js +41 -0
- package/dst/cjs/node/FunctionNode.d.ts +17 -0
- package/dst/cjs/node/FunctionNode.js +51 -0
- package/dst/cjs/node/InvocationNode.d.ts +5 -0
- package/dst/cjs/node/InvocationNode.js +34 -0
- package/dst/cjs/node/JumpNode.d.ts +7 -9
- package/dst/cjs/node/JumpNode.js +12 -19
- package/dst/cjs/node/LoopNode.d.ts +7 -5
- package/dst/cjs/node/LoopNode.js +33 -22
- package/dst/cjs/node/MatchNode.d.ts +17 -0
- package/dst/cjs/node/MatchNode.js +65 -0
- package/dst/cjs/node/Node.d.ts +14 -0
- package/dst/cjs/{Node.js → node/Node.js} +17 -11
- package/dst/cjs/node/ObjectNode.d.ts +7 -4
- package/dst/cjs/node/ObjectNode.js +42 -11
- package/dst/cjs/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
- package/dst/cjs/node/TernaryNode.js +45 -0
- package/dst/cjs/node/VariableNode.d.ts +10 -6
- package/dst/cjs/node/VariableNode.js +50 -16
- package/dst/dictionary/Constants.d.ts +2 -0
- package/dst/dictionary/Constants.js +24 -0
- package/dst/dictionary/Functions.d.ts +2 -0
- package/dst/{Functions.js → dictionary/Functions.js} +26 -24
- package/dst/native/Aggregable.js +11 -3
- package/dst/native/Array.d.ts +2 -3
- package/dst/native/Array.js +37 -38
- package/dst/native/Boolean.d.ts +1 -1
- package/dst/native/Boolean.js +12 -9
- package/dst/native/Buffer.d.ts +5 -5
- package/dst/native/Buffer.js +66 -42
- package/dst/native/Composite.d.ts +5 -0
- package/dst/native/Composite.js +32 -0
- package/dst/native/Enumerable.js +20 -8
- package/dst/native/Float.d.ts +3 -1
- package/dst/native/Float.js +48 -11
- package/dst/native/Integer.d.ts +4 -2
- package/dst/native/Integer.js +56 -23
- package/dst/native/Iterable.d.ts +0 -2
- package/dst/native/Iterable.js +8 -27
- package/dst/native/Number.d.ts +0 -2
- package/dst/native/Number.js +34 -46
- package/dst/native/Object.d.ts +1 -1
- package/dst/native/Object.js +4 -5
- package/dst/native/String.d.ts +7 -6
- package/dst/native/String.js +24 -24
- package/dst/native/Timestamp.d.ts +3 -3
- package/dst/native/Timestamp.js +31 -33
- package/dst/native/Unknown.d.ts +3 -3
- package/dst/native/Unknown.js +49 -18
- package/dst/native/notation/AN.d.ts +1 -1
- package/dst/native/notation/AN.js +37 -35
- package/dst/native/notation/IP.d.ts +1 -1
- package/dst/native/notation/IP.js +10 -11
- package/dst/native/notation/JSON.d.ts +1 -1
- package/dst/native/notation/JSON.js +6 -5
- package/dst/node/AccessNode.d.ts +24 -0
- package/dst/node/AccessNode.js +189 -0
- package/dst/node/ArrayNode.d.ts +7 -4
- package/dst/node/ArrayNode.js +26 -15
- package/dst/node/AssignableNode.d.ts +16 -0
- package/dst/node/AssignableNode.js +14 -0
- package/dst/node/AssignmentNode.d.ts +20 -0
- package/dst/node/AssignmentNode.js +62 -0
- package/dst/node/BlockNode.d.ts +5 -4
- package/dst/node/BlockNode.js +18 -11
- package/dst/node/CallNode.d.ts +7 -5
- package/dst/node/CallNode.js +40 -41
- package/dst/node/CastNode.d.ts +5 -6
- package/dst/node/CastNode.js +16 -17
- package/dst/node/ConstantNode.d.ts +4 -6
- package/dst/node/ConstantNode.js +9 -17
- package/dst/node/FinalNode.d.ts +19 -0
- package/dst/node/FinalNode.js +37 -0
- package/dst/node/FunctionNode.d.ts +17 -0
- package/dst/node/FunctionNode.js +47 -0
- package/dst/node/InvocationNode.d.ts +5 -0
- package/dst/node/InvocationNode.js +30 -0
- package/dst/node/JumpNode.d.ts +7 -9
- package/dst/node/JumpNode.js +12 -19
- package/dst/node/LoopNode.d.ts +7 -5
- package/dst/node/LoopNode.js +33 -22
- package/dst/node/MatchNode.d.ts +17 -0
- package/dst/node/MatchNode.js +61 -0
- package/dst/node/Node.d.ts +14 -0
- package/dst/node/Node.js +28 -0
- package/dst/node/ObjectNode.d.ts +7 -4
- package/dst/node/ObjectNode.js +42 -11
- package/dst/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
- package/dst/node/TernaryNode.js +41 -0
- package/dst/node/VariableNode.d.ts +10 -6
- package/dst/node/VariableNode.js +50 -16
- package/package.json +4 -4
- package/dst/Atom.d.ts +0 -7
- package/dst/Constants.d.ts +0 -2
- package/dst/Constants.js +0 -24
- package/dst/Functions.d.ts +0 -2
- package/dst/Jump.d.ts +0 -1
- package/dst/Jump.js +0 -1
- package/dst/JumpException.d.ts +0 -9
- package/dst/JumpException.js +0 -14
- package/dst/Keywords.d.ts +0 -1
- package/dst/Keywords.js +0 -17
- package/dst/Node.d.ts +0 -14
- package/dst/Node.js +0 -22
- package/dst/ScriptSpan.d.ts +0 -10
- package/dst/cjs/Atom.d.ts +0 -7
- package/dst/cjs/Constants.d.ts +0 -2
- package/dst/cjs/Constants.js +0 -27
- package/dst/cjs/Functions.d.ts +0 -2
- package/dst/cjs/Jump.d.ts +0 -1
- package/dst/cjs/Jump.js +0 -2
- package/dst/cjs/JumpException.d.ts +0 -9
- package/dst/cjs/JumpException.js +0 -18
- package/dst/cjs/Keywords.d.ts +0 -1
- package/dst/cjs/Keywords.js +0 -20
- package/dst/cjs/Node.d.ts +0 -14
- package/dst/cjs/ScriptSpan.d.ts +0 -10
- package/dst/cjs/node/SwitchNode.js +0 -49
- package/dst/cjs/node/VariadicNode.d.ts +0 -4
- package/dst/cjs/node/VariadicNode.js +0 -10
- package/dst/node/SwitchNode.js +0 -45
- package/dst/node/VariadicNode.d.ts +0 -4
- package/dst/node/VariadicNode.js +0 -6
- /package/dst/{Atom.js → atom/Atom.js} +0 -0
- /package/dst/cjs/{Atom.js → atom/Atom.js} +0 -0
package/dst/Affinirum.js
CHANGED
|
@@ -1,52 +1,59 @@
|
|
|
1
|
-
import { parseAN, formatAN } from "./native/notation/AN.js";
|
|
2
|
-
import { funcOr, funcAnd, funcNot } from "./native/Boolean.js";
|
|
3
|
-
import { funcAdd } from "./native/Aggregable.js";
|
|
4
|
-
import { funcAt, funcHas } from "./native/Iterable.js";
|
|
5
|
-
import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcPower, funcNegate } from "./native/Number.js";
|
|
6
|
-
import { funcCoalesce, funcEqual, funcNotEqual } from "./native/Unknown.js";
|
|
7
|
-
import { Constant } from "./Constant.js";
|
|
8
|
-
import { Variable } from "./Variable.js";
|
|
9
|
-
import { normalize } from "./Value.js";
|
|
10
1
|
import { Type } from "./Type.js";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
2
|
+
import { normalize } from "./Value.js";
|
|
3
|
+
import { Constant } from "./Constant.js";
|
|
4
|
+
import { LocalVariable, InputVariable } from "./Variable.js";
|
|
5
|
+
import { CallFrame } from "./CallFrame.js";
|
|
6
|
+
import { ParserState, keywords } from "./ParserState.js";
|
|
7
|
+
import { LexicalScope } from "./LexicalScope.js";
|
|
8
|
+
import { TypeParser } from "./TypeParser.js";
|
|
14
9
|
import { ArrayNode } from "./node/ArrayNode.js";
|
|
10
|
+
import { AssignmentNode } from "./node/AssignmentNode.js";
|
|
11
|
+
import { AssignableNode } from "./node/AssignableNode.js";
|
|
12
|
+
import { AccessNode } from "./node/AccessNode.js";
|
|
15
13
|
import { BlockNode } from "./node/BlockNode.js";
|
|
16
14
|
import { CallNode } from "./node/CallNode.js";
|
|
17
15
|
import { CastNode } from "./node/CastNode.js";
|
|
18
16
|
import { ConstantNode } from "./node/ConstantNode.js";
|
|
19
|
-
import {
|
|
17
|
+
import { FunctionNode } from "./node/FunctionNode.js";
|
|
18
|
+
import { TernaryNode } from "./node/TernaryNode.js";
|
|
19
|
+
import { MatchNode } from "./node/MatchNode.js";
|
|
20
20
|
import { LoopNode } from "./node/LoopNode.js";
|
|
21
|
+
import { JumpNode } from "./node/JumpNode.js";
|
|
22
|
+
import { FinalNode } from "./node/FinalNode.js";
|
|
21
23
|
import { ObjectNode } from "./node/ObjectNode.js";
|
|
22
|
-
import { SwitchNode } from "./node/SwitchNode.js";
|
|
23
24
|
import { VariableNode } from "./node/VariableNode.js";
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
25
|
+
import { constants } from "./dictionary/Constants.js";
|
|
26
|
+
import { functions } from "./dictionary/Functions.js";
|
|
27
|
+
import { funcNot } from "./native/Boolean.js";
|
|
28
|
+
import { funcNegate } from "./native/Number.js";
|
|
29
|
+
import { parseAN, formatAN } from "./native/notation/AN.js";
|
|
28
30
|
export class Affinirum {
|
|
29
|
-
static keywords = [...
|
|
31
|
+
static keywords = [...keywords, ...constants.map((c) => c[0])];
|
|
30
32
|
_script;
|
|
31
33
|
_strict;
|
|
34
|
+
_timeout;
|
|
35
|
+
_scope;
|
|
32
36
|
_root;
|
|
37
|
+
_type = new TypeParser();
|
|
33
38
|
_vartypes = new Map();
|
|
34
39
|
_varspans = new Map();
|
|
35
40
|
_variables = new Map();
|
|
36
|
-
_constants = new Map(
|
|
37
|
-
_functions = new Map(
|
|
38
|
-
|
|
41
|
+
_constants = new Map(constants);
|
|
42
|
+
_functions = new Map(functions);
|
|
43
|
+
_results = [];
|
|
39
44
|
/**
|
|
40
|
-
Creates compiled expression. Any parsed
|
|
45
|
+
Creates compiled expression. Any parsed identifier not recognized as a constant or a function will be compiled as a variable.
|
|
41
46
|
@param script Math expression to compile.
|
|
42
|
-
@param config Optional expected type, strict mode,
|
|
47
|
+
@param config Optional expected type, strict mode, default evaluation timeout in milliseconds, and variable types.
|
|
43
48
|
If expected type is provided then expression return type is matched against it.
|
|
44
49
|
If strict mode is set then undeclared variables will not be allowed in expression.
|
|
50
|
+
The default timeout is zero, which disables the timeout.
|
|
45
51
|
*/
|
|
46
52
|
constructor(script, config) {
|
|
47
53
|
this._script = script;
|
|
48
|
-
this._scope = new LexicalScope(undefined, undefined, config?.type
|
|
54
|
+
this._scope = new LexicalScope(undefined, undefined, config?.type);
|
|
49
55
|
this._strict = config?.strict ?? false;
|
|
56
|
+
this._timeout = !config?.timeout || config.timeout < 0 ? 0 : config.timeout;
|
|
50
57
|
if (config?.variables) {
|
|
51
58
|
for (const [vname, vtype] of Object.entries(config.variables)) {
|
|
52
59
|
this._vartypes.set(vname, vtype);
|
|
@@ -54,10 +61,10 @@ export class Affinirum {
|
|
|
54
61
|
}
|
|
55
62
|
const state = new ParserState(this._script);
|
|
56
63
|
this._root = this._list(state.next(), this._scope);
|
|
57
|
-
if (!state.
|
|
64
|
+
if (!state.isEnded) {
|
|
58
65
|
state.throwError("unexpected expression token or expression end");
|
|
59
66
|
}
|
|
60
|
-
this._root = this._root.compile(this._scope.type
|
|
67
|
+
this._root = this._root.compile(this._scope.type);
|
|
61
68
|
}
|
|
62
69
|
/**
|
|
63
70
|
Returns original script text.
|
|
@@ -69,7 +76,7 @@ export class Affinirum {
|
|
|
69
76
|
Returns compiled expression return value type.
|
|
70
77
|
*/
|
|
71
78
|
get type() {
|
|
72
|
-
return this._root.type;
|
|
79
|
+
return Type.union(this._root.type, ...this._results.map((i) => i.valueType));
|
|
73
80
|
}
|
|
74
81
|
/**
|
|
75
82
|
Returns string representing parsed node tree structure.
|
|
@@ -79,8 +86,7 @@ export class Affinirum {
|
|
|
79
86
|
return this._root.toString();
|
|
80
87
|
}
|
|
81
88
|
/**
|
|
82
|
-
Returns
|
|
83
|
-
@returns Record with variable names and types.
|
|
89
|
+
Returns the configured or inferred type of each referenced input variable.
|
|
84
90
|
*/
|
|
85
91
|
variables() {
|
|
86
92
|
const types = Object.create(null);
|
|
@@ -92,35 +98,44 @@ export class Affinirum {
|
|
|
92
98
|
/**
|
|
93
99
|
Evaluates the compiled expression using the provided input values.
|
|
94
100
|
@param values Values for the expression's input variables. Omitted when there are no input variables.
|
|
101
|
+
@param timeout Evaluation timeout in milliseconds. Omitted or non-positive values fallback to the configured default.
|
|
95
102
|
@returns The evaluated result.
|
|
96
103
|
@throws If an input value is missing or evaluation fails.
|
|
97
104
|
*/
|
|
98
|
-
evaluate(values) {
|
|
99
|
-
const
|
|
105
|
+
evaluate(values, timeout) {
|
|
106
|
+
const timer = timeout && timeout >= 0
|
|
107
|
+
? timeout
|
|
108
|
+
: this._timeout;
|
|
109
|
+
const stop = timer ? new Date(new Date().getTime() + timer) : undefined;
|
|
110
|
+
const frame = new CallFrame(stop, this._scope.variables());
|
|
100
111
|
try {
|
|
112
|
+
const composites = new WeakMap();
|
|
101
113
|
for (const [name, variable] of this._variables) {
|
|
102
114
|
if (!values || !Object.hasOwn(values, name)) {
|
|
103
115
|
this._varspans.get(name)?.throwError(`undefined variable ${name}:\n`);
|
|
104
116
|
}
|
|
105
|
-
const value = normalize(values?.[name] ?? undefined);
|
|
106
|
-
if (!variable.type.
|
|
117
|
+
const value = normalize(values?.[name] ?? undefined, composites);
|
|
118
|
+
if (!variable.type.acceptsValue(value)) {
|
|
107
119
|
this._varspans.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
|
|
108
120
|
}
|
|
109
121
|
frame.add(variable, value);
|
|
110
122
|
}
|
|
111
|
-
return
|
|
123
|
+
return this._root.evaluate(frame) ?? undefined;
|
|
112
124
|
}
|
|
113
125
|
catch (e) {
|
|
114
|
-
if (e instanceof
|
|
115
|
-
if (e.
|
|
116
|
-
return
|
|
126
|
+
if (e instanceof FinalNode) {
|
|
127
|
+
if (e.level === "result") {
|
|
128
|
+
return e.value ?? undefined;
|
|
117
129
|
}
|
|
118
130
|
else {
|
|
119
|
-
throw this._root.throwError(`unexpected ${e.
|
|
131
|
+
throw this._root.throwError(`unexpected ${e.level} return`);
|
|
120
132
|
}
|
|
121
133
|
}
|
|
122
134
|
throw e;
|
|
123
135
|
}
|
|
136
|
+
finally {
|
|
137
|
+
frame.clearTimer();
|
|
138
|
+
}
|
|
124
139
|
}
|
|
125
140
|
/** Parses an Affinirum Notation string into a JavaScript value.
|
|
126
141
|
@param value Affinirum Notation string to parse.
|
|
@@ -141,9 +156,9 @@ export class Affinirum {
|
|
|
141
156
|
_list(state, scope) {
|
|
142
157
|
const span = state.start();
|
|
143
158
|
const subnodes = [];
|
|
144
|
-
while (!state.
|
|
145
|
-
subnodes.push(this.
|
|
146
|
-
if (!state.
|
|
159
|
+
while (!state.isEnded) {
|
|
160
|
+
subnodes.push(this._declaration(state, scope));
|
|
161
|
+
if (!state.equals(";")) {
|
|
147
162
|
break;
|
|
148
163
|
}
|
|
149
164
|
else {
|
|
@@ -152,29 +167,64 @@ export class Affinirum {
|
|
|
152
167
|
}
|
|
153
168
|
return new BlockNode(span.end(state), subnodes);
|
|
154
169
|
}
|
|
170
|
+
_declaration(state, scope) {
|
|
171
|
+
if (!state.equals("var", "val")) {
|
|
172
|
+
return this._unit(state, scope);
|
|
173
|
+
}
|
|
174
|
+
const assignable = state.equals("var");
|
|
175
|
+
if (!state.next().isIdentifier) {
|
|
176
|
+
state.throwError("missing variable name");
|
|
177
|
+
}
|
|
178
|
+
const identifier = state.identifier;
|
|
179
|
+
if (scope.has(identifier)) {
|
|
180
|
+
state.throwError(`illegal redefinition of variable ${identifier}`);
|
|
181
|
+
}
|
|
182
|
+
const span = state.start();
|
|
183
|
+
let type;
|
|
184
|
+
if (state.next().equals(":")) {
|
|
185
|
+
type = this._type.parse(state.next(), scope);
|
|
186
|
+
}
|
|
187
|
+
const variable = new LocalVariable(type, assignable);
|
|
188
|
+
scope.set(identifier, variable);
|
|
189
|
+
if (state.isAssignment) {
|
|
190
|
+
if (state.isConstant) {
|
|
191
|
+
state.throwError(`illegal assignment operator with variable ${identifier}`);
|
|
192
|
+
}
|
|
193
|
+
const subnode = this._unit(state.next(), scope);
|
|
194
|
+
if (subnode instanceof FunctionNode) {
|
|
195
|
+
variable.declare(subnode.type);
|
|
196
|
+
}
|
|
197
|
+
const node = new VariableNode(span, variable, true);
|
|
198
|
+
return new AssignmentNode(span, node, subnode);
|
|
199
|
+
}
|
|
200
|
+
else if (type && !type.isOptional) {
|
|
201
|
+
state.throwError(`variable ${identifier} of type ${type} requires initial value`);
|
|
202
|
+
}
|
|
203
|
+
return new VariableNode(span, variable);
|
|
204
|
+
}
|
|
155
205
|
_unit(state, scope) {
|
|
156
206
|
return this._coalescence(state, scope);
|
|
157
207
|
}
|
|
158
208
|
_coalescence(state, scope) {
|
|
159
209
|
let node = this._disjunction(state, scope);
|
|
160
|
-
while (state.
|
|
161
|
-
const fnode = new ConstantNode(state, state.
|
|
210
|
+
while (state.equals("?:")) {
|
|
211
|
+
const fnode = new ConstantNode(state, state.constant);
|
|
162
212
|
node = new CallNode(state, fnode, [node, this._disjunction(state.next(), scope)]);
|
|
163
213
|
}
|
|
164
214
|
return node;
|
|
165
215
|
}
|
|
166
216
|
_disjunction(state, scope) {
|
|
167
217
|
let node = this._conjunction(state, scope);
|
|
168
|
-
while (state.
|
|
169
|
-
const fnode = new ConstantNode(state, state.
|
|
218
|
+
while (state.equals("|", "or")) {
|
|
219
|
+
const fnode = new ConstantNode(state, state.constant);
|
|
170
220
|
node = new CallNode(state, fnode, [node, this._conjunction(state.next(), scope)]);
|
|
171
221
|
}
|
|
172
222
|
return node;
|
|
173
223
|
}
|
|
174
224
|
_conjunction(state, scope) {
|
|
175
225
|
let node = this._negation(state, scope);
|
|
176
|
-
while (state.
|
|
177
|
-
const fnode = new ConstantNode(state, state.
|
|
226
|
+
while (state.equals("&", "and")) {
|
|
227
|
+
const fnode = new ConstantNode(state, state.constant);
|
|
178
228
|
node = new CallNode(state, fnode, [node, this._negation(state.next(), scope)]);
|
|
179
229
|
}
|
|
180
230
|
return node;
|
|
@@ -182,7 +232,7 @@ export class Affinirum {
|
|
|
182
232
|
_negation(state, scope) {
|
|
183
233
|
const span = state.start();
|
|
184
234
|
let negate = false;
|
|
185
|
-
while (state.
|
|
235
|
+
while (state.equals("!", "not")) {
|
|
186
236
|
negate = !negate;
|
|
187
237
|
span.end(state);
|
|
188
238
|
state.next();
|
|
@@ -195,27 +245,33 @@ export class Affinirum {
|
|
|
195
245
|
return node;
|
|
196
246
|
}
|
|
197
247
|
_comparison(state, scope) {
|
|
248
|
+
let node = this._range(state, scope);
|
|
249
|
+
while (state.equals(">", "<", ">=", "<=", "==", "!=")) {
|
|
250
|
+
const fnode = new ConstantNode(state, state.constant);
|
|
251
|
+
node = new CallNode(state, fnode, [node, this._range(state.next(), scope)]);
|
|
252
|
+
}
|
|
253
|
+
return node;
|
|
254
|
+
}
|
|
255
|
+
_range(state, scope) {
|
|
198
256
|
let node = this._aggregate(state, scope);
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|| state.operator === funcEqual || state.operator === funcNotEqual) {
|
|
202
|
-
const fnode = new ConstantNode(state, state.operator);
|
|
257
|
+
if (state.equals("..")) {
|
|
258
|
+
const fnode = new ConstantNode(state, state.constant);
|
|
203
259
|
node = new CallNode(state, fnode, [node, this._aggregate(state.next(), scope)]);
|
|
204
260
|
}
|
|
205
261
|
return node;
|
|
206
262
|
}
|
|
207
263
|
_aggregate(state, scope) {
|
|
208
264
|
let node = this._product(state, scope);
|
|
209
|
-
while (state.
|
|
210
|
-
const fnode = new ConstantNode(state, state.
|
|
265
|
+
while (state.equals("+", "-")) {
|
|
266
|
+
const fnode = new ConstantNode(state, state.constant);
|
|
211
267
|
node = new CallNode(state, fnode, [node, this._product(state.next(), scope)]);
|
|
212
268
|
}
|
|
213
269
|
return node;
|
|
214
270
|
}
|
|
215
271
|
_product(state, scope) {
|
|
216
272
|
let node = this._signum(state, scope);
|
|
217
|
-
while (state.
|
|
218
|
-
const fnode = new ConstantNode(state, state.
|
|
273
|
+
while (state.equals("*", "/", "%")) {
|
|
274
|
+
const fnode = new ConstantNode(state, state.constant);
|
|
219
275
|
node = new CallNode(state, fnode, [node, this._signum(state.next(), scope)]);
|
|
220
276
|
}
|
|
221
277
|
return node;
|
|
@@ -223,8 +279,8 @@ export class Affinirum {
|
|
|
223
279
|
_signum(state, scope) {
|
|
224
280
|
const span = state.start();
|
|
225
281
|
let negate = false;
|
|
226
|
-
while (state.
|
|
227
|
-
if (state.
|
|
282
|
+
while (state.equals("+", "-")) {
|
|
283
|
+
if (state.equals("-")) {
|
|
228
284
|
negate = !negate;
|
|
229
285
|
}
|
|
230
286
|
span.end(state);
|
|
@@ -238,47 +294,54 @@ export class Affinirum {
|
|
|
238
294
|
return node;
|
|
239
295
|
}
|
|
240
296
|
_factor(state, scope) {
|
|
241
|
-
let node = this.
|
|
242
|
-
while (state.
|
|
243
|
-
const fnode = new ConstantNode(state, state.
|
|
244
|
-
node = new CallNode(state, fnode, [node, this.
|
|
297
|
+
let node = this._postfix(state, scope);
|
|
298
|
+
while (state.equals("**")) {
|
|
299
|
+
const fnode = new ConstantNode(state, state.constant);
|
|
300
|
+
node = new CallNode(state, fnode, [node, this._postfix(state.next(), scope)]);
|
|
245
301
|
}
|
|
246
302
|
return node;
|
|
247
303
|
}
|
|
248
|
-
|
|
304
|
+
_postfix(state, scope) {
|
|
249
305
|
const span = state.start();
|
|
250
306
|
let node = this._term(state, scope);
|
|
251
|
-
while (state.
|
|
252
|
-
if (state.
|
|
253
|
-
const
|
|
307
|
+
while (state.equals(".", ".?", "?.", "?.?", "(", "[", "?[")) {
|
|
308
|
+
if (state.equals(".", ".?", "?.", "?.?")) { // index access or existence check operator
|
|
309
|
+
const access = state.equals(".", "?.");
|
|
310
|
+
const optional = state.equals("?.");
|
|
311
|
+
const operator = state.constant;
|
|
254
312
|
if (state.next().isLiteral && (typeof state.literal.value === "string" || typeof state.literal.value === "bigint")) {
|
|
255
|
-
const
|
|
256
|
-
node =
|
|
313
|
+
const inode = new ConstantNode(state, new Constant(state.literal.value));
|
|
314
|
+
node = access
|
|
315
|
+
? new AccessNode(span.end(state), node, inode, operator)
|
|
316
|
+
: new CallNode(span, new ConstantNode(span.end(state), operator), [node, inode]);
|
|
257
317
|
state.next();
|
|
258
318
|
}
|
|
259
|
-
else if (state.
|
|
260
|
-
const func = this._functions.get(state.
|
|
319
|
+
else if (state.isIdentifier) {
|
|
320
|
+
const func = access ? this._functions.get(state.identifier) : undefined;
|
|
261
321
|
if (func) {
|
|
262
|
-
if (state.next().
|
|
322
|
+
if (state.next().equals("(")) {
|
|
263
323
|
const subnodes = [node];
|
|
264
|
-
while (!state.next().
|
|
324
|
+
while (!state.next().equals(")")) {
|
|
265
325
|
subnodes.push(this._unit(state, scope));
|
|
266
|
-
if (!state.
|
|
326
|
+
if (!state.equals(",")) {
|
|
267
327
|
break;
|
|
268
328
|
}
|
|
269
329
|
}
|
|
270
330
|
const fnode = new ConstantNode(span.end(state), func);
|
|
271
|
-
node = new CallNode(span, fnode, subnodes);
|
|
272
|
-
state.closeParentheses()
|
|
331
|
+
node = new CallNode(span, fnode, subnodes, optional);
|
|
332
|
+
state.closeParentheses();
|
|
333
|
+
state.next();
|
|
273
334
|
}
|
|
274
335
|
else {
|
|
275
336
|
const fnode = new ConstantNode(span.end(state), func);
|
|
276
|
-
node = new CallNode(span, fnode, [node]);
|
|
337
|
+
node = new CallNode(span, fnode, [node], optional);
|
|
277
338
|
}
|
|
278
339
|
}
|
|
279
340
|
else {
|
|
280
|
-
const
|
|
281
|
-
node =
|
|
341
|
+
const inode = new ConstantNode(state, new Constant(state.identifier));
|
|
342
|
+
node = access
|
|
343
|
+
? new AccessNode(span.end(state), node, inode, operator)
|
|
344
|
+
: new CallNode(span, new ConstantNode(span.end(state), operator), [node, inode]);
|
|
282
345
|
state.next();
|
|
283
346
|
}
|
|
284
347
|
}
|
|
@@ -286,29 +349,33 @@ export class Affinirum {
|
|
|
286
349
|
state.throwError("missing array index or object key");
|
|
287
350
|
}
|
|
288
351
|
}
|
|
289
|
-
else if (state.
|
|
352
|
+
else if (state.equals("[", "?[")) { // access operator
|
|
353
|
+
const operator = state.constant;
|
|
354
|
+
const inode = this._unit(state.next(), scope);
|
|
355
|
+
state.closeBrackets();
|
|
356
|
+
state.next();
|
|
357
|
+
node = new AccessNode(span.end(state), node, inode, operator);
|
|
358
|
+
}
|
|
359
|
+
else if (state.equals("(")) { // function call
|
|
290
360
|
const subnodes = [];
|
|
291
|
-
while (!state.next().
|
|
361
|
+
while (!state.next().equals(")")) {
|
|
292
362
|
subnodes.push(this._unit(state, scope));
|
|
293
|
-
if (!state.
|
|
363
|
+
if (!state.equals(",")) {
|
|
294
364
|
break;
|
|
295
365
|
}
|
|
296
366
|
}
|
|
297
367
|
state.closeParentheses();
|
|
298
|
-
node = new CallNode(span.end(state), node, subnodes);
|
|
299
|
-
state.next();
|
|
300
|
-
}
|
|
301
|
-
else if (state.isBracketsOpen) { // index access operator
|
|
302
|
-
const subnodes = [node, this._unit(state.next(), scope)];
|
|
303
|
-
state.closeBrackets();
|
|
304
|
-
const fnode = new ConstantNode(span.end(state), funcAt);
|
|
305
|
-
node = new CallNode(span, fnode, subnodes);
|
|
306
368
|
state.next();
|
|
369
|
+
node = new CallNode(span.end(state), node, subnodes);
|
|
307
370
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
371
|
+
}
|
|
372
|
+
if (state.isAssignment) {
|
|
373
|
+
if (!(node instanceof AssignableNode) || !node.assignable) {
|
|
374
|
+
state.throwError("illegal re-assignment");
|
|
311
375
|
}
|
|
376
|
+
const operator = state.isConstant ? state.constant : undefined;
|
|
377
|
+
const subnode = this._unit(state.next(), scope);
|
|
378
|
+
return new AssignmentNode(span, node, subnode, operator);
|
|
312
379
|
}
|
|
313
380
|
return node;
|
|
314
381
|
}
|
|
@@ -319,309 +386,307 @@ export class Affinirum {
|
|
|
319
386
|
state.next();
|
|
320
387
|
return new ConstantNode(span, constant);
|
|
321
388
|
}
|
|
322
|
-
else if (state.
|
|
323
|
-
|
|
324
|
-
const constants = this._constants.get(state.token);
|
|
325
|
-
if (constants != null) {
|
|
326
|
-
if (!state.next().isDot) {
|
|
327
|
-
state.throwError("missing constant accessor operator");
|
|
328
|
-
}
|
|
329
|
-
if (!state.next().isToken) {
|
|
330
|
-
state.throwError("missing constant name");
|
|
331
|
-
}
|
|
332
|
-
const constant = constants[state.token];
|
|
333
|
-
if (!constant) {
|
|
334
|
-
state.throwError(`unknown constant ${state.token}`);
|
|
335
|
-
}
|
|
336
|
-
state.next();
|
|
337
|
-
return new ConstantNode(span, constant);
|
|
338
|
-
}
|
|
339
|
-
let variable = scope.get(state.token) ?? this._variables.get(state.token);
|
|
340
|
-
if (variable == null) {
|
|
341
|
-
const type = this._vartypes.get(state.token);
|
|
342
|
-
if (type == null && this._strict) {
|
|
343
|
-
state.throwError(`undefined variable ${state.token} in strict mode`);
|
|
344
|
-
}
|
|
345
|
-
variable = new Variable(type);
|
|
346
|
-
this._variables.set(state.token, variable);
|
|
347
|
-
this._varspans.set(state.token, state.start());
|
|
348
|
-
}
|
|
349
|
-
if (state.next().isAssignment) {
|
|
350
|
-
if (!variable.assignable) {
|
|
351
|
-
state.throwError("illegal re-assignment");
|
|
352
|
-
}
|
|
353
|
-
if (state.assignment.operator) {
|
|
354
|
-
const operator = state.assignment.operator;
|
|
355
|
-
const fnode = new ConstantNode(span, operator);
|
|
356
|
-
const subnode = new CallNode(span, fnode, [new VariableNode(span, variable), this._unit(state.next(), scope)]);
|
|
357
|
-
return new VariableNode(span, variable, subnode);
|
|
358
|
-
}
|
|
359
|
-
else {
|
|
360
|
-
const subnode = this._unit(state.next(), scope);
|
|
361
|
-
return new VariableNode(span, variable, subnode);
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
return new VariableNode(span, variable);
|
|
389
|
+
else if (state.isIdentifier) { // constant or variable identifier
|
|
390
|
+
return this._identifier(state, scope);
|
|
365
391
|
}
|
|
366
|
-
else if (state.
|
|
367
|
-
|
|
368
|
-
const subscope = scope.subscope();
|
|
369
|
-
const subnodes = [];
|
|
370
|
-
while (!state.next().isBracesClose) {
|
|
371
|
-
subnodes.push(this._unit(state, subscope));
|
|
372
|
-
if (!state.isSemicolon) {
|
|
373
|
-
break;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
span.end(state);
|
|
377
|
-
state.closeBraces().next();
|
|
378
|
-
return new BlockNode(span, subnodes, [...subscope.variables()]);
|
|
392
|
+
else if (state.equals("{")) {
|
|
393
|
+
return this._block(state, scope);
|
|
379
394
|
}
|
|
380
|
-
else if (state.
|
|
395
|
+
else if (state.equals("}")) {
|
|
381
396
|
state.throwError("unexpected closing braces");
|
|
382
397
|
}
|
|
383
|
-
else if (state.
|
|
384
|
-
const
|
|
385
|
-
|
|
398
|
+
else if (state.equals("(")) { // grouping or type cast
|
|
399
|
+
const span = state.start();
|
|
400
|
+
let node = this._unit(state.next(), scope);
|
|
401
|
+
if (state.equals(":")) {
|
|
402
|
+
const type = this._type.parse(state.next(), scope);
|
|
403
|
+
node = new CastNode(span.end(state), node, type);
|
|
404
|
+
}
|
|
405
|
+
state.closeParentheses();
|
|
406
|
+
state.next();
|
|
386
407
|
return node;
|
|
387
408
|
}
|
|
388
|
-
else if (state.
|
|
409
|
+
else if (state.equals(")")) {
|
|
389
410
|
state.throwError("unexpected closing parentheses");
|
|
390
411
|
}
|
|
391
|
-
else if (state.
|
|
392
|
-
|
|
393
|
-
const subnodes = [];
|
|
394
|
-
let index = 0, colon = false;
|
|
395
|
-
while (!state.next().isBracketsClose) {
|
|
396
|
-
if (state.isColon) {
|
|
397
|
-
colon = true;
|
|
398
|
-
state.next();
|
|
399
|
-
break;
|
|
400
|
-
}
|
|
401
|
-
const node = this._unit(state, scope);
|
|
402
|
-
if (state.isColon) {
|
|
403
|
-
colon = true;
|
|
404
|
-
subnodes.push([node, this._unit(state.next(), scope)]);
|
|
405
|
-
}
|
|
406
|
-
else {
|
|
407
|
-
subnodes.push([index++, node]);
|
|
408
|
-
}
|
|
409
|
-
if (!state.isComma) {
|
|
410
|
-
break;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
span.end(state);
|
|
414
|
-
state.closeBrackets().next();
|
|
415
|
-
if (colon) {
|
|
416
|
-
return new ObjectNode(span, subnodes.map(([k, v]) => [typeof k === "number" ? new ConstantNode(v, new Constant(String(k))) : k, v]));
|
|
417
|
-
}
|
|
418
|
-
return new ArrayNode(span, subnodes.map(([, v]) => v));
|
|
412
|
+
else if (state.equals("[")) { // array or object composite
|
|
413
|
+
return this._composite(state, scope);
|
|
419
414
|
}
|
|
420
|
-
else if (state.
|
|
415
|
+
else if (state.equals("]")) {
|
|
421
416
|
state.throwError("unexpected closing brackets");
|
|
422
417
|
}
|
|
423
|
-
else if (state.
|
|
424
|
-
const assignable = state.isVar;
|
|
425
|
-
if (!state.next().isToken) {
|
|
426
|
-
state.throwError("missing variable name");
|
|
427
|
-
}
|
|
428
|
-
const token = state.token;
|
|
429
|
-
if (scope.has(token)) {
|
|
430
|
-
state.throwError(`illegal redefinition of variable ${token}`);
|
|
431
|
-
}
|
|
432
|
-
const span = state.start();
|
|
433
|
-
let type;
|
|
434
|
-
if (state.next().isColon) {
|
|
435
|
-
type = this._type(state.next(), scope);
|
|
436
|
-
}
|
|
437
|
-
const variable = new Variable(type, assignable);
|
|
438
|
-
scope.set(token, variable);
|
|
439
|
-
if (state.isAssignment) {
|
|
440
|
-
if (state.assignment.operator) {
|
|
441
|
-
state.throwError(`illegal assignment operator with variable ${token}`);
|
|
442
|
-
}
|
|
443
|
-
return new VariableNode(span, variable, this._unit(state.next(), scope));
|
|
444
|
-
}
|
|
445
|
-
return new VariableNode(span, variable);
|
|
446
|
-
}
|
|
447
|
-
else if (state.isTilda) {
|
|
418
|
+
else if (state.equals("func", "function")) {
|
|
448
419
|
return this._function(state, scope);
|
|
449
420
|
}
|
|
450
|
-
else if (state.
|
|
451
|
-
|
|
421
|
+
else if (state.equals("->")) {
|
|
422
|
+
if (!scope.inFunction) {
|
|
423
|
+
state.throwError("unexpected -> outside of a function");
|
|
424
|
+
}
|
|
425
|
+
return new FinalNode(state, "return", this._unit(state.next(), scope), scope.type);
|
|
452
426
|
}
|
|
453
|
-
else if (state.
|
|
454
|
-
|
|
427
|
+
else if (state.equals("=>")) {
|
|
428
|
+
const node = new FinalNode(state, "result", this._unit(state.next(), scope), this._scope.type);
|
|
429
|
+
this._results.push(node);
|
|
430
|
+
return node;
|
|
455
431
|
}
|
|
456
|
-
else if (state.
|
|
457
|
-
|
|
432
|
+
else if (state.equals("var", "val")) {
|
|
433
|
+
state.throwError("unexpected variable declaration");
|
|
458
434
|
}
|
|
459
|
-
else if (state.
|
|
460
|
-
return
|
|
435
|
+
else if (state.equals("if")) {
|
|
436
|
+
return this._ternary(state, scope);
|
|
461
437
|
}
|
|
462
|
-
else if (state.
|
|
463
|
-
|
|
464
|
-
state.next();
|
|
465
|
-
return node;
|
|
438
|
+
else if (state.equals("match")) {
|
|
439
|
+
return this._match(state, scope);
|
|
466
440
|
}
|
|
467
|
-
else if (state.
|
|
468
|
-
|
|
441
|
+
else if (state.equals("while", "do")) {
|
|
442
|
+
return this._loop(state, scope);
|
|
443
|
+
}
|
|
444
|
+
else if (state.equals("exit", "next")) {
|
|
445
|
+
const action = state.equals("exit") ? "exit" : "next";
|
|
446
|
+
if (!scope.inLoop) {
|
|
447
|
+
state.throwError(`unexpected ${action} outside of a loop`);
|
|
448
|
+
}
|
|
449
|
+
const node = new JumpNode(state, action);
|
|
469
450
|
state.next();
|
|
470
451
|
return node;
|
|
471
452
|
}
|
|
472
|
-
else if (state.
|
|
453
|
+
else if (state.isEnded) {
|
|
473
454
|
state.throwError("unexpected end of expression");
|
|
474
455
|
}
|
|
475
456
|
state.throwError("unexpected expression token");
|
|
476
457
|
}
|
|
458
|
+
_identifier(state, scope) {
|
|
459
|
+
const span = state.start();
|
|
460
|
+
const constants = this._constants.get(state.identifier);
|
|
461
|
+
if (constants != null) {
|
|
462
|
+
if (!state.next().equals(".")) {
|
|
463
|
+
state.throwError("missing constant accessor operator");
|
|
464
|
+
}
|
|
465
|
+
if (!state.next().isIdentifier) {
|
|
466
|
+
state.throwError("missing constant name");
|
|
467
|
+
}
|
|
468
|
+
const constant = constants[state.identifier];
|
|
469
|
+
if (!constant) {
|
|
470
|
+
state.throwError(`unknown constant ${state.identifier}`);
|
|
471
|
+
}
|
|
472
|
+
state.next();
|
|
473
|
+
return new ConstantNode(span.end(state), constant);
|
|
474
|
+
}
|
|
475
|
+
let variable = scope.get(state.identifier) ?? this._variables.get(state.identifier);
|
|
476
|
+
if (variable == null) {
|
|
477
|
+
const type = this._vartypes.get(state.identifier);
|
|
478
|
+
if (type == null && this._strict) {
|
|
479
|
+
state.throwError(`undefined variable ${state.identifier} in strict mode`);
|
|
480
|
+
}
|
|
481
|
+
if (type?.isNever) {
|
|
482
|
+
state.throwError(`input variable ${state.identifier} cannot have type never`);
|
|
483
|
+
}
|
|
484
|
+
variable = new InputVariable(type);
|
|
485
|
+
this._variables.set(state.identifier, variable);
|
|
486
|
+
this._varspans.set(state.identifier, state.start());
|
|
487
|
+
}
|
|
488
|
+
state.next();
|
|
489
|
+
return new VariableNode(span, variable);
|
|
490
|
+
}
|
|
477
491
|
_function(state, scope) {
|
|
478
492
|
const span = state.start();
|
|
479
493
|
state.next().openParentheses();
|
|
480
494
|
let variadic = false;
|
|
481
495
|
const variables = new Map();
|
|
482
|
-
while (!state.next().
|
|
483
|
-
if (!state.
|
|
496
|
+
while (!state.next().equals(")")) {
|
|
497
|
+
if (!state.isIdentifier) {
|
|
484
498
|
state.throwError("missing function argument name");
|
|
485
499
|
}
|
|
486
|
-
const
|
|
487
|
-
if (variables.has(
|
|
488
|
-
state.throwError(`duplicate function argument name ${
|
|
500
|
+
const identifier = state.identifier;
|
|
501
|
+
if (variables.has(identifier)) {
|
|
502
|
+
state.throwError(`duplicate function argument name ${identifier}`);
|
|
489
503
|
}
|
|
490
|
-
if (scope.has(
|
|
491
|
-
state.throwError(`variable ${
|
|
504
|
+
if (scope.has(identifier)) {
|
|
505
|
+
state.throwError(`variable ${identifier} redefinition`);
|
|
492
506
|
}
|
|
493
|
-
let argType
|
|
494
|
-
if (state.next().
|
|
495
|
-
variadic = state.next().
|
|
507
|
+
let argType;
|
|
508
|
+
if (state.next().equals(":")) {
|
|
509
|
+
variadic = state.next().equals("...");
|
|
496
510
|
if (variadic) {
|
|
497
511
|
state.next();
|
|
498
512
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
variables.set(token, new Variable(argType));
|
|
502
|
-
if (variadic) {
|
|
503
|
-
if (argType.isArray) {
|
|
504
|
-
break;
|
|
513
|
+
if (!variadic || !state.equals(")")) {
|
|
514
|
+
argType = this._type.parse(state, scope);
|
|
505
515
|
}
|
|
506
|
-
|
|
507
|
-
|
|
516
|
+
if (variadic) {
|
|
517
|
+
argType = Type.arrayType(argType);
|
|
508
518
|
}
|
|
509
519
|
}
|
|
510
|
-
|
|
520
|
+
const variable = new LocalVariable(argType, true);
|
|
521
|
+
variables.set(identifier, variable);
|
|
522
|
+
if (variadic) {
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
if (!state.equals(",")) {
|
|
511
526
|
break;
|
|
512
527
|
}
|
|
513
528
|
}
|
|
514
|
-
state.closeParentheses()
|
|
515
|
-
const returnType = state.
|
|
516
|
-
|
|
529
|
+
state.closeParentheses();
|
|
530
|
+
const returnType = state.next().equals(":") ? this._type.parse(state.next(), scope) : Type.Any;
|
|
531
|
+
state.openBraces();
|
|
532
|
+
state.next();
|
|
517
533
|
const args = Array.from(variables.values());
|
|
518
|
-
const subscope = scope.subscope(variables, returnType);
|
|
519
|
-
const
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
return normalize(subnode.evaluate(subframe));
|
|
525
|
-
}
|
|
526
|
-
catch (e) {
|
|
527
|
-
if (e instanceof JumpException) {
|
|
528
|
-
if (e.jump === "return") {
|
|
529
|
-
return normalize(e.value);
|
|
530
|
-
}
|
|
531
|
-
if (e.jump === "stop" || e.jump === "next") {
|
|
532
|
-
subnode.throwError(`unexpected ${e.jump} jump`);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
throw e;
|
|
534
|
+
const subscope = scope.subscope(variables, returnType, true);
|
|
535
|
+
const subnodes = [];
|
|
536
|
+
while (!state.equals("}")) {
|
|
537
|
+
subnodes.push(this._declaration(state, subscope));
|
|
538
|
+
if (!state.equals(";")) {
|
|
539
|
+
break;
|
|
536
540
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
541
|
+
state.next();
|
|
542
|
+
}
|
|
543
|
+
state.closeBraces();
|
|
544
|
+
span.end(state);
|
|
545
|
+
state.next();
|
|
546
|
+
const subnode = new BlockNode(span, subnodes);
|
|
547
|
+
const functionType = Type.functionType([[returnType, args.map((v) => v.type), variadic]]);
|
|
548
|
+
return new FunctionNode(span, subnode, [...subscope.variables()], args, functionType);
|
|
540
549
|
}
|
|
541
|
-
|
|
550
|
+
_block(state, scope) {
|
|
542
551
|
const span = state.start();
|
|
543
|
-
|
|
552
|
+
state.next();
|
|
553
|
+
const subscope = scope.subscope();
|
|
554
|
+
const subnodes = [];
|
|
555
|
+
while (!state.equals("}")) {
|
|
556
|
+
subnodes.push(this._declaration(state, subscope));
|
|
557
|
+
if (!state.equals(";")) {
|
|
558
|
+
break;
|
|
559
|
+
}
|
|
560
|
+
state.next();
|
|
561
|
+
}
|
|
562
|
+
span.end(state);
|
|
563
|
+
state.closeBraces();
|
|
564
|
+
state.next();
|
|
565
|
+
return new BlockNode(span, subnodes, [...subscope.variables()]);
|
|
566
|
+
}
|
|
567
|
+
_composite(state, scope) {
|
|
568
|
+
const span = state.start();
|
|
569
|
+
const subnodes = [];
|
|
570
|
+
let index = 0, colon = false, ellipsis = false;
|
|
571
|
+
while (!state.next().equals("]")) {
|
|
572
|
+
if (state.equals("...")) {
|
|
573
|
+
if (ellipsis) {
|
|
574
|
+
state.throwError("repeated ellipsis");
|
|
575
|
+
}
|
|
576
|
+
if (subnodes.length > 0) {
|
|
577
|
+
state.throwError("ellipsis must appear at the beginning of an array or object literal");
|
|
578
|
+
}
|
|
579
|
+
ellipsis = true;
|
|
580
|
+
state.next();
|
|
581
|
+
}
|
|
582
|
+
if (state.equals(":")) {
|
|
583
|
+
colon = true;
|
|
584
|
+
state.next();
|
|
585
|
+
if (!ellipsis) {
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
if (!state.equals("never")) {
|
|
589
|
+
state.throwError("missing never literal");
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (subnodes.length === 0 && (ellipsis || colon) && state.equals("never")) {
|
|
593
|
+
state.next().closeBrackets();
|
|
594
|
+
span.end(state);
|
|
595
|
+
state.next();
|
|
596
|
+
return new ConstantNode(span, colon
|
|
597
|
+
? new Constant(Object.create(null), Type.objectType(Type.Never))
|
|
598
|
+
: new Constant([], Type.arrayType(Type.Never)));
|
|
599
|
+
}
|
|
600
|
+
const node = this._unit(state, scope);
|
|
601
|
+
if (state.equals(":")) {
|
|
602
|
+
colon = true;
|
|
603
|
+
subnodes.push([node, this._unit(state.next(), scope)]);
|
|
604
|
+
}
|
|
605
|
+
else {
|
|
606
|
+
subnodes.push([index++, node]);
|
|
607
|
+
}
|
|
608
|
+
if (!state.equals(",")) {
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
544
612
|
span.end(state);
|
|
545
|
-
|
|
613
|
+
state.closeBrackets();
|
|
614
|
+
state.next();
|
|
615
|
+
if (colon) {
|
|
616
|
+
return new ObjectNode(span, subnodes.map(([k, v]) => [typeof k === "number" ? new ConstantNode(v, new Constant(String(k))) : k, v]), ellipsis);
|
|
617
|
+
}
|
|
618
|
+
return new ArrayNode(span, subnodes.map(([, v]) => v), ellipsis);
|
|
546
619
|
}
|
|
547
|
-
|
|
620
|
+
_ternary(state, scope) {
|
|
548
621
|
const span = state.start();
|
|
549
622
|
const cnode = this._unit(state.next(), scope);
|
|
550
623
|
span.end(state);
|
|
551
|
-
|
|
552
|
-
|
|
624
|
+
if (state.equals("then")) {
|
|
625
|
+
state.next();
|
|
626
|
+
}
|
|
627
|
+
else if (!state.equals("{")) {
|
|
628
|
+
state.throwError("missing 'then' or '{' after the if condition");
|
|
629
|
+
}
|
|
630
|
+
const tnode = this._unit(state, scope);
|
|
631
|
+
const enode = state.equals("else")
|
|
553
632
|
? this._unit(state.next(), scope)
|
|
554
633
|
: new ConstantNode(state.start(), Constant.Null);
|
|
555
|
-
return new
|
|
634
|
+
return new TernaryNode(span, cnode, [tnode, enode]);
|
|
556
635
|
}
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
636
|
+
_match(state, scope) {
|
|
637
|
+
const span = state.start();
|
|
638
|
+
const vnode = this._unit(state.next(), scope);
|
|
639
|
+
state.openBraces();
|
|
640
|
+
const subnode = [];
|
|
641
|
+
let enode;
|
|
642
|
+
while (!state.next().equals("}")) {
|
|
643
|
+
if (enode) {
|
|
644
|
+
state.throwError("else must be the last match arm");
|
|
645
|
+
}
|
|
646
|
+
if (state.equals("else")) {
|
|
647
|
+
state.next().assertColon();
|
|
648
|
+
enode = this._unit(state.next(), scope);
|
|
563
649
|
}
|
|
564
|
-
|
|
565
|
-
|
|
650
|
+
else {
|
|
651
|
+
const key = this._unit(state, scope);
|
|
652
|
+
state.assertColon();
|
|
653
|
+
subnode.push([key, this._unit(state.next(), scope)]);
|
|
566
654
|
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
else if (state.isBracketsOpen) { // array or object type
|
|
570
|
-
const itemKeyTypes = [];
|
|
571
|
-
let index = 0, colon = false;
|
|
572
|
-
while (!state.next().isBracketsClose) {
|
|
573
|
-
if (state.isLiteral && typeof state.literal.value === "string") {
|
|
574
|
-
const key = state.literal.value;
|
|
575
|
-
state.next().separateByColon().next();
|
|
576
|
-
itemKeyTypes.push([key, this._type(state, scope)]);
|
|
577
|
-
colon = true;
|
|
578
|
-
}
|
|
579
|
-
else {
|
|
580
|
-
itemKeyTypes.push([index++, this._type(state, scope)]);
|
|
581
|
-
}
|
|
582
|
-
if (!state.isComma) {
|
|
583
|
-
break;
|
|
584
|
-
}
|
|
655
|
+
if (state.equals("}") || state.isEnded) {
|
|
656
|
+
break;
|
|
585
657
|
}
|
|
586
|
-
state.
|
|
587
|
-
|
|
588
|
-
? Type.objectType(itemKeyTypes.map(([key, type]) => [key, type]))
|
|
589
|
-
: Type.arrayType(itemKeyTypes.map(([, v]) => v));
|
|
590
|
-
}
|
|
591
|
-
else if (state.isTilda) { // function type
|
|
592
|
-
state.next().openParentheses();
|
|
593
|
-
let variadic = false;
|
|
594
|
-
const argTypes = [];
|
|
595
|
-
while (!state.next().isParenthesesClose) {
|
|
596
|
-
variadic = state.isEllipsis;
|
|
597
|
-
if (variadic) {
|
|
598
|
-
state.next();
|
|
599
|
-
}
|
|
600
|
-
const argType = this._type(state, scope);
|
|
601
|
-
argTypes.push(argType);
|
|
602
|
-
if (variadic) {
|
|
603
|
-
if (argType.isArray) {
|
|
604
|
-
break;
|
|
605
|
-
}
|
|
606
|
-
else {
|
|
607
|
-
state.throwError("variadic function argument must be an array type");
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
if (!state.isComma) {
|
|
611
|
-
break;
|
|
612
|
-
}
|
|
658
|
+
if (!state.equals(",")) {
|
|
659
|
+
state.throwError("expected ',' or '}' after match arm");
|
|
613
660
|
}
|
|
614
|
-
state.closeParentheses().next();
|
|
615
|
-
const retType = state.isColon ? this._type(state.next(), scope) : Type.Unknown;
|
|
616
|
-
return Type.functionType(retType, argTypes, variadic);
|
|
617
661
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
662
|
+
state.closeBraces();
|
|
663
|
+
span.end(state);
|
|
664
|
+
state.next();
|
|
665
|
+
return new MatchNode(span, vnode, subnode, enode);
|
|
666
|
+
}
|
|
667
|
+
_loop(state, scope) {
|
|
668
|
+
const span = state.start();
|
|
669
|
+
const wnode = state.equals("while")
|
|
670
|
+
? this._unit(state.next(), scope)
|
|
671
|
+
: undefined;
|
|
672
|
+
if (wnode) {
|
|
673
|
+
if (state.equals("do")) {
|
|
674
|
+
state.next();
|
|
675
|
+
}
|
|
676
|
+
else if (!state.equals("{")) {
|
|
677
|
+
state.throwError("missing 'do' or '{' after the while condition");
|
|
678
|
+
}
|
|
622
679
|
}
|
|
623
680
|
else {
|
|
624
|
-
state.
|
|
681
|
+
state.next();
|
|
625
682
|
}
|
|
683
|
+
const inLoop = scope.inLoop;
|
|
684
|
+
scope.inLoop = true;
|
|
685
|
+
const subnode = this._unit(state, scope);
|
|
686
|
+
scope.inLoop = inLoop;
|
|
687
|
+
const unode = state.equals("until")
|
|
688
|
+
? this._unit(state.next(), scope)
|
|
689
|
+
: undefined;
|
|
690
|
+
return new LoopNode(span, subnode, wnode, unode);
|
|
626
691
|
}
|
|
627
692
|
}
|