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,193 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AccessNode = void 0;
4
+ const AssignableNode_js_1 = require("./AssignableNode.js");
5
+ const ConstantNode_js_1 = require("./ConstantNode.js");
6
+ const Constant_js_1 = require("../Constant.js");
7
+ const Type_js_1 = require("../Type.js");
8
+ const CallFrame_js_1 = require("../CallFrame.js");
9
+ const Composite_js_1 = require("../native/Composite.js");
10
+ class AccessNode extends AssignableNode_js_1.AssignableNode {
11
+ _subnode;
12
+ _inode;
13
+ _operator;
14
+ _type;
15
+ constructor(span, subnode, inode, operator) {
16
+ super(span);
17
+ this._subnode = subnode;
18
+ this._inode = inode;
19
+ this._operator = operator;
20
+ }
21
+ get type() {
22
+ return this._type ?? Type_js_1.Type.Any;
23
+ }
24
+ get assignable() {
25
+ return this._subnode instanceof AssignableNode_js_1.AssignableNode ? this._subnode.assignable : !this._subnode.constant;
26
+ }
27
+ get optionalAssignment() {
28
+ return this._operator === Composite_js_1.funcOptionalAt && this._subnode.type.isOptional ||
29
+ this._subnode instanceof AccessNode && this._subnode.optionalAssignment;
30
+ }
31
+ get constant() {
32
+ return this._subnode.constant && this._inode.constant;
33
+ }
34
+ compile(type = Type_js_1.Type.Any) {
35
+ this._compileAccess();
36
+ if (!this._subnode.type.isNever) {
37
+ this._type = this.type.toOptional();
38
+ }
39
+ super.compile(type);
40
+ if (this.constant && !this.type.intersects(Type_js_1.Type.Reference)) {
41
+ return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(new CallFrame_js_1.CallFrame()), this.type));
42
+ }
43
+ return this;
44
+ }
45
+ compileAssignment(type) {
46
+ this._compileAccess();
47
+ const compiledType = this.reduceAssignmentType(type);
48
+ if (!compiledType) {
49
+ this._throwTypeError(type);
50
+ }
51
+ this._type = compiledType;
52
+ return this;
53
+ }
54
+ evaluate(frame) {
55
+ const func = this._operator.value;
56
+ const composite = this._subnode.evaluate(frame);
57
+ if (func === Composite_js_1.funcOptionalAt.value && composite == null) {
58
+ return undefined;
59
+ }
60
+ const compositeType = this._operator.type.functionAtomArgType(0) ?? Type_js_1.Type.Any;
61
+ if (!compositeType.acceptsValue(composite)) {
62
+ this.throwError("indexed value must be an array or object");
63
+ }
64
+ const value = func(composite, this._inode.evaluate(frame));
65
+ if (value != null && !this.type.acceptsValue(value)) {
66
+ this.throwError(`indexed value mismatch with expected type ${this._type}`);
67
+ }
68
+ return value;
69
+ }
70
+ resolver(frame) {
71
+ const target = this._subnode instanceof AccessNode ? this._subnode : undefined;
72
+ const resolver = target?.resolver(frame);
73
+ if (target && !resolver) {
74
+ return undefined;
75
+ }
76
+ const composite = resolver ? resolver.get() : this._subnode.evaluate(frame);
77
+ if (composite == null) {
78
+ if (this._operator === Composite_js_1.funcOptionalAt) {
79
+ return undefined;
80
+ }
81
+ this.throwError("cannot assign through null");
82
+ }
83
+ const compositeType = resolver?.type ?? this._subnode.type;
84
+ const key = this._inode.evaluate(frame);
85
+ if (Array.isArray(composite)) {
86
+ if (typeof key !== "bigint") {
87
+ this.throwError("array index must be an integer");
88
+ }
89
+ const num = Number(key);
90
+ const index = num < 0 ? composite.length + num : num;
91
+ const type = compositeType.arrayAtomValType(index);
92
+ return {
93
+ type,
94
+ get: () => index >= 0 && index < composite.length ? composite[index] : undefined,
95
+ set: (value) => {
96
+ if (index < 0 || index >= composite.length) {
97
+ this.throwError("array index is out of range");
98
+ }
99
+ if (!type.acceptsValue(value)) {
100
+ this.throwError(`indexed value mismatch with expected type ${type}`);
101
+ }
102
+ let arr;
103
+ for (const constraint of new Set([compositeType, ...frame.definitions(composite)])) {
104
+ if (constraint.isAny || constraint.equals(Type_js_1.Type.Array)) {
105
+ continue;
106
+ }
107
+ if (!arr) {
108
+ arr = composite.slice();
109
+ arr[index] = value;
110
+ }
111
+ if (!constraint.acceptsValue(arr)) {
112
+ this.throwError(`indexed value mismatch with expected type ${constraint}`);
113
+ }
114
+ }
115
+ composite[index] = value;
116
+ return value;
117
+ },
118
+ };
119
+ }
120
+ if (!Type_js_1.Type.Object.acceptsValue(composite)) {
121
+ this.throwError("indexed value must be an array or object");
122
+ }
123
+ if (typeof key !== "string") {
124
+ this.throwError("object key must be a string");
125
+ }
126
+ const object = composite;
127
+ const type = compositeType.objectAtomValType(key);
128
+ return {
129
+ type,
130
+ get: () => Object.hasOwn(object, key) ? object[key] : undefined,
131
+ set: (value) => {
132
+ if (!type.acceptsValue(value)) {
133
+ this.throwError(`indexed value mismatch with expected type ${type}`);
134
+ }
135
+ let obj;
136
+ for (const constraint of new Set([compositeType, ...frame.definitions(object)])) {
137
+ if (constraint.isAny || constraint.equals(Type_js_1.Type.Object)) {
138
+ continue;
139
+ }
140
+ if (!obj) {
141
+ obj = Object.assign(Object.create(null), object);
142
+ obj[key] = value;
143
+ }
144
+ if (!constraint.acceptsValue(obj)) {
145
+ this.throwError(`indexed value mismatch with expected type ${constraint}`);
146
+ }
147
+ }
148
+ object[key] = value;
149
+ return value;
150
+ },
151
+ };
152
+ }
153
+ toString(indent = 0) {
154
+ return `${super.toString(indent)} index node subnode:\n${this._subnode.toString(indent + 1)}\n`
155
+ + `${super.toString(indent)} index node inode:\n${this._inode.toString(indent + 1)}`;
156
+ }
157
+ _compileAccess() {
158
+ if (this._type) {
159
+ return;
160
+ }
161
+ const receiverType = this._operator.type.functionAtomArgType(0) ?? Type_js_1.Type.Any;
162
+ this._subnode = this._subnode.compile(receiverType);
163
+ const compositeType = this._subnode.type;
164
+ const mandatoryType = compositeType.toMandatory();
165
+ let indexType = this._operator.type.functionAtomArgType(1) ?? Type_js_1.Type.Any;
166
+ if (!mandatoryType.isAny) {
167
+ if (mandatoryType.isArray) {
168
+ indexType = Type_js_1.Type.Integer;
169
+ }
170
+ else if (mandatoryType.isObject) {
171
+ indexType = Type_js_1.Type.String;
172
+ }
173
+ }
174
+ this._inode = this._inode.compile(indexType);
175
+ if (this._inode.type.isNever) {
176
+ this._inode.throwError("index must produce a value");
177
+ }
178
+ const index = this._inode instanceof ConstantNode_js_1.ConstantNode ? this._inode.value : undefined;
179
+ if (compositeType.isNever) {
180
+ this._type = Type_js_1.Type.Never;
181
+ }
182
+ else if (this._inode.type.isInteger) {
183
+ this._type = compositeType.arrayAtomValType(typeof index === "bigint" ? Number(index) : undefined);
184
+ }
185
+ else if (this._inode.type.isString) {
186
+ this._type = compositeType.objectAtomValType(typeof index === "string" ? index : undefined);
187
+ }
188
+ else {
189
+ this._type = Type_js_1.Type.Any;
190
+ }
191
+ }
192
+ }
193
+ exports.AccessNode = AccessNode;
@@ -1,14 +1,17 @@
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";
6
6
  export declare class ArrayNode extends Node {
7
7
  protected _subnodes: Node[];
8
+ protected _uniform: boolean;
8
9
  protected _type: Type;
9
- constructor(span: ScriptSpan, _subnodes: Node[]);
10
+ constructor(span: ParserSpan, subnodes: Node[], uniform?: boolean);
10
11
  get type(): Type;
11
- compile(type: Type): Node;
12
+ satisfies(type: Type): boolean;
13
+ compile(type?: Type): Node;
12
14
  evaluate(frame: CallFrame): Value;
15
+ get constant(): boolean;
13
16
  toString(ident?: number): string;
14
17
  }
