@mark1russell7/vex-compiler 0.7.0 → 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/build/index.d.ts.map +1 -1
- package/build/index.js +482 -61
- package/build/index.js.map +1 -1
- package/package.json +4 -4
- package/src/facts.ts +25 -1
- package/src/lower.ts +59 -11
- package/src/memo.ts +18 -6
- package/src/module.ts +332 -46
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/vex-compiler",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.8.0",
|
|
5
5
|
"description": "The Vex compiler: generated JavaScript for Vex programs, at run time and at build time, with the results of the interpreter.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"vex",
|
|
@@ -43,15 +43,15 @@
|
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@mark1russell7/cue": "github:mark1russell7/cue",
|
|
46
|
-
"@mark1russell7/vex-domains": "0.
|
|
47
|
-
"@mark1russell7/vex-testkit": "0.
|
|
46
|
+
"@mark1russell7/vex-domains": "0.8.0",
|
|
47
|
+
"@mark1russell7/vex-testkit": "0.8.0",
|
|
48
48
|
"@types/node": "^26.6.4",
|
|
49
49
|
"fast-check": "^4.10.2",
|
|
50
50
|
"typescript": "^7.0.2",
|
|
51
51
|
"vitest": "^5.0.3"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@mark1russell7/vex": "0.
|
|
54
|
+
"@mark1russell7/vex": "0.8.0"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "tsdown",
|
package/src/facts.ts
CHANGED
|
@@ -3,7 +3,31 @@
|
|
|
3
3
|
* whose op the interpreter takes first, and the kinds and the lifts of its parameters. The facts come from the op
|
|
4
4
|
* tables, so the same options always give the same facts, and the same source.
|
|
5
5
|
*/
|
|
6
|
-
import { isListOp, isOpName, liftsAt, type AnyDomain, type ExtHandler, type FreeFn, type OpSpec, type ParamKind, type PrimitiveKind, type ReturnKind } from "@mark1russell7/vex";
|
|
6
|
+
import { children, isListOp, isOpName, liftsAt, listOf, type AnyDomain, type Expr, type ExtHandler, type FreeFn, type OpSpec, type ParamKind, type PrimitiveKind, type ReturnKind } from "@mark1russell7/vex";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The most bodies of functions that tier 2 puts at the calls of one program. A function that calls another function
|
|
10
|
+
* twice doubles its body, so a chain of such functions grows fast. A module prints each function once.
|
|
11
|
+
*/
|
|
12
|
+
export const MAX_CALLS = 10_000;
|
|
13
|
+
|
|
14
|
+
/** The list ops that give a list or read a position of a list (LIST.SORT, LIST.TAKE, LIST.NTH). */
|
|
15
|
+
const ORDER_OPS: ReadonlySet<string> = new Set(["sortBy", "take", "nth"]);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* This function gives the reason that tier 2 cannot take a program with lists as values. The reason is the axis `list`
|
|
19
|
+
* or an op on the order of a list. Tier 1 evaluates such a program. It uses a stack, not recursion.
|
|
20
|
+
*/
|
|
21
|
+
export function listReason(expr: Expr): string | undefined {
|
|
22
|
+
const todo: Expr[] = [expr];
|
|
23
|
+
for (let x = todo.pop(); x !== undefined; x = todo.pop()) {
|
|
24
|
+
if ((x.tag === "each" || x.tag === "fold") && listOf(x.axis) !== undefined) return 'the program has the axis "list", and tier 2 does not have lists as values yet';
|
|
25
|
+
// Stryker disable next-line ConditionalExpression: a node that is not an app has no field op, so the set does not have it
|
|
26
|
+
if (x.tag === "app" && ORDER_OPS.has(x.op)) return `the program has the list op "${x.op}", and tier 2 does not have lists as values yet`;
|
|
27
|
+
todo.push(...children(x));
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
7
31
|
|
|
8
32
|
/** The function of an op of a domain, or a method. */
|
|
9
33
|
export type Method = (...args: unknown[]) => unknown;
|
package/src/lower.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* Each other error stops the program with `FALLBACK`.
|
|
8
8
|
*/
|
|
9
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, primitiveOf, type Facts, type Schema } from "./facts.ts";
|
|
10
|
+
import { factsOf, listReason, MAX_CALLS, primitiveOf, type Facts, type Schema } from "./facts.ts";
|
|
11
11
|
import { memoNodes } from "./memo.ts";
|
|
12
12
|
import {
|
|
13
13
|
array,
|
|
@@ -39,12 +39,21 @@ interface Bound {
|
|
|
39
39
|
readonly ok?: LExpr;
|
|
40
40
|
}
|
|
41
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
|
+
|
|
42
49
|
/** The facts of the lexical position of a node: its space, its origin, its focus and its bound names. */
|
|
43
50
|
interface Scope {
|
|
44
51
|
readonly space: LExpr;
|
|
45
52
|
readonly origin: LExpr;
|
|
46
53
|
readonly focus: LExpr;
|
|
47
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>;
|
|
48
57
|
/** The record at the focus. In a loop, it reads the record by its index (SPACE.RECORDS). */
|
|
49
58
|
readonly rec: LExpr;
|
|
50
59
|
}
|
|
@@ -92,10 +101,12 @@ export function lower(expr: Expr, facts: Facts = factsOf({}), schema: Schema = {
|
|
|
92
101
|
const shape = shapeError(expr);
|
|
93
102
|
if (shape !== undefined) throw new Unsupported(shape);
|
|
94
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);
|
|
95
106
|
const l = new Lowerer(memoNodes(expr), facts, schema);
|
|
96
107
|
const b = block(RETURN);
|
|
97
108
|
l.check(b, call("has", local(SPACE), local(ORIGIN)), "the origin is a key of the space");
|
|
98
|
-
const top: Scope = { space: local(SPACE), origin: local(ORIGIN), focus: local(ORIGIN), env: new Map(), rec: prim("get", local(SPACE), local(ORIGIN)) };
|
|
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)) };
|
|
99
110
|
const value = l.expr(expr, top, [], b);
|
|
100
111
|
b.stmts.push({ t: "return", value });
|
|
101
112
|
return { body: b.stmts, locals: l.locals, constants: l.constants };
|
|
@@ -105,6 +116,8 @@ class Lowerer {
|
|
|
105
116
|
locals = FIXED_LOCALS;
|
|
106
117
|
labels = 0;
|
|
107
118
|
memos = 0;
|
|
119
|
+
/** The number of bodies of functions that the lowering put at calls (MAX_CALLS). */
|
|
120
|
+
#calls = 0;
|
|
108
121
|
readonly #memo: ReadonlyMap<Expr, readonly Expr[]>;
|
|
109
122
|
/** The keys of the nodes of the memo whose code the lowering makes, with their values. */
|
|
110
123
|
#keys: ReadonlyMap<Expr, Bound> = new Map();
|
|
@@ -216,6 +229,8 @@ class Lowerer {
|
|
|
216
229
|
return this.#ref(e, s, path, b);
|
|
217
230
|
case "var": {
|
|
218
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);
|
|
219
234
|
if (bound === undefined) {
|
|
220
235
|
const x = this.checked(b, call("var", local(VARS), this.constant(e.name)), "a variable");
|
|
221
236
|
return this.#declared(b, x, primitiveOf(this.#schema.vars?.[e.name]), "a variable of the schema");
|
|
@@ -223,10 +238,14 @@ class Lowerer {
|
|
|
223
238
|
if (bound.ok !== undefined) this.check(b, bound.ok, "a binding with a value");
|
|
224
239
|
return bound.value;
|
|
225
240
|
}
|
|
226
|
-
case "let":
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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);
|
|
230
249
|
case "rec": {
|
|
231
250
|
const values = Object.values(e.fields).map((x, i) => this.expr(x, s, [...path, i], b));
|
|
232
251
|
return this.value(b, call("rec", this.constant(Object.keys(e.fields)), array(...values)));
|
|
@@ -322,12 +341,34 @@ class Lowerer {
|
|
|
322
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
|
|
323
342
|
* a flag tells each read of the name that the binding has no value.
|
|
324
343
|
*/
|
|
325
|
-
#bind(e: ExprOf<"let">, s: Scope, path: readonly number[], b: Block):
|
|
344
|
+
#bind(e: ExprOf<"let">, s: Scope, path: readonly number[], b: Block): Pick<Scope, "env" | "fns"> {
|
|
326
345
|
const env = new Map(s.env);
|
|
346
|
+
const fns = new Map(s.fns);
|
|
327
347
|
Object.entries(e.bind).forEach(([name, x], i) => {
|
|
328
|
-
|
|
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
|
+
}
|
|
329
356
|
});
|
|
330
|
-
return env;
|
|
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);
|
|
331
372
|
}
|
|
332
373
|
|
|
333
374
|
#app(e: ExprOf<"app">, s: Scope, path: readonly number[], b: Block): LExpr {
|
|
@@ -427,7 +468,7 @@ class Lowerer {
|
|
|
427
468
|
let inner = s;
|
|
428
469
|
let at = [...path, 0];
|
|
429
470
|
for (let x = e.args[0] as Expr; x.tag === "let"; x = x.body) {
|
|
430
|
-
inner = { ...inner,
|
|
471
|
+
inner = { ...inner, ...this.#bind(x, inner, at, b) };
|
|
431
472
|
at = [...at, Object.keys(x.bind).length];
|
|
432
473
|
}
|
|
433
474
|
const rest = e.args.slice(1).map((x, i) => this.expr(x, s, [...path, i + 1], b));
|
|
@@ -518,6 +559,13 @@ class Lowerer {
|
|
|
518
559
|
});
|
|
519
560
|
nonEmpty();
|
|
520
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}"`);
|
|
521
569
|
}
|
|
522
570
|
}
|
|
523
571
|
|
|
@@ -583,7 +631,7 @@ class Lowerer {
|
|
|
583
631
|
const body = block(items(label));
|
|
584
632
|
if (skip !== undefined) body.stmts.push({ t: "if", cond: prim("===", key, skip), yes: [{ t: "next" }], no: [] });
|
|
585
633
|
const slot = idx === undefined ? local(index) : prim("member", idx, local(index));
|
|
586
|
-
const at: Scope = { space, origin: base.t === "table" ? key : s.origin, focus: key, env: s.env, rec: prim("recordAt", records, slot, space, key) };
|
|
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) };
|
|
587
635
|
whereTests(axis).forEach((test, i) => {
|
|
588
636
|
const ok = this.#boolean(this.expr(test, at, [...path, firstTest + i], body), body);
|
|
589
637
|
body.stmts.push({ t: "if", cond: prim("!", ok), yes: [{ t: "next" }], no: [] });
|
package/src/memo.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* A key reads the origin or a name from outside the node, and nothing that the node changes. Inside the node, the
|
|
11
11
|
* value of the key replaces the key. Thus the value of the node depends only on the space and the values of the keys.
|
|
12
12
|
*/
|
|
13
|
-
import { addrExprs, isListOp, type Addr, type Axis, type Expr } from "@mark1russell7/vex";
|
|
13
|
+
import { addrExprs, isListOp, listOf, type Addr, type Axis, type Expr } from "@mark1russell7/vex";
|
|
14
14
|
|
|
15
15
|
/** A read of the focus of the expression, outside its own loops. */
|
|
16
16
|
const FOCUS = 1;
|
|
@@ -68,9 +68,15 @@ function reads(e: Expr, outer: ReadonlySet<string>, inner: ReadonlySet<string>,
|
|
|
68
68
|
case "var":
|
|
69
69
|
return inner.has(e.name) ? 0 : outer.has(e.name) ? INTERNAL : inLoop(FREE, top);
|
|
70
70
|
case "ext":
|
|
71
|
-
// A loop of names is not in the memo yet, so a node with one is not either.
|
|
71
|
+
// A loop of names is not in the memo yet, so a node with one is not either. A call reads what its body reads, and a
|
|
72
|
+
// function reads nothing where it is bound, but the memo does not follow them yet (FN.CALL).
|
|
72
73
|
case "fold":
|
|
73
74
|
case "iterate":
|
|
75
|
+
// A let with a function and no call has the value of its body, and a call is opaque, so this case only keeps the
|
|
76
|
+
// memo simple.
|
|
77
|
+
// Stryker disable next-line StringLiteral: see the comment above
|
|
78
|
+
case "fn":
|
|
79
|
+
case "call":
|
|
74
80
|
return OPAQUE;
|
|
75
81
|
case "ref":
|
|
76
82
|
// The expressions of computed moves evaluate at the position of the node (NAV.COMPUTED).
|
|
@@ -86,8 +92,8 @@ function reads(e: Expr, outer: ReadonlySet<string>, inner: ReadonlySet<string>,
|
|
|
86
92
|
case "each": {
|
|
87
93
|
const { base, tests } = parts(e.axis);
|
|
88
94
|
// The targets of `all` do not depend on the focus, but its address can fail (V-051). Each other axis starts at its
|
|
89
|
-
// address.
|
|
90
|
-
const targets = base.t === "table" ? OPAQUE : base.t === "all" && (e.at ?? []).length === 0 ? 0 : addressReads(e.at, top);
|
|
95
|
+
// address. The targets of the axis list are the items of a value, and the memo does not follow them yet (AXIS.LIST).
|
|
96
|
+
const targets = base.t === "table" || listOf(e.axis) !== undefined ? OPAQUE : base.t === "all" && (e.at ?? []).length === 0 ? 0 : addressReads(e.at, top);
|
|
91
97
|
const at = addrExprs(e.at).reduce((r, x) => r | reads(x, outer, inner, top), targets);
|
|
92
98
|
return [e.body, ...tests].reduce((r, x) => r | reads(x, outer, inner, false), at);
|
|
93
99
|
}
|
|
@@ -127,8 +133,9 @@ function keysOf(e: Expr, top: boolean, outer: ReadonlySet<string>, pure: Set<Exp
|
|
|
127
133
|
case "each": {
|
|
128
134
|
const { base, tests } = parts(e.axis);
|
|
129
135
|
// The targets of a loop must not depend on the origin: a set of targets is not a value of a key. The address of
|
|
130
|
-
// `all` can fail, so it must not depend on the origin either.
|
|
131
|
-
|
|
136
|
+
// `all` can fail, so it must not depend on the origin either. The targets of the axis list are the items of a
|
|
137
|
+
// value, and the memo does not follow them yet (AXIS.LIST).
|
|
138
|
+
if (base.t === "table" || listOf(e.axis) !== undefined || ((base.t !== "all" || (e.at ?? []).length > 0) && addressReads(e.at, top) !== 0)) return undefined;
|
|
132
139
|
// The expressions of computed moves give the targets too, so they must read nothing.
|
|
133
140
|
if (addrExprs(e.at).some((x) => reads(x, outer, new Set(), top) !== 0)) return undefined;
|
|
134
141
|
return all([e.body, ...tests].map((x) => keysOf(x, false, outer, pure)));
|
|
@@ -142,6 +149,8 @@ function keysOf(e: Expr, top: boolean, outer: ReadonlySet<string>, pure: Set<Exp
|
|
|
142
149
|
case "ext":
|
|
143
150
|
case "fold":
|
|
144
151
|
case "iterate":
|
|
152
|
+
case "fn":
|
|
153
|
+
case "call":
|
|
145
154
|
return undefined;
|
|
146
155
|
// Stryker restore StringLiteral,ConditionalExpression
|
|
147
156
|
}
|
|
@@ -168,6 +177,9 @@ function loopOutside(e: Expr, keys: ReadonlySet<Expr>): boolean {
|
|
|
168
177
|
case "lit":
|
|
169
178
|
case "var":
|
|
170
179
|
case "ext":
|
|
180
|
+
// A node with a function or a call is opaque (reads), so it is not a node of the memo.
|
|
181
|
+
case "fn":
|
|
182
|
+
case "call":
|
|
171
183
|
return false;
|
|
172
184
|
// Stryker restore StringLiteral,ConditionalExpression
|
|
173
185
|
}
|