@mark1russell7/vex 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +52 -2
- package/build/index.d.ts +915 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1850 -0
- package/build/index.js.map +1 -0
- package/package.json +61 -3
- package/src/builder.ts +723 -0
- package/src/deps.ts +33 -0
- package/src/domain.ts +136 -0
- package/src/errors.ts +102 -0
- package/src/evaluate.ts +642 -0
- package/src/index.ts +16 -0
- package/src/ir.ts +212 -0
- package/src/json.ts +79 -0
- package/src/list.ts +35 -0
- package/src/result.ts +44 -0
- package/src/sheet.ts +146 -0
- package/src/space.ts +329 -0
- package/src/trace.ts +37 -0
- package/src/traversal.ts +125 -0
package/src/deps.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { children, type Addr, type Axis, type Expr } from "./ir.ts";
|
|
2
|
+
|
|
3
|
+
/** One static read of a program: a field path at an address, inside zero or more axes. */
|
|
4
|
+
export interface Dependency {
|
|
5
|
+
/** The address of the read. The empty address is the focus. */
|
|
6
|
+
readonly at: Addr;
|
|
7
|
+
/** The field path of the read. */
|
|
8
|
+
readonly path: readonly string[];
|
|
9
|
+
/** The axes around the read, from the outside to the inside. Inside an axis, the focus is the target. */
|
|
10
|
+
readonly axes: readonly Axis["t"][];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* This function gives the static reads of an expression. A host can use them to recompute a program only
|
|
15
|
+
* when a field that it reads changes.
|
|
16
|
+
*/
|
|
17
|
+
export function deps(e: Expr): readonly Dependency[] {
|
|
18
|
+
const out: Dependency[] = [];
|
|
19
|
+
const walk = (x: Expr, axes: readonly Axis["t"][]): void => {
|
|
20
|
+
if (x.tag === "ref") {
|
|
21
|
+
out.push({ at: x.at ?? [], path: x.path, axes });
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (x.tag === "each") {
|
|
25
|
+
const inner = [...axes, x.axis.t];
|
|
26
|
+
children(x).forEach((c) => walk(c, inner));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
children(x).forEach((c) => walk(c, axes));
|
|
30
|
+
};
|
|
31
|
+
walk(e, []);
|
|
32
|
+
return out;
|
|
33
|
+
}
|
package/src/domain.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Domains. A domain tells which values belong to it and which ops it has. The domain object owns this data.
|
|
3
|
+
* Vex does not write to the prototypes of domain classes, and an import has no side effects.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** An algebraic law that an op claims. `@mark1russell7/vex-testkit` checks each claim with property tests. */
|
|
7
|
+
export type Law = "commutative" | "associative" | "idempotent";
|
|
8
|
+
|
|
9
|
+
/** The kind of a parameter. The interpreter checks the arguments of an op that declares its parameters. */
|
|
10
|
+
export type ParamKind = "domain" | "number" | "boolean" | "string" | "any";
|
|
11
|
+
|
|
12
|
+
/** The description of one op of a domain. */
|
|
13
|
+
export interface OpSpec<D = never> {
|
|
14
|
+
/**
|
|
15
|
+
* The implementation, for an op that is not a method of the value (static-land style). The first argument
|
|
16
|
+
* is the receiver. Without `fn`, the interpreter calls the method of the receiver with the same name.
|
|
17
|
+
*/
|
|
18
|
+
readonly fn?: (self: D, ...args: never[]) => unknown;
|
|
19
|
+
/** The laws that the op claims. */
|
|
20
|
+
readonly laws?: readonly Law[];
|
|
21
|
+
/** The identity element of the op, for reductions over an empty list. */
|
|
22
|
+
readonly identity?: () => D;
|
|
23
|
+
/** Scalar lifting: `true` lifts each number argument with `fromScalar`. An array sets it for each argument. */
|
|
24
|
+
readonly liftScalar?: boolean | readonly boolean[];
|
|
25
|
+
/** The kinds of the parameters after the receiver. When set, the interpreter checks the arguments. */
|
|
26
|
+
readonly params?: readonly ParamKind[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The type of an op that lifts number arguments. Use it to type the ops of a domain under `isolatedDeclarations`. */
|
|
30
|
+
export type LiftedOp<D> = OpSpec<D> & { readonly liftScalar: true };
|
|
31
|
+
|
|
32
|
+
/** The type of an op with an implementation function `F`. */
|
|
33
|
+
export type FnOp<D, F extends (self: D, ...args: never[]) => unknown> = OpSpec<D> & { readonly fn: F };
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The type of an op table: each name in `Names` is an op, and each name in `Lifted` lifts number arguments.
|
|
37
|
+
* Use it to type the ops of a domain under `isolatedDeclarations`.
|
|
38
|
+
*/
|
|
39
|
+
export type OpTable<D, Names extends string, Lifted extends string = never> = {
|
|
40
|
+
readonly [N in Names]: N extends Lifted ? LiftedOp<D> : OpSpec<D>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** The description of a domain, as a caller gives it to `defineDomain`. */
|
|
44
|
+
export interface DomainSpec<Name extends string, D, Ops extends Readonly<Record<string, OpSpec<D>>>> {
|
|
45
|
+
/** The name of the domain. */
|
|
46
|
+
readonly name: Name;
|
|
47
|
+
/** The runtime test for the values of the domain. It must not give false positives. */
|
|
48
|
+
readonly is: (u: unknown) => u is D;
|
|
49
|
+
/** The ops of the domain, with their metadata. */
|
|
50
|
+
readonly ops: Ops;
|
|
51
|
+
/** With `"all"`, each method of a value is an op, also without an entry in `ops`. The default is `"declared"`. */
|
|
52
|
+
readonly methods?: "declared" | "all";
|
|
53
|
+
/** The scalar lift: it makes a domain value from a number. */
|
|
54
|
+
readonly fromScalar?: (n: number) => D;
|
|
55
|
+
/** A check of the values that ops give. A value that fails the check gives the error `#NUM!`. */
|
|
56
|
+
readonly valid?: (d: D) => boolean;
|
|
57
|
+
/** A short text for a value, for traces and the site. */
|
|
58
|
+
readonly show?: (d: D) => string;
|
|
59
|
+
/** The JSON form of a value, for literals in serialized programs. */
|
|
60
|
+
readonly encode?: (d: D) => unknown;
|
|
61
|
+
/** The value of a JSON form that `encode` made. */
|
|
62
|
+
readonly decode?: (json: unknown) => D;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A domain. Make one with `defineDomain`. */
|
|
66
|
+
export interface Domain<Name extends string = string, D = unknown, Ops extends Readonly<Record<string, OpSpec<D>>> = Readonly<Record<string, OpSpec<D>>>>
|
|
67
|
+
extends DomainSpec<Name, D, Ops> {
|
|
68
|
+
readonly kind: "vex.domain";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A domain with any value type. The interpreter uses this type. */
|
|
72
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- a list of domains with different value types needs `any`: `fn` is contravariant in the value type.
|
|
73
|
+
export type AnyDomain = Domain<string, any, Readonly<Record<string, OpSpec<any>>>>;
|
|
74
|
+
|
|
75
|
+
/** This function makes a domain. The result is frozen. */
|
|
76
|
+
export function defineDomain<const Name extends string, D, const Ops extends Readonly<Record<string, OpSpec<D>>>>(
|
|
77
|
+
spec: DomainSpec<Name, D, Ops>,
|
|
78
|
+
): Domain<Name, D, Ops> {
|
|
79
|
+
return Object.freeze({ ...spec, kind: "vex.domain" as const });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const FORBIDDEN = new Set(["constructor", "__proto__", "prototype", "__defineGetter__", "__defineSetter__", "__lookupGetter__", "__lookupSetter__"]);
|
|
83
|
+
|
|
84
|
+
/** This function tells if a name can be an op name. */
|
|
85
|
+
export const isOpName = (name: string): boolean => name.length > 0 && !FORBIDDEN.has(name);
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* This function finds a method on a value: an own function property, or a function on the prototype chain.
|
|
89
|
+
* It stops at `Object.prototype` and `Function.prototype`, so inherited members (for example `toString`) are not ops.
|
|
90
|
+
*/
|
|
91
|
+
export function findMethod(self: unknown, name: string): ((...args: unknown[]) => unknown) | undefined {
|
|
92
|
+
if (!isOpName(name)) return undefined;
|
|
93
|
+
// A primitive has no own methods. The loop below stops at null, so null needs no test here.
|
|
94
|
+
if (typeof self !== "object" && typeof self !== "function") return undefined;
|
|
95
|
+
let o: object | null = self;
|
|
96
|
+
while (o !== null && o !== Object.prototype && o !== Function.prototype) {
|
|
97
|
+
const d = Object.getOwnPropertyDescriptor(o, name);
|
|
98
|
+
if (d !== undefined) {
|
|
99
|
+
return typeof d.value === "function" ? (d.value as (...args: unknown[]) => unknown) : undefined;
|
|
100
|
+
}
|
|
101
|
+
o = Object.getPrototypeOf(o) as object | null;
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** An op that the interpreter found for a receiver. */
|
|
107
|
+
export interface ResolvedOp {
|
|
108
|
+
readonly domain: AnyDomain;
|
|
109
|
+
readonly spec: OpSpec<unknown>;
|
|
110
|
+
readonly call: (args: readonly unknown[]) => unknown;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const EMPTY_SPEC: OpSpec<unknown> = Object.freeze({});
|
|
114
|
+
|
|
115
|
+
/** This function finds the op `name` for `self` in one domain. It gives `undefined` if the domain does not have it. */
|
|
116
|
+
export function resolveOp(domain: AnyDomain, self: unknown, name: string): ResolvedOp | undefined {
|
|
117
|
+
if (!isOpName(name)) return undefined;
|
|
118
|
+
const declared = Object.hasOwn(domain.ops, name) ? (domain.ops[name] as OpSpec<unknown>) : undefined;
|
|
119
|
+
if (declared?.fn !== undefined) {
|
|
120
|
+
const fn = declared.fn as (self: unknown, ...args: unknown[]) => unknown;
|
|
121
|
+
return { domain, spec: declared, call: (args) => fn(self, ...args) };
|
|
122
|
+
}
|
|
123
|
+
if (declared === undefined && domain.methods !== "all") return undefined;
|
|
124
|
+
const method = findMethod(self, name);
|
|
125
|
+
if (method === undefined) return undefined;
|
|
126
|
+
return { domain, spec: declared ?? EMPTY_SPEC, call: (args) => method.apply(self, [...args]) };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** This function gives the domain of a value: the first domain whose `is` accepts it. */
|
|
130
|
+
export function domainOf(domains: readonly AnyDomain[], value: unknown): AnyDomain | undefined {
|
|
131
|
+
return domains.find((d) => d.is(value));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** This function tells if the `liftScalar` setting lifts the argument at `index`. */
|
|
135
|
+
export const liftsAt = (lift: OpSpec<unknown>["liftScalar"], index: number): boolean =>
|
|
136
|
+
lift === true || (Array.isArray(lift) && lift[index] === true);
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error codes of Vex. The names come from the spreadsheet error values, because a Vex program is a
|
|
3
|
+
* spreadsheet formula over records.
|
|
4
|
+
*/
|
|
5
|
+
export type ErrorCode = "#REF!" | "#N/A" | "#VALUE!" | "#NAME?" | "#NUM!" | "#CALC!" | "#ARGS" | "#CYCLE!";
|
|
6
|
+
|
|
7
|
+
/** The cause of an error, in more detail than the code. */
|
|
8
|
+
export type ErrorKind =
|
|
9
|
+
| "unknown-key"
|
|
10
|
+
| "not-a-pair"
|
|
11
|
+
| "out-of-bounds"
|
|
12
|
+
| "no-offset"
|
|
13
|
+
| "no-grid"
|
|
14
|
+
| "no-tree"
|
|
15
|
+
| "missing-field"
|
|
16
|
+
| "empty"
|
|
17
|
+
| "not-instance"
|
|
18
|
+
| "kind-mismatch"
|
|
19
|
+
| "unknown-op"
|
|
20
|
+
| "unbound"
|
|
21
|
+
| "not-finite"
|
|
22
|
+
| "invalid-value"
|
|
23
|
+
| "threw"
|
|
24
|
+
| "undefined-result"
|
|
25
|
+
| "args"
|
|
26
|
+
| "cycle"
|
|
27
|
+
| "bad-expression";
|
|
28
|
+
|
|
29
|
+
const CODE_OF: Readonly<Record<ErrorKind, ErrorCode>> = {
|
|
30
|
+
"unknown-key": "#REF!",
|
|
31
|
+
"not-a-pair": "#REF!",
|
|
32
|
+
"out-of-bounds": "#REF!",
|
|
33
|
+
"no-offset": "#REF!",
|
|
34
|
+
"no-grid": "#REF!",
|
|
35
|
+
"no-tree": "#REF!",
|
|
36
|
+
"missing-field": "#N/A",
|
|
37
|
+
empty: "#N/A",
|
|
38
|
+
"not-instance": "#VALUE!",
|
|
39
|
+
"kind-mismatch": "#VALUE!",
|
|
40
|
+
"bad-expression": "#VALUE!",
|
|
41
|
+
"unknown-op": "#NAME?",
|
|
42
|
+
unbound: "#NAME?",
|
|
43
|
+
"not-finite": "#NUM!",
|
|
44
|
+
"invalid-value": "#NUM!",
|
|
45
|
+
threw: "#CALC!",
|
|
46
|
+
"undefined-result": "#CALC!",
|
|
47
|
+
args: "#ARGS",
|
|
48
|
+
cycle: "#CYCLE!",
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** A Vex error. It is a value: a program can catch it with `ifError`. */
|
|
52
|
+
export interface VexError {
|
|
53
|
+
/** The spreadsheet-style code, for example `#REF!`. */
|
|
54
|
+
readonly code: ErrorCode;
|
|
55
|
+
/** The cause in more detail. */
|
|
56
|
+
readonly kind: ErrorKind;
|
|
57
|
+
/** A sentence that tells what went wrong. */
|
|
58
|
+
readonly message: string;
|
|
59
|
+
/** The position of the failed node in the expression tree. The root is `[]`. */
|
|
60
|
+
readonly path: readonly number[];
|
|
61
|
+
/** The key where the evaluation started. */
|
|
62
|
+
readonly origin?: string;
|
|
63
|
+
/** The key where relative references read when the error occurred. */
|
|
64
|
+
readonly focus?: string;
|
|
65
|
+
/** The op name, for errors of an op. */
|
|
66
|
+
readonly op?: string;
|
|
67
|
+
/** The errors of the arguments, for the code `#ARGS`. */
|
|
68
|
+
readonly causes?: readonly VexError[];
|
|
69
|
+
/** The value that an op threw, for the kind `threw`. */
|
|
70
|
+
readonly thrown?: unknown;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The details that a caller can give to `vexError`. */
|
|
74
|
+
export interface ErrorDetails {
|
|
75
|
+
readonly path?: readonly number[];
|
|
76
|
+
readonly origin?: string;
|
|
77
|
+
readonly focus?: string;
|
|
78
|
+
readonly op?: string;
|
|
79
|
+
readonly causes?: readonly VexError[];
|
|
80
|
+
readonly thrown?: unknown;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** This function gives the code of an error kind. */
|
|
84
|
+
export const codeOf = (kind: ErrorKind): ErrorCode => CODE_OF[kind];
|
|
85
|
+
|
|
86
|
+
/** This function makes a `VexError`. */
|
|
87
|
+
export function vexError(kind: ErrorKind, message: string, details: ErrorDetails = {}): VexError {
|
|
88
|
+
return {
|
|
89
|
+
code: CODE_OF[kind],
|
|
90
|
+
kind,
|
|
91
|
+
message,
|
|
92
|
+
path: details.path ?? [],
|
|
93
|
+
...(details.origin === undefined ? {} : { origin: details.origin }),
|
|
94
|
+
...(details.focus === undefined ? {} : { focus: details.focus }),
|
|
95
|
+
...(details.op === undefined ? {} : { op: details.op }),
|
|
96
|
+
...(details.causes === undefined ? {} : { causes: details.causes }),
|
|
97
|
+
...(details.thrown === undefined ? {} : { thrown: details.thrown }),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** This function gives a short text for an error, for example `#REF! unknown-key: there is no key "Z"`. */
|
|
102
|
+
export const formatError = (e: VexError): string => `${e.code} ${e.kind}: ${e.message}`;
|