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
@@ -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 = new LexicalScope_js_1.LexicalScope();
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.
@@ -97,18 +98,19 @@ class Affinirum {
97
98
  @throws If an input value is missing or evaluation fails.
98
99
  */
99
100
  evaluate(values) {
101
+ const frame = new CallFrame_js_1.CallFrame(undefined, this._scope.variables());
100
102
  try {
101
103
  for (const [name, variable] of this._variables) {
102
104
  if (!values || !Object.hasOwn(values, name)) {
103
- this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
105
+ this._varspans.get(name)?.throwError(`undefined variable ${name}:\n`);
104
106
  }
105
107
  const value = (0, Value_js_1.normalize)(values?.[name] ?? undefined);
106
108
  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`);
109
+ this._varspans.get(name)?.throwError(`unexpected type ${Type_js_1.Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
108
110
  }
109
- variable.value = value;
111
+ frame.add(variable, value);
110
112
  }
111
- return (0, Value_js_1.normalize)(this._root.evaluate() ?? undefined);
113
+ return (0, Value_js_1.normalize)(this._root.evaluate(frame) ?? undefined);
112
114
  }
113
115
  catch (e) {
114
116
  if (e instanceof JumpException_js_1.JumpException) {
@@ -121,12 +123,6 @@ class Affinirum {
121
123
  }
122
124
  throw e;
123
125
  }
124
- finally {
125
- for (const variable of this._variables.values()) {
126
- variable.value = undefined;
127
- }
128
- this._scope.clear();
129
- }
130
126
  }
131
127
  /** Parses an Affinirum Notation string into a JavaScript value.
132
128
  @param value Affinirum Notation string to parse.
@@ -137,7 +133,7 @@ class Affinirum {
137
133
  return (0, AN_js_1.parseAN)(value);
138
134
  }
139
135
  /** Formats a JavaScript value as an Affinirum Notation string.
140
- @param value Value to format.
136
+ @param value Value to format. Null and undefined are formatted as the AN literal null.
141
137
  @param whitespace Optional whitespace used for each indentation level.
142
138
  @returns Affinirum Notation formatted string.
143
139
  */
@@ -145,7 +141,7 @@ class Affinirum {
145
141
  return (0, AN_js_1.formatAN)(value, whitespace);
146
142
  }
147
143
  _list(state, scope) {
148
- const frame = state.starts();
144
+ const span = state.start();
149
145
  const subnodes = [];
150
146
  while (!state.isEmpty) {
151
147
  subnodes.push(this._unit(state, scope));
@@ -156,7 +152,7 @@ class Affinirum {
156
152
  state.next();
157
153
  }
158
154
  }
159
- return new BlockNode_js_1.BlockNode(frame.ends(state), subnodes);
155
+ return new BlockNode_js_1.BlockNode(span.end(state), subnodes);
160
156
  }
161
157
  _unit(state, scope) {
162
158
  return this._coalescence(state, scope);
@@ -186,17 +182,17 @@ class Affinirum {
186
182
  return node;
187
183
  }
188
184
  _negation(state, scope) {
189
- const frame = state.starts();
185
+ const span = state.start();
190
186
  let negate = false;
191
187
  while (state.operator === Boolean_js_1.funcNot) {
192
188
  negate = !negate;
193
- frame.ends(state);
189
+ span.end(state);
194
190
  state.next();
195
191
  }
196
192
  let node = this._comparison(state, scope);
197
193
  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]);
194
+ const fnode = new ConstantNode_js_1.ConstantNode(span, Boolean_js_1.funcNot);
195
+ node = new CallNode_js_1.CallNode(span, fnode, [node]);
200
196
  }
201
197
  return node;
202
198
  }
@@ -227,19 +223,19 @@ class Affinirum {
227
223
  return node;
228
224
  }
229
225
  _signum(state, scope) {
230
- const frame = state.starts();
226
+ const span = state.start();
231
227
  let negate = false;
232
228
  while (state.operator === Aggregable_js_1.funcAdd || state.operator === Number_js_1.funcSubtract) {
233
229
  if (state.operator === Number_js_1.funcSubtract) {
234
230
  negate = !negate;
235
231
  }
236
- frame.ends(state);
232
+ span.end(state);
237
233
  state.next();
238
234
  }
239
235
  let node = this._factor(state, scope);
240
236
  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]);
237
+ const fnode = new ConstantNode_js_1.ConstantNode(span, Number_js_1.funcNegate);
238
+ node = new CallNode_js_1.CallNode(span, fnode, [node]);
243
239
  }
