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