@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/builder.ts
ADDED
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed builder. Method calls make an expression of the IR. The builder has no other job: it does not
|
|
3
|
+
* evaluate anything until a caller asks for a value.
|
|
4
|
+
*/
|
|
5
|
+
import type { AnyDomain, Domain, OpSpec } from "./domain.ts";
|
|
6
|
+
import { evaluate, explain, type EvalOptions, type ExtHandler, type FreeFn } from "./evaluate.ts";
|
|
7
|
+
import { app, each, ext, index as indexMove, key as keyMove, let_, lit, offset as offsetMove, other as otherMove, origin as originMove, parent as parentMove, rec, ref, toPath, v, type Addr, type Axis, type Expr, type Move } from "./ir.ts";
|
|
8
|
+
import { cell as cellRef, SheetRun } from "./sheet.ts";
|
|
9
|
+
import { toOptional, type Optional, type Result } from "./result.ts";
|
|
10
|
+
import type { Space } from "./space.ts";
|
|
11
|
+
import type { Trace } from "./trace.ts";
|
|
12
|
+
import { traversal, type Traversal } from "./traversal.ts";
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------- argument tokens
|
|
15
|
+
|
|
16
|
+
declare const ARG_TYPE: unique symbol;
|
|
17
|
+
declare const CHAIN_VALUE: unique symbol;
|
|
18
|
+
|
|
19
|
+
/** An argument token: an expression with the static type `T` of its value. */
|
|
20
|
+
export interface Arg<T> {
|
|
21
|
+
readonly kind: "vex.arg";
|
|
22
|
+
readonly expr: Expr;
|
|
23
|
+
/** This phantom member carries the type. It is not present at run time. */
|
|
24
|
+
readonly [ARG_TYPE]?: T;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const argToken = <T>(expr: Expr): Arg<T> => ({ kind: "vex.arg", expr });
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
// ---------------------------------------------------------------- type helpers
|
|
31
|
+
|
|
32
|
+
/** The keys of any member of a union of record types. */
|
|
33
|
+
type AllKeys<O> = O extends unknown ? keyof O : never;
|
|
34
|
+
|
|
35
|
+
/** The type of field `P` in the members of `O` that have it. */
|
|
36
|
+
type FieldType<O, P extends PropertyKey> = O extends unknown ? (P extends keyof O ? O[P] : never) : never;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The merged view of a union of record types: each field of any member, with the union of its types. A record
|
|
40
|
+
* without the field gives `#N/A` at run time.
|
|
41
|
+
*/
|
|
42
|
+
export type Merge<O> = { readonly [P in AllKeys<O>]: FieldType<O, P> };
|
|
43
|
+
|
|
44
|
+
/** The keys of `O` whose values have the type `T`. */
|
|
45
|
+
export type KeysOfType<O, T> = { [P in keyof O]-?: [NonNullable<O[P]>] extends [never] ? never : NonNullable<O[P]> extends T ? P : never }[keyof O] & string;
|
|
46
|
+
|
|
47
|
+
/** The method names of `D`. */
|
|
48
|
+
export type MethodKeys<D> = { [P in keyof D]-?: D[P] extends (...args: never[]) => unknown ? P : never }[keyof D] & string;
|
|
49
|
+
|
|
50
|
+
/** The value type of a domain. */
|
|
51
|
+
export type DomainValue<Dm> = Dm extends Domain<string, infer D, infer _Ops> ? D : never;
|
|
52
|
+
|
|
53
|
+
type OpsOf<Dm> = Dm extends Domain<string, unknown, infer Ops> ? Ops : Dm extends { readonly ops: infer Ops } ? Ops : never;
|
|
54
|
+
|
|
55
|
+
/** The op names that a chain over a value of domain `Dm` can use. */
|
|
56
|
+
export type OpNames<Dm> =
|
|
57
|
+
| (keyof OpsOf<Dm> & string)
|
|
58
|
+
| (Dm extends { readonly methods: "all" } ? MethodKeys<DomainValue<Dm>> : never);
|
|
59
|
+
|
|
60
|
+
type FnOf<Dm, M extends string> = M extends keyof OpsOf<Dm>
|
|
61
|
+
? OpsOf<Dm>[M] extends { readonly fn: (self: never, ...args: infer P) => infer R }
|
|
62
|
+
? (...args: P) => R
|
|
63
|
+
: M extends keyof DomainValue<Dm>
|
|
64
|
+
? DomainValue<Dm>[M]
|
|
65
|
+
: never
|
|
66
|
+
: M extends keyof DomainValue<Dm>
|
|
67
|
+
? DomainValue<Dm>[M]
|
|
68
|
+
: never;
|
|
69
|
+
|
|
70
|
+
/** The parameters of op `M` after the receiver. */
|
|
71
|
+
export type OpParams<Dm, M extends string> = FnOf<Dm, M> extends (...args: infer P) => unknown ? P : never;
|
|
72
|
+
|
|
73
|
+
/** The value type that op `M` gives. */
|
|
74
|
+
export type OpReturn<Dm, M extends string> = FnOf<Dm, M> extends (...args: never[]) => infer R ? R : never;
|
|
75
|
+
|
|
76
|
+
type Lifts<Dm, M extends string> = M extends keyof OpsOf<Dm> ? (OpsOf<Dm>[M] extends { readonly liftScalar: true } ? true : false) : false;
|
|
77
|
+
|
|
78
|
+
/** The first domain of `Dms` whose values include `T`, or `never`. */
|
|
79
|
+
export type DomainFor<Dms extends readonly AnyDomain[], T> = Dms extends readonly [infer H, ...infer R extends readonly AnyDomain[]]
|
|
80
|
+
? [T] extends [never]
|
|
81
|
+
? never
|
|
82
|
+
: [T] extends [DomainValue<H>]
|
|
83
|
+
? H
|
|
84
|
+
: DomainFor<R, T>
|
|
85
|
+
: never;
|
|
86
|
+
|
|
87
|
+
type UnionToIntersection<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
88
|
+
type LastOf<U> = UnionToIntersection<U extends unknown ? () => U : never> extends () => infer R ? R : never;
|
|
89
|
+
|
|
90
|
+
/** `true` if `other()` is permitted for the key type: a pair of keys, or keys that the compiler does not know. */
|
|
91
|
+
export type AllowsOther<K extends string> = string extends K
|
|
92
|
+
? true
|
|
93
|
+
: `${number}` extends K
|
|
94
|
+
? true
|
|
95
|
+
: [Exclude<K, LastOf<K>>] extends [never]
|
|
96
|
+
? false
|
|
97
|
+
: [Exclude<Exclude<K, LastOf<K>>, LastOf<Exclude<K, LastOf<K>>>>] extends [never]
|
|
98
|
+
? true
|
|
99
|
+
: false;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The inputs for an argument with the parameter type `P`. The input can be a literal of type `P`, but not a bare
|
|
103
|
+
* string. It can be a field of the record with type `P`, an argument token, or a chain that gives `P`. If the op
|
|
104
|
+
* lifts scalars, it can also be a number.
|
|
105
|
+
*/
|
|
106
|
+
export type ArgInput<P, Ctx extends ChainContext, Lift extends boolean> =
|
|
107
|
+
| (P extends string ? never : P)
|
|
108
|
+
| KeysOfType<Ctx["record"], P>
|
|
109
|
+
| Arg<P>
|
|
110
|
+
| ChainOf<Ctx, P>
|
|
111
|
+
| (Lift extends true ? ([P] extends [DomainValue<DomainFor<Ctx["domains"], P>>] ? number : never) : never);
|
|
112
|
+
|
|
113
|
+
type MapArgs<P extends readonly unknown[], Ctx extends ChainContext, Lift extends boolean> = { [I in keyof P]: ArgInput<P[I], Ctx, Lift> };
|
|
114
|
+
|
|
115
|
+
/** A table of free functions. The first parameter of a function gets the current value of the chain. */
|
|
116
|
+
export type FnTable = Readonly<Record<string, (...args: never[]) => unknown>>;
|
|
117
|
+
|
|
118
|
+
/** The free functions of a builder without `withOptions`: none, so `call` accepts no name. */
|
|
119
|
+
// oxlint-disable-next-line typescript/no-generated-empty-object-type -- an empty table is the intent.
|
|
120
|
+
export type NoFns = Record<never, never>;
|
|
121
|
+
|
|
122
|
+
/** The static context of a chain: its domains, its key type, its record type and its free functions. */
|
|
123
|
+
export interface ChainContext {
|
|
124
|
+
readonly domains: readonly AnyDomain[];
|
|
125
|
+
readonly keys: string;
|
|
126
|
+
readonly record: unknown;
|
|
127
|
+
readonly fns?: FnTable;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
type FnsOf<Ctx extends ChainContext> = NonNullable<Ctx["fns"]>;
|
|
131
|
+
type FnParams<F> = F extends (...args: infer P) => unknown ? P : never;
|
|
132
|
+
type FirstParam<P> = P extends readonly [infer A, ...unknown[]] ? A : unknown;
|
|
133
|
+
type RestParams<P> = P extends readonly [unknown, ...infer R] ? R : [];
|
|
134
|
+
|
|
135
|
+
/** The names of the free functions whose first parameter accepts a value of type `T`. */
|
|
136
|
+
export type CallableFns<Ctx extends ChainContext, T> = {
|
|
137
|
+
[K in keyof FnsOf<Ctx>]: [T] extends [FirstParam<FnParams<FnsOf<Ctx>[K]>>] ? K : never;
|
|
138
|
+
}[keyof FnsOf<Ctx>] &
|
|
139
|
+
string;
|
|
140
|
+
|
|
141
|
+
/** The inputs for the parameters of the free function `N` after its first parameter. */
|
|
142
|
+
export type CallArgs<Ctx extends ChainContext, N extends keyof FnsOf<Ctx>> = MapArgs<RestParams<FnParams<FnsOf<Ctx>[N]>>, Ctx, false>;
|
|
143
|
+
|
|
144
|
+
/** The return type of the free function `N`. */
|
|
145
|
+
export type CallReturn<Ctx extends ChainContext, N extends keyof FnsOf<Ctx>> = FnsOf<Ctx>[N] extends (...args: never[]) => infer R ? R : unknown;
|
|
146
|
+
|
|
147
|
+
/** The ops section `._` of a chain whose value has domain `Dm`. */
|
|
148
|
+
export type OpsProxy<Ctx extends ChainContext, Dm> = {
|
|
149
|
+
readonly [M in OpNames<Dm>]: (...args: MapArgs<OpParams<Dm, M>, Ctx, Lifts<Dm, M>>) => ChainOf<Ctx, OpReturn<Dm, M>>;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// ---------------------------------------------------------------- chain types
|
|
153
|
+
|
|
154
|
+
/** The members of every chain. */
|
|
155
|
+
export interface ChainBase<Ctx extends ChainContext, T> {
|
|
156
|
+
readonly kind: "vex.chain";
|
|
157
|
+
/** This phantom member carries the value type. It is not present at run time. */
|
|
158
|
+
readonly [CHAIN_VALUE]?: T;
|
|
159
|
+
/** The expression of the chain, as plain data. */
|
|
160
|
+
readonly program: Expr;
|
|
161
|
+
/** This method evaluates the chain at a key. An error gives `none`. */
|
|
162
|
+
at(k: Ctx["keys"]): Optional<T>;
|
|
163
|
+
/** This method is the same as `at`. The v0.9 spec calls it `value`. */
|
|
164
|
+
value(k: Ctx["keys"]): Optional<T>;
|
|
165
|
+
/** This method evaluates the chain at a key, and gives the error if there is one. */
|
|
166
|
+
result(k: Ctx["keys"]): Result<T>;
|
|
167
|
+
/** This method evaluates the chain at a key, with a trace of each node. */
|
|
168
|
+
explain(k: Ctx["keys"]): Trace;
|
|
169
|
+
/** This method evaluates the chain at each key of the space (the start axis). */
|
|
170
|
+
all(): Traversal<Ctx["keys"], T>;
|
|
171
|
+
/** This method gives a chain whose value is a field of the record at the current address. */
|
|
172
|
+
from<P extends keyof Ctx["record"] & string>(field: P): ChainOf<Ctx, NonNullable<Ctx["record"][P]>>;
|
|
173
|
+
/** Navigation: later field references read at the key `k`. */
|
|
174
|
+
to(k: Ctx["keys"]): ChainOf<Ctx, T>;
|
|
175
|
+
/** Navigation: later field references read at the key with this position in the key order. */
|
|
176
|
+
index(i: number): ChainOf<Ctx, T>;
|
|
177
|
+
/** Navigation: later field references read at a relative position in an array or a grid. */
|
|
178
|
+
offset(...d: readonly number[]): ChainOf<Ctx, T>;
|
|
179
|
+
/** Navigation: later field references read at the origin again. */
|
|
180
|
+
origin(): ChainOf<Ctx, T>;
|
|
181
|
+
/** Navigation: later field references read at the parent of the focus, in a tree space. A root gives `#REF!`. */
|
|
182
|
+
parent(): ChainOf<Ctx, T>;
|
|
183
|
+
/** This method uses `fallback` when the chain gives an error. */
|
|
184
|
+
ifError<U>(fallback: ArgInput<U, Ctx, false> | T): ChainOf<Ctx, T | U>;
|
|
185
|
+
/**
|
|
186
|
+
* This method applies the free function `name` of `withOptions`. The function gets the current value as its first
|
|
187
|
+
* argument, then `args`. The types accept only the functions whose first parameter accepts the current value.
|
|
188
|
+
*/
|
|
189
|
+
call<N extends CallableFns<Ctx, T>>(name: N, ...args: CallArgs<Ctx, N>): ChainOf<Ctx, CallReturn<Ctx, N>>;
|
|
190
|
+
/** This method evaluates each branch with the current value, and gives a record of the results. */
|
|
191
|
+
fork<const B extends Readonly<Record<string, (c: ChainOf<Ctx, T>) => ChainBase<Ctx, unknown>>>>(branches: B): ChainOf<Ctx, { readonly [N in keyof B]: ValueOf<ReturnType<B[N]>> }>;
|
|
192
|
+
/** This method binds names for the body. The body gets a variable token for each name. */
|
|
193
|
+
with<const B extends Readonly<Record<string, unknown>>, U>(
|
|
194
|
+
binds: B,
|
|
195
|
+
body: (c: ChainOf<Ctx, T>, vars: { readonly [N in keyof B]: Arg<ArgValue<Ctx, B[N]>> }) => ChainBase<Ctx, U>,
|
|
196
|
+
): ChainOf<Ctx, U>;
|
|
197
|
+
/** The others axis: the body evaluates at each key except the focus. Its start value is the current value. */
|
|
198
|
+
others<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
199
|
+
/** The neighbors axis of a grid. */
|
|
200
|
+
neighbors<U>(n: 4 | 8, body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
201
|
+
/** The all axis inside the expression: the body evaluates at each key, with the current value as start value. */
|
|
202
|
+
each<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
203
|
+
/** The children axis of a tree space: the body evaluates at each child of the focus. */
|
|
204
|
+
children<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
205
|
+
/** The ancestors axis of a tree space: the parent first, the root last. */
|
|
206
|
+
ancestors<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
207
|
+
/** The descendants axis of a tree space: depth first, each parent before its children. */
|
|
208
|
+
descendants<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
209
|
+
/** The siblings axis of a tree space: the other children of the parent, or the other roots for a root. */
|
|
210
|
+
siblings<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** The options of an axis. */
|
|
214
|
+
export interface AxisOpts<Ctx extends ChainContext, T> {
|
|
215
|
+
/** A test at each target. The body evaluates only at the targets where the test gives `true`. */
|
|
216
|
+
readonly where?: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, boolean>;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The members of a chain whose value has a domain. */
|
|
220
|
+
export interface DomainMembers<Ctx extends ChainContext, Dm> {
|
|
221
|
+
/** The ops of the domain of the current value. */
|
|
222
|
+
readonly _: OpsProxy<Ctx, Dm>;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** The members of a chain over a space whose keys permit `other()`. */
|
|
226
|
+
export interface OtherMembers<Ctx extends ChainContext, T> {
|
|
227
|
+
/** Navigation: later field references read at the other key of the pair. */
|
|
228
|
+
other(): ChainOf<Ctx, T>;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** A chain with the current value type `T`. */
|
|
232
|
+
export type ChainOf<Ctx extends ChainContext, T> = ChainBase<Ctx, T> &
|
|
233
|
+
([DomainFor<Ctx["domains"], T>] extends [never] ? unknown : DomainMembers<Ctx, DomainFor<Ctx["domains"], T>>) &
|
|
234
|
+
(AllowsOther<Ctx["keys"]> extends true ? OtherMembers<Ctx, T> : unknown);
|
|
235
|
+
|
|
236
|
+
/** The value type of a chain. */
|
|
237
|
+
export type ValueOf<C> = C extends { readonly [CHAIN_VALUE]?: infer T } ? T : never;
|
|
238
|
+
|
|
239
|
+
/** The value type of an argument input. */
|
|
240
|
+
export type ArgValue<Ctx extends ChainContext, A> = A extends Arg<infer T>
|
|
241
|
+
? T
|
|
242
|
+
: A extends { readonly kind: "vex.chain" }
|
|
243
|
+
? ValueOf<A>
|
|
244
|
+
: A extends keyof Ctx["record"] & string
|
|
245
|
+
? NonNullable<Ctx["record"][A]>
|
|
246
|
+
: A;
|
|
247
|
+
|
|
248
|
+
/** A chain whose value is a list: the result of an axis. */
|
|
249
|
+
export interface ListChain<Ctx extends ChainContext, U> {
|
|
250
|
+
readonly kind: "vex.list-chain";
|
|
251
|
+
/** The expression of the chain, as plain data. */
|
|
252
|
+
readonly program: Expr;
|
|
253
|
+
count(opts?: ListOpts): ChainOf<Ctx, number>;
|
|
254
|
+
sum(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
255
|
+
mean(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
256
|
+
min(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
257
|
+
max(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
258
|
+
any(this: ListChain<Ctx, boolean>, opts?: ListOpts): ChainOf<Ctx, boolean>;
|
|
259
|
+
all(this: ListChain<Ctx, boolean>, opts?: ListOpts): ChainOf<Ctx, boolean>;
|
|
260
|
+
none(this: ListChain<Ctx, boolean>, opts?: ListOpts): ChainOf<Ctx, boolean>;
|
|
261
|
+
values(opts?: ListOpts): ChainOf<Ctx, readonly U[]>;
|
|
262
|
+
first(opts?: ListOpts): ChainOf<Ctx, U>;
|
|
263
|
+
reduce(op: OpNames<DomainFor<Ctx["domains"], U>>, opts?: ListOpts): ChainOf<Ctx, U>;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** The options of a list reduction. */
|
|
267
|
+
export interface ListOpts {
|
|
268
|
+
/** With `true`, an error item makes the result that error. The default skips error items. */
|
|
269
|
+
readonly strict?: boolean;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** The root of the builder for one space. */
|
|
273
|
+
export interface Root<Ctx extends ChainContext> {
|
|
274
|
+
/** This method starts a chain with a field of the record at the focus. */
|
|
275
|
+
from<P extends keyof Ctx["record"] & string>(field: P): ChainOf<Ctx, NonNullable<Ctx["record"][P]>>;
|
|
276
|
+
/** This method starts a chain with a value. A string is a field reference, as in an argument. */
|
|
277
|
+
start<const T>(value: T): ChainOf<Ctx, ArgValue<Ctx, T>>;
|
|
278
|
+
/** This method makes an absolute reference: the field of the record at key `k`. */
|
|
279
|
+
of<P extends keyof Ctx["record"] & string>(k: Ctx["keys"], field: P): Arg<NonNullable<Ctx["record"][P]>>;
|
|
280
|
+
/** This method makes a reference to a dotted path at the focus, with the type that the caller gives. */
|
|
281
|
+
field<T>(path: string): Arg<T>;
|
|
282
|
+
/** This method makes a literal argument. Use it for a string or an array: a bare string is a field reference. */
|
|
283
|
+
lit<const T>(value: T): Arg<T>;
|
|
284
|
+
/** This method makes an absolute reference to a dotted path at key `k`, with the type that the caller gives. */
|
|
285
|
+
ofPath<T>(k: Ctx["keys"], path: string): Arg<T>;
|
|
286
|
+
/** This method makes a record argument from field arguments. */
|
|
287
|
+
rec<const F extends Readonly<Record<string, unknown>>>(fields: F): Arg<{ readonly [N in keyof F]: ArgValue<Ctx, F[N]> }>;
|
|
288
|
+
/**
|
|
289
|
+
* This method makes an extension argument: the node `ext(kind, data)`. The handler of `kind` in the options of
|
|
290
|
+
* `withOptions` gives its value. The caller gives the type `T` of that value.
|
|
291
|
+
*/
|
|
292
|
+
ext<T>(kind: string, data?: unknown): Arg<T>;
|
|
293
|
+
/** This method starts a sheet over the space: named columns of formulas. Refer to `Sheet`. */
|
|
294
|
+
sheet(): Sheet<Ctx, object>;
|
|
295
|
+
/** The space of the root. */
|
|
296
|
+
readonly space: Space<Ctx["keys"], Ctx["record"]>;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** The root of a column formula: the root of the builder, and references to the cells of the sheet. */
|
|
300
|
+
export interface SheetRoot<Ctx extends ChainContext, Cols> extends Root<Ctx> {
|
|
301
|
+
/**
|
|
302
|
+
* This method makes a cell reference: the column `column` at the key `at`, or at the address `at` from the focus.
|
|
303
|
+
* Without `at`, it reads at the focus. The type is the type of a column before this one.
|
|
304
|
+
*/
|
|
305
|
+
cell<N extends keyof Cols & string>(column: N, at?: Ctx["keys"] | Addr): Arg<Cols[N]>;
|
|
306
|
+
/** This method makes a cell reference to any column, also this column or a later one. The caller gives the type. */
|
|
307
|
+
cell<T>(column: string, at?: Ctx["keys"] | Addr): Arg<T>;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** One row of a sheet: the key and the result of each column. */
|
|
311
|
+
export interface SheetRow<K extends string, Cols> {
|
|
312
|
+
readonly key: K;
|
|
313
|
+
readonly cells: { readonly [N in keyof Cols]: Result<Cols[N]> };
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* A sheet: named columns of formulas over a space, like the computed columns of a spreadsheet. A formula reads the
|
|
318
|
+
* other columns with `cell`. Each evaluation evaluates a cell once, and each cell on a cycle of references gives
|
|
319
|
+
* `#CYCLE!`. A sheet is immutable: `column` gives a new sheet.
|
|
320
|
+
*/
|
|
321
|
+
export interface Sheet<Ctx extends ChainContext, Cols, Decl = object> {
|
|
322
|
+
/**
|
|
323
|
+
* This method gives a new sheet with one more column. The formula gets a root with `cell`. If `declare` gave the
|
|
324
|
+
* column a type, the formula must give a value of that type.
|
|
325
|
+
*/
|
|
326
|
+
column<const N extends string, C extends ChainBase<Ctx, DeclaredType<Decl, N>>>(
|
|
327
|
+
name: N,
|
|
328
|
+
formula: (r: SheetRoot<Ctx, Cols & Decl>) => C,
|
|
329
|
+
): Sheet<Ctx, Cols & { readonly [P in N]: ValueOf<C> }, Decl>;
|
|
330
|
+
/**
|
|
331
|
+
* This method gives the types of columns before their formulas. A formula can then read a declared column with
|
|
332
|
+
* `cell` and its type: the column itself (a recurrence) or a later column. It has no effect at run time.
|
|
333
|
+
*/
|
|
334
|
+
declare<D extends Readonly<Record<string, unknown>>>(): Sheet<Ctx, Cols, Decl & D>;
|
|
335
|
+
/** The program of each column. */
|
|
336
|
+
readonly columns: Readonly<Record<string, Expr>>;
|
|
337
|
+
/** This method evaluates one cell, and gives the error if there is one. */
|
|
338
|
+
result<N extends keyof Cols & string>(k: Ctx["keys"], column: N): Result<Cols[N]>;
|
|
339
|
+
/** This method evaluates one cell. An error gives `none`. */
|
|
340
|
+
at<N extends keyof Cols & string>(k: Ctx["keys"], column: N): Optional<Cols[N]>;
|
|
341
|
+
/** This method evaluates one cell, with a trace of the nodes of its column. */
|
|
342
|
+
explain(k: Ctx["keys"], column: keyof Cols & string): Trace;
|
|
343
|
+
/** This method evaluates each cell, in key order, with one result for each cell. */
|
|
344
|
+
table(): readonly SheetRow<Ctx["keys"], Cols>[];
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** The declared type of the column `N`, or `unknown` for a column without a declaration. */
|
|
348
|
+
export type DeclaredType<Decl, N extends string> = N extends keyof Decl ? Decl[N] : unknown;
|
|
349
|
+
|
|
350
|
+
/** The options of the builder. */
|
|
351
|
+
export interface VexOptions<F extends FnTable = FnTable> {
|
|
352
|
+
/** Free function ops. A chain applies them with `call`. */
|
|
353
|
+
readonly fns?: F;
|
|
354
|
+
/** Handlers of extension expression kinds. */
|
|
355
|
+
readonly extensions?: Readonly<Record<string, ExtHandler>>;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** The entry point for a list of domains. */
|
|
359
|
+
export interface VexEntry<Dms extends readonly AnyDomain[], F extends FnTable = NoFns> {
|
|
360
|
+
/** This method gives the root of the builder for a space. */
|
|
361
|
+
over<K extends string, O>(s: Space<K, O>): Root<{ readonly domains: Dms; readonly keys: K; readonly record: Merge<O>; readonly fns: F }>;
|
|
362
|
+
/** The domains. */
|
|
363
|
+
readonly domains: Dms;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// ---------------------------------------------------------------- run time
|
|
367
|
+
|
|
368
|
+
/** The options as the interpreter takes them: free functions get untyped arguments at run time. */
|
|
369
|
+
interface RuntimeOptions {
|
|
370
|
+
readonly fns?: Readonly<Record<string, FreeFn>>;
|
|
371
|
+
readonly extensions?: Readonly<Record<string, ExtHandler>>;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** This function gives the run-time form of the options. A typed free function gets the values that the types promise. */
|
|
375
|
+
const runtimeOptions = (options: VexOptions): RuntimeOptions => ({
|
|
376
|
+
// Stryker disable next-line ConditionalExpression: an undefined option and an absent option are the same
|
|
377
|
+
...(options.fns === undefined ? {} : { fns: options.fns as Readonly<Record<string, FreeFn>> }),
|
|
378
|
+
// Stryker disable next-line ConditionalExpression: an undefined option and an absent option are the same
|
|
379
|
+
...(options.extensions === undefined ? {} : { extensions: options.extensions }),
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
interface Env {
|
|
383
|
+
readonly space: Space;
|
|
384
|
+
readonly domains: readonly AnyDomain[];
|
|
385
|
+
readonly options: RuntimeOptions;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
interface State {
|
|
389
|
+
readonly env: Env;
|
|
390
|
+
/** The current value. */
|
|
391
|
+
readonly expr: Expr;
|
|
392
|
+
/** The address where later field references read. */
|
|
393
|
+
readonly addr: readonly Move[];
|
|
394
|
+
/** The depth of nested axis and binding bodies, for unique variable names. */
|
|
395
|
+
readonly depth: number;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const kindOf = (u: unknown): unknown => (u as { kind?: unknown } | null | undefined)?.kind;
|
|
399
|
+
const isArg = (u: unknown): u is Arg<unknown> => kindOf(u) === "vex.arg";
|
|
400
|
+
const isChainLike = (u: unknown): u is { readonly program: Expr } => kindOf(u) === "vex.chain" || kindOf(u) === "vex.list-chain";
|
|
401
|
+
|
|
402
|
+
/** This function turns a builder argument into an expression. A bare string is a field reference at `addr`. */
|
|
403
|
+
function toExpr(a: unknown, addr: readonly Move[]): Expr {
|
|
404
|
+
if (typeof a === "string") return ref(toPath(a), addr);
|
|
405
|
+
if (isArg(a)) return a.expr;
|
|
406
|
+
if (isChainLike(a)) return a.program;
|
|
407
|
+
return lit(a);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const PROXY_RESERVED = new Set(["then", "catch", "finally", "toJSON", "valueOf", "toString", "constructor", "inspect", "asymmetricMatch", "$$typeof", "nodeType"]);
|
|
411
|
+
|
|
412
|
+
class ChainImpl {
|
|
413
|
+
readonly kind = "vex.chain" as const;
|
|
414
|
+
readonly #s: State;
|
|
415
|
+
|
|
416
|
+
constructor(s: State) {
|
|
417
|
+
this.#s = s;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
get program(): Expr {
|
|
421
|
+
return this.#s.expr;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
get _(): Readonly<Record<string, (...args: readonly unknown[]) => ChainImpl>> {
|
|
425
|
+
const s = this.#s;
|
|
426
|
+
return new Proxy(Object.create(null) as Record<string, never>, {
|
|
427
|
+
get: (_t, prop): unknown => {
|
|
428
|
+
if (typeof prop !== "string" || PROXY_RESERVED.has(prop)) return undefined;
|
|
429
|
+
return (...args: readonly unknown[]): ChainImpl => new ChainImpl({ ...s, expr: app(prop, s.expr, ...args.map((a) => toExpr(a, s.addr))) });
|
|
430
|
+
},
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
#opts(k: string): EvalOptions {
|
|
435
|
+
const { env } = this.#s;
|
|
436
|
+
return {
|
|
437
|
+
space: env.space,
|
|
438
|
+
origin: k,
|
|
439
|
+
domains: env.domains,
|
|
440
|
+
// Stryker disable next-line ConditionalExpression: an undefined option and an absent option are the same
|
|
441
|
+
...(env.options.fns === undefined ? {} : { fns: env.options.fns }),
|
|
442
|
+
// Stryker disable next-line ConditionalExpression: an undefined option and an absent option are the same
|
|
443
|
+
...(env.options.extensions === undefined ? {} : { extensions: env.options.extensions }),
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
result(k: string): Result<unknown> {
|
|
448
|
+
return evaluate(this.#s.expr, this.#opts(k));
|
|
449
|
+
}
|
|
450
|
+
at(k: string): Optional<unknown> {
|
|
451
|
+
return toOptional(this.result(k));
|
|
452
|
+
}
|
|
453
|
+
value(k: string): Optional<unknown> {
|
|
454
|
+
return this.at(k);
|
|
455
|
+
}
|
|
456
|
+
explain(k: string): Trace {
|
|
457
|
+
return explain(this.#s.expr, this.#opts(k));
|
|
458
|
+
}
|
|
459
|
+
all(): Traversal<string, unknown> {
|
|
460
|
+
const { env } = this.#s;
|
|
461
|
+
const items = env.space.keys.map((k) => ({ key: k, result: this.result(k) }));
|
|
462
|
+
return traversal(items, env);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
from(field: string): ChainImpl {
|
|
466
|
+
return new ChainImpl({ ...this.#s, expr: ref(toPath(field), this.#s.addr) });
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
#move(m: Move): ChainImpl {
|
|
470
|
+
return new ChainImpl({ ...this.#s, addr: [...this.#s.addr, m] });
|
|
471
|
+
}
|
|
472
|
+
to(k: string): ChainImpl {
|
|
473
|
+
return this.#move(keyMove(k));
|
|
474
|
+
}
|
|
475
|
+
index(i: number): ChainImpl {
|
|
476
|
+
return this.#move(indexMove(i));
|
|
477
|
+
}
|
|
478
|
+
offset(...d: readonly number[]): ChainImpl {
|
|
479
|
+
return this.#move(offsetMove(...d));
|
|
480
|
+
}
|
|
481
|
+
other(): ChainImpl {
|
|
482
|
+
return this.#move(otherMove);
|
|
483
|
+
}
|
|
484
|
+
origin(): ChainImpl {
|
|
485
|
+
return this.#move(originMove);
|
|
486
|
+
}
|
|
487
|
+
parent(): ChainImpl {
|
|
488
|
+
return this.#move(parentMove);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
call(name: string, ...args: readonly unknown[]): ChainImpl {
|
|
492
|
+
return new ChainImpl({ ...this.#s, expr: app(name, this.#s.expr, ...args.map((a) => toExpr(a, this.#s.addr))) });
|
|
493
|
+
}
|
|
494
|
+
ifError(fallback: unknown): ChainImpl {
|
|
495
|
+
return new ChainImpl({ ...this.#s, expr: app("ifError", this.#s.expr, toExpr(fallback, this.#s.addr)) });
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** This method binds the current value to a fresh name, and gives a chain that starts from that name. */
|
|
499
|
+
#bound(): { readonly name: string; readonly start: ChainImpl } {
|
|
500
|
+
const name = `$${this.#s.depth}`;
|
|
501
|
+
return { name, start: new ChainImpl({ ...this.#s, expr: v(name), depth: this.#s.depth + 1 }) };
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
fork(branches: Readonly<Record<string, (c: ChainImpl) => { readonly program: Expr }>>): ChainImpl {
|
|
505
|
+
const { name, start } = this.#bound();
|
|
506
|
+
const fields: Record<string, Expr> = {};
|
|
507
|
+
for (const [n, f] of Object.entries(branches)) fields[n] = f(start).program;
|
|
508
|
+
return new ChainImpl({ ...this.#s, expr: let_({ [name]: this.#s.expr }, rec(fields)) });
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
with(binds: Readonly<Record<string, unknown>>, body: (c: ChainImpl, vars: Readonly<Record<string, Arg<unknown>>>) => { readonly program: Expr }): ChainImpl {
|
|
512
|
+
const depth = this.#s.depth;
|
|
513
|
+
const bindExprs: Record<string, Expr> = {};
|
|
514
|
+
const vars: Record<string, Arg<unknown>> = {};
|
|
515
|
+
for (const [n, a] of Object.entries(binds)) {
|
|
516
|
+
const unique = `${n}$${depth}`;
|
|
517
|
+
bindExprs[unique] = toExpr(a, this.#s.addr);
|
|
518
|
+
vars[n] = argToken(v(unique));
|
|
519
|
+
}
|
|
520
|
+
const inner = body(new ChainImpl({ ...this.#s, depth: depth + 1 }), vars);
|
|
521
|
+
return new ChainImpl({ ...this.#s, expr: let_(bindExprs, inner.program) });
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
#axis(a: Axis, body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
525
|
+
const { name, start } = this.#bound();
|
|
526
|
+
// Inside the axis, the focus is the target, so the address of the body starts empty.
|
|
527
|
+
const inTarget = new ChainImpl({ ...start.#s, addr: [] });
|
|
528
|
+
const inner = body(inTarget);
|
|
529
|
+
const axis: Axis = opts?.where === undefined ? a : { t: "where", axis: a, test: opts.where(inTarget).program };
|
|
530
|
+
return new ListChainImpl({ ...this.#s, expr: let_({ [name]: this.#s.expr }, each(axis, inner.program)) });
|
|
531
|
+
}
|
|
532
|
+
others(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
533
|
+
return this.#axis({ t: "others" }, body, opts);
|
|
534
|
+
}
|
|
535
|
+
neighbors(n: 4 | 8, body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
536
|
+
return this.#axis({ t: "neighbors", n }, body, opts);
|
|
537
|
+
}
|
|
538
|
+
each(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
539
|
+
return this.#axis({ t: "all" }, body, opts);
|
|
540
|
+
}
|
|
541
|
+
children(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
542
|
+
return this.#axis({ t: "children" }, body, opts);
|
|
543
|
+
}
|
|
544
|
+
ancestors(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
545
|
+
return this.#axis({ t: "ancestors" }, body, opts);
|
|
546
|
+
}
|
|
547
|
+
descendants(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
548
|
+
return this.#axis({ t: "descendants" }, body, opts);
|
|
549
|
+
}
|
|
550
|
+
siblings(body: (c: ChainImpl) => { readonly program: Expr }, opts?: { readonly where?: (c: ChainImpl) => { readonly program: Expr } }): ListChainImpl {
|
|
551
|
+
return this.#axis({ t: "siblings" }, body, opts);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
class ListChainImpl {
|
|
556
|
+
readonly kind = "vex.list-chain" as const;
|
|
557
|
+
readonly #s: State;
|
|
558
|
+
constructor(s: State) {
|
|
559
|
+
this.#s = s;
|
|
560
|
+
}
|
|
561
|
+
get program(): Expr {
|
|
562
|
+
return this.#s.expr;
|
|
563
|
+
}
|
|
564
|
+
#op(op: string, extra: readonly Expr[], opts?: ListOpts): ChainImpl {
|
|
565
|
+
const args = opts?.strict === true ? [...extra, lit({ strict: true })] : extra;
|
|
566
|
+
return new ChainImpl({ ...this.#s, expr: app(op, this.#s.expr, ...args) });
|
|
567
|
+
}
|
|
568
|
+
count(opts?: ListOpts): ChainImpl {
|
|
569
|
+
return this.#op("count", [], opts);
|
|
570
|
+
}
|
|
571
|
+
sum(opts?: ListOpts): ChainImpl {
|
|
572
|
+
return this.#op("sum", [], opts);
|
|
573
|
+
}
|
|
574
|
+
mean(opts?: ListOpts): ChainImpl {
|
|
575
|
+
return this.#op("mean", [], opts);
|
|
576
|
+
}
|
|
577
|
+
min(opts?: ListOpts): ChainImpl {
|
|
578
|
+
return this.#op("min", [], opts);
|
|
579
|
+
}
|
|
580
|
+
max(opts?: ListOpts): ChainImpl {
|
|
581
|
+
return this.#op("max", [], opts);
|
|
582
|
+
}
|
|
583
|
+
any(opts?: ListOpts): ChainImpl {
|
|
584
|
+
return this.#op("any", [], opts);
|
|
585
|
+
}
|
|
586
|
+
all(opts?: ListOpts): ChainImpl {
|
|
587
|
+
return this.#op("all", [], opts);
|
|
588
|
+
}
|
|
589
|
+
none(opts?: ListOpts): ChainImpl {
|
|
590
|
+
return this.#op("none", [], opts);
|
|
591
|
+
}
|
|
592
|
+
values(opts?: ListOpts): ChainImpl {
|
|
593
|
+
return this.#op("values", [], opts);
|
|
594
|
+
}
|
|
595
|
+
first(opts?: ListOpts): ChainImpl {
|
|
596
|
+
return this.#op("first", [], opts);
|
|
597
|
+
}
|
|
598
|
+
reduce(op: string, opts?: ListOpts): ChainImpl {
|
|
599
|
+
return this.#op("reduce", [lit(op)], opts);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
class RootImpl {
|
|
604
|
+
readonly #env: Env;
|
|
605
|
+
constructor(env: Env) {
|
|
606
|
+
this.#env = env;
|
|
607
|
+
}
|
|
608
|
+
get space(): Space {
|
|
609
|
+
return this.#env.space;
|
|
610
|
+
}
|
|
611
|
+
#chain(expr: Expr): ChainImpl {
|
|
612
|
+
return new ChainImpl({ env: this.#env, expr, addr: [], depth: 0 });
|
|
613
|
+
}
|
|
614
|
+
from(field: string): ChainImpl {
|
|
615
|
+
return this.#chain(ref(toPath(field)));
|
|
616
|
+
}
|
|
617
|
+
start(value: unknown): ChainImpl {
|
|
618
|
+
return this.#chain(toExpr(value, []));
|
|
619
|
+
}
|
|
620
|
+
of(k: string, field: string): Arg<unknown> {
|
|
621
|
+
return argToken(ref(toPath(field), [keyMove(k)]));
|
|
622
|
+
}
|
|
623
|
+
field(path: string): Arg<unknown> {
|
|
624
|
+
return argToken(ref(toPath(path)));
|
|
625
|
+
}
|
|
626
|
+
lit(value: unknown): Arg<unknown> {
|
|
627
|
+
return argToken(lit(value));
|
|
628
|
+
}
|
|
629
|
+
ofPath(k: string, path: string): Arg<unknown> {
|
|
630
|
+
return argToken(ref(toPath(path), [keyMove(k)]));
|
|
631
|
+
}
|
|
632
|
+
ext(kind: string, data: unknown = null): Arg<unknown> {
|
|
633
|
+
return argToken(ext(kind, data));
|
|
634
|
+
}
|
|
635
|
+
sheet(): SheetImpl {
|
|
636
|
+
return new SheetImpl(this.#env, new Map());
|
|
637
|
+
}
|
|
638
|
+
rec(fields: Readonly<Record<string, unknown>>): Arg<unknown> {
|
|
639
|
+
const out: Record<string, Expr> = {};
|
|
640
|
+
for (const [n, a] of Object.entries(fields)) out[n] = toExpr(a, []);
|
|
641
|
+
return argToken(rec(out));
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
class SheetRootImpl extends RootImpl {
|
|
646
|
+
cell(column: string, at?: string | Addr): Arg<unknown> {
|
|
647
|
+
return argToken(cellRef(column, typeof at === "string" ? [keyMove(at)] : at));
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
class SheetImpl {
|
|
652
|
+
readonly #env: Env;
|
|
653
|
+
readonly #columns: ReadonlyMap<string, Expr>;
|
|
654
|
+
constructor(env: Env, columns: ReadonlyMap<string, Expr>) {
|
|
655
|
+
this.#env = env;
|
|
656
|
+
this.#columns = columns;
|
|
657
|
+
}
|
|
658
|
+
get columns(): Readonly<Record<string, Expr>> {
|
|
659
|
+
return Object.freeze(Object.fromEntries(this.#columns));
|
|
660
|
+
}
|
|
661
|
+
declare(): SheetImpl {
|
|
662
|
+
return this;
|
|
663
|
+
}
|
|
664
|
+
column(name: string, formula: (r: SheetRootImpl) => { readonly program: Expr }): SheetImpl {
|
|
665
|
+
const program = formula(new SheetRootImpl(this.#env)).program;
|
|
666
|
+
return new SheetImpl(this.#env, new Map([...this.#columns, [name, program]]));
|
|
667
|
+
}
|
|
668
|
+
#run(): SheetRun {
|
|
669
|
+
const { space: s, domains, options } = this.#env;
|
|
670
|
+
return new SheetRun(this.#columns, {
|
|
671
|
+
space: s,
|
|
672
|
+
domains,
|
|
673
|
+
// Stryker disable next-line ConditionalExpression: an undefined option and an absent option are the same
|
|
674
|
+
...(options.fns === undefined ? {} : { fns: options.fns }),
|
|
675
|
+
// Stryker disable next-line ConditionalExpression: an undefined option and an absent option are the same
|
|
676
|
+
...(options.extensions === undefined ? {} : { extensions: options.extensions }),
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
result(k: string, column: string): Result<unknown> {
|
|
680
|
+
const run = this.#run();
|
|
681
|
+
run.warm(column, k);
|
|
682
|
+
return run.cell(column, k);
|
|
683
|
+
}
|
|
684
|
+
at(k: string, column: string): Optional<unknown> {
|
|
685
|
+
return toOptional(this.result(k, column));
|
|
686
|
+
}
|
|
687
|
+
explain(k: string, column: string): Trace {
|
|
688
|
+
const run = this.#run();
|
|
689
|
+
run.warm(column, k);
|
|
690
|
+
return run.explain(column, k);
|
|
691
|
+
}
|
|
692
|
+
table(): readonly SheetRow<string, Record<string, unknown>>[] {
|
|
693
|
+
const run = this.#run();
|
|
694
|
+
const names = [...this.#columns.keys()];
|
|
695
|
+
return this.#env.space.keys.map((key) => ({ key, cells: Object.fromEntries(names.map((c) => [c, run.cell(c, key)])) }));
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* This function gives the entry point of the builder for a list of domains. The interpreter finds the op of a
|
|
701
|
+
* value in the first domain that accepts the value.
|
|
702
|
+
*/
|
|
703
|
+
export function vex<const Dms extends readonly AnyDomain[]>(
|
|
704
|
+
...domains: Dms
|
|
705
|
+
): VexEntry<Dms> & { withOptions<const F extends FnTable = NoFns>(options: VexOptions<F>): VexEntry<Dms, F> } {
|
|
706
|
+
const make = <F extends FnTable>(options: VexOptions<F>): VexEntry<Dms, F> => ({
|
|
707
|
+
domains,
|
|
708
|
+
over: <K extends string, O>(s: Space<K, O>): Root<{ readonly domains: Dms; readonly keys: K; readonly record: Merge<O>; readonly fns: F }> =>
|
|
709
|
+
new RootImpl({ space: s, domains, options: runtimeOptions(options) }) as unknown as Root<{
|
|
710
|
+
readonly domains: Dms;
|
|
711
|
+
readonly keys: K;
|
|
712
|
+
readonly record: Merge<O>;
|
|
713
|
+
readonly fns: F;
|
|
714
|
+
}>,
|
|
715
|
+
});
|
|
716
|
+
return { ...make<NoFns>({}), withOptions: make };
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/** The type of a domain value, from the domain object. */
|
|
720
|
+
export type ValueOfDomain<Dm> = DomainValue<Dm>;
|
|
721
|
+
|
|
722
|
+
/** An op specification type, for domain authors. */
|
|
723
|
+
export type { OpSpec };
|