@mark1russell7/vex-compiler 0.9.0 → 0.10.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/README.md +2 -0
- package/build/index.d.ts +10 -8
- package/build/index.d.ts.map +1 -1
- package/build/index.js +45 -12
- package/build/index.js.map +1 -1
- package/package.json +4 -4
- package/src/facts.ts +2 -2
- package/src/lower.ts +4 -0
- package/src/module.ts +11 -2
- package/src/tiers.ts +15 -9
package/README.md
CHANGED
|
@@ -65,6 +65,8 @@ The source has no name and no literal of the program. It reads each one from a l
|
|
|
65
65
|
| `generate(expr, options)` | The loop IR and the source of a program for the options, or the reason that it has none |
|
|
66
66
|
| `load(generated)` | The function of tier 2. It gives the value, or `FALLBACK`. |
|
|
67
67
|
|
|
68
|
+
`tiered`, `generate` and `emitModule` take an expression or a document of coeval, as `toDocument` of `@mark1russell7/vex` gives it.
|
|
69
|
+
|
|
68
70
|
## Speed
|
|
69
71
|
|
|
70
72
|
The speed gate compares tier 2 with hand-written loops. On 2026-10-13, the nearest other box was 1.0 to 1.1 times the loops, and the Game of Life was 1.05 to 1.13 times. Tier 1 was 80 and 120 times. The plan of the compiler is in [`docs/COMPILER.md`](https://github.com/mark1russell7/vex/blob/main/docs/COMPILER.md).
|
package/build/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Addr, AnyDomain, Axis, CompileOptions, Expr, ExtHandler, FreeFn, PrimitiveKind, Program, Space } from "@mark1russell7/vex";
|
|
1
|
+
import { Addr, AnyDomain, Axis, CompileOptions, Envelope, Expr, ExtHandler, FreeFn, PrimitiveKind, Program, Space } from "@mark1russell7/vex";
|
|
2
2
|
//#region src/facts.d.ts
|
|
3
3
|
/** The function of an op of a domain, or a method. */
|
|
4
4
|
type Method = (...args: unknown[]) => unknown;
|
|
@@ -166,10 +166,10 @@ type Generated = {
|
|
|
166
166
|
/** A function of tier 2. It gives the value of the program, or `FALLBACK`. */
|
|
167
167
|
type Tier2 = (space: Space, origin: string, vars: Readonly<Record<string, unknown>> | undefined, tables: Readonly<Record<string, Space>> | undefined) => unknown;
|
|
168
168
|
/**
|
|
169
|
-
* This function lowers an expression and prints its source for the options. The op tables of
|
|
170
|
-
* fast path of each op site. It is pure, so a build can use it.
|
|
169
|
+
* This function lowers an expression or a document and prints its source for the options (DOC.ENTRY). The op tables of
|
|
170
|
+
* the domains give the fast path of each op site. It is pure, so a build can use it.
|
|
171
171
|
*/
|
|
172
|
-
export declare function generate(
|
|
172
|
+
export declare function generate(source: Expr | Envelope, options?: GenerateOptions): Generated;
|
|
173
173
|
/**
|
|
174
174
|
* This function makes the function of tier 2 with `new Function`, with the options of the source. It throws on a page
|
|
175
175
|
* that does not permit `new Function`.
|
|
@@ -193,10 +193,11 @@ interface TieredProgram extends Program {
|
|
|
193
193
|
readonly stats: TierStats;
|
|
194
194
|
}
|
|
195
195
|
/**
|
|
196
|
-
* This function compiles an expression into tier 2 if it can, and else into tier 1. After a
|
|
197
|
-
* tier 1 does that evaluation again. A run with a trace and `explain` always use tier 1, because
|
|
196
|
+
* This function compiles an expression or a document into tier 2 if it can, and else into tier 1 (DOC.ENTRY). After a
|
|
197
|
+
* failed check of tier 2, tier 1 does that evaluation again. A run with a trace and `explain` always use tier 1, because
|
|
198
|
+
* tier 2 makes no trace.
|
|
198
199
|
*/
|
|
199
|
-
export declare function tiered(
|
|
200
|
+
export declare function tiered(source: Expr | Envelope, options?: GenerateOptions): TieredProgram;
|
|
200
201
|
//#endregion
|
|
201
202
|
//#region src/module.d.ts
|
|
202
203
|
/** The options of a standalone module. */
|
|
@@ -218,8 +219,9 @@ type Emitted = {
|
|
|
218
219
|
/**
|
|
219
220
|
* This function prints a program as a standalone ES module. The module exports `VexContractError`, `VEX_ERROR` and a
|
|
220
221
|
* function that takes the records and the variables and gives the value at each key. It is pure, so a build can use it.
|
|
222
|
+
* The program can be an expression or a document (DOC.ENTRY).
|
|
221
223
|
*/
|
|
222
|
-
export declare function emitModule(
|
|
224
|
+
export declare function emitModule(source: Expr | Envelope, options: ModuleOptions): Emitted;
|
|
223
225
|
//#endregion
|
|
224
226
|
//#region src/runtime.d.ts
|
|
225
227
|
/** The value of a program that stops at a failed check. */
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/facts.ts","../src/loop.ts","../src/tiers.ts","../src/module.ts","../src/runtime.ts"],"mappings":";;;KAgCY,aAAa;;;;;KAMb,OAAO;;;;;UAYF;WACN,SAAS,SAAS,eAAe;WACjC,OAAO,SAAS,eAAe;;;UAIzB;WACN,mBAAmB;WACnB,MAAM,SAAS,eAAe;WAC9B,aAAa,SAAS,eAAe;WACrC,SAAS;;;;;KCzCR;;KAeA;;KA2BA;WACG;WAAqB;;WACrB;WAAqB;;WACrB;WAAmB;;WACnB;WAAoB;;WACpB;;WACA;WAAqB,gBAAgB;;WACrC;WAAoB,IAAI;WAAiB,eAAe;;WACxD;WAAoB,IAAI;WAAoB,eAAe;;WAK3D;WAAkB;WAAuB,eAAe;WAAkB,iBAAiB;;WAE3F;WAAoB;;WAEpB;WAAuB;WAAuB,gBAAgB,OAAO,OAAO;;;;;;;;;;;KAW/E;WACG;;WACA;WAAwB;WAAwB;aAAoB,MAAM;aAAgB,MAAM;;;WAChG;WAAqB;;;KAGxB;WAEG;WAAmB;WAAoB,OAAO;;WAE9C;WAAmB;WAAoB,OAAO;;WAE9C;WAAqB,IAAI;WAAgB;WAAyB,MAAM;;WAExE;WAAoB,MAAM;;WAC1B;WAAkB,MAAM;WAAgB,cAAc;WAAkB,aAAa;;WAKrF;WAAoB;WAAwB;WAAuB;WAAwB;WAAwB,MAAM;WAAgB,eAAe;;WAExJ;WAAqB;WAAwB,eAAe;;WAE5D;WAAsB;WAAwB,eAAe;;WAE7D;;WAEA;WAAoB;WAAoB,OAAO;;WAC/C;WAAsB,OAAO;;;UAG3B;WACN,eAAe;WACf;;WAEA;;;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O,gBAAgB,eAAe;EAC1E,IAAI,MAAM,SAAS,sCAAsC;;;wBAI3C,cAAc,SAAS,iBAAiB"}
|
package/build/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MAX_NESTING, addrExprs, axisTargets, children, compile, findMethod, fixAddr, isFieldName, isListOp, isOpName, isStackOverflow, isVexList, liftsAt, listOf, lit, matchesKind, moveExpr, nestingOf, ok, resolveAddr, safeIs, shapeError, vexList } from "@mark1russell7/vex";
|
|
1
|
+
import { MAX_NESTING, addrExprs, axisTargets, children, compile, findMethod, fixAddr, fromDocument, isDocument, isFieldName, isListOp, isOpName, isStackOverflow, isVexList, liftsAt, listOf, lit, matchesKind, moveExpr, nestingOf, ok, resolveAddr, safeIs, shapeError, vexList } from "@mark1russell7/vex";
|
|
2
2
|
//#region src/facts.ts
|
|
3
3
|
/**
|
|
4
4
|
* The facts of the options that the printer uses (docs/COMPILER.md §13.2). For each op site, they give the domain
|
|
@@ -10,11 +10,15 @@ import { MAX_NESTING, addrExprs, axisTargets, children, compile, findMethod, fix
|
|
|
10
10
|
* twice doubles its body, so a chain of such functions grows fast. A module prints each function once.
|
|
11
11
|
*/
|
|
12
12
|
const MAX_CALLS = 1e4;
|
|
13
|
-
/** The list ops that give a list or read a position of a list (LIST.SORT, LIST.TAKE, LIST.NTH). */
|
|
13
|
+
/** The list ops that give a list or read a position of a list (LIST.SORT, LIST.TAKE, LIST.NTH, LIST.BUILD). */
|
|
14
14
|
const ORDER_OPS = /* @__PURE__ */ new Set([
|
|
15
15
|
"sortBy",
|
|
16
16
|
"take",
|
|
17
|
-
"nth"
|
|
17
|
+
"nth",
|
|
18
|
+
"drop",
|
|
19
|
+
"append",
|
|
20
|
+
"flat",
|
|
21
|
+
"without"
|
|
18
22
|
]);
|
|
19
23
|
/**
|
|
20
24
|
* This function gives the reason that tier 2 cannot take a program with lists as values. The reason is the axis `list`
|
|
@@ -1324,6 +1328,10 @@ var Lowerer = class {
|
|
|
1324
1328
|
case "sortBy":
|
|
1325
1329
|
case "take":
|
|
1326
1330
|
case "nth":
|
|
1331
|
+
case "drop":
|
|
1332
|
+
case "append":
|
|
1333
|
+
case "flat":
|
|
1334
|
+
case "without":
|
|
1327
1335
|
/* v8 ignore next -- @preserve */
|
|
1328
1336
|
throw new Unsupported(`the list op "${op}"`);
|
|
1329
1337
|
}
|
|
@@ -1758,13 +1766,18 @@ function createRuntime(options) {
|
|
|
1758
1766
|
* gives each result that tier 2 cannot give. Thus a tiered program gives the results of the interpreter.
|
|
1759
1767
|
*/
|
|
1760
1768
|
/**
|
|
1761
|
-
* This function lowers an expression and prints its source for the options. The op tables of
|
|
1762
|
-
* fast path of each op site. It is pure, so a build can use it.
|
|
1769
|
+
* This function lowers an expression or a document and prints its source for the options (DOC.ENTRY). The op tables of
|
|
1770
|
+
* the domains give the fast path of each op site. It is pure, so a build can use it.
|
|
1763
1771
|
*/
|
|
1764
|
-
function generate(
|
|
1772
|
+
function generate(source, options = {}) {
|
|
1773
|
+
const read = isDocument(source) ? fromDocument(source, options.domains) : ok(source);
|
|
1774
|
+
if (!read.ok) return {
|
|
1775
|
+
ok: false,
|
|
1776
|
+
reason: read.error.message
|
|
1777
|
+
};
|
|
1765
1778
|
try {
|
|
1766
1779
|
const facts = factsOf(options);
|
|
1767
|
-
const program = lower(
|
|
1780
|
+
const program = lower(read.value, facts, options.schema);
|
|
1768
1781
|
return {
|
|
1769
1782
|
ok: true,
|
|
1770
1783
|
program,
|
|
@@ -1786,11 +1799,13 @@ function load(generated) {
|
|
|
1786
1799
|
return new Function("K", "R", generated.source)(generated.program.constants, createRuntime(generated.options));
|
|
1787
1800
|
}
|
|
1788
1801
|
/**
|
|
1789
|
-
* This function compiles an expression into tier 2 if it can, and else into tier 1. After a
|
|
1790
|
-
* tier 1 does that evaluation again. A run with a trace and `explain` always use tier 1, because
|
|
1802
|
+
* This function compiles an expression or a document into tier 2 if it can, and else into tier 1 (DOC.ENTRY). After a
|
|
1803
|
+
* failed check of tier 2, tier 1 does that evaluation again. A run with a trace and `explain` always use tier 1, because
|
|
1804
|
+
* tier 2 makes no trace.
|
|
1791
1805
|
*/
|
|
1792
|
-
function tiered(
|
|
1793
|
-
const slow = compile(
|
|
1806
|
+
function tiered(source, options = {}) {
|
|
1807
|
+
const slow = compile(source, options);
|
|
1808
|
+
const { expr } = slow;
|
|
1794
1809
|
const generated = generate(expr, options);
|
|
1795
1810
|
let fast;
|
|
1796
1811
|
let reason;
|
|
@@ -2043,6 +2058,13 @@ const BOOLEAN_LIST_OPS = /* @__PURE__ */ new Set([
|
|
|
2043
2058
|
"all",
|
|
2044
2059
|
"none"
|
|
2045
2060
|
]);
|
|
2061
|
+
/** The list ops that build a list from lists (LIST.BUILD). A module gives a reason for them. */
|
|
2062
|
+
const BUILD_OPS = /* @__PURE__ */ new Set([
|
|
2063
|
+
"drop",
|
|
2064
|
+
"append",
|
|
2065
|
+
"flat",
|
|
2066
|
+
"without"
|
|
2067
|
+
]);
|
|
2046
2068
|
/** This function gives the kind of the result of a list op: first gives the kind of its items. */
|
|
2047
2069
|
const listKind = (op, item) => NUMBER_LIST_OPS.has(op) ? "number" : BOOLEAN_LIST_OPS.has(op) ? "boolean" : item;
|
|
2048
2070
|
const LOOPS = /* @__PURE__ */ new WeakMap();
|
|
@@ -2914,6 +2936,7 @@ var Printer = class {
|
|
|
2914
2936
|
const [head] = args;
|
|
2915
2937
|
if (LIST_OPS.has(op) && head?.tag === "each") return this.#list(op, head, args.slice(1), ctx);
|
|
2916
2938
|
if (head !== void 0 && (op === "sortBy" || op === "take")) return this.#order(op, head, args.slice(1), ctx);
|
|
2939
|
+
if (head !== void 0 && BUILD_OPS.has(op) && this.#isList(head, ctx)) throw new NotStandalone(`the list op "${op}" is not in a module yet`);
|
|
2917
2940
|
if (head !== void 0 && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, args.slice(1), ctx);
|
|
2918
2941
|
const raw = args.map((x) => this.expr(x, ctx));
|
|
2919
2942
|
const deps = depsOf(...raw);
|
|
@@ -3721,13 +3744,23 @@ var Printer = class {
|
|
|
3721
3744
|
/**
|
|
3722
3745
|
* This function prints a program as a standalone ES module. The module exports `VexContractError`, `VEX_ERROR` and a
|
|
3723
3746
|
* function that takes the records and the variables and gives the value at each key. It is pure, so a build can use it.
|
|
3747
|
+
* The program can be an expression or a document (DOC.ENTRY).
|
|
3724
3748
|
*/
|
|
3725
|
-
function emitModule(
|
|
3749
|
+
function emitModule(source, options) {
|
|
3726
3750
|
const name = options.name ?? "run";
|
|
3727
3751
|
if (!/^[A-Za-z_$][\w$]*$/.test(name)) return {
|
|
3728
3752
|
ok: false,
|
|
3729
3753
|
reason: `the name "${name}" is not a JavaScript name`
|
|
3730
3754
|
};
|
|
3755
|
+
const read = isDocument(source) ? fromDocument(source, options.domains) : {
|
|
3756
|
+
ok: true,
|
|
3757
|
+
value: source
|
|
3758
|
+
};
|
|
3759
|
+
if (!read.ok) return {
|
|
3760
|
+
ok: false,
|
|
3761
|
+
reason: read.error.message
|
|
3762
|
+
};
|
|
3763
|
+
const expr = read.value;
|
|
3731
3764
|
const shape = shapeError(expr);
|
|
3732
3765
|
if (shape !== void 0) return {
|
|
3733
3766
|
ok: false,
|