affinirum 1.5.1 → 1.5.2

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.
package/README.md CHANGED
@@ -401,8 +401,15 @@ const result = expression.evaluate({ a: 10n, b: 20n }); // 30n
401
401
  During parsing, any valid name that is not recognized as a keyword, constant, or built-in function is treated as an input variable.
402
402
  Use the optional constructor configuration to specify the expected result `type`, enable `strict` handling of undeclared variables,
403
403
  or declare input `variables` and their types. An instance exposes the original `script`, its inferred result `type`,
404
- the inferred input types through `variables()`, and the parsed tree through `toString()`. `Affinirum.format()` formats
405
- a JavaScript value using Affinirum Notation.
404
+ the inferred input types through `variables()`, and the parsed tree through `toString()`.
405
+
406
+ `Affinirum.parse()` parses a single Affinirum Notation value into a JavaScript value. Empty or `undefined` input returns
407
+ `undefined`; invalid input throws an error. `Affinirum.format()` formats a JavaScript value as an Affinirum Notation string:
408
+
409
+ ```ts
410
+ const value = Affinirum.parse('["answer":42,"enabled":true]');
411
+ const notation = Affinirum.format(value);
412
+ ```
406
413
 
407
414
  JavaScript `number` values map to **float**, while **integer** values map to `bigint`. Timestamps use `Date`, buffers use
408
415
  `ArrayBuffer`, and a **void** result is returned as `undefined`. Arrays, objects, and functions use their corresponding
@@ -7,17 +7,18 @@ import { LoopNode } from "./node/LoopNode.js";
7
7
  import { SwitchNode } from "./node/SwitchNode.js";
8
8
  import { ParserFrame } from "./ParserFrame.js";
9
9
  import { ParserState } from "./ParserState.js";
10
- import { StaticScope } from "./StaticScope.js";
10
+ import { Scope } from "./Scope.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
+ protected readonly _vartypes: Map<string, Type>;
16
17
  protected readonly _varframes: Map<string, ParserFrame>;
17
18
  protected readonly _variables: Map<string, Variable>;
18
19
  protected readonly _constants: Map<string, Record<string, Constant>>;
19
20
  protected readonly _functions: Map<string, Constant>;
20
- protected readonly _scope: StaticScope;
21
+ protected readonly _scope: Scope;
21
22
  /**
22
23
  Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
23
24
  @param script Math expression to compile.
@@ -49,33 +50,39 @@ export declare class Affinirum {
49
50
  */
50
51
  variables(): Record<string, Type>;
51
52
  /**
52
- Evaluates compiled expression using provided variable values.
53
- @param values Record with variable names and values.
54
- @returns Calculated value.
53
+ Evaluates the compiled expression using the provided input values.
54
+ @param values Values for the expression's input variables. Omitted when there are no input variables.
55
+ @returns The evaluated result.
56
+ @throws If an input value is missing or evaluation fails.
55
57
  */
56
58
  evaluate(values?: Record<string, Value>): Value;
57
- /**
58
- Produces string in Affinirum Notation.
59
+ /** Parses an Affinirum Notation string into a JavaScript value.
60
+ @param value Affinirum Notation string to parse.
61
+ @returns The parsed value.
62
+ @throws If string is not a valid Affinirum Notation string.
63
+ */
64
+ static parse(value: string | undefined): Value;
65
+ /** Formats a JavaScript value as an Affinirum Notation string.
59
66
  @param value Value to format.
60
- @param whitespace Optional white space characters for readability.
61
- @returns String formatted in Affinirum Notation.
67
+ @param whitespace Optional whitespace used for each indentation level.
68
+ @returns Affinirum Notation formatted string.
62
69
  */
63
70
  static format(value: Value, whitespace?: string): string;
