@mark1russell7/vex-compiler 0.13.4 → 0.15.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.
@@ -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;;;;;;UAaF;WACN,MAAM,OAAO;WACb;;;;;;;UAQM;WACN,SAAS,SAAS,eAAe,OAAO;WACxC,OAAO,SAAS,eAAe;;;UAIzB;WACN,mBAAmB;WACnB,MAAM,SAAS,eAAe;WAC9B,aAAa,SAAS,eAAe;WACrC,SAAS;;;KCzDR;;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;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,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;;;;;;UAaF;WACN,MAAM,OAAO;WACb;;;;;;;UAQM;WACN,SAAS,SAAS,eAAe,OAAO;WACxC,OAAO,SAAS,eAAe;;;UAIzB;WACN,mBAAmB;WACnB,MAAM,SAAS,eAAe;WAC9B,aAAa,SAAS,eAAe;WACrC,SAAS;;;KCzDR;;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;W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AAgB;;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
@@ -12,7 +12,7 @@ import { CELL_KIND, MAX_NESTING, addrExprs, app, axisTargets, children, compile,
12
12
  const MAX_CALLS = 1e4;
13
13
  /**
14
14
  * The list ops that give a list, or read a position or a key of a list (LIST.SORT, LIST.TAKE, LIST.NTH, LIST.BUILD,
15
- * LIST.GET, LIST.INDEXOF, LIST.FILL).
15
+ * LIST.GET, LIST.INDEXOF, LIST.FILL, LIST.DISTINCT, LIST.GROUP, LIST.JOIN).
16
16
  */
17
17
  const ORDER_OPS = /* @__PURE__ */ new Set([
18
18
  "sortBy",
@@ -24,7 +24,10 @@ const ORDER_OPS = /* @__PURE__ */ new Set([
24
24
  "without",
25
25
  "get",
26
26
  "indexOf",
27
- "fill"
27
+ "fill",
28
+ "distinct",
29
+ "group",
30
+ "join"
28
31
  ]);
29
32
  /**
30
33
  * This function gives the reason that tier 2 cannot take a program with lists as values. The reason is the axis `list`,
@@ -1347,6 +1350,9 @@ var Lowerer = class {
1347
1350
  case "without":
1348
1351
  case "get":
1349
1352
  case "indexOf":
1353
+ case "distinct":
1354
+ case "group":
1355
+ case "join":
1350
1356
  /* v8 ignore next -- @preserve */
1351
1357
  throw new Unsupported(`the list op "${op}"`);
1352
1358
  }
@@ -2032,6 +2038,10 @@ function readsFinished(columns, order, self, tree, fused = /* @__PURE__ */ new S
2032
2038
  var NotStandalone = class extends Error {};
2033
2039
  /** The levels of a constant of the run. */
2034
2040
  const NO_DEPS = /* @__PURE__ */ new Set();
2041
+ /** The names of the items of a loop without `as` (COMPILE.MODULE.LIST.ITEM). */
2042
+ const NO_NAMES = /* @__PURE__ */ new Map();
2043
+ /** This function gives the names of a body or of a step: the names `as` of the items hide the names outside (AXIS.LIST.SCOPE). */
2044
+ const withNames = (env, items) => new Map([...env, ...items]);
2035
2045
  /**
2036
2046
  * The level of a read of a cell (COMPILE.MODULE.SHEET). The results of the cells change in a run. Thus a value that reads
2037
2047
  * a cell is not a constant or a column of the run. The level is far from the other levels.
@@ -2078,17 +2088,25 @@ const HELPERS = {
2078
2088
  "}"
2079
2089
  ].join("\n"),
2080
2090
  TK: "function TK(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(0, n) : F(); }",
2081
- NT: [
2082
- "function NT(l, i, s) {",
2091
+ VW: [
2092
+ "function VW(l) {",
2083
2093
  " let o = l.vw;",
2084
2094
  " if (o === undefined) {",
2085
2095
  " o = l.filter((x) => x[1] !== E);",
2086
2096
  " l.vw = o;",
2087
2097
  " }",
2098
+ " return o;",
2099
+ "}"
2100
+ ].join("\n"),
2101
+ NT: [
2102
+ "function NT(l, i, s) {",
2103
+ " const o = VW(l);",
2088
2104
  " if (s && o.length < l.length) F();",
2089
2105
  " return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F();",
2090
2106
  "}"
2091
2107
  ].join("\n"),
2108
+ CT: "function CT(l, s) { const o = VW(l); return s && o.length < l.length ? F() : o.length; }",
2109
+ FS: "function FS(l, s) { const o = VW(l); return (s && o.length < l.length) || o.length === 0 ? F() : o[0][1]; }",
2092
2110
  FI: [
2093
2111
  "function FI(n, v) {",
2094
2112
  " if (!(Number.isSafeInteger(n) && n >= 0 && n <= 100000 && v !== undefined)) F();",
@@ -2120,6 +2138,7 @@ const HELPERS = {
2120
2138
  ].join("\n"),
2121
2139
  DR: "function DR(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(n) : F(); }",
2122
2140
  AP: "function AP(l, o, s) { return V(l, s).concat(V(o, s)); }",
2141
+ AO: "function AO(l, o, s, w) { if (l !== w) return AP(l, o, s); for (const x of V(o, s)) l.push(x); return l; }",
2123
2142
  WO: [
2124
2143
  "function WO(l, o, s) {",
2125
2144
  " const a = V(l, s), g = new Map();",
@@ -2515,11 +2534,15 @@ const NEEDS = {
2515
2534
  SB: ["V", "F"],
2516
2535
  SK: ["V", "F"],
2517
2536
  TK: ["V", "F"],
2518
- NT: ["V", "F"],
2537
+ VW: [],
2538
+ NT: ["VW", "F"],
2539
+ CT: ["VW", "F"],
2540
+ FS: ["VW", "F"],
2519
2541
  FI: ["F"],
2520
2542
  LI: [],
2521
2543
  DR: ["V", "F"],
2522
2544
  AP: ["V"],
2545
+ AO: ["AP", "V"],
2523
2546
  WO: ["V"],
2524
2547
  FL: ["V"],
2525
2548
  GT: ["F"],
@@ -2726,6 +2749,23 @@ const NUMBER_STEPS = /* @__PURE__ */ new Set([
2726
2749
  /** This function gives the kind of the result of a list op: first gives the kind of its items. */
2727
2750
  const listKind = (op, item) => NUMBER_LIST_OPS.has(op) ? "number" : BOOLEAN_LIST_OPS.has(op) ? "boolean" : item;
2728
2751
  const LOOPS = /* @__PURE__ */ new WeakMap();
2752
+ /**
2753
+ * This function tells if the step of the name `name` of a loop appends to the list of the name (COMPILE.MODULE.APPEND.OWN).
2754
+ * It puts the append nodes of the form into `out`. The form is a read of the name,
2755
+ * `append(form, X)`, `if(c, form, form)` or a let with the form as its body. No other part of the form reads the name:
2756
+ * not `X`, not `c` and not a binding of a let.
2757
+ */
2758
+ function ownForm(e, name, out) {
2759
+ const free = (x) => !namesOf(x).has(name);
2760
+ if (e.tag === "var") return e.name === name;
2761
+ if (e.tag === "let") return Object.values(e.bind).every(free) && ownForm(e.body, name, out);
2762
+ if (e.tag !== "app" || e.args.length === 0) return false;
2763
+ const [head, ...rest] = e.args;
2764
+ if (e.op === "if") return free(head) && rest.every((x) => ownForm(x, name, out));
2765
+ if (e.op !== "append" || !rest.every(free) || !ownForm(head, name, out)) return false;
2766
+ out.push(e);
2767
+ return true;
2768
+ }
2729
2769
  /** This function gives the names that an expression reads, also the names that it binds itself. */
2730
2770
  function namesOf(e, out = /* @__PURE__ */ new Set()) {
2731
2771
  if (e.tag === "var") out.add(e.name);
@@ -3007,6 +3047,8 @@ var Printer = class {
3007
3047
  #loops = 1e6;
3008
3048
  /** The node of the memo whose function the printer prints, so it prints the node itself. */
3009
3049
  #inside;
3050
+ /** The owner of each append node of the step of a loop that the printer prints (COMPILE.MODULE.APPEND.OWN). */
3051
+ #own = /* @__PURE__ */ new Map();
3010
3052
  #structures = /* @__PURE__ */ new Map();
3011
3053
  #kinds = /* @__PURE__ */ new Map();
3012
3054
  #ops = /* @__PURE__ */ new Map();
@@ -3250,6 +3292,20 @@ var Printer = class {
3250
3292
  if (key !== void 0) return key.list !== void 0;
3251
3293
  return listy(e, this.#scopeOf(ctx));
3252
3294
  }
3295
+ /**
3296
+ * This method tells if the body of an each can give a list. The facts of the names `as` of its items come from its
3297
+ * list, which the printer prints only in the loop. So here each such name can hold a list. Then a list op goes over the
3298
+ * array of the each, and that array gives the value of the interpreter too (COMPILE.MODULE.LIST.ITEM).
3299
+ */
3300
+ #bodyList(e, ctx) {
3301
+ if (e.as === void 0) return this.#isList(e.body, ctx);
3302
+ const scope = this.#scopeOf(ctx);
3303
+ const names = e.as;
3304
+ return listy(e.body, {
3305
+ ...scope,
3306
+ name: (name) => names.includes(name) || scope.name(name)
3307
+ });
3308
+ }
3253
3309
  /** This method gives the scope of `listy` at a node: the names and the bound functions of its context. */
3254
3310
  #scopeOf(ctx) {
3255
3311
  return {
@@ -3811,10 +3867,10 @@ var Printer = class {
3811
3867
  if (op === "ifError") return this.#ifError(args, ctx);
3812
3868
  const [head] = args;
3813
3869
  const rest = args.slice(1);
3814
- if (FOLD_OPS.has(op) && head?.tag === "each" && !this.#isList(head.body, ctx)) return this.#list(op, head, rest, ctx);
3870
+ if (FOLD_OPS.has(op) && head?.tag === "each" && !this.#bodyList(head, ctx)) return this.#list(op, head, rest, ctx);
3815
3871
  if (FOLD_OPS.has(op) && head?.tag === "let" && rest.every((x) => x.tag === "lit")) return this.#let(head, ctx, (body) => app(op, body, ...rest));
3816
3872
  if (head !== void 0 && (op === "sortBy" || op === "take" || op === "drop" && this.#isList(head, ctx))) return this.#order(op, head, rest, ctx);
3817
- if (head !== void 0 && BUILD_OPS.has(op) && this.#isList(head, ctx)) return this.#build(op, head, rest, ctx);
3873
+ if (head !== void 0 && BUILD_OPS.has(op) && this.#isList(head, ctx)) return this.#build(op, head, rest, ctx, this.#own.get(e));
3818
3874
  if (head !== void 0 && (FOLD_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, rest, ctx);
3819
3875
  for (const x of args) this.#wantList.add(x);
3820
3876
  const raw = args.map((x) => this.expr(x, ctx));
@@ -4079,9 +4135,10 @@ var Printer = class {
4079
4135
  /**
4080
4136
  * This method gives the loop over the targets of an axis: the statements before it, its bounds and the key that it
4081
4137
  * skips. `start` has the levels of the address, and `inner` is the position at a target. The axes all, others,
4082
- * window and list have a loop. In a tree space, the axes of a tree have a loop too.
4138
+ * window and list have a loop. In a tree space, the axes of a tree have a loop too. With `as`, a loop over the axis
4139
+ * list binds the value and the index of each item after its check (COMPILE.MODULE.LIST.ITEM).
4083
4140
  */
4084
- #targetLoop(axis, at, ctx) {
4141
+ #targetLoop(axis, at, ctx, as) {
4085
4142
  const { base: outer, tests } = testsOf(axis);
4086
4143
  const reverse = outer.t === "reverse";
4087
4144
  const base = outer.t === "reverse" ? outer.axis : outer;
@@ -4100,8 +4157,14 @@ var Printer = class {
4100
4157
  const head = reverse ? `for (let ${q} = ${a}.length - 1; ${q} >= 0; ${q}--) {` : `for (let ${q} = 0; ${q} < ${a}.length; ${q}++) {`;
4101
4158
  const key = [`const ${j} = ${a}[${q}][0];`];
4102
4159
  const check = [`if (${a}[${q}][1] === E) ${this.use("F")}();`];
4160
+ const s = [...list.s, `const ${a} = ${list.v.code};`];
4161
+ const named = as === void 0 ? void 0 : this.#itemNames(as, list.v.list, `${a}[${q}][1]`, a, reverse);
4162
+ if (named !== void 0) {
4163
+ s.push(...named.s);
4164
+ check.push(...named.check);
4165
+ }
4103
4166
  return {
4104
- s: [...list.s, `const ${a} = ${list.v.code};`],
4167
+ s,
4105
4168
  j,
4106
4169
  head,
4107
4170
  key,
@@ -4112,7 +4175,9 @@ var Printer = class {
4112
4175
  startSafe: list.v.safe,
4113
4176
  tests,
4114
4177
  inner,
4115
- texts
4178
+ texts,
4179
+ items: named?.items ?? NO_NAMES,
4180
+ item: named?.level
4116
4181
  };
4117
4182
  }
4118
4183
  const s = [];
@@ -4144,7 +4209,9 @@ var Printer = class {
4144
4209
  startSafe,
4145
4210
  tests,
4146
4211
  inner,
4147
- texts: false
4212
+ texts: false,
4213
+ items: NO_NAMES,
4214
+ item: void 0
4148
4215
  };
4149
4216
  }
4150
4217
  if (this.#graph && GRAPH_AXES.has(base.t)) {
@@ -4162,7 +4229,9 @@ var Printer = class {
4162
4229
  startSafe,
4163
4230
  tests,
4164
4231
  inner,
4165
- texts: false
4232
+ texts: false,
4233
+ items: NO_NAMES,
4234
+ item: void 0
4166
4235
  };
4167
4236
  }
4168
4237
  if (this.#graph && base.t === "window") throw new NotStandalone("the axis \"window\" in a graph space");
@@ -4185,7 +4254,49 @@ var Printer = class {
4185
4254
  startSafe,
4186
4255
  tests,
4187
4256
  inner,
4188
- texts: false
4257
+ texts: false,
4258
+ items: NO_NAMES,
4259
+ item: void 0
4260
+ };
4261
+ }
4262
+ /**
4263
+ * This method binds the names `as` of a loop over the axis list (COMPILE.MODULE.LIST.ITEM). The statements come after
4264
+ * the check of the item, so an error item fails before they bind a name (AXIS.LIST.ITEM). The first name holds the
4265
+ * value of the item, with the facts of its list. The index counts the items with a value in the order of the list, as
4266
+ * `nth` does (AXIS.LIST.INDEX). In the reverse order, it counts down from the number of these items. The names have a
4267
+ * level of their own. Thus a part that reads them stays in the loop, and a key of a literal can still be a target.
4268
+ */
4269
+ #itemNames(as, info, value, a, reverse) {
4270
+ const level = this.#loops++;
4271
+ const deps = /* @__PURE__ */ new Set([level]);
4272
+ const [item, index] = as;
4273
+ const y = this.fresh("y");
4274
+ const items = /* @__PURE__ */ new Map([[item, {
4275
+ code: y,
4276
+ kind: info.item,
4277
+ lazy: false,
4278
+ deps,
4279
+ list: info.inner
4280
+ }]]);
4281
+ const check = [`const ${y} = ${value};`];
4282
+ const s = [];
4283
+ if (index !== void 0) {
4284
+ const i = this.fresh("i");
4285
+ const z = this.fresh("z");
4286
+ s.push(`let ${z} = ${reverse ? `${this.use("CT")}(${a}, false)` : "0"};`);
4287
+ check.push(`const ${i} = ${reverse ? `--${z}` : `${z}++`};`);
4288
+ items.set(index, {
4289
+ code: i,
4290
+ kind: "number",
4291
+ lazy: false,
4292
+ deps
4293
+ });
4294
+ }
4295
+ return {
4296
+ s,
4297
+ check,
4298
+ items,
4299
+ level
4189
4300
  };
4190
4301
  }
4191
4302
  /**
@@ -4266,13 +4377,16 @@ var Printer = class {
4266
4377
  #list(op, target, rest, ctx) {
4267
4378
  const { strict, step } = this.#foldOptions(op, rest);
4268
4379
  const at = target.at ?? [];
4269
- const { s, j, head, key, check, skip, fixed, start, startSafe, tests, inner, texts } = this.#targetLoop(target.axis, at, ctx);
4380
+ const { s, j, head, key, check, skip, fixed, start, startSafe, tests, inner, texts, items, item: own } = this.#targetLoop(target.axis, at, ctx, target.as);
4270
4381
  const { level } = inner;
4271
4382
  const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
4272
- const body = this.#hoist(this.expr(target.body, inner), hasLoop(target.body));
4383
+ const body = this.#hoist(this.expr(target.body, {
4384
+ ...inner,
4385
+ env: withNames(inner.env, items)
4386
+ }), hasLoop(target.body));
4273
4387
  this.#readsNoTarget(texts, level, [body, ...testCodes].map((x) => x.v.deps));
4274
4388
  if (op === "count" && fixed && tests.length === 0 && body.v.safe && !body.v.deps.has(CELLS)) return bare(plainValue("n", "number"));
4275
- const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== level));
4389
+ const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== level && d !== own));
4276
4390
  const r = this.#reduction(op, body.v.kind, step);
4277
4391
  const { v } = r;
4278
4392
  s.push(r.init);
@@ -4446,12 +4560,15 @@ var Printer = class {
4446
4560
  */
4447
4561
  #eachList(e, ctx) {
4448
4562
  const at = e.at ?? [];
4449
- const { s, j, head, key, check, skip, start, startSafe, tests, inner, texts } = this.#targetLoop(e.axis, at, ctx);
4563
+ const { s, j, head, key, check, skip, start, startSafe, tests, inner, texts, items, item: own } = this.#targetLoop(e.axis, at, ctx, e.as);
4450
4564
  const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
4451
4565
  if (this.#wantList.has(e)) this.#wantList.add(e.body);
4452
- const body = this.#hoist(this.expr(e.body, inner), hasLoop(e.body));
4566
+ const body = this.#hoist(this.expr(e.body, {
4567
+ ...inner,
4568
+ env: withNames(inner.env, items)
4569
+ }), hasLoop(e.body));
4453
4570
  this.#readsNoTarget(texts, inner.level, [body, ...testCodes].map((x) => x.v.deps));
4454
- const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== inner.level));
4571
+ const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== inner.level && d !== own));
4455
4572
  const list = this.fresh("L");
4456
4573
  const v = this.fresh("x");
4457
4574
  const item = [
@@ -4546,9 +4663,10 @@ var Printer = class {
4546
4663
  }
4547
4664
  /**
4548
4665
  * This method prints append, without and flat (LIST.BUILD), get (LIST.GET) and indexOf (LIST.INDEXOF) over the array
4549
- * of a list. Each list that the op reads follows LIST.LENIENT, as in the interpreter.
4666
+ * of a list. Each list that the op reads follows LIST.LENIENT, as in the interpreter. An append with an owner appends
4667
+ * into the array of its name, if the name in the context is the name of the loop (COMPILE.MODULE.APPEND.OWN).
4550
4668
  */
4551
- #build(op, head, rest, ctx) {
4669
+ #build(op, head, rest, ctx, owner) {
4552
4670
  const list = this.#listOf(head, ctx);
4553
4671
  const info = list.v.list;
4554
4672
  const two = op === "append" || op === "without";
@@ -4558,10 +4676,13 @@ var Printer = class {
4558
4676
  const deps = depsOf(...raw);
4559
4677
  const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
4560
4678
  const helper = BUILD_HELPERS[op];
4679
+ const args = [...parts.map((x) => x.v.code), String(strict)];
4680
+ const w = owner !== void 0 && ctx.env.get(owner.name) === owner.read ? owner.w ??= this.fresh("w") : void 0;
4681
+ const code = w === void 0 ? `${this.use(helper)}(${args.join(", ")})` : `(${w} = ${this.use("AO")}(${[...args, w].join(", ")}))`;
4561
4682
  const value = (kind, facts) => ({
4562
4683
  s: stmtsOf(...parts),
4563
4684
  v: {
4564
- code: `${this.use(helper)}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
4685
+ code,
4565
4686
  kind,
4566
4687
  safe: false,
4567
4688
  deps,
@@ -4596,6 +4717,26 @@ var Printer = class {
4596
4717
  list: inner
4597
4718
  }
4598
4719
  };
4720
+ if (op === "count") return {
4721
+ s: l.s,
4722
+ v: {
4723
+ code: `${this.use("CT")}(${l.v.code}, ${String(strict)})`,
4724
+ kind: "number",
4725
+ safe: false,
4726
+ deps,
4727
+ list: void 0
4728
+ }
4729
+ };
4730
+ if (op === "first") return {
4731
+ s: l.s,
4732
+ v: {
4733
+ code: `${this.use("FS")}(${l.v.code}, ${String(strict)})`,
4734
+ kind: item,
4735
+ safe: false,
4736
+ deps,
4737
+ list: inner
4738
+ }
4739
+ };
4599
4740
  const r = this.#reduction(op, item, step);
4600
4741
  const { v } = r;
4601
4742
  return {
@@ -4657,8 +4798,42 @@ var Printer = class {
4657
4798
  deps
4658
4799
  };
4659
4800
  }
4660
- /** This method gives the statements of one step of a loop: each new value first, and then each name gets its value. */
4661
- #loopStep(step, inner, vars) {
4801
+ /**
4802
+ * This method gives the statements of one step of a loop: each new value first, and then each name gets its value.
4803
+ * `init` has the statements before the loop: the variable of each name that appends to its own list
4804
+ * (COMPILE.MODULE.APPEND.OWN). Each such name has the form of `ownForm`, and no step of another name and no `until`
4805
+ * reads it. The tests of a fold read only the names outside the fold, so they cannot read it.
4806
+ */
4807
+ #loopStep(step, inner, vars, until) {
4808
+ const owners = [];
4809
+ const nodes = [];
4810
+ const steps = Object.entries(step);
4811
+ for (const [name, x] of steps) {
4812
+ const own = [];
4813
+ const others = [...steps.filter(([k]) => k !== name).map(([, y]) => y), ...until === void 0 ? [] : [until]];
4814
+ if (!ownForm(x, name, own) || others.some((y) => namesOf(y).has(name))) continue;
4815
+ const owner = {
4816
+ name,
4817
+ read: vars.get(name),
4818
+ w: void 0
4819
+ };
4820
+ for (const node of own) this.#own.set(node, owner);
4821
+ owners.push(owner);
4822
+ nodes.push(...own);
4823
+ }
4824
+ try {
4825
+ const { s, deps } = this.#steps(step, inner, vars);
4826
+ return {
4827
+ s,
4828
+ deps,
4829
+ init: owners.flatMap((o) => o.w === void 0 ? [] : [`let ${o.w} = null;`])
4830
+ };
4831
+ } finally {
4832
+ for (const node of nodes) this.#own.delete(node);
4833
+ }
4834
+ }
4835
+ /** This method gives the statements of one step of a loop, without the variables of COMPILE.MODULE.APPEND.OWN. */
4836
+ #steps(step, inner, vars) {
4662
4837
  const s = [];
4663
4838
  const sets = [];
4664
4839
  const deps = /* @__PURE__ */ new Set();
@@ -4691,7 +4866,7 @@ var Printer = class {
4691
4866
  };
4692
4867
  if (this.#wantList.has(e)) this.#wantList.add(e.result);
4693
4868
  const until = e.until === void 0 ? void 0 : this.expr(e.until, inner);
4694
- const step = this.#loopStep(e.step, inner, names.vars);
4869
+ const step = this.#loopStep(e.step, inner, names.vars, e.until);
4695
4870
  const result = this.expr(e.result, inner);
4696
4871
  const k = this.fresh("k");
4697
4872
  const body = [
@@ -4708,6 +4883,7 @@ var Printer = class {
4708
4883
  return {
4709
4884
  s: [
4710
4885
  ...names.s,
4886
+ ...step.init,
4711
4887
  `for (let ${k} = 0; ; ${k}++) {`,
4712
4888
  ...ind(body),
4713
4889
  "}",
@@ -4730,12 +4906,12 @@ var Printer = class {
4730
4906
  const loop = this.#loops++;
4731
4907
  const names = this.#loopNames(e.init, e.step, ctx, loop);
4732
4908
  if (this.#wantList.has(e)) this.#wantList.add(e.result);
4733
- const t = this.#targetLoop(e.axis, e.at ?? [], ctx);
4909
+ const t = this.#targetLoop(e.axis, e.at ?? [], ctx, e.as);
4734
4910
  const tests = t.tests.map((x) => this.#hoist(this.expr(x, t.inner), hasLoop(x)));
4735
4911
  if (tests.some((x) => x.v.deps.has(CELLS))) throw new NotStandalone("a test of the axis of a fold reads a cell");
4736
4912
  const step = this.#loopStep(e.step, {
4737
4913
  ...t.inner,
4738
- env: names.env
4914
+ env: withNames(names.env, t.items)
4739
4915
  }, names.vars);
4740
4916
  this.#readsNoTarget(t.texts, t.inner.level, [...tests.map((x) => x.v.deps), step.deps]);
4741
4917
  const result = this.expr(e.result, {
@@ -4749,7 +4925,7 @@ var Printer = class {
4749
4925
  ...tests.flatMap((c) => [...c.s, `if (!${this.#boolean(c.v)}) continue;`]),
4750
4926
  ...step.s
4751
4927
  ];
4752
- const inside = [...tests.flatMap((c) => [...c.v.deps]), ...step.deps].filter((d) => d !== loop && d !== t.inner.level);
4928
+ const inside = [...tests.flatMap((c) => [...c.v.deps]), ...step.deps].filter((d) => d !== loop && d !== t.inner.level && d !== t.item);
4753
4929
  const deps = /* @__PURE__ */ new Set([
4754
4930
  ...names.deps,
4755
4931
  ...t.start,
@@ -4759,6 +4935,7 @@ var Printer = class {
4759
4935
  return {
4760
4936
  s: [
4761
4937
  ...names.s,
4938
+ ...step.init,
4762
4939
  ...t.s,
4763
4940
  t.head,
4764
4941
  ...ind(body),