@mark1russell7/vex-compiler 0.10.0 → 0.11.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/vex-compiler",
4
- "version": "0.10.0",
4
+ "version": "0.11.1",
5
5
  "description": "The Vex compiler: generated JavaScript for Vex programs, at run time and at build time, with the results of the interpreter.",
6
6
  "keywords": [
7
7
  "vex",
@@ -43,15 +43,15 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@mark1russell7/cue": "github:mark1russell7/cue",
46
- "@mark1russell7/vex-domains": "0.10.0",
47
- "@mark1russell7/vex-testkit": "0.10.0",
46
+ "@mark1russell7/vex-domains": "0.11.1",
47
+ "@mark1russell7/vex-testkit": "0.11.1",
48
48
  "@types/node": "^26.6.4",
49
49
  "fast-check": "^4.10.2",
50
50
  "typescript": "^7.0.2",
51
51
  "vitest": "^5.0.3"
52
52
  },
53
53
  "dependencies": {
54
- "@mark1russell7/vex": "0.10.0"
54
+ "@mark1russell7/vex": "0.11.1"
55
55
  },
56
56
  "scripts": {
57
57
  "build": "tsdown",
package/src/facts.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * whose op the interpreter takes first, and the kinds and the lifts of its parameters. The facts come from the op
4
4
  * tables, so the same options always give the same facts, and the same source.
5
5
  */
6
- import { children, isListOp, isOpName, liftsAt, listOf, type AnyDomain, type Expr, type ExtHandler, type FreeFn, type OpSpec, type ParamKind, type PrimitiveKind, type ReturnKind } from "@mark1russell7/vex";
6
+ import { children, isListOp, isOpName, isVexList, liftsAt, listOf, type AnyDomain, type Expr, type ExtHandler, type FreeFn, type OpSpec, type ParamKind, type PrimitiveKind, type ReturnKind } from "@mark1russell7/vex";
7
7
 
8
8
  /**
9
9
  * The most bodies of functions that tier 2 puts at the calls of one program. A function that calls another function
@@ -11,12 +11,16 @@ import { children, isListOp, isOpName, liftsAt, listOf, type AnyDomain, type Exp
11
11
  */
12
12
  export const MAX_CALLS = 10_000;
13
13
 
14
- /** The list ops that give a list or read a position of a list (LIST.SORT, LIST.TAKE, LIST.NTH, LIST.BUILD). */
15
- const ORDER_OPS: ReadonlySet<string> = new Set(["sortBy", "take", "nth", "drop", "append", "flat", "without"]);
14
+ /**
15
+ * 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,
16
+ * LIST.GET).
17
+ */
18
+ const ORDER_OPS: ReadonlySet<string> = new Set(["sortBy", "take", "nth", "drop", "append", "flat", "without", "get"]);
16
19
 
17
20
  /**
18
- * This function gives the reason that tier 2 cannot take a program with lists as values. The reason is the axis `list`
19
- * or an op on the order of a list. Tier 1 evaluates such a program. It uses a stack, not recursion.
21
+ * This function gives the reason that tier 2 cannot take a program with lists as values. The reason is the axis `list`,
22
+ * an op on the order or the keys of a list, or a list literal. Tier 1 evaluates such a program. It uses a stack, not
23
+ * recursion.
20
24
  */
21
25
  export function listReason(expr: Expr): string | undefined {
22
26
  const todo: Expr[] = [expr];
@@ -24,6 +28,7 @@ export function listReason(expr: Expr): string | undefined {
24
28
  if ((x.tag === "each" || x.tag === "fold") && listOf(x.axis) !== undefined) return 'the program has the axis "list", and tier 2 does not have lists as values yet';
25
29
  // Stryker disable next-line ConditionalExpression: a node that is not an app has no field op, so the set does not have it
26
30
  if (x.tag === "app" && ORDER_OPS.has(x.op)) return `the program has the list op "${x.op}", and tier 2 does not have lists as values yet`;
31
+ if (x.tag === "lit" && isVexList(x.value)) return "the program has a list literal, and tier 2 does not have lists as values yet";
27
32
  todo.push(...children(x));
28
33
  }
29
34
  return undefined;
package/src/lower.ts CHANGED
@@ -567,6 +567,7 @@ class Lowerer {
567
567
  case "append":
568
568
  case "flat":
569
569
  case "without":
570
+ case "get":
570
571
  /* v8 ignore next -- @preserve */
571
572
  // Stryker disable next-line all: listReason stops these ops before the lowering
572
573
  throw new Unsupported(`the list op "${op}"`);
package/src/module.ts CHANGED
@@ -369,8 +369,8 @@ const NEEDS: Readonly<Record<Helper, readonly Helper[]>> = {
369
369
  const LIST_OPS: ReadonlySet<string> = new Set(["count", "sum", "mean", "min", "max", "any", "all", "none", "first"]);
370
370
  const NUMBER_LIST_OPS: ReadonlySet<string> = new Set(["count", "sum", "mean", "min", "max"]);
371
371
  const BOOLEAN_LIST_OPS: ReadonlySet<string> = new Set(["any", "all", "none"]);
372
- /** The list ops that build a list from lists (LIST.BUILD). A module gives a reason for them. */
373
- const BUILD_OPS: ReadonlySet<string> = new Set(["drop", "append", "flat", "without"]);
372
+ /** The list ops that build a list from lists (LIST.BUILD), and `get` (LIST.GET). A module gives a reason for them. */
373
+ const BUILD_OPS: ReadonlySet<string> = new Set(["drop", "append", "flat", "without", "get"]);
374
374
 
375
375
  /** This function gives the kind of the result of a list op: first gives the kind of its items. */
376
376
  const listKind = (op: string, item: Kind | undefined): Kind | undefined => (NUMBER_LIST_OPS.has(op) ? "number" : BOOLEAN_LIST_OPS.has(op) ? "boolean" : item);
@@ -571,6 +571,23 @@ interface InlineForm {
571
571
  readonly uses: readonly number[];
572
572
  }
573
573
 
574
+ /**
575
+ * The tokens of the text between the names of an inline form: digits, spaces, operators and parentheses. The tokenizer
576
+ * takes the first token that matches at each position, as the tokenizer of JavaScript takes the longest operator. So
577
+ * `<<=`, `>>=` and `>>>=` leave a lone `=`, and an assignment stops the inline form (V-058).
578
+ */
579
+ const TOKEN = /\d|\.\d|\s|===?|!==?|<<|>>>?|<=|>=|[+\-*/%<>!?:(),&|^~]/y;
580
+
581
+ /**
582
+ * This function tells if a text is a sequence of tokens. It takes one token at each step, so its time is linear in the
583
+ * length of the text. A repeated group of the tokens can split `!==` in two ways, and its time is exponential (V-057).
584
+ */
585
+ function isTokens(t: string): boolean {
586
+ TOKEN.lastIndex = 0;
587
+ while (TOKEN.lastIndex < t.length) if (!TOKEN.test(t)) return false;
588
+ return true;
589
+ }
590
+
574
591
  /**
575
592
  * This function gives the inline form of the source of an arrow function. The body of the function must be an
576
593
  * expression of operators, numbers, its parameters and the functions of `CALLS`. Such a body cannot throw for primitive
@@ -588,7 +605,7 @@ function inlineForm(src: string): InlineForm | undefined {
588
605
  const split = (m[2] as string).split(/([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)?)/);
589
606
  for (const [i, t] of split.entries()) {
590
607
  if (i % 2 === 0) {
591
- if (!/^(?:\d|\.\d|\s|===?|!==?|<=|>=|[+\-*/%<>!?:(),&|^~])*$/.test(t) || /\+\+|--|\)\s*\(/.test(t)) return undefined;
608
+ if (!isTokens(t) || /\+\+|--|\)\s*\(/.test(t)) return undefined;
592
609
  text += t;
593
610
  } else if (CALLS.has(t)) text += t;
594
611
  else {
@@ -1196,7 +1213,7 @@ class Printer {
1196
1213
  const [head] = args;
1197
1214
  if (LIST_OPS.has(op) && head?.tag === "each") return this.#list(op, head, args.slice(1), ctx);
1198
1215
  if (head !== undefined && (op === "sortBy" || op === "take")) return this.#order(op, head, args.slice(1), ctx);
1199
- // LIST.BUILD: a module does not have the ops that build a list yet. The interpreter evaluates them.
1216
+ // LIST.BUILD and LIST.GET: a module does not have the ops that build a list, or get, yet. The interpreter evaluates them.
1200
1217
  if (head !== undefined && BUILD_OPS.has(op) && this.#isList(head, ctx)) throw new NotStandalone(`the list op "${op}" is not in a module yet`);
1201
1218
  if (head !== undefined && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, args.slice(1), ctx);
1202
1219
  const raw = args.map((x) => this.expr(x, ctx));