64
- protected _list(state: ParserState, scope: StaticScope): Node;
65
- protected _unit(state: ParserState, scope: StaticScope): Node;
66
- protected _coalescence(state: ParserState, scope: StaticScope): Node;
67
- protected _disjunction(state: ParserState, scope: StaticScope): Node;
68
- protected _conjunction(state: ParserState, scope: StaticScope): Node;
69
- protected _negation(state: ParserState, scope: StaticScope): Node;
70
- protected _comparison(state: ParserState, scope: StaticScope): Node;
71
- protected _aggregate(state: ParserState, scope: StaticScope): Node;
72
- protected _product(state: ParserState, scope: StaticScope): Node;
73
- protected _signum(state: ParserState, scope: StaticScope): Node;
74
- protected _factor(state: ParserState, scope: StaticScope): Node;
75
- protected _accessor(state: ParserState, scope: StaticScope): Node;
76
- protected _term(state: ParserState, scope: StaticScope): Node;
77
- protected _function(state: ParserState, scope: StaticScope): Node;
78
- protected _loop(state: ParserState, scope: StaticScope): LoopNode;
79
- protected _switch(state: ParserState, scope: StaticScope): SwitchNode;
80
- protected _type(state: ParserState, scope: StaticScope): Type;
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;
81
88
  }
package/dst/Affinirum.js CHANGED
@@ -1,4 +1,4 @@
1
- import { formatAN } from "./native/notation/AN.js";
1
+ import { parseAN, formatAN } from "./native/notation/AN.js";
2
2
  import { funcOr, funcAnd, funcNot } from "./native/Boolean.js";
3
3
  import { funcAdd } from "./native/Aggregable.js";
4
4
  import { funcAt, funcHas } from "./native/Iterable.js";
@@ -22,18 +22,19 @@ 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 { StaticScope } from "./StaticScope.js";
25
+ import { Scope } from "./Scope.js";
26
26
  import { JumpException } from "./JumpException.js";
