@mark1russell7/coeval 0.1.0 → 0.2.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 +41 -0
- package/build/index.d.ts +832 -148
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1898 -376
- package/build/index.js.map +1 -1
- package/package.json +2 -2
- package/src/context.ts +25 -21
- package/src/deref.ts +165 -54
- package/src/doc.ts +86 -17
- package/src/errors.ts +141 -0
- package/src/evaluate.ts +797 -281
- package/src/formula.ts +409 -0
- package/src/index.ts +72 -7
- package/src/parts.ts +140 -27
- package/src/static.ts +364 -0
- package/src/stdlib.ts +60 -33
- package/src/world.ts +96 -22
package/build/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//#region src/doc.d.ts
|
|
2
|
-
/** A JSON value. */
|
|
2
|
+
/** A JSON value. A document is JSON. */
|
|
3
3
|
type Json = null | boolean | number | string | readonly Json[] | {
|
|
4
4
|
readonly [key: string]: Json;
|
|
5
5
|
};
|
|
@@ -40,12 +40,13 @@ type Envelope = {
|
|
|
40
40
|
/** The keys that an envelope can have. */
|
|
41
41
|
export declare const ENVELOPE_KEYS: readonly ["type", "value", "name", "own"];
|
|
42
42
|
/**
|
|
43
|
-
* This function tells if a value is a
|
|
43
|
+
* This function tells if a value is a plain record: an object with the prototype of a plain object, or with no
|
|
44
|
+
* prototype. A list, `null`, a date, a map and an instance of a class are not plain records.
|
|
44
45
|
*
|
|
45
46
|
* @param value - The value to examine.
|
|
46
|
-
* @returns `true` for a record.
|
|
47
|
+
* @returns `true` for a plain record.
|
|
47
48
|
*/
|
|
48
|
-
export declare function isRecord(value:
|
|
49
|
+
export declare function isRecord(value: unknown): value is JsonRecord;
|
|
49
50
|
/**
|
|
50
51
|
* This function reads a field of a record. It reads only the own fields, thus `constructor` is not a field.
|
|
51
52
|
*
|
|
@@ -60,7 +61,7 @@ export declare function field(record: JsonRecord, key: string): Json | undefined
|
|
|
60
61
|
* @param value - The value to examine.
|
|
61
62
|
* @returns `true` for a key.
|
|
62
63
|
*/
|
|
63
|
-
export declare function isKey(value:
|
|
64
|
+
export declare function isKey(value: unknown): value is Key;
|
|
64
65
|
/**
|
|
65
66
|
* This function tells if a value is a reference to a type. The reference is a text, a list of keys, or a document
|
|
66
67
|
* of type `ref` whose path starts at the root `world`.
|
|
@@ -68,7 +69,7 @@ export declare function isKey(value: Json | undefined): value is Key;
|
|
|
68
69
|
* @param value - The value to examine.
|
|
69
70
|
* @returns `true` for a reference to a type.
|
|
70
71
|
*/
|
|
71
|
-
export declare function isTypeRef(value:
|
|
72
|
+
export declare function isTypeRef(value: unknown): value is TypeRef;
|
|
72
73
|
/**
|
|
73
74
|
* This function gives the path of a reference to a type, from the root of the world.
|
|
74
75
|
*
|
|
@@ -91,7 +92,22 @@ export declare function typeName(type: TypeRef): string;
|
|
|
91
92
|
* @param value - The value to examine.
|
|
92
93
|
* @returns `true` when the value has the form of an envelope.
|
|
93
94
|
*/
|
|
94
|
-
export declare function isEnvelope(value:
|
|
95
|
+
export declare function isEnvelope(value: unknown): value is JsonRecord & Envelope;
|
|
96
|
+
/** A stored document: an envelope, and the keys of its store beside it, for example `_id`. */
|
|
97
|
+
interface Stored {
|
|
98
|
+
readonly envelope: Envelope;
|
|
99
|
+
/** The keys of the store, with their values. */
|
|
100
|
+
readonly header: Readonly<Record<string, Json>>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* This function reads a stored document: an envelope that also has the keys of a store, for example `{ _id, type,
|
|
104
|
+
* value }`. Only the keys in the list can be beside the keys of the envelope.
|
|
105
|
+
*
|
|
106
|
+
* @param value - The value to examine.
|
|
107
|
+
* @param header - The keys of the store that the document can have.
|
|
108
|
+
* @returns The envelope and the keys of the store, or `undefined` when the value has another form.
|
|
109
|
+
*/
|
|
110
|
+
export declare function stored(value: unknown, header: readonly string[]): Stored | undefined;
|
|
95
111
|
/**
|
|
96
112
|
* This function makes an envelope.
|
|
97
113
|
*
|
|
@@ -124,43 +140,206 @@ export declare function toJson(doc: Envelope): JsonRecord;
|
|
|
124
140
|
* @param value - The JSON value.
|
|
125
141
|
* @returns The envelope, or `undefined`.
|
|
126
142
|
*/
|
|
127
|
-
export declare function fromJson(value:
|
|
143
|
+
export declare function fromJson(value: unknown): Envelope | undefined;
|
|
144
|
+
/**
|
|
145
|
+
* This function tells if two JSON values are equal. Equal values have the same texts, numbers and truth values,
|
|
146
|
+
* the same items in the same order, and the same fields. The order of the fields does not matter. It does not use the stack for the
|
|
147
|
+
* depth, thus a deep document cannot overflow it.
|
|
148
|
+
*
|
|
149
|
+
* @param a - The first value.
|
|
150
|
+
* @param b - The second value.
|
|
151
|
+
* @returns `true` when the values are equal.
|
|
152
|
+
*/
|
|
153
|
+
export declare function docEquals(a: unknown, b: unknown): boolean;
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/errors.d.ts
|
|
156
|
+
/** The codes that the kernel gives. */
|
|
157
|
+
type KernelCode = "#KIND?" | "#NOEVAL" | "#PART?" | "#NAME?" | "#REF!" | "#N/A" | "#CYCLE!" | "#STEP?" | "#VALUE!" | "#NUM!" | "#ARGS" | "#CAP!" | "#CALC!" | "#DEPTH!" | "#ABORT";
|
|
158
|
+
/** The code of an error value. A language can add its own codes. Each code starts with `#`. */
|
|
159
|
+
type ErrorCode = KernelCode | `#${string}`;
|
|
160
|
+
/**
|
|
161
|
+
* An error value: a code and a reason. An evaluation gives an error value, and it does not throw.
|
|
162
|
+
*
|
|
163
|
+
* - `place`: the keys from the root document to the document that failed.
|
|
164
|
+
* - `cause`: the error of the host that caused this error, for example the exception of an effect.
|
|
165
|
+
*/
|
|
166
|
+
interface EvalError {
|
|
167
|
+
readonly code: ErrorCode;
|
|
168
|
+
readonly reason: string;
|
|
169
|
+
readonly place?: readonly Key[];
|
|
170
|
+
readonly cause?: unknown;
|
|
171
|
+
}
|
|
172
|
+
/** The result of an evaluation: a value, or an error value. A value can be any value of the host. */
|
|
173
|
+
type Result<V = unknown> = {
|
|
174
|
+
readonly ok: true;
|
|
175
|
+
readonly value: V;
|
|
176
|
+
} | {
|
|
177
|
+
readonly ok: false;
|
|
178
|
+
readonly error: EvalError;
|
|
179
|
+
};
|
|
180
|
+
/** A result with an error value. */
|
|
181
|
+
type Failed = {
|
|
182
|
+
readonly ok: false;
|
|
183
|
+
readonly error: EvalError;
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* This function makes a result with a value.
|
|
187
|
+
*
|
|
188
|
+
* @param value - The value.
|
|
189
|
+
* @returns The result.
|
|
190
|
+
*/
|
|
191
|
+
export declare function ok<V>(value: V): Result<V>;
|
|
192
|
+
/**
|
|
193
|
+
* This function makes a result with an error value.
|
|
194
|
+
*
|
|
195
|
+
* @param code - The code of the error.
|
|
196
|
+
* @param reason - One sentence that tells the cause.
|
|
197
|
+
* @param more - The place and the cause of the error, when they are known.
|
|
198
|
+
* @returns The result.
|
|
199
|
+
*/
|
|
200
|
+
export declare function fail(code: ErrorCode, reason: string, more?: {
|
|
201
|
+
readonly place?: readonly Key[];
|
|
202
|
+
readonly cause?: unknown;
|
|
203
|
+
}): Failed;
|
|
204
|
+
/**
|
|
205
|
+
* This function gives a failed result its place, when it has none. A result with a value, or an error with a
|
|
206
|
+
* place, stays the same object.
|
|
207
|
+
*
|
|
208
|
+
* @param result - The result.
|
|
209
|
+
* @param place - The keys from the root document to the document that failed.
|
|
210
|
+
* @returns The result with a place.
|
|
211
|
+
*/
|
|
212
|
+
export declare function placed<V>(result: Result<V>, place: readonly Key[]): Result<V>;
|
|
213
|
+
/**
|
|
214
|
+
* An exception that carries an error value through code of the host. A closure of a formula throws it when the
|
|
215
|
+
* host calls the closure and the closure fails. The kernel catches it and gives its error value again.
|
|
216
|
+
*/
|
|
217
|
+
export declare class Failure extends Error {
|
|
218
|
+
readonly error: EvalError;
|
|
219
|
+
/**
|
|
220
|
+
* This constructor makes the exception.
|
|
221
|
+
*
|
|
222
|
+
* @param error - The error value that the exception carries.
|
|
223
|
+
*/
|
|
224
|
+
constructor(error: EvalError);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* This function tells if an exception is the overflow of the stack of the host.
|
|
228
|
+
*
|
|
229
|
+
* @param error - The exception.
|
|
230
|
+
* @returns `true` for a stack overflow.
|
|
231
|
+
*/
|
|
232
|
+
export declare function isStackOverflow(error: unknown): boolean;
|
|
233
|
+
/**
|
|
234
|
+
* This function changes an exception into an error value. A `Failure` gives its error value. A stack overflow
|
|
235
|
+
* gives `#DEPTH!`. Each other exception gives `#CALC!`, with the exception as the cause.
|
|
236
|
+
*
|
|
237
|
+
* @param error - The exception.
|
|
238
|
+
* @param who - A short text that names the code that threw, for example `the evaluator math.add`.
|
|
239
|
+
* @returns The failed result.
|
|
240
|
+
*/
|
|
241
|
+
export declare function thrown(error: unknown, who: string): Failed;
|
|
242
|
+
/**
|
|
243
|
+
* This function gives a short text for an exception or any value. It does not throw.
|
|
244
|
+
*
|
|
245
|
+
* @param value - The value.
|
|
246
|
+
* @returns The text.
|
|
247
|
+
*/
|
|
248
|
+
export declare function describe(value: unknown): string;
|
|
128
249
|
//#endregion
|
|
129
250
|
//#region src/world.d.ts
|
|
130
251
|
/** The two sides of evaluation. `derive` is pure. `run` causes effects in order. */
|
|
131
252
|
type Side = "derive" | "run";
|
|
132
253
|
/**
|
|
133
|
-
* Where the parts of a value are. A part is a typed document that the evaluator gets
|
|
254
|
+
* Where the parts of a value are: the shape of the value. A part is a typed document that the evaluator gets
|
|
255
|
+
* sealed. `of` is the type of a bare part: a part without an envelope gets this type from its position.
|
|
134
256
|
*
|
|
135
257
|
* - `none`: the value is data. It has no parts.
|
|
136
|
-
* - `
|
|
137
|
-
* - `
|
|
138
|
-
*
|
|
139
|
-
* `
|
|
258
|
+
* - `term`: the value itself is one part.
|
|
259
|
+
* - `list`: the value is a list. Each item has the shape `item` (the default is a part).
|
|
260
|
+
* - `map`: the value is a record. Each field has the shape `item` (the default is a part).
|
|
261
|
+
* - `record`: the value is a record. Each named field has its shape. A list of names makes each named field a
|
|
262
|
+
* part. The other fields have the shape `rest` (the default is data). `required` names the fields that must be.
|
|
263
|
+
* - `deep`: each envelope at any depth of the value is a part, and the rest is data.
|
|
140
264
|
*/
|
|
141
|
-
type
|
|
265
|
+
type Shape = {
|
|
142
266
|
readonly shape: "none";
|
|
267
|
+
} | {
|
|
268
|
+
readonly shape: "term";
|
|
269
|
+
readonly of?: string;
|
|
143
270
|
} | {
|
|
144
271
|
readonly shape: "list";
|
|
145
272
|
readonly of?: string;
|
|
273
|
+
readonly item?: Shape;
|
|
274
|
+
} | {
|
|
275
|
+
readonly shape: "map";
|
|
276
|
+
readonly of?: string;
|
|
277
|
+
readonly item?: Shape;
|
|
146
278
|
} | {
|
|
147
279
|
readonly shape: "record";
|
|
148
|
-
readonly fields: readonly string[]
|
|
280
|
+
readonly fields: readonly string[] | Readonly<Record<string, Shape>>;
|
|
281
|
+
readonly of?: string;
|
|
282
|
+
readonly required?: readonly string[];
|
|
283
|
+
readonly rest?: Shape;
|
|
284
|
+
} | {
|
|
285
|
+
readonly shape: "deep";
|
|
149
286
|
readonly of?: string;
|
|
150
287
|
};
|
|
288
|
+
/** The former name of `Shape`. */
|
|
289
|
+
type PartsSpec = Shape;
|
|
290
|
+
/**
|
|
291
|
+
* Where the names of a scope rule come from.
|
|
292
|
+
*
|
|
293
|
+
* - `fixed`: the names in the list.
|
|
294
|
+
* - `keysAt`: the keys of the record at this path of the value, for example the bindings of a `let`.
|
|
295
|
+
* - `listAt`: the texts of the list at this path of the value, for example the parameters of a `fn`.
|
|
296
|
+
* - `textAt`: the text at this path of the value, or `otherwise` when the value has no text there.
|
|
297
|
+
* - `headers`: the binding name (`name`) of each part that the rule covers.
|
|
298
|
+
*/
|
|
299
|
+
type NamesSource = {
|
|
300
|
+
readonly fixed: readonly string[];
|
|
301
|
+
} | {
|
|
302
|
+
readonly keysAt: readonly Key[];
|
|
303
|
+
} | {
|
|
304
|
+
readonly listAt: readonly Key[];
|
|
305
|
+
} | {
|
|
306
|
+
readonly textAt: readonly Key[];
|
|
307
|
+
readonly otherwise?: string;
|
|
308
|
+
} | {
|
|
309
|
+
readonly headers: true;
|
|
310
|
+
};
|
|
311
|
+
/**
|
|
312
|
+
* A scope rule of a kind: names that the kind binds, and the parts that see them. The static kit reads the rules,
|
|
313
|
+
* and the evaluator of the kind binds the same names when it evaluates the parts.
|
|
314
|
+
*/
|
|
315
|
+
interface ScopeRule {
|
|
316
|
+
readonly names: NamesSource;
|
|
317
|
+
/** The parts that see the names, as patterns of their paths in the value. The key `"*"` matches each key. */
|
|
318
|
+
readonly in: readonly (readonly Key[])[];
|
|
319
|
+
/** `true`: a part sees only the names of the parts before it, as in a sequence. */
|
|
320
|
+
readonly sequential?: boolean;
|
|
321
|
+
}
|
|
151
322
|
/** A kind gives the name of an evaluator for each verb, and the contract of its value. */
|
|
152
323
|
interface Kind {
|
|
153
324
|
readonly verbs: Readonly<Record<string, string>>;
|
|
154
325
|
/** Where the parts of the value are. The default is `none`. */
|
|
155
|
-
readonly parts?:
|
|
326
|
+
readonly parts?: Shape;
|
|
327
|
+
/** The names that the kind binds for its parts. */
|
|
328
|
+
readonly scope?: readonly ScopeRule[];
|
|
156
329
|
/** The side of the kind. A kind of the run side causes effects. The default is `derive`. */
|
|
157
330
|
readonly side?: Side;
|
|
331
|
+
/**
|
|
332
|
+
* The effects that a document of this kind can cause. It is a list of the kinds of the effects. The text
|
|
333
|
+
* `"type"` means that the kind of the effect is the type of the document itself, as for a call.
|
|
334
|
+
*/
|
|
335
|
+
readonly effects?: "type" | readonly string[];
|
|
158
336
|
/** One sentence that tells what the kind is. */
|
|
159
337
|
readonly summary?: string;
|
|
160
338
|
}
|
|
161
|
-
/** A namespace: a group of entries under one key of the world. */
|
|
339
|
+
/** A namespace: a group of entries under one key of the world. `fallback` is the kind of each other key in it. */
|
|
162
340
|
interface Namespace {
|
|
163
341
|
readonly kinds: Readonly<Record<string, WorldEntry>>;
|
|
342
|
+
readonly fallback?: Kind;
|
|
164
343
|
}
|
|
165
344
|
/** An alias: a reference to another entry of the world. The lookup follows it (collapse). */
|
|
166
345
|
type Alias = {
|
|
@@ -199,9 +378,17 @@ export declare function isKind(entry: WorldEntry | undefined): entry is Kind;
|
|
|
199
378
|
* @returns `true` for an alias.
|
|
200
379
|
*/
|
|
201
380
|
export declare function isAlias(entry: WorldEntry | undefined): entry is Alias;
|
|
381
|
+
/**
|
|
382
|
+
* This function tells if an entry of the world is a namespace.
|
|
383
|
+
*
|
|
384
|
+
* @param entry - The entry.
|
|
385
|
+
* @returns `true` for a namespace.
|
|
386
|
+
*/
|
|
387
|
+
export declare function isNamespace(entry: WorldEntry | undefined): entry is Namespace;
|
|
202
388
|
/**
|
|
203
389
|
* This function finds the kind that a type names. It walks the path from the root of the world, one key at a
|
|
204
|
-
* time. An alias on the way is followed until a kind answers (collapse). A cycle of aliases stops the lookup.
|
|
390
|
+
* time. An alias on the way is followed until a kind answers (collapse). A cycle of aliases stops the lookup. In a
|
|
391
|
+
* namespace with a fallback, a key that the namespace does not have gets the fallback kind.
|
|
205
392
|
*
|
|
206
393
|
* @param world - The world.
|
|
207
394
|
* @param type - The reference to the type.
|
|
@@ -224,6 +411,13 @@ export declare function entriesAsJson(entries: Readonly<Record<string, WorldEntr
|
|
|
224
411
|
* @returns The document of type `kind`.
|
|
225
412
|
*/
|
|
226
413
|
export declare function kindDocument(name: string, kind: Kind): Envelope;
|
|
414
|
+
/**
|
|
415
|
+
* This function tells if a JSON value is the record of a kind.
|
|
416
|
+
*
|
|
417
|
+
* @param value - The value.
|
|
418
|
+
* @returns `true` when the value has verbs.
|
|
419
|
+
*/
|
|
420
|
+
export declare function looksLikeKind(value: unknown): boolean;
|
|
227
421
|
//#endregion
|
|
228
422
|
//#region src/ladder.d.ts
|
|
229
423
|
/** The rungs of the ladder, in the order that a question visits them. */
|
|
@@ -309,7 +503,10 @@ export declare function kindNamed(world: World, name: TypeRef): Kind | undefined
|
|
|
309
503
|
export declare function ask(world: World, doc: Header, verb: string, options?: AskOptions): Answer;
|
|
310
504
|
//#endregion
|
|
311
505
|
//#region src/deref.d.ts
|
|
312
|
-
/**
|
|
506
|
+
/**
|
|
507
|
+
* The brand of a host thing: an object of the host that answers the steps of a path itself. The brand is a
|
|
508
|
+
* global symbol, thus two copies of the kernel in one program know the things of each other.
|
|
509
|
+
*/
|
|
313
510
|
export declare const THING: unique symbol;
|
|
314
511
|
/**
|
|
315
512
|
* A host thing: an object that answers the steps of a path itself, for example a node of a live tree. It is the
|
|
@@ -323,7 +520,7 @@ interface HostThing {
|
|
|
323
520
|
* This function answers one step.
|
|
324
521
|
*
|
|
325
522
|
* @param key - The key of the step.
|
|
326
|
-
* @returns What the step reaches, or `undefined
|
|
523
|
+
* @returns What the step reaches, or `undefined` when it reaches nothing.
|
|
327
524
|
*/
|
|
328
525
|
deref(key: Key): Thing | undefined;
|
|
329
526
|
/**
|
|
@@ -331,21 +528,36 @@ interface HostThing {
|
|
|
331
528
|
*
|
|
332
529
|
* @returns The value.
|
|
333
530
|
*/
|
|
334
|
-
toJson():
|
|
531
|
+
toJson(): unknown;
|
|
532
|
+
/**
|
|
533
|
+
* This optional function answers all the steps of a path that are keys, in one call. A store uses it to read a
|
|
534
|
+
* path without one call for each step.
|
|
535
|
+
*
|
|
536
|
+
* @param keys - The keys.
|
|
537
|
+
* @returns What the keys reach, or `undefined` when they reach nothing.
|
|
538
|
+
*/
|
|
539
|
+
resolve?(keys: readonly Key[]): {
|
|
540
|
+
readonly thing: Thing;
|
|
541
|
+
} | undefined;
|
|
335
542
|
}
|
|
336
|
-
/** Something that a path can reach:
|
|
337
|
-
type Thing =
|
|
543
|
+
/** Something that a path can reach: any value of the host, or a host thing. */
|
|
544
|
+
type Thing = unknown;
|
|
338
545
|
/**
|
|
339
546
|
* This function tells if a thing is a host thing.
|
|
340
547
|
*
|
|
341
548
|
* @param thing - The thing.
|
|
342
549
|
* @returns `true` for a host thing.
|
|
343
550
|
*/
|
|
344
|
-
export declare function isHostThing(thing:
|
|
551
|
+
export declare function isHostThing(thing: unknown): thing is HostThing;
|
|
345
552
|
/** The roots of a context. Each path starts with one of them. */
|
|
346
553
|
export declare const ROOTS: readonly ["self", "names", "world", "event", "caps"];
|
|
347
554
|
/** One root of a context. */
|
|
348
555
|
type Root = (typeof ROOTS)[number];
|
|
556
|
+
/**
|
|
557
|
+
* A capability: the name of a kind of effect, or its keys. A capability permits its kind and each kind under it.
|
|
558
|
+
* Keys are necessary when a key has a dot in it.
|
|
559
|
+
*/
|
|
560
|
+
type Cap = string | readonly Key[];
|
|
349
561
|
/**
|
|
350
562
|
* The context of a term: a record with five roots. A reference reaches outside its term only through them.
|
|
351
563
|
*
|
|
@@ -357,10 +569,10 @@ type Root = (typeof ROOTS)[number];
|
|
|
357
569
|
*/
|
|
358
570
|
interface Context {
|
|
359
571
|
readonly self: Thing;
|
|
360
|
-
readonly names: Readonly<Record<string,
|
|
572
|
+
readonly names: Readonly<Record<string, unknown>>;
|
|
361
573
|
readonly world: Thing;
|
|
362
574
|
readonly event: Thing;
|
|
363
|
-
readonly caps: readonly
|
|
575
|
+
readonly caps: readonly Cap[];
|
|
364
576
|
}
|
|
365
577
|
/** The code of a `deref` evaluator: it answers one step of a path on a document of its kind. */
|
|
366
578
|
type DerefCode = (doc: Envelope, key: Key) => Thing | undefined;
|
|
@@ -390,11 +602,19 @@ interface WalkInput {
|
|
|
390
602
|
}
|
|
391
603
|
/** A walker: the code of the verb `step` of a kind of step documents. */
|
|
392
604
|
type Walker = (input: WalkInput) => WalkResult;
|
|
393
|
-
/**
|
|
605
|
+
/**
|
|
606
|
+
* What a resolution needs: the world, and the code of the `deref` evaluators and of the walkers.
|
|
607
|
+
*
|
|
608
|
+
* - `plain`: `true` when the values are data. Then a step does not ask the kind of an envelope, and a reference
|
|
609
|
+
* that a step reaches stays a value (no collapse).
|
|
610
|
+
* - `roots`: the roots that a path can start at. The default is each root.
|
|
611
|
+
*/
|
|
394
612
|
interface Navigator {
|
|
395
613
|
readonly world: World;
|
|
396
614
|
readonly derefs?: Readonly<Record<string, DerefCode>>;
|
|
397
615
|
readonly walkers?: Readonly<Record<string, Walker>>;
|
|
616
|
+
readonly plain?: boolean;
|
|
617
|
+
readonly roots?: readonly Root[];
|
|
398
618
|
}
|
|
399
619
|
/** The way that one hop of a path was answered. */
|
|
400
620
|
type HopBy = "root" | "own" | "kind" | "default" | "step" | "collapse";
|
|
@@ -404,6 +624,8 @@ interface Hop {
|
|
|
404
624
|
readonly thing: Thing;
|
|
405
625
|
readonly by: HopBy;
|
|
406
626
|
}
|
|
627
|
+
/** The codes of a failed resolution. */
|
|
628
|
+
type ResolveCode = "#REF!" | "#NAME?" | "#N/A" | "#CYCLE!" | "#STEP?";
|
|
407
629
|
/** The result of a resolution: what the path reached, with each hop, or an error with the hops before it. */
|
|
408
630
|
type Resolution = {
|
|
409
631
|
readonly ok: true;
|
|
@@ -411,36 +633,42 @@ type Resolution = {
|
|
|
411
633
|
readonly hops: readonly Hop[];
|
|
412
634
|
} | {
|
|
413
635
|
readonly ok: false;
|
|
414
|
-
readonly code:
|
|
636
|
+
readonly code: ResolveCode;
|
|
415
637
|
readonly reason: string;
|
|
416
638
|
readonly hops: readonly Hop[];
|
|
417
639
|
};
|
|
640
|
+
/** The options of a resolution. */
|
|
641
|
+
interface ResolveOptions {
|
|
642
|
+
/** `false`: do not record the hops. The default is `true`. */
|
|
643
|
+
readonly hops?: boolean;
|
|
644
|
+
}
|
|
418
645
|
/**
|
|
419
646
|
* This function answers one step of a path on a thing, through the ladder of the verb `deref`. A host thing
|
|
420
|
-
* answers for itself (own). A document asks its own entry, then its kind. The default goes into the value:
|
|
421
|
-
* of
|
|
647
|
+
* answers for itself (own). A document asks its own entry, then its kind. The default goes into the value: an own
|
|
648
|
+
* field of an object, or an item of a list.
|
|
422
649
|
*
|
|
423
650
|
* @param nav - The world and the code of the evaluators.
|
|
424
651
|
* @param thing - The thing that the path reached.
|
|
425
652
|
* @param key - The key of the step.
|
|
426
|
-
* @returns What the step reaches and the rung that answered, or `undefined
|
|
653
|
+
* @returns What the step reaches and the rung that answered, or `undefined` when it reaches nothing.
|
|
427
654
|
*/
|
|
428
655
|
export declare function derefStep(nav: Navigator, thing: Thing, key: Key): {
|
|
429
656
|
readonly thing: Thing;
|
|
430
657
|
readonly by: HopBy;
|
|
431
658
|
} | undefined;
|
|
432
659
|
/**
|
|
433
|
-
* This function tells if a thing is a reference document: an envelope of the type `ref
|
|
660
|
+
* This function tells if a thing is a reference document: an envelope of the type `ref` with a list for its path.
|
|
434
661
|
*
|
|
435
662
|
* @param thing - The thing.
|
|
436
663
|
* @returns `true` for a reference document.
|
|
437
664
|
*/
|
|
438
|
-
export declare function isRef(thing:
|
|
665
|
+
export declare function isRef(thing: unknown): thing is Envelope & {
|
|
439
666
|
readonly value: readonly Json[];
|
|
440
667
|
};
|
|
441
668
|
/**
|
|
442
669
|
* This function makes the thing of the root `world`: the entries of the world as JSON. An alias is a reference
|
|
443
|
-
* document in it, thus a walk through the world follows each alias (collapse).
|
|
670
|
+
* document in it, thus a walk through the world follows each alias (collapse). The thing of a world is made one
|
|
671
|
+
* time.
|
|
444
672
|
*
|
|
445
673
|
* @param world - The world.
|
|
446
674
|
* @returns The thing.
|
|
@@ -459,25 +687,34 @@ export declare function context(parts?: Partial<Context>): Context;
|
|
|
459
687
|
* The first step is a root of the context. Each later step is a key or a step document. A key goes up the ladder
|
|
460
688
|
* of the verb `deref` on the thing that the path reached. A step document is evaluated by the walker of its kind,
|
|
461
689
|
* with the thing that the path reached. After each hop, a reference document is followed until the path reaches
|
|
462
|
-
* something else (collapse).
|
|
690
|
+
* something else (collapse), except in a plain navigation.
|
|
691
|
+
*
|
|
692
|
+
* The codes of a failure:
|
|
693
|
+
*
|
|
694
|
+
* - `#REF!`: the path has no root, or a root that is not open here.
|
|
695
|
+
* - `#NAME?`: the root `names` has no such name.
|
|
696
|
+
* - `#N/A`: a step reaches nothing.
|
|
697
|
+
* - `#STEP?`: a step is not a key and not a step document.
|
|
698
|
+
* - `#CYCLE!`: the references form a cycle.
|
|
463
699
|
*
|
|
464
700
|
* @param nav - The world and the code of the evaluators.
|
|
465
701
|
* @param ctx - The context.
|
|
466
702
|
* @param path - The steps.
|
|
703
|
+
* @param options - `hops: false` does not record the hops.
|
|
467
704
|
* @returns What the path reached and each hop, or an error.
|
|
468
705
|
*/
|
|
469
|
-
export declare function resolve(nav: Navigator, ctx: Context, path: readonly
|
|
706
|
+
export declare function resolve(nav: Navigator, ctx: Context, path: readonly unknown[], options?: ResolveOptions): Resolution;
|
|
470
707
|
/**
|
|
471
|
-
* This function gives the value of a thing: the
|
|
708
|
+
* This function gives the value of a thing: the value that a host thing gives, or else the thing itself.
|
|
472
709
|
*
|
|
473
710
|
* @param thing - The thing.
|
|
474
711
|
* @returns The value.
|
|
475
712
|
*/
|
|
476
|
-
export declare function valueOfThing(thing: Thing):
|
|
713
|
+
export declare function valueOfThing(thing: Thing): unknown;
|
|
477
714
|
//#endregion
|
|
478
715
|
//#region src/classify.d.ts
|
|
479
716
|
/** A shape proposes a kind for each document that has all of its fields. */
|
|
480
|
-
interface Shape {
|
|
717
|
+
interface Shape$1 {
|
|
481
718
|
readonly name: string;
|
|
482
719
|
readonly fields: readonly string[];
|
|
483
720
|
readonly kind: string;
|
|
@@ -501,7 +738,7 @@ interface Classification {
|
|
|
501
738
|
* @param value - The value without a type.
|
|
502
739
|
* @returns The proposed kind, or an error with the candidates.
|
|
503
740
|
*/
|
|
504
|
-
export declare function classify(shapes: readonly Shape[], value: Json): Classification;
|
|
741
|
+
export declare function classify(shapes: readonly Shape$1[], value: Json): Classification;
|
|
505
742
|
/**
|
|
506
743
|
* This function puts a value in an envelope with a type. The value does not change.
|
|
507
744
|
*
|
|
@@ -512,62 +749,54 @@ export declare function classify(shapes: readonly Shape[], value: Json): Classif
|
|
|
512
749
|
export declare function tag(value: Json, kind: string): Envelope;
|
|
513
750
|
//#endregion
|
|
514
751
|
//#region src/parts.d.ts
|
|
515
|
-
/** One part of a value: its
|
|
752
|
+
/** One part of a value: its place in the value, the JSON of the part, and the type that its position declares. */
|
|
516
753
|
interface PartSlot {
|
|
754
|
+
/** The keys from the value of the document to the part. */
|
|
755
|
+
readonly path: readonly Key[];
|
|
756
|
+
/** The path as one text, with a dot between the keys, for example `bind.x`. A part of a list has its index. */
|
|
517
757
|
readonly key: string;
|
|
518
758
|
readonly item: Json;
|
|
519
759
|
readonly of?: string;
|
|
520
760
|
}
|
|
521
|
-
/** The value of a document, split into its
|
|
761
|
+
/** The value of a document, split into its parts, by the shape that its kind declares. */
|
|
522
762
|
type Split = {
|
|
523
763
|
readonly ok: true;
|
|
524
|
-
|
|
764
|
+
/** The parts, in the order of the value. */
|
|
525
765
|
readonly parts: readonly PartSlot[];
|
|
766
|
+
/** The value without the parts: a field that holds a part is not in it, and a list of parts gives `null`. */
|
|
767
|
+
readonly data: Json;
|
|
768
|
+
/**
|
|
769
|
+
* This function gives the value with a handle in the place of each part.
|
|
770
|
+
*
|
|
771
|
+
* @param handles - One handle for each part, in the order of `parts`.
|
|
772
|
+
* @returns The value with the handles.
|
|
773
|
+
*/
|
|
774
|
+
fill<P>(handles: readonly P[]): unknown;
|
|
526
775
|
} | {
|
|
527
776
|
readonly ok: false;
|
|
528
777
|
readonly reason: string;
|
|
778
|
+
readonly at: readonly Key[];
|
|
529
779
|
};
|
|
530
780
|
/**
|
|
531
|
-
* This function splits a value into its
|
|
532
|
-
*
|
|
781
|
+
* This function splits a value into its parts, by the shape that its kind declares. The evaluator, the view and
|
|
782
|
+
* the static kit use the same split, thus each one sees the same parts.
|
|
533
783
|
*
|
|
534
|
-
* @param
|
|
784
|
+
* @param shape - The shape of the value. The default is data.
|
|
535
785
|
* @param value - The value of the document.
|
|
536
|
-
* @returns The
|
|
786
|
+
* @returns The parts and a way to fill their places, or the reason why the value does not have the shape.
|
|
537
787
|
*/
|
|
538
|
-
export declare function splitParts(
|
|
539
|
-
//#endregion
|
|
540
|
-
//#region src/evaluate.d.ts
|
|
541
|
-
/** The code of an error value. */
|
|
542
|
-
type ErrorCode = "#KIND?" | "#NOEVAL" | "#PART?" | "#NAME?" | "#REF!" | "#CYCLE!" | "#STEP?" | "#VALUE!" | "#ARGS" | "#CAP!" | "#CALC!" | "#DEPTH!";
|
|
543
|
-
/** An error value: a code and a reason. An evaluation gives an error value, and it does not throw. */
|
|
544
|
-
interface EvalError {
|
|
545
|
-
readonly code: ErrorCode;
|
|
546
|
-
readonly reason: string;
|
|
547
|
-
}
|
|
548
|
-
/** The result of an evaluation: a value, or an error value. */
|
|
549
|
-
type Result = {
|
|
550
|
-
readonly ok: true;
|
|
551
|
-
readonly value: Json;
|
|
552
|
-
} | {
|
|
553
|
-
readonly ok: false;
|
|
554
|
-
readonly error: EvalError;
|
|
555
|
-
};
|
|
788
|
+
export declare function splitParts(shape: Shape | undefined, value: Json): Split;
|
|
556
789
|
/**
|
|
557
|
-
* This function
|
|
790
|
+
* This function tells if the path of a part matches a pattern of a scope rule. The key `"*"` of a pattern matches
|
|
791
|
+
* each key.
|
|
558
792
|
*
|
|
559
|
-
* @param
|
|
560
|
-
* @
|
|
793
|
+
* @param path - The path of the part.
|
|
794
|
+
* @param pattern - The pattern.
|
|
795
|
+
* @returns `true` when the path matches.
|
|
561
796
|
*/
|
|
562
|
-
export declare function
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
*
|
|
566
|
-
* @param code - The code of the error.
|
|
567
|
-
* @param reason - One sentence that tells the cause.
|
|
568
|
-
* @returns The result.
|
|
569
|
-
*/
|
|
570
|
-
export declare function fail(code: ErrorCode, reason: string): Result;
|
|
797
|
+
export declare function pathMatches(path: readonly Key[], pattern: readonly Key[]): boolean;
|
|
798
|
+
//#endregion
|
|
799
|
+
//#region src/evaluate.d.ts
|
|
571
800
|
/**
|
|
572
801
|
* This function makes a context with names and capabilities. The other roots are empty, and an evaluation sets
|
|
573
802
|
* the world.
|
|
@@ -577,14 +806,151 @@ export declare function fail(code: ErrorCode, reason: string): Result;
|
|
|
577
806
|
* @param self - The node of the term, if it has one.
|
|
578
807
|
* @returns The context.
|
|
579
808
|
*/
|
|
580
|
-
export declare function makeContext(names?: Readonly<Record<string,
|
|
809
|
+
export declare function makeContext(names?: Readonly<Record<string, unknown>>, caps?: readonly Cap[], self?: Thing): Context;
|
|
810
|
+
/**
|
|
811
|
+
* This function gives the keys of a capability or of a kind of effect: a text is split at its dots.
|
|
812
|
+
*
|
|
813
|
+
* @param kind - The text or the keys.
|
|
814
|
+
* @returns The keys, as texts.
|
|
815
|
+
*/
|
|
816
|
+
export declare function capKeys(kind: Cap): readonly string[];
|
|
817
|
+
/**
|
|
818
|
+
* This function tells if the capabilities permit an effect. A capability permits the kind of its name, and each kind
|
|
819
|
+
* under it: `store` permits `store.update`. The comparison is key by key, thus `fs.read` does not permit the
|
|
820
|
+
* kind with the keys `fs` and `read.json`.
|
|
821
|
+
*
|
|
822
|
+
* @param caps - The capabilities of the context.
|
|
823
|
+
* @param kind - The kind of the effect: a text with dots, or its keys.
|
|
824
|
+
* @returns `true` when one capability permits it.
|
|
825
|
+
*/
|
|
826
|
+
export declare function permits(caps: readonly Cap[], kind: Cap): boolean;
|
|
827
|
+
/**
|
|
828
|
+
* This function narrows a context for a part: the part keeps only the capabilities that the context of its parent
|
|
829
|
+
* permits. Thus an evaluator cannot give a part more effects than it has.
|
|
830
|
+
*
|
|
831
|
+
* @param parent - The context of the parent.
|
|
832
|
+
* @param next - The context that the evaluator gives the part.
|
|
833
|
+
* @returns The context of the part.
|
|
834
|
+
*/
|
|
835
|
+
export declare function narrow(parent: Context, next: Context): Context;
|
|
836
|
+
/**
|
|
837
|
+
* The steps of an evaluation that can wait. It yields a promise only when the host makes it wait. A runner takes the
|
|
838
|
+
* result of a part with `yield*`, thus a run goes on at once until an effect waits. A runner does not yield a
|
|
839
|
+
* promise itself: it uses `yield*` on the steps that the kernel gives.
|
|
840
|
+
*/
|
|
841
|
+
type Steps = Generator<Promise<unknown>, Result, unknown>;
|
|
842
|
+
/** An effect that an evaluation caused, in the order of the effects. */
|
|
843
|
+
interface Effect {
|
|
844
|
+
readonly index: number;
|
|
845
|
+
/** The kind of the effect, as a text with dots. */
|
|
846
|
+
readonly kind: string;
|
|
847
|
+
/** The keys of the kind of the effect. */
|
|
848
|
+
readonly path: readonly Key[];
|
|
849
|
+
readonly payload: unknown;
|
|
850
|
+
/** The ID of the trace node that caused the effect, or -1 without a trace. */
|
|
851
|
+
readonly node: number;
|
|
852
|
+
/** The number of events before the effect. The effect comes before the end of its node. */
|
|
853
|
+
readonly at: number;
|
|
854
|
+
/** The keys from the root document to the document that caused the effect. */
|
|
855
|
+
readonly place: readonly Key[];
|
|
856
|
+
/** The context of the document that caused the effect: its names, its node and its capabilities. */
|
|
857
|
+
readonly context: Context;
|
|
858
|
+
}
|
|
859
|
+
/** The host side of a run: it does one effect, and gives its result. A throw or a rejection is an error value. */
|
|
860
|
+
type Perform = (effect: Effect) => unknown;
|
|
861
|
+
/**
|
|
862
|
+
* The runtime of one evaluation or run. Compiled code gets it with the context, and asks it to resolve paths and
|
|
863
|
+
* to cause effects.
|
|
864
|
+
*/
|
|
865
|
+
interface Runtime {
|
|
866
|
+
readonly machine: Machine;
|
|
867
|
+
/** `true` in a run: the host does each effect, and a part that waits needs a parent that can wait. */
|
|
868
|
+
readonly running: boolean;
|
|
869
|
+
/**
|
|
870
|
+
* This function resolves a path from a context.
|
|
871
|
+
*
|
|
872
|
+
* @param ctx - The context.
|
|
873
|
+
* @param path - The steps. The first step is a root.
|
|
874
|
+
* @param options - `hops: true` records each hop. The default records them only in a traced evaluation.
|
|
875
|
+
* @returns What the path reached, or an error.
|
|
876
|
+
*/
|
|
877
|
+
resolve(ctx: Context, path: readonly unknown[], options?: ResolveOptions): Resolution;
|
|
878
|
+
/**
|
|
879
|
+
* This function causes an effect from an evaluator. In a run, an evaluator cannot cause an effect: the host does
|
|
880
|
+
* each effect, thus only a runner can wait for it.
|
|
881
|
+
*
|
|
882
|
+
* @param ctx - The context of the document.
|
|
883
|
+
* @param kind - The kind of the effect.
|
|
884
|
+
* @param payload - The data of the effect.
|
|
885
|
+
* @param place - The place of the document.
|
|
886
|
+
* @returns The payload, or an error value.
|
|
887
|
+
*/
|
|
888
|
+
effect(ctx: Context, kind: Cap, payload: unknown, place: readonly Key[]): Result;
|
|
889
|
+
/**
|
|
890
|
+
* This function causes an effect from a runner. In a run, the host does it, and the steps wait when the host
|
|
891
|
+
* waits.
|
|
892
|
+
*
|
|
893
|
+
* @param ctx - The context of the document.
|
|
894
|
+
* @param kind - The kind of the effect.
|
|
895
|
+
* @param payload - The data of the effect.
|
|
896
|
+
* @param place - The place of the document.
|
|
897
|
+
* @returns The steps that give the result of the host, or an error value.
|
|
898
|
+
*/
|
|
899
|
+
runEffect(ctx: Context, kind: Cap, payload: unknown, place: readonly Key[]): Steps;
|
|
900
|
+
/**
|
|
901
|
+
* This function drives several steps at the same time. When one waits, the others go on.
|
|
902
|
+
*
|
|
903
|
+
* @param list - One function for each item. Each function starts the steps of its item.
|
|
904
|
+
* @param options - The most items that run at the same time. The default is each item.
|
|
905
|
+
* @returns The steps that give each result, in the order of the list.
|
|
906
|
+
*/
|
|
907
|
+
all(list: readonly (() => Steps)[], options?: {
|
|
908
|
+
readonly concurrency?: number;
|
|
909
|
+
}): Generator<Promise<unknown>, readonly Result[], unknown>;
|
|
910
|
+
}
|
|
911
|
+
/** Compiled code: the evaluation of a document in a context. */
|
|
912
|
+
type Code = (ctx: Context, rt: Runtime) => Result;
|
|
913
|
+
/** Compiled code that can wait: the run of a document in a context. */
|
|
914
|
+
type RunCode = (ctx: Context, rt: Runtime) => Steps;
|
|
915
|
+
/** A compiled part: its place in its parent, its binding name, and its code. It has no field with its value. */
|
|
916
|
+
interface CompiledPart {
|
|
917
|
+
/** The path in the value of the parent as one text, for example `0` or `bind.x`. */
|
|
918
|
+
readonly key: string;
|
|
919
|
+
/** The keys from the value of the parent to the part. */
|
|
920
|
+
readonly path: readonly Key[];
|
|
921
|
+
/** The binding name from the header of the part. */
|
|
922
|
+
readonly name?: string;
|
|
923
|
+
/** The code that evaluates the part. */
|
|
924
|
+
readonly code: Code;
|
|
925
|
+
/** The code that runs the part. Its steps can wait. */
|
|
926
|
+
readonly run: RunCode;
|
|
927
|
+
}
|
|
928
|
+
/** The input of a compiler: the document, its kind, and its parts, compiled. */
|
|
929
|
+
interface CompileInput {
|
|
930
|
+
/** The text of the type of the document, for example `add` or `math.sum` after its alias. */
|
|
931
|
+
readonly kind: string;
|
|
932
|
+
/** The keys of the kind after each alias. */
|
|
933
|
+
readonly typePath: readonly Key[];
|
|
934
|
+
readonly doc: Envelope;
|
|
935
|
+
/** The value of the document with a compiled part in the place of each part. */
|
|
936
|
+
readonly value: unknown;
|
|
937
|
+
/** The value of the document without its parts. */
|
|
938
|
+
readonly data: Json;
|
|
939
|
+
/** The compiled parts, in the order of the value. */
|
|
940
|
+
readonly parts: readonly CompiledPart[];
|
|
941
|
+
/** The keys from the root document to this document. */
|
|
942
|
+
readonly place: readonly Key[];
|
|
943
|
+
readonly machine: Machine;
|
|
944
|
+
}
|
|
581
945
|
/**
|
|
582
946
|
* A sealed part. An evaluator can read the binding name of a part and evaluate it. It cannot read the value of the
|
|
583
947
|
* part, thus two parts that give the same results can replace each other.
|
|
584
948
|
*/
|
|
585
949
|
interface Part$1 {
|
|
586
|
-
/** The
|
|
950
|
+
/** The path in the value of the parent as one text, for example `0` or `bind.x`. */
|
|
587
951
|
readonly key: string;
|
|
952
|
+
/** The keys from the value of the parent to the part. */
|
|
953
|
+
readonly path: readonly Key[];
|
|
588
954
|
/** The binding name from the header of the part. */
|
|
589
955
|
readonly name?: string;
|
|
590
956
|
/**
|
|
@@ -599,19 +965,27 @@ interface Part$1 {
|
|
|
599
965
|
interface EvalInput {
|
|
600
966
|
/** The text of the type of the document, for example `add` or `math.sum` after its alias. */
|
|
601
967
|
readonly kind: string;
|
|
968
|
+
/** The keys of the kind after each alias. */
|
|
969
|
+
readonly typePath: readonly Key[];
|
|
970
|
+
readonly doc: Envelope;
|
|
971
|
+
/** The value of the document with a sealed part in the place of each part. */
|
|
972
|
+
readonly value: unknown;
|
|
602
973
|
/** The value of the document without its parts. */
|
|
603
974
|
readonly data: Json;
|
|
604
975
|
/** The parts, in the order of the value. */
|
|
605
976
|
readonly parts: readonly Part$1[];
|
|
606
977
|
/** The context of the document. */
|
|
607
978
|
readonly context: Context;
|
|
979
|
+
/** The keys from the root document to this document. */
|
|
980
|
+
readonly place: readonly Key[];
|
|
608
981
|
/**
|
|
609
982
|
* This function resolves a path from the context of the document.
|
|
610
983
|
*
|
|
611
984
|
* @param path - The steps. The first step is a root.
|
|
612
|
-
* @
|
|
985
|
+
* @param options - `hops: true` records each hop.
|
|
986
|
+
* @returns What the path reached, or an error.
|
|
613
987
|
*/
|
|
614
|
-
resolve(path: readonly
|
|
988
|
+
resolve(path: readonly unknown[], options?: ResolveOptions): Resolution;
|
|
615
989
|
/**
|
|
616
990
|
* This function causes an effect. The context must have the capability for the kind of the effect. In a run, an
|
|
617
991
|
* evaluator cannot cause an effect: the host does each effect, thus only a runner can wait for it.
|
|
@@ -620,19 +994,14 @@ interface EvalInput {
|
|
|
620
994
|
* @param payload - The data of the effect.
|
|
621
995
|
* @returns The payload, or the error `#CAP!`.
|
|
622
996
|
*/
|
|
623
|
-
effect(kind:
|
|
997
|
+
effect(kind: Cap, payload: unknown): Result;
|
|
624
998
|
}
|
|
625
|
-
/** An evaluator: a function from the input of a document to a result. */
|
|
626
|
-
type Evaluator = (input: EvalInput) => Result;
|
|
627
|
-
/**
|
|
628
|
-
* The steps of an evaluation that can wait. It yields a promise only when the host makes it wait. A runner takes the
|
|
629
|
-
* result of a part with `yield*`, thus a run goes on at once until an effect waits.
|
|
630
|
-
*/
|
|
631
|
-
type Steps = Generator<Promise<Result>, Result, Result>;
|
|
632
999
|
/** A part for a runner. Its evaluation can wait for the effects in it. */
|
|
633
1000
|
interface RunPart {
|
|
634
|
-
/** The
|
|
1001
|
+
/** The path in the value of the parent as one text. */
|
|
635
1002
|
readonly key: string;
|
|
1003
|
+
/** The keys from the value of the parent to the part. */
|
|
1004
|
+
readonly path: readonly Key[];
|
|
636
1005
|
/** The binding name from the header of the part. */
|
|
637
1006
|
readonly name?: string;
|
|
638
1007
|
/**
|
|
@@ -645,77 +1014,84 @@ interface RunPart {
|
|
|
645
1014
|
}
|
|
646
1015
|
/** The input of a runner: the input of an evaluator, with parts and effects that can wait. */
|
|
647
1016
|
interface RunInput {
|
|
648
|
-
/** The text of the type of the document. */
|
|
649
1017
|
readonly kind: string;
|
|
650
|
-
|
|
1018
|
+
readonly typePath: readonly Key[];
|
|
1019
|
+
readonly doc: Envelope;
|
|
1020
|
+
/** The value of the document with a part for a runner in the place of each part. */
|
|
1021
|
+
readonly value: unknown;
|
|
651
1022
|
readonly data: Json;
|
|
652
|
-
/** The parts, in the order of the value. */
|
|
653
1023
|
readonly parts: readonly RunPart[];
|
|
654
|
-
/** The context of the document. */
|
|
655
1024
|
readonly context: Context;
|
|
1025
|
+
readonly place: readonly Key[];
|
|
1026
|
+
resolve(path: readonly unknown[], options?: ResolveOptions): Resolution;
|
|
656
1027
|
/**
|
|
657
|
-
* This function
|
|
658
|
-
*
|
|
659
|
-
* @param path - The steps. The first step is a root.
|
|
660
|
-
* @returns What the path reached, with each hop.
|
|
661
|
-
*/
|
|
662
|
-
resolve(path: readonly Json[]): Resolution;
|
|
663
|
-
/**
|
|
664
|
-
* This function causes an effect. The context must have the capability. In a run, the host does the effect,
|
|
665
|
-
* and the steps wait when the host waits. In an evaluation, the effect gives its payload. Take its result with
|
|
666
|
-
* `yield*`.
|
|
1028
|
+
* This function causes an effect. In a run, the host does the effect, and the steps wait when the host waits. In
|
|
1029
|
+
* an evaluation, the effect gives its payload. Take its result with `yield*`.
|
|
667
1030
|
*
|
|
668
1031
|
* @param kind - The kind of the effect, for example `log`.
|
|
669
1032
|
* @param payload - The data of the effect.
|
|
670
1033
|
* @returns The steps that give the result of the host, or an error value.
|
|
671
1034
|
*/
|
|
672
|
-
effect(kind:
|
|
1035
|
+
effect(kind: Cap, payload: unknown): Steps;
|
|
1036
|
+
/**
|
|
1037
|
+
* This function evaluates parts at the same time. Take the results with `yield*`.
|
|
1038
|
+
*
|
|
1039
|
+
* @param parts - The parts.
|
|
1040
|
+
* @param options - The most parts that run at the same time.
|
|
1041
|
+
* @returns The steps that give each result, in the order of the parts.
|
|
1042
|
+
*/
|
|
1043
|
+
all(parts: readonly RunPart[], options?: {
|
|
1044
|
+
readonly concurrency?: number;
|
|
1045
|
+
}): Generator<Promise<unknown>, readonly Result[], unknown>;
|
|
1046
|
+
}
|
|
1047
|
+
/** An evaluator that is a function of its input. The kernel calls it for each evaluation. */
|
|
1048
|
+
type EvalFn = (input: EvalInput) => Result;
|
|
1049
|
+
/** An evaluator that compiles a document one time into code. The code then evaluates it many times. */
|
|
1050
|
+
interface CompiledEvaluator {
|
|
1051
|
+
/**
|
|
1052
|
+
* This function compiles a document.
|
|
1053
|
+
*
|
|
1054
|
+
* @param input - The document, its kind and its compiled parts.
|
|
1055
|
+
* @returns The code that evaluates the document.
|
|
1056
|
+
*/
|
|
1057
|
+
readonly compile: (input: CompileInput) => Code;
|
|
673
1058
|
}
|
|
1059
|
+
/** An evaluator: a function of its input, or a compiler. */
|
|
1060
|
+
type Evaluator = EvalFn | CompiledEvaluator;
|
|
1061
|
+
/** A runner that is a generator function of its input. */
|
|
1062
|
+
type RunFn = (input: RunInput) => Steps;
|
|
1063
|
+
/** A runner that compiles a document one time into code that can wait. */
|
|
1064
|
+
interface CompiledRunner {
|
|
1065
|
+
/**
|
|
1066
|
+
* This function compiles a document.
|
|
1067
|
+
*
|
|
1068
|
+
* @param input - The document, its kind and its compiled parts.
|
|
1069
|
+
* @returns The code that runs the document.
|
|
1070
|
+
*/
|
|
1071
|
+
readonly compileRun: (input: CompileInput) => RunCode;
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* A runner: the code of a kind whose parts or effects can wait. The same runner serves an evaluation, which does
|
|
1075
|
+
* not wait at any time, and a run, in which the host does each effect.
|
|
1076
|
+
*/
|
|
1077
|
+
type Runner = RunFn | CompiledRunner;
|
|
674
1078
|
/**
|
|
675
|
-
* A
|
|
676
|
-
*
|
|
1079
|
+
* A machine: a world of kinds, and the code of each evaluator name, of each `deref` and of each walker.
|
|
1080
|
+
*
|
|
1081
|
+
* - `maxDepth`: the deepest nesting of documents. The default is 1000.
|
|
1082
|
+
* - `permits`: the rule of the host for an effect. The default is the function `permits` on the capabilities.
|
|
677
1083
|
*/
|
|
678
|
-
type Runner = (input: RunInput) => Steps;
|
|
679
|
-
/** A machine: a world of kinds, and the code of each evaluator name, of each `deref` and of each walker. */
|
|
680
1084
|
interface Machine extends Navigator {
|
|
681
1085
|
readonly evaluators: Readonly<Record<string, Evaluator>>;
|
|
682
1086
|
/** The runners, by evaluator name. A runner wins over an evaluator of the same name. */
|
|
683
1087
|
readonly runners?: Readonly<Record<string, Runner>>;
|
|
684
|
-
/** The deepest nesting of evaluations. The default is 200. */
|
|
685
1088
|
readonly maxDepth?: number;
|
|
1089
|
+
readonly permits?: (caps: readonly Cap[], kind: readonly Key[], context: Context) => boolean;
|
|
686
1090
|
}
|
|
687
|
-
/** An effect that an evaluation caused, in the order of the effects. */
|
|
688
|
-
interface Effect {
|
|
689
|
-
readonly index: number;
|
|
690
|
-
readonly kind: string;
|
|
691
|
-
readonly payload: Json;
|
|
692
|
-
/** The ID of the trace node that caused the effect. */
|
|
693
|
-
readonly node: number;
|
|
694
|
-
/** The number of events before the effect. The effect comes before the end of its node. */
|
|
695
|
-
readonly at: number;
|
|
696
|
-
}
|
|
697
|
-
/** The host side of a run: it does one effect, and gives its result. A throw or a rejection is an error value. */
|
|
698
|
-
type Perform = (effect: Effect) => Json | Promise<Json>;
|
|
699
|
-
/** The options of a run. */
|
|
700
|
-
interface RunOptions {
|
|
701
|
-
/** The host code that does each effect. Without it, an effect gives its payload, as in an evaluation. */
|
|
702
|
-
readonly perform?: Perform;
|
|
703
|
-
/** The type of a bare root value. */
|
|
704
|
-
readonly declaredType?: string;
|
|
705
|
-
}
|
|
706
|
-
/**
|
|
707
|
-
* This function tells if the capabilities permit an effect. A capability permits the kind of its name, and each kind
|
|
708
|
-
* in its namespace: `store` permits `store.update`.
|
|
709
|
-
*
|
|
710
|
-
* @param caps - The capabilities of the context.
|
|
711
|
-
* @param kind - The kind of the effect.
|
|
712
|
-
* @returns `true` when one capability permits it.
|
|
713
|
-
*/
|
|
714
|
-
export declare function permits(caps: readonly string[], kind: string): boolean;
|
|
715
1091
|
/** One evaluation in a trace. The children are the evaluations of its parts, in the order that they started. */
|
|
716
1092
|
interface TraceNode {
|
|
717
1093
|
readonly id: number;
|
|
718
|
-
/** The keys from the root document to this document. */
|
|
1094
|
+
/** The keys from the root document to this document, as texts. */
|
|
719
1095
|
readonly path: readonly string[];
|
|
720
1096
|
/** The document. A bare value has the type that its position declares. */
|
|
721
1097
|
readonly doc: Envelope;
|
|
@@ -727,9 +1103,9 @@ interface TraceNode {
|
|
|
727
1103
|
/** The names that the document could read when its evaluation started. */
|
|
728
1104
|
readonly names: readonly string[];
|
|
729
1105
|
/** The names and their values when the evaluation started. */
|
|
730
|
-
readonly scope: Readonly<Record<string,
|
|
1106
|
+
readonly scope: Readonly<Record<string, unknown>>;
|
|
731
1107
|
/** The capabilities that the document had when its evaluation started. */
|
|
732
|
-
readonly caps: readonly
|
|
1108
|
+
readonly caps: readonly Cap[];
|
|
733
1109
|
/** A short text for the root `self` when the evaluation started. */
|
|
734
1110
|
readonly self: string;
|
|
735
1111
|
/** The time of the start. Each start and each end takes one tick. */
|
|
@@ -749,6 +1125,78 @@ interface Run {
|
|
|
749
1125
|
/** The number of ticks. */
|
|
750
1126
|
readonly time: number;
|
|
751
1127
|
}
|
|
1128
|
+
/** A signal of the host that stops a run: the shape of the standard `AbortSignal`. */
|
|
1129
|
+
interface StopSignal {
|
|
1130
|
+
readonly aborted: boolean;
|
|
1131
|
+
readonly reason?: unknown;
|
|
1132
|
+
addEventListener(type: "abort", listener: () => void, options?: {
|
|
1133
|
+
readonly once?: boolean;
|
|
1134
|
+
}): void;
|
|
1135
|
+
}
|
|
1136
|
+
/** The options of a run. */
|
|
1137
|
+
interface RunOptions {
|
|
1138
|
+
/** The host code that does each effect. Without it, an effect gives its payload, as in an evaluation. */
|
|
1139
|
+
readonly perform?: Perform;
|
|
1140
|
+
/** The type of a bare root value. */
|
|
1141
|
+
readonly declaredType?: string;
|
|
1142
|
+
/** A signal of the host. When it aborts, the run stops with `#ABORT`. */
|
|
1143
|
+
readonly signal?: StopSignal;
|
|
1144
|
+
}
|
|
1145
|
+
/** The options of an evaluation of a plan. */
|
|
1146
|
+
interface EvalOptions {
|
|
1147
|
+
/** A list that gets each effect, in its order. */
|
|
1148
|
+
readonly effects?: Effect[];
|
|
1149
|
+
}
|
|
1150
|
+
/** The options of a compilation. */
|
|
1151
|
+
interface PrepareOptions {
|
|
1152
|
+
/** The type of a bare root value. */
|
|
1153
|
+
readonly declaredType?: string;
|
|
1154
|
+
/** `true`: each evaluation records a trace. The default is `false`. */
|
|
1155
|
+
readonly trace?: boolean;
|
|
1156
|
+
}
|
|
1157
|
+
/** A compiled document. It evaluates many times, and it does the work of the ladder one time. */
|
|
1158
|
+
interface Plan {
|
|
1159
|
+
readonly machine: Machine;
|
|
1160
|
+
/** `true` when each evaluation records a trace. */
|
|
1161
|
+
readonly traced: boolean;
|
|
1162
|
+
/** The compiled root document. */
|
|
1163
|
+
readonly root: CompiledPart;
|
|
1164
|
+
/**
|
|
1165
|
+
* This function evaluates the document. It does not wait, and the host does no effect.
|
|
1166
|
+
*
|
|
1167
|
+
* @param context - The names and the capabilities at the root.
|
|
1168
|
+
* @param options - A list that gets the effects.
|
|
1169
|
+
* @returns The result.
|
|
1170
|
+
*/
|
|
1171
|
+
evaluate(context?: Context, options?: EvalOptions): Result;
|
|
1172
|
+
/**
|
|
1173
|
+
* This function runs the document: the host does each effect, and the run waits when the host waits.
|
|
1174
|
+
*
|
|
1175
|
+
* @param context - The names and the capabilities at the root.
|
|
1176
|
+
* @param options - The host code that does the effects, and a signal to stop the run.
|
|
1177
|
+
* @returns The result, when the run ends.
|
|
1178
|
+
*/
|
|
1179
|
+
run(context?: Context, options?: RunOptions): Promise<Result>;
|
|
1180
|
+
}
|
|
1181
|
+
/**
|
|
1182
|
+
* This function compiles a document into a plan. The ladder, the kinds and the parts are known after it. A plan
|
|
1183
|
+
* evaluates many times. A document that does not compile gives a plan that fails with the reason, when it evaluates.
|
|
1184
|
+
*
|
|
1185
|
+
* @param machine - The world and the code of the evaluators.
|
|
1186
|
+
* @param doc - The document, as JSON.
|
|
1187
|
+
* @param options - The type of a bare root value, and a trace.
|
|
1188
|
+
* @returns The plan.
|
|
1189
|
+
*/
|
|
1190
|
+
export declare function prepare(machine: Machine, doc: unknown, options?: PrepareOptions): Plan;
|
|
1191
|
+
/**
|
|
1192
|
+
* This function makes a cache of plans for one machine. A document object compiles one time. Use it only for
|
|
1193
|
+
* documents that do not change after they are made.
|
|
1194
|
+
*
|
|
1195
|
+
* @param machine - The machine.
|
|
1196
|
+
* @param options - The options of each compilation.
|
|
1197
|
+
* @returns A function that gives the plan of a document.
|
|
1198
|
+
*/
|
|
1199
|
+
export declare function planner(machine: Machine, options?: PrepareOptions): (doc: unknown) => Plan;
|
|
752
1200
|
/**
|
|
753
1201
|
* This function evaluates a document, and records each evaluation in a trace.
|
|
754
1202
|
*
|
|
@@ -762,19 +1210,19 @@ interface Run {
|
|
|
762
1210
|
* @param declaredType - The type of a bare root value.
|
|
763
1211
|
* @returns The result, the trace and the effects in their order.
|
|
764
1212
|
*/
|
|
765
|
-
export declare function evaluate(machine: Machine, doc:
|
|
1213
|
+
export declare function evaluate(machine: Machine, doc: unknown, context?: Context, declaredType?: string): Run;
|
|
766
1214
|
/**
|
|
767
1215
|
* This function runs a document: an evaluation in which the host does each effect. A runner waits for its
|
|
768
1216
|
* parts and for its effects, thus a step can read the result of the step before. The part of the run before the
|
|
769
|
-
* first wait happens at once, in the call.
|
|
1217
|
+
* first wait happens at once, in the call. The run records a trace.
|
|
770
1218
|
*
|
|
771
1219
|
* @param machine - The world, the evaluators and the runners.
|
|
772
1220
|
* @param doc - The document, as JSON.
|
|
773
1221
|
* @param context - The names and the capabilities at the root.
|
|
774
|
-
* @param options - The host code that does the effects,
|
|
1222
|
+
* @param options - The host code that does the effects, the type of a bare root value, and a signal.
|
|
775
1223
|
* @returns The result, the trace and the effects in their order, when the run ends.
|
|
776
1224
|
*/
|
|
777
|
-
export declare function run(machine: Machine, doc:
|
|
1225
|
+
export declare function run(machine: Machine, doc: unknown, context?: Context, options?: RunOptions): Promise<Run>;
|
|
778
1226
|
/**
|
|
779
1227
|
* This function finds a node of a trace by its path.
|
|
780
1228
|
*
|
|
@@ -782,7 +1230,7 @@ export declare function run(machine: Machine, doc: Json, context?: Context, opti
|
|
|
782
1230
|
* @param path - The keys from the root.
|
|
783
1231
|
* @returns The first evaluation at that path, or `undefined` when the document did not evaluate.
|
|
784
1232
|
*/
|
|
785
|
-
export declare function nodeAt(trace: TraceNode, path: readonly
|
|
1233
|
+
export declare function nodeAt(trace: TraceNode, path: readonly Key[]): TraceNode | undefined;
|
|
786
1234
|
/**
|
|
787
1235
|
* This function lists the nodes of a trace in the order of their start.
|
|
788
1236
|
*
|
|
@@ -790,6 +1238,14 @@ export declare function nodeAt(trace: TraceNode, path: readonly string[]): Trace
|
|
|
790
1238
|
* @returns Each node, the root first.
|
|
791
1239
|
*/
|
|
792
1240
|
export declare function flatten(trace: TraceNode): readonly TraceNode[];
|
|
1241
|
+
/**
|
|
1242
|
+
* This function lists the errors of a trace: each node whose result is an error and whose parts did not fail
|
|
1243
|
+
* the same way. These nodes are where each error started.
|
|
1244
|
+
*
|
|
1245
|
+
* @param trace - The root of the trace.
|
|
1246
|
+
* @returns The errors, in the order of their start.
|
|
1247
|
+
*/
|
|
1248
|
+
export declare function failures(trace: TraceNode): readonly EvalError[];
|
|
793
1249
|
//#endregion
|
|
794
1250
|
//#region src/stdlib.d.ts
|
|
795
1251
|
/**
|
|
@@ -799,6 +1255,28 @@ export declare function flatten(trace: TraceNode): readonly TraceNode[];
|
|
|
799
1255
|
export declare const STD_WORLD: World;
|
|
800
1256
|
/** The code of the evaluators of the standard world, by name. */
|
|
801
1257
|
export declare const STD_EVALUATORS: Readonly<Record<string, Evaluator>>;
|
|
1258
|
+
/** The options of a walker `nearest`. */
|
|
1259
|
+
interface NearestOptions {
|
|
1260
|
+
/**
|
|
1261
|
+
* This function tells if a node has the wanted type. The default compares the field `type` of a host thing.
|
|
1262
|
+
*
|
|
1263
|
+
* @param node - The node that the walk reached.
|
|
1264
|
+
* @param wanted - The type that the step document asks for.
|
|
1265
|
+
* @returns `true` when the node has the type.
|
|
1266
|
+
*/
|
|
1267
|
+
readonly matches?: (node: Thing, wanted: Json | undefined) => boolean;
|
|
1268
|
+
/** The most hops of one walk. The default is 256. */
|
|
1269
|
+
readonly maxHops?: number;
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* This function makes a walker `nearest`. The walker follows a key (the default is `parent`) until the node that it
|
|
1273
|
+
* reaches has the wanted type. With `self: true`, the node where the walk starts is the first candidate. A node
|
|
1274
|
+
* without that key ends the walk with an error.
|
|
1275
|
+
*
|
|
1276
|
+
* @param options - The test of a type and the most hops.
|
|
1277
|
+
* @returns The walker.
|
|
1278
|
+
*/
|
|
1279
|
+
export declare function nearestWalker(options?: NearestOptions): Walker;
|
|
802
1280
|
/** The walkers of the standard world, by name. */
|
|
803
1281
|
export declare const STD_WALKERS: Readonly<Record<string, Walker>>;
|
|
804
1282
|
/**
|
|
@@ -869,7 +1347,7 @@ export declare function layers(steps: readonly StepSpec[]): readonly (readonly s
|
|
|
869
1347
|
//#endregion
|
|
870
1348
|
//#region src/context.d.ts
|
|
871
1349
|
/** The parts of a context. A term can reach outside itself only through these parts. */
|
|
872
|
-
export declare const PARTS: readonly ["
|
|
1350
|
+
export declare const PARTS: readonly ["self", "names", "world", "event", "caps"];
|
|
873
1351
|
/** One part of a context. */
|
|
874
1352
|
type Part = (typeof PARTS)[number];
|
|
875
1353
|
/** The description of one part of a context. */
|
|
@@ -912,5 +1390,211 @@ interface Construct {
|
|
|
912
1390
|
/** Six constructs that show the range of the context. */
|
|
913
1391
|
export declare const CONSTRUCTS: readonly Construct[];
|
|
914
1392
|
//#endregion
|
|
915
|
-
|
|
1393
|
+
//#region src/static.d.ts
|
|
1394
|
+
/** One document that a walk visits. */
|
|
1395
|
+
interface DocVisit {
|
|
1396
|
+
/** The keys from the root document to this document, through the parts. It is the place of an error. */
|
|
1397
|
+
readonly place: readonly Key[];
|
|
1398
|
+
/** The keys from the root JSON value to this document. A change at this path changes this document. */
|
|
1399
|
+
readonly at: readonly Key[];
|
|
1400
|
+
/** The document. A bare value has the type that its position declares. */
|
|
1401
|
+
readonly doc: Envelope;
|
|
1402
|
+
/** `true` when the type came from the position and not from an envelope. */
|
|
1403
|
+
readonly declared: boolean;
|
|
1404
|
+
/** The kind of the document, when the world has it. */
|
|
1405
|
+
readonly kind?: Kind;
|
|
1406
|
+
/** The keys of the kind after each alias. */
|
|
1407
|
+
readonly typePath?: readonly Key[];
|
|
1408
|
+
/** The number of documents around this document. The root has 0. */
|
|
1409
|
+
readonly depth: number;
|
|
1410
|
+
/** The names that the kinds around this document bind for it. */
|
|
1411
|
+
readonly names: ReadonlySet<string>;
|
|
1412
|
+
/** The names that a sequence around this document binds only for the parts after it. */
|
|
1413
|
+
readonly later: ReadonlySet<string>;
|
|
1414
|
+
}
|
|
1415
|
+
/** A position that a walk cannot read as a document. */
|
|
1416
|
+
interface WalkProblem {
|
|
1417
|
+
readonly place: readonly Key[];
|
|
1418
|
+
readonly at: readonly Key[];
|
|
1419
|
+
readonly code: "#PART?" | "#VALUE!";
|
|
1420
|
+
readonly reason: string;
|
|
1421
|
+
}
|
|
1422
|
+
/** The options of a walk. */
|
|
1423
|
+
interface WalkOptions {
|
|
1424
|
+
/** The type of a bare root value. */
|
|
1425
|
+
readonly declaredType?: string;
|
|
1426
|
+
/** The names that the host binds at the root. */
|
|
1427
|
+
readonly names?: readonly string[];
|
|
1428
|
+
/**
|
|
1429
|
+
* This function gets each position that is not a document.
|
|
1430
|
+
*
|
|
1431
|
+
* @param problem - The position and the reason.
|
|
1432
|
+
*/
|
|
1433
|
+
readonly problem?: (problem: WalkProblem) => void;
|
|
1434
|
+
}
|
|
1435
|
+
/**
|
|
1436
|
+
* This function visits each document of a document: the root and each part at each depth, by the contracts of the
|
|
1437
|
+
* kinds. A part of a kind that the world does not have is not visited. The walk uses no stack of the host for the
|
|
1438
|
+
* depth, thus a deep document cannot overflow it.
|
|
1439
|
+
*
|
|
1440
|
+
* @param world - The world of the kinds.
|
|
1441
|
+
* @param doc - The document, as JSON.
|
|
1442
|
+
* @param visit - The function that gets each document. It gives `"skip"` to not visit the parts of a document.
|
|
1443
|
+
* @param options - The type of a bare root value, the names of the host, and a function for the problems.
|
|
1444
|
+
*/
|
|
1445
|
+
export declare function walk(world: World, doc: unknown, visit: (visit: DocVisit) => void | "skip", options?: WalkOptions): void;
|
|
1446
|
+
/** One reference of a document. */
|
|
1447
|
+
interface RefUse {
|
|
1448
|
+
/** The place of the reference document, through the parts. */
|
|
1449
|
+
readonly place: readonly Key[];
|
|
1450
|
+
/** The keys from the root JSON value to the reference document. */
|
|
1451
|
+
readonly at: readonly Key[];
|
|
1452
|
+
/** The path of the reference: its root and its steps. */
|
|
1453
|
+
readonly path: readonly Json[];
|
|
1454
|
+
readonly root: string;
|
|
1455
|
+
/** `false` when a kind around the reference binds its first name. A reference to another root is free. */
|
|
1456
|
+
readonly free: boolean;
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* This function finds each reference of a document: each document of the type `ref` that a walk reaches. A
|
|
1460
|
+
* reference to a name that a kind around it binds is not free. The others are what the document reads from
|
|
1461
|
+
* outside: its dependencies.
|
|
1462
|
+
*
|
|
1463
|
+
* @param world - The world of the kinds.
|
|
1464
|
+
* @param doc - The document, as JSON.
|
|
1465
|
+
* @param options - The type of a bare root value. The names of the host do not change the result.
|
|
1466
|
+
* @returns The references, in the order of the document.
|
|
1467
|
+
*/
|
|
1468
|
+
export declare function refsOf(world: World, doc: unknown, options?: WalkOptions): readonly RefUse[];
|
|
1469
|
+
/**
|
|
1470
|
+
* This function changes the free references of a document. The document is not changed: the function gives a new
|
|
1471
|
+
* document that shares each part without a change.
|
|
1472
|
+
*
|
|
1473
|
+
* @param world - The world of the kinds.
|
|
1474
|
+
* @param doc - The document, as JSON.
|
|
1475
|
+
* @param change - The function that gives the new document of a reference, or `undefined` to keep it.
|
|
1476
|
+
* @param options - The type of a bare root value, and the names that the host binds at the root.
|
|
1477
|
+
* @returns The new document.
|
|
1478
|
+
*/
|
|
1479
|
+
export declare function mapRefs(world: World, doc: unknown, change: (use: RefUse) => unknown, options?: WalkOptions): unknown;
|
|
1480
|
+
/** A problem that a check finds before a document runs. */
|
|
1481
|
+
interface Issue {
|
|
1482
|
+
readonly code: ErrorCode;
|
|
1483
|
+
/** The keys from the root document to the document with the problem. */
|
|
1484
|
+
readonly place: readonly Key[];
|
|
1485
|
+
readonly reason: string;
|
|
1486
|
+
}
|
|
1487
|
+
/** The options of a check. */
|
|
1488
|
+
interface CheckOptions {
|
|
1489
|
+
/** The type of a bare root value. */
|
|
1490
|
+
readonly declaredType?: string;
|
|
1491
|
+
/** The names that the host binds at the root. `"any"` does not check the names. The default is no names. */
|
|
1492
|
+
readonly names?: readonly string[] | "any";
|
|
1493
|
+
/** The capabilities at the root. Without them, the check does not check the effects. */
|
|
1494
|
+
readonly caps?: readonly Cap[];
|
|
1495
|
+
}
|
|
1496
|
+
/**
|
|
1497
|
+
* This function checks a document before an evaluation, by the contracts of its kinds. It finds the problems
|
|
1498
|
+
* that an evaluation finds when it reaches them:
|
|
1499
|
+
*
|
|
1500
|
+
* - `#KIND?`: a kind that the world does not have.
|
|
1501
|
+
* - `#NOEVAL`: no evaluator.
|
|
1502
|
+
* - `#VALUE!`: a value without the shape of its kind.
|
|
1503
|
+
* - `#PART?`: a value without a type.
|
|
1504
|
+
* - `#DEPTH!`: too much nesting.
|
|
1505
|
+
* - `#REF!` and `#STEP?`: a bad path.
|
|
1506
|
+
* - `#NAME?`: a name that nothing binds.
|
|
1507
|
+
* - `#CAP!`: an effect that the capabilities do not permit.
|
|
1508
|
+
*
|
|
1509
|
+
* @param machine - The world and the code of the evaluators.
|
|
1510
|
+
* @param doc - The document, as JSON.
|
|
1511
|
+
* @param options - The type of a bare root value, the names of the host and the capabilities.
|
|
1512
|
+
* @returns The problems, in the order of the document.
|
|
1513
|
+
*/
|
|
1514
|
+
export declare function check(machine: Machine, doc: unknown, options?: CheckOptions): readonly Issue[];
|
|
1515
|
+
//#endregion
|
|
1516
|
+
//#region src/formula.d.ts
|
|
1517
|
+
/** An operation of a formula language: a function of the values of its arguments. */
|
|
1518
|
+
type OpFn = (...args: never[]) => unknown;
|
|
1519
|
+
/**
|
|
1520
|
+
* A lazy operation: it gets one function for each argument. It evaluates only the arguments that it needs, for
|
|
1521
|
+
* example a default for an error.
|
|
1522
|
+
*/
|
|
1523
|
+
type LazyOp = (args: readonly (() => Result)[]) => Result;
|
|
1524
|
+
/** The code of an extension: it gives the value of the data of an extension document. */
|
|
1525
|
+
type ExtFn = (data: Json) => Result;
|
|
1526
|
+
/**
|
|
1527
|
+
* The rules of one formula language: its operations, and the rules that differ between languages.
|
|
1528
|
+
*
|
|
1529
|
+
* - `ops`: the operations of the namespace `ops`, by their name (the keys after `ops`, with dots between them).
|
|
1530
|
+
* - `lazy`: the operations that evaluate their arguments themselves.
|
|
1531
|
+
* - `ext`: the extensions of the namespace `ext`, by their name. The kit has `ext.undefined` and `ext.number`.
|
|
1532
|
+
* - `truthy`: the truth of a value for `if`, `and` and `or`. `undefined` is an error `#VALUE!`. The default
|
|
1533
|
+
* accepts only `true` and `false`.
|
|
1534
|
+
* - `otherwise`: the value of an `if` without `else`. The default is `null`.
|
|
1535
|
+
* - `logic`: what `and` and `or` give: a truth value (the default), or the value that decided.
|
|
1536
|
+
* - `omit`: the errors that leave a field out of a `record`, as an absent value.
|
|
1537
|
+
* - `missing`: the values that a reference cannot give. Such a value gives `#N/A`.
|
|
1538
|
+
* - `check`: a check of the value of an operation, for example "a number must be finite".
|
|
1539
|
+
*/
|
|
1540
|
+
interface FormulaProfile {
|
|
1541
|
+
readonly ops?: Readonly<Record<string, OpFn>> | ((name: string) => OpFn | undefined);
|
|
1542
|
+
readonly lazy?: Readonly<Record<string, LazyOp>>;
|
|
1543
|
+
readonly ext?: Readonly<Record<string, ExtFn>>;
|
|
1544
|
+
readonly truthy?: (value: unknown) => boolean | undefined;
|
|
1545
|
+
readonly otherwise?: unknown;
|
|
1546
|
+
readonly logic?: "boolean" | "value";
|
|
1547
|
+
readonly omit?: (error: EvalError) => boolean;
|
|
1548
|
+
readonly missing?: (value: unknown) => boolean;
|
|
1549
|
+
readonly check?: (value: unknown, op: string) => Result | undefined;
|
|
1550
|
+
}
|
|
1551
|
+
/** The value of a name whose binding failed. A reference to the name gives the error. */
|
|
1552
|
+
export declare class Held {
|
|
1553
|
+
readonly error: EvalError;
|
|
1554
|
+
constructor(error: EvalError);
|
|
1555
|
+
}
|
|
1556
|
+
declare const CLOSURE: unique symbol;
|
|
1557
|
+
/** A function that the construct `fn` made. It throws a `Failure` when its body fails. */
|
|
1558
|
+
type Closure = ((...args: readonly unknown[]) => unknown) & {
|
|
1559
|
+
readonly [CLOSURE]: {
|
|
1560
|
+
readonly params: readonly string[];
|
|
1561
|
+
};
|
|
1562
|
+
};
|
|
1563
|
+
/**
|
|
1564
|
+
* This function tells if a value is a function that the construct `fn` made.
|
|
1565
|
+
*
|
|
1566
|
+
* @param value - The value.
|
|
1567
|
+
* @returns `true` for a closure.
|
|
1568
|
+
*/
|
|
1569
|
+
export declare function isClosure(value: unknown): value is Closure;
|
|
1570
|
+
/** The standard extensions: the values that JSON cannot write. */
|
|
1571
|
+
export declare const STD_EXT: Readonly<Record<string, ExtFn>>;
|
|
1572
|
+
/** The truth of a value in the rules of JavaScript: `0`, `""`, `null`, `undefined` and `NaN` are false. */
|
|
1573
|
+
export declare function jsTruthy(value: unknown): boolean;
|
|
1574
|
+
/**
|
|
1575
|
+
* This function makes the kinds of the formula kit, for a world. The kinds are the shared constructs of D13 and
|
|
1576
|
+
* the namespaces `ops` and `ext`. A bare value in a position of a term is a literal.
|
|
1577
|
+
*
|
|
1578
|
+
* @returns The entries of the world, by their key.
|
|
1579
|
+
*/
|
|
1580
|
+
export declare function formulaKinds(): Readonly<Record<string, WorldEntry>>;
|
|
1581
|
+
/**
|
|
1582
|
+
* This function makes the evaluators of the formula kit for one language. Each evaluator compiles its document
|
|
1583
|
+
* one time into code.
|
|
1584
|
+
*
|
|
1585
|
+
* @param profile - The operations and the rules of the language.
|
|
1586
|
+
* @returns The evaluators, by their name.
|
|
1587
|
+
*/
|
|
1588
|
+
export declare function formulaEvaluators(profile?: FormulaProfile): Readonly<Record<string, Evaluator>>;
|
|
1589
|
+
/**
|
|
1590
|
+
* This function makes a machine for a formula language. The machine has the kinds and the evaluators of the
|
|
1591
|
+
* formula kit. It also has the kinds, the evaluators and the walkers that the language adds.
|
|
1592
|
+
*
|
|
1593
|
+
* @param profile - The operations and the rules of the language.
|
|
1594
|
+
* @param more - The parts of a machine that the language adds. Its kinds win over the kinds of the kit.
|
|
1595
|
+
* @returns The machine.
|
|
1596
|
+
*/
|
|
1597
|
+
export declare function formulaMachine(profile?: FormulaProfile, more?: Partial<Machine>): Machine;
|
|
1598
|
+
//#endregion
|
|
1599
|
+
export type { Alias, Answer, AskOptions, Cap, CheckOptions, Classification, Closure, Code, CompileInput, CompiledEvaluator, CompiledPart, CompiledRunner, Construct, Context, Part as ContextPart, DerefCode, DocVisit, Edge, Effect, Envelope, ErrorCode, EvalError, EvalFn, EvalInput, EvalOptions, Evaluator, ExtFn, Failed, FormulaProfile, Give, Header, Hop, HopBy, HostThing, Issue, Json, JsonRecord, KernelCode, Key, Kind, KindSource, LazyOp, Lookup, Machine, NamesSource, Namespace, Navigator, NearestOptions, OpFn, OrderProblem, Outcome, Part$1 as Part, PartInfo, Shape as PartShape, PartSlot, PartsSpec, Perform, Plan, PrepareOptions, RefUse, Resolution, ResolveCode, ResolveOptions, Result, Root, Run, RunCode, RunFn, RunInput, RunOptions, RunPart, Rung, Runner, Runtime, ScopeRule, Shape$1 as Shape, Side, Split, Step, StepSpec, Steps, StopSignal, Stored, Thing, TraceNode, TypeRef, Use, WalkInput, WalkOptions, WalkProblem, WalkResult, Walker, World, WorldEntry };
|
|
916
1600
|
//# sourceMappingURL=index.d.ts.map
|