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