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/README.md
CHANGED
|
@@ -173,7 +173,8 @@ Casts can be followed by property access, index access, function calls, or anoth
|
|
|
173
173
|
|
|
174
174
|
Native conversions use the following **void** handling:
|
|
175
175
|
|
|
176
|
-
- `Format`
|
|
176
|
+
- General `Format` conversions and `JSON.Format` return an empty string for **void**.
|
|
177
|
+
- `AN.Format` and `Affinirum.format` serialize **void** as the literal `null`, including array elements and object properties.
|
|
177
178
|
- `Encode` returns a zero-length buffer for **void**.
|
|
178
179
|
- `Decode` and `Parse` return **void** when their input value is **void**.
|
|
179
180
|
|
|
@@ -240,7 +241,7 @@ Invalid non-void inputs are function-specific and may raise an evaluation error.
|
|
|
240
241
|
- **Object.Merge(values: ...(object | [object])):object** — Merge objects and arrays of objects
|
|
241
242
|
|
|
242
243
|
#### AN
|
|
243
|
-
- **AN.Format(value: ??, whitespace: string?):string** — Format a value as AN notation
|
|
244
|
+
- **AN.Format(value: ??, whitespace: string?):string** — Format a value as AN notation; **void** values are written as `null`
|
|
244
245
|
|
|
245
246
|
#### JSON
|
|
246
247
|
- **JSON.Format(value: (void | boolean | timestamp | float | integer | string | array | object), whitespace: string?):string** — Format a value as JSON
|
|
@@ -409,8 +410,17 @@ the inferred input types through `variables()`, and the parsed tree through `toS
|
|
|
409
410
|
```ts
|
|
410
411
|
const value = Affinirum.parse('["answer":42,"enabled":true]');
|
|
411
412
|
const notation = Affinirum.format(value);
|
|
413
|
+
|
|
414
|
+
Affinirum.format(undefined); // "null"
|
|
415
|
+
Affinirum.format(null); // "null"
|
|
416
|
+
Affinirum.format([1n, undefined]); // "[1,null]"
|
|
417
|
+
Affinirum.format({ missing: undefined }); // '["missing":null]'
|
|
412
418
|
```
|
|
413
419
|
|
|
420
|
+
Both `Affinirum.format()` and `AN.Format()` write JavaScript `null` and `undefined` as the AN literal `null` at every nesting level.
|
|
421
|
+
Parsing that literal produces JavaScript `undefined`. This preserves array lengths and object properties containing **void** values,
|
|
422
|
+
including trailing array elements. Indented output follows the same rule.
|
|
423
|
+
|
|
414
424
|
JavaScript `number` values map to **float**, while **integer** values map to `bigint`. Timestamps use `Date`, buffers use
|
|
415
425
|
`ArrayBuffer`, and a **void** result is returned as `undefined`. Arrays, objects, and functions use their corresponding
|
|
416
426
|
JavaScript values. Injected non-function values are checked against the types inferred or declared during compilation.
|
package/dst/Affinirum.d.ts
CHANGED
|
@@ -5,20 +5,20 @@ import { Type } from "./Type.js";
|
|
|
5
5
|
import { Node } from "./Node.js";
|
|
6
6
|
import { LoopNode } from "./node/LoopNode.js";
|
|
7
7
|
import { SwitchNode } from "./node/SwitchNode.js";
|
|
8
|
-
import {
|
|
8
|
+
import { ScriptSpan } from "./ScriptSpan.js";
|
|
9
9
|
import { ParserState } from "./ParserState.js";
|
|
10
|
-
import {
|
|
10
|
+
import { LexicalScope } from "./LexicalScope.js";
|
|
11
11
|
export declare class Affinirum {
|
|
12
12
|
static readonly keywords: string[];
|
|
13
13
|
protected readonly _script: string;
|
|
14
14
|
protected readonly _strict: boolean;
|
|
15
15
|
protected readonly _root: Node;
|
|
16
16
|
protected readonly _vartypes: Map<string, Type>;
|
|
17
|
-
protected readonly
|
|
17
|
+
protected readonly _varspans: Map<string, ScriptSpan>;
|
|
18
18
|
protected readonly _variables: Map<string, Variable>;
|
|
19
19
|
protected readonly _constants: Map<string, Record<string, Constant>>;
|
|
20
20
|
protected readonly _functions: Map<string, Constant>;
|
|
21
|
-
protected readonly _scope:
|
|
21
|
+
protected readonly _scope: LexicalScope;
|
|
22
22
|
/**
|
|
23
23
|
Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
|
|
24
24
|
@param script Math expression to compile.
|
|
@@ -63,26 +63,26 @@ export declare class Affinirum {
|
|
|
63
63
|
*/
|
|
64
64
|
static parse(value: string | undefined): Value;
|
|
65
65
|
/** Formats a JavaScript value as an Affinirum Notation string.
|
|
66
|
-
@param value Value to format.
|
|
66
|
+
@param value Value to format. Null and undefined are formatted as the AN literal null.
|
|
67
67
|
@param whitespace Optional whitespace used for each indentation level.
|
|
68
68
|
@returns Affinirum Notation formatted string.
|
|
69
69
|
*/
|
|
70
70
|
static format(value: Value, whitespace?: string): string;
|
|
71
|
-
protected _list(state: ParserState, scope:
|
|
72
|
-
protected _unit(state: ParserState, scope:
|
|
73
|
-
protected _coalescence(state: ParserState, scope:
|
|
74
|
-
protected _disjunction(state: ParserState, scope:
|
|
75
|
-
protected _conjunction(state: ParserState, scope:
|
|
76
|
-
protected _negation(state: ParserState, scope:
|
|
77
|
-
protected _comparison(state: ParserState, scope:
|
|
78
|
-
protected _aggregate(state: ParserState, scope:
|
|
79
|
-
protected _product(state: ParserState, scope:
|
|
80
|
-
protected _signum(state: ParserState, scope:
|
|
81
|
-
protected _factor(state: ParserState, scope:
|
|
82
|
-
protected _accessor(state: ParserState, scope:
|
|
83
|
-
protected _term(state: ParserState, scope:
|
|
84
|
-
protected _function(state: ParserState, scope:
|
|
85
|
-
protected _loop(state: ParserState, scope:
|
|
86
|
-
protected _switch(state: ParserState, scope:
|
|
87
|
-
protected _type(state: ParserState, scope:
|
|
71
|
+
protected _list(state: ParserState, scope: LexicalScope): Node;
|
|
72
|
+
protected _unit(state: ParserState, scope: LexicalScope): Node;
|
|
73
|
+
protected _coalescence(state: ParserState, scope: LexicalScope): Node;
|
|
74
|
+
protected _disjunction(state: ParserState, scope: LexicalScope): Node;
|
|
75
|
+
protected _conjunction(state: ParserState, scope: LexicalScope): Node;
|
|
76
|
+
protected _negation(state: ParserState, scope: LexicalScope): Node;
|
|
77
|
+
protected _comparison(state: ParserState, scope: LexicalScope): Node;
|
|
78
|
+
protected _aggregate(state: ParserState, scope: LexicalScope): Node;
|
|
79
|
+
protected _product(state: ParserState, scope: LexicalScope): Node;
|
|
80
|
+
protected _signum(state: ParserState, scope: LexicalScope): Node;
|
|
81
|
+
protected _factor(state: ParserState, scope: LexicalScope): Node;
|
|
82
|
+
protected _accessor(state: ParserState, scope: LexicalScope): Node;
|
|
83
|
+
protected _term(state: ParserState, scope: LexicalScope): Node;
|
|
84
|
+
protected _function(state: ParserState, scope: LexicalScope): Node;
|
|
85
|
+
protected _loop(state: ParserState, scope: LexicalScope): LoopNode;
|
|
86
|
+
protected _switch(state: ParserState, scope: LexicalScope): SwitchNode;
|
|
87
|
+
protected _type(state: ParserState, scope: LexicalScope): Type;
|
|
88
88
|
}
|
package/dst/Affinirum.js
CHANGED
|
@@ -22,7 +22,8 @@ import { ObjectNode } from "./node/ObjectNode.js";
|
|
|
22
22
|
import { SwitchNode } from "./node/SwitchNode.js";
|
|
23
23
|
import { VariableNode } from "./node/VariableNode.js";
|
|
24
24
|
import { ParserState } from "./ParserState.js";
|
|
25
|
-
import {
|
|
25
|
+
import { LexicalScope } from "./LexicalScope.js";
|
|
26
|
+
import { CallFrame } from "./CallFrame.js";
|
|
26
27
|
import { JumpException } from "./JumpException.js";
|
|
27
28
|
export class Affinirum {
|
|
28
29
|
static keywords = [...Keywords, ...Constants.map((c) => c[0])];
|
|
@@ -30,11 +31,11 @@ export class Affinirum {
|
|
|
30
31
|
_strict;
|
|
31
32
|
_root;
|
|
32
33
|
_vartypes = new Map();
|
|
33
|
-
|
|
34
|
+
_varspans = new Map();
|
|
34
35
|
_variables = new Map();
|
|
35
36
|
_constants = new Map(Constants);
|
|
36
37
|
_functions = new Map(Functions);
|
|
37
|
-
_scope = new
|
|
38
|
+
_scope = new LexicalScope();
|
|
38
39
|
/**
|
|
39
40
|
Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
|
|
40
41
|
@param script Math expression to compile.
|
|
@@ -94,18 +95,19 @@ export class Affinirum {
|
|
|
94
95
|
@throws If an input value is missing or evaluation fails.
|
|
95
96
|
*/
|
|
96
97
|
evaluate(values) {
|
|
98
|
+
const frame = new CallFrame(undefined, this._scope.variables());
|
|
97
99
|
try {
|
|
98
100
|
for (const [name, variable] of this._variables) {
|
|
99
101
|
if (!values || !Object.hasOwn(values, name)) {
|
|
100
|
-
this.
|
|
102
|
+
this._varspans.get(name)?.throwError(`undefined variable ${name}:\n`);
|
|
101
103
|
}
|
|
102
104
|
const value = normalize(values?.[name] ?? undefined);
|
|
103
105
|
if (!variable.type.acceptValue(value)) {
|
|
104
|
-
this.
|
|
106
|
+
this._varspans.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
|
|
105
107
|
}
|
|
106
|
-
variable
|
|
108
|
+
frame.add(variable, value);
|
|
107
109
|
}
|
|
108
|
-
return normalize(this._root.evaluate() ?? undefined);
|
|
110
|
+
return normalize(this._root.evaluate(frame) ?? undefined);
|
|
109
111
|
}
|
|
110
112
|
catch (e) {
|
|
111
113
|
if (e instanceof JumpException) {
|
|
@@ -118,12 +120,6 @@ export class Affinirum {
|
|
|
118
120
|
}
|
|
119
121
|
throw e;
|
|
120
122
|
}
|
|
121
|
-
finally {
|
|
122
|
-
for (const variable of this._variables.values()) {
|
|
123
|
-
variable.value = undefined;
|
|
124
|
-
}
|
|
125
|
-
this._scope.clear();
|
|
126
|
-
}
|
|
127
123
|
}
|
|
128
124
|
/** Parses an Affinirum Notation string into a JavaScript value.
|
|
129
125
|
@param value Affinirum Notation string to parse.
|
|
@@ -134,7 +130,7 @@ export class Affinirum {
|
|
|
134
130
|
return parseAN(value);
|
|
135
131
|
}
|
|
136
132
|
/** Formats a JavaScript value as an Affinirum Notation string.
|
|
137
|
-
@param value Value to format.
|
|
133
|
+
@param value Value to format. Null and undefined are formatted as the AN literal null.
|
|
138
134
|
@param whitespace Optional whitespace used for each indentation level.
|
|
139
135
|
@returns Affinirum Notation formatted string.
|
|
140
136
|
*/
|
|
@@ -142,7 +138,7 @@ export class Affinirum {
|
|
|
142
138
|
return formatAN(value, whitespace);
|
|
143
139
|
}
|
|
144
140
|
_list(state, scope) {
|
|
145
|
-
const
|
|
141
|
+
const span = state.start();
|
|
146
142
|
const subnodes = [];
|
|
147
143
|
while (!state.isEmpty) {
|
|
148
144
|
subnodes.push(this._unit(state, scope));
|
|
@@ -153,7 +149,7 @@ export class Affinirum {
|
|
|
153
149
|
state.next();
|
|
154
150
|
}
|
|
155
151
|
}
|
|
156
|
-
return new BlockNode(
|
|
152
|
+
return new BlockNode(span.end(state), subnodes);
|
|
157
153
|
}
|
|
158
154
|
_unit(state, scope) {
|
|
159
155
|
return this._coalescence(state, scope);
|
|
@@ -183,17 +179,17 @@ export class Affinirum {
|
|
|
183
179
|
return node;
|
|
184
180
|
}
|
|
185
181
|
_negation(state, scope) {
|
|
186
|
-
const
|
|
182
|
+
const span = state.start();
|
|
187
183
|
let negate = false;
|
|
188
184
|
while (state.operator === funcNot) {
|
|
189
185
|
negate = !negate;
|
|
190
|
-
|
|
186
|
+
span.end(state);
|
|
191
187
|
state.next();
|
|
192
188
|
}
|
|
193
189
|
let node = this._comparison(state, scope);
|
|
194
190
|
if (negate) {
|
|
195
|
-
const fnode = new ConstantNode(
|
|
196
|
-
node = new CallNode(
|
|
191
|
+
const fnode = new ConstantNode(span, funcNot);
|
|
192
|
+
node = new CallNode(span, fnode, [node]);
|
|
197
193
|
}
|
|
198
194
|
return node;
|
|
199
195
|
}
|
|
@@ -224,19 +220,19 @@ export class Affinirum {
|
|
|
224
220
|
return node;
|
|
225
221
|
}
|
|
226
222
|
_signum(state, scope) {
|
|
227
|
-
const
|
|
223
|
+
const span = state.start();
|
|
228
224
|
let negate = false;
|
|
229
225
|
while (state.operator === funcAdd || state.operator === funcSubtract) {
|
|
230
226
|
if (state.operator === funcSubtract) {
|
|
231
227
|
negate = !negate;
|
|
232
228
|
}
|
|
233
|
-
|
|
229
|
+
span.end(state);
|
|
234
230
|
state.next();
|
|
235
231
|
}
|
|
236
232
|
let node = this._factor(state, scope);
|
|
237
233
|
if (negate) {
|
|
238
|
-
const fnode = new ConstantNode(
|
|
239
|
-
node = new CallNode(
|
|
234
|
+
const fnode = new ConstantNode(span, funcNegate);
|
|
235
|
+
node = new CallNode(span, fnode, [node]);
|
|
240
236
|
}
|
|
241
237
|
return node;
|
|
242
238
|
}
|
|
@@ -249,14 +245,14 @@ export class Affinirum {
|
|
|
249
245
|
return node;
|
|
250
246
|
}
|
|
251
247
|
_accessor(state, scope) {
|
|
252
|
-
const
|
|
248
|
+
const span = state.start();
|
|
253
249
|
let node = this._term(state, scope);
|
|
254
250
|
while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen || state.isCast) {
|
|
255
251
|
if (state.isDot || state.isQuestion) { // property access or property existance operator
|
|
256
252
|
const operator = state.isDot ? funcAt : funcHas;
|
|
257
253
|
if (state.next().isLiteral && (typeof state.literal.value === "string" || typeof state.literal.value === "bigint")) {
|
|
258
|
-
const fnode = new ConstantNode(
|
|
259
|
-
node = new CallNode(
|
|
254
|
+
const fnode = new ConstantNode(span.end(state), operator);
|
|
255
|
+
node = new CallNode(span, fnode, [node, new ConstantNode(state, new Constant(state.literal.value))]);
|
|
260
256
|
state.next();
|
|
261
257
|
}
|
|
262
258
|
else if (state.isToken) {
|
|
@@ -270,18 +266,18 @@ export class Affinirum {
|
|
|
270
266
|
break;
|
|
271
267
|
}
|
|
272
268
|
}
|
|
273
|
-
const fnode = new ConstantNode(
|
|
274
|
-
node = new CallNode(
|
|
269
|
+
const fnode = new ConstantNode(span.end(state), func);
|
|
270
|
+
node = new CallNode(span, fnode, subnodes);
|
|
275
271
|
state.closeParentheses().next();
|
|
276
272
|
}
|
|
277
273
|
else {
|
|
278
|
-
const fnode = new ConstantNode(
|
|
279
|
-
node = new CallNode(
|
|
274
|
+
const fnode = new ConstantNode(span.end(state), func);
|
|
275
|
+
node = new CallNode(span, fnode, [node]);
|
|
280
276
|
}
|
|
281
277
|
}
|
|
282
278
|
else {
|
|
283
|
-
const fnode = new ConstantNode(
|
|
284
|
-
node = new CallNode(
|
|
279
|
+
const fnode = new ConstantNode(span.end(state), operator);
|
|
280
|
+
node = new CallNode(span, fnode, [node, new ConstantNode(state, new Constant(state.token))]);
|
|
285
281
|
state.next();
|
|
286
282
|
}
|
|
287
283
|
}
|
|
@@ -298,32 +294,32 @@ export class Affinirum {
|
|
|
298
294
|
}
|
|
299
295
|
}
|
|
300
296
|
state.closeParentheses();
|
|
301
|
-
node = new CallNode(
|
|
297
|
+
node = new CallNode(span.end(state), node, subnodes);
|
|
302
298
|
state.next();
|
|
303
299
|
}
|
|
304
300
|
else if (state.isBracketsOpen) { // index access operator
|
|
305
301
|
const subnodes = [node, this._unit(state.next(), scope)];
|
|
306
302
|
state.closeBrackets();
|
|
307
|
-
const fnode = new ConstantNode(
|
|
308
|
-
node = new CallNode(
|
|
303
|
+
const fnode = new ConstantNode(span.end(state), funcAt);
|
|
304
|
+
node = new CallNode(span, fnode, subnodes);
|
|
309
305
|
state.next();
|
|
310
306
|
}
|
|
311
307
|
else if (state.isCast) { // type cast operator
|
|
312
308
|
const type = this._type(state.next(), scope);
|
|
313
|
-
node = new CastNode(
|
|
309
|
+
node = new CastNode(span.end(state), node, type);
|
|
314
310
|
}
|
|
315
311
|
}
|
|
316
312
|
return node;
|
|
317
313
|
}
|
|
318
314
|
_term(state, scope) {
|
|
319
315
|
if (state.isLiteral) { // literal constant
|
|
320
|
-
const
|
|
316
|
+
const span = state.start();
|
|
321
317
|
const constant = new Constant(state.literal.value);
|
|
322
318
|
state.next();
|
|
323
|
-
return new ConstantNode(
|
|
319
|
+
return new ConstantNode(span, constant);
|
|
324
320
|
}
|
|
325
321
|
else if (state.isToken) { // variable
|
|
326
|
-
const
|
|
322
|
+
const span = state.start();
|
|
327
323
|
const constants = this._constants.get(state.token);
|
|
328
324
|
if (constants != null) {
|
|
329
325
|
if (!state.next().isDot) {
|
|
@@ -337,7 +333,7 @@ export class Affinirum {
|
|
|
337
333
|
state.throwError(`unknown constant ${state.token}`);
|
|
338
334
|
}
|
|
339
335
|
state.next();
|
|
340
|
-
return new ConstantNode(
|
|
336
|
+
return new ConstantNode(span, constant);
|
|
341
337
|
}
|
|
342
338
|
let variable = scope.get(state.token) ?? this._variables.get(state.token);
|
|
343
339
|
if (variable == null) {
|
|
@@ -347,7 +343,7 @@ export class Affinirum {
|
|
|
347
343
|
}
|
|
348
344
|
variable = new Variable(type);
|
|
349
345
|
this._variables.set(state.token, variable);
|
|
350
|
-
this.
|
|
346
|
+
this._varspans.set(state.token, state.start());
|
|
351
347
|
}
|
|
352
348
|
if (state.next().isAssignment) {
|
|
353
349
|
if (!variable.assignable) {
|
|
@@ -355,19 +351,19 @@ export class Affinirum {
|
|
|
355
351
|
}
|
|
356
352
|
if (state.assignment.operator) {
|
|
357
353
|
const operator = state.assignment.operator;
|
|
358
|
-
const fnode = new ConstantNode(
|
|
359
|
-
const subnode = new CallNode(
|
|
360
|
-
return new VariableNode(
|
|
354
|
+
const fnode = new ConstantNode(span, operator);
|
|
355
|
+
const subnode = new CallNode(span, fnode, [new VariableNode(span, variable), this._unit(state.next(), scope)]);
|
|
356
|
+
return new VariableNode(span, variable, subnode);
|
|
361
357
|
}
|
|
362
358
|
else {
|
|
363
359
|
const subnode = this._unit(state.next(), scope);
|
|
364
|
-
return new VariableNode(
|
|
360
|
+
return new VariableNode(span, variable, subnode);
|
|
365
361
|
}
|
|
366
362
|
}
|
|
367
|
-
return new VariableNode(
|
|
363
|
+
return new VariableNode(span, variable);
|
|
368
364
|
}
|
|
369
365
|
else if (state.isBracesOpen) { // code block
|
|
370
|
-
const
|
|
366
|
+
const span = state.start();
|
|
371
367
|
const subscope = scope.subscope();
|
|
372
368
|
const subnodes = [];
|
|
373
369
|
while (!state.next().isBracesClose) {
|
|
@@ -376,9 +372,9 @@ export class Affinirum {
|
|
|
376
372
|
break;
|
|
377
373
|
}
|
|
378
374
|
}
|
|
379
|
-
|
|
375
|
+
span.end(state);
|
|
380
376
|
state.closeBraces().next();
|
|
381
|
-
return new BlockNode(
|
|
377
|
+
return new BlockNode(span, subnodes, [...subscope.variables()]);
|
|
382
378
|
}
|
|
383
379
|
else if (state.isBracesClose) {
|
|
384
380
|
state.throwError("unexpected closing braces");
|
|
@@ -392,7 +388,7 @@ export class Affinirum {
|
|
|
392
388
|
state.throwError("unexpected closing parentheses");
|
|
393
389
|
}
|
|
394
390
|
else if (state.isBracketsOpen) { // array or object
|
|
395
|
-
const
|
|
391
|
+
const span = state.start();
|
|
396
392
|
const subnodes = [];
|
|
397
393
|
let index = 0, colon = false;
|
|
398
394
|
while (!state.next().isBracketsClose) {
|
|
@@ -413,12 +409,12 @@ export class Affinirum {
|
|
|
413
409
|
break;
|
|
414
410
|
}
|
|
415
411
|
}
|
|
416
|
-
|
|
412
|
+
span.end(state);
|
|
417
413
|
state.closeBrackets().next();
|
|
418
414
|
if (colon) {
|
|
419
|
-
return new ObjectNode(
|
|
415
|
+
return new ObjectNode(span, subnodes.map(([k, v]) => [typeof k === "number" ? new ConstantNode(v, new Constant(String(k))) : k, v]));
|
|
420
416
|
}
|
|
421
|
-
return new ArrayNode(
|
|
417
|
+
return new ArrayNode(span, subnodes.map(([, v]) => v));
|
|
422
418
|
}
|
|
423
419
|
else if (state.isBracketsClose) {
|
|
424
420
|
state.throwError("unexpected closing brackets");
|
|
@@ -432,7 +428,7 @@ export class Affinirum {
|
|
|
432
428
|
if (scope.has(token)) {
|
|
433
429
|
state.throwError(`illegal redefinition of variable ${token}`);
|
|
434
430
|
}
|
|
435
|
-
const
|
|
431
|
+
const span = state.start();
|
|
436
432
|
let type;
|
|
437
433
|
if (state.next().isColon) {
|
|
438
434
|
type = this._type(state.next(), scope);
|
|
@@ -443,9 +439,9 @@ export class Affinirum {
|
|
|
443
439
|
if (state.assignment.operator) {
|
|
444
440
|
state.throwError(`illegal assignment operator with variable ${token}`);
|
|
445
441
|
}
|
|
446
|
-
return new VariableNode(
|
|
442
|
+
return new VariableNode(span, variable, this._unit(state.next(), scope));
|
|
447
443
|
}
|
|
448
|
-
return new VariableNode(
|
|
444
|
+
return new VariableNode(span, variable);
|
|
449
445
|
}
|
|
450
446
|
else if (state.isTilda) {
|
|
451
447
|
return this._function(state, scope);
|
|
@@ -478,7 +474,7 @@ export class Affinirum {
|
|
|
478
474
|
state.throwError("unexpected expression token");
|
|
479
475
|
}
|
|
480
476
|
_function(state, scope) {
|
|
481
|
-
const
|
|
477
|
+
const span = state.start();
|
|
482
478
|
state.next().openParentheses();
|
|
483
479
|
let variadic = false;
|
|
484
480
|
const variables = new Map();
|
|
@@ -487,8 +483,11 @@ export class Affinirum {
|
|
|
487
483
|
state.throwError("missing function argument name");
|
|
488
484
|
}
|
|
489
485
|
const token = state.token;
|
|
486
|
+
if (variables.has(token)) {
|
|
487
|
+
state.throwError(`duplicate function argument name ${token}`);
|
|
488
|
+
}
|
|
490
489
|
if (scope.has(token)) {
|
|
491
|
-
state.throwError(
|
|
490
|
+
state.throwError(`variable ${token} redefinition`);
|
|
492
491
|
}
|
|
493
492
|
let argType = Type.Unknown;
|
|
494
493
|
if (state.next().isColon) {
|
|
@@ -513,38 +512,46 @@ export class Affinirum {
|
|
|
513
512
|
}
|
|
514
513
|
state.closeParentheses().next();
|
|
515
514
|
const retType = state.isColon ? this._type(state.next(), scope) : Type.Unknown;
|
|
516
|
-
|
|
515
|
+
span.end(state);
|
|
517
516
|
const args = Array.from(variables.values());
|
|
518
517
|
const subscope = scope.subscope(variables);
|
|
519
518
|
const subnode = this._unit(state, subscope);
|
|
520
|
-
const
|
|
521
|
-
const
|
|
519
|
+
const createFunction = (frame) => (...values) => {
|
|
520
|
+
const subframe = frame.subframe(subscope.variables());
|
|
522
521
|
try {
|
|
523
|
-
args.forEach((arg, ix) => arg
|
|
524
|
-
return normalize(subnode.evaluate());
|
|
522
|
+
args.forEach((arg, ix) => subframe.set(arg, normalize(values[ix])));
|
|
523
|
+
return normalize(subnode.evaluate(subframe));
|
|
525
524
|
}
|
|
526
|
-
|
|
527
|
-
|
|
525
|
+
catch (e) {
|
|
526
|
+
if (e instanceof JumpException) {
|
|
527
|
+
if (e.jump === "return") {
|
|
528
|
+
return normalize(e.value);
|
|
529
|
+
}
|
|
530
|
+
if (e.jump === "stop" || e.jump === "next") {
|
|
531
|
+
subnode.throwError(`unexpected ${e.jump} jump`);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
throw e;
|
|
528
535
|
}
|
|
529
536
|
};
|
|
530
|
-
const constant = new Constant(
|
|
531
|
-
return new ConstantNode(
|
|
537
|
+
const constant = new Constant(createFunction, Type.functionType(retType, args.map((v) => v.type), variadic));
|
|
538
|
+
return new ConstantNode(span, constant, subnode, createFunction);
|
|
532
539
|
}
|
|
533
540
|
_loop(state, scope) {
|
|
534
|
-
const
|
|
541
|
+
const span = state.start();
|
|
535
542
|
const cnode = this._unit(state.next(), scope);
|
|
536
|
-
|
|
537
|
-
return new LoopNode(
|
|
543
|
+
span.end(state);
|
|
544
|
+
return new LoopNode(span, cnode, this._unit(state, scope));
|
|
538
545
|
}
|
|
539
546
|
_switch(state, scope) {
|
|
540
|
-
const
|
|
547
|
+
const span = state.start();
|
|
541
548
|
const cnode = this._unit(state.next(), scope);
|
|
542
|
-
|
|
549
|
+
span.end(state);
|
|
543
550
|
const cthen = this._unit(state, scope);
|
|
544
551
|
const celse = state.isElse
|
|
545
552
|
? this._unit(state.next(), scope)
|
|
546
|
-
: new ConstantNode(state.
|
|
547
|
-
return new SwitchNode(
|
|
553
|
+
: new ConstantNode(state.start(), Constant.Null);
|
|
554
|
+
return new SwitchNode(span, cnode, [cthen, celse]);
|
|
548
555
|
}
|
|
549
556
|
_type(state, scope) {
|
|
550
557
|
if (state.isType) { // literal type
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Value } from "./Value.js";
|
|
2
|
+
import type { Variable } from "./Variable.js";
|
|
3
|
+
export declare class CallFrame {
|
|
4
|
+
protected readonly _superframe?: CallFrame;
|
|
5
|
+
protected readonly _values: Map<Variable, Value>;
|
|
6
|
+
constructor(superframe?: CallFrame, variables?: Iterable<Variable>);
|
|
7
|
+
subframe(variables: Iterable<Variable>): CallFrame;
|
|
8
|
+
add(variable: Variable, value?: Value): void;
|
|
9
|
+
get(variable: Variable): Value;
|
|
10
|
+
set(variable: Variable, value: Value): Value;
|
|
11
|
+
}
|
package/dst/CallFrame.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export class CallFrame {
|
|
2
|
+
_superframe;
|
|
3
|
+
_values = new Map();
|
|
4
|
+
constructor(superframe, variables = []) {
|
|
5
|
+
this._superframe = superframe;
|
|
6
|
+
for (const variable of variables) {
|
|
7
|
+
this.add(variable);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
subframe(variables) {
|
|
11
|
+
return new CallFrame(this, variables);
|
|
12
|
+
}
|
|
13
|
+
add(variable, value = undefined) {
|
|
14
|
+
this._values.set(variable, value);
|
|
15
|
+
}
|
|
16
|
+
get(variable) {
|
|
17
|
+
if (this._values.has(variable)) {
|
|
18
|
+
return this._values.get(variable);
|
|
19
|
+
}
|
|
20
|
+
if (this._superframe) {
|
|
21
|
+
return this._superframe.get(variable);
|
|
22
|
+
}
|
|
23
|
+
throw new Error("variable is not declared in call frame");
|
|
24
|
+
}
|
|
25
|
+
set(variable, value) {
|
|
26
|
+
if (this._values.has(variable)) {
|
|
27
|
+
this._values.set(variable, value);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
if (this._superframe) {
|
|
31
|
+
return this._superframe.set(variable, value);
|
|
32
|
+
}
|
|
33
|
+
throw new Error("variable is not declared in call frame");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Variable } from "./Variable.js";
|
|
2
|
+
export declare class LexicalScope {
|
|
3
|
+
protected readonly _superscope?: LexicalScope;
|
|
4
|
+
protected readonly _subscopes: LexicalScope[];
|
|
5
|
+
protected readonly _variables: Map<string, Variable>;
|
|
6
|
+
protected readonly _boundary: boolean;
|
|
7
|
+
constructor(superscope?: LexicalScope, variables?: Map<string, Variable>);
|
|
8
|
+
has(name: string): boolean;
|
|
9
|
+
get(name: string): Variable | undefined;
|
|
10
|
+
set(name: string, variable: Variable): void;
|
|
11
|
+
subscope(variables?: Map<string, Variable>): LexicalScope;
|
|
12
|
+
variables(): IterableIterator<Variable>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export class LexicalScope {
|
|
2
|
+
_superscope;
|
|
3
|
+
_subscopes = [];
|
|
4
|
+
_variables;
|
|
5
|
+
_boundary;
|
|
6
|
+
constructor(superscope, variables) {
|
|
7
|
+
superscope?._subscopes.push(this);
|
|
8
|
+
this._superscope = superscope;
|
|
9
|
+
this._variables = variables ?? new Map();
|
|
10
|
+
this._boundary = Boolean(variables);
|
|
11
|
+
}
|
|
12
|
+
has(name) {
|
|
13
|
+
return this._variables.has(name) || Boolean(this._superscope?.has(name));
|
|
14
|
+
}
|
|
15
|
+
get(name) {
|
|
16
|
+
return this._variables.get(name) ?? this._superscope?.get(name);
|
|
17
|
+
}
|
|
18
|
+
set(name, variable) {
|
|
19
|
+
this._variables.set(name, variable);
|
|
20
|
+
}
|
|
21
|
+
subscope(variables) {
|
|
22
|
+
return new LexicalScope(this, variables);
|
|
23
|
+
}
|
|
24
|
+
variables() {
|
|
25
|
+
return this._variables.values();
|
|
26
|
+
}
|
|
27
|
+
}
|
package/dst/Node.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ScriptSpan } from "./ScriptSpan.js";
|
|
2
2
|
import { Type } from "./Type.js";
|
|
3
3
|
import { Value } from "./Value.js";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import { CallFrame } from "./CallFrame.js";
|
|
5
|
+
export declare abstract class Node extends ScriptSpan {
|
|
6
|
+
constructor(span: ScriptSpan);
|
|
6
7
|
abstract type: Type;
|
|
7
8
|
abstract compile(type: Type): Node;
|
|
8
|
-
abstract evaluate(): Value;
|
|
9
|
+
abstract evaluate(frame: CallFrame): Value;
|
|
9
10
|
get constant(): boolean;
|
|
10
11
|
protected reduceType(type: Type): Type;
|
|
11
12
|
protected throwTypeError(type: Type): never;
|
package/dst/Node.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export class Node extends
|
|
3
|
-
constructor(
|
|
4
|
-
super(
|
|
1
|
+
import { ScriptSpan } from "./ScriptSpan.js";
|
|
2
|
+
export class Node extends ScriptSpan {
|
|
3
|
+
constructor(span) {
|
|
4
|
+
super(span);
|
|
5
5
|
}
|
|
6
6
|
get constant() {
|
|
7
7
|
return false;
|
|
@@ -17,6 +17,6 @@ export class Node extends ParserFrame {
|
|
|
17
17
|
return this.throwError(`type ${this.type} mismatch with expected type ${type}`);
|
|
18
18
|
}
|
|
19
19
|
toString(ident = 0) {
|
|
20
|
-
return " ".repeat(ident) + `[${this._start}:${this.
|
|
20
|
+
return " ".repeat(ident) + `[${this._start}:${this._end}] <${this.type}>`;
|
|
21
21
|
}
|
|
22
22
|
}
|
package/dst/ParserState.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Constant } from "./Constant.js";
|
|
2
2
|
import { Value } from "./Value.js";
|
|
3
3
|
import { Type } from "./Type.js";
|
|
4
|
-
import {
|
|
4
|
+
import { ScriptSpan } from "./ScriptSpan.js";
|
|
5
5
|
declare class Literal {
|
|
6
6
|
readonly value: Value;
|
|
7
7
|
constructor(value: Value);
|
|
@@ -10,9 +10,9 @@ declare class Assignment {
|
|
|
10
10
|
readonly operator?: Constant | undefined;
|
|
11
11
|
constructor(operator?: Constant | undefined);
|
|
12
12
|
}
|
|
13
|
-
export declare class ParserState extends
|
|
13
|
+
export declare class ParserState extends ScriptSpan {
|
|
14
14
|
protected _fragment: Literal | Assignment | Constant | Type | symbol | string | undefined;
|
|
15
|
-
constructor(
|
|
15
|
+
constructor(script: string);
|
|
16
16
|
get literal(): Literal;
|
|
17
17
|
get assignment(): Assignment;
|
|
18
18
|
get operator(): Constant;
|