@mark1russell7/vex-compiler 0.13.2 → 0.13.3

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
@@ -225,11 +225,20 @@ interface SheetModuleOptions extends ModuleOptions {
225
225
  /**
226
226
  * The kind of the values of each column. The printer prints each column after the columns that it reads. A column
227
227
  * that a formula reads before its own formula prints needs its kind here, for example a column that reads itself.
228
+ * A column of lists has a `ListKind` (COMPILE.MODULE.SHEET.LIST).
228
229
  */
229
- readonly kinds?: Readonly<Record<string, Kind>>;
230
+ readonly kinds?: Readonly<Record<string, Kind | ListKind>>;
230
231
  /** The budget of the stack of the search of a run, in estimated frames, as in `SheetRun`. The default is 3000. */
231
232
  readonly budget?: number;
232
233
  }
234
+ /**
235
+ * The kind of a column of lists in a module of a sheet (COMPILE.MODULE.SHEET.LIST): the kind of the items, which can be
236
+ * a list too. With `texts`, a key of an item can be the text of a key of a list literal.
237
+ */
238
+ interface ListKind {
239
+ readonly list: Kind | ListKind;
240
+ readonly texts?: boolean;
241
+ }
233
242
  /** The source of a standalone module, or the reason that the program cannot be one. */
