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.
Files changed (243) 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 +925 -355
  4. package/dst/Affinirum.d.ts +29 -19
  5. package/dst/Affinirum.js +399 -334
  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 +398 -333
  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 -25
  70. package/dst/cjs/native/Aggregable.js +10 -2
  71. package/dst/cjs/native/Array.d.ts +2 -3
  72. package/dst/cjs/native/Array.js +38 -39
  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 +7 -9
  125. package/dst/cjs/node/JumpNode.js +12 -19
  126. package/dst/cjs/node/LoopNode.d.ts +7 -5
  127. package/dst/cjs/node/LoopNode.js +33 -22
  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} +17 -11
  132. package/dst/cjs/node/ObjectNode.d.ts +7 -4
  133. package/dst/cjs/node/ObjectNode.js +42 -11
  134. package/dst/cjs/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
  135. package/dst/cjs/node/TernaryNode.js +45 -0
  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 -24
  142. package/dst/native/Aggregable.js +11 -3
  143. package/dst/native/Array.d.ts +2 -3
  144. package/dst/native/Array.js +37 -38
  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 +7 -9
  197. package/dst/node/JumpNode.js +12 -19
  198. package/dst/node/LoopNode.d.ts +7 -5
  199. package/dst/node/LoopNode.js +33 -22
  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/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
  207. package/dst/node/TernaryNode.js +41 -0
  208. package/dst/node/VariableNode.d.ts +10 -6
  209. package/dst/node/VariableNode.js +50 -16
  210. package/package.json +4 -4
  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/Jump.d.ts +0 -1
  216. package/dst/Jump.js +0 -1
  217. package/dst/JumpException.d.ts +0 -9
  218. package/dst/JumpException.js +0 -14
  219. package/dst/Keywords.d.ts +0 -1
  220. package/dst/Keywords.js +0 -17
  221. package/dst/Node.d.ts +0 -14
  222. package/dst/Node.js +0 -22
  223. package/dst/ScriptSpan.d.ts +0 -10
  224. package/dst/cjs/Atom.d.ts +0 -7
  225. package/dst/cjs/Constants.d.ts +0 -2
  226. package/dst/cjs/Constants.js +0 -27
  227. package/dst/cjs/Functions.d.ts +0 -2
  228. package/dst/cjs/Jump.d.ts +0 -1
  229. package/dst/cjs/Jump.js +0 -2
  230. package/dst/cjs/JumpException.d.ts +0 -9
  231. package/dst/cjs/JumpException.js +0 -18
  232. package/dst/cjs/Keywords.d.ts +0 -1
  233. package/dst/cjs/Keywords.js +0 -20
  234. package/dst/cjs/Node.d.ts +0 -14
  235. package/dst/cjs/ScriptSpan.d.ts +0 -10
  236. package/dst/cjs/node/SwitchNode.js +0 -49
  237. package/dst/cjs/node/VariadicNode.d.ts +0 -4
  238. package/dst/cjs/node/VariadicNode.js +0 -10
  239. package/dst/node/SwitchNode.js +0 -45
  240. package/dst/node/VariadicNode.d.ts +0 -4
  241. package/dst/node/VariadicNode.js +0 -6
  242. /package/dst/{Atom.js → atom/Atom.js} +0 -0
  243. /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 { 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";
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 { ParserState } from "./ParserState.js";
25
- import { LexicalScope } from "./LexicalScope.js";
26
- import { CallFrame } from "./CallFrame.js";
27
- import { JumpException } from "./JumpException.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,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 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 JumpException) {
115
- if (e.jump === "exit") {
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.jump} jump`);
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.isEmpty) {
145
- subnodes.push(this._unit(state, scope));
146
- if (!state.isSemicolon) {
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.operator === funcCoalesce) {
161
- const fnode = new ConstantNode(state, state.operator);
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.operator === funcOr) {
169
- const fnode = new ConstantNode(state, state.operator);
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.operator === funcAnd) {
177
- const fnode = new ConstantNode(state, state.operator);
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.operator === funcNot) {
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
- while (state.operator === funcGreaterThan || state.operator === funcLessThan
200
- || state.operator === funcGreaterOrEqual || state.operator === funcLessOrEqual
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.operator === funcAdd || state.operator === funcSubtract) {
210
- const fnode = new ConstantNode(state, state.operator);
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.operator === funcMultiply || state.operator === funcDivide || state.operator === funcRemainder) {
218
- const fnode = new ConstantNode(state, state.operator);
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.operator === funcAdd || state.operator === funcSubtract) {
227
- if (state.operator === funcSubtract) {
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._accessor(state, scope);
242
- while (state.operator === funcPower) {
243
- const fnode = new ConstantNode(state, state.operator);
244
- 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)]);
245
301
  }
246
302
  return node;
247
303
  }
248
- _accessor(state, scope) {
304
+ _postfix(state, scope) {
249
305
  const span = state.start();
250
306
  let node = this._term(state, scope);
251
- while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen || state.isCast) {
252
- if (state.isDot || state.isQuestion) { // property access or property existance operator
253
- 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;
254
312
  if (state.next().isLiteral && (typeof state.literal.value === "string" || typeof state.literal.value === "bigint")) {
255
- const fnode = new ConstantNode(span.end(state), operator);
256
- 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]);
257
317
  state.next();
258
318
  }
259
- else if (state.isToken) {
260
- const func = this._functions.get(state.token);
319
+ else if (state.isIdentifier) {
320
+ const func = access ? this._functions.get(state.identifier) : undefined;
261
321
  if (func) {
262
- if (state.next().isParenthesesOpen) {
322
+ if (state.next().equals("(")) {
263
323
  const subnodes = [node];
264
- while (!state.next().isParenthesesClose) {
324
+ while (!state.next().equals(")")) {
265
325
  subnodes.push(this._unit(state, scope));
266
- if (!state.isComma) {
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().next();
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 fnode = new ConstantNode(span.end(state), operator);
281
- 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]);
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.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
290
360
  const subnodes = [];
291
- while (!state.next().isParenthesesClose) {
361
+ while (!state.next().equals(")")) {
292
362
  subnodes.push(this._unit(state, scope));
293
- if (!state.isComma) {
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
- else if (state.isCast) { // type cast operator
309
- const type = this._type(state.next(), scope);
310
- 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");
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.isToken) { // variable
323
- const span = state.start();
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.isBracesOpen) { // code block
367
- const span = state.start();
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.isBracesClose) {
395
+ else if (state.equals("}")) {
381
396
  state.throwError("unexpected closing braces");
382
397
  }
383
- else if (state.isParenthesesOpen) { // unit grouping
384
- const node = this._unit(state.next(), scope);
385
- 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();
386
407
  return node;
387
408
  }
388
- else if (state.isParenthesesClose) {
409
+ else if (state.equals(")")) {
389
410
  state.throwError("unexpected closing parentheses");
390
411
  }
391
- else if (state.isBracketsOpen) { // array or object
392
- const span = state.start();
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.isBracketsClose) {
415
+ else if (state.equals("]")) {
421
416
  state.throwError("unexpected closing brackets");
422
417
  }
423
- else if (state.isVar || state.isVal) { // variable declaration
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.isIf) {
451
- return this._switch(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);
452
426
  }
453
- else if (state.isWhile) {
454
- 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;
455
431
  }
456
- else if (state.isExit) {
457
- return new JumpNode(state, "exit", this._unit(state.next(), scope), this._scope.type());
432
+ else if (state.equals("var", "val")) {
433
+ state.throwError("unexpected variable declaration");
458
434
  }
459
- else if (state.isReturn) {
460
- return new JumpNode(state, "return", this._unit(state.next(), scope), scope.type());
435
+ else if (state.equals("if")) {
436
+ return this._ternary(state, scope);
461
437
  }
462
- else if (state.isStop) {
463
- const node = new JumpNode(state, "stop");
464
- state.next();
465
- return node;
438
+ else if (state.equals("match")) {
439
+ return this._match(state, scope);
466
440
  }
467
- else if (state.isNext) {
468
- 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);
469
450
  state.next();
470
451
  return node;
471
452
  }
472
- else if (state.isEmpty) {
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().isParenthesesClose) {
483
- if (!state.isToken) {
496
+ while (!state.next().equals(")")) {
497
+ if (!state.isIdentifier) {
484
498
  state.throwError("missing function argument name");
485
499
  }
486
- const token = state.token;
487
- if (variables.has(token)) {
488
- 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}`);
489
503
  }
