affinirum 1.5.4 → 2.0.0

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 (243) hide show
  1. package/.cache/arrow-preview-vWvB5K/affinirum-arrow-functions.patch +155 -0
  2. package/.cache/function-syntax-tests.log +20 -0
  3. package/README.md +925 -355
  4. package/dst/Affinirum.d.ts +29 -19
  5. package/dst/Affinirum.js +399 -334
  6. package/dst/CallFrame.d.ts +12 -3
  7. package/dst/CallFrame.js +49 -10
  8. package/dst/Constant.d.ts +1 -3
  9. package/dst/Constant.js +3 -8
  10. package/dst/LexicalScope.d.ts +11 -9
  11. package/dst/LexicalScope.js +10 -6
  12. package/dst/ParserSpan.d.ts +11 -0
  13. package/dst/{ScriptSpan.js → ParserSpan.js} +8 -5
  14. package/dst/ParserState.d.ts +28 -49
  15. package/dst/ParserState.js +256 -489
  16. package/dst/Type.d.ts +34 -17
  17. package/dst/Type.js +211 -115
  18. package/dst/TypeParser.d.ts +9 -0
  19. package/dst/TypeParser.js +127 -0
  20. package/dst/Value.d.ts +1 -1
  21. package/dst/Value.js +21 -10
  22. package/dst/Variable.d.ts +16 -5
  23. package/dst/Variable.js +32 -7
  24. package/dst/atom/ArrayAtom.d.ts +10 -10
  25. package/dst/atom/ArrayAtom.js +62 -55
  26. package/dst/atom/Atom.d.ts +7 -0
  27. package/dst/atom/FunctionAtom.d.ts +13 -15
  28. package/dst/atom/FunctionAtom.js +53 -104
  29. package/dst/atom/FunctionSignature.d.ts +23 -0
  30. package/dst/atom/FunctionSignature.js +124 -0
  31. package/dst/atom/ObjectAtom.d.ts +11 -9
  32. package/dst/atom/ObjectAtom.js +73 -33
  33. package/dst/atom/PrimitiveAtom.d.ts +6 -7
  34. package/dst/atom/PrimitiveAtom.js +7 -10
  35. package/dst/cjs/Affinirum.d.ts +29 -19
  36. package/dst/cjs/Affinirum.js +398 -333
  37. package/dst/cjs/CallFrame.d.ts +12 -3
  38. package/dst/cjs/CallFrame.js +49 -10
  39. package/dst/cjs/Constant.d.ts +1 -3
  40. package/dst/cjs/Constant.js +3 -8
  41. package/dst/cjs/LexicalScope.d.ts +11 -9
  42. package/dst/cjs/LexicalScope.js +10 -6
  43. package/dst/cjs/ParserSpan.d.ts +11 -0
  44. package/dst/cjs/{ScriptSpan.js → ParserSpan.js} +10 -7
  45. package/dst/cjs/ParserState.d.ts +28 -49
  46. package/dst/cjs/ParserState.js +255 -488
  47. package/dst/cjs/Type.d.ts +34 -17
  48. package/dst/cjs/Type.js +211 -115
  49. package/dst/cjs/TypeParser.d.ts +9 -0
  50. package/dst/cjs/TypeParser.js +131 -0
  51. package/dst/cjs/Value.d.ts +1 -1
  52. package/dst/cjs/Value.js +21 -10
  53. package/dst/cjs/Variable.d.ts +16 -5
  54. package/dst/cjs/Variable.js +36 -9
  55. package/dst/cjs/atom/ArrayAtom.d.ts +10 -10
  56. package/dst/cjs/atom/ArrayAtom.js +62 -55
  57. package/dst/cjs/atom/Atom.d.ts +7 -0
  58. package/dst/cjs/atom/FunctionAtom.d.ts +13 -15
  59. package/dst/cjs/atom/FunctionAtom.js +54 -105
  60. package/dst/cjs/atom/FunctionSignature.d.ts +23 -0
  61. package/dst/cjs/atom/FunctionSignature.js +128 -0
  62. package/dst/cjs/atom/ObjectAtom.d.ts +11 -9
  63. package/dst/cjs/atom/ObjectAtom.js +73 -33
  64. package/dst/cjs/atom/PrimitiveAtom.d.ts +6 -7
  65. package/dst/cjs/atom/PrimitiveAtom.js +7 -10
  66. package/dst/cjs/dictionary/Constants.d.ts +2 -0
  67. package/dst/cjs/dictionary/Constants.js +27 -0
  68. package/dst/cjs/dictionary/Functions.d.ts +2 -0
  69. package/dst/cjs/{Functions.js → dictionary/Functions.js} +27 -25
  70. package/dst/cjs/native/Aggregable.js +10 -2
  71. package/dst/cjs/native/Array.d.ts +2 -3
  72. package/dst/cjs/native/Array.js +38 -39
  73. package/dst/cjs/native/Boolean.d.ts +1 -1
  74. package/dst/cjs/native/Boolean.js +13 -10
  75. package/dst/cjs/native/Buffer.d.ts +5 -5
  76. package/dst/cjs/native/Buffer.js +70 -46
  77. package/dst/cjs/native/Composite.d.ts +5 -0
  78. package/dst/cjs/native/Composite.js +35 -0
  79. package/dst/cjs/native/Enumerable.js +20 -8
  80. package/dst/cjs/native/Float.d.ts +3 -1
  81. package/dst/cjs/native/Float.js +51 -12
  82. package/dst/cjs/native/Integer.d.ts +4 -2
  83. package/dst/cjs/native/Integer.js +60 -25
  84. package/dst/cjs/native/Iterable.d.ts +0 -2
  85. package/dst/cjs/native/Iterable.js +9 -28
  86. package/dst/cjs/native/Number.d.ts +0 -2
  87. package/dst/cjs/native/Number.js +34 -48
  88. package/dst/cjs/native/Object.d.ts +1 -1
  89. package/dst/cjs/native/Object.js +5 -6
  90. package/dst/cjs/native/String.d.ts +7 -6
  91. package/dst/cjs/native/String.js +28 -27
  92. package/dst/cjs/native/Timestamp.d.ts +3 -3
  93. package/dst/cjs/native/Timestamp.js +34 -36
  94. package/dst/cjs/native/Unknown.d.ts +3 -3
  95. package/dst/cjs/native/Unknown.js +48 -17
  96. package/dst/cjs/native/notation/AN.d.ts +1 -1
  97. package/dst/cjs/native/notation/AN.js +37 -35
  98. package/dst/cjs/native/notation/IP.d.ts +1 -1
  99. package/dst/cjs/native/notation/IP.js +11 -12
  100. package/dst/cjs/native/notation/JSON.d.ts +1 -1
  101. package/dst/cjs/native/notation/JSON.js +7 -6
  102. package/dst/cjs/node/AccessNode.d.ts +24 -0
  103. package/dst/cjs/node/AccessNode.js +193 -0
  104. package/dst/cjs/node/ArrayNode.d.ts +7 -4
  105. package/dst/cjs/node/ArrayNode.js +26 -15
  106. package/dst/cjs/node/AssignableNode.d.ts +16 -0
  107. package/dst/cjs/node/AssignableNode.js +18 -0
  108. package/dst/cjs/node/AssignmentNode.d.ts +20 -0
  109. package/dst/cjs/node/AssignmentNode.js +66 -0
  110. package/dst/cjs/node/BlockNode.d.ts +5 -4
  111. package/dst/cjs/node/BlockNode.js +18 -11
  112. package/dst/cjs/node/CallNode.d.ts +7 -5
  113. package/dst/cjs/node/CallNode.js +40 -41
  114. package/dst/cjs/node/CastNode.d.ts +5 -6
  115. package/dst/cjs/node/CastNode.js +16 -17
  116. package/dst/cjs/node/ConstantNode.d.ts +4 -6
  117. package/dst/cjs/node/ConstantNode.js +9 -17
  118. package/dst/cjs/node/FinalNode.d.ts +19 -0
  119. package/dst/cjs/node/FinalNode.js +41 -0
  120. package/dst/cjs/node/FunctionNode.d.ts +17 -0
  121. package/dst/cjs/node/FunctionNode.js +51 -0
  122. package/dst/cjs/node/InvocationNode.d.ts +5 -0
  123. package/dst/cjs/node/InvocationNode.js +34 -0
  124. package/dst/cjs/node/JumpNode.d.ts +7 -9
  125. package/dst/cjs/node/JumpNode.js +12 -19
  126. package/dst/cjs/node/LoopNode.d.ts +7 -5
  127. package/dst/cjs/node/LoopNode.js +33 -22
  128. package/dst/cjs/node/MatchNode.d.ts +17 -0
  129. package/dst/cjs/node/MatchNode.js +65 -0
  130. package/dst/cjs/node/Node.d.ts +14 -0
  131. package/dst/cjs/{Node.js → node/Node.js} +17 -11
  132. package/dst/cjs/node/ObjectNode.d.ts +7 -4
  133. package/dst/cjs/node/ObjectNode.js +42 -11
  134. package/dst/cjs/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
  135. package/dst/cjs/node/TernaryNode.js +45 -0
  136. package/dst/cjs/node/VariableNode.d.ts +10 -6
  137. package/dst/cjs/node/VariableNode.js +50 -16
  138. package/dst/dictionary/Constants.d.ts +2 -0
  139. package/dst/dictionary/Constants.js +24 -0
  140. package/dst/dictionary/Functions.d.ts +2 -0
  141. package/dst/{Functions.js → dictionary/Functions.js} +26 -24
  142. package/dst/native/Aggregable.js +11 -3
  143. package/dst/native/Array.d.ts +2 -3
  144. package/dst/native/Array.js +37 -38
  145. package/dst/native/Boolean.d.ts +1 -1
  146. package/dst/native/Boolean.js +12 -9
  147. package/dst/native/Buffer.d.ts +5 -5
  148. package/dst/native/Buffer.js +66 -42
  149. package/dst/native/Composite.d.ts +5 -0
  150. package/dst/native/Composite.js +32 -0
  151. package/dst/native/Enumerable.js +20 -8
  152. package/dst/native/Float.d.ts +3 -1
  153. package/dst/native/Float.js +48 -11
  154. package/dst/native/Integer.d.ts +4 -2
  155. package/dst/native/Integer.js +56 -23
  156. package/dst/native/Iterable.d.ts +0 -2
  157. package/dst/native/Iterable.js +8 -27
  158. package/dst/native/Number.d.ts +0 -2
  159. package/dst/native/Number.js +34 -46
  160. package/dst/native/Object.d.ts +1 -1
  161. package/dst/native/Object.js +4 -5
  162. package/dst/native/String.d.ts +7 -6
  163. package/dst/native/String.js +24 -24
  164. package/dst/native/Timestamp.d.ts +3 -3
  165. package/dst/native/Timestamp.js +31 -33
  166. package/dst/native/Unknown.d.ts +3 -3
  167. package/dst/native/Unknown.js +49 -18
  168. package/dst/native/notation/AN.d.ts +1 -1
  169. package/dst/native/notation/AN.js +37 -35
  170. package/dst/native/notation/IP.d.ts +1 -1
  171. package/dst/native/notation/IP.js +10 -11
  172. package/dst/native/notation/JSON.d.ts +1 -1
  173. package/dst/native/notation/JSON.js +6 -5
  174. package/dst/node/AccessNode.d.ts +24 -0
  175. package/dst/node/AccessNode.js +189 -0
  176. package/dst/node/ArrayNode.d.ts +7 -4
  177. package/dst/node/ArrayNode.js +26 -15
  178. package/dst/node/AssignableNode.d.ts +16 -0
  179. package/dst/node/AssignableNode.js +14 -0
  180. package/dst/node/AssignmentNode.d.ts +20 -0
  181. package/dst/node/AssignmentNode.js +62 -0
  182. package/dst/node/BlockNode.d.ts +5 -4
  183. package/dst/node/BlockNode.js +18 -11
  184. package/dst/node/CallNode.d.ts +7 -5
  185. package/dst/node/CallNode.js +40 -41
  186. package/dst/node/CastNode.d.ts +5 -6
  187. package/dst/node/CastNode.js +16 -17
  188. package/dst/node/ConstantNode.d.ts +4 -6
  189. package/dst/node/ConstantNode.js +9 -17
  190. package/dst/node/FinalNode.d.ts +19 -0
  191. package/dst/node/FinalNode.js +37 -0
  192. package/dst/node/FunctionNode.d.ts +17 -0
  193. package/dst/node/FunctionNode.js +47 -0
  194. package/dst/node/InvocationNode.d.ts +5 -0
  195. package/dst/node/InvocationNode.js +30 -0
  196. package/dst/node/JumpNode.d.ts +7 -9
  197. package/dst/node/JumpNode.js +12 -19
  198. package/dst/node/LoopNode.d.ts +7 -5
  199. package/dst/node/LoopNode.js +33 -22
  200. package/dst/node/MatchNode.d.ts +17 -0
  201. package/dst/node/MatchNode.js +61 -0
  202. package/dst/node/Node.d.ts +14 -0
  203. package/dst/node/Node.js +28 -0
  204. package/dst/node/ObjectNode.d.ts +7 -4
  205. package/dst/node/ObjectNode.js +42 -11
  206. package/dst/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
  207. package/dst/node/TernaryNode.js +41 -0
  208. package/dst/node/VariableNode.d.ts +10 -6
  209. package/dst/node/VariableNode.js +50 -16
  210. package/package.json +4 -4
  211. package/dst/Atom.d.ts +0 -7
  212. package/dst/Constants.d.ts +0 -2
  213. package/dst/Constants.js +0 -24
  214. package/dst/Functions.d.ts +0 -2
  215. package/dst/Jump.d.ts +0 -1
  216. package/dst/Jump.js +0 -1
  217. package/dst/JumpException.d.ts +0 -9
  218. package/dst/JumpException.js +0 -14
  219. package/dst/Keywords.d.ts +0 -1
  220. package/dst/Keywords.js +0 -17
  221. package/dst/Node.d.ts +0 -14
  222. package/dst/Node.js +0 -22
  223. package/dst/ScriptSpan.d.ts +0 -10
  224. package/dst/cjs/Atom.d.ts +0 -7
  225. package/dst/cjs/Constants.d.ts +0 -2
  226. package/dst/cjs/Constants.js +0 -27
  227. package/dst/cjs/Functions.d.ts +0 -2
  228. package/dst/cjs/Jump.d.ts +0 -1
  229. package/dst/cjs/Jump.js +0 -2
  230. package/dst/cjs/JumpException.d.ts +0 -9
  231. package/dst/cjs/JumpException.js +0 -18
  232. package/dst/cjs/Keywords.d.ts +0 -1
  233. package/dst/cjs/Keywords.js +0 -20
  234. package/dst/cjs/Node.d.ts +0 -14
  235. package/dst/cjs/ScriptSpan.d.ts +0 -10
  236. package/dst/cjs/node/SwitchNode.js +0 -49
  237. package/dst/cjs/node/VariadicNode.d.ts +0 -4
  238. package/dst/cjs/node/VariadicNode.js +0 -10
  239. package/dst/node/SwitchNode.js +0 -45
  240. package/dst/node/VariadicNode.d.ts +0 -4
  241. package/dst/node/VariadicNode.js +0 -6
  242. /package/dst/{Atom.js → atom/Atom.js} +0 -0
  243. /package/dst/cjs/{Atom.js → atom/Atom.js} +0 -0
