@mark1russell7/vex 0.7.0 → 0.8.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.
package/src/ir.ts CHANGED
@@ -36,6 +36,8 @@ export type Axis =
36
36
  | { readonly t: "successors" }
37
37
  | { readonly t: "predecessors" }
38
38
  | { readonly t: "reachable" }
39
+ | { readonly t: "list"; readonly list: Expr }
40
+ | { readonly t: "reverse"; readonly axis: Axis }
39
41
  | { readonly t: "where"; readonly axis: Axis; readonly test: Expr };
40
42
 
41
43
  /** A Vex expression. */
@@ -49,6 +51,8 @@ export type Expr =
49
51
  | { readonly tag: "each"; readonly axis: Axis; readonly body: Expr; readonly at?: Addr }
50
52
  | { readonly tag: "fold"; readonly axis: Axis; readonly init: Names; readonly step: Names; readonly result: Expr; readonly at?: Addr }
51
53
  | { readonly tag: "iterate"; readonly limit: number; readonly init: Names; readonly step: Names; readonly until?: Expr; readonly result: Expr }
54
+ | { readonly tag: "fn"; readonly params: readonly string[]; readonly body: Expr }
55
+ | { readonly tag: "call"; readonly fn: string; readonly args: readonly Expr[] }
52
56
  | { readonly tag: "ext"; readonly kind: string; readonly data: unknown };
53
57
 
54
58
  /** The names of a loop and an expression for each (LOOP.NAMES). */
@@ -114,6 +118,15 @@ export function iterate(limit: number, init: Names, step: Names, result: Expr, u
114
118
  return until === undefined ? { tag: "iterate", limit, init, step, result } : { tag: "iterate", limit, init, step, until, result };
115
119
  }
116
120
 
121
+ /**
122
+ * This function makes a function (FN.BIND): the names of its parameters and its body. A function is only the value of
123
+ * a binding of `let`. The body sees the names of the place of that `let`, so a function cannot call itself.
124
+ */
125
+ export const fn = (params: readonly string[], body: Expr): ExprOf<"fn"> => ({ tag: "fn", params, body });
126
+
127
+ /** This function makes a call of a bound function (FN.CALL). The body evaluates at the position of the call. */
128
+ export const call = (name: string, ...args: Expr[]): ExprOf<"call"> => ({ tag: "call", fn: name, args });
129
+
117
130
  /** This function makes an expression of an extension kind. */
118
131
  export const ext = (kind: string, data: unknown): ExprOf<"ext"> => ({ tag: "ext", kind, data });
119
132
 
@@ -186,6 +199,16 @@ export const axes = {
186
199
  predecessors: Object.freeze({ t: "predecessors" }) as Axis,
187
200
  /** Each key that a path of edges reaches from the focus, in breadth-first order, without the focus. */
188
201
  reachable: Object.freeze({ t: "reachable" }) as Axis,
202
+ /**
203
+ * The targets of `axis` in the reverse order (AXIS.REVERSE). The inner axis is a base axis: not `where`, `reverse` or
204
+ * `table`. A test goes outside: `where(reverse(axis), test)`.
205
+ */
206
+ reverse: (axis: Axis): Axis => ({ t: "reverse", axis }),
207
+ /**
208
+ * The keys of the items of the list that `list` gives, in the order of the list (AXIS.LIST). The expression evaluates
209
+ * before the targets. An error item gives an error item for its target.
210
+ */
211
+ list: (list: Expr): Axis => ({ t: "list", list }),
189
212
  /** The targets of `axis` where `test` gives `true`. */
190
213
  where: (axis: Axis, test: Expr): Axis => ({ t: "where", axis, test }),
191
214
  } as const;
@@ -240,7 +263,7 @@ export function isExpr(u: unknown): u is Expr {
240
263
  }
241
264
 
242
265
  /** The tags of the expressions of this version of Vex. */
243
- const TAGS: ReadonlySet<unknown> = new Set(["lit", "ref", "app", "let", "var", "rec", "each", "fold", "iterate", "ext"]);
266
+ const TAGS: ReadonlySet<unknown> = new Set(["lit", "ref", "app", "let", "var", "rec", "each", "fold", "iterate", "fn", "call", "ext"]);
244
267
 
