@mark1russell7/vex-compiler 0.13.1 → 0.13.2

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 CHANGED
@@ -211,11 +211,12 @@ interface ModuleOptions {
211
211
  */
212
212
  readonly name?: string;
213
213
  /**
214
- * The space of the records. In both spaces, the key of a record is the text of its position. The default `array`
214
+ * The space of the records. In each space, the key of a record is the text of its position. The default `array`
215
215
  * has no parents. A `tree` takes the parents as the third argument of the run (COMPILE.MODULE.TREE). Each entry
216
- * is the position of the parent of its record, or -1 for a root.
216
+ * is the position of the parent of its record, or -1 for a root. A `graph` takes the edges as the third argument
217
+ * (COMPILE.MODULE.GRAPH). Each entry is an array of the positions of the ends of the edges from its record, in order.
217
218
  */
218
- readonly space?: "array" | "tree";
219
+ readonly space?: "array" | "tree" | "graph";
219
220
  }
220
221
  /** The options of a module of a sheet (COMPILE.MODULE.SHEET): the options of a module, and the parts of a sheet. */
221
222
  interface SheetModuleOptions extends ModuleOptions {
@@ -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":";;;KAqCY,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;;;;;KC9CR;;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/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I;WACJ,MAAM;;;KAIL,WAAW,QAAQ;WAAiB;;;UAG/B;WACN;WACA;;;UAIM;WACN;WACA;;;;;;UAOM;EACf,IAAI,GAAG,OAAO,2BAA2B;EACzC,IAAI,GAAG,OAAO,0BAA0B,YAAY,cAAc;;;UAgBnD;WACN,GAAG;EACZ,IAAI,GAAG,OAAO;;EAEd,KAAK,GAAG,OAAO,gBAAgB,eAAe,IAAI,MAAM;;;;;EAKxD,KAAK,MAAM,YAAY,GAAG,OAAO,gBAAgB,kBAAkB,UAAU;EAC7E,MAAM,QAAQ,SAAS,eAAe,qBAAqB;EAC3D,KAAK,GAAG;;EAER,KAAK,cAAc,gBAAgB;;EAEnC,QAAQ;;EAER,OAAO,gBAAgB;;EAEvB,MAAM,kBAAkB;EACxB,KAAK,cAAc,cAAc;EACjC,OAAO;EACP,KAAK;EACL,IAAI,0BAA0B;EAC9B,KAAK,yBAAyB;EAC9B,OAAO;EACP,IAAI,cAAc,eAAe,GAAG,OAAO,gBAAgB,eAAe;EAC1E,IAAI,MAAM,SAAS,sCAAsC;;;wBAI3C,cAAc,SAAS,iBAAiB"}
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":";;;KAqCY,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;;;;;KC9CR;;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,g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;;;;UAOZ;MACX;EACJ,GAAG;WACM,IAAI;WACJ,MAAM;;;KAIL,WAAW,QAAQ;WAAiB;;;UAG/B;WACN;WACA;;;UAIM;WACN;WACA;;;;;;UAOM;EACf,IAAI,GAAG,OAAO,2BAA2B;EACzC,IAAI,GAAG,OAAO,0BAA0B,YAAY,cAAc;;;UAgBnD;WACN,GAAG;EACZ,IAAI,GAAG,OAAO;;EAEd,KAAK,GAAG,OAAO,gBAAgB,eAAe,IAAI,MAAM;;;;;EAKxD,KAAK,MAAM,YAAY,GAAG,OAAO,gBAAgB,kBAAkB,UAAU;EAC7E,MAAM,QAAQ,SAAS,eAAe,qBAAqB;EAC3D,KAAK,GAAG;;EAER,KAAK,cAAc,gBAAgB;;EAEnC,QAAQ;;EAER,OAAO,gBAAgB;;EAEvB,MAAM,kBAAkB;EACxB,KAAK,cAAc,cAAc;EACjC,OAAO;EACP,KAAK;EACL,IAAI,0BAA0B;EAC9B,KAAK,yBAAyB;EAC9B,OAAO;EACP,IAAI,cAAc,eAAe,GAAG,OAAO,gBAAgB,eAAe;EAC1E,IAAI,MAAM,SAAS,sCAAsC;;;wBAI3C,cAAc,SAAS,iBAAiB"}
package/build/index.js CHANGED
@@ -1672,6 +1672,7 @@ function createRuntime(options) {
1672
1672
  });
1673
1673
  if (r !== void 0) return r;
1674
1674
  }
1675
+ if (listOp && fn === void 0 && Array.isArray(values[0])) throw new Error("tier 1 gives a list op on an array");
1675
1676
  try {
1676
1677
  return fn === void 0 ? F : checked(fn(...values));
1677
1678
  } catch (thrown) {
@@ -1940,6 +1941,53 @@ function keyOrderOf(d) {
1940
1941
  back: post !== reverse
1941
1942
  };
1942
1943
  }