244
240
  return node;
245
241
  }
@@ -252,14 +248,14 @@ class Affinirum {
252
248
  return node;
253
249
  }
254
250
  _accessor(state, scope) {
255
- const frame = state.starts();
251
+ const span = state.start();
256
252
  let node = this._term(state, scope);
257
253
  while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen || state.isCast) {
258
254
  if (state.isDot || state.isQuestion) { // property access or property existance operator
259
255
  const operator = state.isDot ? Iterable_js_1.funcAt : Iterable_js_1.funcHas;
260
256
  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))]);
257
+ const fnode = new ConstantNode_js_1.ConstantNode(span.end(state), operator);
258
+ node = new CallNode_js_1.CallNode(span, fnode, [node, new ConstantNode_js_1.ConstantNode(state, new Constant_js_1.Constant(state.literal.value))]);
263
259
  state.next();
264
260
  }
265
261
  else if (state.isToken) {
@@ -273,18 +269,18 @@ class Affinirum {
273
269
  break;
274
270
  }
275
271
  }
276
- const fnode = new ConstantNode_js_1.ConstantNode(frame.ends(state), func);
277
- node = new CallNode_js_1.CallNode(frame, fnode, subnodes);
272
+ const fnode = new ConstantNode_js_1.ConstantNode(span.end(state), func);
273
+ node = new CallNode_js_1.CallNode(span, fnode, subnodes);
278
274
  state.closeParentheses().next();
279
275
  }
280
276
  else {
281
- const fnode = new ConstantNode_js_1.ConstantNode(frame.ends(state), func);
282
- node = new CallNode_js_1.CallNode(frame, fnode, [node]);
277
+ const fnode = new ConstantNode_js_1.ConstantNode(span.end(state), func);
278
+ node = new CallNode_js_1.CallNode(span, fnode, [node]);
283
279
  }
284
280
  }
285
281
  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))]);
282
+ const fnode = new ConstantNode_js_1.ConstantNode(span.end(state), operator);
283
+ node = new CallNode_js_1.CallNode(span, fnode, [node, new ConstantNode_js_1.ConstantNode(state, new Constant_js_1.Constant(state.token))]);
288
284
  state.next();
289
285
  }
290
286
  }
@@ -301,32 +297,32 @@ class Affinirum {
301
297
  }
302
298
  }
303
299
  state.closeParentheses();
304
- node = new CallNode_js_1.CallNode(frame.ends(state), node, subnodes);
300
+ node = new CallNode_js_1.CallNode(span.end(state), node, subnodes);
305
301
  state.next();
306
302
  }
307
303
  else if (state.isBracketsOpen) { // index access operator
308
304
  const subnodes = [node, this._unit(state.next(), scope)];
309
305
  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);
306
+ const fnode = new ConstantNode_js_1.ConstantNode(span.end(state), Iterable_js_1.funcAt);
307
+ node = new CallNode_js_1.CallNode(span, fnode, subnodes);
312
308
  state.next();
313
309
  }
314
310
  else if (state.isCast) { // type cast operator
315
311
  const type = this._type(state.next(), scope);
316
- node = new CastNode_js_1.CastNode(frame.ends(state), node, type);
312
+ node = new CastNode_js_1.CastNode(span.end(state), node, type);
317
313
  }
318
314
  }
319
315
  return node;
320
316
  }
