@mark1russell7/vex-compiler 0.0.0-stage → 0.8.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.
- package/LICENSE +21 -0
- package/README.md +73 -2
- package/build/index.d.ts +297 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +3736 -0
- package/build/index.js.map +1 -0
- package/package.json +60 -3
- package/src/facts.ts +206 -0
- package/src/index.ts +8 -0
- package/src/js.ts +324 -0
- package/src/loop.ts +138 -0
- package/src/lower.ts +710 -0
- package/src/memo.ts +235 -0
- package/src/module.ts +1723 -0
- package/src/runtime.ts +332 -0
- package/src/tiers.ts +102 -0
package/src/lower.ts
ADDED
|
@@ -0,0 +1,710 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lowering: a Vex expression becomes a program of the loop IR (docs/COMPILER.md §3.3). A list op over a direct
|
|
3
|
+
* `each` becomes one loop with a running value, so it makes no list.
|
|
4
|
+
*
|
|
5
|
+
* Each value that the interpreter can give as an error has a check. The block of the check tells what a failure does
|
|
6
|
+
* (`Fail`). A list op skips an error item, and an error in the first argument of `ifError` gives the second argument.
|
|
7
|
+
* Each other error stops the program with `FALLBACK`.
|
|
8
|
+
*/
|
|
9
|
+
import { addrExprs, isFieldName, isListOp, MAX_NESTING, nestingOf, shapeError, type Addr, type Axis, type Expr, type ExprOf, type ListOp, type Names, type PrimitiveKind } from "@mark1russell7/vex";
|
|
10
|
+
import { factsOf, listReason, MAX_CALLS, primitiveOf, type Facts, type Schema } from "./facts.ts";
|
|
11
|
+
import { memoNodes } from "./memo.ts";
|
|
12
|
+
import {
|
|
13
|
+
array,
|
|
14
|
+
bool,
|
|
15
|
+
call,
|
|
16
|
+
constant,
|
|
17
|
+
FALLBACK_EXPR,
|
|
18
|
+
FIXED_LOCALS,
|
|
19
|
+
local,
|
|
20
|
+
num,
|
|
21
|
+
ORIGIN,
|
|
22
|
+
prim,
|
|
23
|
+
RETURN,
|
|
24
|
+
SPACE,
|
|
25
|
+
TABLES,
|
|
26
|
+
VARS,
|
|
27
|
+
type Fail,
|
|
28
|
+
type LExpr,
|
|
29
|
+
type LoopProgram,
|
|
30
|
+
type LStmt,
|
|
31
|
+
} from "./loop.ts";
|
|
32
|
+
|
|
33
|
+
/** The error of an expression that the compiler cannot lower. Then the program operates in tier 1. */
|
|
34
|
+
export class Unsupported extends Error {}
|
|
35
|
+
|
|
36
|
+
/** A bound name: its value, and the flag that tells if its binding gave a value. Without a flag, it always does. */
|
|
37
|
+
interface Bound {
|
|
38
|
+
readonly value: LExpr;
|
|
39
|
+
readonly ok?: LExpr;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A bound function: its node, the path of its node, and the scope of its binding. */
|
|
43
|
+
interface BoundFn {
|
|
44
|
+
readonly fn: ExprOf<"fn">;
|
|
45
|
+
readonly path: readonly number[];
|
|
46
|
+
readonly scope: Scope;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The facts of the lexical position of a node: its space, its origin, its focus and its bound names. */
|
|
50
|
+
interface Scope {
|
|
51
|
+
readonly space: LExpr;
|
|
52
|
+
readonly origin: LExpr;
|
|
53
|
+
readonly focus: LExpr;
|
|
54
|
+
readonly env: ReadonlyMap<string, Bound>;
|
|
55
|
+
/** The bound functions (FN.BIND): a call puts the body of its function at the place of the call. */
|
|
56
|
+
readonly fns: ReadonlyMap<string, BoundFn>;
|
|
57
|
+
/** The record at the focus. In a loop, it reads the record by its index (SPACE.RECORDS). */
|
|
58
|
+
readonly rec: LExpr;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A list of statements, and what a failed check in it does. */
|
|
62
|
+
interface Block {
|
|
63
|
+
readonly stmts: LStmt[];
|
|
64
|
+
readonly fail: Fail;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const block = (fail: Fail): Block => ({ stmts: [], fail });
|
|
68
|
+
|
|
69
|
+
/** This function gives the axis inside the `where` axes of an axis. */
|
|
70
|
+
const baseAxis = (a: Axis): Exclude<Axis, { readonly t: "where" }> => (a.t === "where" ? baseAxis(a.axis) : a);
|
|
71
|
+
|
|
72
|
+
/** This function gives the tests of the `where` axes of an axis, from the inside out, as the interpreter applies them. */
|
|
73
|
+
function whereTests(a: Axis): readonly Expr[] {
|
|
74
|
+
const out: Expr[] = [];
|
|
75
|
+
for (let x: Axis = a; x.t === "where"; x = x.axis) out.unshift(x.test);
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** This function gives the `each` under the `let` expressions of a list argument, or `undefined`. */
|
|
80
|
+
function eachUnderLets(e: Expr | undefined): ExprOf<"each"> | undefined {
|
|
81
|
+
let x = e;
|
|
82
|
+
while (x?.tag === "let") x = x.body;
|
|
83
|
+
return x?.tag === "each" ? x : undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The kinds of the values of the primitive operations that always give a boolean. */
|
|
87
|
+
const BOOLEAN_PRIMS: ReadonlySet<string> = new Set(["!", "===", "!==", "isNumber", "isBoolean", "isString", "isValue", "isObject"]);
|
|
88
|
+
|
|
89
|
+
/** The checks of the kinds of a schema. */
|
|
90
|
+
const KIND_CHECKS = { number: "isNumber", string: "isString", boolean: "isBoolean" } as const;
|
|
91
|
+
|
|
92
|
+
/** This function gives the kind of two values in the branches of a form, if they have the same kind. */
|
|
93
|
+
const join = (a: PrimitiveKind | undefined, b: PrimitiveKind | undefined): PrimitiveKind | undefined => (a === b ? a : undefined);
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* This function lowers an expression into a program of the loop IR. It throws `Unsupported` for an expression it cannot
|
|
97
|
+
* lower. The facts of the options decide the op of a site whose values have known kinds (KIND.STATIC).
|
|
98
|
+
*/
|
|
99
|
+
export function lower(expr: Expr, facts: Facts = factsOf({}), schema: Schema = {}): LoopProgram {
|
|
100
|
+
// EVAL.SHAPE and EVAL.NESTING: tier 1 gives the error of a program that is not valid or that is nested too deep.
|
|
101
|
+
const shape = shapeError(expr);
|
|
102
|
+
if (shape !== undefined) throw new Unsupported(shape);
|
|
103
|
+
if (nestingOf(expr) > MAX_NESTING) throw new Unsupported(`the expression is nested deeper than ${MAX_NESTING} levels`);
|
|
104
|
+
const lists = listReason(expr);
|
|
105
|
+
if (lists !== undefined) throw new Unsupported(lists);
|
|
106
|
+
const l = new Lowerer(memoNodes(expr), facts, schema);
|
|
107
|
+
const b = block(RETURN);
|
|
108
|
+
l.check(b, call("has", local(SPACE), local(ORIGIN)), "the origin is a key of the space");
|
|
109
|
+
const top: Scope = { space: local(SPACE), origin: local(ORIGIN), focus: local(ORIGIN), env: new Map(), fns: new Map(), rec: prim("get", local(SPACE), local(ORIGIN)) };
|
|
110
|
+
const value = l.expr(expr, top, [], b);
|
|
111
|
+
b.stmts.push({ t: "return", value });
|
|
112
|
+
return { body: b.stmts, locals: l.locals, constants: l.constants };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
class Lowerer {
|
|
116
|
+
locals = FIXED_LOCALS;
|
|
117
|
+
labels = 0;
|
|
118
|
+
memos = 0;
|
|
119
|
+
/** The number of bodies of functions that the lowering put at calls (MAX_CALLS). */
|
|
120
|
+
#calls = 0;
|
|
121
|
+
readonly #memo: ReadonlyMap<Expr, readonly Expr[]>;
|
|
122
|
+
/** The keys of the nodes of the memo whose code the lowering makes, with their values. */
|
|
123
|
+
#keys: ReadonlyMap<Expr, Bound> = new Map();
|
|
124
|
+
readonly #facts: Facts;
|
|
125
|
+
readonly #schema: Schema;
|
|
126
|
+
/** The kind of each local whose kind the lowering knows. A local gets its kind where it gets its value. */
|
|
127
|
+
readonly #kinds = new Map<number, PrimitiveKind>();
|
|
128
|
+
|
|
129
|
+
constructor(memo: ReadonlyMap<Expr, readonly Expr[]>, facts: Facts, schema: Schema) {
|
|
130
|
+
this.#memo = memo;
|
|
131
|
+
this.#facts = facts;
|
|
132
|
+
this.#schema = schema;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** This method gives the kind of a value, if the lowering knows it. */
|
|
136
|
+
kindOf(x: LExpr): PrimitiveKind | undefined {
|
|
137
|
+
if (x.t === "const") {
|
|
138
|
+
const t = typeof this.constants[x.i];
|
|
139
|
+
return t === "number" || t === "string" || t === "boolean" ? t : undefined;
|
|
140
|
+
}
|
|
141
|
+
if (x.t === "local") return this.#kinds.get(x.n);
|
|
142
|
+
// The other values have no kind that the lowering knows.
|
|
143
|
+
return x.t === "prim" && BOOLEAN_PRIMS.has(x.op) ? "boolean" : undefined;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** This method gives a local the kind `kind`. A value that is not a local keeps its kind. */
|
|
147
|
+
#typed(x: LExpr, kind: PrimitiveKind | undefined): LExpr {
|
|
148
|
+
if (x.t === "local" && kind !== undefined) this.#kinds.set(x.n, kind);
|
|
149
|
+
return x;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* This method checks the kind that the schema declares for a value. A failure is no error of the interpreter, so it
|
|
154
|
+
* stops the program, and tier 1 evaluates it.
|
|
155
|
+
*/
|
|
156
|
+
#declared(b: Block, x: LExpr, kind: PrimitiveKind | undefined, reason: string): LExpr {
|
|
157
|
+
if (kind === undefined) return x;
|
|
158
|
+
b.stmts.push({ t: "check", ok: prim(KIND_CHECKS[kind], x), reason, fail: RETURN });
|
|
159
|
+
return this.#typed(x, kind);
|
|
160
|
+
}
|
|
161
|
+
readonly constants: unknown[] = [];
|
|
162
|
+
readonly #pool = new Map<unknown, number>();
|
|
163
|
+
|
|
164
|
+
/** This method gives the index of the constant of a value. The same value has one constant. A map key does not tell 0 from -0, so -0 has its own. */
|
|
165
|
+
index(value: unknown): number {
|
|
166
|
+
// Stryker disable next-line UnaryOperator: with +0 as the special key, 0 and -0 still get different constants
|
|
167
|
+
const k = Object.is(value, -0) ? NEGATIVE_ZERO : value;
|
|
168
|
+
let i = this.#pool.get(k);
|
|
169
|
+
if (i === undefined) {
|
|
170
|
+
i = this.constants.push(value) - 1;
|
|
171
|
+
this.#pool.set(k, i);
|
|
172
|
+
}
|
|
173
|
+
return i;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** This method gives the constant of a value. */
|
|
177
|
+
constant(value: unknown): LExpr {
|
|
178
|
+
return constant(this.index(value));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** This method gives the number of a new local of a kind. */
|
|
182
|
+
typedLocal(kind: PrimitiveKind): number {
|
|
183
|
+
const n = this.locals++;
|
|
184
|
+
this.#kinds.set(n, kind);
|
|
185
|
+
return n;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** This method gives a new local with a value. */
|
|
189
|
+
value(b: Block, value: LExpr): LExpr {
|
|
190
|
+
const n = this.locals++;
|
|
191
|
+
b.stmts.push({ t: "let", n, value });
|
|
192
|
+
return local(n);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
check(b: Block, ok: LExpr, reason: string): void {
|
|
196
|
+
b.stmts.push({ t: "check", ok, reason, fail: b.fail });
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** This method gives a new local with the value of a call of the runtime, and a check that the value is not `FALLBACK`. */
|
|
200
|
+
checked(b: Block, value: LExpr, reason: string): LExpr {
|
|
201
|
+
const x = this.value(b, value);
|
|
202
|
+
this.check(b, prim("!==", x, FALLBACK_EXPR), reason);
|
|
203
|
+
return x;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** This method adds a failure: the interpreter always gives an error here. */
|
|
207
|
+
fail(b: Block): LExpr {
|
|
208
|
+
b.stmts.push({ t: "fail", fail: b.fail });
|
|
209
|
+
return FALLBACK_EXPR;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
expr(e: Expr, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
213
|
+
// Inside a node of the memo, the value of a key replaces the key. An error of the key is an error at each place.
|
|
214
|
+
const key = this.#keys.get(e);
|
|
215
|
+
if (key !== undefined) {
|
|
216
|
+
if (key.ok !== undefined) this.check(b, key.ok, "a key with a value");
|
|
217
|
+
return key.value;
|
|
218
|
+
}
|
|
219
|
+
const keys = this.#memo.get(e);
|
|
220
|
+
return keys === undefined ? this.#node(e, s, path, b) : this.#memoized(e, keys, s, path, b);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** This method lowers a node itself, without the memo. */
|
|
224
|
+
#node(e: Expr, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
225
|
+
switch (e.tag) {
|
|
226
|
+
case "lit":
|
|
227
|
+
return this.constant(e.value);
|
|
228
|
+
case "ref":
|
|
229
|
+
return this.#ref(e, s, path, b);
|
|
230
|
+
case "var": {
|
|
231
|
+
const bound = s.env.get(e.name);
|
|
232
|
+
// A function is not a value (FN.VALUE): tier 1 gives the error. A name of a loop hides a function with its name.
|
|
233
|
+
if (bound === undefined && s.fns.has(e.name)) return this.fail(b);
|
|
234
|
+
if (bound === undefined) {
|
|
235
|
+
const x = this.checked(b, call("var", local(VARS), this.constant(e.name)), "a variable");
|
|
236
|
+
return this.#declared(b, x, primitiveOf(this.#schema.vars?.[e.name]), "a variable of the schema");
|
|
237
|
+
}
|
|
238
|
+
if (bound.ok !== undefined) this.check(b, bound.ok, "a binding with a value");
|
|
239
|
+
return bound.value;
|
|
240
|
+
}
|
|
241
|
+
case "let":
|
|
242
|
+
return this.expr(e.body, { ...s, ...this.#bind(e, s, path, b) }, [...path, Object.keys(e.bind).length], b);
|
|
243
|
+
// A function is only a binding of let, so the check of the shape stops this node first.
|
|
244
|
+
// Stryker disable next-line all: see the comment above
|
|
245
|
+
/* v8 ignore next -- @preserve */
|
|
246
|
+
case "fn": return this.fail(b);
|
|
247
|
+
case "call":
|
|
248
|
+
return this.#call(e, s, path, b);
|
|
249
|
+
case "rec": {
|
|
250
|
+
const values = Object.values(e.fields).map((x, i) => this.expr(x, s, [...path, i], b));
|
|
251
|
+
return this.value(b, call("rec", this.constant(Object.keys(e.fields)), array(...values)));
|
|
252
|
+
}
|
|
253
|
+
case "app":
|
|
254
|
+
return this.#app(e, s, path, b);
|
|
255
|
+
case "fold":
|
|
256
|
+
return this.#fold(e, s, path, b);
|
|
257
|
+
case "iterate":
|
|
258
|
+
return this.#iterate(e, s, path, b);
|
|
259
|
+
case "each": {
|
|
260
|
+
// A list keeps its error items, and generated code makes no error values. Thus an error item stops the program.
|
|
261
|
+
const keys = this.locals++;
|
|
262
|
+
const values = this.locals++;
|
|
263
|
+
b.stmts.push({ t: "let", n: keys, value: array() }, { t: "let", n: values, value: array() });
|
|
264
|
+
this.#loop(e, s, path, b, () => RETURN, (body, key, v) => {
|
|
265
|
+
body.stmts.push({ t: "push", n: keys, value: key }, { t: "push", n: values, value: v });
|
|
266
|
+
});
|
|
267
|
+
return this.value(b, call("list", local(keys), local(values)));
|
|
268
|
+
}
|
|
269
|
+
case "ext": {
|
|
270
|
+
// An extension can give the error #DEPTH!, which a list op does not skip. Thus an error of an extension stops the program.
|
|
271
|
+
const x = this.value(b, call("ext", this.constant(e.kind), this.constant(e.data), s.space, s.origin, s.focus, this.constant(path)));
|
|
272
|
+
b.stmts.push({ t: "check", ok: prim("!==", x, FALLBACK_EXPR), reason: "an extension", fail: RETURN });
|
|
273
|
+
return x;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* This method gives the value of a node that depends only on the space (§7.2). The memo has an entry for each space.
|
|
280
|
+
* Without the entry of the space, the node evaluates in a block, and an error of it makes an entry without a value.
|
|
281
|
+
* An exception stops the program, so the memo gets no entry for it.
|
|
282
|
+
*/
|
|
283
|
+
#memoized(e: Expr, keys: readonly Expr[], s: Scope, path: readonly number[], b: Block): LExpr {
|
|
284
|
+
const memo: LExpr = { t: "memo", id: this.memos++ };
|
|
285
|
+
// Each key evaluates here, in a block of its own, as a binding does.
|
|
286
|
+
const values = keys.map((k): Bound => this.#attempt(b, (inner) => this.expr(k, s, path, inner)));
|
|
287
|
+
const tuple = array(...values.flatMap((k) => [k.value, flag(k)]));
|
|
288
|
+
const h = this.locals++;
|
|
289
|
+
const outer = this.#keys;
|
|
290
|
+
this.#keys = new Map([...outer, ...keys.map((k, i): [Expr, Bound] => [k, values[i] as Bound])]);
|
|
291
|
+
const compute = block(b.fail);
|
|
292
|
+
const node = this.#attempt(compute, (inner) => this.#node(e, s, path, inner));
|
|
293
|
+
this.#keys = outer;
|
|
294
|
+
compute.stmts.push({ t: "set", n: h, value: prim("memoPut", memo, s.space, tuple, node.value, flag(node)) });
|
|
295
|
+
b.stmts.push({ t: "let", n: h, value: prim("memoGet", memo, s.space, tuple) }, { t: "if", cond: prim("!", prim("isValue", local(h))), yes: compute.stmts, no: [] });
|
|
296
|
+
this.check(b, prim("entryOk", local(h)), "a value of the memo");
|
|
297
|
+
// Each entry of the memo comes from the code of the node, so it has the kind of the value of the node.
|
|
298
|
+
return this.#typed(this.value(b, prim("entryValue", local(h))), this.kindOf(node.value));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* This method evaluates an expression in a block whose errors end the block, as a binding does. It gives the value
|
|
303
|
+
* and the flag that tells if the value exists. Without statements, the expression always has a value, so it gets no flag.
|
|
304
|
+
*/
|
|
305
|
+
#attempt(b: Block, make: (inner: Block) => LExpr): Bound {
|
|
306
|
+
const label = this.labels++;
|
|
307
|
+
const attempt = block({ t: "break", label });
|
|
308
|
+
const value = make(attempt);
|
|
309
|
+
if (attempt.stmts.length === 0) return { value };
|
|
310
|
+
const v = this.locals++;
|
|
311
|
+
const ok = this.locals++;
|
|
312
|
+
this.#typed(local(v), this.kindOf(value));
|
|
313
|
+
attempt.stmts.push({ t: "set", n: v, value }, { t: "set", n: ok, value: bool(true) });
|
|
314
|
+
b.stmts.push({ t: "let", n: ok, value: bool(false) }, { t: "block", label, body: attempt.stmts });
|
|
315
|
+
return { value: local(v), ok: local(ok) };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* This method reads a field, as `ref` of the interpreter: the record of the key, and then each segment of the path.
|
|
320
|
+
* A segment that cannot name a field always gives an error.
|
|
321
|
+
*/
|
|
322
|
+
#ref(e: ExprOf<"ref">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
323
|
+
const record = e.at === undefined || e.at.length === 0 ? s.rec : prim("get", s.space, this.#address(e.at, s, path, 0, b));
|
|
324
|
+
if (!e.path.every(isFieldName)) return this.fail(b);
|
|
325
|
+
const n = this.locals++;
|
|
326
|
+
const x = local(n);
|
|
327
|
+
b.stmts.push({ t: "let", n, value: record });
|
|
328
|
+
this.check(b, prim("isValue", x), "a record");
|
|
329
|
+
for (const seg of e.path) {
|
|
330
|
+
this.check(b, prim("isObject", x), "an object");
|
|
331
|
+
b.stmts.push({ t: "set", n, value: prim("member", x, this.constant(seg)) });
|
|
332
|
+
this.check(b, prim("isValue", x), "a field");
|
|
333
|
+
}
|
|
334
|
+
const [field, ...more] = e.path;
|
|
335
|
+
// Tier 2 knows only the primitive kinds. A value of a domain goes through the dispatch of the runtime.
|
|
336
|
+
return field === undefined || more.length > 0 ? x : this.#declared(b, x, primitiveOf(this.#schema.fields?.[field]), "a field of the schema");
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* This method binds the names of a `let`. The interpreter evaluates each binding in the scope outside the `let`. An
|
|
341
|
+
* error of a binding is an error only where the body reads the name. Thus an error ends the block of the binding, and
|
|
342
|
+
* a flag tells each read of the name that the binding has no value.
|
|
343
|
+
*/
|
|
344
|
+
#bind(e: ExprOf<"let">, s: Scope, path: readonly number[], b: Block): Pick<Scope, "env" | "fns"> {
|
|
345
|
+
const env = new Map(s.env);
|
|
346
|
+
const fns = new Map(s.fns);
|
|
347
|
+
Object.entries(e.bind).forEach(([name, x], i) => {
|
|
348
|
+
// A function evaluates at each call. A binding sees the scope of the let, so a function cannot call itself.
|
|
349
|
+
// A value hides a function of its name, because a read looks at the values first.
|
|
350
|
+
if (x.tag === "fn") {
|
|
351
|
+
fns.set(name, { fn: x, path: [...path, i], scope: s });
|
|
352
|
+
env.delete(name);
|
|
353
|
+
} else {
|
|
354
|
+
env.set(name, this.#attempt(b, (inner) => this.expr(x, s, [...path, i], inner)));
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
return { env, fns };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* This method lowers a call (FN.CALL). The body of the function goes at the place of the call, with the values of the
|
|
362
|
+
* arguments for its parameters. The body sees the names of the place of its binding. A call that is not valid goes
|
|
363
|
+
* to tier 1.
|
|
364
|
+
*/
|
|
365
|
+
#call(e: ExprOf<"call">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
366
|
+
const bound = s.env.has(e.fn) ? undefined : s.fns.get(e.fn);
|
|
367
|
+
if (bound?.fn.params.length !== e.args.length) return this.fail(b);
|
|
368
|
+
if (++this.#calls > MAX_CALLS) throw new Unsupported(`the calls put the bodies of functions in more than ${String(MAX_CALLS)} places`);
|
|
369
|
+
const env = new Map(bound.scope.env);
|
|
370
|
+
bound.fn.params.forEach((p, i) => env.set(p, this.#attempt(b, (inner) => this.expr(e.args[i] as Expr, s, [...path, i], inner))));
|
|
371
|
+
return this.expr(bound.fn.body, { ...s, env, fns: bound.scope.fns }, [...bound.path, 0], b);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
#app(e: ExprOf<"app">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
375
|
+
const { op, args } = e;
|
|
376
|
+
switch (op) {
|
|
377
|
+
case "if":
|
|
378
|
+
return this.#if(args, s, path, b);
|
|
379
|
+
case "and":
|
|
380
|
+
case "or":
|
|
381
|
+
return args.length === 0 ? this.fail(b) : this.#andOr(op === "or", args, 0, s, path, b);
|
|
382
|
+
case "ifError":
|
|
383
|
+
return this.#ifError(args, s, path, b);
|
|
384
|
+
}
|
|
385
|
+
if (isListOp(op)) {
|
|
386
|
+
const target = eachUnderLets(args[0]);
|
|
387
|
+
if (target !== undefined) return this.#fused(op, e, target, s, path, b);
|
|
388
|
+
}
|
|
389
|
+
const values = args.map((x, i) => this.expr(x, s, [...path, i], b));
|
|
390
|
+
const kinds = values.map((x) => this.kindOf(x));
|
|
391
|
+
// A static site checks the kind of its result, so the result has the kind that the op declares (KIND.RETURNS).
|
|
392
|
+
const site = this.#facts.staticSite(op, kinds);
|
|
393
|
+
const x = this.checked(b, { t: "op", name: this.index(op), args: values, kinds }, "an op");
|
|
394
|
+
return this.#typed(x, site?.t === "fn" ? primitiveOf(site.returns) : undefined);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** The condition must be a boolean. Only the branch that the condition selects is evaluated. */
|
|
398
|
+
#if(args: readonly Expr[], s: Scope, path: readonly number[], b: Block): LExpr {
|
|
399
|
+
const [cond, yes, no] = args;
|
|
400
|
+
if (cond === undefined) return this.fail(b);
|
|
401
|
+
const c = this.#boolean(this.expr(cond, s, [...path, 0], b), b);
|
|
402
|
+
const r = this.locals++;
|
|
403
|
+
const kinds: (PrimitiveKind | undefined)[] = [];
|
|
404
|
+
const branch = (x: Expr | undefined, i: number): LStmt[] => {
|
|
405
|
+
const inner = block(b.fail);
|
|
406
|
+
const value = x === undefined ? this.fail(inner) : this.expr(x, s, [...path, i], inner);
|
|
407
|
+
kinds.push(this.kindOf(value));
|
|
408
|
+
inner.stmts.push({ t: "set", n: r, value });
|
|
409
|
+
return inner.stmts;
|
|
410
|
+
};
|
|
411
|
+
b.stmts.push({ t: "if", cond: c, yes: branch(yes, 1), no: branch(no, 2) });
|
|
412
|
+
return this.#typed(local(r), join(kinds[0], kinds[1]));
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** `or` stops at the first `true`, and `and` at the first `false`. Each argument before the stop must be a boolean. */
|
|
416
|
+
#andOr(isOr: boolean, args: readonly Expr[], i: number, s: Scope, path: readonly number[], b: Block, r = this.locals++): LExpr {
|
|
417
|
+
const x = this.#boolean(this.expr(args[i] as Expr, s, [...path, i], b), b);
|
|
418
|
+
this.#typed(local(r), "boolean");
|
|
419
|
+
if (i === args.length - 1) {
|
|
420
|
+
b.stmts.push({ t: "set", n: r, value: x });
|
|
421
|
+
} else {
|
|
422
|
+
const rest = block(b.fail);
|
|
423
|
+
this.#andOr(isOr, args, i + 1, s, path, rest, r);
|
|
424
|
+
const stop: LStmt[] = [{ t: "set", n: r, value: bool(isOr) }];
|
|
425
|
+
b.stmts.push(isOr ? { t: "if", cond: x, yes: stop, no: rest.stmts } : { t: "if", cond: x, yes: rest.stmts, no: stop });
|
|
426
|
+
}
|
|
427
|
+
return local(r);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* The value of the first argument, or else the value of the second argument. An error of the first argument ends its
|
|
432
|
+
* block. An exception, for example #DEPTH!, stops the program, so `ifError` does not hide it (EVAL.DEPTH).
|
|
433
|
+
*/
|
|
434
|
+
#ifError(args: readonly Expr[], s: Scope, path: readonly number[], b: Block): LExpr {
|
|
435
|
+
const [first, second] = args;
|
|
436
|
+
if (first === undefined) return this.fail(b);
|
|
437
|
+
const label = this.labels++;
|
|
438
|
+
const r = this.locals++;
|
|
439
|
+
const ok = this.locals++;
|
|
440
|
+
const attempt = block({ t: "break", label });
|
|
441
|
+
const value = this.expr(first, s, [...path, 0], attempt);
|
|
442
|
+
attempt.stmts.push({ t: "set", n: r, value }, { t: "set", n: ok, value: bool(true) });
|
|
443
|
+
const otherwise = block(b.fail);
|
|
444
|
+
const fallback = second === undefined ? this.fail(otherwise) : this.expr(second, s, [...path, 1], otherwise);
|
|
445
|
+
otherwise.stmts.push({ t: "set", n: r, value: fallback });
|
|
446
|
+
b.stmts.push({ t: "let", n: ok, value: bool(false) }, { t: "block", label, body: attempt.stmts }, { t: "if", cond: prim("!", local(ok)), yes: otherwise.stmts, no: [] });
|
|
447
|
+
return this.#typed(local(r), join(this.kindOf(value), this.kindOf(fallback)));
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/** A value of a known kind needs no check. */
|
|
451
|
+
#boolean(x: LExpr, b: Block): LExpr {
|
|
452
|
+
if (this.kindOf(x) !== "boolean") this.check(b, prim("isBoolean", x), "a boolean");
|
|
453
|
+
return x;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
#number(x: LExpr, b: Block): LExpr {
|
|
457
|
+
if (this.kindOf(x) !== "number") this.check(b, prim("isNumber", x), "a number");
|
|
458
|
+
return x;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* This method lowers a list op over an `each` into one loop. Like the interpreter, the loop skips an error item, and
|
|
463
|
+
* with the option `strict` the list op fails at an error item. `errors` gives the error values, so an error item of
|
|
464
|
+
* `errors` stops the program.
|
|
465
|
+
*/
|
|
466
|
+
#fused(op: ListOp, e: ExprOf<"app">, target: ExprOf<"each">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
467
|
+
// The `let` expressions above the `each`, from the outside in.
|
|
468
|
+
let inner = s;
|
|
469
|
+
let at = [...path, 0];
|
|
470
|
+
for (let x = e.args[0] as Expr; x.tag === "let"; x = x.body) {
|
|
471
|
+
inner = { ...inner, ...this.#bind(x, inner, at, b) };
|
|
472
|
+
at = [...at, Object.keys(x.bind).length];
|
|
473
|
+
}
|
|
474
|
+
const rest = e.args.slice(1).map((x, i) => this.expr(x, s, [...path, i + 1], b));
|
|
475
|
+
const options = op === "reduce" ? rest[1] : rest[0];
|
|
476
|
+
const strict = options === undefined ? undefined : this.value(b, prim("===", call("opts", options), bool(true)));
|
|
477
|
+
const name = rest[0];
|
|
478
|
+
if (op === "reduce") {
|
|
479
|
+
if (name === undefined) return this.fail(b);
|
|
480
|
+
this.check(b, prim("isString", name), "an op name");
|
|
481
|
+
}
|
|
482
|
+
const items = (label: number): Fail => {
|
|
483
|
+
if (op === "errors") return RETURN;
|
|
484
|
+
return strict === undefined ? { t: "continue", label } : { t: "continue", label, strict: { cond: strict, else: b.fail } };
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
const acc = this.locals++;
|
|
488
|
+
const n = this.typedLocal("number");
|
|
489
|
+
const init = (value: LExpr): void => {
|
|
490
|
+
b.stmts.push({ t: "let", n: acc, value });
|
|
491
|
+
};
|
|
492
|
+
b.stmts.push({ t: "let", n, value: num(0) });
|
|
493
|
+
const count = (body: Block): void => {
|
|
494
|
+
body.stmts.push({ t: "set", n, value: prim("+", local(n), num(1)) });
|
|
495
|
+
};
|
|
496
|
+
// A check of the list op itself is in the loop, but its failure is a failure of the list op.
|
|
497
|
+
const opCheck = (body: Block): Block => ({ stmts: body.stmts, fail: b.fail });
|
|
498
|
+
const firstThen = (body: Block, v: LExpr, then: (later: Block) => LExpr): void => {
|
|
499
|
+
const later = block(b.fail);
|
|
500
|
+
later.stmts.push({ t: "set", n: acc, value: then(later) });
|
|
501
|
+
body.stmts.push({ t: "if", cond: prim("===", local(n), num(0)), yes: [{ t: "set", n: acc, value: v }], no: later.stmts });
|
|
502
|
+
};
|
|
503
|
+
const nonEmpty = (): void => {
|
|
504
|
+
this.check(b, prim("!==", local(n), num(0)), "a list that is not empty");
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
switch (op) {
|
|
508
|
+
case "count":
|
|
509
|
+
this.#loop(target, inner, at, b, items, (body) => count(body));
|
|
510
|
+
return local(n);
|
|
511
|
+
case "sum":
|
|
512
|
+
case "mean":
|
|
513
|
+
init(num(0));
|
|
514
|
+
this.#loop(target, inner, at, b, items, (body, _key, v) => {
|
|
515
|
+
body.stmts.push({ t: "set", n: acc, value: prim("+", local(acc), this.#number(v, opCheck(body))) });
|
|
516
|
+
count(body);
|
|
517
|
+
});
|
|
518
|
+
if (op === "sum") return this.#typed(this.checked(b, call("result", local(acc)), "a finite sum"), "number");
|
|
519
|
+
nonEmpty();
|
|
520
|
+
return this.#typed(this.checked(b, call("result", prim("/", local(acc), local(n))), "a finite mean"), "number");
|
|
521
|
+
case "min":
|
|
522
|
+
case "max":
|
|
523
|
+
this.#loop(target, inner, at, b, items, (body, _key, v) => {
|
|
524
|
+
this.#number(v, opCheck(body));
|
|
525
|
+
firstThen(body, v, () => prim(op, local(acc), v));
|
|
526
|
+
count(body);
|
|
527
|
+
});
|
|
528
|
+
nonEmpty();
|
|
529
|
+
return this.#typed(local(acc), "number");
|
|
530
|
+
case "any":
|
|
531
|
+
case "all":
|
|
532
|
+
case "none":
|
|
533
|
+
init(bool(op === "all"));
|
|
534
|
+
this.#loop(target, inner, at, b, items, (body, _key, v) => {
|
|
535
|
+
body.stmts.push({ t: "set", n: acc, value: prim(op === "all" ? "&&" : "||", local(acc), this.#boolean(v, opCheck(body))) });
|
|
536
|
+
});
|
|
537
|
+
return op === "none" ? prim("!", local(acc)) : this.#typed(local(acc), "boolean");
|
|
538
|
+
case "first":
|
|
539
|
+
this.#loop(target, inner, at, b, items, (body, _key, v) => {
|
|
540
|
+
firstThen(body, v, () => local(acc));
|
|
541
|
+
count(body);
|
|
542
|
+
});
|
|
543
|
+
nonEmpty();
|
|
544
|
+
return local(acc);
|
|
545
|
+
case "values":
|
|
546
|
+
case "keys":
|
|
547
|
+
init(array());
|
|
548
|
+
this.#loop(target, inner, at, b, items, (body, key, v) => {
|
|
549
|
+
body.stmts.push({ t: "push", n: acc, value: op === "keys" ? key : v });
|
|
550
|
+
});
|
|
551
|
+
return this.value(b, call("frozen", local(acc)));
|
|
552
|
+
case "errors":
|
|
553
|
+
this.#loop(target, inner, at, b, items, () => undefined);
|
|
554
|
+
return this.value(b, call("frozen", array()));
|
|
555
|
+
case "reduce":
|
|
556
|
+
this.#loop(target, inner, at, b, items, (body, _key, v) => {
|
|
557
|
+
firstThen(body, v, (later) => this.checked(later, call("step", name as LExpr, local(acc), v), "a step of reduce"));
|
|
558
|
+
count(body);
|
|
559
|
+
});
|
|
560
|
+
nonEmpty();
|
|
561
|
+
return local(acc);
|
|
562
|
+
// listReason stops a program with these ops before the lowering, so this case is defensive.
|
|
563
|
+
case "sortBy":
|
|
564
|
+
case "take":
|
|
565
|
+
case "nth":
|
|
566
|
+
/* v8 ignore next -- @preserve */
|
|
567
|
+
// Stryker disable next-line all: listReason stops these ops before the lowering
|
|
568
|
+
throw new Unsupported(`the list op "${op}"`);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* This method lowers the loop over the targets of an axis. The tests of `where` and the body evaluate at each target
|
|
574
|
+
* in a block whose failure is `items(label)`. Then `onItem` adds the statements for the value of the body.
|
|
575
|
+
*/
|
|
576
|
+
/**
|
|
577
|
+
* This method gives the key of an address. The expressions of its computed moves evaluate first, at the position of
|
|
578
|
+
* the node, and their child indexes start at `first` (NAV.COMPUTED).
|
|
579
|
+
*/
|
|
580
|
+
#address(at: Addr, s: Scope, path: readonly number[], first: number, b: Block): LExpr {
|
|
581
|
+
const values = addrExprs(at).map((x, i) => this.expr(x, s, [...path, first + i], b));
|
|
582
|
+
const args = [s.space, s.origin, s.focus, this.constant(at), ...(values.length === 0 ? [] : [array(...values)])];
|
|
583
|
+
return this.checked(b, call("addr", ...args), "an address");
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
#loop(e: ExprOf<"each">, s: Scope, path: readonly number[], b: Block, items: (label: number) => Fail, onItem: (body: Block, key: LExpr, value: LExpr) => void): void {
|
|
587
|
+
this.#targetLoop(e.axis, e.at, s, path, 1, b, items, (body, key, at) => {
|
|
588
|
+
onItem(body, key, this.expr(e.body, at, [...path, 0], body));
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* This method lowers the loop over the targets of an axis with the address `addr`. The tests of `where` have the child
|
|
594
|
+
* indexes from `firstTest`, and the expressions of the address come after them. `each` adds the statements of a target.
|
|
595
|
+
*/
|
|
596
|
+
#targetLoop(
|
|
597
|
+
axis: Axis,
|
|
598
|
+
addr: Addr | undefined,
|
|
599
|
+
s: Scope,
|
|
600
|
+
path: readonly number[],
|
|
601
|
+
firstTest: number,
|
|
602
|
+
b: Block,
|
|
603
|
+
items: (label: number) => Fail,
|
|
604
|
+
each: (body: Block, key: LExpr, at: Scope) => void,
|
|
605
|
+
): void {
|
|
606
|
+
const from = addr === undefined || addr.length === 0 ? s.focus : this.#address(addr, s, path, firstTest + whereTests(axis).length, b);
|
|
607
|
+
const base = baseAxis(axis);
|
|
608
|
+
let space = s.space;
|
|
609
|
+
let over: LExpr;
|
|
610
|
+
let skip: LExpr | undefined;
|
|
611
|
+
// The index of the record of each target: the index of the loop, or else the index that the axis gives.
|
|
612
|
+
let idx: LExpr | undefined;
|
|
613
|
+
if (base.t === "table") {
|
|
614
|
+
// The axis table moves the evaluation to the space of its table, and there the origin is the target.
|
|
615
|
+
space = this.checked(b, call("table", local(TABLES), this.constant(base.name)), "a table");
|
|
616
|
+
over = call("keys", space);
|
|
617
|
+
} else if (base.t === "all" || base.t === "others") {
|
|
618
|
+
over = call("keys", s.space);
|
|
619
|
+
if (base.t === "others") skip = from;
|
|
620
|
+
} else {
|
|
621
|
+
const targets = this.checked(b, { t: "targets", axis: this.index(base), args: [s.space, s.origin, from] }, "the targets of an axis");
|
|
622
|
+
over = prim("keysOf", targets);
|
|
623
|
+
idx = this.value(b, prim("idxOf", targets));
|
|
624
|
+
}
|
|
625
|
+
const records = this.value(b, prim("records", space));
|
|
626
|
+
const label = this.labels++;
|
|
627
|
+
const array_ = this.locals++;
|
|
628
|
+
const index = this.locals++;
|
|
629
|
+
const item = this.locals++;
|
|
630
|
+
const key = local(item);
|
|
631
|
+
const body = block(items(label));
|
|
632
|
+
if (skip !== undefined) body.stmts.push({ t: "if", cond: prim("===", key, skip), yes: [{ t: "next" }], no: [] });
|
|
633
|
+
const slot = idx === undefined ? local(index) : prim("member", idx, local(index));
|
|
634
|
+
const at: Scope = { space, origin: base.t === "table" ? key : s.origin, focus: key, env: s.env, fns: s.fns, rec: prim("recordAt", records, slot, space, key) };
|
|
635
|
+
whereTests(axis).forEach((test, i) => {
|
|
636
|
+
const ok = this.#boolean(this.expr(test, at, [...path, firstTest + i], body), body);
|
|
637
|
+
body.stmts.push({ t: "if", cond: prim("!", ok), yes: [{ t: "next" }], no: [] });
|
|
638
|
+
});
|
|
639
|
+
each(body, key, at);
|
|
640
|
+
b.stmts.push({ t: "loop", label, item, index, array: array_, over, body: body.stmts });
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/** This method gives a local for each name of a loop, with its first value at the node (LOOP.NAMES). */
|
|
644
|
+
#loopNames(init: Names, s: Scope, path: readonly number[], b: Block): { readonly env: ReadonlyMap<string, Bound>; readonly locals: ReadonlyMap<string, number> } {
|
|
645
|
+
const env = new Map(s.env);
|
|
646
|
+
const locals = new Map<string, number>();
|
|
647
|
+
Object.entries(init).forEach(([name, x], i) => {
|
|
648
|
+
const value = this.expr(x, s, [...path, i], b);
|
|
649
|
+
const n = this.locals++;
|
|
650
|
+
b.stmts.push({ t: "let", n, value });
|
|
651
|
+
this.#typed(local(n), this.kindOf(value));
|
|
652
|
+
env.set(name, { value: local(n) });
|
|
653
|
+
locals.set(name, n);
|
|
654
|
+
});
|
|
655
|
+
return { env, locals };
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* This method lowers one step of a loop: each new value first, and then each name gets its value. A name keeps the
|
|
660
|
+
* kind of its first value, so a new value of another kind stops the program, and tier 1 evaluates it.
|
|
661
|
+
*/
|
|
662
|
+
#loopStep(step: Names, s: Scope, path: readonly number[], first: number, b: Block, locals: ReadonlyMap<string, number>): void {
|
|
663
|
+
const values = Object.entries(step).map(([name, x], i): [number, LExpr] => {
|
|
664
|
+
const n = locals.get(name) as number;
|
|
665
|
+
const value = this.value(b, this.expr(x, s, [...path, first + i], b));
|
|
666
|
+
return [n, this.#declared(b, value, this.kindOf(local(n)), "a value of the kind of a name of a loop")];
|
|
667
|
+
});
|
|
668
|
+
for (const [n, value] of values) b.stmts.push({ t: "set", n, value });
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/** This method lowers an iteration (ITERATE.LIMIT, ITERATE.UNTIL): a repeated block with a count of its steps. */
|
|
672
|
+
#iterate(e: ExprOf<"iterate">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
673
|
+
const first = Object.keys(e.init).length;
|
|
674
|
+
const tests = first + Object.keys(e.step).length;
|
|
675
|
+
const { env, locals } = this.#loopNames(e.init, s, path, b);
|
|
676
|
+
const inner: Scope = { ...s, env };
|
|
677
|
+
const label = this.labels++;
|
|
678
|
+
const k = this.typedLocal("number");
|
|
679
|
+
b.stmts.push({ t: "let", n: k, value: num(0) });
|
|
680
|
+
const body = block(b.fail);
|
|
681
|
+
if (e.until !== undefined) {
|
|
682
|
+
const stop = this.#boolean(this.expr(e.until, inner, [...path, tests], body), body);
|
|
683
|
+
body.stmts.push({ t: "check", ok: prim("!", stop), reason: "the test until", fail: { t: "break", label } });
|
|
684
|
+
}
|
|
685
|
+
// At the limit, an iteration without a test ends, and an iteration with a test fails.
|
|
686
|
+
body.stmts.push({ t: "check", ok: prim("!==", local(k), num(e.limit)), reason: "the limit of iterate", fail: e.until === undefined ? { t: "break", label } : b.fail });
|
|
687
|
+
this.#loopStep(e.step, inner, path, first, body, locals);
|
|
688
|
+
body.stmts.push({ t: "set", n: k, value: prim("+", local(k), num(1)) });
|
|
689
|
+
b.stmts.push({ t: "repeat", label, body: body.stmts });
|
|
690
|
+
return this.expr(e.result, inner, [...path, tests + (e.until === undefined ? 0 : 1)], b);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/** This method lowers a fold (FOLD.ORDER): the loop over the targets, with a step at each target. */
|
|
694
|
+
#fold(e: ExprOf<"fold">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
695
|
+
const first = Object.keys(e.init).length;
|
|
696
|
+
const tests = first + Object.keys(e.step).length;
|
|
697
|
+
const { env, locals } = this.#loopNames(e.init, s, path, b);
|
|
698
|
+
// An error of a test or of a step is the error of the fold.
|
|
699
|
+
this.#targetLoop(e.axis, e.at, s, path, tests, b, () => b.fail, (body, _key, at) => {
|
|
700
|
+
this.#loopStep(e.step, { ...at, env }, path, first, body, locals);
|
|
701
|
+
});
|
|
702
|
+
return this.expr(e.result, { ...s, env }, [...path, tests + whereTests(e.axis).length + addrExprs(e.at).length], b);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
const NEGATIVE_ZERO = Symbol("-0");
|
|
707
|
+
|
|
708
|
+
/** This function gives the flag of a bound value: its local, or `true` for a value without a flag. */
|
|
709
|
+
// Stryker disable next-line BooleanLiteral: a value without a flag always exists, so each constant gives the same entries
|
|
710
|
+
const flag = (b: Bound): LExpr => b.ok ?? bool(true);
|