affinirum 1.5.2 → 1.5.4

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 (121) hide show
  1. package/README.md +12 -2
  2. package/dst/Affinirum.d.ts +22 -22
  3. package/dst/Affinirum.js +88 -80
  4. package/dst/CallFrame.d.ts +11 -0
  5. package/dst/CallFrame.js +35 -0
  6. package/dst/LexicalScope.d.ts +15 -0
  7. package/dst/LexicalScope.js +31 -0
  8. package/dst/Node.d.ts +5 -4
  9. package/dst/Node.js +5 -5
  10. package/dst/ParserState.d.ts +3 -3
  11. package/dst/ParserState.js +40 -40
  12. package/dst/ScriptSpan.d.ts +10 -0
  13. package/dst/{ParserFrame.js → ScriptSpan.js} +20 -31
  14. package/dst/Type.d.ts +2 -0
  15. package/dst/Type.js +2 -0
  16. package/dst/Variable.d.ts +0 -4
  17. package/dst/Variable.js +0 -7
  18. package/dst/cjs/Affinirum.d.ts +88 -0
  19. package/dst/cjs/Affinirum.js +88 -80
  20. package/dst/cjs/Atom.d.ts +7 -0
  21. package/dst/cjs/CallFrame.d.ts +11 -0
  22. package/dst/cjs/CallFrame.js +39 -0
  23. package/dst/cjs/Constant.d.ts +13 -0
  24. package/dst/cjs/Constants.d.ts +2 -0
  25. package/dst/cjs/Functions.d.ts +2 -0
  26. package/dst/cjs/Jump.d.ts +1 -0
  27. package/dst/cjs/JumpException.d.ts +9 -0
  28. package/dst/cjs/Keywords.d.ts +1 -0
  29. package/dst/cjs/LexicalScope.d.ts +15 -0
  30. package/dst/cjs/LexicalScope.js +35 -0
  31. package/dst/cjs/Node.d.ts +14 -0
  32. package/dst/cjs/Node.js +5 -5
  33. package/dst/cjs/ParserState.d.ts +60 -0
  34. package/dst/cjs/ParserState.js +40 -40
  35. package/dst/cjs/ScriptSpan.d.ts +10 -0
  36. package/dst/cjs/{ParserFrame.js → ScriptSpan.js} +22 -33
  37. package/dst/cjs/Type.d.ts +71 -0
  38. package/dst/cjs/Type.js +2 -0
  39. package/dst/cjs/Value.d.ts +5 -0
  40. package/dst/cjs/Variable.d.ts +9 -0
  41. package/dst/cjs/Variable.js +0 -7
  42. package/dst/cjs/atom/ArrayAtom.d.ts +16 -0
  43. package/dst/cjs/atom/FunctionAtom.d.ts +22 -0
  44. package/dst/cjs/atom/ObjectAtom.d.ts +14 -0
  45. package/dst/cjs/atom/PrimitiveAtom.d.ts +12 -0
  46. package/dst/cjs/index.d.ts +3 -0
  47. package/dst/cjs/native/Aggregable.d.ts +4 -0
  48. package/dst/cjs/native/Array.d.ts +16 -0
  49. package/dst/cjs/native/Boolean.d.ts +6 -0
  50. package/dst/cjs/native/Buffer.d.ts +13 -0
  51. package/dst/cjs/native/Buffer.js +1 -1
  52. package/dst/cjs/native/Enumerable.d.ts +4 -0
  53. package/dst/cjs/native/Float.d.ts +5 -0
  54. package/dst/cjs/native/Integer.d.ts +7 -0
  55. package/dst/cjs/native/Iterable.d.ts +5 -0
  56. package/dst/cjs/native/Number.d.ts +26 -0
  57. package/dst/cjs/native/Number.js +25 -12
  58. package/dst/cjs/native/Object.d.ts +5 -0
  59. package/dst/cjs/native/String.d.ts +28 -0
  60. package/dst/cjs/native/String.js +97 -48
  61. package/dst/cjs/native/Timestamp.d.ts +22 -0
  62. package/dst/cjs/native/Unknown.d.ts +14 -0
  63. package/dst/cjs/native/Unknown.js +10 -1
  64. package/dst/cjs/native/notation/AN.d.ts +4 -0
  65. package/dst/cjs/native/notation/AN.js +2 -2
  66. package/dst/cjs/native/notation/IP.d.ts +1 -0
  67. package/dst/cjs/native/notation/JSON.d.ts +3 -0
  68. package/dst/cjs/node/ArrayNode.d.ts +14 -0
  69. package/dst/cjs/node/ArrayNode.js +4 -11
  70. package/dst/cjs/node/BlockNode.d.ts +15 -0
  71. package/dst/cjs/node/BlockNode.js +10 -6
  72. package/dst/cjs/node/CallNode.d.ts +15 -0
  73. package/dst/cjs/node/CallNode.js +15 -28
  74. package/dst/cjs/node/CastNode.d.ts +16 -0
  75. package/dst/cjs/node/CastNode.js +4 -4
  76. package/dst/cjs/node/ConstantNode.d.ts +17 -0
  77. package/dst/cjs/node/ConstantNode.js +7 -5
  78. package/dst/cjs/node/JumpNode.d.ts +16 -0
  79. package/dst/cjs/node/JumpNode.js +10 -6
  80. package/dst/cjs/node/LoopNode.d.ts +14 -0
  81. package/dst/cjs/node/LoopNode.js +5 -5
  82. package/dst/cjs/node/ObjectNode.d.ts +14 -0
  83. package/dst/cjs/node/ObjectNode.js +6 -12
  84. package/dst/cjs/node/SwitchNode.d.ts +15 -0
  85. package/dst/cjs/node/SwitchNode.js +8 -7
  86. package/dst/cjs/node/VariableNode.d.ts +15 -0
  87. package/dst/cjs/node/VariableNode.js +5 -5
  88. package/dst/cjs/node/VariadicNode.d.ts +4 -0
  89. package/dst/cjs/node/VariadicNode.js +10 -0
  90. package/dst/native/Buffer.js +1 -1
  91. package/dst/native/Number.js +25 -12
  92. package/dst/native/String.js +97 -48
  93. package/dst/native/Unknown.js +10 -1
  94. package/dst/native/notation/AN.js +2 -2
  95. package/dst/node/ArrayNode.d.ts +4 -3
  96. package/dst/node/ArrayNode.js +4 -11
  97. package/dst/node/BlockNode.d.ts +6 -3
  98. package/dst/node/BlockNode.js +10 -6
  99. package/dst/node/CallNode.d.ts +4 -3
  100. package/dst/node/CallNode.js +15 -28
  101. package/dst/node/CastNode.d.ts +4 -3
  102. package/dst/node/CastNode.js +4 -4
  103. package/dst/node/ConstantNode.d.ts +5 -3
  104. package/dst/node/ConstantNode.js +7 -5
  105. package/dst/node/JumpNode.d.ts +5 -3
  106. package/dst/node/JumpNode.js +10 -6
  107. package/dst/node/LoopNode.d.ts +4 -3
  108. package/dst/node/LoopNode.js +5 -5
  109. package/dst/node/ObjectNode.d.ts +4 -3
  110. package/dst/node/ObjectNode.js +6 -12
  111. package/dst/node/SwitchNode.d.ts +4 -3
  112. package/dst/node/SwitchNode.js +8 -7
  113. package/dst/node/VariableNode.d.ts +4 -3
  114. package/dst/node/VariableNode.js +5 -5
  115. package/dst/node/VariadicNode.d.ts +4 -0
  116. package/dst/node/VariadicNode.js +6 -0
  117. package/package.json +10 -9
  118. package/dst/ParserFrame.d.ts +0 -13
  119. package/dst/Scope.d.ts +0 -16
  120. package/dst/Scope.js +0 -49
  121. package/dst/cjs/Scope.js +0 -53