@@ -1,34 +1,45 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ArrayNode = void 0;
4
- const Node_js_1 = require("../Node.js");
4
+ const Node_js_1 = require("./Node.js");
5
5
  const Type_js_1 = require("../Type.js");
6
6
  class ArrayNode extends Node_js_1.Node {
7
7
  _subnodes;
8
+ _uniform;
8
9
  _type;
9
- constructor(span, _subnodes) {
10
+ constructor(span, subnodes, uniform = false) {
10
11
  super(span);
11
- this._subnodes = _subnodes;
12
- this._type = Type_js_1.Type.arrayType(_subnodes.map((i) => i.type));
12
+ this._subnodes = subnodes;
13
+ this._uniform = uniform;
14
+ const types = subnodes.map((i) => i.type);
15
+ this._type = Type_js_1.Type.arrayType(uniform ? Type_js_1.Type.union(...types) : types);
13
16
  }
14
17
  get type() {
15
18
  return this._type;
16
19
  }
17
- compile(type) {
20
+ satisfies(type) {
21
+ return this.type.isNever || super.satisfies(type) &&
22
+ this._subnodes.every((node, index) => node.satisfies(type.arrayAtomValType(index)));
23
+ }
24
+ compile(type = Type_js_1.Type.Any) {
25
+ const valTypes = [];
26
+ let never = false;
18
27
  for (let i = 0; i < this._subnodes.length; ++i) {
19
- const valType = type.arrayAtomValType(i);
20
- this._subnodes[i] = this._subnodes[i].compile(valType);
28
+ this._subnodes[i] = this._subnodes[i].compile(type.arrayAtomValType(i));
29
+ never ||= this._subnodes[i].type.isNever;
30
+ valTypes.push(this._subnodes[i].type);
21
31
  }
22
- this._type = Type_js_1.Type.arrayType(this._subnodes.map((node) => node.type));
23
- this._type = this.reduceType(type);
24
- return this;
32
+ this._type = never
33
+ ? Type_js_1.Type.Never
34
+ : Type_js_1.Type.arrayType(this._uniform ? Type_js_1.Type.union(...valTypes) : valTypes);
35
+ return super.compile(type);
25
36
  }
26
37
  evaluate(frame) {
27
- const result = [];
28
- for (const subnode of this._subnodes) {
29
- result.push(subnode.evaluate(frame));
30
- }
31
- return result;
38
+ const result = this._subnodes.map((node) => node.evaluate(frame));
39
+ return frame.define(result, this.type);
40
+ }
41
+ get constant() {
42
+ return this._subnodes.every((node) => node.constant);
32
43
  }
33
44
  toString(ident = 0) {
34
45
  const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join("\n");
@@ -0,0 +1,16 @@
1
+ import { Node } from "./Node.js";
2
+ import { Type } from "../Type.js";
3
+ import { Value } from "../Value.js";
4
+ import { CallFrame } from "../CallFrame.js";
5
+ export interface AssignmentResolver {
6
+ readonly type?: Type;
7
+ set: (value: Value) => Value;
8
+ get: () => Value;
9
+ }
10
+ export declare abstract class AssignableNode extends Node {
11
+ abstract get assignable(): boolean;
12
+ get optionalAssignment(): boolean;
13
+ abstract compileAssignment(type: Type): AssignableNode;
14
+ abstract resolver(frame: CallFrame): AssignmentResolver | undefined;
15
+ reduceAssignmentType(type: Type): Type | undefined;
16
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssignableNode = void 0;
4
+ const Node_js_1 = require("./Node.js");
5
+ class AssignableNode extends Node_js_1.Node {
6
+ get optionalAssignment() {
7
+ return false;
8
+ }
9
+ reduceAssignmentType(type) {
10
+ const targetType = this.type;
11
+ if (!targetType.accepts(type)) {
12
+ return undefined;
13
+ }
14
+ const compiledType = targetType.reduce(type);
15
+ return compiledType?.accepts(type) ? compiledType : targetType;
16
+ }
17
+ }
18
+ exports.AssignableNode = AssignableNode;
@@ -0,0 +1,20 @@
1
+ import { Node } from "./Node.js";
2
+ import { InvocationNode } from "./InvocationNode.js";
3
+ import { AssignableNode } from "./AssignableNode.js";
4
+ import { Constant } from "../Constant.js";
5
+ import { ParserSpan } from "../ParserSpan.js";
6
+ import { Value } from "../Value.js";
7
+ import { Type } from "../Type.js";
8
+ import { CallFrame } from "../CallFrame.js";
9
+ export declare class AssignmentNode extends InvocationNode {
10
+ protected _anode: AssignableNode;
11
+ protected _subnode: Node;
12
+ protected readonly _operator?: Constant;
13
+ private _type?;
14
+ constructor(span: ParserSpan, anode: AssignableNode, subnode: Node, operator?: Constant);
15
+ get type(): Type;
16
+ satisfies(type: Type): boolean;
17
+ compile(type?: Type): Node;
18
+ evaluate(frame: CallFrame): Value;
19
+ toString(ident?: number): string;
20
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssignmentNode = void 0;
4
+ const InvocationNode_js_1 = require("./InvocationNode.js");
5
+ const Type_js_1 = require("../Type.js");
6
+ const Boolean_js_1 = require("../native/Boolean.js");
7
+ class AssignmentNode extends InvocationNode_js_1.InvocationNode {
8
+ _anode;
9
+ _subnode;
10
+ _operator;
11
+ _type;
12
+ constructor(span, anode, subnode, operator) {
13
+ super(span);
14
+ this._anode = anode;
15
+ this._subnode = subnode;
16
+ this._operator = operator;
17
+ }
18
+ get type() {
19
+ const valType = this._type ?? this._operator?.type.functionAtomRetType() ?? this._subnode.type;
20
+ const resultType = this._anode.reduceAssignmentType(valType) ?? this._anode.type;
21
+ return this._anode.optionalAssignment ? resultType.toOptional() : resultType;
22
+ }
23
+ satisfies(type) {
24
+ return super.satisfies(type) &&
25
+ (this._operator !== undefined || !type.accepts(this._subnode.type) || this._subnode.satisfies(type));
26
+ }
27
+ compile(type = Type_js_1.Type.Any) {
28
+ if (this._operator) {
29
+ // Resolve the target type before checking it as the operator's first operand.
30
+ this._anode = this._anode.compileAssignment(Type_js_1.Type.Any);
31
+ const args = [this._anode, this._subnode];
32
+ this._type = this.compileArguments(this._operator.type, args, 1);
33
+ this._subnode = args[1];
34
+ }
35
+ else {
36
+ this._subnode = this._subnode.compile(type);
37
+ }
38
+ this._anode = this._anode.compileAssignment(this._type ?? this._subnode.type);
39
+ return super.compile(type);
40
+ }
41
+ evaluate(frame) {
42
+ const resolver = this._anode.resolver(frame);
43
+ if (!resolver) {
44
+ return undefined;
45
+ }
46
+ if (!this._operator) {
47
+ return resolver.set(this._subnode.evaluate(frame));
48
+ }
49
+ const value = resolver.get();
50
+ if (resolver.type && !resolver.type.acceptsValue(value)) {
51
+ this._anode.throwError(`indexed value mismatch with expected type ${resolver.type}`);
52
+ }
53
+ const operation = this._operator.value;
54
+ if (operation === Boolean_js_1.funcAnd.value && value === false ||
55
+ operation === Boolean_js_1.funcOr.value && value === true) {
56
+ return value;
57
+ }
58
+ const result = operation(value, this._subnode.evaluate(frame));
59
+ return resolver.set(frame.define(result, this._type ?? Type_js_1.Type.Any));
60
+ }
61
+ toString(ident = 0) {
62
+ return `${super.toString(ident)} assignment node anode:\n${this._anode.toString(ident + 1)}\n`
63
+ + `${super.toString(ident)} assignment node subnode:\n${this._subnode.toString(ident + 1)}`;
64
+ }
65
+ }
66
+ exports.AssignmentNode = AssignmentNode;
@@ -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,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BlockNode = void 0;
4
- const Node_js_1 = require("../Node.js");
4
+ 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");
@@ -9,25 +9,32 @@ const CallFrame_js_1 = require("../CallFrame.js");
9
9
  class BlockNode extends Node_js_1.Node {
10
10
  _subnodes;
11
11
  _variables;
12
- constructor(span, _subnodes, _variables = []) {
12
+ constructor(span, subnodes, variables = []) {
13
13
  super(span);
14
- this._subnodes = _subnodes;
15
- this._variables = _variables;
14
+ this._subnodes = subnodes;
15
+ this._variables = variables;
16
16
  }
17
17
  get type() {
18
- return this._subnodes.length ? this._subnodes[this._subnodes.length - 1].type : Type_js_1.Type.Void;
18
+ if (this._subnodes.some((node) => node.type.isNever)) {
19
+ return Type_js_1.Type.Never;
20
+ }
21
+ return this._subnodes.length ? this._subnodes[this._subnodes.length - 1].type : super.type;
22
+ }
23
+ satisfies(type) {
24
+ return this.type.isNever || (this._subnodes.at(-1)?.satisfies(type) ?? super.satisfies(type));
19
25
  }
20
- compile(type) {
26
+ compile(type = Type_js_1.Type.Any) {
21
27
  if (this._subnodes.length === 0) {
22
- this.reduceType(type);
23
- return new ConstantNode_js_1.ConstantNode(this, Constant_js_1.Constant.Null);
28
+ return new ConstantNode_js_1.ConstantNode(this, Constant_js_1.Constant.Null).compile(type);
24
29
  }
25
30
  let constant = true;
31
+ let never = false;
26
32
  for (let i = 0, last = this._subnodes.length - 1; i < this._subnodes.length; ++i) {
27
- this._subnodes[i] = this._subnodes[i].compile(i < last ? Type_js_1.Type.Unknown : type);
28
- constant &&= this._subnodes[i].constant;
33
+ const node = this._subnodes[i] = this._subnodes[i].compile(i < last || never ? Type_js_1.Type.Any : type);
34
+ never ||= node.type.isNever;
35
+ constant &&= node.constant;
29
36
  }
30
- if (constant) {
37
+ if (constant && !this.type.intersects(Type_js_1.Type.Reference)) {
31
38
  return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(new CallFrame_js_1.CallFrame()), this.type));
32
39
  }
33
40
  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,59 +1,42 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CallNode = void 0;
4
- const Node_js_1 = require("../Node.js");
4
+ const InvocationNode_js_1 = require("./InvocationNode.js");
5
5
  const Constant_js_1 = require("../Constant.js");
6
6
  const ConstantNode_js_1 = require("./ConstantNode.js");
7
- const VariadicNode_js_1 = require("./VariadicNode.js");
8
- const Value_js_1 = require("../Value.js");
9
7
  const Type_js_1 = require("../Type.js");
10
8
  const CallFrame_js_1 = require("../CallFrame.js");
11
- class CallNode extends Node_js_1.Node {
9
+ const Unknown_js_1 = require("../native/Unknown.js");
10
+ const Boolean_js_1 = require("../native/Boolean.js");
11
+ class CallNode extends InvocationNode_js_1.InvocationNode {
12
12
  _fnode;
13
13
  _subnodes;
14
+ _optional;
14
15
  _type;
15
- constructor(span, _fnode, _subnodes) {
16
+ constructor(span, fnode, subnodes, optional = false) {
16
17
  super(span);
17
- this._fnode = _fnode;
18
- this._subnodes = _subnodes;
19
- const functionAtoms = this._fnode.type.functionAtoms(Type_js_1.Type.Unknown, this._subnodes.length);
20
- if (!functionAtoms.length) {
21
- this.throwError(`value of type ${this._fnode.type} cannot be called with ${this._subnodes.length} argument(s)`);
18
+ this._fnode = fnode;
19
+ this._subnodes = subnodes;
20
+ this._optional = optional;
21
+ const argc = subnodes.length;
22
+ const signatures = fnode.type.functionAtoms().flatMap((atom) => atom.signatures(argc));
23
+ if (!signatures.length) {
24
+ this.throwError(`value of type ${fnode.type} cannot be called with ${argc} argument(s)`);
22
25
  }
23
- this._type = Type_js_1.Type.union(...functionAtoms.map((i) => i.retType));
26
+ const retType = Type_js_1.Type.union(...signatures.map((signature) => signature.retType));
27
+ this._type = optional ? retType.toOptional() : retType;
24
28
  }
25
29
  get type() {
26
30
  return this._type;
27
31
  }
28
- compile(type) {
29
- this._fnode = this._fnode.compile(this._fnode.type);
30
- const functionAtoms_ = this._fnode.type.functionAtoms(type, this._subnodes.length);
31
- let functionAtoms = functionAtoms_.filter((atom) => !atom.isVariadic && this._subnodes.every((node, index) => node.type.intersect(atom.paramType(index) ?? Type_js_1.Type.Unknown)));
32
- if (!functionAtoms.length) {
33
- functionAtoms = functionAtoms_.filter((atom) => atom.isVariadic && this._subnodes.every((node, index) => node.type.intersect(atom.paramType(index) ?? Type_js_1.Type.Unknown)));
34
- }
35
- if (!functionAtoms.length) {
36
- this.throwError(`value of type ${this._fnode.type} cannot be called with ${this._subnodes.length} argument(s) and return a type compatible with ${type}`);
37
- }
38
- this._type = Type_js_1.Type.union(...functionAtoms.map((i) => i.retType));
39
- const variadicArity = functionAtoms[0].isVariadic
40
- ? Math.max(...functionAtoms.map((atom) => atom.arity))
41
- : 0;
42
- if (variadicArity > 0) {
43
- const span = this._subnodes[variadicArity - 1].start();
44
- const vnodes = this._subnodes.splice(variadicArity - 1);
45
- this._subnodes.push(new VariadicNode_js_1.VariadicNode(span.end(vnodes[vnodes.length - 1]), vnodes));
46
- }
47
- let constant = this._fnode.constant;
48
- for (let i = 0; i < this._subnodes.length; ++i) {
49
- const argTypes = functionAtoms.map((a) => a.argType(i));
50
- const argType = variadicArity > 0 && i === variadicArity - 1
51
- ? Type_js_1.Type.arrayType([Type_js_1.Type.union(...functionAtoms.map((a) => a.paramType(i) ?? Type_js_1.Type.Unknown))])
52
- : Type_js_1.Type.union(...argTypes.filter((t) => t != null));
53
- this._subnodes[i] = this._subnodes[i].compile(argType);
54
- constant &&= this._subnodes[i].constant;
55
- }
56
- if (constant && !this.type.intersect(Type_js_1.Type.Reference)) {
32
+ compile(type = Type_js_1.Type.Any) {
33
+ this._fnode = this._fnode.compile(Type_js_1.Type.Function);
34
+ const functionType = this._fnode.type;
35
+ this._type = this.compileArguments(this._optional ? functionType.optionalFirstParameter() : functionType, this._subnodes);
36
+ this._subnodes.find((arg) => arg.type.isNever)?.throwError("call argument must be a value");
37
+ super.compile(type);
38
+ if (this._fnode.constant && !this._fnode.type.isVolatileFunction &&
39
+ this._subnodes.every((node) => node.constant) && !this.type.intersects(Type_js_1.Type.Reference)) {
57
40
  return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(new CallFrame_js_1.CallFrame()), this.type));
58
41
  }
59
42
  return this;
@@ -63,7 +46,23 @@ class CallNode extends Node_js_1.Node {
63
46
  if (typeof func !== "function") {
64
47
  this.throwError(`function expected not ${Type_js_1.Type.of(func)}`);
65
48
  }
66
- return (0, Value_js_1.normalize)(func(...this._subnodes.map((node) => node.evaluate(frame))));
49
+ const args = [];
50
+ for (let i = 0; i < this._subnodes.length; ++i) {
51
+ const arg = this._subnodes[i].evaluate(frame);
52
+ if (i === 0) {
53
+ if (this._optional && arg == null) {
54
+ return undefined;
55
+ }
56
+ if (func === Unknown_js_1.funcCoalesce.value && arg != null) {
57
+ return arg;
58
+ }
59
+ }
60
+ if (func === Boolean_js_1.funcAnd.value && arg === false || func === Boolean_js_1.funcOr.value && arg === true) {
61
+ return arg;
62
+ }
63
+ args.push(arg);
64
+ }
65
+ return frame.define(func(...args), this.type);
67
66
  }
68
67
  toString(ident = 0) {
69
68
  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;