affinirum 1.5.2 → 1.5.3

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 (67) hide show
  1. package/README.md +12 -2
  2. package/dst/Affinirum.d.ts +22 -22
  3. package/dst/Affinirum.js +82 -75
  4. package/dst/CallFrame.d.ts +11 -0
  5. package/dst/CallFrame.js +35 -0
  6. package/dst/LexicalScope.d.ts +13 -0
  7. package/dst/LexicalScope.js +27 -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.js +82 -75
  19. package/dst/cjs/CallFrame.js +39 -0
  20. package/dst/cjs/LexicalScope.js +31 -0
  21. package/dst/cjs/Node.js +5 -5
  22. package/dst/cjs/ParserState.js +40 -40
  23. package/dst/cjs/{ParserFrame.js → ScriptSpan.js} +22 -33
  24. package/dst/cjs/Type.js +2 -0
  25. package/dst/cjs/Variable.js +0 -7
  26. package/dst/cjs/native/Number.js +24 -11
  27. package/dst/cjs/native/notation/AN.js +2 -2
  28. package/dst/cjs/node/ArrayNode.js +4 -11
  29. package/dst/cjs/node/BlockNode.js +10 -6
  30. package/dst/cjs/node/CallNode.js +15 -28
  31. package/dst/cjs/node/CastNode.js +4 -4
  32. package/dst/cjs/node/ConstantNode.js +7 -5
  33. package/dst/cjs/node/JumpNode.js +4 -4
  34. package/dst/cjs/node/LoopNode.js +5 -5
  35. package/dst/cjs/node/ObjectNode.js +6 -12
  36. package/dst/cjs/node/SwitchNode.js +8 -7
  37. package/dst/cjs/node/VariableNode.js +5 -5
  38. package/dst/cjs/node/VariadicNode.js +10 -0
  39. package/dst/native/Number.js +24 -11
  40. package/dst/native/notation/AN.js +2 -2
  41. package/dst/node/ArrayNode.d.ts +4 -3
  42. package/dst/node/ArrayNode.js +4 -11
  43. package/dst/node/BlockNode.d.ts +6 -3
  44. package/dst/node/BlockNode.js +10 -6
  45. package/dst/node/CallNode.d.ts +4 -3
  46. package/dst/node/CallNode.js +15 -28
  47. package/dst/node/CastNode.d.ts +4 -3
  48. package/dst/node/CastNode.js +4 -4
  49. package/dst/node/ConstantNode.d.ts +5 -3
  50. package/dst/node/ConstantNode.js +7 -5
  51. package/dst/node/JumpNode.d.ts +4 -3
  52. package/dst/node/JumpNode.js +4 -4
  53. package/dst/node/LoopNode.d.ts +4 -3
  54. package/dst/node/LoopNode.js +5 -5
  55. package/dst/node/ObjectNode.d.ts +4 -3
  56. package/dst/node/ObjectNode.js +6 -12
  57. package/dst/node/SwitchNode.d.ts +4 -3
  58. package/dst/node/SwitchNode.js +8 -7
  59. package/dst/node/VariableNode.d.ts +4 -3
  60. package/dst/node/VariableNode.js +5 -5
  61. package/dst/node/VariadicNode.d.ts +4 -0
  62. package/dst/node/VariadicNode.js +6 -0
  63. package/package.json +10 -9
  64. package/dst/ParserFrame.d.ts +0 -13
  65. package/dst/Scope.d.ts +0 -16
  66. package/dst/Scope.js +0 -49
  67. package/dst/cjs/Scope.js +0 -53
package/README.md CHANGED
@@ -173,7 +173,8 @@ Casts can be followed by property access, index access, function calls, or anoth
173
173
 
174
174
  Native conversions use the following **void** handling:
175
175
 
176
- - `Format` returns an empty string for **void**.
176
+ - General `Format` conversions and `JSON.Format` return an empty string for **void**.
177
+ - `AN.Format` and `Affinirum.format` serialize **void** as the literal `null`, including array elements and object properties.
177
178
  - `Encode` returns a zero-length buffer for **void**.
178
179
  - `Decode` and `Parse` return **void** when their input value is **void**.
179
180
 