@@ -25,7 +25,8 @@ const ObjectNode_js_1 = require("./node/ObjectNode.js");
25
25
  const SwitchNode_js_1 = require("./node/SwitchNode.js");
26
26
  const VariableNode_js_1 = require("./node/VariableNode.js");
27
27
  const ParserState_js_1 = require("./ParserState.js");
28
- const Scope_js_1 = require("./Scope.js");
28
+ const LexicalScope_js_1 = require("./LexicalScope.js");
29
+ const CallFrame_js_1 = require("./CallFrame.js");
29
30
  const JumpException_js_1 = require("./JumpException.js");
30
31
  class Affinirum {
31
32
  static keywords = [...Keywords_js_1.Keywords, ...Constants_js_1.Constants.map((c) => c[0])];
@@ -33,11 +34,11 @@ class Affinirum {
33
34
  _strict;
34
35
  _root;
35
36
  _vartypes = new Map();
36
- _varframes = new Map();
37
+ _varspans = new Map();
37
38
  _variables = new Map();
38
39
  _constants = new Map(Constants_js_1.Constants);
39
40
  _functions = new Map(Functions_js_1.Functions);
40
- _scope = new Scope_js_1.Scope();
41
+ _scope;
41
42
  /**
42
43
  Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
43
44
  @param script Math expression to compile.
@@ -47,6 +48,7 @@ class Affinirum {
47
48
  */
48
49
  constructor(script, config) {
49
50
  this._script = script;
51
+ this._scope = new LexicalScope_js_1.LexicalScope(undefined, undefined, config?.type ?? Type_js_1.Type.Unknown);
50
52
  this._strict = config?.strict ?? false;
51
53
  if (config?.variables) {
52
54
  for (const [vname, vtype] of Object.entries(config.variables)) {
@@ -58,7 +60,7 @@ class Affinirum {
58
60
  if (!state.isEmpty) {
59
61
  state.throwError("unexpected expression token or expression end");
60
62
  }
61
- this._root = this._root.compile(config?.type ?? Type_js_1.Type.Unknown);
63
+ this._root = this._root.compile(this._scope.type());
62
64
  }
63
65
  /**
64
66
  Returns original script text.
@@ -97,18 +99,19 @@ class Affinirum {
97
99
  @throws If an input value is missing or evaluation fails.
98
100
  */
99
101
  evaluate(values) {
102
+ const frame = new CallFrame_js_1.CallFrame(undefined, this._scope.variables());
100
103
  try {
101
104
  for (const [name, variable] of this._variables) {
102
105
  if (!values || !Object.hasOwn(values, name)) {
103
- this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
106
+ this._varspans.get(name)?.throwError(`undefined variable ${name}:\n`);
104
107
  }
105
108
  const value = (0, Value_js_1.normalize)(values?.[name] ?? undefined);
106
109
  if (!variable.type.acceptValue(value)) {
107
- this._varframes.get(name)?.throwError(`unexpected type ${Type_js_1.Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
110
+ this._varspans.get(name)?.throwError(`unexpected type ${Type_js_1.Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
108
111
  }
109
- variable.value = value;
112
+ frame.add(variable, value);
110
113
  }
111
- return (0, Value_js_1.normalize)(this._root.evaluate() ?? undefined);
114
+ return (0, Value_js_1.normalize)(this._root.evaluate(frame) ?? undefined);
112
115
  }
113
116
  catch (e) {
114
117
  if (e instanceof JumpException_js_1.JumpException) {
@@ -121,12 +124,6 @@ class Affinirum {
121
124
  }
122
125
  throw e;
123
126
  }
124
- finally {
125
- for (const variable of this._variables.values()) {
126
- variable.value = undefined;
127
- }
128
- this._scope.clear();
129
- }
130
127
  }
131
128
  /** Parses an Affinirum Notation string into a JavaScript value.
132
129
  @param value Affinirum Notation string to parse.
@@ -137,7 +134,7 @@ class Affinirum {
137
134
  return (0, AN_js_1.parseAN)(value);
138
135
  }
139
136
  /** Formats a JavaScript value as an Affinirum Notation string.
140
- @param value Value to format.
137
+ @param value Value to format. Null and undefined are formatted as the AN literal null.
141
138
  @param whitespace Optional whitespace used for each indentation level.
142
139
  @returns Affinirum Notation formatted string.
143
140
  */
@@ -145,7 +142,7 @@ class Affinirum {
145
142
  return (0, AN_js_1.formatAN)(value, whitespace);
146
143
  }
147
144
  _list(state, scope) {
148
- const frame = state.starts();
145
+ const span = state.start();
149
146
  const subnodes = [];
150
147
  while (!state.isEmpty) {
151
148
  subnodes.push(this._unit(state, scope));
@@ -156,7 +153,7 @@ class Affinirum {
156
153
  state.next();
157
154
  }
158
155
  }
159
- return new BlockNode_js_1.BlockNode(frame.ends(state), subnodes);
156
+ return new BlockNode_js_1.BlockNode(span.end(state), subnodes);
160
157
  }
161
158
  _unit(state, scope) {
162
159
  return this._coalescence(state, scope);
@@ -186,17 +183,17 @@ class Affinirum {
186
183
  return node;
187
184
  }
188
185
  _negation(state, scope) {
189
- const frame = state.starts();
186
+ const span = state.start();
190
187
  let negate = false;
191
188
  while (state.operator === Boolean_js_1.funcNot) {
192
189
  negate = !negate;
193
- frame.ends(state);
190
+ span.end(state);
194
191
  state.next();
195
192
  }
196
193
  let node = this._comparison(state, scope);
197
194
  if (negate) {
198
- const fnode = new ConstantNode_js_1.ConstantNode(frame, Boolean_js_1.funcNot);
199
- node = new CallNode_js_1.CallNode(frame, fnode, [node]);
195
+ const fnode = new ConstantNode_js_1.ConstantNode(span, Boolean_js_1.funcNot);
196
+ node = new CallNode_js_1.CallNode(span, fnode, [node]);
200
197
  }
201
198
  return node;
202
199
  }
@@ -227,19 +224,19 @@ class Affinirum {
227
224
  return node;
228
225
  }
229
226
  _signum(state, scope) {
230
- const frame = state.starts();
227
+ const span = state.start();
231
228
  let negate = false;
232
229
  while (state.operator === Aggregable_js_1.funcAdd || state.operator === Number_js_1.funcSubtract) {
233
230
  if (state.operator === Number_js_1.funcSubtract) {
234
231
  negate = !negate;
235
232
  }
236
- frame.ends(state);
233
+ span.end(state);
237
234
  state.next();
238
235
  }
239
236
  let node = this._factor(state, scope);
240
237
  if (negate) {
241
- const fnode = new ConstantNode_js_1.ConstantNode(frame, Number_js_1.funcNegate);
242
- node = new CallNode_js_1.CallNode(frame, fnode, [node]);
238
+ const fnode = new ConstantNode_js_1.ConstantNode(span, Number_js_1.funcNegate);
239
+ node = new CallNode_js_1.CallNode(span, fnode, [node]);
243
240
  }
244
241
  return node;
245
242
  }
@@ -252,14 +249,14 @@ class Affinirum {
252
249
  return node;
253
250
  }
254
251
  _accessor(state, scope) {
255
- const frame = state.starts();
252
+ const span = state.start();
256
253
  let node = this._term(state, scope);
257
254
  while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen || state.isCast) {
258
255
  if (state.isDot || state.isQuestion) { // property access or property existance operator
259
256
  const operator = state.isDot ? Iterable_js_1.funcAt : Iterable_js_1.funcHas;
260
257
  if (state.next().isLiteral && (typeof state.literal.value === "string" || typeof state.literal.value === "bigint")) {
261
- const fnode = new ConstantNode_js_1.ConstantNode(frame.ends(state), operator);
262
- node = new CallNode_js_1.CallNode(frame, fnode, [node, new ConstantNode_js_1.ConstantNode(state, new Constant_js_1.Constant(state.literal.value))]);
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))]);
263
260
  state.next();
264
261
  }
265
262
  else if (state.isToken) {
@@ -273,18 +270,18 @@ class Affinirum {
273
270
  break;
274
271
  }
275
272
  }
276
- const fnode = new ConstantNode_js_1.ConstantNode(frame.ends(state), func);
277
- node = new CallNode_js_1.CallNode(frame, fnode, subnodes);
273
+ const fnode = new ConstantNode_js_1.ConstantNode(span.end(state), func);
274
+ node = new CallNode_js_1.CallNode(span, fnode, subnodes);
278
275
  state.closeParentheses().next();
279
276
  }
280
277
  else {
281
- const fnode = new ConstantNode_js_1.ConstantNode(frame.ends(state), func);
282
- node = new CallNode_js_1.CallNode(frame, fnode, [node]);
278
+ const fnode = new ConstantNode_js_1.ConstantNode(span.end(state), func);
279
+ node = new CallNode_js_1.CallNode(span, fnode, [node]);
283
280
  }
284
281
  }
285
282
  else {
286
- const fnode = new ConstantNode_js_1.ConstantNode(frame.ends(), operator);
287
- node = new CallNode_js_1.CallNode(frame, fnode, [node, new ConstantNode_js_1.ConstantNode(state, new Constant_js_1.Constant(state.token))]);
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))]);
288
285
  state.next();
289
286
  }
290
287
  }
