affinirum 1.6.0 → 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.

Potentially problematic release.


This version of affinirum might be problematic. Click here for more details.

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