@mark1russell7/vex 0.17.2 → 0.19.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/README.md +6 -0
- package/build/index.d.ts +529 -125
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2573 -374
- package/build/index.js.map +1 -1
- package/package.json +5 -4
- package/src/builder.ts +75 -16
- package/src/constructs.ts +31 -0
- package/src/deps.ts +7 -1
- package/src/document.ts +18 -4
- package/src/domain.ts +128 -5
- package/src/error-text.ts +114 -0
- package/src/evaluate.ts +272 -87
- package/src/fix.ts +46 -0
- package/src/index.ts +6 -0
- package/src/ir.ts +64 -6
- package/src/json.ts +10 -1
- package/src/kernel.ts +126 -10
- package/src/limit-error.ts +14 -0
- package/src/list.ts +48 -2
- package/src/monotone.ts +259 -0
- package/src/plan.ts +11 -5
- package/src/sheet.ts +30 -19
- package/src/size.ts +1104 -0
- package/src/steps.ts +83 -0
- package/src/trace.ts +2 -0
- package/src/write.ts +18 -7
package/src/steps.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The steps of Vex (STEP.LADDER, STEP.LOWEST, STEP.MAX). A step is a level of the power of the language. Step A is a
|
|
3
|
+
* total formula language. Step B adds limits from the data and fixed points. Step C adds functions that call
|
|
4
|
+
* themselves. The checker gives the lowest step that a program needs, with the reason. The host option `maxStep` sets
|
|
5
|
+
* the highest step that the host accepts. The default is B. A program has no field for its step, so a document cannot
|
|
6
|
+
* raise its own step. The ladder is the ladder of steps of coeval (D116 of coeval), with the table of `constructs.ts`.
|
|
7
|
+
*/
|
|
8
|
+
import { checkStep, isStep, stepLadder, stepOf, stepRank, type StepLadder, type StepReason, type StepWalk } from "@mark1russell7/coeval";
|
|
9
|
+
import { CONSTRUCT_STEPS, ITERATE_SIZE, type VexStep } from "./constructs.ts";
|
|
10
|
+
import { children, limitAxis, type Expr } from "./ir.ts";
|
|
11
|
+
|
|
12
|
+
export { CONSTRUCT_STEPS, type VexStep } from "./constructs.ts";
|
|
13
|
+
|
|
14
|
+
/** The steps of Vex, from the lowest to the highest. */
|
|
15
|
+
export const VEX_STEP_NAMES: readonly VexStep[] = ["A", "B", "C"];
|
|
16
|
+
|
|
17
|
+
/** The ladder of the steps of Vex: the steps and the step of each construct. */
|
|
18
|
+
export const VEX_STEPS: StepLadder<VexStep> = /* @__PURE__ */ stepLadder<VexStep>(VEX_STEP_NAMES, CONSTRUCT_STEPS);
|
|
19
|
+
|
|
20
|
+
/** The highest step that a host accepts without the option `maxStep`. */
|
|
21
|
+
export const DEFAULT_MAX_STEP: VexStep = "B";
|
|
22
|
+
|
|
23
|
+
/** The options of the check of steps: the highest step that the host accepts. */
|
|
24
|
+
export interface StepOptions {
|
|
25
|
+
/** The highest step that the host accepts. The default is `DEFAULT_MAX_STEP`, step B. */
|
|
26
|
+
readonly maxStep?: VexStep;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The step of a program, with the reason as text, and the construct and the path of the node that needs the step. */
|
|
30
|
+
export interface ProgramStep {
|
|
31
|
+
readonly step: VexStep;
|
|
32
|
+
/** The reason as text. */
|
|
33
|
+
readonly reason: string;
|
|
34
|
+
/** The construct that needs the step. A program of step A has none. */
|
|
35
|
+
readonly construct?: string;
|
|
36
|
+
/** The path of the node of the construct, as child indexes (EVAL.LOCATION). A program of step A has none. */
|
|
37
|
+
readonly path?: readonly number[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The walk of an expression: the construct of a node is its tag, and the key of a child is its child index. An
|
|
42
|
+
* `iterate` with a limit from the data is its own construct (ITERATE.LIMIT.DATA).
|
|
43
|
+
*/
|
|
44
|
+
const WALK: StepWalk<Expr> = {
|
|
45
|
+
construct: (e) => (e.tag === "iterate" && limitAxis(e) !== undefined ? ITERATE_SIZE : e.tag),
|
|
46
|
+
children: (e) => children(e).map((child, i) => [i, child] as const),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** The position of the highest step of a construct of the table. A limit at that step or higher accepts each program without a walk. The table is frozen, so the module computes this value one time. */
|
|
50
|
+
const HIGHEST: number = Math.max(0, ...Object.values(VEX_STEPS.constructs).map((s) => stepRank(VEX_STEPS, s)));
|
|
51
|
+
|
|
52
|
+
const at = (path: readonly unknown[]): string => (path.length === 0 ? "at the root" : `at the path [${path.join(", ")}]`);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* This function gives the lowest step that a program needs, with the reason (STEP.LOWEST). The reason names the first
|
|
56
|
+
* node in the order of `children` whose construct needs that step. The program must have a valid shape (EVAL.SHAPE).
|
|
57
|
+
*/
|
|
58
|
+
export function programStep(e: Expr): ProgramStep {
|
|
59
|
+
const need = stepOf(VEX_STEPS, WALK, e);
|
|
60
|
+
if (need.reason === undefined) return { step: need.step, reason: `each construct of the program is of step ${need.step}` };
|
|
61
|
+
const { construct, path } = need.reason;
|
|
62
|
+
return { step: need.step, reason: `the node "${construct}" ${at(path)} needs step ${need.step}`, construct, path: path as readonly number[] };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* This function gives the reason that a host refuses a program for its step, or `undefined` (STEP.MAX). A program of a
|
|
67
|
+
* step above the option `maxStep` is not valid, and the reason names the step that it needs. A value of `maxStep` that
|
|
68
|
+
* is not a step of Vex is not valid too. The program must have a valid shape (EVAL.SHAPE).
|
|
69
|
+
*/
|
|
70
|
+
export function stepError(e: Expr, opts: StepOptions = {}): string | undefined {
|
|
71
|
+
const maxStep: unknown = opts.maxStep ?? DEFAULT_MAX_STEP;
|
|
72
|
+
if (!isStep(VEX_STEPS, maxStep)) {
|
|
73
|
+
const shown = typeof maxStep === "string" ? JSON.stringify(maxStep) : `of the type ${typeof maxStep}`;
|
|
74
|
+
return `the option maxStep ${shown} is not a step of Vex. The steps are ${VEX_STEP_NAMES.join(", ")} (STEP.MAX)`;
|
|
75
|
+
}
|
|
76
|
+
// Stryker disable next-line ConditionalExpression: without the shortcut, the walk gives the same answer
|
|
77
|
+
if (stepRank(VEX_STEPS, maxStep) >= HIGHEST) return undefined;
|
|
78
|
+
const refusal = checkStep(VEX_STEPS, stepOf(VEX_STEPS, WALK, e), maxStep);
|
|
79
|
+
if (refusal === undefined) return undefined;
|
|
80
|
+
// A refusal of `stepOf` has a reason, because its step is above the lowest step.
|
|
81
|
+
const { construct, path } = refusal.reason as StepReason<Expr, VexStep>;
|
|
82
|
+
return `the program needs step ${refusal.needed}, and the host accepts the steps up to ${maxStep}: the node "${construct}" ${at(path)} needs step ${refusal.needed} (STEP.MAX)`;
|
|
83
|
+
}
|
package/src/trace.ts
CHANGED
|
@@ -21,6 +21,8 @@ export interface TraceEvent {
|
|
|
21
21
|
readonly focus: string;
|
|
22
22
|
/** The field reads of a reference node. */
|
|
23
23
|
readonly reads?: readonly Read[];
|
|
24
|
+
/** The round of a `fix`: its count from 0, and the keys of the rows that it changed (FIX.TRACE). The result is the column. */
|
|
25
|
+
readonly round?: { readonly index: number; readonly changed: readonly string[] };
|
|
24
26
|
/** The result of the node. */
|
|
25
27
|
readonly result: Result<unknown>;
|
|
26
28
|
}
|
package/src/write.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { isEnvelope, ref as path, typed, type Envelope, type Json, type JsonReco
|
|
|
7
7
|
import { encodeValue } from "./codec.ts";
|
|
8
8
|
import type { AnyDomain } from "./domain.ts";
|
|
9
9
|
import type { VexError } from "./errors.ts";
|
|
10
|
-
import type
|
|
10
|
+
import { limitAxis, type Axis, type Expr, type Move } from "./ir.ts";
|
|
11
11
|
|
|
12
12
|
/** The options of the writer. */
|
|
13
13
|
export interface WriteOptions {
|
|
@@ -178,18 +178,29 @@ class Writer {
|
|
|
178
178
|
const step = Object.keys(e.step);
|
|
179
179
|
const until = e.until === undefined ? NONE : [term(e.until)];
|
|
180
180
|
const after = init.length + step.length;
|
|
181
|
+
const axis = limitAxis(e);
|
|
181
182
|
return {
|
|
182
|
-
jobs: [...Object.values(e.init).map(term), ...Object.values(e.step).map(term), ...until, term(e.result)],
|
|
183
|
-
make: (docs) =>
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
183
|
+
jobs: [...Object.values(e.init).map(term), ...Object.values(e.step).map(term), ...until, term(e.result), ...(axis === undefined ? [] : [{ t: "axis" as const, a: axis }])],
|
|
184
|
+
make: (docs) => {
|
|
185
|
+
const first = { init: namesOf(init, docs, 0) };
|
|
186
|
+
const limit = axis === undefined ? (e.limit as number) : this.#keep(typed("size", { axis: docs[after + until.length + 1] as Json }), e);
|
|
187
|
+
return typed("iterate", {
|
|
188
|
+
// The limit evaluates after the first values, so the order "reads" gives its reads after them.
|
|
189
|
+
...(reads ? { ...first, limit } : { limit, ...first }),
|
|
187
190
|
step: namesOf(step, docs, init.length),
|
|
188
191
|
...(e.until === undefined ? {} : { until: docs[after] as Json }),
|
|
189
192
|
result: docs[after + until.length] as Json,
|
|
190
|
-
})
|
|
193
|
+
});
|
|
194
|
+
},
|
|
191
195
|
};
|
|
192
196
|
}
|
|
197
|
+
case "fix":
|
|
198
|
+
return {
|
|
199
|
+
jobs: [term(e.init), term(e.step)],
|
|
200
|
+
make: (docs) => typed("fix", { name: e.name, join: e.join, init: docs[0] as Json, step: docs[1] as Json }),
|
|
201
|
+
};
|
|
202
|
+
case "last":
|
|
203
|
+
return { jobs: NONE, make: () => typed("last", e.name) };
|
|
193
204
|
case "ext":
|
|
194
205
|
return { jobs: NONE, make: () => typed(["ext", ...e.kind.split(".")], e.data as Json) };
|
|
195
206
|
}
|