@@ -301,32 +298,32 @@ class Affinirum {
301
298
  }
302
299
  }
303
300
  state.closeParentheses();
304
- node = new CallNode_js_1.CallNode(frame.ends(state), node, subnodes);
301
+ node = new CallNode_js_1.CallNode(span.end(state), node, subnodes);
305
302
  state.next();
306
303
  }
307
304
  else if (state.isBracketsOpen) { // index access operator
308
305
  const subnodes = [node, this._unit(state.next(), scope)];
309
306
  state.closeBrackets();
310
- const fnode = new ConstantNode_js_1.ConstantNode(frame.ends(state), Iterable_js_1.funcAt);
311
- node = new CallNode_js_1.CallNode(frame, fnode, subnodes);
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);
312
309
  state.next();
313
310
  }
314
311
  else if (state.isCast) { // type cast operator
315
312
  const type = this._type(state.next(), scope);
316
- node = new CastNode_js_1.CastNode(frame.ends(state), node, type);
313
+ node = new CastNode_js_1.CastNode(span.end(state), node, type);
317
314
  }
318
315
  }
319
316
  return node;
320
317
  }
321
318
  _term(state, scope) {
322
319
  if (state.isLiteral) { // literal constant
323
- const frame = state.starts();
320
+ const span = state.start();
324
321
  const constant = new Constant_js_1.Constant(state.literal.value);
325
322
  state.next();
326
- return new ConstantNode_js_1.ConstantNode(frame, constant);
323
+ return new ConstantNode_js_1.ConstantNode(span, constant);
327
324
  }
328
325
  else if (state.isToken) { // variable
329
- const frame = state.starts();
326
+ const span = state.start();
330
327
  const constants = this._constants.get(state.token);
331
328
  if (constants != null) {
332
329
  if (!state.next().isDot) {
@@ -340,7 +337,7 @@ class Affinirum {
340
337
  state.throwError(`unknown constant ${state.token}`);
341
338
  }
342
339
  state.next();
343
- return new ConstantNode_js_1.ConstantNode(frame, constant);
340
+ return new ConstantNode_js_1.ConstantNode(span, constant);
344
341
  }
345
342
  let variable = scope.get(state.token) ?? this._variables.get(state.token);
346
343
  if (variable == null) {
@@ -350,7 +347,7 @@ class Affinirum {
350
347
  }
351
348
  variable = new Variable_js_1.Variable(type);
352
349
  this._variables.set(state.token, variable);
353
- this._varframes.set(state.token, state.starts());
350
+ this._varspans.set(state.token, state.start());
354
351
  }
355
352
  if (state.next().isAssignment) {
356
353
  if (!variable.assignable) {
@@ -358,19 +355,19 @@ class Affinirum {
358
355
  }
359
356
  if (state.assignment.operator) {
360
357
  const operator = state.assignment.operator;
361
- const fnode = new ConstantNode_js_1.ConstantNode(frame, operator);
362
- const subnode = new CallNode_js_1.CallNode(frame, fnode, [new VariableNode_js_1.VariableNode(frame, variable), this._unit(state.next(), scope)]);
363
- return new VariableNode_js_1.VariableNode(frame, variable, subnode);
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);
364
361
  }
365
362
  else {
366
363
  const subnode = this._unit(state.next(), scope);
367
- return new VariableNode_js_1.VariableNode(frame, variable, subnode);
364
+ return new VariableNode_js_1.VariableNode(span, variable, subnode);
368
365
  }
369
366
  }
370
- return new VariableNode_js_1.VariableNode(frame, variable);
367
+ return new VariableNode_js_1.VariableNode(span, variable);
371
368
  }
372
369
  else if (state.isBracesOpen) { // code block
373
- const frame = state.starts();
370
+ const span = state.start();
374
371
  const subscope = scope.subscope();
375
372
  const subnodes = [];
376
373
  while (!state.next().isBracesClose) {
@@ -379,9 +376,9 @@ class Affinirum {
379
376
  break;
380
377
  }
381
378
  }
382
- frame.ends(state);
379
+ span.end(state);
383
380
  state.closeBraces().next();
384
- return new BlockNode_js_1.BlockNode(frame, subnodes);
381
+ return new BlockNode_js_1.BlockNode(span, subnodes, [...subscope.variables()]);
385
382
  }
386
383
  else if (state.isBracesClose) {
387
384
  state.throwError("unexpected closing braces");
@@ -395,7 +392,7 @@ class Affinirum {
395
392
  state.throwError("unexpected closing parentheses");
396
393
  }
397
394
  else if (state.isBracketsOpen) { // array or object
398
- const frame = state.starts();
395
+ const span = state.start();
399
396
  const subnodes = [];
400
397
  let index = 0, colon = false;
401
398
  while (!state.next().isBracketsClose) {
@@ -416,12 +413,12 @@ class Affinirum {
416
413
  break;
417
414
  }
418
415
  }
419
- frame.ends(state);
416
+ span.end(state);
420
417
  state.closeBrackets().next();
421
418
  if (colon) {
422
- return new ObjectNode_js_1.ObjectNode(frame, subnodes.map(([k, v]) => [typeof k === "number" ? new ConstantNode_js_1.ConstantNode(v, new Constant_js_1.Constant(String(k))) : k, v]));
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]));
423
420
  }
424
- return new ArrayNode_js_1.ArrayNode(frame, subnodes.map(([, v]) => v));
421
+ return new ArrayNode_js_1.ArrayNode(span, subnodes.map(([, v]) => v));
425
422
  }