1944
+ /** This function gives the axis under the tests of `where` and under `reverse`. Both read the targets of that axis. */
1945
+ function baseOf(axis) {
1946
+ let base = axis;
1947
+ while (base.t === "where" || base.t === "reverse") base = base.axis;
1948
+ return base;
1949
+ }
1950
+ /**
1951
+ * This function tells if each read of a cell of the column `self` reads a finished cell in the static order of the
1952
+ * fill (COMPILE.MODULE.SHEET.DIRECT). A read of a column before `self` in `order` reads a finished cell. A read of a
1953
+ * column that the sheet does not have also reads a finished cell. A read of `self` reads a finished cell only in
1954
+ * these three forms:
1955
+ * - in a tree in pre-order, at the move `parent` from the key,
1956
+ * - in an array in key order, at an offset of one negative step from the key,
1957
+ * - in a tree in post-order, at the key of a target of the axis `children` of the key.
1958
+ *
1959
+ * A read in the body of a function can evaluate at each place of a call, so it is not one of these forms.
1960
+ */
1961
+ function readsFinished(columns, order, self, tree) {
1962
+ const formula = columns.get(self);
1963
+ if (formula === void 0 || shapeError(formula) !== void 0) return false;
1964
+ const { reverse, post } = keyOrderOf(readsOf(columns, /* @__PURE__ */ new Map(), self).directions);
1965
+ const position = order.indexOf(self);
1966
+ const todo = [[formula, "key"]];
1967
+ for (let item = todo.pop(); item !== void 0; item = todo.pop()) {
1968
+ const [e, place] = item;
1969
+ if (e.tag === "each" || e.tag === "fold") {
1970
+ const inner = place === "key" && (e.at === void 0 || e.at.length === 0) && baseOf(e.axis).t === "children" ? "children" : "unknown";
1971
+ const { tests } = partsOf$1(e.axis);
1972
+ for (const x of [...e.tag === "each" ? [e.body] : Object.values(e.step), ...tests]) todo.push([x, inner]);
1973
+ const list = listOf(e.axis);
1974
+ for (const x of [
1975
+ ...e.tag === "fold" ? [...Object.values(e.init), e.result] : [],
1976
+ ...addrExprs(e.at),
1977
+ ...list === void 0 ? [] : [list]
1978
+ ]) todo.push([x, place]);
1979
+ } else if (e.tag === "fn") todo.push([e.body, "unknown"]);
1980
+ else if (e.tag !== "ext") for (const child of children(e)) todo.push([child, place]);
1981
+ else if (e.kind === CELL_KIND && isCellRef(e.data)) {
1982
+ const { column, at } = e.data;
1983
+ if (order.indexOf(column) > position) return false;
1984
+ if (column !== self) continue;
1985
+ const [move, ...rest] = at;
1986
+ if (!(place === "key" ? move !== void 0 && rest.length === 0 && (tree && !post && move.t === "parent" || !tree && !reverse && move.t === "offset" && move.d.length === 1 && (move.d[0] ?? 0) < 0) : place === "children" && tree && post && at.length === 0)) return false;
1987
+ }
1988
+ }
1989
+ return true;
1990
+ }
1943
1991
  //#endregion
1944
1992
  //#region src/module.ts