27
27
  export class Affinirum {
28
28
  static keywords = [...Keywords, ...Constants.map((c) => c[0])];
29
29
  _script;
30
30
  _strict;
31
31
  _root;
32
+ _vartypes = new Map();
32
33
  _varframes = new Map();
33
34
  _variables = new Map();
34
35
  _constants = new Map(Constants);
35
36
  _functions = new Map(Functions);
36
- _scope = new StaticScope();
37
+ _scope = new Scope();
37
38
  /**
38
39
  Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
39
40
  @param script Math expression to compile.
@@ -46,7 +47,7 @@ export class Affinirum {
46
47
  this._strict = config?.strict ?? false;
47
48
  if (config?.variables) {
48
49
  for (const [vname, vtype] of Object.entries(config.variables)) {
49
- this._variables.set(vname, new Variable(vtype));
50
+ this._vartypes.set(vname, vtype);
50
51
  }
51
52
  }
52
53
  const state = new ParserState(this._script);
@@ -81,31 +82,29 @@ export class Affinirum {
81
82
  */
82
83
  variables() {
83
84
  const types = Object.create(null);
84
- const variables = this._scope.variables();
85
- for (const name in variables) {
86
- types[name] = variables[name].type;
85
+ for (const [name, variable] of this._variables) {
86
+ types[name] = variable.type;
87
87
  }
88
88
  return types;
89
89
  }
90
90
  /**
91
- Evaluates compiled expression using provided variable values.
92
- @param values Record with variable names and values.
93
- @returns Calculated value.
91
+ Evaluates the compiled expression using the provided input values.
92
+ @param values Values for the expression's input variables. Omitted when there are no input variables.
93
+ @returns The evaluated result.
94
+ @throws If an input value is missing or evaluation fails.
94
95
  */
95
96
  evaluate(values) {
96
- const variables = this._scope.variables();
97
- for (const name in variables) {
98
- if (!values || !Object.hasOwn(values, name)) {
99
- this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
100
- }
101
- const variable = variables[name];
102
- const value = normalize(values?.[name] ?? undefined);
103
- if (!variable.type.acceptValue(value)) {
104
- this._varframes.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
105
- }
106
- variable.value = value;
107
- }
108
97
  try {
98
+ for (const [name, variable] of this._variables) {
99
+ if (!values || !Object.hasOwn(values, name)) {
100
+ this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
101
+ }
102
+ const value = normalize(values?.[name] ?? undefined);
103
+ if (!variable.type.acceptValue(value)) {
104
+ this._varframes.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
105
+ }
106
+ variable.value = value;
107
+ }
109
108
  return normalize(this._root.evaluate() ?? undefined);
110
109
  }
111
110
  catch (e) {
@@ -119,12 +118,25 @@ export class Affinirum {
119
118
  }
120
119
  throw e;
121
120
  }
121
+ finally {
122
+ for (const variable of this._variables.values()) {
123
+ variable.value = undefined;
124
+ }
125
+ this._scope.clear();
126
+ }
122
127
  }
123
- /**
124
- Produces string in Affinirum Notation.
128
+ /** Parses an Affinirum Notation string into a JavaScript value.
129
+ @param value Affinirum Notation string to parse.
130
+ @returns The parsed value.
131
+ @throws If string is not a valid Affinirum Notation string.
132
+ */
133
+ static parse(value) {
134
+ return parseAN(value);
135
+ }
136
+ /** Formats a JavaScript value as an Affinirum Notation string.
125
137
  @param value Value to format.
126
- @param whitespace Optional white space characters for readability.
127
- @returns String formatted in Affinirum Notation.
138
+ @param whitespace Optional whitespace used for each indentation level.
139
+ @returns Affinirum Notation formatted string.
128
140
  */
129
141
  static format(value, whitespace) {
130
142
  return formatAN(value, whitespace);
@@ -327,20 +339,14 @@ export class Affinirum {
327
339
  state.next();
328
340
  return new ConstantNode(frame, constant);
329
341
  }
330
- let variable = scope.get(state.token);
342
+ let variable = scope.get(state.token) ?? this._variables.get(state.token);
331
343
  if (variable == null) {
332
- variable = this._variables.get(state.token);
333
- if (variable == null) {
334
- if (this._strict) {
335
- state.throwError(`undefined variable ${state.token} in strict mode`);
336
- }
337
- else {
338
- variable = new Variable();
339
- }
344
+ const type = this._vartypes.get(state.token);
345
+ if (type == null && this._strict) {
346
+ state.throwError(`undefined variable ${state.token} in strict mode`);
340
347
  }
341
- scope.global(state.token, variable);
342
- }
343
- if (!this._varframes.has(state.token)) {
348
+ variable = new Variable(type);
349
+ this._variables.set(state.token, variable);
344
350
  this._varframes.set(state.token, state.starts());
345
351
  }
346
352
  if (state.next().isAssignment) {
@@ -362,9 +368,10 @@ export class Affinirum {
362
368
  }
363
369
  else if (state.isBracesOpen) { // code block
364
370
  const frame = state.starts();
371
+ const subscope = scope.subscope();
365
372
  const subnodes = [];
366
373
  while (!state.next().isBracesClose) {
367
- subnodes.push(this._unit(state, scope));
374
+ subnodes.push(this._unit(state, subscope));
368
375
  if (!state.isSemicolon) {
369
376
  break;
370
377
  }
@@ -431,7 +438,7 @@ export class Affinirum {
431
438
  type = this._type(state.next(), scope);
432
439
  }
433
440
  const variable = new Variable(type, assignable);
434
- scope.local(token, variable);
441
+ scope.set(token, variable);
435
442
  if (state.isAssignment) {
436
443
  if (state.assignment.operator) {
437
444
  state.throwError(`illegal assignment operator with variable ${token}`);
@@ -480,7 +487,7 @@ export class Affinirum {
480
487
  state.throwError("missing function argument name");
481
488
  }
482
489
  const token = state.token;
483
- if (scope.get(token)) {
490
+ if (scope.has(token)) {
484
491
  state.throwError("variable redefinition");
485
492
  }
486
493
  let argType = Type.Unknown;
@@ -508,10 +515,17 @@ export class Affinirum {
508
515
  const retType = state.isColon ? this._type(state.next(), scope) : Type.Unknown;
509
516
  frame.ends(state);
510
517
  const args = Array.from(variables.values());
511
- const subnode = this._unit(state, scope.subscope(variables));
518
+ const subscope = scope.subscope(variables);
519
+ const subnode = this._unit(state, subscope);
512
520
  const func = (...values) => {
513
- args.forEach((arg, ix) => arg.value = normalize(values[ix]));
514
- return normalize(subnode.evaluate());
521
+ const previous = subscope.enter();
522
+ try {
523
+ args.forEach((arg, ix) => arg.value = normalize(values[ix]));
524
+ return normalize(subnode.evaluate());
525
+ }
526
+ finally {
527
+ subscope.leave(previous);
528
+ }
515
529
  };
516
530
  const constant = new Constant(func, Type.functionType(retType, args.map((v) => v.type), variadic));
517
531
  return new ConstantNode(frame, constant, subnode);
package/dst/Scope.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { Value } from "./Value.js";
2
+ import { Variable } from "./Variable.js";
3
+ export declare class Scope {
4
+ protected readonly _subscopes: Scope[];
5
+ protected readonly _superscope?: Scope;
6
+ protected readonly _variables: Map<string, Variable>;
7
+ protected readonly _boundary: boolean;
8
+ constructor(superscope?: Scope, variables?: Map<string, Variable>);
9
+ has(name: string): boolean;
10
+ get(name: string): Variable | undefined;
11
+ set(name: string, variable: Variable): void;
12
+ subscope(variables?: Map<string, Variable>): Scope;
13
+ enter(values?: Map<Variable, Value>): Map<Variable, Value>;
14
+ leave(values: Map<Variable, Value>): void;
15
+ clear(): void;
16
+ }
package/dst/Scope.js ADDED
@@ -0,0 +1,49 @@
1
+ export class Scope {
2
+ _subscopes = [];
3
+ _superscope;
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 Scope(this, variables);
23
+ }
24
+ enter(values = new Map()) {
25
+ for (const variable of this._variables.values()) {
26
+ values.set(variable, variable.value);
27
+ variable.value = undefined;
28
+ }
29
+ for (const subscope of this._subscopes) {
30
+ if (!subscope._boundary) {
31
+ subscope.enter(values);
32
+ }
33
+ }
34
+ return values;
35
+ }
36
+ leave(values) {
37
+ for (const [variable, value] of values) {
38
+ variable.value = value;
39
+ }
40
+ }
41
+ clear() {
42
+ for (const variable of this._variables.values()) {
43
+ variable.value = undefined;
44
+ }
45
+ for (const subscope of this._subscopes) {
46
+ subscope.clear();
47
+ }
48
+ }
49
+ }
@@ -25,18 +25,19 @@ 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 StaticScope_js_1 = require("./StaticScope.js");
28
+ const Scope_js_1 = require("./Scope.js");
29
29
  const JumpException_js_1 = require("./JumpException.js");
30
30
  class Affinirum {
31
31
  static keywords = [...Keywords_js_1.Keywords, ...Constants_js_1.Constants.map((c) => c[0])];
32
32
  _script;
33
33
  _strict;
34
34
  _root;
35
+ _vartypes = new Map();
35
36
  _varframes = new Map();
36
37
  _variables = new Map();
37
38
  _constants = new Map(Constants_js_1.Constants);
38
39
  _functions = new Map(Functions_js_1.Functions);
39
- _scope = new StaticScope_js_1.StaticScope();
40
+ _scope = new Scope_js_1.Scope();
40
41
  /**
41
42
  Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
42
43
  @param script Math expression to compile.
@@ -49,7 +50,7 @@ class Affinirum {
49
50
  this._strict = config?.strict ?? false;
50
51
  if (config?.variables) {
51
52
  for (const [vname, vtype] of Object.entries(config.variables)) {
52
- this._variables.set(vname, new Variable_js_1.Variable(vtype));
53
+ this._vartypes.set(vname, vtype);
53
54
  }
54
55
  }
55
56
  const state = new ParserState_js_1.ParserState(this._script);
@@ -84,31 +85,29 @@ class Affinirum {
84
85
  */
85
86
  variables() {
86
87
  const types = Object.create(null);
87
- const variables = this._scope.variables();
88
- for (const name in variables) {
89
- types[name] = variables[name].type;
88
+ for (const [name, variable] of this._variables) {
89
+ types[name] = variable.type;
90
90
  }
91
91
  return types;
92
92
  }
93
93
  /**
94
- Evaluates compiled expression using provided variable values.
95
- @param values Record with variable names and values.
96
- @returns Calculated value.
94
+ Evaluates the compiled expression using the provided input values.
95
+ @param values Values for the expression's input variables. Omitted when there are no input variables.
96
+ @returns The evaluated result.
97
+ @throws If an input value is missing or evaluation fails.
97
98
  */
98
99
  evaluate(values) {
99
- const variables = this._scope.variables();
100
- for (const name in variables) {
101
- if (!values || !Object.hasOwn(values, name)) {
102
- this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
103
- }
104
- const variable = variables[name];
105
- const value = (0, Value_js_1.normalize)(values?.[name] ?? undefined);
106
- 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`);
108
- }
109
- variable.value = value;
110
- }
111
100
  try {
101
+ for (const [name, variable] of this._variables) {
102
+ if (!values || !Object.hasOwn(values, name)) {
103
+ this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
104
+ }
105
+ const value = (0, Value_js_1.normalize)(values?.[name] ?? undefined);
106
+ 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`);
108
+ }
109
+ variable.value = value;
110
+ }
112
111
  return (0, Value_js_1.normalize)(this._root.evaluate() ?? undefined);
113
112
  }
114
113
  catch (e) {
@@ -122,12 +121,25 @@ class Affinirum {
122
121
  }
123
122
  throw e;
124
123
  }
124
+ finally {
125
+ for (const variable of this._variables.values()) {
126
+ variable.value = undefined;
127
+ }
128
+ this._scope.clear();
129
+ }
125
130
  }
126
- /**
127
- Produces string in Affinirum Notation.
131
+ /** Parses an Affinirum Notation string into a JavaScript value.
132
+ @param value Affinirum Notation string to parse.
133
+ @returns The parsed value.
134
+ @throws If string is not a valid Affinirum Notation string.
135
+ */
136
+ static parse(value) {
137
+ return (0, AN_js_1.parseAN)(value);
138
+ }
139
+ /** Formats a JavaScript value as an Affinirum Notation string.
128
140
  @param value Value to format.
129
- @param whitespace Optional white space characters for readability.
130
- @returns String formatted in Affinirum Notation.
141
+ @param whitespace Optional whitespace used for each indentation level.
142
+ @returns Affinirum Notation formatted string.
131
143
  */
132
144
  static format(value, whitespace) {
133
145
  return (0, AN_js_1.formatAN)(value, whitespace);
@@ -330,20 +342,14 @@ class Affinirum {
330
342
  state.next();
331
343
  return new ConstantNode_js_1.ConstantNode(frame, constant);
332
344
  }
333
- let variable = scope.get(state.token);
345
+ let variable = scope.get(state.token) ?? this._variables.get(state.token);
334
346
  if (variable == null) {
335
- variable = this._variables.get(state.token);
336
- if (variable == null) {
337
- if (this._strict) {
338
- state.throwError(`undefined variable ${state.token} in strict mode`);
339
- }
340
- else {
341
- variable = new Variable_js_1.Variable();
342
- }
347
+ const type = this._vartypes.get(state.token);
348
+ if (type == null && this._strict) {
349
+ state.throwError(`undefined variable ${state.token} in strict mode`);
343
350
  }
344
- scope.global(state.token, variable);
345
- }
346
- if (!this._varframes.has(state.token)) {
351
+ variable = new Variable_js_1.Variable(type);
352
+ this._variables.set(state.token, variable);
347
353
  this._varframes.set(state.token, state.starts());
348
354
  }
349
355
  if (state.next().isAssignment) {
@@ -365,9 +371,10 @@ class Affinirum {
365
371
  }
366
372
  else if (state.isBracesOpen) { // code block
367
373
  const frame = state.starts();
374
+ const subscope = scope.subscope();
368
375
  const subnodes = [];
369
376
  while (!state.next().isBracesClose) {
370
- subnodes.push(this._unit(state, scope));
377
+ subnodes.push(this._unit(state, subscope));
371
378
  if (!state.isSemicolon) {
372
379
  break;
373
380
  }
@@ -434,7 +441,7 @@ class Affinirum {
434
441
  type = this._type(state.next(), scope);
435
442
  }
436
443
  const variable = new Variable_js_1.Variable(type, assignable);
437
- scope.local(token, variable);
444
+ scope.set(token, variable);
438
445
  if (state.isAssignment) {
439
446
  if (state.assignment.operator) {
440
447
  state.throwError(`illegal assignment operator with variable ${token}`);
@@ -483,7 +490,7 @@ class Affinirum {
483
490
  state.throwError("missing function argument name");
484
491
  }
485
492
  const token = state.token;
486
- if (scope.get(token)) {
493
+ if (scope.has(token)) {
487
494
  state.throwError("variable redefinition");
488
495
  }
489
496
  let argType = Type_js_1.Type.Unknown;
@@ -511,10 +518,17 @@ class Affinirum {
511
518
  const retType = state.isColon ? this._type(state.next(), scope) : Type_js_1.Type.Unknown;
512
519
  frame.ends(state);
513
520
  const args = Array.from(variables.values());
514
- const subnode = this._unit(state, scope.subscope(variables));
521
+ const subscope = scope.subscope(variables);
522
+ const subnode = this._unit(state, subscope);
515
523
  const func = (...values) => {
516
- args.forEach((arg, ix) => arg.value = (0, Value_js_1.normalize)(values[ix]));
517
- return (0, Value_js_1.normalize)(subnode.evaluate());
524
+ const previous = subscope.enter();
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());
528
+ }
529
+ finally {
530
+ subscope.leave(previous);
531
+ }
518
532
  };
519
533
  const constant = new Constant_js_1.Constant(func, Type_js_1.Type.functionType(retType, args.map((v) => v.type), variadic));
520
534
  return new ConstantNode_js_1.ConstantNode(frame, constant, subnode);
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Scope = void 0;
4
+ class Scope {
5
+ _subscopes = [];
6
+ _superscope;
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 Scope(this, variables);
26
+ }
27
+ enter(values = new Map()) {
28
+ for (const variable of this._variables.values()) {
29
+ values.set(variable, variable.value);
30
+ variable.value = undefined;
31
+ }
32
+ for (const subscope of this._subscopes) {
33
+ if (!subscope._boundary) {
34
+ subscope.enter(values);
35
+ }
36
+ }
37
+ return values;
38
+ }
39
+ leave(values) {
40
+ for (const [variable, value] of values) {
41
+ variable.value = value;
42
+ }
43
+ }
44
+ clear() {
45
+ for (const variable of this._variables.values()) {
46
+ variable.value = undefined;
47
+ }
48
+ for (const subscope of this._subscopes) {
49
+ subscope.clear();
50
+ }
51
+ }
52
+ }
53
+ exports.Scope = Scope;
@@ -1,4 +1,4 @@
1
1
  import { Value } from "../../Value.js";
2
2
  export declare const formatAN: (value: Value, whitespace?: string) => string;
3
- export declare const parseAN: (value?: string) => Value;
3
+ export declare const parseAN: (value: string | undefined) => Value;
4
4
  export declare const constAN: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "affinirum",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Affinirum Scripting Language",
5
5
  "main": "dst/cjs/index.js",
6
6
  "module": "dst/index.js",
@@ -1,13 +0,0 @@
1
- import { Variable } from "./Variable.js";
2
- export declare class StaticScope {
3
- protected _superscope?: StaticScope;
4
- protected _subscopes: StaticScope[];
5
- protected _variables: Map<string, Variable>;
6
- protected _locals: Set<string>;
7
- has(name: string): boolean;
8
- get(name: string): Variable | undefined;
9
- global(name: string, variable: Variable): StaticScope;
10
- local(name: string, variable: Variable): StaticScope;
11
- subscope(variables: Map<string, Variable>): StaticScope;
12
- variables(): Record<string, Variable>;
13
- }
@@ -1,44 +0,0 @@
1
- export class StaticScope {
2
- _superscope;
3
- _subscopes = [];
4
- _variables = new Map();
5
- _locals = new Set();
6
- has(name) {
7
- return this._variables.has(name) || Boolean(this._superscope?.has(name));
8
- }
9
- get(name) {
10
- return this._variables.get(name) ?? this._superscope?.get(name);
11
- }
12
- global(name, variable) {
13
- if (this._superscope) {
14
- this._superscope.global(name, variable);
15
- }
16
- else {
17
- this._variables.set(name, variable);
18
- }
19
- return this;
20
- }
21
- local(name, variable) {
22
- this._variables.set(name, variable);
23
- this._locals.add(name);
24
- return this;
25
- }
26
- subscope(variables) {
27
- const scope = new StaticScope();
28
- scope._superscope = this;
29
- this._subscopes.push(scope);
30
- for (const [name, variable] of variables) {
31
- scope.local(name, variable);
32
- }
33
- return scope;
34
- }
35
- variables() {
36
- const variables = Object.create(null);
37
- for (const [name, variable] of this._variables) {
38
- if (!this._locals.has(name)) {
39
- variables[name] = variable;
40
- }
41
- }
42
- return variables;
43
- }
44
- }
@@ -1,48 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StaticScope = void 0;
4
- class StaticScope {
5
- _superscope;
6
- _subscopes = [];
7
- _variables = new Map();
8
- _locals = new Set();
9
- has(name) {
10
- return this._variables.has(name) || Boolean(this._superscope?.has(name));
11
- }
12
- get(name) {
13
- return this._variables.get(name) ?? this._superscope?.get(name);
14
- }
15
- global(name, variable) {
16
- if (this._superscope) {
17
- this._superscope.global(name, variable);
18
- }
19
- else {
20
- this._variables.set(name, variable);
21
- }
22
- return this;
23
- }
24
- local(name, variable) {
25
- this._variables.set(name, variable);
26
- this._locals.add(name);
27
- return this;
28
- }
29
- subscope(variables) {
30
- const scope = new StaticScope();
31
- scope._superscope = this;
32
- this._subscopes.push(scope);
33
- for (const [name, variable] of variables) {
34
- scope.local(name, variable);
35
- }
36
- return scope;
37
- }
38
- variables() {
39
- const variables = Object.create(null);
40
- for (const [name, variable] of this._variables) {
41
- if (!this._locals.has(name)) {
42
- variables[name] = variable;
43
- }
44
- }
45
- return variables;
46
- }
47
- }
48
- exports.StaticScope = StaticScope;