426
423
  else if (state.isBracketsClose) {
427
424
  state.throwError("unexpected closing brackets");
@@ -435,7 +432,7 @@ class Affinirum {
435
432
  if (scope.has(token)) {
436
433
  state.throwError(`illegal redefinition of variable ${token}`);
437
434
  }
438
- const frame = state.starts();
435
+ const span = state.start();
439
436
  let type;
440
437
  if (state.next().isColon) {
441
438
  type = this._type(state.next(), scope);
@@ -446,9 +443,9 @@ class Affinirum {
446
443
  if (state.assignment.operator) {
447
444
  state.throwError(`illegal assignment operator with variable ${token}`);
448
445
  }
449
- return new VariableNode_js_1.VariableNode(frame, variable, this._unit(state.next(), scope));
446
+ return new VariableNode_js_1.VariableNode(span, variable, this._unit(state.next(), scope));
450
447
  }
451
- return new VariableNode_js_1.VariableNode(frame, variable);
448
+ return new VariableNode_js_1.VariableNode(span, variable);
452
449
  }
453
450
  else if (state.isTilda) {
454
451
  return this._function(state, scope);
@@ -460,10 +457,10 @@ class Affinirum {
460
457
  return this._loop(state, scope);
461
458
  }
462
459
  else if (state.isExit) {
463
- return new JumpNode_js_1.JumpNode(state, "exit", this._unit(state.next(), scope));
460
+ return new JumpNode_js_1.JumpNode(state, "exit", this._unit(state.next(), scope), this._scope.type());
464
461
  }
465
462
  else if (state.isReturn) {
466
- return new JumpNode_js_1.JumpNode(state, "return", this._unit(state.next(), scope));
463
+ return new JumpNode_js_1.JumpNode(state, "return", this._unit(state.next(), scope), scope.type());
467
464
  }
468
465
  else if (state.isStop) {
469
466
  const node = new JumpNode_js_1.JumpNode(state, "stop");
@@ -481,7 +478,7 @@ class Affinirum {
481
478
  state.throwError("unexpected expression token");
482
479
  }
483
480
  _function(state, scope) {
484
- const frame = state.starts();
481
+ const span = state.start();
485
482
  state.next().openParentheses();
486
483
  let variadic = false;
487
484
  const variables = new Map();
@@ -490,8 +487,11 @@ class Affinirum {
490
487
  state.throwError("missing function argument name");
491
488
  }
492
489
  const token = state.token;
490
+ if (variables.has(token)) {
491
+ state.throwError(`duplicate function argument name ${token}`);
492
+ }
493
493
  if (scope.has(token)) {
494
- state.throwError("variable redefinition");
494
+ state.throwError(`variable ${token} redefinition`);
495
495
  }
496
496
  let argType = Type_js_1.Type.Unknown;
497
497
  if (state.next().isColon) {
@@ -515,39 +515,47 @@ class Affinirum {
515
515
  }
516
516
  }
517
517
  state.closeParentheses().next();
518
- const retType = state.isColon ? this._type(state.next(), scope) : Type_js_1.Type.Unknown;
519
- frame.ends(state);
518
+ const returnType = state.isColon ? this._type(state.next(), scope) : Type_js_1.Type.Unknown;
519
+ span.end(state);
520
520
  const args = Array.from(variables.values());
521
- const subscope = scope.subscope(variables);
521
+ const subscope = scope.subscope(variables, returnType);
522
522
  const subnode = this._unit(state, subscope);
523
- const func = (...values) => {
524
- const previous = subscope.enter();
523
+ const createFunction = (frame) => (...values) => {
524
+ const subframe = frame.subframe(subscope.variables());
525
525
  try {
526
- args.forEach((arg, ix) => arg.value = (0, Value_js_1.normalize)(values[ix]));
527
- return (0, Value_js_1.normalize)(subnode.evaluate());
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
528
  }
529
- finally {
530
- subscope.leave(previous);
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;
531
539
  }
532
540
  };
533
- const constant = new Constant_js_1.Constant(func, Type_js_1.Type.functionType(retType, args.map((v) => v.type), variadic));
534
- return new ConstantNode_js_1.ConstantNode(frame, constant, subnode);
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);
535
543
  }
536
544
  _loop(state, scope) {
537
- const frame = state.starts();
545
+ const span = state.start();
538
546
  const cnode = this._unit(state.next(), scope);
539
- frame.ends(state);
540
- return new LoopNode_js_1.LoopNode(frame, cnode, this._unit(state, scope));
547
+ span.end(state);
548
+ return new LoopNode_js_1.LoopNode(span, cnode, this._unit(state, scope));
541
549
  }
542
550
  _switch(state, scope) {
543
- const frame = state.starts();
551
+ const span = state.start();
544
552
  const cnode = this._unit(state.next(), scope);
545
- frame.ends(state);
553
+ span.end(state);
546
554
  const cthen = this._unit(state, scope);
547
555
  const celse = state.isElse
548
556
  ? this._unit(state.next(), scope)
549
- : new ConstantNode_js_1.ConstantNode(state.starts(), Constant_js_1.Constant.Null);
550
- return new SwitchNode_js_1.SwitchNode(frame, cnode, [cthen, celse]);
557
+ : new ConstantNode_js_1.ConstantNode(state.start(), Constant_js_1.Constant.Null);
558
+ return new SwitchNode_js_1.SwitchNode(span, cnode, [cthen, celse]);
551
559
  }
552
560
  _type(state, scope) {
553
561
  if (state.isType) { // literal type
@@ -0,0 +1,7 @@
1
+ export interface IAtom {
2
+ equate(atom: IAtom): boolean;
3
+ accept(atom: IAtom): boolean;
4
+ intersect(atom: IAtom): boolean;
5
+ weight(): number;
6
+ toString(): string;
7
+ }
@@ -0,0 +1,11 @@
1
+ import type { Value } from "./Value.js";
2
+ import type { Variable } from "./Variable.js";
3
+ export declare class CallFrame {
4
+ protected readonly _superframe?: CallFrame;
5
+ protected readonly _values: Map<Variable, Value>;
6
+ constructor(superframe?: CallFrame, variables?: Iterable<Variable>);
7
+ subframe(variables: Iterable<Variable>): CallFrame;
8
+ add(variable: Variable, value?: Value): void;
9
+ get(variable: Variable): Value;
10
+ set(variable: Variable, value: Value): Value;
11
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CallFrame = void 0;
4
+ class CallFrame {
5
+ _superframe;
6
+ _values = new Map();
7
+ constructor(superframe, variables = []) {
8
+ this._superframe = superframe;
9
+ for (const variable of variables) {
10
+ this.add(variable);
11
+ }
12
+ }
13
+ subframe(variables) {
14
+ return new CallFrame(this, variables);
15
+ }
16
+ add(variable, value = undefined) {
17
+ this._values.set(variable, value);
18
+ }
19
+ get(variable) {
20
+ if (this._values.has(variable)) {
21
+ return this._values.get(variable);
22
+ }
23
+ if (this._superframe) {
24
+ return this._superframe.get(variable);
25
+ }
26
+ throw new Error("variable is not declared in call frame");
27
+ }
28
+ set(variable, value) {
29
+ if (this._values.has(variable)) {
30
+ this._values.set(variable, value);
31
+ return value;
32
+ }
33
+ if (this._superframe) {
34
+ return this._superframe.set(variable, value);
35
+ }
36
+ throw new Error("variable is not declared in call frame");
37
+ }
38
+ }
39
+ exports.CallFrame = CallFrame;
@@ -0,0 +1,13 @@
1
+ import { Value } from "./Value.js";
2
+ import { Type } from "./Type.js";
3
+ export declare class Constant {
4
+ protected readonly _value: Value;
5
+ protected _type: Type;
6
+ protected readonly _deterministic: boolean;
7
+ constructor(_value: Value, _type?: Type, _deterministic?: boolean);
8
+ get value(): Value;
9
+ get type(): Type;
10
+ set type(type: Type);
11
+ get deterministic(): boolean;
12
+ static Null: Constant;
13
+ }
@@ -0,0 +1,2 @@
1
+ import { Constant } from "./Constant.js";
2
+ export declare const Constants: [string, Record<string, Constant>][];
@@ -0,0 +1,2 @@
1
+ import { Constant } from "./Constant.js";
2
+ export declare const Functions: [string, Constant][];
@@ -0,0 +1 @@
1
+ export type Jump = "exit" | "return" | "stop" | "next";
@@ -0,0 +1,9 @@
1
+ import { Jump } from "./Jump.js";
2
+ import { Value } from "./Value.js";
3
+ export declare class JumpException {
4
+ private readonly _jump;
5
+ private readonly _value?;
6
+ constructor(_jump: Jump, _value?: Value);
7
+ get jump(): Jump;
8
+ get value(): Value;
9
+ }
@@ -0,0 +1 @@
1
+ export declare const Keywords: string[];
@@ -0,0 +1,15 @@
1
+ import { Type } from "./Type.js";
2
+ import { Variable } from "./Variable.js";
3
+ export declare class LexicalScope {
4
+ protected readonly _superscope?: LexicalScope;
5
+ protected readonly _subscopes: LexicalScope[];
6
+ protected readonly _variables: Map<string, Variable>;
7
+ protected readonly _type: Type | undefined;
8
+ constructor(superscope?: LexicalScope, variables?: Map<string, Variable>, type?: Type);
9
+ has(name: string): boolean;
10
+ get(name: string): Variable | undefined;
11
+ set(name: string, variable: Variable): void;
12
+ subscope(variables?: Map<string, Variable>, type?: Type): LexicalScope;
13
+ variables(): IterableIterator<Variable>;
14
+ type(): Type;
15
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LexicalScope = void 0;
4
+ const Type_js_1 = require("./Type.js");
5
+ class LexicalScope {
6
+ _superscope;
7
+ _subscopes = [];
8
+ _variables;
9
+ _type;
10
+ constructor(superscope, variables, type) {
11
+ superscope?._subscopes.push(this);
12
+ this._superscope = superscope;
13
+ this._variables = variables ?? new Map();
14
+ this._type = type;
15
+ }
16
+ has(name) {
17
+ return this._variables.has(name) || Boolean(this._superscope?.has(name));
18
+ }
19
+ get(name) {
20
+ return this._variables.get(name) ?? this._superscope?.get(name);
21
+ }
22
+ set(name, variable) {
23
+ this._variables.set(name, variable);
24
+ }
25
+ subscope(variables, type) {
26
+ return new LexicalScope(this, variables, type);
27
+ }
28
+ variables() {
29
+ return this._variables.values();
30
+ }
31
+ type() {
32
+ return this._type ?? this._superscope?.type() ?? Type_js_1.Type.Unknown;
33
+ }
34
+ }
35
+ exports.LexicalScope = LexicalScope;
@@ -0,0 +1,14 @@
1
+ import { ScriptSpan } from "./ScriptSpan.js";
2
+ import { Type } from "./Type.js";
3
+ import { Value } from "./Value.js";
4
+ import { CallFrame } from "./CallFrame.js";
5
+ export declare abstract class Node extends ScriptSpan {
6
+ constructor(span: ScriptSpan);
7
+ abstract type: Type;
8
+ abstract compile(type: Type): Node;
9
+ abstract evaluate(frame: CallFrame): Value;
10
+ get constant(): boolean;
11
+ protected reduceType(type: Type): Type;
12
+ protected throwTypeError(type: Type): never;
13
+ toString(ident?: number): string;
14
+ }
package/dst/cjs/Node.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Node = void 0;
4
- const ParserFrame_js_1 = require("./ParserFrame.js");
5
- class Node extends ParserFrame_js_1.ParserFrame {
6
- constructor(frame) {
7
- super(frame.expr, frame.start, frame.end);
4
+ const ScriptSpan_js_1 = require("./ScriptSpan.js");
5
+ class Node extends ScriptSpan_js_1.ScriptSpan {
6
+ constructor(span) {
7
+ super(span);
8
8
  }
9
9
  get constant() {
10
10
  return false;
@@ -20,7 +20,7 @@ class Node extends ParserFrame_js_1.ParserFrame {
20
20
  return this.throwError(`type ${this.type} mismatch with expected type ${type}`);
21
21
  }
22
22
  toString(ident = 0) {
23
- return " ".repeat(ident) + `[${this._start}:${this.end}] <${this.type}>`;
23
+ return " ".repeat(ident) + `[${this._start}:${this._end}] <${this.type}>`;
24
24
  }
25
25
  }
26
26
  exports.Node = Node;