245
268
  /**
246
269
  * This function gives the reason that a value is not a valid expression, or `undefined` for a valid expression
@@ -253,6 +276,7 @@ export function shapeError(u: unknown): string | undefined {
253
276
  const tag = isObject(bad.u) ? bad.u["tag"] : undefined;
254
277
  if (bad.axis) return "the expression has an axis that is not valid";
255
278
  if (typeof tag === "string" && !TAGS.has(tag)) return `the expression has a node with the tag "${tag}", and this version of Vex does not know it`;
279
+ if (tag === "fn") return "the expression has a function outside the bindings of a let, or a function with parameters that are not distinct names";
256
280
  return typeof tag === "string" ? `the expression has a node "${tag}" with parts that are not valid` : "the expression has a part that is not a node";
257
281
  }
258
282
 
@@ -305,6 +329,13 @@ function badPart(todo: { readonly axis: boolean; readonly u: unknown }[], whole:
305
329
  } else if (t === "where") {
306
330
  push(true, [u["axis"]]);
307
331
  push(false, [u["test"]]);
332
+ } else if (t === "list") {
333
+ push(false, [u["list"]]);
334
+ } else if (t === "reverse") {
335
+ // The inner axis of reverse is a base axis.
336
+ const inner = u["axis"];
337
+ if (!isObject(inner) || ["where", "reverse", "table"].includes(inner["t"] as string)) return item;
338
+ push(true, [inner]);
308
339
  } else if (!["all", "others", "other", "children", "ancestors", "descendants", "siblings", "successors", "predecessors", "reachable"].includes(t as string)) {
309
340
  return item;
310
341
  }
@@ -324,9 +355,16 @@ function badPart(todo: { readonly axis: boolean; readonly u: unknown }[], whole:
324
355
  if (typeof u["op"] !== "string" || !Array.isArray(u["args"])) return item;
325
356
  push(false, u["args"]);
326
357
  break;
327
- case "let":
358
+ case "let": {
328
359
  if (!isObject(u["bind"]) || Array.isArray(u["bind"])) return item;
329
- push(false, [...Object.values(u["bind"]), u["body"]]);
360
+ // A binding can be a function (FN.BIND): its parameters are distinct names, and its body is an expression.
361
+ const values = Object.values(u["bind"]).map((b) => (isObject(b) && b["tag"] === "fn" && isStringArray(b["params"]) && new Set(b["params"]).size === b["params"].length ? b["body"] : b));
362
+ push(false, [...values, u["body"]]);
363
+ break;
364
+ }
365
+ case "call":
366
+ if (typeof u["fn"] !== "string" || !Array.isArray(u["args"])) return item;
367
+ push(false, u["args"]);
330
368
  break;
331
369
  case "var":
332
370
  if (typeof u["name"] !== "string") return item;
@@ -374,15 +412,18 @@ function badPart(todo: { readonly axis: boolean; readonly u: unknown }[], whole:
374
412
  export function children(e: Expr): readonly Expr[] {
375
413
  switch (e.tag) {
376
414
  case "app":
415
+ case "call":
377
416
  return e.args;
417
+ case "fn":
418
+ return [e.body];
378
419
  case "let":
379
420
  return [...Object.values(e.bind), e.body];
380
421
  case "rec":
381
422
  return Object.values(e.fields);
382
423
  case "each":
383
- return [e.body, ...whereTests(e.axis), ...addrExprs(e.at)];
424
+ return [e.body, ...axisExprs(e.axis), ...addrExprs(e.at)];
384
425
  case "fold":
385
- return [...Object.values(e.init), ...Object.values(e.step), ...whereTests(e.axis), ...addrExprs(e.at), e.result];
426
+ return [...Object.values(e.init), ...Object.values(e.step), ...axisExprs(e.axis), ...addrExprs(e.at), e.result];
386
427
  case "iterate":
387
428
  return [...Object.values(e.init), ...Object.values(e.step), ...(e.until === undefined ? [] : [e.until]), e.result];
388
429
  case "ref":
@@ -394,9 +435,21 @@ export function children(e: Expr): readonly Expr[] {
394
435
  }
395
436
  }
396
437
 
397
- /** This function gives the tests of the `where` axes of an axis, from the inside out. It uses a loop, not recursion. */
398
- function whereTests(a: Axis): readonly Expr[] {
438
+ /**
439
+ * This function gives the expressions of an axis, in the order of their child indexes. The expression of the axis
440
+ * `list` comes first, and then the tests of the `where` axes from the inside out. It uses a loop, not recursion.
441
+ */
442
+ export function axisExprs(a: Axis): readonly Expr[] {
399
443
  const out: Expr[] = [];
400
- for (let x: Axis = a; x.t === "where"; x = x.axis) out.unshift(x.test);
401
- return out;
444
+ for (let x = a; x.t === "where"; x = x.axis) out.unshift(x.test);
445
+ const list = listOf(a);
446
+ return list === undefined ? out : [list, ...out];
447
+ }
448
+
449
+ /** This function gives the expression of the axis `list` inside an axis, through `where` and `reverse`, if it has one. */
450
+ export function listOf(a: Axis): Expr | undefined {
451
+ let x = a;
452
+ while (x.t === "where") x = x.axis;
453
+ const base = x.t === "reverse" ? x.axis : x;
454
+ return base.t === "list" ? base.list : undefined;
402
455
  }
package/src/json.ts CHANGED
@@ -146,8 +146,18 @@ function mapValues(u: unknown, f: (value: unknown) => Result<unknown>): Result<u
146
146
  out["at"] = moves;
147
147
  return undefined;
148
148
  };