234
243
  type Emitted = {
235
244
  readonly ok: true;
@@ -328,5 +337,5 @@ interface Runtime {
328
337
  /** This function makes the runtime of one set of options. */
329
338
  export declare function createRuntime(options: RuntimeOptions): Runtime;
330
339
  //#endregion
331
- export type { Emitted, Fallback, GenerateOptions, Generated, LExpr, LStmt, LoopProgram, ModuleOptions, PrimOp, Runtime, RuntimeFn, RuntimeOptions, Schema, SheetModuleOptions, Tier2, TierStats, TieredProgram };
340
+ export type { Emitted, Fallback, GenerateOptions, Generated, LExpr, LStmt, ListKind, LoopProgram, ModuleOptions, PrimOp, Runtime, RuntimeFn, RuntimeOptions, Schema, SheetModuleOptions, Tier2, TierStats, TieredProgram };
332
341
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/facts.ts","../src/loop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E,IAAI,MAAM,SAAS,sCAAsC;;;wBAI3C,cAAc,SAAS,iBAAiB"}
package/build/index.js CHANGED
@@ -1957,8 +1957,12 @@ function baseOf(axis) {
1957
1957
  * - in a tree in post-order, at the key of a target of the axis `children` of the key.
1958
1958
  *
1959
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
+ * The columns of `fused` come before `self` in one loop over the keys (COMPILE.MODULE.ONCE). The loop evaluates each of
1962
+ * them at a key before `self` at that key. Thus a read of one of them reads a finished cell only at the key itself, or
1963
+ * in one of the three forms.
1960
1964
  */
1961
- function readsFinished(columns, order, self, tree) {
1965
+ function readsFinished(columns, order, self, tree, fused = /* @__PURE__ */ new Set()) {
1962
1966
  const formula = columns.get(self);
1963
1967
  if (formula === void 0 || shapeError(formula) !== void 0) return false;
1964
1968
  const { reverse, post } = keyOrderOf(readsOf(columns, /* @__PURE__ */ new Map(), self).directions);
@@ -1981,9 +1985,9 @@ function readsFinished(columns, order, self, tree) {
1981
1985
  else if (e.kind === CELL_KIND && isCellRef(e.data)) {
1982
1986
  const { column, at } = e.data;
1983
1987
  if (order.indexOf(column) > position) return false;
1984
- if (column !== self) continue;
1988
+ if (column !== self && !fused.has(column)) continue;
1985
1989
  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;
1990
+ if (!(column !== self && place === "key" && at.every((m) => m.t === "origin") || (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
1991
  }
1988
1992
  }
1989
1993
  return true;
@@ -2011,12 +2015,14 @@ function readsFinished(columns, order, self, tree) {
2011
2015
  * the fields and the variables of the schema. It takes the axes `all`, `others` and `window`. A tree space also has the
2012
2016
  * move `parent` and the axes of a tree (COMPILE.MODULE.TREE). A graph space has the axes of a graph
2013
2017
  * (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.
2018
+ * lists as names of loops are in a module (COMPILE.MODULE.LIST, COMPILE.MODULE.REDUCE). The value of a program can be a
2019
+ * list, and the run gives it as a `vex.list` (COMPILE.MODULE.LIST.RESULT). The kinds of each op site must decide its op.
2020
+ * Another construct gives a reason, not code.
2016
2021
  *
2017
2022
  * `emitSheetModule` prints the formula columns of a sheet as one module (COMPILE.MODULE.SHEET). Each column is an array
2018
2023
  * of results, and the run fills the arrays in the static order of `SheetRun.fill`. A read of a cell that is not finished
2019
- * starts the search of `SheetRun`. Thus each cell on a cycle gives the error of Vex.
2024
+ * starts the search of `SheetRun`. Thus each cell on a cycle gives the error of Vex. A cell can hold a list, and a
2025
+ * formula reads it with the list ops (COMPILE.MODULE.SHEET.LIST).
2020
2026
  */
2021
2027
  /** The error of a construct that a standalone module does not have. */
2022
2028
  var NotStandalone = class extends Error {};
@@ -2182,6 +2188,8 @@ const HELPERS = {
2182
2188
  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
2189
  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
2190
  DO: "function DO(d) { for (let k = 0; k < d.length; k++) if (d[k] === E) d[k] = VEX_ERROR; return d; }",
2191
+ LO: "function LO(l, h) { return { kind: \"vex.list\", items: l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LO(v, h - 1) : v } })) }; }",
2192
+ LC: "function LC(d, h) { for (let k = 0; k < d.length; k++) if (d[k] !== VEX_ERROR) d[k] = LO(d[k], h); return d; }",
2185
2193
  SH: [
2186
2194
  "function SH(n, cols, own, budget) {",
2187
2195
  " const pg = (c, k) => {",
@@ -2465,7 +2473,9 @@ const NEEDS = {
2465
2473
  RB: [],
2466
2474
  SH: [],
2467
2475
  KO: [],
2468
- DO: []
2476
+ DO: [],
2477
+ LO: [],
2478
+ LC: ["LO"]
2469
2479
  };
2470
2480
  /** The statement of each part of a tree in a run, with the helper that computes it. */
2471
2481
  const TREE_PARTS = {
@@ -2546,43 +2556,52 @@ const LIST_RESULTS = /* @__PURE__ */ new Set([
2546
2556
  * This function tells if an expression gives a list. An each, a list literal and a list op of `LIST_RESULTS` give a
2547
2557
  * list. A name of a list, and a let or a loop whose result is a list, give a list too. `isName` tells if a name outside the expression holds a list.
2548
2558
  */
2549
- function listy(e, isName) {
2559
+ function listy(e, isName, isCell) {
2550
2560
  switch (e.tag) {
2551
2561
  case "each": return true;
2552
2562
  case "lit": return isVexList(e.value);
2553
- case "app": return LIST_RESULTS.has(e.op);
2563
+ case "app": return LIST_RESULTS.has(e.op) || (e.op === "if" || e.op === "ifError") && e.args.slice(e.op === "if" ? 1 : 0).some((x) => listy(x, isName, isCell));
2554
2564
  case "var": return isName(e.name);
2555
- case "let": return listy(e.body, namesIn(e.bind, isName));
2565
+ case "let": return listy(e.body, namesIn(e.bind, isName, isCell), isCell);
2556
2566
  case "fold":
2557
- case "iterate": return listy(e.result, namesIn(e.init, isName));
2567
+ case "iterate": return listy(e.result, namesIn(e.init, isName, isCell), isCell);
2568
+ case "ext": return isCell(e);
2558
2569
  case "call":
2559
- case "ext":
2560
2570
  case "fn":
2561
2571
  case "rec":
2562
2572
  case "ref": return false;
2563
2573
  }
2564
2574
  }
2565
2575
  /** This function tells if a name holds a list in the scope of some bindings. A binding hides a name outside, and a function is not a list. */
2566
- function namesIn(bind, isName) {
2567
- const lists = new Map(Object.entries(bind).map(([name, x]) => [name, x.tag !== "fn" && listy(x, isName)]));
2576
+ function namesIn(bind, isName, isCell) {
2577
+ const lists = new Map(Object.entries(bind).map(([name, x]) => [name, x.tag !== "fn" && listy(x, isName, isCell)]));
2568
2578
  return (name) => lists.get(name) ?? isName(name);
2569
2579
  }
2570
2580
  /** This function tells if an expression has a list literal with an item, so a key of the list can be a text (LIST.LITERAL). */
2571
2581
  function hasKeyedLiteral(e) {
2572
2582
  return e.tag === "lit" && isVexList(e.value) && e.value.items.length > 0 || children(e).some(hasKeyedLiteral);
2573
2583
  }
2574
- /** This function gives the facts of the list of `append` from the facts of its two lists. An empty list fits each list. */
2575
- function joinLists(a, b) {
2584
+ /**
2585
+ * This function gives the facts of the list of `append` from the facts of its two lists. It also gives the list of
2586
+ * `if` from its two branches. An empty list fits each list. The op `op` names the op in the reason.
2587
+ */
2588
+ function joinLists(a, b, op) {
2576
2589
  if (a.empty === true) return b;
2577
2590
  if (b.empty === true) return a;
2578
- if (a.inner === void 0 !== (b.inner === void 0)) throw new NotStandalone("\"append\" of a list of lists and a list of values");
2591
+ if (a.inner === void 0 !== (b.inner === void 0)) throw new NotStandalone(`"${op}" of a list of lists and a list of values`);
2579
2592
  const texts = a.texts === true || b.texts === true;
2580
2593
  return {
2581
2594
  item: join(a.item, b.item),
2582
- inner: a.inner === void 0 ? void 0 : joinLists(a.inner, b.inner),
2595
+ inner: a.inner === void 0 ? void 0 : joinLists(a.inner, b.inner, op),
2583
2596
  texts
2584
2597
  };
2585
2598
  }
2599
+ /** This function gives the facts of the list of `if` or `ifError` from its two branches. Two values that are not lists give undefined. */
2600
+ function branchLists(op, a, b) {
2601
+ if (a === void 0 && b === void 0) return void 0;
2602
+ if (a === void 0 || b === void 0) throw new NotStandalone(`"${op}" of a list and a value that is not a list`);
2603
+ return joinLists(a, b, op);
2604
+ }
2586
2605
  /**
2587
2606
  * This function tells if a list of a step fits the list of a name of a loop (LOOP.LIST). The items of the step have the
2588
2607
  * kind of the items of the name, if the name has one. The name also has the text keys and the lists of lists of the step.
@@ -3109,7 +3128,11 @@ var Printer = class {
3109
3128
  }
3110
3129
  /** This method tells if an expression gives a list at a place that takes one (`listy`). */
3111
3130
  #isList(e, ctx) {
3112
- return listy(e, (name) => ctx.env.get(name)?.list !== void 0);
3131
+ return listy(e, (name) => ctx.env.get(name)?.list !== void 0, (x) => this.#cellList(x) !== void 0);
3132
+ }
3133
+ /** This method gives the facts of the list of a read of a cell. A read of a column that holds no list gives undefined. */
3134
+ #cellList(e) {
3135
+ return e.kind === CELL_KIND && isCellRef(e.data) ? this.#sheet?.lists.get(e.data.column) : void 0;
3113
3136
  }
3114
3137
  /** This method prints a list literal (LIST.LITERAL) as an array of pairs of a key and a value. An error item holds the sentinel. */
3115
3138
  #listLiteral(l) {
@@ -3257,19 +3280,23 @@ var Printer = class {
3257
3280
  const { column, at } = e.data;
3258
3281
  const deps = /* @__PURE__ */ new Set([...this.#addressDeps(at, ctx, []), CELLS]);
3259
3282
  const index = sheet.columns.get(column);
3283
+ const list = sheet.lists.get(column);
3260
3284
  if (index === void 0) return bare({
3261
3285
  code: `${F}()`,
3262
3286
  kind: sheet.kinds.get(column),
3263
3287
  safe: false,
3264
- deps
3288
+ deps,
3289
+ list
3265
3290
  });
3266
3291
  if (!sheet.kinds.has(column)) throw new NotStandalone(`a formula reads the column "${column}" before its formula, so the column needs its kind in the option kinds`);
3267
- const read = `${READ_CELL}(${String(index)}, ${this.#address(at, ctx, [])})`;
3292
+ const address = this.#address(at, ctx, []);
3293
+ const read = sheet.direct ? `D[${String(index)}][${address}]` : `${READ_CELL}(${String(index)}, ${address})`;
3268
3294
  const v = {
3269
3295
  code: `${this.use("G")}(${read})`,
3270
3296
  kind: sheet.kinds.get(column),
3271
3297
  safe: false,
3272
- deps
3298
+ deps,
3299
+ list
3273
3300
  };
3274
3301
  return bare(at.every((m) => m.t === "origin") ? {
3275
3302
  ...v,
@@ -3612,6 +3639,7 @@ var Printer = class {
3612
3639
  }
3613
3640
  #app(e, ctx) {
3614
3641
  const { op, args } = e;
3642
+ if ((op === "if" || op === "ifError") && this.#wantList.has(e)) for (const x of args.slice(op === "if" ? 1 : 0)) this.#wantList.add(x);
3615
3643
  if (op === "if") return this.#if(args, ctx);
3616
3644
  if (op === "and" || op === "or") return this.#andOr(op === "and", args, ctx);
3617
3645
  if (op === "ifError") return this.#ifError(args, ctx);
@@ -3695,7 +3723,8 @@ var Printer = class {
3695
3723
  const v = {
3696
3724
  kind: join(yes.v.kind, no.v.kind),
3697
3725
  safe: this.#safeBoolean(cond.v) && yes.v.safe && no.v.safe,
3698
- deps
3726
+ deps,
3727
+ list: branchLists("if", yes.v.list, no.v.list)
3699
3728
  };
3700
3729
  if (yes.s.length === 0 && no.s.length === 0) return {
3701
3730
  s: cond.s,
@@ -3777,7 +3806,8 @@ var Printer = class {
3777
3806
  code: t,
3778
3807
  kind: join(first.v.kind, second.v.kind),
3779
3808
  safe: second.v.safe,
3780
- deps
3809
+ deps,
3810
+ list: branchLists("ifError", first.v.list, second.v.list)
3781
3811
  }
3782
3812
  };
3783
3813
  }
@@ -4280,7 +4310,7 @@ var Printer = class {
4280
4310
  if (op === "get") return value(info.item, info.inner);
4281
4311
  if (op === "indexOf") return value("number", void 0);
4282
4312
  if (op === "without") return value(void 0, info);
4283
- if (op === "append") return value(void 0, joinLists(info, parts[1].v.list));
4313
+ if (op === "append") return value(void 0, joinLists(info, parts[1].v.list, op));
4284
4314
  if (info.empty === true) return value(void 0, info);
4285
4315
  if (info.inner === void 0) throw new NotStandalone("\"flat\" over a list whose items are not lists");
4286
4316
  return value(void 0, info.inner);
@@ -4665,6 +4695,7 @@ var Printer = class {
4665
4695
  /** This method gives the code of the program at the key of the run. A constant of the run evaluates once. */
4666
4696
  /** This method gives the code of a program at the key `o` of the run. A sheet has one program for each formula. */
4667
4697
  root(expr) {
4698
+ this.#wantList.add(expr);
4668
4699
  return this.#hoist(this.expr(expr, {
4669
4700
  origin: "o",
4670
4701
  focus: "o",
@@ -4829,12 +4860,13 @@ function emitModule(source, options) {
4829
4860
  } catch (thrown) {
4830
4861
  return reasonOf(thrown);
4831
4862
  }
4863
+ const result = value.v.list === void 0 ? value.v.code : `${p.use("LO")}(${value.v.code}, ${String(depthOf(value.v.list))})`;
4832
4864
  return printModule(p, options, run, [
4833
4865
  "const out = [];",
4834
4866
  "for (let o = 0; o < n; o++) {",
4835
4867
  " try {",
4836
4868
  ...ind(ind(value.s)),
4837
- ` out.push(${value.v.code});`,
4869
+ ` out.push(${result});`,
4838
4870
  " } catch {",
4839
4871
  " out.push(VEX_ERROR);",
4840
4872
  " }",
@@ -4846,8 +4878,31 @@ function emitModule(source, options) {
4846
4878
  const SHEET_BUDGET = 3e3;
4847
4879
  /** This function gives the words for a formula in a reason: the formula of a column, or of one cell. */
4848
4880
  const formulaText = (f) => f.key === void 0 ? `the formula of the column "${f.column}"` : `the formula of the cell "${f.column}" at "${String(f.key)}"`;
4849
- /** This function gives the words for a kind in a reason. */
4850
- const kindText = (k) => k ?? "that the printer does not know";
4881
+ /** This function gives the words for a kind in a reason. A list gives the kind of its items. */
4882
+ const kindText = (k, l) => l === void 0 ? k ?? "that the printer does not know" : `list of ${l.inner === void 0 ? kindText(l.item) : kindText(void 0, l.inner)}`;
4883
+ /** This function gives the number of the levels of lists in the items of a list: 0 for a list of values. */
4884
+ const depthOf = (l) => l.inner === void 0 ? 0 : 1 + depthOf(l.inner);
4885
+ /** This function gives the facts of the lists of a column from the kind of the option `kinds` (COMPILE.MODULE.SHEET.LIST). */
4886
+ const listOfKind = (k) => {
4887
+ const texts = k.texts === true;
4888
+ return typeof k.list === "object" ? {
4889
+ item: void 0,
4890
+ inner: listOfKind(k.list),
4891
+ texts
4892
+ } : {
4893
+ item: k.list,
4894
+ texts
4895
+ };
4896
+ };
4897
+ /**
4898
+ * This function gives the facts of the lists of a column from the list of its first formula. The other cells of the
4899
+ * column can have other formulas. Thus a list without items does not tell that each list of the column is empty.
4900
+ */
4901
+ const columnList = (l) => ({
4902
+ item: l.item,
4903
+ inner: l.inner === void 0 ? void 0 : columnList(l.inner),
4904
+ texts: l.texts === true
4905
+ });
4851
4906
  /**
4852
4907
  * This function prints the formula columns of a sheet as a standalone ES module (COMPILE.MODULE.SHEET). The function of
4853
4908
  * the module takes the records and the variables, and the parents for a tree. It gives an object with an array for
@@ -4891,22 +4946,46 @@ function emitSheetModule(columns, options) {
4891
4946
  reason: `${formulaText(f)}: ${shape}`
4892
4947
  };
4893
4948
  }
4894
- const kinds = new Map(Object.entries(options.kinds ?? {}));
4949
+ const kinds = /* @__PURE__ */ new Map();
4950
+ const lists = /* @__PURE__ */ new Map();
4951
+ for (const [c, k] of Object.entries(options.kinds ?? {})) {
4952
+ kinds.set(c, typeof k === "object" ? void 0 : k);
4953
+ if (typeof k === "object") lists.set(c, listOfKind(k));
4954
+ }
4895
4955
  const index = new Map(names.map((c, i) => [c, i]));
4896
4956
  const direct = formulas.every((f) => f.key === void 0) && order.every((c) => readsFinished(columns, order, c, run.tree));
4897
4957
  const p = new Printer(rec(Object.fromEntries(formulas.map((f, i) => [`f${String(i)}`, f.expr]))), options, {
4898
4958
  columns: index,
4899
4959
  kinds,
4960
+ lists,
4900
4961
  direct
4901
4962
  });
4902
4963
  const fns = [];
4903
4964
  const decls = [];
4965
+ const blocks = /* @__PURE__ */ new Map();
4904
4966
  try {
4905
4967
  for (const f of formulas) {
4906
4968
  const code = p.root(f.expr);
4907
- const kind = code.v.kind;
4908
- if (!kinds.has(f.column)) kinds.set(f.column, kind);
4909
- else if (kinds.get(f.column) !== kind) throw new NotStandalone(`${formulaText(f)} gives a value of the kind ${kindText(kind)}, not of the kind ${kindText(kinds.get(f.column))} of its column`);
4969
+ const { kind, list } = code.v;
4970
+ if (!kinds.has(f.column)) {
4971
+ kinds.set(f.column, kind);
4972
+ if (list !== void 0) lists.set(f.column, columnList(list));
4973
+ } else {
4974
+ const known = lists.get(f.column);
4975
+ if (kinds.get(f.column) !== kind || known === void 0 !== (list === void 0) || known !== void 0 && !fits(known, list)) throw new NotStandalone(`${formulaText(f)} gives a value of the kind ${kindText(kind, list)}, not of the kind ${kindText(kinds.get(f.column), known)} of its column`);
4976
+ }
4977
+ if (direct) {
4978
+ const at = `D[${String(index.get(f.column))}][o]`;
4979
+ blocks.set(f.column, code.v.safe && code.s.length === 0 ? [`${at} = ${code.v.code};`] : [
4980
+ "try {",
4981
+ ...ind(code.s),
4982
+ ` ${at} = ${code.v.code};`,
4983
+ "} catch {",
4984
+ ` ${at} = E;`,
4985
+ "}"
4986
+ ]);
4987
+ continue;
4988
+ }
4910
4989
  const fn = p.fresh("cf");
4911
4990
  fns.push(fn);
4912
4991
  decls.push(...code.v.safe && code.s.length === 0 ? [`const ${fn} = (o) => ${code.v.code};`] : [
@@ -4924,38 +5003,56 @@ function emitSheetModule(columns, options) {
4924
5003
  } catch (thrown) {
4925
5004
  return reasonOf(thrown);
4926
5005
  }
4927
- const entry = (i) => `[${fns[i]}, ${String(3 * nestingOf(formulas[i].expr) + 16)}]`;
4928
- const own = names.map((c) => {
4929
- const at = formulas.flatMap((f, i) => f.column === c && f.key !== void 0 ? [`${String(f.key)}: ${entry(i)}`] : []);
4930
- return at.length === 0 ? "undefined" : `{ ${at.join(", ")} }`;
4931
- });
4932
- const cols = names.map((c) => {
4933
- const i = formulas.findIndex((f) => f.column === c && f.key === void 0);
4934
- return i < 0 ? "undefined" : entry(i);
4935
- });
4936
5006
  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);`;
4939
5007
  const orders = /* @__PURE__ */ new Map();
4940
- const fill = order.map((c) => {
5008
+ const headOf = (c) => {
4941
5009
  const { reverse, post, back } = keyOrderOf(readsOf(columns, cells, c).directions);
4942
- const at = String(index.get(c));
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)}`;
5010
+ if (!run.tree) return reverse ? "for (let o = n - 1; o >= 0; o--)" : "for (let o = 0; o < n; o++)";
4944
5011
  const args = `${p.use("KO")}(H, RT, ${String(post)}, ${String(back)})`;
4945
5012
  let keys = orders.get(args);
4946
5013
  if (keys === void 0) {
4947
5014
  keys = p.fresh("K");
4948
5015
  orders.set(args, keys);
4949
5016
  }
4950
- return `for (const o of ${keys}) ${fillOf(c, at)}`;
4951
- });
5017
+ return `for (const o of ${keys})`;
5018
+ };
5019
+ let fill;
5020
+ if (direct) {
5021
+ const loops = [];
5022
+ for (const c of order) {
5023
+ const head = headOf(c);
5024
+ const last = loops.at(-1);
5025
+ if (last?.head === head && readsFinished(columns, order, c, run.tree, new Set(last.columns))) last.columns.push(c);
5026
+ else loops.push({
5027
+ head,
5028
+ columns: [c]
5029
+ });
5030
+ }
5031
+ fill = [];
5032
+ for (const { head, columns: cs } of loops) fill.push(`${head} {`, ...cs.flatMap((c) => ind(blocks.get(c))), "}");
5033
+ } else {
5034
+ const entry = (i) => `[${fns[i]}, ${String(3 * nestingOf(formulas[i].expr) + 16)}]`;
5035
+ const own = names.map((c) => {
5036
+ const at = formulas.flatMap((f, i) => f.column === c && f.key !== void 0 ? [`${String(f.key)}: ${entry(i)}`] : []);
5037
+ return at.length === 0 ? "undefined" : `{ ${at.join(", ")} }`;
5038
+ });
5039
+ const cols = names.map((c) => {
5040
+ const i = formulas.findIndex((f) => f.column === c && f.key === void 0);
5041
+ return i < 0 ? "undefined" : entry(i);
5042
+ });
5043
+ decls.push(`const SS = SH(n, [${cols.join(", ")}], [${own.join(", ")}], ${String(budget)});`, `const ${READ_CELL} = SS.r;`);
5044
+ fill = order.map((c) => `${headOf(c)} SS.f(${String(index.get(c))}, o);`);
5045
+ }
4952
5046
  if (run.tree) p.treeParts.add("children");
5047
+ const resultOf = (c, read) => {
5048
+ const l = lists.get(c);
5049
+ return l === void 0 ? read : `${p.use("LC")}(${read}, ${String(depthOf(l))})`;
5050
+ };
4953
5051
  return printModule(p, options, run, [
4954
- ...decls,
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;`],
5052
+ ...direct ? [`const D = [${names.map(() => "new Array(n)").join(", ")}];`] : decls,
4956
5053
  ...[...orders].map(([args, keys]) => `const ${keys} = ${args};`),
4957
5054
  ...fill,
4958
- `return { ${names.map((c, i) => `${JSON.stringify(c)}: ${direct ? `DO(D[${String(i)}])` : `SS.o(${String(i)})`}`).join(", ")} };`
5055
+ `return { ${names.map((c, i) => `${JSON.stringify(c)}: ${resultOf(c, direct ? `DO(D[${String(i)}])` : `SS.o(${String(i)})`)}`).join(", ")} };`
4959
5056
  ]);
4960
5057
  }
4961
5058
  //#endregion