@@ -0,0 +1,62 @@
1
+ import { InvocationNode } from "./InvocationNode.js";
2
+ import { Type } from "../Type.js";
3
+ import { funcAnd, funcOr } from "../native/Boolean.js";
4
+ export class AssignmentNode extends InvocationNode {
5
+ _anode;
6
+ _subnode;
7
+ _operator;
8
+ _type;
9
+ constructor(span, anode, subnode, operator) {
10
+ super(span);
11
+ this._anode = anode;
12
+ this._subnode = subnode;
13
+ this._operator = operator;
14
+ }
15
+ get type() {
16
+ const valType = this._type ?? this._operator?.type.functionAtomRetType() ?? this._subnode.type;
17
+ const resultType = this._anode.reduceAssignmentType(valType) ?? this._anode.type;
18
+ return this._anode.optionalAssignment ? resultType.toOptional() : resultType;
19
+ }
20
+ satisfies(type) {
21
+ return super.satisfies(type) &&
22
+ (this._operator !== undefined || !type.accepts(this._subnode.type) || this._subnode.satisfies(type));
23
+ }
24
+ compile(type = Type.Any) {
25
+ if (this._operator) {
26
+ // Resolve the target type before checking it as the operator's first operand.
27
+ this._anode = this._anode.compileAssignment(Type.Any);
28
+ const args = [this._anode, this._subnode];
29
+ this._type = this.compileArguments(this._operator.type, args, 1);
30
+ this._subnode = args[1];
31
+ }
32
+ else {
33
+ this._subnode = this._subnode.compile(type);
34
+ }
35
+ this._anode = this._anode.compileAssignment(this._type ?? this._subnode.type);
36
+ return super.compile(type);
37
+ }
38
+ evaluate(frame) {
39
+ const resolver = this._anode.resolver(frame);
40
+ if (!resolver) {
41
+ return undefined;
42
+ }
43
+ if (!this._operator) {
44
+ return resolver.set(this._subnode.evaluate(frame));
45
+ }
46
+ const value = resolver.get();
47
+ if (resolver.type && !resolver.type.acceptsValue(value)) {
48
+ this._anode.throwError(`indexed value mismatch with expected type ${resolver.type}`);
49
+ }
50
+ const operation = this._operator.value;
51
+ if (operation === funcAnd.value && value === false ||
52
+ operation === funcOr.value && value === true) {
53
+ return value;
54
+ }
55
+ const result = operation(value, this._subnode.evaluate(frame));
56
+ return resolver.set(frame.define(result, this._type ?? Type.Any));
57
+ }
58
+ toString(ident = 0) {
59
+ return `${super.toString(ident)} assignment node anode:\n${this._anode.toString(ident + 1)}\n`
60
+ + `${super.toString(ident)} assignment node subnode:\n${this._subnode.toString(ident + 1)}`;
61
+ }
62
+ }
@@ -1,5 +1,5 @@
1
- import { Node } from "../Node.js";
2
- import { ScriptSpan } from "../ScriptSpan.js";
1
+ import { Node } from "./Node.js";
2
+ import { ParserSpan } from "../ParserSpan.js";
3
3
  import { Value } from "../Value.js";