149
+ // The test of where and the list of the axis list are expressions, also inside reverse.
149
150
  const axis = (a: unknown): Result<unknown> => {
150
- if (!isRecord(a) || a["t"] !== "where") return ok(a);
151
+ if (!isRecord(a)) return ok(a);
152
+ if (a["t"] === "list") {
153
+ const list = mapValues(a["list"], f);
154
+ return list.ok ? ok({ ...a, list: list.value }) : list;
155
+ }
156
+ if (a["t"] === "reverse") {
157
+ const inner = axis(a["axis"]);
158
+ return inner.ok ? ok({ ...a, axis: inner.value }) : inner;
159
+ }
160
+ if (a["t"] !== "where") return ok(a);
151
161
  const inner = axis(a["axis"]);
152
162
  if (!inner.ok) return inner;
153
163
  const test = mapValues(a["test"], f);
@@ -181,6 +191,10 @@ function mapValues(u: unknown, f: (value: unknown) => Result<unknown>): Result<u
181
191
  }
182
192
  case "iterate":
183
193
  return each("init") ?? each("step") ?? (u["until"] === undefined ? undefined : child("until")) ?? child("result") ?? ok(out);
194
+ case "fn":
195
+ return child("body") ?? ok(out);
196
+ case "call":
197
+ return each("args") ?? ok(out);
184
198
  case "ref":
185
199
  return address() ?? ok(out);
186
200
  default:
package/src/list.ts CHANGED
@@ -19,11 +19,11 @@ export const vexList = <T>(items: readonly ListItem<T>[]): VexList<T> => Object.
19
19
  export const isVexList = (u: unknown): u is VexList =>
20
20
  (u as { kind?: unknown } | null | undefined)?.kind === "vex.list" && Array.isArray((u as { items?: unknown }).items);
21
21
 
22
- /** The name of a core list op. */
23
- export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors";
22
+ /** The name of a core list op. `sortBy` and `take` give a list, and the other ops give a value. */
23
+ export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors" | "sortBy" | "take" | "nth";
24
24
 
25
25
  /** The names of the core list ops. They apply when the first argument is a list. */
26
- export const LIST_OPS: ReadonlySet<string> = new Set<ListOp>(["count", "sum", "min", "max", "mean", "any", "all", "none", "values", "keys", "reduce", "first", "errors"]);
26
+ export const LIST_OPS: ReadonlySet<string> = new Set<ListOp>(["count", "sum", "min", "max", "mean", "any", "all", "none", "values", "keys", "reduce", "first", "errors", "sortBy", "take", "nth"]);
27
27
 
28
28
  /** This function tells if a name is the name of a core list op. */
29
29
  export const isListOp = (op: string): op is ListOp => LIST_OPS.has(op);
package/src/sheet.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  */
12
12
  import { vexError } from "./errors.ts";
13
13
  import { evaluate, explain, nestingOf, type CompileOptions, type EvalOptions, type ExtHandler, type Program } from "./evaluate.ts";