321
317
  _term(state, scope) {
322
318
  if (state.isLiteral) { // literal constant
323
- const frame = state.starts();
319
+ const span = state.start();
324
320
  const constant = new Constant_js_1.Constant(state.literal.value);
325
321
  state.next();
326
- return new ConstantNode_js_1.ConstantNode(frame, constant);
322
+ return new ConstantNode_js_1.ConstantNode(span, constant);
327
323
  }
328
324
  else if (state.isToken) { // variable
329
- const frame = state.starts();
325
+ const span = state.start();
330
326
  const constants = this._constants.get(state.token);
331
327
  if (constants != null) {
332
328
  if (!state.next().isDot) {
@@ -340,7 +336,7 @@ class Affinirum {
340
336
  state.throwError(`unknown constant ${state.token}`);
341
337
  }
342
338
  state.next();
343
- return new ConstantNode_js_1.ConstantNode(frame, constant);
339
+ return new ConstantNode_js_1.ConstantNode(span, constant);
344
340
  }
345
341
  let variable = scope.get(state.token) ?? this._variables.get(state.token);
346
342
  if (variable == null) {
@@ -350,7 +346,7 @@ class Affinirum {
350
346
  }
351
347
  variable = new Variable_js_1.Variable(type);
352
348
  this._variables.set(state.token, variable);
353
- this._varframes.set(state.token, state.starts());
349
+ this._varspans.set(state.token, state.start());
354
350
  }
355
351
  if (state.next().isAssignment) {
356
352
  if (!variable.assignable) {
@@ -358,19 +354,19 @@ class Affinirum {
358
354
  }
359
355
  if (state.assignment.operator) {
360
356
  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);
357
+ const fnode = new ConstantNode_js_1.ConstantNode(span, operator);
358
+ const subnode = new CallNode_js_1.CallNode(span, fnode, [new VariableNode_js_1.VariableNode(span, variable), this._unit(state.next(), scope)]);
359
+ return new VariableNode_js_1.VariableNode(span, variable, subnode);
364
360
  }
365
361
  else {
366
362
  const subnode = this._unit(state.next(), scope);
367
- return new VariableNode_js_1.VariableNode(frame, variable, subnode);
363
+ return new VariableNode_js_1.VariableNode(span, variable, subnode);
368
364
  }
369
365
  }
370
- return new VariableNode_js_1.VariableNode(frame, variable);
366
+ return new VariableNode_js_1.VariableNode(span, variable);
371
367
  }
372
368
  else if (state.isBracesOpen) { // code block
373
- const frame = state.starts();
369
+ const span = state.start();
374
370
  const subscope = scope.subscope();
375
371
  const subnodes = [];
376
372
  while (!state.next().isBracesClose) {
@@ -379,9 +375,9 @@ class Affinirum {
379
375
  break;
380
376
  }
381
377
  }
382
- frame.ends(state);
378
+ span.end(state);
383
379
  state.closeBraces().next();
384
- return new BlockNode_js_1.BlockNode(frame, subnodes);
380
+ return new BlockNode_js_1.BlockNode(span, subnodes, [...subscope.variables()]);
385
381
  }
386
382
  else if (state.isBracesClose) {
387
383
  state.throwError("unexpected closing braces");
@@ -395,7 +391,7 @@ class Affinirum {
395
391
  state.throwError("unexpected closing parentheses");
396
392
  }
397
393
  else if (state.isBracketsOpen) { // array or object
398
- const frame = state.starts();
394
+ const span = state.start();
399
395
  const subnodes = [];
400
396
  let index = 0, colon = false;
401
397
  while (!state.next().isBracketsClose) {
@@ -416,12 +412,12 @@ class Affinirum {
416
412
  break;
417
413
  }
418
414
  }
419
- frame.ends(state);
415
+ span.end(state);
420
416
  state.closeBrackets().next();
421
417
  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]));
418
+ 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
419
  }
424
- return new ArrayNode_js_1.ArrayNode(frame, subnodes.map(([, v]) => v));
420
+ return new ArrayNode_js_1.ArrayNode(span, subnodes.map(([, v]) => v));
425
421
  }
