@mark1russell7/vex-compiler 0.8.0 → 0.9.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":";;;KAgCY,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;;;;;KCzCR;;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;;;;;;KAUvD;WACG;WAAmB,SAAS;WAAs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BAAuB;;;;;wBAerD,KAAK,WAAW,QAAQ;WAAsB;KAAc;;UAO3D;;WAEN;;WAEA;;;UAIM,sBAAsB;;WAE5B;;WAEA;WACA,OAAO;;;;;;wBAOF,OAAO,MAAM,MAAM,UAAS,kBAAuB;;;;UC7ClD;WACN,mBAAmB;;WAEnB,QAAQ;;WAER;;;KAIC;WAAqB;WAAmB;;WAA8B;WAAoB;;;;;;wBAmrDtF,WAAW,MAAM,MAAM,SAAS,gBAAgB;;;;qBCtrDnD;KACD,kBAAkB;;KAGlB,iBAAiB;;;;;;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
@@ -2059,6 +2059,11 @@ function namesOf(e, out = /* @__PURE__ */ new Set()) {
2059
2059
  const COLUMN_WORK = 10;
2060
2060
  /** This function gives the head of a loop over targets: from `lo` up to `hi`, or from `hi` down to `lo` in the reverse order. */
2061
2061
  const forHead = (j, lo, hi, reverse) => reverse ? `for (let ${j} = ${hi} - 1; ${j} >= ${lo}; ${j}--) {` : `for (let ${j} = ${lo}; ${j} < ${hi}; ${j}++) {`;
2062
+ /** This function tells if an expression has a loop in a loop: an axis, a fold or an iteration in another one. */
2063
+ function nestsLoops(e, inLoop = false) {
2064
+ const loop = e.tag === "each" || e.tag === "fold" || e.tag === "iterate";
2065
+ return loop && inLoop || children(e).some((x) => nestsLoops(x, inLoop || loop));
2066
+ }
2062
2067
  /** This function tells if an expression has a loop of names: a fold or an iteration. */
2063
2068
  function hasLoop(e) {
2064
2069
  let found = LOOPS.get(e);
@@ -2297,6 +2302,14 @@ var Printer = class {
2297
2302
  #constants = /* @__PURE__ */ new Map();
2298
2303
  /** The columns of the run, by the structure of their expression. */
2299
2304
  #columns = /* @__PURE__ */ new Map();
2305
+ /** The expressions whose code is the read of a column of the run. */
2306
+ #columned = /* @__PURE__ */ new WeakSet();
2307
+ /** The lazy constants of the run, and the printed functions that have a loop: code that reads one waits for its read. */
2308
+ #deferred = /* @__PURE__ */ new Set();
2309
+ /** The functions that the printer prints at this time, the innermost last. */
2310
+ #scopes = [];
2311
+ /** The work of the body of each bound function, by the number of its binding. */
2312
+ #fnWork = /* @__PURE__ */ new Map();
2300
2313
  /** The expressions whose code is a constant of the run. */
2301
2314
  #constant = /* @__PURE__ */ new WeakSet();
2302
2315
  /** The expressions at a place that takes a list: a binding, the head of a list op and the list of an axis. */
@@ -2381,6 +2394,10 @@ var Printer = class {
2381
2394
  fresh(prefix) {
2382
2395
  return `${prefix}${this.#names++}`;
2383
2396
  }
2397
+ /** This method tells if code reads a lazy constant of the run, or calls a printed function that has a loop. */
2398
+ #defers(c) {
2399
+ return [c.v.code, ...c.s].some((line) => [...line.matchAll(/\b(c\d+|fn\d+)\(/g)].some((m) => this.#deferred.has(m[1])));
2400
+ }
2384
2401
  /** This method gives the code of a name: a read that throws the error of its value, if it can hold an error. */
2385
2402
  #read(name) {
2386
2403
  const base = {
@@ -2430,7 +2447,8 @@ var Printer = class {
2430
2447
  }
2431
2448
  /**
2432
2449
  * This method evaluates a constant of the run once, before the loop over the keys, and gives the read of its value. A
2433
- * constant with a loop (`lazy`) evaluates on its first read instead, as a branch of the interpreter does.
2450
+ * constant with a loop (`lazy`) evaluates on its first read instead, as a branch of the interpreter does. So does a
2451
+ * constant that reads a lazy constant or calls a function with a loop. Else it starts that loop before the run.
2434
2452
  */
2435
2453
  #hoist(c, lazy = false) {
2436
2454
  if (c.v.deps.size > 0 || c.v.plain === true && c.s.length === 0) return c;
@@ -2438,10 +2456,13 @@ var Printer = class {
2438
2456
  const hit = key === void 0 ? void 0 : this.#constants.get(key);
2439
2457
  if (hit !== void 0) return hit;
2440
2458
  const name = this.fresh("c");
2441
- if (lazy) this.run.push(`const ${name} = ${this.use("L")}(() => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
2442
- else this.run.push(...this.#store(name, c, true));
2459
+ const deferred = lazy || this.#defers(c);
2460
+ if (deferred) {
2461
+ this.#deferred.add(name);
2462
+ this.run.push(`const ${name} = ${this.use("L")}(() => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
2463
+ } else this.run.push(...this.#store(name, c, true));
2443
2464
  const { list } = c.v;
2444
- const read = bare(this.#read(lazy ? {
2465
+ const read = bare(this.#read(deferred ? {
2445
2466
  code: `${name}()`,
2446
2467
  kind: c.v.kind,
2447
2468
  lazy: true,
@@ -2495,9 +2516,27 @@ var Printer = class {
2495
2516
  * from outside is a constant of the run, so its code can be outside the loop.
2496
2517
  */
2497
2518
  #columnar(e, c, ctx) {
2498
- if (ctx.level < 2 || c.v.deps.size !== 1 || !c.v.deps.has(ctx.level) || this.#work(e) < COLUMN_WORK) return false;
2519
+ if (ctx.level < 2 || !c.v.deps.has(ctx.level) || this.#work(e, ctx.fns) < COLUMN_WORK) return false;
2520
+ const scope = this.#callScope(c, ctx);
2521
+ if (c.v.deps.size !== 1 && scope === void 0) return false;
2499
2522
  if (this.#keyed(e)) return false;
2500
- return [...namesOf(e)].every((name) => (ctx.env.get(name)?.deps.size ?? 0) === 0) && !this.#callsLocal(e, ctx);
2523
+ const visible = (name) => {
2524
+ const bound = ctx.env.get(name);
2525
+ return (bound?.deps.size ?? 0) === 0 || (scope?.names.has(bound.code) ?? false);
2526
+ };
2527
+ return [...namesOf(e)].every(visible) && !this.#callsLocal(e, ctx);
2528
+ }
2529
+ /**
2530
+ * This method gives the function whose call can hold a column of a part. The part is in a loop of the function, and it
2531
+ * reads a parameter. The body of the function has a loop in a loop, so a call can read the part more than once. Each
2532
+ * name that the part reads is a parameter, a binding at the root of the body or a constant (`#columnar`). So the
2533
+ * part depends only on its target and on values that one call does not change. These are the parameters, the focus
2534
+ * and the origin. The levels in a node of the memo start at 1, far below the focus of a function. So a node of the
2535
+ * memo holds no column of a call.
2536
+ */
2537
+ #callScope(c, ctx) {
2538
+ const scope = this.#scopes.at(-1);
2539
+ return scope !== void 0 && scope.repeats && ctx.level > scope.focus && c.v.deps.has(scope.param) ? scope : void 0;
2501
2540
  }
2502
2541
  /** This method tells if an expression calls a function that is at the place of its binding, not at the start of a run. */
2503
2542
  #callsLocal(e, ctx) {
@@ -2508,10 +2547,28 @@ var Printer = class {
2508
2547
  #keyed(e) {
2509
2548
  return (this.#memo.get(e)?.length ?? 0) > 0 || children(e).some((x) => this.#keyed(x));
2510
2549
  }
2511
- /** This method gives the work of an expression: a node of the memo and a constant of the run are one read each. */
2512
- #work(e) {
2550
+ /**
2551
+ * This method gives the work of an expression: a node of the memo and a constant of the run are one read each. A call
2552
+ * adds the work of the body of its function, because the body evaluates at each call.
2553
+ */
2554
+ #work(e, fns) {
2513
2555
  if (this.#memo.has(e) || this.#constant.has(e)) return 1;
2514
- return children(e).reduce((sum, x) => sum + this.#work(x), 1);
2556
+ const body = e.tag === "call" ? this.#bodyWork(fns.get(e.fn)) : 0;
2557
+ return children(e).reduce((sum, x) => sum + this.#work(x, fns), 1 + body);
2558
+ }
2559
+ /**
2560
+ * This method gives the work of the body of a function, once for each function. Thus a chain of calls costs no more
2561
+ * than its functions. A body that is a column of the run is one read. A body sees the functions of the place of its
2562
+ * binding, and no function calls itself (FN.TOTAL).
2563
+ */
2564
+ #bodyWork(bound) {
2565
+ if (bound === void 0) return 0;
2566
+ let work = this.#fnWork.get(bound.id);
2567
+ if (work === void 0) {
2568
+ work = this.#columned.has(bound.fn.body) ? 1 : this.#work(bound.fn.body, bound.ctx.fns);
2569
+ this.#fnWork.set(bound.id, work);
2570
+ }
2571
+ return work;
2515
2572
  }
2516
2573
  /** This method gives the read of a column of the run. Two parts with the same structure share one column. */
2517
2574
  #column(e, c, ctx) {
@@ -2519,9 +2576,10 @@ var Printer = class {
2519
2576
  let name = this.#columns.get(key);
2520
2577
  if (name === void 0) {
2521
2578
  name = this.fresh("col");
2522
- this.run.push(`const ${name} = ${this.use("Q")}((${ctx.focus}) => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
2579
+ (this.#callScope(c, ctx)?.decls ?? this.run).push(`const ${name} = ${this.use("Q")}((${ctx.focus}) => {`, ...ind(c.s), ` return ${c.v.code};`, "});");
2523
2580
  this.#columns.set(key, name);
2524
2581
  }
2582
+ this.#columned.add(e);
2525
2583
  return bare({
2526
2584
  code: `${this.use("G")}(${name}(${ctx.focus}))`,
2527
2585
  kind: c.v.kind,
@@ -2671,6 +2729,8 @@ var Printer = class {
2671
2729
  const s = [];
2672
2730
  let deps = /* @__PURE__ */ new Set();
2673
2731
  const bound = [];
2732
+ const scope = this.#scopes.at(-1);
2733
+ const root = scope !== void 0 && scope.root === e ? scope : void 0;
2674
2734
  for (const [name, x] of Object.entries(e.bind)) {
2675
2735
  if (x.tag === "fn") {
2676
2736
  const b = {
@@ -2701,8 +2761,10 @@ var Printer = class {
2701
2761
  deps: c.v.deps,
2702
2762
  list: c.v.list
2703
2763
  });
2764
+ root?.names.add(b);
2704
2765
  deps = /* @__PURE__ */ new Set([...deps, ...c.v.deps]);
2705
2766
  }
2767
+ if (root !== void 0) root.root = e.body;
2706
2768
  const body = this.expr(e.body, {
2707
2769
  ...ctx,
2708
2770
  env,
@@ -2786,6 +2848,15 @@ var Printer = class {
2786
2848
  lazy: true,
2787
2849
  deps: /* @__PURE__ */ new Set([param])
2788
2850
  }));
2851
+ const scope = {
2852
+ param,
2853
+ focus,
2854
+ repeats: nestsLoops(bound.fn.body),
2855
+ names: new Set(names),
2856
+ decls: [],
2857
+ root: bound.fn.body
2858
+ };
2859
+ this.#scopes.push(scope);
2789
2860
  const body = this.expr(bound.fn.body, {
2790
2861
  origin: o,
2791
2862
  focus: k,
@@ -2795,6 +2866,7 @@ var Printer = class {
2795
2866
  fns: bound.ctx.fns,
2796
2867
  keys: /* @__PURE__ */ new Map()
2797
2868
  });
2869
+ this.#scopes.pop();
2798
2870
  const markers = /* @__PURE__ */ new Set([
2799
2871
  origin,
2800
2872
  focus,
@@ -2802,11 +2874,13 @@ var Printer = class {
2802
2874
  ]);
2803
2875
  const global = [...body.v.deps].every((d) => markers.has(d));
2804
2876
  const name = this.fresh("fn");
2877
+ if (hasLoop(bound.fn.body) || this.#defers(body)) this.#deferred.add(name);
2805
2878
  const position = body.v.deps.has(origin) ? [k, o] : body.v.deps.has(focus) ? [k] : [];
2806
2879
  const head = `const ${name} = (${[...names, ...position].join(", ")}) =>`;
2807
- const decl = body.s.length === 0 ? [`${head} ${body.v.code};`] : [
2880
+ const s = [...scope.decls, ...body.s];
2881
+ const decl = s.length === 0 ? [`${head} ${body.v.code};`] : [
2808
2882
  `${head} {`,
2809
- ...ind(body.s),
2883
+ ...ind(s),
2810
2884
  ` return ${body.v.code};`,
2811
2885
  "};"
2812
2886
  ];