@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/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.0",
4
+ "version": "0.10.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.9.0",
47
- "@mark1russell7/vex-testkit": "0.9.0",
46
+ "@mark1russell7/vex-domains": "0.10.0",
47
+ "@mark1russell7/vex-testkit": "0.10.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.9.0"
54
+ "@mark1russell7/vex": "0.10.0"
55
55
  },
56
56
  "scripts": {
57
57
  "build": "tsdown",
package/src/facts.ts CHANGED
@@ -11,8 +11,8 @@ import { children, isListOp, isOpName, liftsAt, listOf, type AnyDomain, type Exp
11
11
  */
12
12
  export const MAX_CALLS = 10_000;
13
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"]);
14
+ /** The list ops that give a list or read a position of a list (LIST.SORT, LIST.TAKE, LIST.NTH, LIST.BUILD). */
15
+ const ORDER_OPS: ReadonlySet<string> = new Set(["sortBy", "take", "nth", "drop", "append", "flat", "without"]);
16
16
 
17
17
  /**
18
18
  * This function gives the reason that tier 2 cannot take a program with lists as values. The reason is the axis `list`
package/src/lower.ts CHANGED
@@ -563,6 +563,10 @@ class Lowerer {
563
563
  case "sortBy":
564
564
  case "take":
565
565
  case "nth":
566
+ case "drop":
567
+ case "append":
568
+ case "flat":
569
+ case "without":
566
570
  /* v8 ignore next -- @preserve */
567
571
  // Stryker disable next-line all: listReason stops these ops before the lowering
568
572
  throw new Unsupported(`the list op "${op}"`);
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
 
@@ -369,6 +369,8 @@ const NEEDS: Readonly<Record<Helper, readonly Helper[]>> = {
369
369
  const LIST_OPS: ReadonlySet<string> = new Set(["count", "sum", "mean", "min", "max", "any", "all", "none", "first"]);
370
370
  const NUMBER_LIST_OPS: ReadonlySet<string> = new Set(["count", "sum", "mean", "min", "max"]);
371
371
  const BOOLEAN_LIST_OPS: ReadonlySet<string> = new Set(["any", "all", "none"]);
372
+ /** The list ops that build a list from lists (LIST.BUILD). A module gives a reason for them. */
373
+ const BUILD_OPS: ReadonlySet<string> = new Set(["drop", "append", "flat", "without"]);
372
374
 
373
375
  /** This function gives the kind of the result of a list op: first gives the kind of its items. */
374
376
  const listKind = (op: string, item: Kind | undefined): Kind | undefined => (NUMBER_LIST_OPS.has(op) ? "number" : BOOLEAN_LIST_OPS.has(op) ? "boolean" : item);
@@ -1194,6 +1196,8 @@ class Printer {
1194
1196
  const [head] = args;
1195
1197
  if (LIST_OPS.has(op) && head?.tag === "each") return this.#list(op, head, args.slice(1), ctx);
1196
1198
  if (head !== undefined && (op === "sortBy" || op === "take")) return this.#order(op, head, args.slice(1), ctx);
1199
+ // LIST.BUILD: a module does not have the ops that build a list yet. The interpreter evaluates them.
1200
+ if (head !== undefined && BUILD_OPS.has(op) && this.#isList(head, ctx)) throw new NotStandalone(`the list op "${op}" is not in a module yet`);
1197
1201
  if (head !== undefined && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, args.slice(1), ctx);
1198
1202
  const raw = args.map((x) => this.expr(x, ctx));
1199
1203
  const deps = depsOf(...raw);
@@ -1747,10 +1751,15 @@ class Printer {
1747
1751
  /**
1748
1752
  * This function prints a program as a standalone ES module. The module exports `VexContractError`, `VEX_ERROR` and a
1749
1753
  * function that takes the records and the variables and gives the value at each key. It is pure, so a build can use it.
1754
+ * The program can be an expression or a document (DOC.ENTRY).
1750
1755
  */
1751
- export function emitModule(expr: Expr, options: ModuleOptions): Emitted {
1756
+ export function emitModule(source: Expr | Envelope, options: ModuleOptions): Emitted {
1752
1757
  const name = options.name ?? "run";
1753
1758
  if (!/^[A-Za-z_$][\w$]*$/.test(name)) return { ok: false, reason: `the name "${name}" is not a JavaScript name` };
1759
+ // DOC.ENTRY: a document gives its expression first, or the reason that it is not valid.
1760
+ const read = isDocument(source) ? fromDocument(source, options.domains) : { ok: true as const, value: source };
1761
+ if (!read.ok) return { ok: false, reason: read.error.message };
1762
+ const expr = read.value;
1754
1763
  // EVAL.SHAPE: a module of an expression that is not valid has no use, so the printer gives the reason.
1755
1764
  const shape = shapeError(expr);
1756
1765
  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 the domains give the
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(expr: Expr, options: GenerateOptions = {}): Generated {
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(expr, facts, options.schema);
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 failed check of tier 2,
70
- * tier 1 does that evaluation again. A run with a trace and `explain` always use tier 1, because tier 2 makes no trace.
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(expr: Expr, options: GenerateOptions = {}): TieredProgram {
73
- const slow = compile(expr, options);
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;