@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/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.9.
|
|
4
|
+
"version": "0.9.1",
|
|
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.9.
|
|
47
|
-
"@mark1russell7/vex-testkit": "0.9.
|
|
46
|
+
"@mark1russell7/vex-domains": "0.9.1",
|
|
47
|
+
"@mark1russell7/vex-testkit": "0.9.1",
|
|
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.9.
|
|
54
|
+
"@mark1russell7/vex": "0.9.1"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "tsdown",
|
package/src/module.ts
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* schema, and the axes `all`, `others` and `window`. The kinds of each op site must decide its op. Another construct
|
|
20
20
|
* gives a reason, not code.
|
|
21
21
|
*/
|
|
22
|
-
import { addrExprs, children, isFieldName, lit, moveExpr, shapeError, type AnyDomain, type Axis, type Expr, type ExprOf, type Move, type Names, type PrimitiveKind } from "@mark1russell7/vex";
|
|
22
|
+
import { addrExprs, children, fromDocument, isDocument, isFieldName, lit, moveExpr, shapeError, type AnyDomain, type Axis, type Envelope, type Expr, type ExprOf, type Move, type Names, type PrimitiveKind } from "@mark1russell7/vex";
|
|
23
23
|
import { factsOf, kindOfDomain, primitiveOf, type Facts, type Kind, type Schema } from "./facts.ts";
|
|
24
24
|
import { memoNodes } from "./memo.ts";
|
|
25
25
|
|
|
@@ -1747,10 +1747,15 @@ class Printer {
|
|
|
1747
1747
|
/**
|
|
1748
1748
|
* This function prints a program as a standalone ES module. The module exports `VexContractError`, `VEX_ERROR` and a
|
|
1749
1749
|
* function that takes the records and the variables and gives the value at each key. It is pure, so a build can use it.
|
|
1750
|
+
* The program can be an expression or a document (DOC.ENTRY).
|
|
1750
1751
|
*/
|
|
1751
|
-
export function emitModule(
|
|
1752
|
+
export function emitModule(source: Expr | Envelope, options: ModuleOptions): Emitted {
|
|
1752
1753
|
const name = options.name ?? "run";
|
|
1753
1754
|
if (!/^[A-Za-z_$][\w$]*$/.test(name)) return { ok: false, reason: `the name "${name}" is not a JavaScript name` };
|
|
1755
|
+
// DOC.ENTRY: a document gives its expression first, or the reason that it is not valid.
|
|
1756
|
+
const read = isDocument(source) ? fromDocument(source, options.domains) : { ok: true as const, value: source };
|
|
1757
|
+
if (!read.ok) return { ok: false, reason: read.error.message };
|
|
1758
|
+
const expr = read.value;
|
|
1754
1759
|
// EVAL.SHAPE: a module of an expression that is not valid has no use, so the printer gives the reason.
|
|
1755
1760
|
const shape = shapeError(expr);
|
|
1756
1761
|
if (shape !== undefined) return { ok: false, reason: shape };
|
package/src/tiers.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The tiers (docs/COMPILER.md §2.1). Tier 2 is generated JavaScript. Tier 1 is the closure compiler of the core, and it
|
|
3
3
|
* gives each result that tier 2 cannot give. Thus a tiered program gives the results of the interpreter.
|
|
4
4
|
*/
|
|
5
|
-
import { compile, ok, type CompileOptions, type Expr, type Program, type Space } from "@mark1russell7/vex";
|
|
5
|
+
import { compile, fromDocument, isDocument, ok, type CompileOptions, type Envelope, type Expr, type Program, type Space } from "@mark1russell7/vex";
|
|
6
6
|
import { factsOf, type Schema } from "./facts.ts";
|
|
7
7
|
|
|
8
8
|
/** The options of the compiler: the options of `compile`, and the schema of the host (docs/COMPILER.md §3.2). */
|
|
@@ -24,13 +24,16 @@ export type Generated =
|
|
|
24
24
|
export type Tier2 = (space: Space, origin: string, vars: Readonly<Record<string, unknown>> | undefined, tables: Readonly<Record<string, Space>> | undefined) => unknown;
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* This function lowers an expression and prints its source for the options. The op tables of
|
|
28
|
-
* fast path of each op site. It is pure, so a build can use it.
|
|
27
|
+
* This function lowers an expression or a document and prints its source for the options (DOC.ENTRY). The op tables of
|
|
28
|
+
* the domains give the fast path of each op site. It is pure, so a build can use it.
|
|
29
29
|
*/
|
|
30
|
-
export function generate(
|
|
30
|
+
export function generate(source: Expr | Envelope, options: GenerateOptions = {}): Generated {
|
|
31
|
+
// DOC.ENTRY: a document gives its expression first, or the reason that it is not valid.
|
|
32
|
+
const read = isDocument(source) ? fromDocument(source, options.domains) : ok(source);
|
|
33
|
+
if (!read.ok) return { ok: false, reason: read.error.message };
|
|
31
34
|
try {
|
|
32
35
|
const facts = factsOf(options);
|
|
33
|
-
const program = lower(
|
|
36
|
+
const program = lower(read.value, facts, options.schema);
|
|
34
37
|
return { ok: true, program, source: printJs(program, facts), options };
|
|
35
38
|
} catch (thrown) {
|
|
36
39
|
// A stack overflow in the lowering of a deep expression also goes to tier 1.
|
|
@@ -66,11 +69,14 @@ export interface TieredProgram extends Program {
|
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
/**
|
|
69
|
-
* This function compiles an expression into tier 2 if it can, and else into tier 1. After a
|
|
70
|
-
* tier 1 does that evaluation again. A run with a trace and `explain` always use tier 1, because
|
|
72
|
+
* This function compiles an expression or a document into tier 2 if it can, and else into tier 1 (DOC.ENTRY). After a
|
|
73
|
+
* failed check of tier 2, tier 1 does that evaluation again. A run with a trace and `explain` always use tier 1, because
|
|
74
|
+
* tier 2 makes no trace.
|
|
71
75
|
*/
|
|
72
|
-
export function tiered(
|
|
73
|
-
const slow = compile(
|
|
76
|
+
export function tiered(source: Expr | Envelope, options: GenerateOptions = {}): TieredProgram {
|
|
77
|
+
const slow = compile(source, options);
|
|
78
|
+
// The program of tier 1 has the expression of a document, so tier 2 does not read the document again.
|
|
79
|
+
const { expr } = slow;
|
|
74
80
|
const generated = generate(expr, options);
|
|
75
81
|
let fast: Tier2 | undefined;
|
|
76
82
|
let reason: string | undefined;
|