@mark1russell7/vex-compiler 0.13.4 → 0.14.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
@@ -2032,6 +2032,10 @@ function readsFinished(columns, order, self, tree, fused = /* @__PURE__ */ new S
2032
2032
  var NotStandalone = class extends Error {};
2033
2033
  /** The levels of a constant of the run. */
2034
2034
  const NO_DEPS = /* @__PURE__ */ new Set();
2035
+ /** The names of the items of a loop without `as` (COMPILE.MODULE.LIST.ITEM). */
2036
+ const NO_NAMES = /* @__PURE__ */ new Map();
2037
+ /** 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). */
2038
+ const withNames = (env, items) => new Map([...env, ...items]);
2035
2039
  /**
2036
2040
  * 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
2041
  * a cell is not a constant or a column of the run. The level is far from the other levels.
@@ -2078,17 +2082,25 @@ const HELPERS = {
2078
2082
  "}"
2079
2083
  ].join("\n"),
2080
2084
  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) {",
2085
+ VW: [
2086
+ "function VW(l) {",
2083
2087
  " let o = l.vw;",
2084
2088
  " if (o === undefined) {",
2085
2089
  " o = l.filter((x) => x[1] !== E);",
2086
2090
  " l.vw = o;",
2087
2091
  " }",
2092
+ " return o;",
2093
+ "}"
2094
+ ].join("\n"),
2095
+ NT: [
2096
+ "function NT(l, i, s) {",
2097
+ " const o = VW(l);",
2088
2098
  " if (s && o.length < l.length) F();",
2089
2099
  " return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F();",
2090
2100
  "}"
2091
2101
  ].join("\n"),
2102
+ CT: "function CT(l, s) { const o = VW(l); return s && o.length < l.length ? F() : o.length; }",
2103
+ FS: "function FS(l, s) { const o = VW(l); return (s && o.length < l.length) || o.length === 0 ? F() : o[0][1]; }",
2092
2104
  FI: [
2093
2105
  "function FI(n, v) {",
2094
2106
  " if (!(Number.isSafeInteger(n) && n >= 0 && n <= 100000 && v !== undefined)) F();",
@@ -2120,6 +2132,7 @@ const HELPERS = {
2120
2132
  ].join("\n"),
2121
2133
  DR: "function DR(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(n) : F(); }",
2122
2134
  AP: "function AP(l, o, s) { return V(l, s).concat(V(o, s)); }",
2135
+ 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
2136
  WO: [
2124
2137
  "function WO(l, o, s) {",
2125
2138
  " const a = V(l, s), g = new Map();",
@@ -2515,11 +2528,15 @@ const NEEDS = {
2515
2528
  SB: ["V", "F"],
2516
2529
  SK: ["V", "F"],
2517
2530
  TK: ["V", "F"],
2518
- NT: ["V", "F"],
2531
+ VW: [],
2532
+ NT: ["VW", "F"],
2533
+ CT: ["VW", "F"],
2534
+ FS: ["VW", "F"],
2519
2535
  FI: ["F"],
2520
2536
  LI: [],
2521
2537
  DR: ["V", "F"],
2522
2538
  AP: ["V"],
2539
+ AO: ["AP", "V"],
2523
2540
  WO: ["V"],
2524
2541
  FL: ["V"],
2525
2542
  GT: ["F"],
@@ -2726,6 +2743,23 @@ const NUMBER_STEPS = /* @__PURE__ */ new Set([
2726
2743
  /** This function gives the kind of the result of a list op: first gives the kind of its items. */
2727
2744
  const listKind = (op, item) => NUMBER_LIST_OPS.has(op) ? "number" : BOOLEAN_LIST_OPS.has(op) ? "boolean" : item;
2728
2745
  const LOOPS = /* @__PURE__ */ new WeakMap();
2746
+ /**
2747
+ * This function tells if the step of the name `name` of a loop appends to the list of the name (COMPILE.MODULE.APPEND.OWN).
2748
+ * It puts the append nodes of the form into `out`. The form is a read of the name,
2749
+ * `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:
2750
+ * not `X`, not `c` and not a binding of a let.
2751
+ */
2752
+ function ownForm(e, name, out) {
2753
+ const free = (x) => !namesOf(x).has(name);
2754
+ if (e.tag === "var") return e.name === name;
2755
+ if (e.tag === "let") return Object.values(e.bind).every(free) && ownForm(e.body, name, out);
2756
+ if (e.tag !== "app" || e.args.length === 0) return false;
2757
+ const [head, ...rest] = e.args;
2758
+ if (e.op === "if") return free(head) && rest.every((x) => ownForm(x, name, out));
2759
+ if (e.op !== "append" || !rest.every(free) || !ownForm(head, name, out)) return false;
2760
+ out.push(e);
2761
+ return true;
2762
+ }
2729
2763
  /** This function gives the names that an expression reads, also the names that it binds itself. */
2730
2764
  function namesOf(e, out = /* @__PURE__ */ new Set()) {
2731
2765
  if (e.tag === "var") out.add(e.name);
@@ -3007,6 +3041,8 @@ var Printer = class {
3007
3041
  #loops = 1e6;
3008
3042
  /** The node of the memo whose function the printer prints, so it prints the node itself. */
3009
3043
  #inside;
3044
+ /** The owner of each append node of the step of a loop that the printer prints (COMPILE.MODULE.APPEND.OWN). */
3045
+ #own = /* @__PURE__ */ new Map();
3010
3046
  #structures = /* @__PURE__ */ new Map();
3011
3047
  #kinds = /* @__PURE__ */ new Map();
3012
3048
  #ops = /* @__PURE__ */ new Map();
@@ -3250,6 +3286,20 @@ var Printer = class {
3250
3286
  if (key !== void 0) return key.list !== void 0;
3251
3287
  return listy(e, this.#scopeOf(ctx));
3252
3288
  }
3289
+ /**
3290
+ * 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
3291
+ * 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
3292
+ * array of the each, and that array gives the value of the interpreter too (COMPILE.MODULE.LIST.ITEM).
3293
+ */
3294
+ #bodyList(e, ctx) {
3295
+ if (e.as === void 0) return this.#isList(e.body, ctx);
3296
+ const scope = this.#scopeOf(ctx);
3297
+ const names = e.as;
3298
+ return listy(e.body, {
3299
+ ...scope,
3300
+ name: (name) => names.includes(name) || scope.name(name)
3301
+ });
3302
+ }
3253
3303
  /** This method gives the scope of `listy` at a node: the names and the bound functions of its context. */
3254
3304
  #scopeOf(ctx) {
3255
3305
  return {
@@ -3811,10 +3861,10 @@ var Printer = class {
3811
3861
  if (op === "ifError") return this.#ifError(args, ctx);
3812
3862
  const [head] = args;
3813
3863
  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);
3864
+ if (FOLD_OPS.has(op) && head?.tag === "each" && !this.#bodyList(head, ctx)) return this.#list(op, head, rest, ctx);
3815
3865
  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
3866
  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);
3867
+ if (head !== void 0 && BUILD_OPS.has(op) && this.#isList(head, ctx)) return this.#build(op, head, rest, ctx, this.#own.get(e));
3818
3868
  if (head !== void 0 && (FOLD_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, rest, ctx);
3819
3869
  for (const x of args) this.#wantList.add(x);
3820
3870
  const raw = args.map((x) => this.expr(x, ctx));
@@ -4079,9 +4129,10 @@ var Printer = class {
4079
4129
  /**
4080
4130
  * This method gives the loop over the targets of an axis: the statements before it, its bounds and the key that it
4081
4131
  * 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.
4132
+ * 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
4133
+ * list binds the value and the index of each item after its check (COMPILE.MODULE.LIST.ITEM).
4083
4134
  */
4084
- #targetLoop(axis, at, ctx) {
4135
+ #targetLoop(axis, at, ctx, as) {
4085
4136
  const { base: outer, tests } = testsOf(axis);
4086
4137
  const reverse = outer.t === "reverse";
4087
4138
  const base = outer.t === "reverse" ? outer.axis : outer;
@@ -4100,8 +4151,14 @@ var Printer = class {
4100
4151
  const head = reverse ? `for (let ${q} = ${a}.length - 1; ${q} >= 0; ${q}--) {` : `for (let ${q} = 0; ${q} < ${a}.length; ${q}++) {`;
4101
4152
  const key = [`const ${j} = ${a}[${q}][0];`];
4102
4153
  const check = [`if (${a}[${q}][1] === E) ${this.use("F")}();`];
4154
+ const s = [...list.s, `const ${a} = ${list.v.code};`];
4155
+ const named = as === void 0 ? void 0 : this.#itemNames(as, list.v.list, `${a}[${q}][1]`, a, reverse);
4156
+ if (named !== void 0) {
4157
+ s.push(...named.s);
4158
+ check.push(...named.check);
4159
+ }
4103
4160
  return {
4104
- s: [...list.s, `const ${a} = ${list.v.code};`],
4161
+ s,
4105
4162
  j,
4106
4163
  head,
4107
4164
  key,
@@ -4112,7 +4169,9 @@ var Printer = class {
4112
4169
  startSafe: list.v.safe,
4113
4170
  tests,
4114
4171
  inner,
4115
- texts
4172
+ texts,
4173
+ items: named?.items ?? NO_NAMES,
4174
+ item: named?.level
4116
4175
  };
4117
4176
  }
4118
4177
  const s = [];
@@ -4144,7 +4203,9 @@ var Printer = class {
4144
4203
  startSafe,
4145
4204
  tests,
4146
4205
  inner,
4147
- texts: false
4206
+ texts: false,
4207
+ items: NO_NAMES,
4208
+ item: void 0
4148
4209
  };
4149
4210
  }
4150
4211
  if (this.#graph && GRAPH_AXES.has(base.t)) {
@@ -4162,7 +4223,9 @@ var Printer = class {
4162
4223
  startSafe,
4163
4224
  tests,
4164
4225
  inner,
4165
- texts: false
4226
+ texts: false,
4227
+ items: NO_NAMES,
4228
+ item: void 0
4166
4229
  };
4167
4230
  }
4168
4231
  if (this.#graph && base.t === "window") throw new NotStandalone("the axis \"window\" in a graph space");
@@ -4185,7 +4248,49 @@ var Printer = class {
4185
4248
  startSafe,
4186
4249
  tests,
4187
4250
  inner,
4188
- texts: false
4251
+ texts: false,
4252
+ items: NO_NAMES,
4253
+ item: void 0
4254
+ };
4255
+ }
4256
+ /**
4257
+ * This method binds the names `as` of a loop over the axis list (COMPILE.MODULE.LIST.ITEM). The statements come after
4258
+ * the check of the item, so an error item fails before they bind a name (AXIS.LIST.ITEM). The first name holds the
4259
+ * 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
4260
+ * `nth` does (AXIS.LIST.INDEX). In the reverse order, it counts down from the number of these items. The names have a
4261
+ * 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.
4262
+ */
4263
+ #itemNames(as, info, value, a, reverse) {
4264
+ const level = this.#loops++;
4265
+ const deps = /* @__PURE__ */ new Set([level]);
4266
+ const [item, index] = as;
4267
+ const y = this.fresh("y");
4268
+ const items = /* @__PURE__ */ new Map([[item, {
4269
+ code: y,
4270
+ kind: info.item,
4271
+ lazy: false,
4272
+ deps,
4273
+ list: info.inner
4274
+ }]]);
4275
+ const check = [`const ${y} = ${value};`];
4276
+ const s = [];
4277
+ if (index !== void 0) {
4278
+ const i = this.fresh("i");
4279
+ const z = this.fresh("z");
4280
+ s.push(`let ${z} = ${reverse ? `${this.use("CT")}(${a}, false)` : "0"};`);
4281
+ check.push(`const ${i} = ${reverse ? `--${z}` : `${z}++`};`);
4282
+ items.set(index, {
4283
+ code: i,
4284
+ kind: "number",
4285
+ lazy: false,
4286
+ deps
4287
+ });
4288
+ }
4289
+ return {
4290
+ s,
4291
+ check,
4292
+ items,
4293
+ level
4189
4294
  };
4190
4295
  }
4191
4296
  /**
@@ -4266,13 +4371,16 @@ var Printer = class {
4266
4371
  #list(op, target, rest, ctx) {
4267
4372
  const { strict, step } = this.#foldOptions(op, rest);
4268
4373
  const at = target.at ?? [];
4269
- const { s, j, head, key, check, skip, fixed, start, startSafe, tests, inner, texts } = this.#targetLoop(target.axis, at, ctx);
4374
+ 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
4375
  const { level } = inner;
4271
4376
  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));
4377
+ const body = this.#hoist(this.expr(target.body, {
4378
+ ...inner,
4379
+ env: withNames(inner.env, items)
4380
+ }), hasLoop(target.body));
4273
4381
  this.#readsNoTarget(texts, level, [body, ...testCodes].map((x) => x.v.deps));
4274
4382
  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));
4383
+ const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== level && d !== own));
4276
4384
  const r = this.#reduction(op, body.v.kind, step);
4277
4385
  const { v } = r;
4278
4386
  s.push(r.init);
@@ -4446,12 +4554,15 @@ var Printer = class {
4446
4554
  */
4447
4555
  #eachList(e, ctx) {
4448
4556
  const at = e.at ?? [];
4449
- const { s, j, head, key, check, skip, start, startSafe, tests, inner, texts } = this.#targetLoop(e.axis, at, ctx);
4557
+ const { s, j, head, key, check, skip, start, startSafe, tests, inner, texts, items, item: own } = this.#targetLoop(e.axis, at, ctx, e.as);
4450
4558
  const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
4451
4559
  if (this.#wantList.has(e)) this.#wantList.add(e.body);
4452
- const body = this.#hoist(this.expr(e.body, inner), hasLoop(e.body));
4560
+ const body = this.#hoist(this.expr(e.body, {
4561
+ ...inner,
4562
+ env: withNames(inner.env, items)
4563
+ }), hasLoop(e.body));
4453
4564
  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));
4565
+ const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== inner.level && d !== own));
4455
4566
  const list = this.fresh("L");
4456
4567
  const v = this.fresh("x");
4457
4568
  const item = [
@@ -4546,9 +4657,10 @@ var Printer = class {
4546
4657
  }
4547
4658
  /**
4548
4659
  * 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.
4660
+ * of a list. Each list that the op reads follows LIST.LENIENT, as in the interpreter. An append with an owner appends
4661
+ * into the array of its name, if the name in the context is the name of the loop (COMPILE.MODULE.APPEND.OWN).
4550
4662
  */
4551
- #build(op, head, rest, ctx) {
4663
+ #build(op, head, rest, ctx, owner) {
4552
4664
  const list = this.#listOf(head, ctx);
4553
4665
  const info = list.v.list;
4554
4666
  const two = op === "append" || op === "without";
@@ -4558,10 +4670,13 @@ var Printer = class {
4558
4670
  const deps = depsOf(...raw);
4559
4671
  const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
4560
4672
  const helper = BUILD_HELPERS[op];
4673
+ const args = [...parts.map((x) => x.v.code), String(strict)];
4674
+ const w = owner !== void 0 && ctx.env.get(owner.name) === owner.read ? owner.w ??= this.fresh("w") : void 0;
4675
+ const code = w === void 0 ? `${this.use(helper)}(${args.join(", ")})` : `(${w} = ${this.use("AO")}(${[...args, w].join(", ")}))`;
4561
4676
  const value = (kind, facts) => ({
4562
4677
  s: stmtsOf(...parts),
4563
4678
  v: {
4564
- code: `${this.use(helper)}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
4679
+ code,
4565
4680
  kind,
4566
4681
  safe: false,
4567
4682
  deps,
@@ -4596,6 +4711,26 @@ var Printer = class {
4596
4711
  list: inner
4597
4712
  }
4598
4713
  };
4714
+ if (op === "count") return {
4715
+ s: l.s,
4716
+ v: {
4717
+ code: `${this.use("CT")}(${l.v.code}, ${String(strict)})`,
4718
+ kind: "number",
4719
+ safe: false,
4720
+ deps,
4721
+ list: void 0
4722
+ }
4723
+ };
4724
+ if (op === "first") return {
4725
+ s: l.s,
4726
+ v: {
4727
+ code: `${this.use("FS")}(${l.v.code}, ${String(strict)})`,
4728
+ kind: item,
4729
+ safe: false,
4730
+ deps,
4731
+ list: inner
4732
+ }
4733
+ };
4599
4734
  const r = this.#reduction(op, item, step);
4600
4735
  const { v } = r;
4601
4736
  return {
@@ -4657,8 +4792,42 @@ var Printer = class {
4657
4792
  deps
4658
4793
  };
4659
4794
  }
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) {
4795
+ /**
4796
+ * This method gives the statements of one step of a loop: each new value first, and then each name gets its value.
4797
+ * `init` has the statements before the loop: the variable of each name that appends to its own list
4798
+ * (COMPILE.MODULE.APPEND.OWN). Each such name has the form of `ownForm`, and no step of another name and no `until`
4799
+ * reads it. The tests of a fold read only the names outside the fold, so they cannot read it.
4800
+ */
4801
+ #loopStep(step, inner, vars, until) {
4802
+ const owners = [];
4803
+ const nodes = [];
4804
+ const steps = Object.entries(step);
4805
+ for (const [name, x] of steps) {
4806
+ const own = [];
4807
+ const others = [...steps.filter(([k]) => k !== name).map(([, y]) => y), ...until === void 0 ? [] : [until]];
4808
+ if (!ownForm(x, name, own) || others.some((y) => namesOf(y).has(name))) continue;
4809
+ const owner = {
4810
+ name,
4811
+ read: vars.get(name),
4812
+ w: void 0
4813
+ };
4814
+ for (const node of own) this.#own.set(node, owner);
4815
+ owners.push(owner);
4816
+ nodes.push(...own);
4817
+ }
4818
+ try {
4819
+ const { s, deps } = this.#steps(step, inner, vars);
4820
+ return {
4821
+ s,
4822
+ deps,
4823
+ init: owners.flatMap((o) => o.w === void 0 ? [] : [`let ${o.w} = null;`])
4824
+ };
4825
+ } finally {
4826
+ for (const node of nodes) this.#own.delete(node);
4827
+ }
4828
+ }
4829
+ /** This method gives the statements of one step of a loop, without the variables of COMPILE.MODULE.APPEND.OWN. */
4830
+ #steps(step, inner, vars) {
4662
4831
  const s = [];
4663
4832
  const sets = [];
4664
4833
  const deps = /* @__PURE__ */ new Set();
@@ -4691,7 +4860,7 @@ var Printer = class {
4691
4860
  };
4692
4861
  if (this.#wantList.has(e)) this.#wantList.add(e.result);
4693
4862
  const until = e.until === void 0 ? void 0 : this.expr(e.until, inner);
4694
- const step = this.#loopStep(e.step, inner, names.vars);
4863
+ const step = this.#loopStep(e.step, inner, names.vars, e.until);
4695
4864
  const result = this.expr(e.result, inner);
4696
4865
  const k = this.fresh("k");
4697
4866
  const body = [
@@ -4708,6 +4877,7 @@ var Printer = class {
4708
4877
  return {
4709
4878
  s: [
4710
4879
  ...names.s,
4880
+ ...step.init,
4711
4881
  `for (let ${k} = 0; ; ${k}++) {`,
4712
4882
  ...ind(body),
4713
4883
  "}",
@@ -4730,12 +4900,12 @@ var Printer = class {
4730
4900
  const loop = this.#loops++;
4731
4901
  const names = this.#loopNames(e.init, e.step, ctx, loop);
4732
4902
  if (this.#wantList.has(e)) this.#wantList.add(e.result);
4733
- const t = this.#targetLoop(e.axis, e.at ?? [], ctx);
4903
+ const t = this.#targetLoop(e.axis, e.at ?? [], ctx, e.as);
4734
4904
  const tests = t.tests.map((x) => this.#hoist(this.expr(x, t.inner), hasLoop(x)));
4735
4905
  if (tests.some((x) => x.v.deps.has(CELLS))) throw new NotStandalone("a test of the axis of a fold reads a cell");
4736
4906
  const step = this.#loopStep(e.step, {
4737
4907
  ...t.inner,
4738
- env: names.env
4908
+ env: withNames(names.env, t.items)
4739
4909
  }, names.vars);
4740
4910
  this.#readsNoTarget(t.texts, t.inner.level, [...tests.map((x) => x.v.deps), step.deps]);
4741
4911
  const result = this.expr(e.result, {
@@ -4749,7 +4919,7 @@ var Printer = class {
4749
4919
  ...tests.flatMap((c) => [...c.s, `if (!${this.#boolean(c.v)}) continue;`]),
4750
4920
  ...step.s
4751
4921
  ];
4752
- const inside = [...tests.flatMap((c) => [...c.v.deps]), ...step.deps].filter((d) => d !== loop && d !== t.inner.level);
4922
+ const inside = [...tests.flatMap((c) => [...c.v.deps]), ...step.deps].filter((d) => d !== loop && d !== t.inner.level && d !== t.item);
4753
4923
  const deps = /* @__PURE__ */ new Set([
4754
4924
  ...names.deps,
4755
4925
  ...t.start,
@@ -4759,6 +4929,7 @@ var Printer = class {
4759
4929
  return {
4760
4930
  s: [
4761
4931
  ...names.s,
4932
+ ...step.init,
4762
4933
  ...t.s,
4763
4934
  t.head,
4764
4935
  ...ind(body),