@mark1russell7/vex-compiler 0.11.0 → 0.11.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/src/runtime.ts CHANGED
@@ -308,13 +308,8 @@ export function createRuntime(options: RuntimeOptions): Runtime {
308
308
  },
309
309
  // A list op reads the option `strict` of its options. The read can throw, as in the interpreter.
310
310
  opts: (value) => (value as { readonly strict?: unknown } | null | undefined)?.strict,
311
- rec(names, values) {
312
- const out: Record<string, unknown> = {};
313
- names.forEach((n, i) => {
314
- out[n] = values[i];
315
- });
316
- return Object.freeze(out);
317
- },
311
+ // Object.fromEntries gives an own field for each name, also for "__proto__" (V-062).
312
+ rec: (names, values) => Object.freeze(Object.fromEntries(names.map((n, i) => [n, values[i]]))),
318
313
  list: (keys, values) => vexList(keys.map((key, i) => ({ key, result: { ok: true, value: values[i] } }))),
319
314
  frozen: (values) => Object.freeze([...values]),
320
315
  ext(kind, data, s, origin, focus, path) {
@@ -0,0 +1,123 @@
1
+ /**
2
+ * The static order of the cells of a sheet in a module (COMPILE.MODULE.SHEET). It is the order of `SheetRun.fill` in
3
+ * `packages/core/src/sheet.ts` (SHEET.FILL). Each column comes after the columns that it reads. The keys of a column
4
+ * follow its reads from itself. The order changes only the speed, not a result (SHEET.ORDER).
5
+ */
6
+ import { addrExprs, CELL_KIND, children, isMove, listOf, moveExpr, shapeError, type Axis, type CellFormulas, type CellRef, type Expr, type Move } from "@mark1russell7/vex";
7
+
8
+ /** A direction of the reads of a column from itself: to an earlier key, to a later key, or down a tree to the children. */
9
+ export type Direction = "before" | "after" | "down";
10
+
11
+ /** This function tells if the data of an extension is a cell reference: a column, and an address of fixed moves. */
12
+ export const isCellRef = (u: unknown): u is CellRef =>
13
+ typeof u === "object" &&
14
+ u !== null &&
15
+ typeof (u as { column?: unknown }).column === "string" &&
16
+ Array.isArray((u as { at?: unknown }).at) &&
17
+ (u as { at: readonly unknown[] }).at.every((m) => isMove(m) && moveExpr(m) === undefined);
18
+
19
+ /** This function adds the direction of one move or one axis of a read of a column from itself. */
20
+ function addDirection(d: Set<Direction>, m: Move | Axis): void {
21
+ // The reverse order reads the same keys as its axis.
22
+ if (m.t === "reverse") {
23
+ addDirection(d, m.axis);
24
+ return;
25
+ }
26
+ if (m.t === "offset" || m.t === "window") {
27
+ // An offset moves by its first step that is not 0. A window reads the keys from `from` to `to`.
28
+ const steps = m.t === "offset" ? [m.d.find((x) => x !== 0) ?? 0] : [m.from, m.to];
29
+ // A step of 0 reads the cell itself, which is a cycle in each order.
30
+ // Stryker disable next-line EqualityOperator: see the comment above
31
+ if (steps.some((x) => x < 0)) d.add("before");
32
+ // Stryker disable next-line EqualityOperator: see the comment above
33
+ if (steps.some((x) => x > 0)) d.add("after");
34
+ } else if (m.t === "children" || m.t === "descendants") d.add("down");
35
+ // The items of a list can be at each key, so the axis list gives no direction, and the other reads decide the order.
36
+ }
37
+
38
+ /** This function gives the base axis of an axis, and the tests of its `where` axes. */
39
+ function partsOf(axis: Axis): { readonly base: Axis; readonly tests: readonly Expr[] } {
40
+ const tests: Expr[] = [];
41
+ let base = axis;
42
+ while (base.t === "where") {
43
+ tests.push(base.test);
44
+ base = base.axis;
45
+ }
46
+ return { base, tests };
47
+ }
48
+
49
+ /**
50
+ * This function gives the columns that the formulas of the column `self` read with a cell reference: its formula and
51
+ * the formulas of its cells. It also gives the directions of their reads of the column itself. A read in the body or
52
+ * in a test of an axis also has the direction of that axis.
53
+ */
54
+ export function readsOf(columns: ReadonlyMap<string, Expr>, cells: CellFormulas, self: string): { readonly columns: ReadonlySet<string>; readonly directions: ReadonlySet<Direction> } {
55
+ const read = new Set<string>();
56
+ const directions = new Set<Direction>();
57
+ const formula = columns.get(self);
58
+ // An expression that is not valid reads no column. A module gives the reason for it.
59
+ const todo: [Expr, Axis | undefined][] = [...(formula === undefined ? [] : [formula]), ...(cells.get(self)?.values() ?? [])].filter((e) => shapeError(e) === undefined).map((e) => [e, undefined]);
60
+ for (let item = todo.pop(); item !== undefined; item = todo.pop()) {
61
+ const [e, axis] = item;
62
+ if (e.tag === "each" || e.tag === "fold") {
63
+ const { base, tests } = partsOf(e.axis);
64
+ for (const x of [...(e.tag === "each" ? [e.body] : Object.values(e.step)), ...tests]) todo.push([x, base]);
65
+ // The values of init, the result of a fold, the list of the axis list and the expressions of the address evaluate
66
+ // at the node, so they keep the outer direction.
67
+ const list = listOf(e.axis);
68
+ for (const x of [...(e.tag === "fold" ? [...Object.values(e.init), e.result] : []), ...addrExprs(e.at), ...(list === undefined ? [] : [list])]) todo.push([x, axis]);
69
+ } else if (e.tag !== "ext") {
70
+ for (const child of children(e)) todo.push([child, axis]);
71
+ // The data of an extension of another kind can look like a cell reference. It only changes the order of a fill.
72
+ // Stryker disable next-line ConditionalExpression,LogicalOperator: see the comment above
73
+ } else if (e.kind === CELL_KIND && isCellRef(e.data)) {
74
+ read.add(e.data.column);
75
+ // The directions of the reads of another column only change the order, because that column comes first.
76
+ // Stryker disable next-line ConditionalExpression: see the comment above
77
+ if (e.data.column === self) for (const m of [...(axis === undefined ? [] : [axis]), ...e.data.at]) addDirection(directions, m);
78
+ }
79
+ }
80
+ return { columns: read, directions };
81
+ }
82
+
83
+ /**
84
+ * This function gives the columns of a sheet in an order where each column comes after the columns that it reads. The
85
+ * columns of a cycle keep their order. The columns with formulas of cells only come last.
86
+ */
87
+ export function columnOrder(columns: ReadonlyMap<string, Expr>, cells: CellFormulas): readonly string[] {
88
+ const names = new Set([...columns.keys(), ...cells.keys()]);
89
+ let left = [...names].map((c) => ({ c, needs: [...readsOf(columns, cells, c).columns].filter((x) => x !== c && names.has(x)) }));
90
+ const order: string[] = [];
91
+ const placed = new Set<string>();
92
+ // Each pass places each column whose reads are placed. A pass that places no column leaves a cycle, so it places
93
+ // the first column that is left.
94
+ while (left.length > 0) {
95
+ const ready = left.filter((x) => x.needs.every((n) => placed.has(n)));
96
+ for (const x of ready.length > 0 ? ready : left.slice(0, 1)) {
97
+ order.push(x.c);
98
+ placed.add(x.c);
99
+ }
100
+ left = left.filter((x) => !placed.has(x.c));
101
+ }
102
+ return order;
103
+ }
104
+
105
+ /**
106
+ * The order of the keys of a column, from the directions of its reads from itself. `reverse` reverses the key order. In
107
+ * a tree, `post` puts the children before their parent, and `back` reverses the order of the siblings.
108
+ */
109
+ export interface KeyOrder {
110
+ readonly reverse: boolean;
111
+ readonly post: boolean;
112
+ readonly back: boolean;
113
+ }
114
+
115
+ /**
116
+ * This function gives the order of the keys of a column. A read of a later key reverses the order. In a tree, a read of
117
+ * the children puts the children before their parent, as `keyOrder` of `SheetRun` does.
118
+ */
119
+ export function keyOrderOf(d: ReadonlySet<Direction>): KeyOrder {
120
+ const reverse = d.has("after") && !d.has("before");
121
+ const post = d.has("down");
122
+ return { reverse, post, back: post !== reverse };
123
+ }