490
- if (scope.has(token)) {
491
- state.throwError(`variable ${token} redefinition`);
504
+ if (scope.has(identifier)) {
505
+ state.throwError(`variable ${identifier} redefinition`);
492
506
  }
493
- let argType = Type.Unknown;
494
- if (state.next().isColon) {
495
- variadic = state.next().isEllipsis;
507
+ let argType;
508
+ if (state.next().equals(":")) {
509
+ variadic = state.next().equals("...");
496
510
  if (variadic) {
497
511
  state.next();
498
512
  }
499
- argType = this._type(state, scope);
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
- else {
507
- state.throwError("variadic function argument must be an array type");
516
+ if (variadic) {
517
+ argType = Type.arrayType(argType);
508
518
  }
509
519
  }
510
- 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(",")) {
511
526
  break;
512
527
  }
513
528
  }
514
- state.closeParentheses().next();
515
- const returnType = state.isColon ? this._type(state.next(), scope) : Type.Unknown;
516
- 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();
517
533
  const args = Array.from(variables.values());
518
- const subscope = scope.subscope(variables, returnType);
519
- const subnode = this._unit(state, subscope);
520
- const createFunction = (frame) => (...values) => {
521
- const subframe = frame.subframe(subscope.variables());
522
- try {
523
- args.forEach((arg, ix) => subframe.set(arg, normalize(values[ix])));
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
- const constant = new Constant(createFunction, Type.functionType(returnType, args.map((v) => v.type), variadic));
539
- 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);
540
549
  }
541
- _loop(state, scope) {
550
+ _block(state, scope) {
542
551
  const span = state.start();
543
- 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
+ }
544
612
  span.end(state);
545
- 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);
546
619
  }
547
- _switch(state, scope) {
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
- const cthen = this._unit(state, scope);
552
- 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")
553
632
  ? this._unit(state.next(), scope)
554
633
  : new ConstantNode(state.start(), Constant.Null);
555
- return new SwitchNode(span, cnode, [cthen, celse]);
634
+ return new TernaryNode(span, cnode, [tnode, enode]);
556
635
  }
557
- _type(state, scope) {
558
- if (state.isType) { // literal type
559
- let type = state.type;
560
- if (state.next().isQuestion) {
561
- type = type.toOptional();
562
- 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);
563
649
  }
564
- if (state.operator === funcOr) { // type union
565
- 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)]);
566
654
  }
567
- return type;
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.closeBrackets().next();
587
- return colon
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
- else if (state.isParenthesesOpen) { // type grouping
619
- const type = this._type(state.next(), scope);
620
- state.closeParentheses().next();
621
- 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
+ }
622
679
  }
623
680
  else {
624
- state.throwError("missing type name");
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
  }