14
- import { addrExprs, children, ext, isMove, moveExpr, shapeError, type Axis, type Expr, type FixedMove, type Move } from "./ir.ts";
14
+ import { addrExprs, children, ext, isMove, listOf, moveExpr, shapeError, type Axis, type Expr, type FixedMove, type Move } from "./ir.ts";
15
15
  import { fail, type Result } from "./result.ts";
16
16
  import { resolveAddr, type Space } from "./space.ts";
17
17
  import type { Trace } from "./trace.ts";
@@ -120,6 +120,11 @@ type Direction = "before" | "after" | "down";
120
120
 
121
121
  /** This function adds the direction of one move or one axis of a read of a column from itself. */
122
122
  function addDirection(d: Set<Direction>, m: Move | Axis): void {
123
+ // The reverse order reads the same keys as its axis.
124
+ if (m.t === "reverse") {
125
+ addDirection(d, m.axis);
126
+ return;
127
+ }
123
128
  if (m.t === "offset" || m.t === "window") {
124
129
  // An offset moves by its first step that is not 0. A window reads the keys from `from` to `to`.
125
130
  const steps = m.t === "offset" ? [m.d.find((x) => x !== 0) ?? 0] : [m.from, m.to];
@@ -129,6 +134,7 @@ function addDirection(d: Set<Direction>, m: Move | Axis): void {
129
134
  // Stryker disable next-line EqualityOperator: see the comment above
130
135
  if (steps.some((x) => x > 0)) d.add("after");
131
136
  } else if (m.t === "children" || m.t === "descendants") d.add("down");
137
+ // The items of a list can be at each key, so the axis list gives no direction, and the other reads decide the order.
132
138
  }
133
139
 
134
140
  /** This function gives the base axis of an axis, and the tests of its `where` axes. */
@@ -158,9 +164,10 @@ function readsOf(t: Table, self: string): { readonly columns: ReadonlySet<string
158
164
  if (e.tag === "each" || e.tag === "fold") {
159
165
  const { base, tests } = partsOf(e.axis);
160
166
  for (const x of [...(e.tag === "each" ? [e.body] : Object.values(e.step)), ...tests]) todo.push([x, base]);
161
- // The values of init, the result of a fold and the expressions of the address evaluate at the node, so they keep
162
- // the outer direction.
163
- for (const x of [...(e.tag === "fold" ? [...Object.values(e.init), e.result] : []), ...addrExprs(e.at)]) todo.push([x, axis]);
167
+ // The values of init, the result of a fold, the list of the axis list and the expressions of the address evaluate
168
+ // at the node, so they keep the outer direction.
169
+ const list = listOf(e.axis);
170
+ for (const x of [...(e.tag === "fold" ? [...Object.values(e.init), e.result] : []), ...addrExprs(e.at), ...(list === undefined ? [] : [list])]) todo.push([x, axis]);
164
171
  } else if (e.tag !== "ext") {
165
172
  for (const child of children(e)) todo.push([child, axis]);
166
173
  // The data of an extension of another kind can look like a cell reference. It only changes the order of a fill.
package/src/space.ts CHANGED
@@ -450,9 +450,17 @@ export function axisTargets(s: Space, at: Position, a: Exclude<Axis, { readonly
450
450
  case "predecessors":
451
451
  case "reachable":
452
452
  return graphTargets(s, at, a.t);
453
+ case "reverse": {
454
+ // The validation of the IR keeps a base axis inside reverse: not where, reverse or table.
455
+ const r = axisTargets(s, at, a.axis as Exclude<Axis, { readonly t: "where" }>);
456
+ return r.ok ? ok(r.value.toReversed()) : r;
457
+ }
453
458
  case "table":
454
459
  // The evaluator gives the axis table the space of its table. Here, without the tables, it has no targets.
455
460
  return fail(vexError("no-table", `the axis "table" needs the tables of the evaluation, and "${a.name}" is not one of them`, at));
461
+ case "list":
462
+ // The evaluator gives the targets of the axis list from the value of its expression. A space alone has no list.
463
+ return fail(vexError("bad-expression", 'the axis "list" needs the evaluator, which gives the value of its list', at));
456
464
  }
457
465
  }
458
466