1945
1993
  /**
@@ -1959,11 +2007,12 @@ function keyOrderOf(d) {
1959
2007
  * before the loop over the keys. A node of the memo is one function of its other keys, with a map of its values. The
1960
2008
  * nodes with the same expression share the function.
1961
2009
  *
1962
- * The printer takes a part of the language. It takes an array space or a tree space of records, and the fields and
1963
- * the variables of the schema. It takes the axes `all`, `others` and `window`. A tree space also has the move `parent` and the
1964
- * axes of a tree (COMPILE.MODULE.TREE). A list is an array of pairs of a key and a value. The list ops, list literals
1965
- * and lists as names of loops are in a module (COMPILE.MODULE.LIST). The kinds of each op site must decide its op.
1966
- * Another construct gives a reason, not code.
2010
+ * The printer takes a part of the language. It takes an array space, a tree space or a graph space of records, and
2011
+ * the fields and the variables of the schema. It takes the axes `all`, `others` and `window`. A tree space also has the
2012
+ * move `parent` and the axes of a tree (COMPILE.MODULE.TREE). A graph space has the axes of a graph
2013
+ * (COMPILE.MODULE.GRAPH). A list is an array of pairs of a key and a value. The list ops, `reduce`, list literals and
2014
+ * lists as names of loops are in a module (COMPILE.MODULE.LIST, COMPILE.MODULE.REDUCE). The kinds of each op site must
2015
+ * decide its op. Another construct gives a reason, not code.
1967
2016
  *
1968
2017
  * `emitSheetModule` prints the formula columns of a sheet as one module (COMPILE.MODULE.SHEET). Each column is an array
1969
2018
  * of results, and the run fills the arrays in the static order of `SheetRun.fill`. A read of a cell that is not finished
@@ -2110,6 +2159,29 @@ const HELPERS = {
2110
2159
  SO: "function SO(g, s, k, d) { const t = g[k][s[k] + d]; return t === undefined ? F() : t; }",
2111
2160
  AN: "function AN(P, k) { const o = []; for (let p = P[k]; p >= 0; p = P[p]) o.push(p); return o; }",
2112
2161
  BF: "function BF(h, k) { const o = [k]; for (let i = 0; i < o.length; i++) for (const c of h[o[i]]) o.push(c); return o; }",
2162
+ GR: [
2163
+ "function GR(e, n) {",
2164
+ " if (!Array.isArray(e) || e.length !== n) C(\"the edges are not an array with the ends of the edges of each record\");",
2165
+ " const O = [], s = new Int32Array(n);",
2166
+ " for (let i = 0; i < n; i++) {",
2167
+ " const x = e[i];",
2168
+ " if (!Array.isArray(x)) C(`the edges of the record ${i} are not an array`);",
2169
+ " const o = [];",
2170
+ " for (const t of x) {",
2171
+ " if (!(Number.isInteger(t) && t >= 0 && t < n)) C(`an edge of the record ${i} does not go to the position of a record`);",
2172
+ " if (s[t] !== i + 1) {",
2173
+ " s[t] = i + 1;",
2174
+ " o.push(t);",
2175
+ " }",
2176
+ " }",
2177
+ " O.push(o);",
2178
+ " }",
2179
+ " return O;",
2180
+ "}"
2181
+ ].join("\n"),
2182
+ GI: "function GI(O, n) { const I = []; for (let i = 0; i < n; i++) I.push([]); for (let i = 0; i < n; i++) for (const t of O[i]) I[t].push(i); return I; }",
2183
+ RB: "function RB(O, k, n) { const s = new Uint8Array(n), o = [k]; s[k] = 1; for (let i = 0; i < o.length; i++) for (const c of O[o[i]]) if (s[c] === 0) { s[c] = 1; o.push(c); } return o; }",
2184
+ DO: "function DO(d) { for (let k = 0; k < d.length; k++) if (d[k] === E) d[k] = VEX_ERROR; return d; }",
2113
2185
  SH: [
2114
2186
  "function SH(n, cols, own, budget) {",
2115
2187
  " const pg = (c, k) => {",
@@ -2381,6 +2453,7 @@ const NEEDS = {
2381
2453
  GT: ["F"],
2382
2454
  IX: ["V", "F"],
2383
2455
  PT: ["C"],
2456
+ GR: ["C"],
2384
2457
  UP: ["F"],
2385
2458
  SO: ["F"],
2386
2459
  CH: [],
@@ -2388,8 +2461,11 @@ const NEEDS = {
2388
2461
  PR: [],
2389
2462
  AN: [],
2390
2463
  BF: [],
2464
+ GI: [],
2465
+ RB: [],
2391
2466
  SH: [],
2392
- KO: []
2467
+ KO: [],
2468
+ DO: []
2393
2469
  };
2394
2470
  /** The statement of each part of a tree in a run, with the helper that computes it. */
