@mark1russell7/vex 0.9.1 → 0.10.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.
@@ -727,15 +729,14 @@ class Compiler {
727
729
  }
728
730
 
729
731
  #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);
732
+ // The options come after the second argument of an op with one, for example the count of take.
733
+ const optsArg = WITH_ARGUMENT.has(op) ? rest[1] : rest[0];
735
734
  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] : []));
735
+ const good = goodItems(list, strict);
736
+ if (!good.ok) return good;
737
+ const okItems = good.value;
738
+ // Stryker disable next-line ConditionalExpression,ArrayDeclaration: each item of okItems has a value
739
+ const okValues = okItems.flatMap((it) => (it.result.ok ? [it.result.value] : []));
739
740
  const numbers = (): Result<readonly number[]> => {
740
741
  const bad = okValues.find((x) => typeof x !== "number");
741
742
  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 +746,34 @@ class Compiler {
745
746
  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
747
  };
747
748
  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.
749
+ // A count or a position of take, drop and nth is a whole number from 0.
749
750
  const whole = (): Result<number> => {
750
751
  const n = rest[0] as number;
751
752
  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
753
  };
753
- const okItems = list.items.filter((it) => it.result.ok);
754
+ // The second list of append and without follows the rules of the first list (LIST.BUILD).
755
+ 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
756
  switch (op) {
755
757
  case "sortBy":
756
758
  return this.#sortBy(okItems, okValues, f, path);
757
- case "take": {
759
+ case "take":
760
+ case "drop": {
758
761
  const n = whole();
759
- return n.ok ? ok(vexList(okItems.slice(0, n.value))) : n;
762
+ if (!n.ok) return n;
763
+ return ok(vexList(op === "take" ? okItems.slice(0, n.value) : okItems.slice(n.value)));
764
+ }
765
+ case "append": {
766
+ const more = other();
767
+ return more.ok ? ok(vexList([...okItems, ...more.value])) : more;
760
768
  }
769
+ case "without": {
770
+ const more = other();
771
+ if (!more.ok) return more;
772
+ const gone = new Set(more.value.map((it) => it.key));
773
+ return ok(vexList(okItems.filter((it) => !gone.has(it.key))));
774
+ }
775
+ case "flat":
776
+ return this.#flat(okValues, strict, f, path);
761
777
  case "nth": {
762
778
  const i = whole();
763
779
  if (!i.ok) return i;
@@ -768,7 +784,7 @@ class Compiler {
768
784
  case "values":
769
785
  return ok(Object.freeze([...okValues]));
770
786
  case "keys":
771
- return ok(Object.freeze(list.items.filter((it) => it.result.ok).map((it) => it.key)));
787
+ return ok(Object.freeze(okItems.map((it) => it.key)));
772
788
  case "errors":
773
789
  return ok(Object.freeze(list.items.flatMap((it) => (it.result.ok ? [] : [it.result.error]))));
774
790
  case "first":
@@ -803,6 +819,21 @@ class Compiler {
803
819
  }
804
820
  }
805
821
 
822
+ /**
823
+ * This method joins the lists that are the values of the items of a list, in order (LIST.FLAT). Each list follows the
824
+ * rules of LIST.BUILD. A value that is not a list gives `#VALUE!`.
825
+ */
826
+ #flat(values: readonly unknown[], strict: boolean, f: Frame, path: readonly number[]): Result<VexList> {
827
+ const out: ListItem[] = [];
828
+ for (const inner of values) {
829
+ 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" });
830
+ const good = goodItems(inner, strict);
831
+ if (!good.ok) return good;
832
+ out.push(...good.value);
833
+ }
834
+ return ok(vexList(out));
835
+ }
836
+
806
837
  /**
807
838
  * This method sorts the items of a list by their values, from the smallest (LIST.SORT). The sort is stable. The values
808
839
  * are all numbers without NaN, or all texts. Texts compare by their UTF-16 code units.
@@ -854,6 +885,21 @@ class Compiler {
854
885
  }
855
886
  }
856
887
 
888
+ /** The list ops with a second argument before their options: a count, a position, an op name or a list. */
889
+ const WITH_ARGUMENT: ReadonlySet<ListOp> = new Set<ListOp>(["reduce", "take", "nth", "drop", "append", "without"]);
890
+
891
+ /**
892
+ * This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
893
+ * error comes first (EVAL.DEPTH). With `strict`, the first error item is the result.
894
+ */
895
+ function goodItems(list: VexList, strict: boolean): Result<readonly ListItem[]> {
896
+ const depth = list.items.find((it) => !it.result.ok && isDepthError(it.result.error))?.result;
897
+ if (depth !== undefined && !depth.ok) return fail(depth.error);
898
+ const firstError = list.items.find((it) => !it.result.ok)?.result;
899
+ if (strict && firstError !== undefined && !firstError.ok) return fail(firstError.error);
900
+ return ok(list.items.filter((it) => it.result.ok));
901
+ }
902
+
857
903
  const FORBIDDEN_FIELDS = new Set(["__proto__", "constructor", "prototype"]);
858
904
  /** This function tells if a segment of a path can name a field. A compiler uses the same rule as the interpreter. */
859
905
  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";