4
4
  import { Type } from "../Type.js";
5
5
  import { CallFrame } from "../CallFrame.js";
@@ -7,9 +7,10 @@ import { Variable } from "../Variable.js";
7
7
  export declare class BlockNode extends Node {
8
8
  protected _subnodes: Node[];
9
9
  protected readonly _variables: Variable[];
10
- constructor(span: ScriptSpan, _subnodes: Node[], _variables?: Variable[]);
10
+ constructor(span: ParserSpan, subnodes: Node[], variables?: Variable[]);
11
11
  get type(): Type;
12
- compile(type: Type): Node;
12
+ satisfies(type: Type): boolean;
13
+ compile(type?: Type): Node;
13
14
  evaluate(frame: CallFrame): Value;
14
15
  toString(ident?: number): string;
15
16
  }
@@ -1,4 +1,4 @@
1
- import { Node } from "../Node.js";
1
+ 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";
@@ -6,25 +6,32 @@ import { CallFrame } from "../CallFrame.js";
6
6
  export class BlockNode extends Node {
7
7
  _subnodes;
8
8
  _variables;
9
- constructor(span, _subnodes, _variables = []) {
9
+ constructor(span, subnodes, variables = []) {
10
10
  super(span);
11
- this._subnodes = _subnodes;
12
- this._variables = _variables;
11
+ this._subnodes = subnodes;
12
+ this._variables = variables;
13
13
  }
14
14
  get type() {
15
- return this._subnodes.length ? this._subnodes[this._subnodes.length - 1].type : Type.Void;
15
+ if (this._subnodes.some((node) => node.type.isNever)) {
16
+ return Type.Never;
17
+ }
18
+ return this._subnodes.length ? this._subnodes[this._subnodes.length - 1].type : super.type;
19
+ }
20
+ satisfies(type) {
21
+ return this.type.isNever || (this._subnodes.at(-1)?.satisfies(type) ?? super.satisfies(type));
16
22
  }
17
- compile(type) {
23
+ compile(type = Type.Any) {
18
24
  if (this._subnodes.length === 0) {
19
- this.reduceType(type);
20
- return new ConstantNode(this, Constant.Null);
25
+ return new ConstantNode(this, Constant.Null).compile(type);
21
26
  }
22
27
  let constant = true;
28
+ let never = false;
23
29
  for (let i = 0, last = this._subnodes.length - 1; i < this._subnodes.length; ++i) {
24
- this._subnodes[i] = this._subnodes[i].compile(i < last ? Type.Unknown : type);
25
- constant &&= this._subnodes[i].constant;
30
+ const node = this._subnodes[i] = this._subnodes[i].compile(i < last || never ? Type.Any : type);
31
+ never ||= node.type.isNever;
32
+ constant &&= node.constant;
26
33
  }
27
- if (constant) {
34
+ if (constant && !this.type.intersects(Type.Reference)) {
28
35
  return new ConstantNode(this, new Constant(this.evaluate(new CallFrame()), this.type));
29
36
  }
30
37
  if (this._subnodes.length === 1 && this._variables.length === 0) {
@@ -1,15 +1,17 @@
1
- import { Node } from "../Node.js";
2
- import { ScriptSpan } from "../ScriptSpan.js";
1
+ import { Node } from "./Node.js";
2
+ import { InvocationNode } from "./InvocationNode.js";
3
+ import { ParserSpan } from "../ParserSpan.js";
3
4
  import { Value } from "../Value.js";
4
5
  import { Type } from "../Type.js";
5
6
  import { CallFrame } from "../CallFrame.js";
6
- export declare class CallNode extends Node {
7
+ export declare class CallNode extends InvocationNode {
7
8
  protected _fnode: Node;
8
9
  protected _subnodes: Node[];
10
+ protected readonly _optional: boolean;
9
11
  protected _type: Type;
10
- constructor(span: ScriptSpan, _fnode: Node, _subnodes: Node[]);
12
+ constructor(span: ParserSpan, fnode: Node, subnodes: Node[], optional?: boolean);
11
13
  get type(): Type;
12
- compile(type: Type): Node;
14
+ compile(type?: Type): Node;
13
15
  evaluate(frame: CallFrame): Value;
14
16
  toString(ident?: number): string;
15
17
  }
@@ -1,56 +1,39 @@
1
- import { Node } from "../Node.js";
1
+ import { InvocationNode } from "./InvocationNode.js";
2
2
  import { Constant } from "../Constant.js";
3
3
  import { ConstantNode } from "./ConstantNode.js";
4
- import { VariadicNode } from "./VariadicNode.js";
5
- import { normalize } from "../Value.js";
6
4
  import { Type } from "../Type.js";
7
5
  import { CallFrame } from "../CallFrame.js";
8
- export class CallNode extends Node {
6
+ import { funcCoalesce } from "../native/Unknown.js";
7
+ import { funcAnd, funcOr } from "../native/Boolean.js";
8
+ export class CallNode extends InvocationNode {
9
9
  _fnode;
10
10
  _subnodes;
11
+ _optional;
11
12
  _type;
12
- constructor(span, _fnode, _subnodes) {
13
+ constructor(span, fnode, subnodes, optional = false) {
13
14
  super(span);
14
- this._fnode = _fnode;
15
- this._subnodes = _subnodes;
16
- const functionAtoms = this._fnode.type.functionAtoms(Type.Unknown, this._subnodes.length);
17
- if (!functionAtoms.length) {
18
- this.throwError(`value of type ${this._fnode.type} cannot be called with ${this._subnodes.length} argument(s)`);
15
+ this._fnode = fnode;
16
+ this._subnodes = subnodes;
17
+ this._optional = optional;
18
+ const argc = subnodes.length;
19
+ const signatures = fnode.type.functionAtoms().flatMap((atom) => atom.signatures(argc));
20
+ if (!signatures.length) {
21
+ this.throwError(`value of type ${fnode.type} cannot be called with ${argc} argument(s)`);
19
22
  }
20
- this._type = Type.union(...functionAtoms.map((i) => i.retType));
23
+ const retType = Type.union(...signatures.map((signature) => signature.retType));
24
+ this._type = optional ? retType.toOptional() : retType;
21
25
  }
22
26
  get type() {
23
27
  return this._type;
24
28
  }
25
- compile(type) {
26
- this._fnode = this._fnode.compile(this._fnode.type);
27
- const functionAtoms_ = this._fnode.type.functionAtoms(type, this._subnodes.length);
28
- let functionAtoms = functionAtoms_.filter((atom) => !atom.isVariadic && this._subnodes.every((node, index) => node.type.intersect(atom.paramType(index) ?? Type.Unknown)));
29
- if (!functionAtoms.length) {
30
- functionAtoms = functionAtoms_.filter((atom) => atom.isVariadic && this._subnodes.every((node, index) => node.type.intersect(atom.paramType(index) ?? Type.Unknown)));
31
- }
32
- if (!functionAtoms.length) {
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
- }
35
- this._type = Type.union(...functionAtoms.map((i) => i.retType));
36
- const variadicArity = functionAtoms[0].isVariadic
37
- ? Math.max(...functionAtoms.map((atom) => atom.arity))
38
- : 0;
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
- }
44
- let constant = this._fnode.constant;
45
- for (let i = 0; i < this._subnodes.length; ++i) {
46
- const argTypes = functionAtoms.map((a) => a.argType(i));
47
- const argType = variadicArity > 0 && i === variadicArity - 1
48
- ? Type.arrayType([Type.union(...functionAtoms.map((a) => a.paramType(i) ?? Type.Unknown))])
49
- : Type.union(...argTypes.filter((t) => t != null));
50
- this._subnodes[i] = this._subnodes[i].compile(argType);
51
- constant &&= this._subnodes[i].constant;
52
- }
53
- if (constant && !this.type.intersect(Type.Reference)) {
29
+ compile(type = Type.Any) {
30
+ this._fnode = this._fnode.compile(Type.Function);
31
+ const functionType = this._fnode.type;
32
+ this._type = this.compileArguments(this._optional ? functionType.optionalFirstParameter() : functionType, this._subnodes);
33
+ this._subnodes.find((arg) => arg.type.isNever)?.throwError("call argument must be a value");
34
+ super.compile(type);
35
+ if (this._fnode.constant && !this._fnode.type.isVolatileFunction &&
36
+ this._subnodes.every((node) => node.constant) && !this.type.intersects(Type.Reference)) {
54
37
  return new ConstantNode(this, new Constant(this.evaluate(new CallFrame()), this.type));
55
38
  }
56
39
  return this;
@@ -60,7 +43,23 @@ export class CallNode extends Node {
60
43
  if (typeof func !== "function") {
61
44
  this.throwError(`function expected not ${Type.of(func)}`);
62
45
  }
63
- return normalize(func(...this._subnodes.map((node) => node.evaluate(frame))));
46
+ const args = [];
47
+ for (let i = 0; i < this._subnodes.length; ++i) {
48
+ const arg = this._subnodes[i].evaluate(frame);
49
+ if (i === 0) {
50
+ if (this._optional && arg == null) {
51
+ return undefined;
52
+ }
53
+ if (func === funcCoalesce.value && arg != null) {
54
+ return arg;
55
+ }
56
+ }
57
+ if (func === funcAnd.value && arg === false || func === funcOr.value && arg === true) {
58
+ return arg;
59
+ }
60
+ args.push(arg);
61
+ }
62
+ return frame.define(func(...args), this.type);
64
63
  }
65
64
  toString(ident = 0) {
66
65
  const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join("\n");
@@ -1,15 +1,14 @@
1
- import { Node } from "../Node.js";
2
- import { ScriptSpan } from "../ScriptSpan.js";
1
+ import { Node } from "./Node.js";
2
+ import { ParserSpan } from "../ParserSpan.js";
3
3
  import { Type } from "../Type.js";
4
4
  import { Value } from "../Value.js";
5
5
  import { CallFrame } from "../CallFrame.js";
6
6
  export declare class CastNode extends Node {
7
7
  protected _subnode: Node;
8
- protected readonly _target: Type;
9
- protected _type: Type;
10
- constructor(span: ScriptSpan, _subnode: Node, _target: Type);
8
+ protected readonly _type: Type;
9
+ constructor(span: ParserSpan, subnode: Node, type: Type);
11
10
  get type(): Type;
12
- compile(type: Type): Node;
11
+ compile(type?: Type): Node;
13
12
  evaluate(frame: CallFrame): Value;
14
13
  get constant(): boolean;
15
14
  toString(ident?: number): string;
@@ -1,31 +1,30 @@
1
- import { Node } from "../Node.js";
1
+ import { Node } from "./Node.js";
2
2
  import { Type } from "../Type.js";
3
3
  export class CastNode extends Node {
4
4
  _subnode;
5
- _target;
6
5
  _type;
7
- constructor(span, _subnode, _target) {
6
+ constructor(span, subnode, type) {
8
7
  super(span);
9
- this._subnode = _subnode;
10
- this._target = _target;
11
- this._type = _target;
8
+ this._subnode = subnode;
9
+ this._type = type;
12
10
  }
13
11
  get type() {
14
- return this._type;
12
+ return this._subnode.type.isNever ? Type.Never : this._type;
15
13
  }
16
- compile(type) {
17
- this._subnode = this._subnode.compile(Type.Unknown);
18
- if (!this._subnode.type.intersect(this._target)) {
19
- this.throwError(`cannot cast type ${this._subnode.type} to ${this._target}`);
14
+ compile(type = Type.Any) {
15
+ this._subnode = this._subnode.compile(Type.Any);
16
+ if (this._subnode.type.isNever) {
17
+ this._subnode.throwError("cast operand must produce a value");
20
18
  }
21
- this._type = this._target;
22
- this._type = this.reduceType(type);
23
- return this;
19
+ if (!this._subnode.type.intersects(this._type)) {
20
+ this.throwError(`cannot cast type ${this._subnode.type} to type ${this._type}`);
21
+ }
22
+ return super.compile(type);
24
23
  }
25
24
  evaluate(frame) {
26
25
  const value = this._subnode.evaluate(frame);
27
- if (!this._type.acceptValue(value)) {
28
- this.throwError(`cannot cast value of type ${Type.of(value)} to ${this._type}`);
26
+ if (!this._type.acceptsValue(value)) {
27
+ this.throwError(`cannot cast value of type ${Type.of(value)} to type ${this._type}`);
29
28
  }
30
29
  return value;
31
30
  }
@@ -33,6 +32,6 @@ export class CastNode extends Node {
33
32
  return this._subnode.constant;
34
33
  }
35
34
  toString(ident = 0) {
36
- return `${super.toString(ident)} cast node target <${this._target}>:\n${this._subnode.toString(ident + 1)}`;
35
+ return `${super.toString(ident)} cast node target type ${this._type}:\n${this._subnode.toString(ident + 1)}`;
37
36
  }
38
37
  }
@@ -1,16 +1,14 @@
1
- import { Node } from "../Node.js";
2
- import { ScriptSpan } from "../ScriptSpan.js";
1
+ import { Node } from "./Node.js";
2
+ import { ParserSpan } from "../ParserSpan.js";
3
3
  import { Constant } from "../Constant.js";
4
4
  import { Value } from "../Value.js";
5
5
  import { Type } from "../Type.js";
6
6
  import { CallFrame } from "../CallFrame.js";
7
7
  export declare class ConstantNode extends Node {
8
8
  protected readonly _constant: Constant;
9
- protected _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);
9
+ constructor(span: ParserSpan, constant: Constant);
10
+ get value(): Value;
12
11
  get type(): Type;
13
- compile(type: Type): Node;
14
12
  evaluate(frame: CallFrame): Value;
15
13
  get constant(): boolean;
16
14
  toString(ident?: number): string;
@@ -1,31 +1,23 @@
1
- import { Node } from "../Node.js";
1
+ import { Node } from "./Node.js";
2
2
  export class ConstantNode extends Node {
3
3
  _constant;
4
- _subnode;
5
- _factory;
6
- constructor(span, _constant, _subnode, _factory) {
4
+ constructor(span, constant) {
7
5
  super(span);
8
- this._constant = _constant;
9
- this._subnode = _subnode;
10
- this._factory = _factory;
6
+ this._constant = constant;
7
+ }
8
+ get value() {
9
+ return this._constant.value;
11
10
  }
12
11
  get type() {
13
12
  return this._constant.type;
14
13
  }
15
- compile(type) {
16
- this._constant.type = this.reduceType(type);
17
- this._subnode = this._subnode?.compile(this._constant.type.functionAtomRetType(type));
18
- return this;
19
- }
20
14
  evaluate(frame) {
21
- return this._factory ? this._factory(frame) : this._constant.value;
15
+ return frame.define(this._constant.value, this.type);
22
16
  }
23
17
  get constant() {
24
- return !this._factory && !this._subnode && this._constant.deterministic;
18
+ return !this._constant.type.isVolatileFunction;
25
19
  }
26
20
  toString(ident = 0) {
27
- return this._subnode
28
- ? `${super.toString(ident)} constant node subnode:\n${this._subnode.toString(ident + 1)}`
29
- : `${super.toString(ident)} constant node`;
21
+ return `${super.toString(ident)} constant node`;
30
22
  }
31
23
  }
@@ -0,0 +1,19 @@
1
+ import { Node } from "./Node.js";
2
+ import { ParserSpan } from "../ParserSpan.js";
3
+ import { Value } from "../Value.js";
4
+ import { Type } from "../Type.js";
5
+ import { CallFrame } from "../CallFrame.js";
6
+ export declare class FinalNode extends Node {
7
+ protected _level: "result" | "return";
8
+ protected _subnode: Node;
9
+ protected readonly _type: Type;
10
+ protected _value: Value;
11
+ constructor(span: ParserSpan, level: "result" | "return", subnode: Node, type: Type);
12
+ get level(): "result" | "return";
13
+ get value(): Value;
14
+ get valueType(): Type;
15
+ get type(): Type;
16
+ compile(): Node;
17
+ evaluate(frame: CallFrame): Value;
18
+ toString(ident?: number): string;
19
+ }
@@ -0,0 +1,37 @@
1
+ import { Node } from "./Node.js";
2
+ import { Type } from "../Type.js";
3
+ export class FinalNode extends Node {
4
+ _level;
5
+ _subnode;
6
+ _type;
7
+ _value;
8
+ constructor(span, level, subnode, type) {
9
+ super(span);
10
+ this._level = level;
11
+ this._subnode = subnode;
12
+ this._type = type;
13
+ }
14
+ get level() {
15
+ return this._level;
16
+ }
17
+ get value() {
18
+ return this._value;
19
+ }
20
+ get valueType() {
21
+ return this._subnode.type;
22
+ }
23
+ get type() {
24
+ return Type.Never;
25
+ }
26
+ compile() {
27
+ this._subnode = this._subnode.compile(this._type);
28
+ return this;
29
+ }
30
+ evaluate(frame) {
31
+ this._value = this._subnode.evaluate(frame);
32
+ throw this;
33
+ }
34
+ toString(ident = 0) {
35
+ return `${super.toString(ident)} ${this._level} node subnode:\n${this._subnode.toString(ident + 1)}`;
36
+ }
37
+ }
@@ -0,0 +1,17 @@
1
+ import { Node } from "./Node.js";
2
+ import { ParserSpan } from "../ParserSpan.js";
3
+ import { Variable } from "../Variable.js";
4
+ import { Type } from "../Type.js";
5
+ import { Value } from "../Value.js";
6
+ import { CallFrame } from "../CallFrame.js";
7
+ export declare class FunctionNode extends Node {
8
+ protected _subnode: Node;
9
+ protected readonly _variables: readonly Variable[];
10
+ protected readonly _arguments: readonly Variable[];
11
+ protected _type: Type;
12
+ constructor(span: ParserSpan, subnode: Node, variables: readonly Variable[], args: readonly Variable[], type: Type);
13
+ get type(): Type;
14
+ compile(type?: Type): Node;
15
+ evaluate(frame: CallFrame): Value;
16
+ toString(ident?: number): string;
17
+ }
@@ -0,0 +1,47 @@
1
+ import { Node } from "./Node.js";
2
+ import { Type } from "../Type.js";
3
+ import { FinalNode } from "./FinalNode.js";
4
+ export class FunctionNode extends Node {
5
+ _subnode;
6
+ _variables;
7
+ _arguments;
8
+ _type;
9
+ constructor(span, subnode, variables, args, type) {
10
+ super(span);
11
+ this._subnode = subnode;
12
+ this._arguments = args;
13
+ this._variables = variables;
14
+ this._type = type;
15
+ }
16
+ get type() {
17
+ return this._type;
18
+ }
19
+ compile(type = Type.Any) {
20
+ const retType = this._type.functionAtomRetType() ?? Type.Any;
21
+ this._subnode = this._subnode.compile(retType);
22
+ return super.compile(type);
23
+ }
24
+ evaluate(frame) {
25
+ const index = this._type.isVariadicFunction ? this._arguments.length - 1 : -1;
26
+ return (...values) => {
27
+ frame.timer();
28
+ const subframe = frame.subframe(this._variables);
29
+ try {
30
+ this._arguments.forEach((arg, ix) => {
31
+ subframe.set(arg, ix === index ? values.slice(ix) : values[ix]);
32
+ });
33
+ return this._subnode.evaluate(subframe);
34
+ }
35
+ catch (e) {
36
+ if (e instanceof FinalNode && e.level === "return") {
37
+ return e.value;
38
+ }
39
+ throw e;
40
+ }
41
+ };
42
+ }
43
+ toString(ident = 0) {
44
+ return `${super.toString(ident)} function node subnode:\n`
45
+ + this._subnode.toString(ident + 1);
46
+ }
47
+ }
@@ -0,0 +1,5 @@
1
+ import { Node } from "./Node.js";
2
+ import { Type } from "../Type.js";
3
+ export declare abstract class InvocationNode extends Node {
4
+ protected compileArguments(functionType: Type, args: Node[], compiledCount?: number): Type;
5
+ }
@@ -0,0 +1,30 @@
1
+ import { Node } from "./Node.js";
2
+ import { Type } from "../Type.js";
3
+ export class InvocationNode extends Node {
4
+ compileArguments(functionType, args, compiledCount = 0) {
5
+ const argc = args.length;
6
+ const candidateGroups = functionType.functionAtoms().map((atom) => atom.signatures(argc));
7
+ const candidates = candidateGroups.flat();
8
+ let hints = candidates.filter((signature) => args.every((node, index) => node.type.isNever || node.type.intersects(signature.paramType(index) ?? Type.Any)));
9
+ for (let i = compiledCount; i < argc; ++i) {
10
+ const parameters = (hints.length ? hints : candidates).map((signature) => signature.paramType(i) ?? Type.Any);
11
+ const union = Type.union(...parameters);
12
+ // An inferred union must fit a candidate parameter to stay a concrete type.
13
+ const hint = parameters.some((type) => type.accepts(union)) ? union : Type.Any;
14
+ const node = args[i] = args[i].compile(hint);
15
+ hints = hints.filter((signature) => node.type.isNever || node.type.intersects(signature.paramType(i) ?? Type.Any));
16
+ }
17
+ const compiledArgTypes = args.map((node) => node.type);
18
+ if (functionType.isNever || functionType.isFunction && compiledArgTypes.some((type) => type.isNever)) {
19
+ return Type.Never;
20
+ }
21
+ const groups = candidateGroups.map((group) => group.filter((signature) => args.every((node, i) => node.satisfies(signature.paramType(i) ?? Type.Any)) &&
22
+ signature.acceptsVariadic(compiledArgTypes)));
23
+ if (!functionType.isFunction || groups.some((group) => !group.length)) {
24
+ this.throwError(`value of type ${functionType} cannot be called with ${argc} argument(s)`);
25
+ }
26
+ const dynamic = compiledArgTypes.some((type) => type.isAny);
27
+ const signatures = groups.flatMap((group) => dynamic ? group : group.slice(0, 1));
28
+ return Type.union(...signatures.map((signature) => signature.retType));
29
+ }
30
+ }
@@ -1,16 +1,14 @@
1
- import { Node } from "../Node.js";
2
- import { ScriptSpan } from "../ScriptSpan.js";
1
+ import { Node } from "./Node.js";
2
+ import { ParserSpan } from "../ParserSpan.js";
3
3
  import { Value } from "../Value.js";
4
4
  import { Type } from "../Type.js";
5
- import { Jump } from "../Jump.js";
6
5
  import { CallFrame } from "../CallFrame.js";
7
6
  export declare class JumpNode extends Node {
8
- protected _jump: Jump;
9
- protected _subnode?: Node | undefined;
10
- protected _type?: Type | undefined;
11
- constructor(span: ScriptSpan, _jump: Jump, _subnode?: Node | undefined, _type?: Type | undefined);
7
+ protected _action: "exit" | "next";
8
+ constructor(span: ParserSpan, action: "exit" | "next");
9
+ get action(): "next" | "exit";
12
10
  get type(): Type;
13
- compile(type: Type): Node;
14
- evaluate(frame: CallFrame): Value;
11
+ compile(): Node;
12
+ evaluate(_: CallFrame): Value;
15
13
  toString(ident?: number): string;
16
14
  }
@@ -1,31 +1,24 @@
1
- import { Node } from "../Node.js";
1
+ import { Node } from "./Node.js";
2
2
  import { Type } from "../Type.js";
3
- import { JumpException } from "../JumpException.js";
4
3
  export class JumpNode extends Node {
5
- _jump;
6
- _subnode;
7
- _type;
8
- constructor(span, _jump, _subnode, _type) {
4
+ _action;
5
+ constructor(span, action) {
9
6
  super(span);
10
- this._jump = _jump;
11
- this._subnode = _subnode;
12
- this._type = _type;
7
+ this._action = action;
8
+ }
9
+ get action() {
10
+ return this._action;
13
11
  }
14
12
  get type() {
15
- return this._type ?? this._subnode?.type ?? Type.Void;
13
+ return Type.Never;
16
14
  }
17
- compile(type) {
18
- this._subnode = this._subnode?.compile(this._jump === "exit" || this._jump === "return"
19
- ? this._type ?? Type.Unknown
20
- : type);
15
+ compile() {
21
16
  return this;
22
17
  }
23
- evaluate(frame) {
24
- throw new JumpException(this._jump, this._subnode?.evaluate(frame));
18
+ evaluate(_) {
19
+ throw this;
25
20
  }
26
21
  toString(ident = 0) {
27
- return this._subnode
28
- ? `${super.toString(ident)} ${this._jump} node subnode:\n${this._subnode.toString(ident + 1)}`
29
- : `${super.toString(ident)} ${this._jump} node`;
22
+ return `${super.toString(ident)} ${this._action} node`;
30
23
  }
31
24
  }