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
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;
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.
@@ -44,6 +45,7 @@ export class Affinirum {
44
45
  */
45
46
  constructor(script, config) {
46
47
  this._script = script;
48
+ this._scope = new LexicalScope(undefined, undefined, config?.type ?? Type.Unknown);
47
49
  this._strict = config?.strict ?? false;
48
50
  if (config?.variables) {
49
51
  for (const [vname, vtype] of Object.entries(config.variables)) {
@@ -55,7 +57,7 @@ export class Affinirum {
55
57
  if (!state.isEmpty) {
56
58
  state.throwError("unexpected expression token or expression end");
57
59
  }
58
- this._root = this._root.compile(config?.type ?? Type.Unknown);
60
+ this._root = this._root.compile(this._scope.type());
59
61
  }
60
62
  /**
61
63
  Returns original script text.
@@ -94,18 +96,19 @@ export class Affinirum {
94
96
  @throws If an input value is missing or evaluation fails.
95
97
  */
96
98
  evaluate(values) {
99
+ const frame = new CallFrame(undefined, this._scope.variables());
97
100
  try {
98
101
  for (const [name, variable] of this._variables) {
99
102
  if (!values || !Object.hasOwn(values, name)) {
100
- this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
103
+ this._varspans.get(name)?.throwError(`undefined variable ${name}:\n`);
101
104
  }
102
105
  const value = normalize(values?.[name] ?? undefined);
103
106
  if (!variable.type.acceptValue(value)) {
104
- this._varframes.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
107
+ this._varspans.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
105
108
  }
106
- variable.value = value;
109
+ frame.add(variable, value);
107
110
  }
108
- return normalize(this._root.evaluate() ?? undefined);
111
+ return normalize(this._root.evaluate(frame) ?? undefined);
109
112
  }
110
113
  catch (e) {
111
114
  if (e instanceof JumpException) {
@@ -118,12 +121,6 @@ export class Affinirum {
118
121
  }
119
122
  throw e;
120
123
  }
121
- finally {
122
- for (const variable of this._variables.values()) {
123
- variable.value = undefined;
124
- }
125
- this._scope.clear();
126
- }
127
124
  }
128
125
  /** Parses an Affinirum Notation string into a JavaScript value.
129
126
  @param value Affinirum Notation string to parse.
@@ -134,7 +131,7 @@ export class Affinirum {
134
131
  return parseAN(value);
135
132
  }
136
133
  /** Formats a JavaScript value as an Affinirum Notation string.
137
- @param value Value to format.
134
+ @param value Value to format. Null and undefined are formatted as the AN literal null.
138
135
  @param whitespace Optional whitespace used for each indentation level.
139
136
  @returns Affinirum Notation formatted string.
140
137
  */
@@ -142,7 +139,7 @@ export class Affinirum {
142
139
  return formatAN(value, whitespace);
143
140
  }
144
141
  _list(state, scope) {
145
- const frame = state.starts();
142
+ const span = state.start();
146
143
  const subnodes = [];
147
144
  while (!state.isEmpty) {
148
145
  subnodes.push(this._unit(state, scope));
@@ -153,7 +150,7 @@ export class Affinirum {
153
150
  state.next();
154
151
  }
155
152
  }
156
- return new BlockNode(frame.ends(state), subnodes);
153
+ return new BlockNode(span.end(state), subnodes);
157
154
  }
158
155
  _unit(state, scope) {
159
156
  return this._coalescence(state, scope);
@@ -183,17 +180,17 @@ export class Affinirum {
183
180
  return node;
184
181
  }
185
182
  _negation(state, scope) {
186
- const frame = state.starts();
183
+ const span = state.start();
187
184
  let negate = false;
188
185
  while (state.operator === funcNot) {
189
186
  negate = !negate;
190
- frame.ends(state);
187
+ span.end(state);
191
188
  state.next();
192
189
  }
193
190
  let node = this._comparison(state, scope);
194
191
  if (negate) {
195
- const fnode = new ConstantNode(frame, funcNot);
196
- node = new CallNode(frame, fnode, [node]);
192
+ const fnode = new ConstantNode(span, funcNot);
193
+ node = new CallNode(span, fnode, [node]);
197
194
  }
198
195
  return node;
199
196
  }
@@ -224,19 +221,19 @@ export class Affinirum {
224
221
  return node;
225
222
  }
226
223
  _signum(state, scope) {
227
- const frame = state.starts();
224
+ const span = state.start();
228
225
  let negate = false;
229
226
  while (state.operator === funcAdd || state.operator === funcSubtract) {
230
227
  if (state.operator === funcSubtract) {
231
228
  negate = !negate;
232
229
  }
233
- frame.ends(state);
230
+ span.end(state);
234
231
  state.next();
235
232
  }
236
233
  let node = this._factor(state, scope);
237
234
  if (negate) {
238
- const fnode = new ConstantNode(frame, funcNegate);
239
- node = new CallNode(frame, fnode, [node]);
235
+ const fnode = new ConstantNode(span, funcNegate);
236
+ node = new CallNode(span, fnode, [node]);
240
237
  }
241
238
  return node;
242
239
  }
@@ -249,14 +246,14 @@ export class Affinirum {
249
246
  return node;
250
247
  }
251
248
  _accessor(state, scope) {
252
- const frame = state.starts();
249
+ const span = state.start();
253
250
  let node = this._term(state, scope);
254
251
  while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen || state.isCast) {
255
252
  if (state.isDot || state.isQuestion) { // property access or property existance operator
256
253
  const operator = state.isDot ? funcAt : funcHas;
257
254
  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))]);
255
+ const fnode = new ConstantNode(span.end(state), operator);
256
+ node = new CallNode(span, fnode, [node, new ConstantNode(state, new Constant(state.literal.value))]);
260
257
  state.next();
261
258
  }
262
259
  else if (state.isToken) {
@@ -270,18 +267,18 @@ export class Affinirum {
270
267
  break;
271
268
  }
272
269
  }
273
- const fnode = new ConstantNode(frame.ends(state), func);
274
- node = new CallNode(frame, fnode, subnodes);
270
+ const fnode = new ConstantNode(span.end(state), func);
271
+ node = new CallNode(span, fnode, subnodes);
275
272
  state.closeParentheses().next();
276
273
  }
277
274
  else {
278
- const fnode = new ConstantNode(frame.ends(state), func);
279
- node = new CallNode(frame, fnode, [node]);
275
+ const fnode = new ConstantNode(span.end(state), func);
276
+ node = new CallNode(span, fnode, [node]);
280
277
  }
281
278
  }
282
279
  else {
283
- const fnode = new ConstantNode(frame.ends(), operator);
284
- node = new CallNode(frame, fnode, [node, new ConstantNode(state, new Constant(state.token))]);
280
+ const fnode = new ConstantNode(span.end(state), operator);
281
+ node = new CallNode(span, fnode, [node, new ConstantNode(state, new Constant(state.token))]);
285
282
  state.next();
286
283
  }
287
284
  }