2395
2471
  const TREE_PARTS = {
@@ -2409,6 +2485,12 @@ const TREE_AXES = /* @__PURE__ */ new Set([
2409
2485
  ]);
2410
2486
  /** This function tells if an axis has its targets in a range of an array of positions in a tree space. */
2411
2487
  const isTreeAxis = (a) => a.t === "window" || TREE_AXES.has(a.t);
2488
+ /** The axes of a graph space in a module (COMPILE.MODULE.GRAPH), other than the axes of each space. */
2489
+ const GRAPH_AXES = /* @__PURE__ */ new Set([
2490
+ "successors",
2491
+ "predecessors",
2492
+ "reachable"
2493
+ ]);
2412
2494
  /** The list ops of a standalone module. */
2413
2495
  const LIST_OPS = /* @__PURE__ */ new Set([
2414
2496
  "count",
@@ -2421,6 +2503,8 @@ const LIST_OPS = /* @__PURE__ */ new Set([
2421
2503
  "none",
2422
2504
  "first"
2423
2505
  ]);
2506
+ /** The list ops with a running value in a module: the list ops above, and `reduce` with the op of a domain (COMPILE.MODULE.REDUCE). */
2507
+ const FOLD_OPS = /* @__PURE__ */ new Set([...LIST_OPS, "reduce"]);
2424
2508
  const NUMBER_LIST_OPS = /* @__PURE__ */ new Set([
2425
2509
  "count",
2426
2510
  "sum",
@@ -2510,7 +2594,7 @@ function fits(name, step) {
2510
2594
  return name.inner === void 0 ? step.inner === void 0 : step.inner !== void 0 && fits(name.inner, step.inner);
2511
2595
  }
2512
2596
  /** This function tells if the step of a list op throws for a value of the kind `kind`, which is the wrong kind. */
2513
- const stepThrows = (op, kind) => NUMBER_STEPS.has(op) && kind !== "number" || BOOLEAN_LIST_OPS.has(op) && kind !== "boolean";
2597
+ const stepThrows = (op, kind) => op === "reduce" || NUMBER_STEPS.has(op) && kind !== "number" || BOOLEAN_LIST_OPS.has(op) && kind !== "boolean";
2514
2598
  /** The list ops whose step takes a number. */
2515
2599
  const NUMBER_STEPS = /* @__PURE__ */ new Set([
2516
2600
  "sum",
@@ -2787,6 +2871,9 @@ var Printer = class {
2787
2871
  /** True for a tree space, and the parts of the tree that the program reads (COMPILE.MODULE.TREE). */
2788
2872
  #tree;
2789
2873
  treeParts = /* @__PURE__ */ new Set();
2874
+ /** True for a graph space, and true when the program reads the predecessors of a graph (COMPILE.MODULE.GRAPH). */
2875
+ #graph;
2876
+ into = false;
2790
2877
  /** The columns of a sheet, for a module of a sheet (COMPILE.MODULE.SHEET). */
2791
2878
  #sheet;
2792
2879
  #names = 0;
@@ -2829,6 +2916,7 @@ var Printer = class {
2829
2916
  this.#schema = options.schema;
2830
2917
  this.#memo = memoOf(expr);
2831
2918
  this.#tree = options.space === "tree";
2919
+ this.#graph = options.space === "graph";
2832
2920
  this.#sheet = sheet;
2833
2921
  }
2834
2922
  use(name) {
@@ -3200,6 +3288,7 @@ var Printer = class {
3200
3288
  * must read no cell.
3201
3289
  */
3202
3290
  #inOrder(parts, all) {
3291
+ if (this.#sheet?.direct === true) return parts;
3203
3292
  const last = parts.findLastIndex((x) => x.v.deps.has(CELLS));
3204
3293
  const after = parts.slice(last + 1).every((x) => x.s.length === 0 || x.v.safe);
3205
3294
  const before = !all || parts.slice(0, Math.max(last, 0)).every((x) => x.v.safe) || !READS_IN_CODE.test(parts[last].v.code);
@@ -3263,7 +3352,7 @@ var Printer = class {
3263
3352
  const [d, ...more] = m.d;
3264
3353
  /* v8 ignore next -- @preserve */
3265
3354
  if (focus === void 0) throw new NotStandalone("a node of the memo reads its focus");
3266
- if (d === void 0 || more.length > 0 || !Number.isInteger(d)) return `${F}()`;
3355
+ if (this.#graph || d === void 0 || more.length > 0 || !Number.isInteger(d)) return `${F}()`;
3267
3356
  if (this.#tree) return `${this.use("SO")}(${this.#part("siblings", "GP")}, SI, ${focus}, ${d})`;
3268
3357
  return `${this.use("X")}(${focus} + ${d}, n)`;
3269
3358
  }
@@ -3528,14 +3617,18 @@ var Printer = class {
3528
3617
  if (op === "ifError") return this.#ifError(args, ctx);
3529
3618
  const [head] = args;
3530
3619
  const rest = args.slice(1);
3531
- if (LIST_OPS.has(op) && head?.tag === "each" && !this.#isList(head.body, ctx)) return this.#list(op, head, rest, ctx);
3532
- if (LIST_OPS.has(op) && head?.tag === "let" && rest.every((x) => x.tag === "lit")) return this.#let(head, ctx, (body) => app(op, body, ...rest));
3620
+ if (FOLD_OPS.has(op) && head?.tag === "each" && !this.#isList(head.body, ctx)) return this.#list(op, head, rest, ctx);
3621
+ if (FOLD_OPS.has(op) && head?.tag === "let" && rest.every((x) => x.tag === "lit")) return this.#let(head, ctx, (body) => app(op, body, ...rest));
3533
3622
  if (head !== void 0 && (op === "sortBy" || op === "take" || op === "drop" && this.#isList(head, ctx))) return this.#order(op, head, rest, ctx);
3534
3623
  if (head !== void 0 && BUILD_OPS.has(op) && this.#isList(head, ctx)) return this.#build(op, head, rest, ctx);
3535
- if (head !== void 0 && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, rest, ctx);
3624
+ if (head !== void 0 && (FOLD_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, rest, ctx);
3536
3625
  const raw = args.map((x) => this.expr(x, ctx));
3537
3626
  const deps = depsOf(...raw);
3538
3627
  const values = this.#inOrder(this.#settle(raw, deps, args), true);
3628
+ return this.#opCall(op, values, deps);
3629
+ }
3630
+ /** This method prints the call of the op of a domain on some values, from the static site of their kinds. */
3631
+ #opCall(op, values, deps) {
3539
3632
  const site = this.#facts.staticSite(op, values.map((x) => x.v.kind));
3540
3633
  if (site?.t !== "fn") throw new NotStandalone(`the kinds of the values of "${op}" do not decide its op`);
3541
3634
  if (site.bad) return {
@@ -3700,15 +3793,35 @@ var Printer = class {
3700
3793
  * This method gives the option strict of a list op. The options must be one literal with only strict. Only a literal
3701
3794
  * has a value, so another expression gives the reason too.
3702
3795
  */
3703
- #strict(op, rest) {
3796
+ #strict(op, rest, names = ["strict"]) {
3704
3797
  const [options, ...more] = rest;
3705
3798
  if (more.length > 0) throw new NotStandalone(`"${op}" with more than one option`);
3706
3799
  if (options === void 0) return false;
3707
3800
  const opts = options.value;
3708
- if (typeof opts !== "object" || opts === null || Object.keys(opts).some((k) => k !== "strict")) throw new NotStandalone(`the options of "${op}" are not a literal with strict`);
3801
+ if (typeof opts !== "object" || opts === null || Object.keys(opts).some((k) => !names.includes(k))) throw new NotStandalone(`the options of "${op}" are not a literal with ${names.join(" or ")}`);
3709
3802
  return opts.strict === true;
3710
3803
  }
3711
3804
  /**
3805
+ * This method gives the option strict of a list op. For `reduce`, it also gives the op of each step: the first
3806
+ * argument, a literal text, and the value of the option `domain` (COMPILE.MODULE.REDUCE).
3807
+ */
3808
+ #foldOptions(op, rest) {
3809
+ if (op !== "reduce") return {
3810
+ strict: this.#strict(op, rest),
3811
+ step: void 0
3812
+ };
3813
+ const [name, ...options] = rest;
3814
+ const value = name?.value;
3815
+ if (name?.tag !== "lit" || typeof value !== "string") throw new NotStandalone("\"reduce\" without a literal text as its op");
3816
+ return {
3817
+ strict: this.#strict(op, options, ["strict", "domain"]),
3818
+ step: {
3819
+ op: value,
3820
+ domain: options[0]?.value?.domain
3821
+ }
3822
+ };
3823
+ }
3824
+ /**
3712
3825
  * A list op over an axis: one `for` loop with a running value. An item with an error is skipped, and with `strict` it
3713
3826
  * is an error of the list op. A value of the wrong kind is an error of the list op, as in the interpreter.
3714
3827
  */
@@ -3781,6 +3894,24 @@ var Printer = class {
3781
3894
  inner
3782
3895
  };
3783
3896
  }
3897
+ if (this.#graph && GRAPH_AXES.has(base.t)) {
3898
+ const range = this.#graphRange(base.t, f);
3899
+ const q = this.fresh("q");
3900
+ return {
3901
+ s: [...s, ...range.s],
3902
+ j,
3903
+ head: forHead(q, range.lo, range.hi, reverse),
3904
+ key: [`const ${j} = ${range.a}[${q}];`],
3905
+ check: [],
3906
+ skip: void 0,
3907
+ fixed: false,
3908
+ start,
3909
+ startSafe,
3910
+ tests,
3911
+ inner
3912
+ };
3913
+ }
3914
+ if (this.#graph && base.t === "window") throw new NotStandalone("the axis \"window\" in a graph space");
3784
3915
  if (base.t === "others") skip = f;
3785
3916
  else if (base.t === "window") {
3786
3917
  const h = this.fresh("h");
@@ -3803,6 +3934,26 @@ var Printer = class {
3803
3934
  };
3804
3935
  }
3805
3936
  /**
3937
+ * This method gives the targets of an axis of a graph space at the position `f` (AXIS.GRAPH). They are the positions
3938
+ * `a[q]` for `q` from `lo` up to `hi`. The first key of the search of `reachable` is the focus, which is not a target.
3939
+ */
3940
+ #graphRange(t, f) {
3941
+ const a = this.fresh("A");
3942
+ const range = (code, lo = "0") => ({
3943
+ s: [`const ${a} = ${code};`],
3944
+ a,
3945
+ lo,
3946
+ hi: `${a}.length`,
3947
+ skip: void 0
3948
+ });
3949
+ if (t === "successors") return range(`GO[${f}]`);
3950
+ if (t === "predecessors") {
3951
+ this.into = true;
3952
+ return range(`GN[${f}]`);
3953
+ }
3954
+ return range(`${this.use("RB")}(GO, ${f}, n)`, "1");
3955
+ }
3956
+ /**
3806
3957
  * This method gives the targets of an axis of a tree space at the position `f`. They are the positions `a[q]` for `q`
3807
3958
  * from `lo` up to `hi`, without the key that the loop skips. The axes come from AXIS.TREE, AXIS.GRAPH and AXIS.WINDOW.
3808
3959
  */
@@ -3851,7 +4002,7 @@ var Printer = class {
3851
4002
  }
3852
4003
  }
3853
4004
  #list(op, target, rest, ctx) {
3854
- const strict = this.#strict(op, rest);
4005
+ const { strict, step } = this.#foldOptions(op, rest);
3855
4006
  const at = target.at ?? [];
3856
4007
  const { s, j, head, key, check, skip, fixed, start, startSafe, tests, inner } = this.#targetLoop(target.axis, at, ctx);
3857
4008
  const { level } = inner;
@@ -3859,7 +4010,7 @@ var Printer = class {
3859
4010
  const body = this.#hoist(this.expr(target.body, inner), hasLoop(target.body));
3860
4011
  if (op === "count" && fixed && tests.length === 0 && body.v.safe && !body.v.deps.has(CELLS)) return bare(plainValue("n", "number"));
3861
4012
  const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== level));
3862
- const r = this.#reduction(op, body.v.kind);
4013
+ const r = this.#reduction(op, body.v.kind, step);
3863
4014
  const { v } = r;
3864
4015
  s.push(r.init);
3865
4016
  const item = [
@@ -3900,7 +4051,7 @@ var Printer = class {
3900
4051
  ],
3901
4052
  v: {
3902
4053
  code: r.result,
3903
- kind: listKind(op, body.v.kind),
4054
+ kind: r.kind,
3904
4055
  safe: false,
3905
4056
  deps
3906
4057
  }
@@ -3926,7 +4077,7 @@ var Printer = class {
3926
4077
  s,
3927
4078
  v: {
3928
4079
  code: r.result,
3929
- kind: listKind(op, body.v.kind),
4080
+ kind: r.kind,
3930
4081
  safe,
3931
4082
  deps
3932
4083
  }
@@ -3936,10 +4087,15 @@ var Printer = class {
3936
4087
  * This method gives the parts of a list op that reduces the values of its items. The parts are the variable `v` of
3937
4088
  * the value of an item, the first statement, the step and the result. A value of the wrong kind is an error of the op.
3938
4089
  */
3939
- #reduction(op, kind) {
4090
+ #reduction(op, kind, step) {
3940
4091
  const a = this.fresh("a");
3941
4092
  const n = this.fresh("n");
3942
4093
  const v = this.fresh("x");
4094
+ if (step !== void 0) return {
4095
+ v,
4096
+ init: `let ${a}, ${n} = 0;`,
4097
+ ...this.#reduceStep(step, kind, a, n, v)
4098
+ };
3943
4099
  const num = kind === "number" ? v : `${this.use("U")}(${v})`;
3944
4100
  const bool = kind === "boolean" ? v : `${this.use("B")}(${v})`;
3945
4101
  const inits = {
@@ -3974,7 +4130,51 @@ var Printer = class {
3974
4130
  v,
3975
4131
  init: inits[op] ?? `let ${a}, ${n} = 0;`,
3976
4132
  step: steps[op],
3977
- result: results[op] ?? `(${n} === 0 ? ${F}() : ${a})`
4133
+ result: results[op] ?? `(${n} === 0 ? ${F}() : ${a})`,
4134
+ kind: listKind(op, kind)
4135
+ };
4136
+ }
4137
+ /**
4138
+ * This method gives the step and the result of `reduce` (COMPILE.MODULE.REDUCE), as in the interpreter. The first item
4139
+ * is the first running value. Each later item goes into the op of the domain of the running value. The kinds of the
4140
+ * items decide that op, and the op must give a value of the same kind. No items give the identity of the op.
4141
+ */
4142
+ #reduceStep(step, kind, a, n, v) {
4143
+ const arg = (code) => bare({
4144
+ code,
4145
+ kind,
4146
+ safe: true,
4147
+ deps: NO_DEPS,
4148
+ plain: true
4149
+ });
4150
+ const opCode = this.#opCall(step.op, [arg(a), arg(v)], NO_DEPS);
4151
+ if (opCode.v.kind !== kind) throw new NotStandalone(`the op "${step.op}" of "reduce" gives a value of the kind ${kindText(opCode.v.kind)}, not of the kind ${kindText(kind)} of the items`);
4152
+ const empty = this.#identity(step);
4153
+ return {
4154
+ step: `${a} = ${n}++ === 0 ? ${v} : ${opCode.v.code};`,
4155
+ result: `(${n} === 0 ? ${empty.code} : ${a})`,
4156
+ kind: empty.kind === void 0 || empty.kind === kind ? kind : void 0
4157
+ };
4158
+ }
4159
+ /**
4160
+ * This method gives the code of `reduce` over no items, as in the interpreter. The code is the identity of the op in
4161
+ * the one domain that has one, or in the domain of the option `domain`. Another case is an error. The kind is the kind
4162
+ * of the domain.
4163
+ */
4164
+ #identity(step) {
4165
+ const { op, domain } = step;
4166
+ const fail = {
4167
+ code: `${this.use("F")}()`,
4168
+ kind: void 0
4169
+ };
4170
+ const [only, second] = [...this.#domains.entries()].filter(([, d]) => Object.hasOwn(d.ops, op) && d.ops[op].identity !== void 0 && (domain === void 0 || d.name === domain));
4171
+ if (only === void 0 || second !== void 0) return fail;
4172
+ const [i, d] = only;
4173
+ const name = `id_${String(i)}_${String(this.#opIndex(op))}`;
4174
+ if (!this.top.has(name)) this.top.set(name, `const ${name} = ${copied(d.ops[op].identity, [], `the identity of the op "${op}" of the domain ${d.name}`)};`);
4175
+ return {
4176
+ code: `${name}()`,
4177
+ kind: kindOfDomain(d)
3978
4178
  };
3979
4179
  }
3980
4180
  /**
@@ -4089,7 +4289,7 @@ var Printer = class {
4089
4289
  #overList(op, head, rest, ctx) {
4090
4290
  const list = this.#listOf(head, ctx);
4091
4291
  const exprs = [head, ...op === "nth" ? [rest[0] ?? lit(null)] : []];
4092
- const strict = this.#strict(op, rest.slice(exprs.length - 1));
4292
+ const { strict, step } = this.#foldOptions(op, rest.slice(exprs.length - 1));
4093
4293
  const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
4094
4294
  const deps = depsOf(...raw);
4095
4295
  const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
@@ -4105,7 +4305,7 @@ var Printer = class {
4105
4305
  list: inner
4106
4306
  }
4107
4307
  };
4108
- const r = this.#reduction(op, item);
4308
+ const r = this.#reduction(op, item, step);
4109
4309
  const { v } = r;
4110
4310
  return {
4111
4311
  s: [
@@ -4118,7 +4318,7 @@ var Printer = class {
4118
4318
  ],
4119
4319
  v: {
4120
4320
  code: r.result,
4121
- kind: listKind(op, item),
4321
+ kind: r.kind,
4122
4322
  safe: false,
4123
4323
  deps,
4124
4324
  list: op === "first" ? inner : void 0
@@ -4501,11 +4701,12 @@ var Printer = class {
4501
4701
  function runOf(options) {
4502
4702
  const name = options.name ?? "run";
4503
4703
  if (!/^[A-Za-z_$][\w$]*$/.test(name)) return { reason: `the name "${name}" is not a JavaScript name` };
4504
- const tree = options.space === "tree";
4505
- if (!tree && options.space !== void 0 && options.space !== "array") return { reason: `the space "${String(options.space)}" is not "array" or "tree"` };
4704
+ const space = options.space ?? "array";
4705
+ if (space !== "array" && space !== "tree" && space !== "graph") return { reason: `the space "${String(space)}" is not "array", "tree" or "graph"` };
4506
4706
  return {
4507
4707
  name,
4508
- tree
4708
+ tree: space === "tree",
4709
+ graph: space === "graph"
4509
4710
  };
4510
4711
  }
4511
4712
  /** This function gives the reason of an error of the printer. Another exception is a defect, so it goes to the caller. */
@@ -4537,7 +4738,7 @@ function printModule(p, options, run, body) {
4537
4738
  } catch (thrown) {
4538
4739
  return reasonOf(thrown);
4539
4740
  }
4540
- const { name, tree } = run;
4741
+ const { name, tree, graph } = run;
4541
4742
  if (tree) p.use("PT");
4542
4743
  if (p.treeParts.size > 0) p.treeParts.add("children");
4543
4744
  const parts = [
@@ -4546,6 +4747,8 @@ function printModule(p, options, run, body) {
4546
4747
  "order"
4547
4748
  ].filter((x) => p.treeParts.has(x));
4548
4749
  for (const x of parts) p.use(TREE_PARTS[x][0]);
4750
+ if (graph) p.use("GR");
4751
+ if (p.into) p.use("GI");
4549
4752
  [...p.helpers].toSorted().forEach(add);
4550
4753
  const bad = nameReason(name, /* @__PURE__ */ new Set([
4551
4754
  "VexContractError",
@@ -4573,7 +4776,7 @@ function printModule(p, options, run, body) {
4573
4776
  ...helpers.has("S") ? ["const N = {}, W = {};"] : [],
4574
4777
  ...[...helpers].map((h) => h === "S" ? helperS(p.features) : HELPERS[h]),
4575
4778
  ...p.top.values(),
4576
- `export function ${name}(records, vars${tree ? ", parents" : ""}) {`,
4779
+ `export function ${name}(records, vars${tree ? ", parents" : graph ? ", edges" : ""}) {`,
4577
4780
  " if (!Array.isArray(records)) C(\"the records are not an array\");",
4578
4781
  " const R = records;",
4579
4782
  " const n = R.length;",
@@ -4583,6 +4786,7 @@ function printModule(p, options, run, body) {
4583
4786
  " }",
4584
4787
  ` if (typeof vars !== "object" || vars === null${varChecks}) C("the variables do not keep the schema");`,
4585
4788
  ...tree ? [" const P = PT(parents, n);", ...parts.map((x) => ` ${TREE_PARTS[x][1]}`)] : [],
4789
+ ...graph ? [" const GO = GR(edges, n);", ...p.into ? [" const GN = GI(GO, n);"] : []] : [],
4586
4790
  ...[...p.vars].map(([v, code]) => ` const ${code} = vars[${JSON.stringify(v)}];`),
4587
4791
  ...ind(p.run),
4588
4792
  ...ind(body),
@@ -4689,9 +4893,11 @@ function emitSheetModule(columns, options) {
4689
4893
  }
4690
4894
  const kinds = new Map(Object.entries(options.kinds ?? {}));
4691
4895
  const index = new Map(names.map((c, i) => [c, i]));
4896
+ const direct = formulas.every((f) => f.key === void 0) && order.every((c) => readsFinished(columns, order, c, run.tree));
4692
4897
  const p = new Printer(rec(Object.fromEntries(formulas.map((f, i) => [`f${String(i)}`, f.expr]))), options, {
4693
4898
  columns: index,
4694
- kinds
4899
+ kinds,
4900
+ direct
4695
4901
  });
4696
4902
  const fns = [];
4697
4903
  const decls = [];
@@ -4727,28 +4933,29 @@ function emitSheetModule(columns, options) {
4727
4933
  const i = formulas.findIndex((f) => f.column === c && f.key === void 0);
4728
4934
  return i < 0 ? "undefined" : entry(i);
4729
4935
  });
4730
- p.use("SH");
4936
+ p.use(direct ? "DO" : "SH");
4937
+ const fnOf = new Map(formulas.map((f, i) => [f.column, fns[i]]));
4938
+ const fillOf = (c, at) => direct ? `D[${at}][o] = ${fnOf.get(c)}(o);` : `SS.f(${at}, o);`;
4731
4939
  const orders = /* @__PURE__ */ new Map();
4732
4940
  const fill = order.map((c) => {
4733
4941
  const { reverse, post, back } = keyOrderOf(readsOf(columns, cells, c).directions);
4734
4942
  const at = String(index.get(c));
4735
- if (!run.tree) return reverse ? `for (let o = n - 1; o >= 0; o--) SS.f(${at}, o);` : `for (let o = 0; o < n; o++) SS.f(${at}, o);`;
4943
+ if (!run.tree) return reverse ? `for (let o = n - 1; o >= 0; o--) ${fillOf(c, at)}` : `for (let o = 0; o < n; o++) ${fillOf(c, at)}`;
4736
4944
  const args = `${p.use("KO")}(H, RT, ${String(post)}, ${String(back)})`;
4737
4945
  let keys = orders.get(args);
4738
4946
  if (keys === void 0) {
4739
4947
  keys = p.fresh("K");
4740
4948
  orders.set(args, keys);
4741
4949
  }
4742
- return `for (const o of ${keys}) SS.f(${at}, o);`;
4950
+ return `for (const o of ${keys}) ${fillOf(c, at)}`;
4743
4951
  });
4744
4952
  if (run.tree) p.treeParts.add("children");
4745
4953
  return printModule(p, options, run, [
4746
4954
  ...decls,
4747
- `const SS = SH(n, [${cols.join(", ")}], [${own.join(", ")}], ${String(budget)});`,
4748
- `const ${READ_CELL} = SS.r;`,
4955
+ ...direct ? [`const D = [${names.map(() => "new Array(n)").join(", ")}];`, `const ${READ_CELL} = (c, k) => D[c][k];`] : [`const SS = SH(n, [${cols.join(", ")}], [${own.join(", ")}], ${String(budget)});`, `const ${READ_CELL} = SS.r;`],
4749
4956
  ...[...orders].map(([args, keys]) => `const ${keys} = ${args};`),
4750
4957
  ...fill,
4751
- `return { ${names.map((c, i) => `${JSON.stringify(c)}: SS.o(${String(i)})`).join(", ")} };`
4958
+ `return { ${names.map((c, i) => `${JSON.stringify(c)}: ${direct ? `DO(D[${String(i)}])` : `SS.o(${String(i)})`}`).join(", ")} };`
4752
4959
  ]);
4753
4960
  }
4754
4961
  //#endregion