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
@@ -5,10 +5,12 @@ const Node_js_1 = require("../Node.js");
5
5
  class ConstantNode extends Node_js_1.Node {
6
6
  _constant;
7
7
  _subnode;
8
- constructor(frame, _constant, _subnode) {
9
- super(frame);
8
+ _factory;
9
+ constructor(span, _constant, _subnode, _factory) {
10
+ super(span);
10
11
  this._constant = _constant;
11
12
  this._subnode = _subnode;
13
+ this._factory = _factory;
12
14
  }
13
15
  get type() {
14
16
  return this._constant.type;
@@ -18,11 +20,11 @@ class ConstantNode extends Node_js_1.Node {
18
20
  this._subnode = this._subnode?.compile(this._constant.type.functionAtomRetType(type));
19
21
  return this;
20
22
  }
21
- evaluate() {
22
- return this._constant.value;
23
+ evaluate(frame) {
24
+ return this._factory ? this._factory(frame) : this._constant.value;
23
25
  }
24
26
  get constant() {
25
- return !this._subnode && this._constant.deterministic;
27
+ return !this._factory && !this._subnode && this._constant.deterministic;
26
28
  }
27
29
  toString(ident = 0) {
28
30
  return this._subnode
@@ -7,8 +7,8 @@ const JumpException_js_1 = require("../JumpException.js");
7
7
  class JumpNode extends Node_js_1.Node {
8
8
  _jump;
9
9
  _subnode;
10
- constructor(frame, _jump, _subnode) {
11
- super(frame);
10
+ constructor(span, _jump, _subnode) {
11
+ super(span);
12
12
  this._jump = _jump;
13
13
  this._subnode = _subnode;
14
14
  }
@@ -19,8 +19,8 @@ class JumpNode extends Node_js_1.Node {
19
19
  this._subnode = this._subnode?.compile(type);
20
20
  return this;
21
21
  }
22
- evaluate() {
23
- throw new JumpException_js_1.JumpException(this._jump, this._subnode?.evaluate());
22
+ evaluate(frame) {
23
+ throw new JumpException_js_1.JumpException(this._jump, this._subnode?.evaluate(frame));
24
24
  }
25
25
  toString(ident = 0) {
26
26
  return this._subnode
@@ -7,8 +7,8 @@ const JumpException_js_1 = require("../JumpException.js");
7
7
  class LoopNode extends Node_js_1.Node {
8
8
  _cnode;
9
9
  _subnode;
10
- constructor(frame, _cnode, _subnode) {
11
- super(frame);
10
+ constructor(span, _cnode, _subnode) {
11
+ super(span);
12
12
  this._cnode = _cnode;
13
13
  this._subnode = _subnode;
14
14
  }
@@ -20,11 +20,11 @@ class LoopNode extends Node_js_1.Node {
20
20
  this._subnode = this._subnode.compile(type);
21
21
  return this;
22
22
  }
23
- evaluate() {
23
+ evaluate(frame) {
24
24
  let result = undefined;
25
- while (this._cnode.evaluate()) {
25
+ while (this._cnode.evaluate(frame)) {
26
26
  try {
27
- result = this._subnode.evaluate();
27
+ result = this._subnode.evaluate(frame);
28
28
  }
29
29
  catch (e) {
30
30
  if (e instanceof JumpException_js_1.JumpException) {
@@ -2,38 +2,32 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ObjectNode = void 0;
4
4
  const Node_js_1 = require("../Node.js");
5
- const Constant_js_1 = require("../Constant.js");
6
5
  const ConstantNode_js_1 = require("./ConstantNode.js");
7
6
  const Type_js_1 = require("../Type.js");
7
+ const CallFrame_js_1 = require("../CallFrame.js");
8
8
  class ObjectNode extends Node_js_1.Node {
9
9
  _subnodes;
10
10
  _type;
11
- constructor(frame, _subnodes) {
12
- super(frame);
11
+ constructor(span, _subnodes) {
12
+ super(span);
13
13
  this._subnodes = _subnodes;
14
- this._type = Type_js_1.Type.objectType(_subnodes.filter(([i]) => i instanceof ConstantNode_js_1.ConstantNode && i.constant).map(([k, v]) => [k.evaluate(), v.type]));
14
+ this._type = Type_js_1.Type.objectType(_subnodes.filter(([i]) => i instanceof ConstantNode_js_1.ConstantNode && i.constant).map(([k, v]) => [k.evaluate(new CallFrame_js_1.CallFrame()), v.type]));
15
15
  }
16
16
  get type() {
17
17
  return this._type;
18
18
  }
19
19
  compile(type) {
20
20
  this._type = this.reduceType(type);
21
- let constant = true;
22
21
  for (let i = 0; i < this._subnodes.length; ++i) {
23
22
  this._subnodes[i][0] = this._subnodes[i][0].compile(Type_js_1.Type.String);
24
- constant &&= this._subnodes[i][0].constant;
25
23
  this._subnodes[i][1] = this._subnodes[i][1].compile(Type_js_1.Type.Unknown);
26
- constant &&= this._subnodes[i][1].constant;
27
- }
28
- if (constant) {
29
- return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type));
30
24
  }
31
25
  return this;
32
26
  }
33
- evaluate() {
27
+ evaluate(frame) {
34
28
  const result = Object.create(null);
35
29
  for (const [key, value] of this._subnodes) {
36
- result[key.evaluate()] = value.evaluate();
30
+ result[key.evaluate(frame)] = value.evaluate(frame);
37
31
  }
38
32
  return result;
39
33
  }
@@ -5,12 +5,13 @@ const Node_js_1 = require("../Node.js");
5
5
  const Constant_js_1 = require("../Constant.js");
6
6
  const ConstantNode_js_1 = require("./ConstantNode.js");
7
7
  const Type_js_1 = require("../Type.js");
8
+ const CallFrame_js_1 = require("../CallFrame.js");
8
9
  class SwitchNode extends Node_js_1.Node {
9
10
  _cnode;
10
11
  _subnodes;
11
12
  _type;
12
- constructor(frame, _cnode, _subnodes) {
13
- super(frame);
13
+ constructor(span, _cnode, _subnodes) {
14
+ super(span);
14
15
  this._cnode = _cnode;
15
16
  this._subnodes = _subnodes;
16
17
  if (_subnodes.length !== 2) {
@@ -30,14 +31,14 @@ class SwitchNode extends Node_js_1.Node {
30
31
  constant &&= this._subnodes[i].constant;
31
32
  }
32
33
  if (constant) {
33
- return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type));
34
+ return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(new CallFrame_js_1.CallFrame()), this.type));
34
35
  }
35
36
  return this;
36
37
  }
37
- evaluate() {
38
- return this._cnode.evaluate()
39
- ? this._subnodes[0].evaluate()
40
- : this._subnodes[1].evaluate();
38
+ evaluate(frame) {
39
+ return this._cnode.evaluate(frame)
40
+ ? this._subnodes[0].evaluate(frame)
41
+ : this._subnodes[1].evaluate(frame);
41
42
  }
42
43
  toString(ident = 0) {
43
44
  const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join("\n");
@@ -5,8 +5,8 @@ const Node_js_1 = require("../Node.js");
5
5
  class VariableNode extends Node_js_1.Node {
6
6
  _variable;
7
7
  _subnode;
8
- constructor(frame, _variable, _subnode) {
9
- super(frame);
8
+ constructor(span, _variable, _subnode) {
9
+ super(span);
10
10
  this._variable = _variable;
11
11
  this._subnode = _subnode;
12
12
  }
@@ -18,10 +18,10 @@ class VariableNode extends Node_js_1.Node {
18
18
  this._variable.type = this.reduceType(this._subnode?.type ?? type);
19
19
  return this;
20
20
  }
21
- evaluate() {
21
+ evaluate(frame) {
22
22
  return this._subnode
23
- ? this._variable.value = this._subnode.evaluate()
24
- : this._variable.value;
23
+ ? frame.set(this._variable, this._subnode.evaluate(frame))
24
+ : frame.get(this._variable);
25
25
  }
26
26
  toString(ident = 0) {
27
27
  return this._subnode
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VariadicNode = void 0;
4
+ const ArrayNode_js_1 = require("./ArrayNode.js");
5
+ class VariadicNode extends ArrayNode_js_1.ArrayNode {
6
+ get constant() {
7
+ return this._subnodes.every((node) => node.constant);
8
+ }
9
+ }
10
+ exports.VariadicNode = VariadicNode;
@@ -4,9 +4,10 @@ import { boundInteger } from "./Integer.js";
4
4
  export const isSignSymbol = (c) => c === "+" || c === "-";
5
5
  export const isNumericSymbol = (c) => c >= "0" && c <= "9";
6
6
  export const formatLiteralNumber = (value) => {
7
- return typeof value === "number" && Number.isInteger(value)
8
- ? `${value.toString()}.0`
9
- : value.toString();
7
+ const str = value.toString();
8
+ return typeof value === "number" && Number.isInteger(value) && !str.includes("e")
9
+ ? `${str}.0`
10
+ : str;
10
11
  };
11
12
  export const parseLiteralNumber = (value, offset = 0) => {
12
13
  let i = offset;
@@ -84,23 +85,28 @@ export const funcPower = new Constant((value, exponent) => typeof value === "big
84
85
  ? boundInteger(value ** BigInt(exponent))
85
86
  : Number(value) ** Number(exponent), typeOperator);
86
87
  export const funcRoot = new Constant((value, exponent) => {
88
+ if (exponent <= 0) {
89
+ return undefined;
90
+ }
87
91
  if (typeof value === "bigint" && typeof exponent === "bigint") {
88
92
  const v = boundInteger(value);
89
93
  const e = boundInteger(exponent);
90
- if (v < 0n && e % 2n === 0n) {
94
+ const negative = v < 0n;
95
+ if (negative && e % 2n === 0n) {
91
96
  return undefined;
92
97
  }
93
- if (v <= 1n || e === 1n) {
98
+ const magnitude = negative ? -v : v;
99
+ if (magnitude <= 1n || e === 1n) {
94
100
  return v;
95
101
  }
96
- let low = 0n, high = v, result = 0n;
102
+ let low = 0n, high = magnitude, result = 0n;
97
103
  while (low <= high) {
98
104
  const mid = low + high >> 1n;
99
105
  const midPow = mid ** e;
100
- if (midPow === v) {
101
- return mid;
106
+ if (midPow === magnitude) {
107
+ return negative ? -mid : mid;
102
108
  }
103
- else if (midPow < v) {
109
+ else if (midPow < magnitude) {
104
110
  result = mid;
105
111
  low = mid + 1n;
106
112
  }
@@ -108,8 +114,15 @@ export const funcRoot = new Constant((value, exponent) => {
108
114
  high = mid - 1n;
109
115
  }
110
116
  }
111
- return result;
117
+ return negative ? -result : result;
112
118
  }
113
- return Math.pow(Number(value), 1 / Number(exponent));
119
+ const v = Number(value);
120
+ const e = Number(exponent);
121
+ const odd = typeof exponent === "bigint"
122
+ ? exponent % 2n !== 0n
123
+ : Number.isInteger(exponent) && exponent % 2 !== 0;
124
+ return (v < 0 || Object.is(v, -0)) && odd
125
+ ? -Math.pow(-v, 1 / e)
126
+ : Math.pow(v, 1 / e);
114
127
  }, typeOperator);
115
128
  export const funcNegate = new Constant((value) => value == null ? undefined : typeof value === "number" ? -value : boundInteger(-value), Type.union(Type.functionType(Type.Float, [Type.Float]), Type.functionType(Type.Integer, [Type.Integer])));
@@ -10,7 +10,7 @@ import { formatLiteralTimestamp } from "../Timestamp.js";
10
10
  const typeAN = Type.union(Type.Void, Type.Boolean, Type.Timestamp, Type.Float, Type.Integer, Type.Buffer, Type.String, Type.Array, Type.Object);
11
11
  export const formatAN = (value, whitespace) => {
12
12
  if (value == null) {
13
- return "";
13
+ return "null";
14
14
  }
15
15
  if (typeof value === "boolean") {
16
16
  return value ? "true" : "false";
@@ -29,7 +29,7 @@ export const formatAN = (value, whitespace) => {
29
29
  }
30
30
  if (Array.isArray(value)) {
31
31
  const [prefix, suffix] = whitespace ? ["\n" + whitespace, "\n"] : ["", ""];
32
- const lines = value.map((i) => `${prefix}${formatAN(i, whitespace)?.split("\n").join(prefix) ?? ""}`);
32
+ const lines = Array.from(value, (i) => `${prefix}${formatAN(i, whitespace)?.split("\n").join(prefix) ?? ""}`);
33
33
  return lines.length ? `[${lines.join(",")}${suffix}]` : "[]";
34
34
  }
35
35
  if (typeof value === "object") {
@@ -1,13 +1,14 @@
1
1
  import { Node } from "../Node.js";
2
- import { ParserFrame } from "../ParserFrame.js";
2
+ import { ScriptSpan } from "../ScriptSpan.js";
3
3
  import { Value } from "../Value.js";
4
4
  import { Type } from "../Type.js";
5
+ import { CallFrame } from "../CallFrame.js";
5
6
  export declare class ArrayNode extends Node {
6
7
  protected _subnodes: Node[];
7
8
  protected _type: Type;
8
- constructor(frame: ParserFrame, _subnodes: Node[]);
9
+ constructor(span: ScriptSpan, _subnodes: Node[]);
9
10
  get type(): Type;
10
11
  compile(type: Type): Node;
11
- evaluate(): Value;
12
+ evaluate(frame: CallFrame): Value;
12
13
  toString(ident?: number): string;
13
14
  }
@@ -1,12 +1,10 @@
1
1
  import { Node } from "../Node.js";
2
- import { Constant } from "../Constant.js";
3
- import { ConstantNode } from "./ConstantNode.js";
4
2
  import { Type } from "../Type.js";
5
3
  export class ArrayNode extends Node {
6
4
  _subnodes;
7
5
  _type;
8
- constructor(frame, _subnodes) {
9
- super(frame);
6
+ constructor(span, _subnodes) {
7
+ super(span);
10
8
  this._subnodes = _subnodes;
11
9
  this._type = Type.arrayType(_subnodes.map((i) => i.type));
12
10
  }
@@ -14,23 +12,18 @@ export class ArrayNode extends Node {
14
12
  return this._type;
15
13
  }
16
14
  compile(type) {
17
- let constant = true;
18
15
  for (let i = 0; i < this._subnodes.length; ++i) {
19
16
  const valType = type.arrayAtomValType(i);
20
17
  this._subnodes[i] = this._subnodes[i].compile(valType);
21
- constant &&= this._subnodes[i].constant;
22
18
  }
23
19
  this._type = Type.arrayType(this._subnodes.map((node) => node.type));
24
20
  this._type = this.reduceType(type);
25
- if (constant) {
26
- return new ConstantNode(this, new Constant(this.evaluate(), this.type));
27
- }
28
21
  return this;
29
22
  }
30
- evaluate() {
23
+ evaluate(frame) {
31
24
  const result = [];
32
25
  for (const subnode of this._subnodes) {
33
- result.push(subnode.evaluate());
26
+ result.push(subnode.evaluate(frame));
34
27
  }
35
28
  return result;
36
29
  }
@@ -1,12 +1,15 @@
1
1
  import { Node } from "../Node.js";
2
- import { ParserFrame } from "../ParserFrame.js";
2
+ import { ScriptSpan } from "../ScriptSpan.js";
3
3
  import { Value } from "../Value.js";
4
4
  import { Type } from "../Type.js";
5
+ import { CallFrame } from "../CallFrame.js";
6
+ import { Variable } from "../Variable.js";
5
7
  export declare class BlockNode extends Node {
6
8
  protected _subnodes: Node[];
7
- constructor(frame: ParserFrame, _subnodes: Node[]);
9
+ protected readonly _variables: Variable[];
10
+ constructor(span: ScriptSpan, _subnodes: Node[], _variables?: Variable[]);
8
11
  get type(): Type;
9
12
  compile(type: Type): Node;
10
- evaluate(): Value;
13
+ evaluate(frame: CallFrame): Value;
11
14
  toString(ident?: number): string;
12
15
  }
@@ -2,11 +2,14 @@ import { Node } from "../Node.js";
2
2
  import { Constant } from "../Constant.js";
3
3
  import { ConstantNode } from "./ConstantNode.js";
4
4
  import { Type } from "../Type.js";
5
+ import { CallFrame } from "../CallFrame.js";
5
6
  export class BlockNode extends Node {
6
7
  _subnodes;
7
- constructor(frame, _subnodes) {
8
- super(frame);
8
+ _variables;
9
+ constructor(span, _subnodes, _variables = []) {
10
+ super(span);
9
11
  this._subnodes = _subnodes;
12
+ this._variables = _variables;
10
13
  }
11
14
  get type() {
12
15
  return this._subnodes.length ? this._subnodes[this._subnodes.length - 1].type : Type.Void;
@@ -22,17 +25,18 @@ export class BlockNode extends Node {
22
25
  constant &&= this._subnodes[i].constant;
23
26
  }
24
27
  if (constant) {
25
- return new ConstantNode(this, new Constant(this.evaluate(), this.type));
28
+ return new ConstantNode(this, new Constant(this.evaluate(new CallFrame()), this.type));
26
29
  }
27
- if (this._subnodes.length === 1) {
30
+ if (this._subnodes.length === 1 && this._variables.length === 0) {
28
31
  return this._subnodes[0];
29
32
  }
30
33
  return this;
31
34
  }
32
- evaluate() {
35
+ evaluate(frame) {
36
+ const subframe = this._variables.length ? frame.subframe(this._variables) : frame;
33
37
  let result = undefined;
34
38
  for (const subnode of this._subnodes) {
35
- result = subnode.evaluate();
39
+ result = subnode.evaluate(subframe);
36
40
  }
37
41
  return result;
38
42
  }
@@ -1,14 +1,15 @@
1
1
  import { Node } from "../Node.js";
2
- import { ParserFrame } from "../ParserFrame.js";
2
+ import { ScriptSpan } from "../ScriptSpan.js";
3
3
  import { Value } from "../Value.js";
4
4
  import { Type } from "../Type.js";
5
+ import { CallFrame } from "../CallFrame.js";
5
6
  export declare class CallNode extends Node {
6
7
  protected _fnode: Node;
7
8
  protected _subnodes: Node[];
8
9
  protected _type: Type;
9
- constructor(frame: ParserFrame, _fnode: Node, _subnodes: Node[]);
10
+ constructor(span: ScriptSpan, _fnode: Node, _subnodes: Node[]);
10
11
  get type(): Type;
11
12
  compile(type: Type): Node;
12
- evaluate(): Value;
13
+ evaluate(frame: CallFrame): Value;
13
14
  toString(ident?: number): string;
14
15
  }
@@ -1,16 +1,16 @@
1
1
  import { Node } from "../Node.js";
2
2
  import { Constant } from "../Constant.js";
3
3
  import { ConstantNode } from "./ConstantNode.js";
4
- import { ArrayNode } from "./ArrayNode.js";
4
+ import { VariadicNode } from "./VariadicNode.js";
5
5
  import { normalize } from "../Value.js";
6
6
  import { Type } from "../Type.js";
7
- import { JumpException } from "../JumpException.js";
7
+ import { CallFrame } from "../CallFrame.js";
8
8
  export class CallNode extends Node {
9
9
  _fnode;
10
10
  _subnodes;
11
11
  _type;
12
- constructor(frame, _fnode, _subnodes) {
13
- super(frame);
12
+ constructor(span, _fnode, _subnodes) {
13
+ super(span);
14
14
  this._fnode = _fnode;
15
15
  this._subnodes = _subnodes;
16
16
  const functionAtoms = this._fnode.type.functionAtoms(Type.Unknown, this._subnodes.length);
@@ -33,47 +33,34 @@ export class CallNode extends Node {
33
33
  this.throwError(`value of type ${this._fnode.type} cannot be called with ${this._subnodes.length} argument(s) and return a type compatible with ${type}`);
34
34
  }
35
35
  this._type = Type.union(...functionAtoms.map((i) => i.retType));
36
- const arity = functionAtoms[0].isVariadic
36
+ const variadicArity = functionAtoms[0].isVariadic
37
37
  ? Math.max(...functionAtoms.map((atom) => atom.arity))
38
38
  : 0;
39
- if (arity > 0) {
40
- const frame = this._subnodes[arity - 1].starts();
41
- const vnodes = this._subnodes.splice(arity - 1);
42
- this._subnodes.push(new ArrayNode(frame.ends(vnodes[vnodes.length - 1]), vnodes));
39
+ if (variadicArity > 0) {
40
+ const span = this._subnodes[variadicArity - 1].start();
41
+ const vnodes = this._subnodes.splice(variadicArity - 1);
42
+ this._subnodes.push(new VariadicNode(span.end(vnodes[vnodes.length - 1]), vnodes));
43
43
  }
44
44
  let constant = this._fnode.constant;
45
45
  for (let i = 0; i < this._subnodes.length; ++i) {
46
46
  const argTypes = functionAtoms.map((a) => a.argType(i));
47
- const argType = arity > 0 && i === arity - 1
47
+ const argType = variadicArity > 0 && i === variadicArity - 1
48
48
  ? Type.arrayType([Type.union(...functionAtoms.map((a) => a.paramType(i) ?? Type.Unknown))])
49
49
  : Type.union(...argTypes.filter((t) => t != null));
50
50
  this._subnodes[i] = this._subnodes[i].compile(argType);
51
51
  constant &&= this._subnodes[i].constant;
52
52
  }
53
- if (constant) {
54
- return new ConstantNode(this, new Constant(this.evaluate(), this.type));
53
+ if (constant && !this.type.intersect(Type.Reference)) {
54
+ return new ConstantNode(this, new Constant(this.evaluate(new CallFrame()), this.type));
55
55
  }
56
56
  return this;
57
57
  }
58
- evaluate() {
59
- const func = this._fnode.evaluate();
58
+ evaluate(frame) {
59
+ const func = this._fnode.evaluate(frame);
60
60
  if (typeof func !== "function") {
61
61
  this.throwError(`function expected not ${Type.of(func)}`);
62
62
  }
63
- try {
64
- return normalize(func(...this._subnodes.map((node) => node.evaluate())));
65
- }
66
- catch (e) {
67
- if (e instanceof JumpException) {
68
- if (e.jump === "return") {
69
- return normalize(e.value);
70
- }
71
- if (e.jump === "stop" || e.jump === "next") {
72
- throw this.throwError(`unexpected ${e.jump} jump`);
73
- }
74
- }
75
- throw e;
76
- }
63
+ return normalize(func(...this._subnodes.map((node) => node.evaluate(frame))));
77
64
  }
78
65
  toString(ident = 0) {
79
66
  const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join("\n");
@@ -1,15 +1,16 @@
1
1
  import { Node } from "../Node.js";
2
- import { ParserFrame } from "../ParserFrame.js";
2
+ import { ScriptSpan } from "../ScriptSpan.js";
3
3
  import { Type } from "../Type.js";
4
4
  import { Value } from "../Value.js";
5
+ import { CallFrame } from "../CallFrame.js";
5
6
  export declare class CastNode extends Node {
6
7
  protected _subnode: Node;
7
8
  protected readonly _target: Type;
8
9
  protected _type: Type;
9
- constructor(frame: ParserFrame, _subnode: Node, _target: Type);
10
+ constructor(span: ScriptSpan, _subnode: Node, _target: Type);
10
11
  get type(): Type;
11
12
  compile(type: Type): Node;
12
- evaluate(): Value;
13
+ evaluate(frame: CallFrame): Value;
13
14
  get constant(): boolean;
14
15
  toString(ident?: number): string;
15
16
  }
@@ -4,8 +4,8 @@ export class CastNode extends Node {
4
4
  _subnode;
5
5
  _target;
6
6
  _type;
7
- constructor(frame, _subnode, _target) {
8
- super(frame);
7
+ constructor(span, _subnode, _target) {
8
+ super(span);
9
9
  this._subnode = _subnode;
10
10
  this._target = _target;
11
11
  this._type = _target;
@@ -22,8 +22,8 @@ export class CastNode extends Node {
22
22
  this._type = this.reduceType(type);
23
23
  return this;
24
24
  }
25
- evaluate() {
26
- const value = this._subnode.evaluate();
25
+ evaluate(frame) {
26
+ const value = this._subnode.evaluate(frame);
27
27
  if (!this._type.acceptValue(value)) {
28
28
  this.throwError(`cannot cast value of type ${Type.of(value)} to ${this._type}`);
29
29
  }
@@ -1,15 +1,17 @@
1
1
  import { Node } from "../Node.js";
2
- import { ParserFrame } from "../ParserFrame.js";
2
+ import { ScriptSpan } from "../ScriptSpan.js";
3
3
  import { Constant } from "../Constant.js";
4
4
  import { Value } from "../Value.js";
5
5
  import { Type } from "../Type.js";
6
+ import { CallFrame } from "../CallFrame.js";
6
7
  export declare class ConstantNode extends Node {
7
8
  protected readonly _constant: Constant;
8
9
  protected _subnode?: Node | undefined;
9
- constructor(frame: ParserFrame, _constant: Constant, _subnode?: Node | undefined);
10
+ protected readonly _factory?: ((frame: CallFrame) => Value) | undefined;
11
+ constructor(span: ScriptSpan, _constant: Constant, _subnode?: Node | undefined, _factory?: ((frame: CallFrame) => Value) | undefined);
10
12
  get type(): Type;
11
13
  compile(type: Type): Node;
12
- evaluate(): Value;
14
+ evaluate(frame: CallFrame): Value;
13
15
  get constant(): boolean;
14
16
  toString(ident?: number): string;
15
17
  }
@@ -2,10 +2,12 @@ import { Node } from "../Node.js";
2
2
  export class ConstantNode extends Node {
3
3
  _constant;
4
4
  _subnode;
5
- constructor(frame, _constant, _subnode) {
6
- super(frame);
5
+ _factory;
6
+ constructor(span, _constant, _subnode, _factory) {
7
+ super(span);
7
8
  this._constant = _constant;
8
9
  this._subnode = _subnode;
10
+ this._factory = _factory;
9
11
  }
10
12
  get type() {
11
13
  return this._constant.type;
@@ -15,11 +17,11 @@ export class ConstantNode extends Node {
15
17
  this._subnode = this._subnode?.compile(this._constant.type.functionAtomRetType(type));
16
18
  return this;
17
19
  }
18
- evaluate() {
19
- return this._constant.value;
20
+ evaluate(frame) {
21
+ return this._factory ? this._factory(frame) : this._constant.value;
20
22
  }
21
23
  get constant() {
22
- return !this._subnode && this._constant.deterministic;
24
+ return !this._factory && !this._subnode && this._constant.deterministic;
23
25
  }
24
26
  toString(ident = 0) {
25
27
  return this._subnode
@@ -1,14 +1,15 @@
1
1
  import { Node } from "../Node.js";
2
- import { ParserFrame } from "../ParserFrame.js";
2
+ import { ScriptSpan } from "../ScriptSpan.js";
3
3
  import { Value } from "../Value.js";
4
4
  import { Type } from "../Type.js";
5
5
  import { Jump } from "../Jump.js";
6
+ import { CallFrame } from "../CallFrame.js";
6
7
  export declare class JumpNode extends Node {
7
8
  protected _jump: Jump;
8
9
  protected _subnode?: Node | undefined;
9
- constructor(frame: ParserFrame, _jump: Jump, _subnode?: Node | undefined);
10
+ constructor(span: ScriptSpan, _jump: Jump, _subnode?: Node | undefined);
10
11
  get type(): Type;
11
12
  compile(type: Type): Node;
12
- evaluate(): Value;
13
+ evaluate(frame: CallFrame): Value;
13
14
  toString(ident?: number): string;
14
15
  }
@@ -4,8 +4,8 @@ import { JumpException } from "../JumpException.js";
4
4
  export class JumpNode extends Node {
5
5
  _jump;
6
6
  _subnode;
7
- constructor(frame, _jump, _subnode) {
8
- super(frame);
7
+ constructor(span, _jump, _subnode) {
8
+ super(span);
9
9
  this._jump = _jump;
10
10
  this._subnode = _subnode;
11
11
  }
@@ -16,8 +16,8 @@ export class JumpNode extends Node {
16
16
  this._subnode = this._subnode?.compile(type);
17
17
  return this;
18
18
  }
19
- evaluate() {
20
- throw new JumpException(this._jump, this._subnode?.evaluate());
19
+ evaluate(frame) {
20
+ throw new JumpException(this._jump, this._subnode?.evaluate(frame));
21
21
  }
22
22
  toString(ident = 0) {
23
23
  return this._subnode