@mark1russell7/vex 0.9.1 → 0.11.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/evaluate.ts CHANGED
@@ -149,6 +149,8 @@ export function compile(source: Expr | Envelope, opts: CompileOptions = {}): Pro
149
149
  const CACHE = new WeakMap<Expr | Envelope, { readonly domains: unknown; readonly fns: unknown; readonly extensions: unknown; readonly program: Program }>();
150
150
 
151
151
  function compiled(expr: Expr | Envelope, opts: CompileOptions): Program {
152
+ // V-056: a program that is not an object cannot be a key of the cache. Its compilation gives #VALUE! (EVAL.SHAPE).
153
+ if (typeof expr !== "object" || expr === null) return compile(expr, opts);
152
154
  const hit = CACHE.get(expr);
153
155
  // Without the cache, each evaluation compiles again: the result is the same, so a mutant that removes a hit is
154
156
  // equivalent. A false hit changes results, and the tests find it.
@@ -193,6 +195,8 @@ class Compiler {
193
195
  readonly #domains: readonly AnyDomain[];
194
196
  readonly #fns: Readonly<Record<string, FreeFn>> | undefined;
195
197
  readonly #extensions: Readonly<Record<string, ExtHandler>> | undefined;
198
+ /** The index of the keys of each list that `get` read (LIST.GET). A list does not change, so its index stays right. */
199
+ readonly #keyIndex = new WeakMap<VexList, KeyIndex>();
196
200
 
197
201
  constructor(opts: CompileOptions) {
198
202
  // Stryker disable next-line ArrayDeclaration: a list with a value that is not a domain matches no value
@@ -727,15 +731,15 @@ class Compiler {
727
731
  }
728
732
 
729
733
  #listOp(op: ListOp, list: VexList, rest: readonly unknown[], f: Frame, path: readonly number[]): Result<unknown> {
730
- // The options come after the first argument of reduce, take and nth.
731
- const optsArg = op === "reduce" || op === "take" || op === "nth" ? rest[1] : rest[0];
732
- // A reduction that skips error items does not skip #DEPTH! (EVAL.DEPTH).
733
- const depth = list.items.find((it) => !it.result.ok && isDepthError(it.result.error))?.result;
734
- if (depth !== undefined && !depth.ok) return fail(depth.error);
734
+ // The options come after the second argument of an op with one, for example the count of take.
735
+ const optsArg = WITH_ARGUMENT.has(op) ? rest[1] : rest[0];
735
736
  const strict = (optsArg as ListOptions | null | undefined)?.strict === true;
736
- const firstError = list.items.find((it) => !it.result.ok)?.result;
737
- if (strict && firstError !== undefined && !firstError.ok) return fail(firstError.error);
738
- const okValues = list.items.flatMap((it) => (it.result.ok ? [it.result.value] : []));
737
+ if (op === "get") return this.#get(list, rest[0], strict, f, path);
738
+ const good = goodItems(list, strict);
739
+ if (!good.ok) return good;
740
+ const okItems = good.value;
741
+ // Stryker disable next-line ConditionalExpression,ArrayDeclaration: each item of okItems has a value
742
+ const okValues = okItems.flatMap((it) => (it.result.ok ? [it.result.value] : []));
739
743
  const numbers = (): Result<readonly number[]> => {
740
744
  const bad = okValues.find((x) => typeof x !== "number");
741
745
  return bad === undefined ? ok(okValues as readonly number[]) : this.#err("kind-mismatch", `"${op}" needs numbers, but an item is a ${describeType(bad)}`, f, path, { op });
@@ -745,19 +749,34 @@ class Compiler {
745
749
  return bad === undefined ? ok(okValues as readonly boolean[]) : this.#err("kind-mismatch", `"${op}" needs booleans, but an item is a ${describeType(bad)}`, f, path, { op });
746
750
  };
747
751
  const empty = (): Result<never> => this.#err("empty", `"${op}" has no values to work with`, f, path, { op });
748
- // A count or a position of take and nth is a whole number from 0.
752
+ // A count or a position of take, drop and nth is a whole number from 0.
749
753
  const whole = (): Result<number> => {
750
754
  const n = rest[0] as number;
751
755
  return Number.isSafeInteger(n) && n >= 0 ? ok(n) : this.#err("kind-mismatch", `"${op}" needs a whole number from 0, not ${describeType(n)}`, f, path, { op });
752
756
  };
753
- const okItems = list.items.filter((it) => it.result.ok);
757
+ // The second list of append and without follows the rules of the first list (LIST.BUILD).
758
+ const other = (): Result<readonly ListItem[]> => (isVexList(rest[0]) ? goodItems(rest[0], strict) : this.#err("kind-mismatch", `"${op}" needs a list as its second argument, not a ${describeType(rest[0])}`, f, path, { op }));
754
759
  switch (op) {
755
760
  case "sortBy":
756
761
  return this.#sortBy(okItems, okValues, f, path);
757
- case "take": {
762
+ case "take":
763
+ case "drop": {
758
764
  const n = whole();
759
- return n.ok ? ok(vexList(okItems.slice(0, n.value))) : n;
765
+ if (!n.ok) return n;
766
+ return ok(vexList(op === "take" ? okItems.slice(0, n.value) : okItems.slice(n.value)));
767
+ }
768
+ case "append": {
769
+ const more = other();
770
+ return more.ok ? ok(vexList([...okItems, ...more.value])) : more;
771
+ }
772
+ case "without": {
773
+ const more = other();
774
+ if (!more.ok) return more;
775
+ const gone = new Set(more.value.map((it) => it.key));
776
+ return ok(vexList(okItems.filter((it) => !gone.has(it.key))));
760
777
  }
778
+ case "flat":
779
+ return this.#flat(okValues, strict, f, path);
761
780
  case "nth": {
762
781
  const i = whole();
763
782
  if (!i.ok) return i;
@@ -768,7 +787,7 @@ class Compiler {
768
787
  case "values":
769
788
  return ok(Object.freeze([...okValues]));
770
789
  case "keys":
771
- return ok(Object.freeze(list.items.filter((it) => it.result.ok).map((it) => it.key)));
790
+ return ok(Object.freeze(okItems.map((it) => it.key)));
772
791
  case "errors":
773
792
  return ok(Object.freeze(list.items.flatMap((it) => (it.result.ok ? [] : [it.result.error]))));
774
793
  case "first":
@@ -803,6 +822,39 @@ class Compiler {
803
822
  }
804
823
  }
805
824
 
825
+ /**
826
+ * This method gives the value of the first item with a key (LIST.GET). LIST.LENIENT applies, so an error item is not
827
+ * an item of the key. The program keeps the index of each list, so a step that reads each key of a list reads it once.
828
+ */
829
+ #get(list: VexList, key: unknown, strict: boolean, f: Frame, path: readonly number[]): Result<unknown> {
830
+ let index = this.#keyIndex.get(list);
831
+ // Stryker disable next-line ConditionalExpression: a new index of the same list has the same values
832
+ if (index === undefined) {
833
+ index = keyIndexOf(list);
834
+ this.#keyIndex.set(list, index);
835
+ }
836
+ // As goodItems: #DEPTH! comes first, and with strict, the first error item is the result.
837
+ const stop = index.depth ?? (strict ? index.first : undefined);
838
+ if (stop !== undefined) return fail(stop);
839
+ if (typeof key !== "string") return this.#err("kind-mismatch", `"get" needs a text key, not a ${describeType(key)}`, f, path, { op: "get" });
840
+ return index.values.has(key) ? ok(index.values.get(key)) : this.#err("missing-field", `the list has no item with the key ${JSON.stringify(key)}`, f, path, { op: "get" });
841
+ }
842
+
843
+ /**
844
+ * This method joins the lists that are the values of the items of a list, in order (LIST.FLAT). Each list follows the
845
+ * rules of LIST.BUILD. A value that is not a list gives `#VALUE!`.
846
+ */
847
+ #flat(values: readonly unknown[], strict: boolean, f: Frame, path: readonly number[]): Result<VexList> {
848
+ const out: ListItem[] = [];
849
+ for (const inner of values) {
850
+ if (!isVexList(inner)) return this.#err("kind-mismatch", `"flat" needs a list as the value of each item, but an item is a ${describeType(inner)}`, f, path, { op: "flat" });
851
+ const good = goodItems(inner, strict);
852
+ if (!good.ok) return good;
853
+ out.push(...good.value);
854
+ }
855
+ return ok(vexList(out));
856
+ }
857
+
806
858
  /**
807
859
  * This method sorts the items of a list by their values, from the smallest (LIST.SORT). The sort is stable. The values
808
860
  * are all numbers without NaN, or all texts. Texts compare by their UTF-16 code units.
@@ -854,6 +906,47 @@ class Compiler {
854
906
  }
855
907
  }
856
908
 
909
+ /** The list ops with a second argument before their options: a count, a position, an op name or a list. */
910
+ const WITH_ARGUMENT: ReadonlySet<ListOp> = new Set<ListOp>(["reduce", "take", "nth", "drop", "append", "without", "get"]);
911
+
912
+ /**
913
+ * This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
914
+ * error comes first (EVAL.DEPTH). With `strict`, the first error item is the result.
915
+ */
916
+ function goodItems(list: VexList, strict: boolean): Result<readonly ListItem[]> {
917
+ const depth = list.items.find((it) => !it.result.ok && isDepthError(it.result.error))?.result;
918
+ if (depth !== undefined && !depth.ok) return fail(depth.error);
919
+ const firstError = list.items.find((it) => !it.result.ok)?.result;
920
+ if (strict && firstError !== undefined && !firstError.ok) return fail(firstError.error);
921
+ return ok(list.items.filter((it) => it.result.ok));
922
+ }
923
+
924
+ /** The index of the keys of a list (LIST.GET): the value of the first item of each key, and the first errors. */
925
+ interface KeyIndex {
926
+ readonly values: ReadonlyMap<string, unknown>;
927
+ /** The first error item. With strict, it is the result. */
928
+ readonly first: VexError | undefined;
929
+ /** The first item of `#DEPTH!`. A list op does not skip it (EVAL.DEPTH). */
930
+ readonly depth: VexError | undefined;
931
+ }
932
+
933
+ /** This function makes the index of the keys of a list. An error item is not an item of its key (LIST.LENIENT). */
934
+ function keyIndexOf(list: VexList): KeyIndex {
935
+ const values = new Map<string, unknown>();
936
+ let first: VexError | undefined;
937
+ let depth: VexError | undefined;
938
+ for (const it of list.items) {
939
+ if (it.result.ok) {
940
+ // The first item of a key gives its value. A later item with the same key does not replace it.
941
+ if (!values.has(it.key)) values.set(it.key, it.result.value);
942
+ } else {
943
+ first ??= it.result.error;
944
+ if (isDepthError(it.result.error)) depth ??= it.result.error;
945
+ }
946
+ }
947
+ return { values, first, depth };
948
+ }
949
+
857
950
  const FORBIDDEN_FIELDS = new Set(["__proto__", "constructor", "prototype"]);
858
951
  /** This function tells if a segment of a path can name a field. A compiler uses the same rule as the interpreter. */
859
952
  export const isFieldName = (seg: string): boolean => seg.length > 0 && !FORBIDDEN_FIELDS.has(seg);
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /**
2
- * The kernel of Vex: spaces, addresses, the expression IR and its documents, domains, the interpreter and its folds.
2
+ * The core of Vex: spaces, addresses, the expression IR and its documents, domains, the interpreter and its folds. It
3
+ * also has Vex in the kernel of coeval: the contracts of its kinds and the reference tier.
3
4
  */
4
5
  export * from "./result.ts";
5
6
  export * from "./errors.ts";
@@ -12,6 +13,8 @@ export * from "./evaluate.ts";
12
13
  export * from "./deps.ts";
13
14
  export * from "./json.ts";
14
15
  export * from "./document.ts";
16
+ export * from "./kernel.ts";
17
+ export * from "./plan.ts";
15
18
  export * from "./traversal.ts";
16
19
  export * from "./sheet.ts";
17
20
  export * from "./builder.ts";