426
422
  else if (state.isBracketsClose) {
427
423
  state.throwError("unexpected closing brackets");
@@ -435,7 +431,7 @@ class Affinirum {
435
431
  if (scope.has(token)) {
436
432
  state.throwError(`illegal redefinition of variable ${token}`);
437
433
  }
438
- const frame = state.starts();
434
+ const span = state.start();
439
435
  let type;
440
436
  if (state.next().isColon) {
441
437
  type = this._type(state.next(), scope);
@@ -446,9 +442,9 @@ class Affinirum {
446
442
  if (state.assignment.operator) {
447
443
  state.throwError(`illegal assignment operator with variable ${token}`);
448
444
  }
449
- return new VariableNode_js_1.VariableNode(frame, variable, this._unit(state.next(), scope));
445
+ return new VariableNode_js_1.VariableNode(span, variable, this._unit(state.next(), scope));
450
446
  }
451
- return new VariableNode_js_1.VariableNode(frame, variable);
447
+ return new VariableNode_js_1.VariableNode(span, variable);
452
448
  }
453
449
  else if (state.isTilda) {
454
450
  return this._function(state, scope);
@@ -481,7 +477,7 @@ class Affinirum {
481
477
  state.throwError("unexpected expression token");
482
478
  }
483
479
  _function(state, scope) {
484
- const frame = state.starts();
480
+ const span = state.start();
485
481
  state.next().openParentheses();
486
482
  let variadic = false;
487
483
  const variables = new Map();
@@ -490,8 +486,11 @@ class Affinirum {
490
486
  state.throwError("missing function argument name");
491
487
  }
492
488
  const token = state.token;
489
+ if (variables.has(token)) {
490
+ state.throwError(`duplicate function argument name ${token}`);
491
+ }
493
492
  if (scope.has(token)) {
494
- state.throwError("variable redefinition");
493
+ state.throwError(`variable ${token} redefinition`);
495
494
  }
496
495
  let argType = Type_js_1.Type.Unknown;
497
496
  if (state.next().isColon) {
@@ -516,38 +515,46 @@ class Affinirum {
516
515
  }
517
516
  state.closeParentheses().next();
518
517
  const retType = state.isColon ? this._type(state.next(), scope) : Type_js_1.Type.Unknown;
519
- frame.ends(state);
518
+ span.end(state);
520
519
  const args = Array.from(variables.values());
521
520
  const subscope = scope.subscope(variables);
522
521
  const subnode = this._unit(state, subscope);
523
- const func = (...values) => {
524
- const previous = subscope.enter();
522
+ const createFunction = (frame) => (...values) => {
523
+ const subframe = frame.subframe(subscope.variables());
525
524
  try {
526
- args.forEach((arg, ix) => arg.value = (0, Value_js_1.normalize)(values[ix]));
527
- return (0, Value_js_1.normalize)(subnode.evaluate());
525
+ args.forEach((arg, ix) => subframe.set(arg, (0, Value_js_1.normalize)(values[ix])));
526
+ return (0, Value_js_1.normalize)(subnode.evaluate(subframe));
528
527
  }
529
- finally {
530
- subscope.leave(previous);
528
+ catch (e) {
529
+ if (e instanceof JumpException_js_1.JumpException) {
530
+ if (e.jump === "return") {
531
+ return (0, Value_js_1.normalize)(e.value);
532
+ }
533
+ if (e.jump === "stop" || e.jump === "next") {
534
+ subnode.throwError(`unexpected ${e.jump} jump`);
535
+ }
536
+ }
537
+ throw e;
531
538
  }
532
539
  };
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);
540
+ const constant = new Constant_js_1.Constant(createFunction, Type_js_1.Type.functionType(retType, args.map((v) => v.type), variadic));
541
+ return new ConstantNode_js_1.ConstantNode(span, constant, subnode, createFunction);
535
542
  }
536
543
  _loop(state, scope) {
537
- const frame = state.starts();
544
+ const span = state.start();
538
545
  const cnode = this._unit(state.next(), scope);
539
- frame.ends(state);
540
- return new LoopNode_js_1.LoopNode(frame, cnode, this._unit(state, scope));
546
+ span.end(state);
547
+ return new LoopNode_js_1.LoopNode(span, cnode, this._unit(state, scope));
541
548
  }
542
549
  _switch(state, scope) {
543
- const frame = state.starts();
550
+ const span = state.start();
544
551
  const cnode = this._unit(state.next(), scope);
545
- frame.ends(state);
552
+ span.end(state);
546
553
  const cthen = this._unit(state, scope);
547
554
  const celse = state.isElse
548
555
  ? 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]);
556
+ : new ConstantNode_js_1.ConstantNode(state.start(), Constant_js_1.Constant.Null);
557
+ return new SwitchNode_js_1.SwitchNode(span, cnode, [cthen, celse]);
551
558
  }
552
559
  _type(state, scope) {
553
560
  if (state.isType) { // literal type
@@ -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,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LexicalScope = void 0;
4
+ class LexicalScope {
5
+ _superscope;
6
+ _subscopes = [];
7
+ _variables;
8
+ _boundary;
9
+ constructor(superscope, variables) {
10
+ superscope?._subscopes.push(this);
11
+ this._superscope = superscope;
12
+ this._variables = variables ?? new Map();
13
+ this._boundary = Boolean(variables);
14
+ }
15
+ has(name) {
16
+ return this._variables.has(name) || Boolean(this._superscope?.has(name));
17
+ }
18
+ get(name) {
19
+ return this._variables.get(name) ?? this._superscope?.get(name);
20
+ }
21
+ set(name, variable) {
22
+ this._variables.set(name, variable);
23
+ }
24
+ subscope(variables) {
25
+ return new LexicalScope(this, variables);
26
+ }
27
+ variables() {
28
+ return this._variables.values();
29
+ }
30
+ }
31
+ exports.LexicalScope = LexicalScope;
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;