@mark1russell7/vex-compiler 0.9.0 → 0.9.1
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 +27 -10
- package/build/index.js.map +1 -1
- package/package.json +4 -4
- package/src/module.ts +7 -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;;;;;KCrHC,kBAAkB;WAA4B,SAAS;;;;;;K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S,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
|
|
@@ -1758,13 +1758,18 @@ function createRuntime(options) {
|
|
|
1758
1758
|
* gives each result that tier 2 cannot give. Thus a tiered program gives the results of the interpreter.
|
|
1759
1759
|
*/
|
|
1760
1760
|
/**
|
|
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.
|
|
1761
|
+
* This function lowers an expression or a document and prints its source for the options (DOC.ENTRY). The op tables of
|
|
1762
|
+
* the domains give the fast path of each op site. It is pure, so a build can use it.
|
|
1763
1763
|
*/
|
|
1764
|
-
function generate(
|
|
1764
|
+
function generate(source, options = {}) {
|
|
1765
|
+
const read = isDocument(source) ? fromDocument(source, options.domains) : ok(source);
|
|
1766
|
+
if (!read.ok) return {
|
|
1767
|
+
ok: false,
|
|
1768
|
+
reason: read.error.message
|
|
1769
|
+
};
|
|
1765
1770
|
try {
|
|
1766
1771
|
const facts = factsOf(options);
|
|
1767
|
-
const program = lower(
|
|
1772
|
+
const program = lower(read.value, facts, options.schema);
|
|
1768
1773
|
return {
|
|
1769
1774
|
ok: true,
|
|
1770
1775
|
program,
|
|
@@ -1786,11 +1791,13 @@ function load(generated) {
|
|
|
1786
1791
|
return new Function("K", "R", generated.source)(generated.program.constants, createRuntime(generated.options));
|
|
1787
1792
|
}
|
|
1788
1793
|
/**
|
|
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
|
|
1794
|
+
* This function compiles an expression or a document into tier 2 if it can, and else into tier 1 (DOC.ENTRY). After a
|
|
1795
|
+
* failed check of tier 2, tier 1 does that evaluation again. A run with a trace and `explain` always use tier 1, because
|
|
1796
|
+
* tier 2 makes no trace.
|
|
1791
1797
|
*/
|
|
1792
|
-
function tiered(
|
|
1793
|
-
const slow = compile(
|
|
1798
|
+
function tiered(source, options = {}) {
|
|
1799
|
+
const slow = compile(source, options);
|
|
1800
|
+
const { expr } = slow;
|
|
1794
1801
|
const generated = generate(expr, options);
|
|
1795
1802
|
let fast;
|
|
1796
1803
|
let reason;
|
|
@@ -3721,13 +3728,23 @@ var Printer = class {
|
|
|
3721
3728
|
/**
|
|
3722
3729
|
* This function prints a program as a standalone ES module. The module exports `VexContractError`, `VEX_ERROR` and a
|
|
3723
3730
|
* function that takes the records and the variables and gives the value at each key. It is pure, so a build can use it.
|
|
3731
|
+
* The program can be an expression or a document (DOC.ENTRY).
|
|
3724
3732
|
*/
|
|
3725
|
-
function emitModule(
|
|
3733
|
+
function emitModule(source, options) {
|
|
3726
3734
|
const name = options.name ?? "run";
|
|
3727
3735
|
if (!/^[A-Za-z_$][\w$]*$/.test(name)) return {
|
|
3728
3736
|
ok: false,
|
|
3729
3737
|
reason: `the name "${name}" is not a JavaScript name`
|
|
3730
3738
|
};
|
|
3739
|
+
const read = isDocument(source) ? fromDocument(source, options.domains) : {
|
|
3740
|
+
ok: true,
|
|
3741
|
+
value: source
|
|
3742
|
+
};
|
|
3743
|
+
if (!read.ok) return {
|
|
3744
|
+
ok: false,
|
|
3745
|
+
reason: read.error.message
|
|
3746
|
+
};
|
|
3747
|
+
const expr = read.value;
|
|
3731
3748
|
const shape = shapeError(expr);
|
|
3732
3749
|
if (shape !== void 0) return {
|
|
3733
3750
|
ok: false,
|