@@ -298,32 +295,32 @@ export class Affinirum {
298
295
  }
299
296
  }
300
297
  state.closeParentheses();
301
- node = new CallNode(frame.ends(state), node, subnodes);
298
+ node = new CallNode(span.end(state), node, subnodes);
302
299
  state.next();
303
300
  }
304
301
  else if (state.isBracketsOpen) { // index access operator
305
302
  const subnodes = [node, this._unit(state.next(), scope)];
306
303
  state.closeBrackets();
307
- const fnode = new ConstantNode(frame.ends(state), funcAt);
308
- node = new CallNode(frame, fnode, subnodes);
304
+ const fnode = new ConstantNode(span.end(state), funcAt);
305
+ node = new CallNode(span, fnode, subnodes);
309
306
  state.next();
310
307
  }
311
308
  else if (state.isCast) { // type cast operator
312
309
  const type = this._type(state.next(), scope);
313
- node = new CastNode(frame.ends(state), node, type);
310
+ node = new CastNode(span.end(state), node, type);
314
311
  }
315
312
  }
316
313
  return node;
317
314
  }
318
315
  _term(state, scope) {
319
316
  if (state.isLiteral) { // literal constant
320
- const frame = state.starts();
317
+ const span = state.start();
321
318
  const constant = new Constant(state.literal.value);
322
319
  state.next();
323
- return new ConstantNode(frame, constant);
320
+ return new ConstantNode(span, constant);
324
321
  }
325
322
  else if (state.isToken) { // variable
326
- const frame = state.starts();
323
+ const span = state.start();
327
324
  const constants = this._constants.get(state.token);
328
325
  if (constants != null) {
329
326
  if (!state.next().isDot) {
@@ -337,7 +334,7 @@ export class Affinirum {
337
334
  state.throwError(`unknown constant ${state.token}`);
338
335
  }
339
336
  state.next();
340
- return new ConstantNode(frame, constant);
337
+ return new ConstantNode(span, constant);
341
338
  }
342
339
  let variable = scope.get(state.token) ?? this._variables.get(state.token);
343
340
  if (variable == null) {
@@ -347,7 +344,7 @@ export class Affinirum {
347
344
  }
348
345
  variable = new Variable(type);
349
346
  this._variables.set(state.token, variable);
350
- this._varframes.set(state.token, state.starts());
347
+ this._varspans.set(state.token, state.start());
351
348
  }
352
349
  if (state.next().isAssignment) {
353
350
  if (!variable.assignable) {
@@ -355,19 +352,19 @@ export class Affinirum {
355
352
  }
356
353
  if (state.assignment.operator) {
357
354
  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);
355
+ const fnode = new ConstantNode(span, operator);
356
+ const subnode = new CallNode(span, fnode, [new VariableNode(span, variable), this._unit(state.next(), scope)]);
357
+ return new VariableNode(span, variable, subnode);
361
358
  }
362
359
  else {
363
360
  const subnode = this._unit(state.next(), scope);
364
- return new VariableNode(frame, variable, subnode);
361
+ return new VariableNode(span, variable, subnode);
365
362
  }
366
363
  }
367
- return new VariableNode(frame, variable);
364
+ return new VariableNode(span, variable);
368
365
  }
369
366
  else if (state.isBracesOpen) { // code block
370
- const frame = state.starts();
367
+ const span = state.start();
371
368
  const subscope = scope.subscope();
372
369
  const subnodes = [];
373
370
  while (!state.next().isBracesClose) {
@@ -376,9 +373,9 @@ export class Affinirum {
376
373
  break;
377
374
  }
378
375
  }
379
- frame.ends(state);
376
+ span.end(state);
380
377
  state.closeBraces().next();
381
- return new BlockNode(frame, subnodes);
378
+ return new BlockNode(span, subnodes, [...subscope.variables()]);
382
379
  }
383
380
  else if (state.isBracesClose) {
384
381
  state.throwError("unexpected closing braces");
@@ -392,7 +389,7 @@ export class Affinirum {
392
389
  state.throwError("unexpected closing parentheses");
393
390
  }
394
391
  else if (state.isBracketsOpen) { // array or object
395
- const frame = state.starts();
392
+ const span = state.start();
396
393
  const subnodes = [];
397
394
  let index = 0, colon = false;
398
395
  while (!state.next().isBracketsClose) {
@@ -413,12 +410,12 @@ export class Affinirum {
413
410
  break;
414
411
  }
415
412
  }
416
- frame.ends(state);
413
+ span.end(state);
417
414
  state.closeBrackets().next();
418
415
  if (colon) {
419
- return new ObjectNode(frame, subnodes.map(([k, v]) => [typeof k === "number" ? new ConstantNode(v, new Constant(String(k))) : k, v]));
416
+ return new ObjectNode(span, subnodes.map(([k, v]) => [typeof k === "number" ? new ConstantNode(v, new Constant(String(k))) : k, v]));
420
417
  }
421
- return new ArrayNode(frame, subnodes.map(([, v]) => v));
418
+ return new ArrayNode(span, subnodes.map(([, v]) => v));
422
419
  }
423
420
  else if (state.isBracketsClose) {
424
421
  state.throwError("unexpected closing brackets");
@@ -432,7 +429,7 @@ export class Affinirum {
432
429
  if (scope.has(token)) {
433
430
  state.throwError(`illegal redefinition of variable ${token}`);
434
431
  }
435
- const frame = state.starts();
432
+ const span = state.start();
436
433
  let type;
437
434
  if (state.next().isColon) {
438
435
  type = this._type(state.next(), scope);
@@ -443,9 +440,9 @@ export class Affinirum {
443
440
  if (state.assignment.operator) {
444
441
  state.throwError(`illegal assignment operator with variable ${token}`);
445
442
  }
446
- return new VariableNode(frame, variable, this._unit(state.next(), scope));
443
+ return new VariableNode(span, variable, this._unit(state.next(), scope));
447
444
  }
448
- return new VariableNode(frame, variable);
445
+ return new VariableNode(span, variable);
449
446
  }
450
447
  else if (state.isTilda) {
451
448
  return this._function(state, scope);
@@ -457,10 +454,10 @@ export class Affinirum {
457
454
  return this._loop(state, scope);
458
455
  }
459
456
  else if (state.isExit) {
460
- return new JumpNode(state, "exit", this._unit(state.next(), scope));
457
+ return new JumpNode(state, "exit", this._unit(state.next(), scope), this._scope.type());
461
458
  }
462
459
  else if (state.isReturn) {
463
- return new JumpNode(state, "return", this._unit(state.next(), scope));
460
+ return new JumpNode(state, "return", this._unit(state.next(), scope), scope.type());
464
461
  }
465
462
  else if (state.isStop) {
466
463
  const node = new JumpNode(state, "stop");
@@ -478,7 +475,7 @@ export class Affinirum {
478
475
  state.throwError("unexpected expression token");
479
476
  }
480
477
  _function(state, scope) {
481
- const frame = state.starts();
478
+ const span = state.start();
482
479
  state.next().openParentheses();
483
480
  let variadic = false;
484
481
  const variables = new Map();
@@ -487,8 +484,11 @@ export class Affinirum {
487
484
  state.throwError("missing function argument name");
488
485
  }
489
486
  const token = state.token;
487
+ if (variables.has(token)) {
488
+ state.throwError(`duplicate function argument name ${token}`);
489
+ }
490
490
  if (scope.has(token)) {
491
- state.throwError("variable redefinition");
491
+ state.throwError(`variable ${token} redefinition`);
492
492
  }
493
493
  let argType = Type.Unknown;
494
494
  if (state.next().isColon) {
@@ -512,39 +512,47 @@ export class Affinirum {
512
512
  }
513
513
  }
514
514
  state.closeParentheses().next();
515
- const retType = state.isColon ? this._type(state.next(), scope) : Type.Unknown;
516
- frame.ends(state);
515
+ const returnType = state.isColon ? this._type(state.next(), scope) : Type.Unknown;
516
+ span.end(state);
517
517
  const args = Array.from(variables.values());
518
- const subscope = scope.subscope(variables);
518
+ const subscope = scope.subscope(variables, returnType);
519
519
  const subnode = this._unit(state, subscope);
520
- const func = (...values) => {
521
- const previous = subscope.enter();
520
+ const createFunction = (frame) => (...values) => {
521
+ const subframe = frame.subframe(subscope.variables());
522
522
  try {
523
- args.forEach((arg, ix) => arg.value = normalize(values[ix]));
524
- return normalize(subnode.evaluate());
523
+ args.forEach((arg, ix) => subframe.set(arg, normalize(values[ix])));
524
+ return normalize(subnode.evaluate(subframe));
525
525
  }
526
- finally {
527
- subscope.leave(previous);
526
+ catch (e) {
527
+ if (e instanceof JumpException) {
528
+ if (e.jump === "return") {
529
+ return normalize(e.value);
530
+ }
531
+ if (e.jump === "stop" || e.jump === "next") {
532
+ subnode.throwError(`unexpected ${e.jump} jump`);
533
+ }
534
+ }
535
+ throw e;
528
536
  }
529
537
  };
530
- const constant = new Constant(func, Type.functionType(retType, args.map((v) => v.type), variadic));
531
- return new ConstantNode(frame, constant, subnode);
538
+ const constant = new Constant(createFunction, Type.functionType(returnType, args.map((v) => v.type), variadic));
539
+ return new ConstantNode(span, constant, subnode, createFunction);
532
540
  }
533
541
  _loop(state, scope) {
534
- const frame = state.starts();
542
+ const span = state.start();
535
543
  const cnode = this._unit(state.next(), scope);
536
- frame.ends(state);
537
- return new LoopNode(frame, cnode, this._unit(state, scope));
544
+ span.end(state);
545
+ return new LoopNode(span, cnode, this._unit(state, scope));
538
546
  }
539
547
  _switch(state, scope) {
540
- const frame = state.starts();
548
+ const span = state.start();
541
549
  const cnode = this._unit(state.next(), scope);
542
- frame.ends(state);
550
+ span.end(state);
543
551
  const cthen = this._unit(state, scope);
544
552
  const celse = state.isElse
545
553
  ? this._unit(state.next(), scope)
546
- : new ConstantNode(state.starts(), Constant.Null);
547
- return new SwitchNode(frame, cnode, [cthen, celse]);
554
+ : new ConstantNode(state.start(), Constant.Null);
555
+ return new SwitchNode(span, cnode, [cthen, celse]);
548
556
  }
549
557
  _type(state, scope) {
550
558
  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,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,31 @@
1
+ import { Type } from "./Type.js";
2
+ export class LexicalScope {
3
+ _superscope;
4
+ _subscopes = [];
5
+ _variables;
6
+ _type;
7
+ constructor(superscope, variables, type) {
8
+ superscope?._subscopes.push(this);
9
+ this._superscope = superscope;
10
+ this._variables = variables ?? new Map();
11
+ this._type = type;
12
+ }
13
+ has(name) {
14
+ return this._variables.has(name) || Boolean(this._superscope?.has(name));
15
+ }
16
+ get(name) {
17
+ return this._variables.get(name) ?? this._superscope?.get(name);
18
+ }
19
+ set(name, variable) {
20
+ this._variables.set(name, variable);
21
+ }
22
+ subscope(variables, type) {
23
+ return new LexicalScope(this, variables, type);
24
+ }
25
+ variables() {
26
+ return this._variables.values();
27
+ }
28
+ type() {
29
+ return this._type ?? this._superscope?.type() ?? Type.Unknown;
30
+ }
31
+ }
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;