@@ -240,7 +241,7 @@ Invalid non-void inputs are function-specific and may raise an evaluation error.
240
241
  - **Object.Merge(values: ...(object | [object])):object** — Merge objects and arrays of objects
241
242
 
242
243
  #### AN
243
- - **AN.Format(value: ??, whitespace: string?):string** — Format a value as AN notation
244
+ - **AN.Format(value: ??, whitespace: string?):string** — Format a value as AN notation; **void** values are written as `null`
244
245
 
245
246
  #### JSON
246
247
  - **JSON.Format(value: (void | boolean | timestamp | float | integer | string | array | object), whitespace: string?):string** — Format a value as JSON
@@ -409,8 +410,17 @@ the inferred input types through `variables()`, and the parsed tree through `toS
409
410
  ```ts
410
411
  const value = Affinirum.parse('["answer":42,"enabled":true]');
411
412
  const notation = Affinirum.format(value);
413
+
414
+ Affinirum.format(undefined); // "null"
415
+ Affinirum.format(null); // "null"
416
+ Affinirum.format([1n, undefined]); // "[1,null]"
417
+ Affinirum.format({ missing: undefined }); // '["missing":null]'
412
418
  ```
413
419
 
420
+ Both `Affinirum.format()` and `AN.Format()` write JavaScript `null` and `undefined` as the AN literal `null` at every nesting level.
421
+ Parsing that literal produces JavaScript `undefined`. This preserves array lengths and object properties containing **void** values,
422
+ including trailing array elements. Indented output follows the same rule.
423
+
414
424
  JavaScript `number` values map to **float**, while **integer** values map to `bigint`. Timestamps use `Date`, buffers use
415
425
  `ArrayBuffer`, and a **void** result is returned as `undefined`. Arrays, objects, and functions use their corresponding
416
426
  JavaScript values. Injected non-function values are checked against the types inferred or declared during compilation.
@@ -5,20 +5,20 @@ import { Type } from "./Type.js";
5
5
  import { Node } from "./Node.js";
6
6
  import { LoopNode } from "./node/LoopNode.js";
7
7
  import { SwitchNode } from "./node/SwitchNode.js";
8
- import { ParserFrame } from "./ParserFrame.js";
8
+ import { ScriptSpan } from "./ScriptSpan.js";
9
9
  import { ParserState } from "./ParserState.js";
10
- import { Scope } from "./Scope.js";
10
+ import { LexicalScope } from "./LexicalScope.js";
11
11
  export declare class Affinirum {
12
12
  static readonly keywords: string[];
13
13
  protected readonly _script: string;
14
14
  protected readonly _strict: boolean;
15
15
  protected readonly _root: Node;
16
16
  protected readonly _vartypes: Map<string, Type>;
17
- protected readonly _varframes: Map<string, ParserFrame>;
17
+ protected readonly _varspans: Map<string, ScriptSpan>;
18
18
  protected readonly _variables: Map<string, Variable>;
19
19
  protected readonly _constants: Map<string, Record<string, Constant>>;
20
20
  protected readonly _functions: Map<string, Constant>;
21
- protected readonly _scope: Scope;
21
+ protected readonly _scope: LexicalScope;
22
22
  /**
23
23
  Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
24
24
  @param script Math expression to compile.
@@ -63,26 +63,26 @@ export declare class Affinirum {
63
63
  */
64
64
  static parse(value: string | undefined): Value;
65
65
  /** Formats a JavaScript value as an Affinirum Notation string.
66
- @param value Value to format.
66
+ @param value Value to format. Null and undefined are formatted as the AN literal null.
67
67
  @param whitespace Optional whitespace used for each indentation level.
68
68
  @returns Affinirum Notation formatted string.
69
69
  */
70
70
  static format(value: Value, whitespace?: string): string;
71
- protected _list(state: ParserState, scope: Scope): Node;
72
- protected _unit(state: ParserState, scope: Scope): Node;
73
- protected _coalescence(state: ParserState, scope: Scope): Node;
74
- protected _disjunction(state: ParserState, scope: Scope): Node;
75
- protected _conjunction(state: ParserState, scope: Scope): Node;
76
- protected _negation(state: ParserState, scope: Scope): Node;
77
- protected _comparison(state: ParserState, scope: Scope): Node;
78
- protected _aggregate(state: ParserState, scope: Scope): Node;
79
- protected _product(state: ParserState, scope: Scope): Node;
80
- protected _signum(state: ParserState, scope: Scope): Node;
81
- protected _factor(state: ParserState, scope: Scope): Node;
82
- protected _accessor(state: ParserState, scope: Scope): Node;
83
- protected _term(state: ParserState, scope: Scope): Node;
84
- protected _function(state: ParserState, scope: Scope): Node;
85
- protected _loop(state: ParserState, scope: Scope): LoopNode;
86
- protected _switch(state: ParserState, scope: Scope): SwitchNode;
87
- protected _type(state: ParserState, scope: Scope): Type;
71
+ protected _list(state: ParserState, scope: LexicalScope): Node;
72
+ protected _unit(state: ParserState, scope: LexicalScope): Node;
73
+ protected _coalescence(state: ParserState, scope: LexicalScope): Node;
74
+ protected _disjunction(state: ParserState, scope: LexicalScope): Node;
75
+ protected _conjunction(state: ParserState, scope: LexicalScope): Node;
76
+ protected _negation(state: ParserState, scope: LexicalScope): Node;
77
+ protected _comparison(state: ParserState, scope: LexicalScope): Node;
78
+ protected _aggregate(state: ParserState, scope: LexicalScope): Node;
79
+ protected _product(state: ParserState, scope: LexicalScope): Node;
80
+ protected _signum(state: ParserState, scope: LexicalScope): Node;
81
+ protected _factor(state: ParserState, scope: LexicalScope): Node;
82
+ protected _accessor(state: ParserState, scope: LexicalScope): Node;
83
+ protected _term(state: ParserState, scope: LexicalScope): Node;
84
+ protected _function(state: ParserState, scope: LexicalScope): Node;
85
+ protected _loop(state: ParserState, scope: LexicalScope): LoopNode;
86
+ protected _switch(state: ParserState, scope: LexicalScope): SwitchNode;
87
+ protected _type(state: ParserState, scope: LexicalScope): Type;
88
88
  }
package/dst/Affinirum.js CHANGED
@@ -22,7 +22,8 @@ import { ObjectNode } from "./node/ObjectNode.js";
22
22
  import { SwitchNode } from "./node/SwitchNode.js";
23
23
  import { VariableNode } from "./node/VariableNode.js";
24
24
  import { ParserState } from "./ParserState.js";
25
- import { Scope } from "./Scope.js";
25
+ import { LexicalScope } from "./LexicalScope.js";
26
+ import { CallFrame } from "./CallFrame.js";
26
27
  import { JumpException } from "./JumpException.js";
27
28
  export class Affinirum {
28
29
  static keywords = [...Keywords, ...Constants.map((c) => c[0])];
@@ -30,11 +31,11 @@ export class Affinirum {
30
31
  _strict;
31
32
  _root;
32
33
  _vartypes = new Map();
33
- _varframes = new Map();
34
+ _varspans = new Map();
34
35
  _variables = new Map();
35
36
  _constants = new Map(Constants);
36
37
  _functions = new Map(Functions);
37
- _scope = new Scope();
38
+ _scope = new LexicalScope();
38
39
  /**
39
40
  Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
40
41
  @param script Math expression to compile.
@@ -94,18 +95,19 @@ export class Affinirum {
94
95
  @throws If an input value is missing or evaluation fails.
95
96
  */
96
97
  evaluate(values) {
98
+ const frame = new CallFrame(undefined, this._scope.variables());
97
99
  try {
98
100
  for (const [name, variable] of this._variables) {
99
101
  if (!values || !Object.hasOwn(values, name)) {
100
- this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
102
+ this._varspans.get(name)?.throwError(`undefined variable ${name}:\n`);
101
103
  }
102
104
  const value = normalize(values?.[name] ?? undefined);
103
105
  if (!variable.type.acceptValue(value)) {
104
- this._varframes.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
106
+ this._varspans.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
105
107
  }
106
- variable.value = value;
108
+ frame.add(variable, value);
107
109
  }
108
- return normalize(this._root.evaluate() ?? undefined);
110
+ return normalize(this._root.evaluate(frame) ?? undefined);
109
111
  }
110
112
  catch (e) {
111
113
  if (e instanceof JumpException) {
@@ -118,12 +120,6 @@ export class Affinirum {
118
120
  }
119
121
  throw e;
120
122
  }
121
- finally {
122
- for (const variable of this._variables.values()) {
123
- variable.value = undefined;
124
- }
125
- this._scope.clear();
126
- }
127
123
  }
128
124
  /** Parses an Affinirum Notation string into a JavaScript value.
129
125
  @param value Affinirum Notation string to parse.
@@ -134,7 +130,7 @@ export class Affinirum {
134
130
  return parseAN(value);
135
131
  }
136
132
  /** Formats a JavaScript value as an Affinirum Notation string.
137
- @param value Value to format.
133
+ @param value Value to format. Null and undefined are formatted as the AN literal null.
138
134
  @param whitespace Optional whitespace used for each indentation level.
139
135
  @returns Affinirum Notation formatted string.
140
136
  */
@@ -142,7 +138,7 @@ export class Affinirum {
142
138
  return formatAN(value, whitespace);
143
139
  }
144
140
  _list(state, scope) {
145
- const frame = state.starts();
141
+ const span = state.start();
146
142
  const subnodes = [];
147
143
  while (!state.isEmpty) {
148
144
  subnodes.push(this._unit(state, scope));
@@ -153,7 +149,7 @@ export class Affinirum {
153
149
  state.next();
154
150
  }
155
151
  }
156
- return new BlockNode(frame.ends(state), subnodes);
152
+ return new BlockNode(span.end(state), subnodes);
157
153
  }
158
154
  _unit(state, scope) {
159
155
  return this._coalescence(state, scope);
@@ -183,17 +179,17 @@ export class Affinirum {
183
179
  return node;
184
180
  }
185
181
  _negation(state, scope) {
186
- const frame = state.starts();
182
+ const span = state.start();
187
183
  let negate = false;
188
184
  while (state.operator === funcNot) {
189
185
  negate = !negate;
190
- frame.ends(state);
186
+ span.end(state);
191
187
  state.next();
192
188
  }
193
189
  let node = this._comparison(state, scope);
194
190
  if (negate) {
195
- const fnode = new ConstantNode(frame, funcNot);
196
- node = new CallNode(frame, fnode, [node]);
191
+ const fnode = new ConstantNode(span, funcNot);
192
+ node = new CallNode(span, fnode, [node]);
197
193
  }
198
194
  return node;
199
195
  }
@@ -224,19 +220,19 @@ export class Affinirum {
224
220
  return node;
225
221
  }
226
222
  _signum(state, scope) {
227
- const frame = state.starts();
223
+ const span = state.start();
228
224
  let negate = false;
229
225
  while (state.operator === funcAdd || state.operator === funcSubtract) {
230
226
  if (state.operator === funcSubtract) {
231
227
  negate = !negate;
232
228
  }
233
- frame.ends(state);
229
+ span.end(state);
234
230
  state.next();
235
231
  }
236
232
  let node = this._factor(state, scope);
237
233
  if (negate) {
238
- const fnode = new ConstantNode(frame, funcNegate);
239
- node = new CallNode(frame, fnode, [node]);
234
+ const fnode = new ConstantNode(span, funcNegate);
235
+ node = new CallNode(span, fnode, [node]);
240
236
  }
241
237
  return node;
242
238
  }
@@ -249,14 +245,14 @@ export class Affinirum {
249
245
  return node;
250
246
  }
251
247
  _accessor(state, scope) {
252
- const frame = state.starts();
248
+ const span = state.start();
253
249
  let node = this._term(state, scope);
254
250
  while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen || state.isCast) {
255
251
  if (state.isDot || state.isQuestion) { // property access or property existance operator
256
252
  const operator = state.isDot ? funcAt : funcHas;
257
253
  if (state.next().isLiteral && (typeof state.literal.value === "string" || typeof state.literal.value === "bigint")) {
258
- const fnode = new ConstantNode(frame.ends(state), operator);
259
- node = new CallNode(frame, fnode, [node, new ConstantNode(state, new Constant(state.literal.value))]);
254
+ const fnode = new ConstantNode(span.end(state), operator);
255
+ node = new CallNode(span, fnode, [node, new ConstantNode(state, new Constant(state.literal.value))]);
260
256
  state.next();
261
257
  }
262
258
  else if (state.isToken) {
@@ -270,18 +266,18 @@ export class Affinirum {
270
266
  break;
271
267
  }
272
268
  }
273
- const fnode = new ConstantNode(frame.ends(state), func);
274
- node = new CallNode(frame, fnode, subnodes);
269
+ const fnode = new ConstantNode(span.end(state), func);
270
+ node = new CallNode(span, fnode, subnodes);
275
271
  state.closeParentheses().next();
276
272
  }
277
273
  else {
278
- const fnode = new ConstantNode(frame.ends(state), func);
279
- node = new CallNode(frame, fnode, [node]);
274
+ const fnode = new ConstantNode(span.end(state), func);
275
+ node = new CallNode(span, fnode, [node]);
280
276
  }
281
277
  }
282
278
  else {
283
- const fnode = new ConstantNode(frame.ends(), operator);
284
- node = new CallNode(frame, fnode, [node, new ConstantNode(state, new Constant(state.token))]);
279
+ const fnode = new ConstantNode(span.end(state), operator);
280
+ node = new CallNode(span, fnode, [node, new ConstantNode(state, new Constant(state.token))]);
285
281
  state.next();
286
282
  }
287
283
  }
@@ -298,32 +294,32 @@ export class Affinirum {
298
294
  }
299
295
  }
300
296
  state.closeParentheses();
301
- node = new CallNode(frame.ends(state), node, subnodes);
297
+ node = new CallNode(span.end(state), node, subnodes);
302
298
  state.next();
303
299
  }
304
300
  else if (state.isBracketsOpen) { // index access operator
305
301
  const subnodes = [node, this._unit(state.next(), scope)];
306
302
  state.closeBrackets();
307
- const fnode = new ConstantNode(frame.ends(state), funcAt);
308
- node = new CallNode(frame, fnode, subnodes);
303
+ const fnode = new ConstantNode(span.end(state), funcAt);
304
+ node = new CallNode(span, fnode, subnodes);
309
305
  state.next();
310
306
  }
311
307
  else if (state.isCast) { // type cast operator
312
308
  const type = this._type(state.next(), scope);
313
- node = new CastNode(frame.ends(state), node, type);
309
+ node = new CastNode(span.end(state), node, type);
314
310
  }
315
311
  }
316
312
  return node;
317
313
  }
318
314
  _term(state, scope) {
319
315
  if (state.isLiteral) { // literal constant
320
- const frame = state.starts();
316
+ const span = state.start();
321
317
  const constant = new Constant(state.literal.value);
322
318
  state.next();
323
- return new ConstantNode(frame, constant);
319
+ return new ConstantNode(span, constant);
324
320
  }
325
321
  else if (state.isToken) { // variable
326
- const frame = state.starts();
322
+ const span = state.start();
327
323
  const constants = this._constants.get(state.token);
328
324
  if (constants != null) {
329
325
  if (!state.next().isDot) {
@@ -337,7 +333,7 @@ export class Affinirum {
337
333
  state.throwError(`unknown constant ${state.token}`);
338
334
  }
339
335
  state.next();
340
- return new ConstantNode(frame, constant);
336
+ return new ConstantNode(span, constant);
341
337
  }
342
338
  let variable = scope.get(state.token) ?? this._variables.get(state.token);
343
339
  if (variable == null) {
@@ -347,7 +343,7 @@ export class Affinirum {
347
343
  }
348
344
  variable = new Variable(type);
349
345
  this._variables.set(state.token, variable);
350
- this._varframes.set(state.token, state.starts());
346
+ this._varspans.set(state.token, state.start());
351
347
  }
352
348
  if (state.next().isAssignment) {
353
349
  if (!variable.assignable) {
@@ -355,19 +351,19 @@ export class Affinirum {
355
351
  }
356
352
  if (state.assignment.operator) {
357
353
  const operator = state.assignment.operator;
358
- const fnode = new ConstantNode(frame, operator);
359
- const subnode = new CallNode(frame, fnode, [new VariableNode(frame, variable), this._unit(state.next(), scope)]);
360
- return new VariableNode(frame, variable, subnode);
354
+ const fnode = new ConstantNode(span, operator);
355
+ const subnode = new CallNode(span, fnode, [new VariableNode(span, variable), this._unit(state.next(), scope)]);
356
+ return new VariableNode(span, variable, subnode);
361
357
  }
362
358
  else {
363
359
  const subnode = this._unit(state.next(), scope);
364
- return new VariableNode(frame, variable, subnode);
360
+ return new VariableNode(span, variable, subnode);
365
361
  }
366
362
  }
367
- return new VariableNode(frame, variable);
363
+ return new VariableNode(span, variable);
368
364
  }
369
365
  else if (state.isBracesOpen) { // code block
370
- const frame = state.starts();
366
+ const span = state.start();
371
367
  const subscope = scope.subscope();
372
368
  const subnodes = [];
373
369
  while (!state.next().isBracesClose) {
@@ -376,9 +372,9 @@ export class Affinirum {
376
372
  break;
377
373
  }
378
374
  }
379
- frame.ends(state);
375
+ span.end(state);
380
376
  state.closeBraces().next();
381
- return new BlockNode(frame, subnodes);
377
+ return new BlockNode(span, subnodes, [...subscope.variables()]);
382
378
  }
383
379
  else if (state.isBracesClose) {
384
380
  state.throwError("unexpected closing braces");
@@ -392,7 +388,7 @@ export class Affinirum {
392
388
  state.throwError("unexpected closing parentheses");
393
389
  }
394
390
  else if (state.isBracketsOpen) { // array or object
395
- const frame = state.starts();
391
+ const span = state.start();
396
392
  const subnodes = [];
397
393
  let index = 0, colon = false;
398
394
  while (!state.next().isBracketsClose) {
@@ -413,12 +409,12 @@ export class Affinirum {
413
409
  break;
414
410
  }
415
411
  }
416
- frame.ends(state);
412
+ span.end(state);
417
413
  state.closeBrackets().next();
418
414
  if (colon) {
419
- return new ObjectNode(frame, subnodes.map(([k, v]) => [typeof k === "number" ? new ConstantNode(v, new Constant(String(k))) : k, v]));
415
+ return new ObjectNode(span, subnodes.map(([k, v]) => [typeof k === "number" ? new ConstantNode(v, new Constant(String(k))) : k, v]));
420
416
  }
421
- return new ArrayNode(frame, subnodes.map(([, v]) => v));
417
+ return new ArrayNode(span, subnodes.map(([, v]) => v));
422
418
  }
423
419
  else if (state.isBracketsClose) {
424
420
  state.throwError("unexpected closing brackets");
@@ -432,7 +428,7 @@ export class Affinirum {
432
428
  if (scope.has(token)) {
433
429
  state.throwError(`illegal redefinition of variable ${token}`);
434
430
  }
435
- const frame = state.starts();
431
+ const span = state.start();
436
432
  let type;
437
433
  if (state.next().isColon) {
438
434
  type = this._type(state.next(), scope);
@@ -443,9 +439,9 @@ export class Affinirum {
443
439
  if (state.assignment.operator) {
444
440
  state.throwError(`illegal assignment operator with variable ${token}`);
445
441
  }
446
- return new VariableNode(frame, variable, this._unit(state.next(), scope));
442
+ return new VariableNode(span, variable, this._unit(state.next(), scope));
447
443
  }
448
- return new VariableNode(frame, variable);
444
+ return new VariableNode(span, variable);
449
445
  }
450
446
  else if (state.isTilda) {
451
447
  return this._function(state, scope);
@@ -478,7 +474,7 @@ export class Affinirum {
478
474
  state.throwError("unexpected expression token");
479
475
  }
480
476
  _function(state, scope) {
481
- const frame = state.starts();
477
+ const span = state.start();
482
478
  state.next().openParentheses();
483
479
  let variadic = false;
484
480
  const variables = new Map();
@@ -487,8 +483,11 @@ export class Affinirum {
487
483
  state.throwError("missing function argument name");
488
484
  }
489
485
  const token = state.token;
486
+ if (variables.has(token)) {
487
+ state.throwError(`duplicate function argument name ${token}`);
488
+ }
490
489
  if (scope.has(token)) {
491
- state.throwError("variable redefinition");
490
+ state.throwError(`variable ${token} redefinition`);
492
491
  }
493
492
  let argType = Type.Unknown;
494
493
  if (state.next().isColon) {
@@ -513,38 +512,46 @@ export class Affinirum {
513
512
  }
514
513
  state.closeParentheses().next();
515
514
  const retType = state.isColon ? this._type(state.next(), scope) : Type.Unknown;
516
- frame.ends(state);
515
+ span.end(state);
517
516
  const args = Array.from(variables.values());
518
517
  const subscope = scope.subscope(variables);
519
518
  const subnode = this._unit(state, subscope);
520
- const func = (...values) => {
521
- const previous = subscope.enter();
519
+ const createFunction = (frame) => (...values) => {
520
+ const subframe = frame.subframe(subscope.variables());
522
521
  try {
523
- args.forEach((arg, ix) => arg.value = normalize(values[ix]));
524
- return normalize(subnode.evaluate());
522
+ args.forEach((arg, ix) => subframe.set(arg, normalize(values[ix])));
523
+ return normalize(subnode.evaluate(subframe));
525
524
  }
526
- finally {
527
- subscope.leave(previous);
525
+ catch (e) {
526
+ if (e instanceof JumpException) {
527
+ if (e.jump === "return") {
528
+ return normalize(e.value);
529
+ }
530
+ if (e.jump === "stop" || e.jump === "next") {
531
+ subnode.throwError(`unexpected ${e.jump} jump`);
532
+ }
533
+ }
534
+ throw e;
528
535
  }
529
536
  };
530
- const constant = new Constant(func, Type.functionType(retType, args.map((v) => v.type), variadic));
531
- return new ConstantNode(frame, constant, subnode);
537
+ const constant = new Constant(createFunction, Type.functionType(retType, args.map((v) => v.type), variadic));
538
+ return new ConstantNode(span, constant, subnode, createFunction);
532
539
  }
533
540
  _loop(state, scope) {
534
- const frame = state.starts();
541
+ const span = state.start();
535
542
  const cnode = this._unit(state.next(), scope);
536
- frame.ends(state);
537
- return new LoopNode(frame, cnode, this._unit(state, scope));
543
+ span.end(state);
544
+ return new LoopNode(span, cnode, this._unit(state, scope));
538
545
  }
539
546
  _switch(state, scope) {
540
- const frame = state.starts();
547
+ const span = state.start();
541
548
  const cnode = this._unit(state.next(), scope);
542
- frame.ends(state);
549
+ span.end(state);
543
550
  const cthen = this._unit(state, scope);
544
551
  const celse = state.isElse
545
552
  ? this._unit(state.next(), scope)
546
- : new ConstantNode(state.starts(), Constant.Null);
547
- return new SwitchNode(frame, cnode, [cthen, celse]);
553
+ : new ConstantNode(state.start(), Constant.Null);
554
+ return new SwitchNode(span, cnode, [cthen, celse]);
548
555
  }
549
556
  _type(state, scope) {
550
557
  if (state.isType) { // literal type
@@ -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,35 @@
1
+ export class CallFrame {
2
+ _superframe;
3
+ _values = new Map();
4
+ constructor(superframe, variables = []) {
5
+ this._superframe = superframe;
6
+ for (const variable of variables) {
7
+ this.add(variable);
8
+ }
9
+ }
10
+ subframe(variables) {
11
+ return new CallFrame(this, variables);
12
+ }
13
+ add(variable, value = undefined) {
14
+ this._values.set(variable, value);
15
+ }
16
+ get(variable) {
17
+ if (this._values.has(variable)) {
18
+ return this._values.get(variable);
19
+ }
20
+ if (this._superframe) {
21
+ return this._superframe.get(variable);
22
+ }
23
+ throw new Error("variable is not declared in call frame");
24
+ }
25
+ set(variable, value) {
26
+ if (this._values.has(variable)) {
27
+ this._values.set(variable, value);
28
+ return value;
29
+ }
30
+ if (this._superframe) {
31
+ return this._superframe.set(variable, value);
32
+ }
33
+ throw new Error("variable is not declared in call frame");
34
+ }
35
+ }
@@ -0,0 +1,13 @@
1
+ import { Variable } from "./Variable.js";
2
+ export declare class LexicalScope {
3
+ protected readonly _superscope?: LexicalScope;
4
+ protected readonly _subscopes: LexicalScope[];
5
+ protected readonly _variables: Map<string, Variable>;
6
+ protected readonly _boundary: boolean;
7
+ constructor(superscope?: LexicalScope, variables?: Map<string, Variable>);
8
+ has(name: string): boolean;
9
+ get(name: string): Variable | undefined;
10
+ set(name: string, variable: Variable): void;
11
+ subscope(variables?: Map<string, Variable>): LexicalScope;
12
+ variables(): IterableIterator<Variable>;
13
+ }
@@ -0,0 +1,27 @@
1
+ export class LexicalScope {
2
+ _superscope;
3
+ _subscopes = [];
4
+ _variables;
5
+ _boundary;
6
+ constructor(superscope, variables) {
7
+ superscope?._subscopes.push(this);
8
+ this._superscope = superscope;
9
+ this._variables = variables ?? new Map();
10
+ this._boundary = Boolean(variables);
11
+ }
12
+ has(name) {
13
+ return this._variables.has(name) || Boolean(this._superscope?.has(name));
14
+ }
15
+ get(name) {
16
+ return this._variables.get(name) ?? this._superscope?.get(name);
17
+ }
18
+ set(name, variable) {
19
+ this._variables.set(name, variable);
20
+ }
21
+ subscope(variables) {
22
+ return new LexicalScope(this, variables);
23
+ }
24
+ variables() {
25
+ return this._variables.values();
26
+ }
27
+ }
package/dst/Node.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { ParserFrame } from "./ParserFrame.js";
1
+ import { ScriptSpan } from "./ScriptSpan.js";
2
2
  import { Type } from "./Type.js";
3
3
  import { Value } from "./Value.js";
4
- export declare abstract class Node extends ParserFrame {
5
- constructor(frame: ParserFrame);
4
+ import { CallFrame } from "./CallFrame.js";
5
+ export declare abstract class Node extends ScriptSpan {
6
+ constructor(span: ScriptSpan);
6
7
  abstract type: Type;
7
8
  abstract compile(type: Type): Node;
8
- abstract evaluate(): Value;
9
+ abstract evaluate(frame: CallFrame): Value;
9
10
  get constant(): boolean;
10
11
  protected reduceType(type: Type): Type;
11
12
  protected throwTypeError(type: Type): never;
package/dst/Node.js CHANGED
@@ -1,7 +1,7 @@
1
- import { ParserFrame } from "./ParserFrame.js";
2
- export class Node extends ParserFrame {
3
- constructor(frame) {
4
- super(frame.expr, frame.start, frame.end);
1
+ import { ScriptSpan } from "./ScriptSpan.js";
2
+ export class Node extends ScriptSpan {
3
+ constructor(span) {
4
+ super(span);
5
5
  }
6
6
  get constant() {
7
7
  return false;
@@ -17,6 +17,6 @@ export class Node extends ParserFrame {
17
17
  return this.throwError(`type ${this.type} mismatch with expected type ${type}`);
18
18
  }
19
19
  toString(ident = 0) {
20
- return " ".repeat(ident) + `[${this._start}:${this.end}] <${this.type}>`;
20
+ return " ".repeat(ident) + `[${this._start}:${this._end}] <${this.type}>`;
21
21
  }
22
22
  }
@@ -1,7 +1,7 @@
1
1
  import { Constant } from "./Constant.js";
2
2
  import { Value } from "./Value.js";
3
3
  import { Type } from "./Type.js";
4
- import { ParserFrame } from "./ParserFrame.js";
4
+ import { ScriptSpan } from "./ScriptSpan.js";
5
5
  declare class Literal {
6
6
  readonly value: Value;
7
7
  constructor(value: Value);
@@ -10,9 +10,9 @@ declare class Assignment {
10
10
  readonly operator?: Constant | undefined;
11
11
  constructor(operator?: Constant | undefined);
12
12
  }
13
- export declare class ParserState extends ParserFrame {
13
+ export declare class ParserState extends ScriptSpan {
14
14
  protected _fragment: Literal | Assignment | Constant | Type | symbol | string | undefined;
15
- constructor(expr: string);
15
+ constructor(script: string);
16
16
  get literal(): Literal;
17
17
  get assignment(): Assignment;
18
18
  get operator(): Constant;