@crubal/engine 0.0.0-stage → 0.4.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 +202 -0
- package/README.md +97 -2
- package/dist/bin/crubal.d.ts +2 -0
- package/dist/bin/crubal.js +103 -0
- package/dist/bin/crubal.js.map +1 -0
- package/dist/src/access.d.ts +149 -0
- package/dist/src/access.js +584 -0
- package/dist/src/access.js.map +1 -0
- package/dist/src/acts.d.ts +531 -0
- package/dist/src/acts.js +1466 -0
- package/dist/src/acts.js.map +1 -0
- package/dist/src/address.d.ts +29 -0
- package/dist/src/address.js +59 -0
- package/dist/src/address.js.map +1 -0
- package/dist/src/arrival.d.ts +55 -0
- package/dist/src/arrival.js +127 -0
- package/dist/src/arrival.js.map +1 -0
- package/dist/src/browser.d.ts +10 -0
- package/dist/src/browser.js +13 -0
- package/dist/src/browser.js.map +1 -0
- package/dist/src/bundle.d.ts +74 -0
- package/dist/src/bundle.js +1108 -0
- package/dist/src/bundle.js.map +1 -0
- package/dist/src/canonical.d.ts +6 -0
- package/dist/src/canonical.js +47 -0
- package/dist/src/canonical.js.map +1 -0
- package/dist/src/carriers.d.ts +7 -0
- package/dist/src/carriers.js +33 -0
- package/dist/src/carriers.js.map +1 -0
- package/dist/src/conditions.d.ts +28 -0
- package/dist/src/conditions.js +227 -0
- package/dist/src/conditions.js.map +1 -0
- package/dist/src/corpus.d.ts +8 -0
- package/dist/src/corpus.js +1207 -0
- package/dist/src/corpus.js.map +1 -0
- package/dist/src/defs.d.ts +174 -0
- package/dist/src/defs.js +690 -0
- package/dist/src/defs.js.map +1 -0
- package/dist/src/dictionary.d.ts +77 -0
- package/dist/src/dictionary.js +629 -0
- package/dist/src/dictionary.js.map +1 -0
- package/dist/src/exchange.d.ts +259 -0
- package/dist/src/exchange.js +1088 -0
- package/dist/src/exchange.js.map +1 -0
- package/dist/src/forms.d.ts +16 -0
- package/dist/src/forms.js +181 -0
- package/dist/src/forms.js.map +1 -0
- package/dist/src/index.d.ts +32 -0
- package/dist/src/index.js +34 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lex.d.ts +21 -0
- package/dist/src/lex.js +142 -0
- package/dist/src/lex.js.map +1 -0
- package/dist/src/lifecycle.d.ts +92 -0
- package/dist/src/lifecycle.js +451 -0
- package/dist/src/lifecycle.js.map +1 -0
- package/dist/src/log.d.ts +91 -0
- package/dist/src/log.js +629 -0
- package/dist/src/log.js.map +1 -0
- package/dist/src/migrate.d.ts +129 -0
- package/dist/src/migrate.js +415 -0
- package/dist/src/migrate.js.map +1 -0
- package/dist/src/model.d.ts +81 -0
- package/dist/src/model.js +188 -0
- package/dist/src/model.js.map +1 -0
- package/dist/src/packages.d.ts +372 -0
- package/dist/src/packages.js +1158 -0
- package/dist/src/packages.js.map +1 -0
- package/dist/src/print-text.d.ts +7 -0
- package/dist/src/print-text.js +223 -0
- package/dist/src/print-text.js.map +1 -0
- package/dist/src/publish.d.ts +27 -0
- package/dist/src/publish.js +279 -0
- package/dist/src/publish.js.map +1 -0
- package/dist/src/read-text.d.ts +10 -0
- package/dist/src/read-text.js +400 -0
- package/dist/src/read-text.js.map +1 -0
- package/dist/src/records.d.ts +32 -0
- package/dist/src/records.js +104 -0
- package/dist/src/records.js.map +1 -0
- package/dist/src/review.d.ts +22 -0
- package/dist/src/review.js +198 -0
- package/dist/src/review.js.map +1 -0
- package/dist/src/rules.d.ts +101 -0
- package/dist/src/rules.js +670 -0
- package/dist/src/rules.js.map +1 -0
- package/dist/src/schedule.d.ts +24 -0
- package/dist/src/schedule.js +126 -0
- package/dist/src/schedule.js.map +1 -0
- package/dist/src/std-packages.d.ts +9 -0
- package/dist/src/std-packages.js +268 -0
- package/dist/src/std-packages.js.map +1 -0
- package/dist/src/store.d.ts +191 -0
- package/dist/src/store.js +671 -0
- package/dist/src/store.js.map +1 -0
- package/dist/src/surface.d.ts +47 -0
- package/dist/src/surface.js +92 -0
- package/dist/src/surface.js.map +1 -0
- package/dist/src/tree.d.ts +35 -0
- package/dist/src/tree.js +159 -0
- package/dist/src/tree.js.map +1 -0
- package/dist/src/units-table.d.ts +1 -0
- package/dist/src/units-table.js +200 -0
- package/dist/src/units-table.js.map +1 -0
- package/dist/src/units.d.ts +42 -0
- package/dist/src/units.js +98 -0
- package/dist/src/units.js.map +1 -0
- package/dist/src/versions.d.ts +10 -0
- package/dist/src/versions.js +27 -0
- package/dist/src/versions.js.map +1 -0
- package/dist/src/views.d.ts +372 -0
- package/dist/src/views.js +1170 -0
- package/dist/src/views.js.map +1 -0
- package/package.json +60 -4
package/dist/src/acts.js
ADDED
|
@@ -0,0 +1,1466 @@
|
|
|
1
|
+
// Acts as declarations (chapter 15, revisions 25 and 26): an act a utility provides, said in the package's words.
|
|
2
|
+
// What level it acts on, what it holds beside the record (context kinds), the row a route takes through a rack
|
|
3
|
+
// of rules on a record, what it finds (named records of a kind by picks and an order) and mints when a find
|
|
4
|
+
// comes up empty, what it sets on the record in hand, what it then sets on the records it found when a condition
|
|
5
|
+
// holds (and sends then), what it sends through a transport the app provides, and when it counts as landed. The
|
|
6
|
+
// engine reads, checks and carries it, and answers each step pure: the row taken, the picks as they stand, the
|
|
7
|
+
// values a mint makes, the writes a landing makes; the app runs the finds and counts, makes the mints and the
|
|
8
|
+
// writes and the sends, and stamps the lane (12.11) when the act lands.
|
|
9
|
+
import { evaluate, parseCondition } from "./conditions.js";
|
|
10
|
+
import { serialize } from "./canonical.js";
|
|
11
|
+
import { CrubalError } from "./tree.js";
|
|
12
|
+
import { unquote } from "./defs.js";
|
|
13
|
+
import { checkScalar } from "./dictionary.js";
|
|
14
|
+
const WORD = /^[a-z][\w-]*$/;
|
|
15
|
+
// ─── Reading ──────────────────────────────────────────────────────────────────
|
|
16
|
+
function readRef(t, line, what, routes = new Set()) {
|
|
17
|
+
let m = t.match(/^record\.([\w:]+)$/);
|
|
18
|
+
if (m)
|
|
19
|
+
return { record: m[1] };
|
|
20
|
+
// Revision 44: `given.<name>`, what the act was given.
|
|
21
|
+
m = t.match(/^given\.([\w-]+)$/);
|
|
22
|
+
if (m)
|
|
23
|
+
return { given: m[1] };
|
|
24
|
+
m = t.match(/^context\.([\w-]+)(?:\.([\w:]+))?$/);
|
|
25
|
+
if (m)
|
|
26
|
+
return m[2] ? { context: m[1], facet: m[2] } : { context: m[1] };
|
|
27
|
+
m = t.match(/^([\w-]+)\.([\w:]+)$/);
|
|
28
|
+
if (!m)
|
|
29
|
+
throw new CrubalError(`${what}: ${t} is not record.<facet>, context.<kind>[.<facet>], <found>[.<facet>] or <route>.<column>`, line);
|
|
30
|
+
return routes.has(m[1]) ? { route: m[1], column: m[2] } : { found: m[1], facet: m[2] };
|
|
31
|
+
}
|
|
32
|
+
const isRef = (t) => /^(record|context|[\w-]+)\.[\w:.-]+$/.test(t) && !/\s/.test(t);
|
|
33
|
+
/** A pick's value: a reference, a found record's bare name (its id, revision 26), or a literal. */
|
|
34
|
+
function readPickValue(t, line, routes, founds = new Set()) {
|
|
35
|
+
if (founds.has(t))
|
|
36
|
+
return { found: t };
|
|
37
|
+
// Revision 35: the person running the act, as `link caller` is in a set.
|
|
38
|
+
if (t === "caller")
|
|
39
|
+
return { caller: true };
|
|
40
|
+
// Revision 39: the record in hand itself, its id (`of = record`, `heldIn = record`).
|
|
41
|
+
if (t === "record")
|
|
42
|
+
return { self: true };
|
|
43
|
+
return isRef(t) ? readRef(t, line, "a pick", routes) : { literal: unquote(t) };
|
|
44
|
+
}
|
|
45
|
+
/** `containerStation = context.station, containerState = open` */
|
|
46
|
+
function readPicks(text, line, routes, founds = new Set()) {
|
|
47
|
+
return text
|
|
48
|
+
.split(/,\s*(?=[\w:.]+\s*=)/)
|
|
49
|
+
.map((s) => s.trim())
|
|
50
|
+
.filter(Boolean)
|
|
51
|
+
.map((p) => {
|
|
52
|
+
const pm = p.match(/^([\w:]+(?:\.[\w:]+)?)\s*=\s*(.+)$/);
|
|
53
|
+
if (!pm)
|
|
54
|
+
throw new CrubalError(`a pick is "<facet> = <value>": ${p}`, line);
|
|
55
|
+
return { facet: pm[1], value: readPickValue(pm[2].trim(), line, routes, founds), line };
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/** `boxed -> unit where filedIn = box` (revision 26) */
|
|
59
|
+
function readCount(text, line, routes, founds) {
|
|
60
|
+
const m = text.match(/^([\w-]+)\s*->\s*([\w-]+)(?:\s+where\s+(.*))?$/);
|
|
61
|
+
if (!m)
|
|
62
|
+
throw new CrubalError(`a count is "<name> -> <kind> [where <facet> = <value>, …]": ${text}`, line);
|
|
63
|
+
return { name: m[1], kind: m[2], where: readPicks(m[3] ?? "", line, routes, founds), line };
|
|
64
|
+
}
|
|
65
|
+
/** `box -> first container where containerState = open, containerStation = context.station order containerLabel` */
|
|
66
|
+
function readFind(text, line, routes) {
|
|
67
|
+
const m = text.match(/^([\w-]+)\s*->\s*(first|all)\s+([\w-]+)(?:\s+where\s+(.*?))?(?:\s+order\s+([\w:]+)(?:\s+(after|before))?)?$/);
|
|
68
|
+
if (!m)
|
|
69
|
+
throw new CrubalError(`a find is "<name> -> first|all <kind> [where <facet> = <value>, …] [order <facet> [after|before]]": ${text}`, line);
|
|
70
|
+
return { name: m[1], take: m[2], kind: m[3], where: readPicks(m[4] ?? "", line, routes), ...(m[5] ? { order: { facet: m[5], dir: m[6] ?? "after" } } : {}), line };
|
|
71
|
+
}
|
|
72
|
+
/** `slot -> row of context.station.slots by slotRule over record, record.of keep keep by unit where of = record.of, …` */
|
|
73
|
+
function readRoute(text, line) {
|
|
74
|
+
const m = text.match(/^([\w-]+)\s*->\s*row of\s+(\S+)\s+by\s+([\w:]+)\s+over\s+(.+?)(?:\s+keep\s+([\w:]+)\s+by\s+([\w-]+)(?:\s+where\s+(.*))?)?$/);
|
|
75
|
+
if (!m)
|
|
76
|
+
throw new CrubalError(`a route is "<name> -> row of <ref> by <column> over record[, record.<link>] [keep <column> by <kind> where <picks>]": ${text}`, line);
|
|
77
|
+
const name = m[1];
|
|
78
|
+
const routes = new Set([name]);
|
|
79
|
+
const over = m[4].split(",").map((s) => s.trim()).filter(Boolean).map((t) => {
|
|
80
|
+
if (t === "record")
|
|
81
|
+
return { record: "" };
|
|
82
|
+
const r = readRef(t, line, "a route's over");
|
|
83
|
+
if (!("record" in r))
|
|
84
|
+
throw new CrubalError(`a route reads over record or record.<link facet>, not ${t}`, line);
|
|
85
|
+
return r;
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
name,
|
|
89
|
+
rows: readRef(m[2], line, "a route's rows"),
|
|
90
|
+
by: m[3],
|
|
91
|
+
over,
|
|
92
|
+
...(m[5] ? { keep: { column: m[5], kind: m[6], where: readPicks(m[7] ?? "", line, routes) } } : {}),
|
|
93
|
+
line,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** Revision 45: `<ref|number> + <ref|number>` or `-`, at least one a reference; anything else is not a sum. */
|
|
97
|
+
function readSum(t, line, routes) {
|
|
98
|
+
const m = t.match(/^(\S+)\s+([+*-])\s+(\S+)$/);
|
|
99
|
+
if (!m)
|
|
100
|
+
return undefined;
|
|
101
|
+
const num = (x) => /^\d+(?:\.\d+)?$/.test(x);
|
|
102
|
+
const a = m[1];
|
|
103
|
+
const b = m[3];
|
|
104
|
+
if (!(num(a) || isRef(a)) || !(num(b) || isRef(b)) || (num(a) && num(b)))
|
|
105
|
+
return undefined;
|
|
106
|
+
const operand = (x) => (num(x) ? { number: Number(x) } : readRef(x, line, "a sum", routes));
|
|
107
|
+
return { left: operand(a), op: m[2], right: operand(b) };
|
|
108
|
+
}
|
|
109
|
+
/** `filedIn -> link box`, `filedBox -> box.containerLabel`, `filedAt -> now`, `note -> "words"`,
|
|
110
|
+
* `containerLabel -> next slot.boxPrefix in container.containerLabel, slot.boxSuffix` */
|
|
111
|
+
function readSet(text, line, routes) {
|
|
112
|
+
const m = text.match(/^([\w:]+)\s*->\s*(.+)$/);
|
|
113
|
+
if (!m)
|
|
114
|
+
throw new CrubalError(`a set is "<facet> -> <source>": ${text}`, line);
|
|
115
|
+
const facet = m[1];
|
|
116
|
+
const src = m[2].trim();
|
|
117
|
+
if (src === "link caller")
|
|
118
|
+
return { facet, source: { caller: true }, line };
|
|
119
|
+
// Revision 46: the person running the act as words (their id), where the facet is text and no link.
|
|
120
|
+
if (src === "caller")
|
|
121
|
+
return { facet, source: { person: true }, line };
|
|
122
|
+
// Revision 31: the owner's domain, the act run's id, a link to the record in hand (a told post's sender, run and subject).
|
|
123
|
+
if (src === "owner")
|
|
124
|
+
return { facet, source: { owner: true }, line };
|
|
125
|
+
if (src === "run")
|
|
126
|
+
return { facet, source: { run: true }, line };
|
|
127
|
+
const link = src.match(/^link\s+([\w-]+)$/);
|
|
128
|
+
if (link)
|
|
129
|
+
return { facet, source: { link: link[1] }, line };
|
|
130
|
+
if (src === "now")
|
|
131
|
+
return { facet, source: { now: true }, line };
|
|
132
|
+
if (src === "none")
|
|
133
|
+
return { facet, source: { none: true }, line };
|
|
134
|
+
const next = src.match(/^next\s+("[^"]*"|\S+)\s+in\s+([\w-]+)\.([\w:]+)(?:\s*,\s*(\S+))?$/);
|
|
135
|
+
if (next) {
|
|
136
|
+
return {
|
|
137
|
+
facet,
|
|
138
|
+
source: {
|
|
139
|
+
next: {
|
|
140
|
+
// Revision 35: the prefix may be the words themselves, quoted (`next "PULL" in pull.pullLabel`).
|
|
141
|
+
prefix: next[1].startsWith('"') ? { literal: unquote(next[1]) } : readRef(next[1], line, "a series' prefix", routes),
|
|
142
|
+
in: { kind: next[2], facet: next[3] },
|
|
143
|
+
...(next[4] ? { suffix: readRef(next[4], line, "a series' suffix", routes) } : {}),
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
line,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
const sum = readSum(src, line, routes);
|
|
150
|
+
if (sum)
|
|
151
|
+
return { facet, source: { sum }, line };
|
|
152
|
+
if (isRef(src))
|
|
153
|
+
return { facet, source: readRef(src, line, "a set", routes), line };
|
|
154
|
+
return { facet, source: { literal: unquote(src) }, line };
|
|
155
|
+
}
|
|
156
|
+
function readTerm(t, line, routes, counts) {
|
|
157
|
+
const n = Number(t);
|
|
158
|
+
if (t !== "" && Number.isFinite(n))
|
|
159
|
+
return { number: n };
|
|
160
|
+
const m = t.match(/^(.+?)\s*\+\s*(\d+(?:\.\d+)?)$/);
|
|
161
|
+
const word = (m ? m[1] : t).trim();
|
|
162
|
+
const plus = m ? { plus: Number(m[2]) } : {};
|
|
163
|
+
if (counts.has(word))
|
|
164
|
+
return { count: word, ...plus };
|
|
165
|
+
if (/^[\w-]+$/.test(word))
|
|
166
|
+
throw new CrubalError(`a when reads ${word}, which no count declares above it`, line);
|
|
167
|
+
return { ref: readRef(word, line, "a when", routes), ...plus };
|
|
168
|
+
}
|
|
169
|
+
/** `given.count != record.qty and record.qty > 0` (revision 45: `!=`, and comparisons joined by `and`). */
|
|
170
|
+
export function readWhen(text, line, routes = new Set(), counts = new Set()) {
|
|
171
|
+
const conds = text.split(/\s+and\s+/).map((c) => {
|
|
172
|
+
// Revision 47: `has <ref>`, `no <ref>`.
|
|
173
|
+
const h = c.trim().match(/^(has|no)\s+(\S+)$/);
|
|
174
|
+
if (h)
|
|
175
|
+
return { has: readRef(h[2], line, "a when", routes), ...(h[1] === "no" ? { lacks: true } : {}) };
|
|
176
|
+
// Revision 54: `<ref> is <word>`, `<ref> is not <word>`: a value against a word written here (a choice).
|
|
177
|
+
const w = c.trim().match(/^(\S+)\s+is\s+(not\s+)?([\w.-]+)$/);
|
|
178
|
+
if (w)
|
|
179
|
+
return { has: readRef(w[1], line, "a when", routes), is: w[3], ...(w[2] ? { lacks: true } : {}) };
|
|
180
|
+
const m = c.trim().match(/^(.+?)\s*(<=|>=|!=|=|<|>)\s*(.+)$/);
|
|
181
|
+
if (!m)
|
|
182
|
+
throw new CrubalError(`a when is "<term> <op> <term>", "has <ref>", "no <ref>" or "<ref> is [not] <word>", joined by "and": ${c.trim()}`, line);
|
|
183
|
+
return { left: readTerm(m[1].trim(), line, routes, counts), op: m[2], right: readTerm(m[3].trim(), line, routes, counts) };
|
|
184
|
+
});
|
|
185
|
+
const [first, ...rest] = conds;
|
|
186
|
+
return { ...first, ...(rest.length ? { and: rest } : {}) };
|
|
187
|
+
}
|
|
188
|
+
/** `when box.containerCount + 1 >= box.capacity set box.containerState = full, box.filledAt = now send notify containerFull about context.station` */
|
|
189
|
+
function readThen(text, line, routes, counts) {
|
|
190
|
+
// Revision 47: `[when …] archive <record|found>` or `restore …`, the record's standing in place of sets.
|
|
191
|
+
const st = text.match(/^(?:when\s+(.+?)\s+)?(archive|restore)\s+([\w-]+)(?:\s+as\s+([\w-]+))?$/);
|
|
192
|
+
if (st && !/(^|\s)set\s/.test(st[1] ?? "")) {
|
|
193
|
+
return { ...(st[1] !== undefined ? { when: readWhen(st[1], line, routes, counts) } : {}), sets: [], stand: { to: st[2] === "archive" ? "archived" : "active", on: st[3], ...(st[4] && st[2] === "archive" ? { reason: st[4] } : {}) }, line };
|
|
194
|
+
}
|
|
195
|
+
// Revision 39: the condition may be left out (`set box.containerState = open`).
|
|
196
|
+
const m = text.match(/^(?:when\s+(.+?)\s+)?set\s+(.+?)(?:\s+send\s+([a-z][\w-]*)(?:\s+(.*))?)?$/);
|
|
197
|
+
if (!m)
|
|
198
|
+
throw new CrubalError(`a then is "[when <term> <op> <term> [and …]] set <found>.<facet> = <value>[, …] [send <verb> <words>]": ${text}`, line);
|
|
199
|
+
const sets = m[2]
|
|
200
|
+
.split(/,\s*(?=[\w-]+\.\w+\s*=)/)
|
|
201
|
+
.map((s) => s.trim())
|
|
202
|
+
.filter(Boolean)
|
|
203
|
+
.map((s) => {
|
|
204
|
+
const sm = s.match(/^([\w-]+)\.(\w+)\s*=\s*(.+)$/);
|
|
205
|
+
if (!sm)
|
|
206
|
+
throw new CrubalError(`a then's set is "<found>.<facet> = <value>": ${s}`, line);
|
|
207
|
+
const v = sm[3].trim();
|
|
208
|
+
const link = v.match(/^link\s+([\w-]+)$/);
|
|
209
|
+
const sum = readSum(v, line, routes);
|
|
210
|
+
const value = v === "now" ? { now: true } : v === "none" ? { none: true } : v === "caller" ? { person: true } : link ? (link[1] === "caller" ? { caller: true } : { link: link[1] }) : sum ? { sum } : isRef(v) ? readRef(v, line, "a then", routes) : { literal: unquote(v) };
|
|
211
|
+
return { found: sm[1], facet: sm[2], value };
|
|
212
|
+
});
|
|
213
|
+
return {
|
|
214
|
+
...(m[1] !== undefined ? { when: readWhen(m[1], line, routes, counts) } : {}),
|
|
215
|
+
sets,
|
|
216
|
+
...(m[3] ? { send: { verb: m[3], words: (m[4] ?? "").trim() } } : {}),
|
|
217
|
+
line,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/** Read one `act:` block (15). */
|
|
221
|
+
export function readAct(node) {
|
|
222
|
+
const [name = "", version = ""] = node.value.split(/\s+\//).map((s) => s.trim());
|
|
223
|
+
if (!WORD.test(name))
|
|
224
|
+
throw new CrubalError(`an act is "act: <name> /<version>", the name a lower-case word: ${node.value}`, node.line);
|
|
225
|
+
const a = { name, version, utility: "", on: "", context: [], finds: [], counts: [], sets: [], thens: [], lands: "now", line: node.line };
|
|
226
|
+
// The route is read first wherever it stands, so the lines after it know its name.
|
|
227
|
+
const routeNode = node.children.find((c) => c.key === "route");
|
|
228
|
+
if (routeNode) {
|
|
229
|
+
const lines = routeNode.children.filter((l) => l.text.trim());
|
|
230
|
+
if (lines.length !== 1)
|
|
231
|
+
throw new CrubalError(`an act routes once: one line under route:`, routeNode.line);
|
|
232
|
+
a.route = readRoute(lines[0].text.trim(), lines[0].line);
|
|
233
|
+
}
|
|
234
|
+
const routes = new Set(a.route ? [a.route.name] : []);
|
|
235
|
+
const founds = new Set();
|
|
236
|
+
const countNames = new Set();
|
|
237
|
+
for (const c of node.children) {
|
|
238
|
+
if (!c.key)
|
|
239
|
+
continue;
|
|
240
|
+
const v = c.value.trim();
|
|
241
|
+
if (c.key === "utility")
|
|
242
|
+
a.utility = v;
|
|
243
|
+
else if (c.key === "replaces") {
|
|
244
|
+
if (!WORD.test(v))
|
|
245
|
+
throw new CrubalError(`replaces names an act, a lower-case word: ${v}`, c.line);
|
|
246
|
+
a.replaces = v;
|
|
247
|
+
}
|
|
248
|
+
else if (c.key === "label")
|
|
249
|
+
a.label = unquote(v);
|
|
250
|
+
else if (c.key === "describe")
|
|
251
|
+
a.describe = unquote(v);
|
|
252
|
+
else if (c.key === "on")
|
|
253
|
+
a.on = v;
|
|
254
|
+
else if (c.key === "takes") {
|
|
255
|
+
if (v !== "share")
|
|
256
|
+
throw new CrubalError(`takes is "share": the act may land on fewer than the record holds, not ${v}`, c.line);
|
|
257
|
+
a.takes = "share";
|
|
258
|
+
}
|
|
259
|
+
else if (c.key === "context") {
|
|
260
|
+
a.context = v.split(",").map((s) => s.trim()).filter(Boolean);
|
|
261
|
+
for (const k of a.context)
|
|
262
|
+
if (!WORD.test(k))
|
|
263
|
+
throw new CrubalError(`context names kinds, lower-case words: ${k}`, c.line);
|
|
264
|
+
}
|
|
265
|
+
else if (c.key === "needs") {
|
|
266
|
+
for (const l of c.children) {
|
|
267
|
+
const t = l.text.trim();
|
|
268
|
+
if (!t)
|
|
269
|
+
continue;
|
|
270
|
+
// Revision 51: `<facet> = <value>` and `<facet> != <value>`, the value a word or a number.
|
|
271
|
+
const is = t.match(/^([\w:]+(?:\.\w+)?)\s*(!=|=)\s*([\w.-]+)(?:\s+"([^"]+)")?$/);
|
|
272
|
+
if (is) {
|
|
273
|
+
(a.needs ??= []).push({ facet: is[1], is: { op: is[2], value: is[3] }, ...(is[4] ? { words: is[4] } : {}), line: l.line });
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const m = t.match(/^(no\s+)?([\w:]+(?:\.\w+)?)(?:\s+"([^"]+)")?$/);
|
|
277
|
+
if (!m)
|
|
278
|
+
throw new CrubalError(`a need is "<facet>", "no <facet>", "<facet> = <value>" or "<facet> != <value>" (the facet the record's own, or "<link facet>.<facet>", the linked record's), its words in quotes after it or not: ${t}`, l.line);
|
|
279
|
+
(a.needs ??= []).push({ facet: m[2], ...(m[1] ? { lacks: true } : {}), ...(m[3] ? { words: m[3] } : {}), line: l.line });
|
|
280
|
+
}
|
|
281
|
+
if (!a.needs?.length)
|
|
282
|
+
throw new CrubalError(`the act ${name} needs nothing: its lines go beneath needs:`, c.line);
|
|
283
|
+
}
|
|
284
|
+
else if (c.key === "given") {
|
|
285
|
+
for (const l of c.children) {
|
|
286
|
+
const t = l.text.trim();
|
|
287
|
+
if (!t)
|
|
288
|
+
continue;
|
|
289
|
+
const m = t.match(/^([\w-]+)\s+as\s+([\w:]+)(?:\s+"([^"]+)")?(?:\s+from\s+(\S+)|\s+default\s+(.+))?$/);
|
|
290
|
+
if (!m || !WORD.test(m[1]))
|
|
291
|
+
throw new CrubalError(`a given is "<name> as <facet>", its words in quotes after it or not, then "from <ref>" (what the question opens on) or "default <value>" (what it takes unasked) or neither: ${t}`, l.line);
|
|
292
|
+
if (a.given?.some((g) => g.name === m[1]))
|
|
293
|
+
throw new CrubalError(`the act ${name} is given ${m[1]} twice`, l.line);
|
|
294
|
+
(a.given ??= []).push({ name: m[1], facet: m[2], ...(m[3] ? { words: m[3] } : {}), ...(m[4] ? { from: readRef(m[4], l.line, "a given's from", routes) } : {}), ...(m[5] !== undefined ? { default: unquote(m[5].trim()) } : {}), line: l.line });
|
|
295
|
+
}
|
|
296
|
+
if (!a.given?.length)
|
|
297
|
+
throw new CrubalError(`the act ${name} is given nothing: its lines go beneath given:`, c.line);
|
|
298
|
+
}
|
|
299
|
+
else if (c.key === "route")
|
|
300
|
+
continue;
|
|
301
|
+
else if (c.key === "find")
|
|
302
|
+
for (const l of c.children) {
|
|
303
|
+
if (l.text.trim()) {
|
|
304
|
+
const f = readFind(l.text.trim(), l.line, routes);
|
|
305
|
+
a.finds.push(f);
|
|
306
|
+
founds.add(f.name);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else if (c.key === "count")
|
|
310
|
+
for (const l of c.children) {
|
|
311
|
+
if (l.text.trim()) {
|
|
312
|
+
const k = readCount(l.text.trim(), l.line, routes, founds);
|
|
313
|
+
a.counts.push(k);
|
|
314
|
+
countNames.add(k.name);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
else if (c.key === "mint") {
|
|
318
|
+
for (const l of c.children) {
|
|
319
|
+
if (!l.text.trim())
|
|
320
|
+
continue;
|
|
321
|
+
const m = l.text.trim().match(/^([\w-]+)\s*->\s*([\w-]+)\s+when none$/);
|
|
322
|
+
if (!m)
|
|
323
|
+
throw new CrubalError(`a mint is "<found> -> <kind> when none", its set lines beneath: ${l.text.trim()}`, l.line);
|
|
324
|
+
const f = a.finds.find((x) => x.name === m[1]);
|
|
325
|
+
if (!f)
|
|
326
|
+
throw new CrubalError(`the mint ${m[1]} names no find above it`, l.line);
|
|
327
|
+
if (f.take !== "first")
|
|
328
|
+
throw new CrubalError(`the mint ${m[1]} is of a find that takes all: a mint stands in for the first`, l.line);
|
|
329
|
+
if (f.mint)
|
|
330
|
+
throw new CrubalError(`${m[1]} is minted twice`, l.line);
|
|
331
|
+
f.mint = { kind: m[2], sets: l.children.filter((s) => s.text.trim()).map((s) => readSet(s.text.trim(), s.line, routes)), line: l.line };
|
|
332
|
+
if (!f.mint.sets.length)
|
|
333
|
+
throw new CrubalError(`the mint ${m[1]} sets nothing: its set lines go beneath it`, l.line);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
else if (c.key === "make") {
|
|
337
|
+
// Revision 45: `<name> -> <kind> [when …]`, its set lines beneath: a record made when the act lands.
|
|
338
|
+
for (const l of c.children) {
|
|
339
|
+
if (!l.text.trim())
|
|
340
|
+
continue;
|
|
341
|
+
const m = l.text.trim().match(/^([\w-]+)\s*->\s*([\w-]+)(?:\s+when\s+(.+))?$/);
|
|
342
|
+
if (!m || !WORD.test(m[1]))
|
|
343
|
+
throw new CrubalError(`a make is "<name> -> <kind> [when <term> <op> <term> [and …]]", its set lines beneath: ${l.text.trim()}`, l.line);
|
|
344
|
+
const make = { name: m[1], kind: m[2], ...(m[3] ? { when: readWhen(m[3], l.line, routes, countNames) } : {}), sets: l.children.filter((x) => x.text.trim()).map((x) => readSet(x.text.trim(), x.line, routes)), line: l.line };
|
|
345
|
+
if (!make.sets.length)
|
|
346
|
+
throw new CrubalError(`the make ${make.name} sets nothing: its set lines go beneath it`, l.line);
|
|
347
|
+
(a.makes ??= []).push(make);
|
|
348
|
+
}
|
|
349
|
+
if (!a.makes?.length)
|
|
350
|
+
throw new CrubalError(`the act ${name} makes nothing: its lines go beneath make:`, c.line);
|
|
351
|
+
}
|
|
352
|
+
else if (c.key === "also" || c.key === "first") {
|
|
353
|
+
// Revision 47: `[<utility>/]<act> on record|<found> [with <given> = <value>, …] [when …]`: an act run when
|
|
354
|
+
// this one has landed (`also`) or, revision 49, before it lands (`first`).
|
|
355
|
+
const list = [];
|
|
356
|
+
for (const l of c.children) {
|
|
357
|
+
if (!l.text.trim())
|
|
358
|
+
continue;
|
|
359
|
+
const m = l.text.trim().match(/^(?:([a-z][\w-]*)\/)?([a-z][\w-]*)\s+on\s+([\w-]+)(\s+as\s+place)?(?:\s+with\s+(.+?))?(?:\s+when\s+(.+))?$/);
|
|
360
|
+
if (!m)
|
|
361
|
+
throw new CrubalError(`${c.key === "also" ? "an also" : "a first"} is "[<utility>/]<act> on record|<found> [as place] [with <given> = <value>, …] [when …]": ${l.text.trim()}`, l.line);
|
|
362
|
+
const gives = (m[5] ?? "").split(",").map((x) => x.trim()).filter(Boolean).map((x) => {
|
|
363
|
+
const g = x.match(/^([\w-]+)\s*=\s*(.+)$/);
|
|
364
|
+
if (!g)
|
|
365
|
+
throw new CrubalError(`a with is "<given> = <value>": ${x}`, l.line);
|
|
366
|
+
const v = g[2].trim();
|
|
367
|
+
return { name: g[1], value: isRef(v) ? readRef(v, l.line, "a with", routes) : { literal: unquote(v) } };
|
|
368
|
+
});
|
|
369
|
+
list.push({ ...(m[1] ? { utility: m[1] } : {}), act: m[2], on: m[3], ...(m[4] ? { asPlace: true } : {}), ...(gives.length ? { with: gives } : {}), ...(m[6] ? { when: readWhen(m[6], l.line, routes, countNames) } : {}), line: l.line });
|
|
370
|
+
}
|
|
371
|
+
if (!list.length)
|
|
372
|
+
throw new CrubalError(`the act ${name} runs nothing ${c.key}: its lines go beneath ${c.key}:`, c.line);
|
|
373
|
+
if (c.key === "also")
|
|
374
|
+
a.also = [...(a.also ?? []), ...list];
|
|
375
|
+
else
|
|
376
|
+
a.first = [...(a.first ?? []), ...list];
|
|
377
|
+
}
|
|
378
|
+
else if (c.key === "set")
|
|
379
|
+
for (const l of c.children) {
|
|
380
|
+
if (l.text.trim())
|
|
381
|
+
a.sets.push(readSet(l.text.trim(), l.line, routes));
|
|
382
|
+
}
|
|
383
|
+
else if (c.key === "then")
|
|
384
|
+
for (const l of c.children) {
|
|
385
|
+
if (l.text.trim())
|
|
386
|
+
a.thens.push(readThen(l.text.trim(), l.line, routes, countNames));
|
|
387
|
+
}
|
|
388
|
+
else if (c.key === "send") {
|
|
389
|
+
const m = v.match(/^([a-z][\w-]*)(?:\s+(.*))?$/);
|
|
390
|
+
if (!m)
|
|
391
|
+
throw new CrubalError(`a send is "<verb> <words>": ${v}`, c.line);
|
|
392
|
+
a.send = { verb: m[1], words: (m[2] ?? "").trim() };
|
|
393
|
+
}
|
|
394
|
+
else if (c.key === "lands") {
|
|
395
|
+
if (v === "now")
|
|
396
|
+
a.lands = "now";
|
|
397
|
+
else if (/^when the send reports$/.test(v))
|
|
398
|
+
a.lands = "reports";
|
|
399
|
+
else
|
|
400
|
+
throw new CrubalError(`lands is "now" or "when the send reports", not ${v}`, c.line);
|
|
401
|
+
}
|
|
402
|
+
else if (c.key === "tells") {
|
|
403
|
+
const m = v.match(/^([\w-]+)\s+as\s+([a-z][\w-]*)$/);
|
|
404
|
+
if (!m)
|
|
405
|
+
throw new CrubalError(`tells is "<kind> as <aspect>", its set lines beneath: ${v}`, c.line);
|
|
406
|
+
if (a.tells)
|
|
407
|
+
throw new CrubalError(`the act ${name} tells twice`, c.line);
|
|
408
|
+
a.tells = { kind: m[1], aspect: m[2], sets: c.children.filter((l) => l.text.trim()).map((l) => readSet(l.text.trim(), l.line, routes)), line: c.line };
|
|
409
|
+
if (!a.tells.sets.length)
|
|
410
|
+
throw new CrubalError(`the act ${name} tells ${m[1]} and sets nothing: its set lines go beneath tells:`, c.line);
|
|
411
|
+
}
|
|
412
|
+
else
|
|
413
|
+
throw new CrubalError(`an act has utility, replaces, label, describe, on, takes, context, needs, given, route, find, mint, count, make, set, then, also, first, send, lands and tells, not ${c.key}`, c.line);
|
|
414
|
+
}
|
|
415
|
+
if (!a.utility)
|
|
416
|
+
throw new CrubalError(`the act ${name} names no utility`, node.line);
|
|
417
|
+
if (!a.on)
|
|
418
|
+
throw new CrubalError(`the act ${name} says no on: the level it acts on`, node.line);
|
|
419
|
+
if (a.replaces === a.name)
|
|
420
|
+
throw new CrubalError(`the act ${name} replaces itself`, node.line);
|
|
421
|
+
if (a.lands === "reports" && !a.send)
|
|
422
|
+
throw new CrubalError(`the act ${name} lands when the send reports, but sends nothing`, node.line);
|
|
423
|
+
return a;
|
|
424
|
+
}
|
|
425
|
+
const bare = (facet) => facet.replace(/^\w+::/, "");
|
|
426
|
+
/** Every term a `when` reads, its `and`s among them. */
|
|
427
|
+
export const termsOf = (w) => (w ? [w, ...(w.and ?? [])].flatMap((c) => ("has" in c ? [{ ref: c.has }] : [c.left, c.right])) : []);
|
|
428
|
+
/** `of` is a record's parent (1.1), no facet: it may stand where a facet may (a pick's facet, `record.of`, a
|
|
429
|
+
* route's `over`) and the dictionary is never asked for it. */
|
|
430
|
+
const PARENT = "of";
|
|
431
|
+
/** The checks (15.3). */
|
|
432
|
+
export function checkActList(acts, env) {
|
|
433
|
+
const { strict, here } = env;
|
|
434
|
+
for (const a of acts) {
|
|
435
|
+
const where = `the act ${a.name}`;
|
|
436
|
+
const u = env.utilities.find((x) => x.name === a.utility);
|
|
437
|
+
if (!u && strict)
|
|
438
|
+
throw new CrubalError(`${where} names the utility ${a.utility}, which isn't ${here}`, a.line);
|
|
439
|
+
if (u) {
|
|
440
|
+
const listed = (n) => u.acts.some((x) => x.name === n);
|
|
441
|
+
if (a.replaces && !listed(a.replaces))
|
|
442
|
+
throw new CrubalError(`${where} replaces ${a.replaces}, which isn't among ${a.utility}'s acts`, a.line);
|
|
443
|
+
if (!a.replaces && !listed(a.name))
|
|
444
|
+
throw new CrubalError(`${where} isn't among ${a.utility}'s acts`, a.line);
|
|
445
|
+
}
|
|
446
|
+
if (!env.levels.has(a.on) && strict)
|
|
447
|
+
throw new CrubalError(`${where} is on ${a.on}, which isn't a level ${here}`, a.line);
|
|
448
|
+
if (a.takes && env.shares && env.levels.has(a.on) && !env.shares.has(a.on))
|
|
449
|
+
throw new CrubalError(`${where} takes a share, and ${a.on} declares no quantity: its kind's shares names none for it`, a.line);
|
|
450
|
+
for (const k of a.context)
|
|
451
|
+
if (!env.kinds.has(k) && !env.levels.has(k) && strict)
|
|
452
|
+
throw new CrubalError(`${where}'s context names ${k}, which isn't a kind ${here}`, a.line);
|
|
453
|
+
const facetKnown = (f) => env.facets.has(bare(f));
|
|
454
|
+
const needFacet = (f, line, doing) => {
|
|
455
|
+
if (bare(f) === PARENT)
|
|
456
|
+
return;
|
|
457
|
+
if (!facetKnown(f) && strict)
|
|
458
|
+
throw new CrubalError(`${where} ${doing} ${f}, which isn't a facet ${here}`, line);
|
|
459
|
+
};
|
|
460
|
+
const found = new Set();
|
|
461
|
+
const routeName = a.route?.name;
|
|
462
|
+
const columns = (r) => {
|
|
463
|
+
const key = "context" in r ? r.facet : "record" in r ? r.record : undefined;
|
|
464
|
+
const f = key ? env.facetOf?.(bare(key)) : undefined;
|
|
465
|
+
return f?.type === "compound" && f.children ? new Set(f.children.keys()) : undefined;
|
|
466
|
+
};
|
|
467
|
+
const routeColumns = a.route ? columns(a.route.rows) : undefined;
|
|
468
|
+
const givens = new Set((a.given ?? []).map((g) => g.name));
|
|
469
|
+
const refOk = (r, line, inCount = false) => {
|
|
470
|
+
if ("given" in r) {
|
|
471
|
+
if (!givens.has(r.given))
|
|
472
|
+
throw new CrubalError(`${where} reads given.${r.given}, which its given lacks`, line);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if ("found" in r && !found.has(r.found))
|
|
476
|
+
throw new CrubalError(`${where} names ${r.found}, which no find declares`, line);
|
|
477
|
+
if ("found" in r && !r.facet)
|
|
478
|
+
return;
|
|
479
|
+
if ("route" in r) {
|
|
480
|
+
if (r.route !== routeName)
|
|
481
|
+
throw new CrubalError(`${where} names ${r.route}, which no route declares`, line);
|
|
482
|
+
if (routeColumns && !routeColumns.has(r.column))
|
|
483
|
+
throw new CrubalError(`${where} reads ${r.route}.${r.column}, a column the rows lack`, line);
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if ("context" in r && !a.context.includes(r.context))
|
|
487
|
+
throw new CrubalError(`${where} names context.${r.context}, which its context lacks`, line);
|
|
488
|
+
const facet = "found" in r ? r.facet : "record" in r ? r.record : r.facet;
|
|
489
|
+
if (facet)
|
|
490
|
+
needFacet(facet, line, inCount ? "counts by" : "reads");
|
|
491
|
+
};
|
|
492
|
+
const picksOk = (picks, what, inCount = false) => {
|
|
493
|
+
for (const p of picks) {
|
|
494
|
+
const [head, hop] = p.facet.split(".");
|
|
495
|
+
needFacet(head, p.line, `${what} picks`);
|
|
496
|
+
if (hop) {
|
|
497
|
+
const f = bare(head) === PARENT ? undefined : env.facetOf?.(bare(head));
|
|
498
|
+
if (f && f.type !== "link")
|
|
499
|
+
throw new CrubalError(`${where} ${what} picks through ${head}, which isn't a link`, p.line);
|
|
500
|
+
needFacet(hop, p.line, `${what} picks`);
|
|
501
|
+
}
|
|
502
|
+
if (!("literal" in p.value) && !("caller" in p.value) && !("self" in p.value))
|
|
503
|
+
refOk(p.value, p.line, inCount);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
for (const g of a.given ?? []) {
|
|
507
|
+
needFacet(g.facet, g.line, `is given ${g.name} as`);
|
|
508
|
+
if (g.from) {
|
|
509
|
+
if (!("record" in g.from) && !("context" in g.from))
|
|
510
|
+
throw new CrubalError(`${where}'s given ${g.name} opens from ${refText(g.from)}: a given is asked before anything is found, so it opens from record.<facet> or context.<kind>.<facet>`, g.line);
|
|
511
|
+
refOk(g.from, g.line);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (a.route) {
|
|
515
|
+
const r = a.route;
|
|
516
|
+
if ("given" in r.rows)
|
|
517
|
+
throw new CrubalError(`${where} routes over rows that aren't the record's or a context record's`, r.line);
|
|
518
|
+
if ("context" in r.rows && !r.rows.facet)
|
|
519
|
+
throw new CrubalError(`${where} routes over context.${r.rows.context}, which names no facet: the rows are a compound facet's`, r.line);
|
|
520
|
+
if ("found" in r.rows || "route" in r.rows)
|
|
521
|
+
throw new CrubalError(`${where} routes over rows that aren't the record's or a context record's`, r.line);
|
|
522
|
+
refOk(r.rows, r.line);
|
|
523
|
+
const rowsFacet = "context" in r.rows ? r.rows.facet : "record" in r.rows ? r.rows.record : undefined;
|
|
524
|
+
const rf = rowsFacet ? env.facetOf?.(bare(rowsFacet)) : undefined;
|
|
525
|
+
if (rf && rf.type !== "compound")
|
|
526
|
+
throw new CrubalError(`${where} routes over ${rowsFacet}, which isn't a compound`, r.line);
|
|
527
|
+
if (routeColumns && !routeColumns.has(r.by))
|
|
528
|
+
throw new CrubalError(`${where} routes by ${r.by}, a column the rows lack`, r.line);
|
|
529
|
+
for (const o of r.over)
|
|
530
|
+
if ("record" in o && o.record && bare(o.record) !== PARENT) {
|
|
531
|
+
needFacet(o.record, r.line, "routes over");
|
|
532
|
+
const lf = env.facetOf?.(bare(o.record));
|
|
533
|
+
if (lf && lf.type !== "link")
|
|
534
|
+
throw new CrubalError(`${where} routes over record.${o.record}, which isn't a link`, r.line);
|
|
535
|
+
}
|
|
536
|
+
if (r.keep) {
|
|
537
|
+
if (routeColumns && !routeColumns.has(r.keep.column))
|
|
538
|
+
throw new CrubalError(`${where} keeps by ${r.keep.column}, a column the rows lack`, r.line);
|
|
539
|
+
if (!env.kinds.has(r.keep.kind) && !env.levels.has(r.keep.kind) && strict)
|
|
540
|
+
throw new CrubalError(`${where} counts ${r.keep.kind}, which isn't a kind ${here}`, r.line);
|
|
541
|
+
picksOk(r.keep.where, "'s count", true);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
// Revision 48: a make's name may stand where a link's target does.
|
|
545
|
+
const makeNames = new Set((a.makes ?? []).map((m) => m.name));
|
|
546
|
+
const sumOk = (sum, line) => {
|
|
547
|
+
for (const o of [sum.left, sum.right])
|
|
548
|
+
if (!("number" in o))
|
|
549
|
+
refOk(o, line);
|
|
550
|
+
};
|
|
551
|
+
const sourceOk = (s, doing) => {
|
|
552
|
+
needFacet(s.facet, s.line, doing);
|
|
553
|
+
const src = s.source;
|
|
554
|
+
if ("link" in src) {
|
|
555
|
+
if (src.link !== "record" && !found.has(src.link) && !a.context.includes(src.link) && !makeNames.has(src.link))
|
|
556
|
+
throw new CrubalError(`${where} links ${s.facet} to ${src.link}, which no find or context declares`, s.line);
|
|
557
|
+
}
|
|
558
|
+
else if ("next" in src) {
|
|
559
|
+
if (!("literal" in src.next.prefix))
|
|
560
|
+
refOk(src.next.prefix, s.line);
|
|
561
|
+
if (src.next.suffix)
|
|
562
|
+
refOk(src.next.suffix, s.line);
|
|
563
|
+
if (!env.kinds.has(src.next.in.kind) && !env.levels.has(src.next.in.kind) && strict)
|
|
564
|
+
throw new CrubalError(`${where} numbers ${s.facet} in ${src.next.in.kind}, which isn't a kind ${here}`, s.line);
|
|
565
|
+
needFacet(src.next.in.facet, s.line, "numbers by");
|
|
566
|
+
}
|
|
567
|
+
else if ("sum" in src)
|
|
568
|
+
sumOk(src.sum, s.line);
|
|
569
|
+
else if (!("literal" in src) && !("now" in src) && !("caller" in src) && !("person" in src) && !("none" in src) && !("owner" in src) && !("run" in src))
|
|
570
|
+
refOk(src, s.line);
|
|
571
|
+
};
|
|
572
|
+
for (const f of a.finds) {
|
|
573
|
+
if (found.has(f.name))
|
|
574
|
+
throw new CrubalError(`${where} finds ${f.name} twice`, f.line);
|
|
575
|
+
if (f.name === "given")
|
|
576
|
+
throw new CrubalError(`${where} finds given, the word for what an act is given`, f.line);
|
|
577
|
+
if (f.name === "record")
|
|
578
|
+
throw new CrubalError(`${where} finds record, the word for the record in hand`, f.line);
|
|
579
|
+
if (f.name === routeName)
|
|
580
|
+
throw new CrubalError(`${where} finds ${f.name}, its route's name`, f.line);
|
|
581
|
+
if (!env.kinds.has(f.kind) && !env.levels.has(f.kind) && strict)
|
|
582
|
+
throw new CrubalError(`${where} finds ${f.kind}, which isn't a kind ${here}`, f.line);
|
|
583
|
+
picksOk(f.where, `'s find ${f.name}`);
|
|
584
|
+
if (f.order)
|
|
585
|
+
needFacet(f.order.facet, f.line, `'s find ${f.name} orders by`);
|
|
586
|
+
if (f.mint) {
|
|
587
|
+
if (!env.kinds.has(f.mint.kind) && !env.levels.has(f.mint.kind) && strict)
|
|
588
|
+
throw new CrubalError(`${where} mints ${f.mint.kind}, which isn't a kind ${here}`, f.mint.line);
|
|
589
|
+
const set = new Set();
|
|
590
|
+
for (const s of f.mint.sets) {
|
|
591
|
+
if (set.has(s.facet))
|
|
592
|
+
throw new CrubalError(`${where}'s mint ${f.name} sets ${s.facet} twice`, s.line);
|
|
593
|
+
set.add(s.facet);
|
|
594
|
+
sourceOk(s, "mints");
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
found.add(f.name);
|
|
598
|
+
}
|
|
599
|
+
const counted = new Set();
|
|
600
|
+
for (const k of a.counts) {
|
|
601
|
+
if (counted.has(k.name) || found.has(k.name) || k.name === routeName)
|
|
602
|
+
throw new CrubalError(`${where} counts ${k.name}, a name already taken`, k.line);
|
|
603
|
+
if (!env.kinds.has(k.kind) && !env.levels.has(k.kind) && strict)
|
|
604
|
+
throw new CrubalError(`${where} counts ${k.kind}, which isn't a kind ${here}`, k.line);
|
|
605
|
+
picksOk(k.where, `'s count ${k.name}`, true);
|
|
606
|
+
counted.add(k.name);
|
|
607
|
+
}
|
|
608
|
+
const sets = new Set();
|
|
609
|
+
for (const s of a.sets) {
|
|
610
|
+
if (sets.has(s.facet))
|
|
611
|
+
throw new CrubalError(`${where} sets ${s.facet} twice`, s.line);
|
|
612
|
+
sets.add(s.facet);
|
|
613
|
+
sourceOk(s, "sets");
|
|
614
|
+
}
|
|
615
|
+
if (a.tells) {
|
|
616
|
+
const t = a.tells;
|
|
617
|
+
if (!env.kinds.has(t.kind) && !env.levels.has(t.kind) && strict)
|
|
618
|
+
throw new CrubalError(`${where} tells ${t.kind}, which isn't a kind ${here}`, t.line);
|
|
619
|
+
if (env.aspects && !env.aspects.has(t.aspect) && strict && !env.inertTelling)
|
|
620
|
+
throw new CrubalError(`${where} tells ${t.kind} as ${t.aspect}, an aspect no kind declares under follow ${here}`, t.line);
|
|
621
|
+
const set = new Set();
|
|
622
|
+
for (const s of t.sets) {
|
|
623
|
+
if (set.has(s.facet))
|
|
624
|
+
throw new CrubalError(`${where} tells ${t.kind} and sets ${s.facet} twice`, s.line);
|
|
625
|
+
set.add(s.facet);
|
|
626
|
+
sourceOk(s, "tells with");
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
for (const n of a.needs ?? [])
|
|
630
|
+
for (const part of n.facet.split("."))
|
|
631
|
+
needFacet(part, n.line, "needs");
|
|
632
|
+
const whenOk = (w, line) => {
|
|
633
|
+
for (const term of termsOf(w)) {
|
|
634
|
+
if ("ref" in term)
|
|
635
|
+
refOk(term.ref, line);
|
|
636
|
+
if ("count" in term && !counted.has(term.count))
|
|
637
|
+
throw new CrubalError(`${where} reads ${term.count}, which no count declares`, line);
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
const made = new Set();
|
|
641
|
+
for (const m of a.makes ?? []) {
|
|
642
|
+
if (made.has(m.name) || found.has(m.name) || counted.has(m.name) || m.name === routeName || m.name === "given" || m.name === "record")
|
|
643
|
+
throw new CrubalError(`${where} makes ${m.name}, a name already taken`, m.line);
|
|
644
|
+
made.add(m.name);
|
|
645
|
+
if (!env.kinds.has(m.kind) && !env.levels.has(m.kind) && strict)
|
|
646
|
+
throw new CrubalError(`${where} makes ${m.kind}, which isn't a kind ${here}`, m.line);
|
|
647
|
+
whenOk(m.when, m.line);
|
|
648
|
+
const set = new Set();
|
|
649
|
+
for (const s of m.sets) {
|
|
650
|
+
if (set.has(s.facet))
|
|
651
|
+
throw new CrubalError(`${where}'s make ${m.name} sets ${s.facet} twice`, s.line);
|
|
652
|
+
set.add(s.facet);
|
|
653
|
+
sourceOk(s, "makes with");
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
for (const x of [...(a.also ?? []), ...(a.first ?? [])]) {
|
|
657
|
+
if (x.on !== "record" && !found.has(x.on))
|
|
658
|
+
throw new CrubalError(`${where} also runs ${x.act} on ${x.on}, which no find declares`, x.line);
|
|
659
|
+
const own = !x.utility || x.utility === a.utility;
|
|
660
|
+
if (own && x.act === a.name && x.on === "record")
|
|
661
|
+
throw new CrubalError(`${where} also runs itself on the record in hand`, x.line);
|
|
662
|
+
const xu = env.utilities.find((y) => y.name === (x.utility ?? a.utility));
|
|
663
|
+
if (xu && !xu.acts.some((y) => y.name === x.act) && !acts.some((y) => y.utility === xu.name && y.name === x.act))
|
|
664
|
+
throw new CrubalError(`${where} also runs ${x.act}, which isn't among ${xu.name}'s acts`, x.line);
|
|
665
|
+
whenOk(x.when, x.line);
|
|
666
|
+
}
|
|
667
|
+
for (const t of a.thens) {
|
|
668
|
+
whenOk(t.when, t.line);
|
|
669
|
+
if (t.stand && t.stand.on !== "record" && !found.has(t.stand.on))
|
|
670
|
+
throw new CrubalError(`${where} then ${t.stand.to === "archived" ? "archives" : "restores"} ${t.stand.on}, which no find declares`, t.line);
|
|
671
|
+
for (const s of t.sets) {
|
|
672
|
+
// Revision 45: a then may write on the record in hand, `record.<facet>`.
|
|
673
|
+
if (s.found !== "record" && !found.has(s.found))
|
|
674
|
+
throw new CrubalError(`${where} then sets ${s.found}, which no find declares`, t.line);
|
|
675
|
+
needFacet(s.facet, t.line, "then sets");
|
|
676
|
+
const v = s.value;
|
|
677
|
+
if ("link" in v) {
|
|
678
|
+
if (v.link !== "record" && !found.has(v.link) && !a.context.includes(v.link) && !makeNames.has(v.link))
|
|
679
|
+
throw new CrubalError(`${where} then links ${s.found}.${s.facet} to ${v.link}, which no find or context declares`, t.line);
|
|
680
|
+
}
|
|
681
|
+
else if ("sum" in v)
|
|
682
|
+
sumOk(v.sum, t.line);
|
|
683
|
+
else if (!("literal" in v) && !("now" in v) && !("none" in v) && !("caller" in v) && !("person" in v))
|
|
684
|
+
refOk(v, t.line);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
/** A record's facet, or its parent's id for `of`. */
|
|
690
|
+
const facetOf = (rec, facet) => {
|
|
691
|
+
if (!rec)
|
|
692
|
+
return undefined;
|
|
693
|
+
const f = bare(facet);
|
|
694
|
+
return f === PARENT ? (rec.of ?? rec.values[f]) : rec.values[f];
|
|
695
|
+
};
|
|
696
|
+
const valueOf = (r, i) => {
|
|
697
|
+
if ("given" in r)
|
|
698
|
+
return i.given?.[r.given];
|
|
699
|
+
if ("record" in r)
|
|
700
|
+
return facetOf(i.record, r.record);
|
|
701
|
+
if ("context" in r)
|
|
702
|
+
return r.facet ? facetOf(i.context[r.context], r.facet) : i.context[r.context]?.id;
|
|
703
|
+
if ("route" in r)
|
|
704
|
+
return i.route?.row[r.column];
|
|
705
|
+
return r.facet ? facetOf(i.found[r.found]?.[0], r.facet) : i.found[r.found]?.[0]?.id;
|
|
706
|
+
};
|
|
707
|
+
const amountOf = (v) => {
|
|
708
|
+
const o = v && typeof v === "object" && !Array.isArray(v) ? v : undefined;
|
|
709
|
+
const raw = o ? ("$money" in o ? o.$money : "$qty" in o ? o.$qty : undefined) : v;
|
|
710
|
+
const text = typeof raw === "number" ? String(raw) : typeof raw === "string" ? raw.trim() : "";
|
|
711
|
+
if (text === "" || !Number.isFinite(Number(text)))
|
|
712
|
+
return undefined;
|
|
713
|
+
const places = (text.split(".")[1] ?? "").length;
|
|
714
|
+
return { n: Number(text), places, ...(o && "$money" in o ? { of: { money: String(o.currency ?? "") } } : o && "$qty" in o ? { of: { qty: String(o.unit ?? "") } } : {}) };
|
|
715
|
+
};
|
|
716
|
+
const sameOf = (a, b) => !a || !b || JSON.stringify(a) === JSON.stringify(b);
|
|
717
|
+
const termAmount = (t, i) => {
|
|
718
|
+
if ("number" in t)
|
|
719
|
+
return { n: t.number, places: 0 };
|
|
720
|
+
if ("count" in t) {
|
|
721
|
+
const c = i.counts?.[t.count];
|
|
722
|
+
return c === undefined ? undefined : { n: c + (t.plus ?? 0), places: 0 };
|
|
723
|
+
}
|
|
724
|
+
const a = amountOf(valueOf(t.ref, i));
|
|
725
|
+
return a ? { ...a, n: a.n + (t.plus ?? 0) } : undefined;
|
|
726
|
+
};
|
|
727
|
+
const numberOf = (t, i) => termAmount(t, i)?.n;
|
|
728
|
+
/** Revision 45: a sum's value. A reference that holds nothing reads as 0; one that holds no number refuses. */
|
|
729
|
+
const sumValue = (sum, i) => {
|
|
730
|
+
const read = (o) => {
|
|
731
|
+
if ("number" in o)
|
|
732
|
+
return { n: o.number, places: (String(o.number).split(".")[1] ?? "").length };
|
|
733
|
+
const v = valueOf(o, i);
|
|
734
|
+
if (v === undefined || v === null || v === "")
|
|
735
|
+
return { n: 0, places: 0 };
|
|
736
|
+
return amountOf(v) ?? `${refText(o)} holds no number to ${sum.op === "+" ? "add" : sum.op === "*" ? "multiply" : "take from"}`;
|
|
737
|
+
};
|
|
738
|
+
const l = read(sum.left);
|
|
739
|
+
if (typeof l === "string")
|
|
740
|
+
return { ok: false, why: l };
|
|
741
|
+
const r = read(sum.right);
|
|
742
|
+
if (typeof r === "string")
|
|
743
|
+
return { ok: false, why: r };
|
|
744
|
+
// Revision 48: money and quantities keep what they are of; two that differ are not summed.
|
|
745
|
+
if (!sameOf(l.of, r.of))
|
|
746
|
+
return { ok: false, why: `${"number" in sum.left ? sum.left.number : refText(sum.left)} and ${"number" in sum.right ? sum.right.number : refText(sum.right)} are not of one currency or unit` };
|
|
747
|
+
const said = (o) => ("number" in o ? String(o.number) : refText(o));
|
|
748
|
+
// Revision 52: a product. Two amounts (money by money, a quantity by a quantity) are not multiplied; money
|
|
749
|
+
// or a quantity by a plain number keeps what it is of and its own decimal places, rounded half up.
|
|
750
|
+
if (sum.op === "*") {
|
|
751
|
+
if (l.of && r.of)
|
|
752
|
+
return { ok: false, why: `${said(sum.left)} and ${said(sum.right)} are both amounts, and two amounts are not multiplied` };
|
|
753
|
+
const of = l.of ?? r.of;
|
|
754
|
+
const product = l.n * r.n;
|
|
755
|
+
if (!of)
|
|
756
|
+
return { ok: true, value: String(Number(product.toFixed(6))) };
|
|
757
|
+
const places = Math.min(6, l.of ? l.places : r.places);
|
|
758
|
+
const rounded = (Math.sign(product) * Math.round(Math.abs(product) * 10 ** places + 1e-9)) / 10 ** places;
|
|
759
|
+
const text = rounded.toFixed(places);
|
|
760
|
+
return { ok: true, value: "money" in of ? { $money: text, currency: of.money } : { $qty: text, unit: of.qty } };
|
|
761
|
+
}
|
|
762
|
+
const n = sum.op === "+" ? l.n + r.n : l.n - r.n;
|
|
763
|
+
const of = l.of ?? r.of;
|
|
764
|
+
if (!of)
|
|
765
|
+
return { ok: true, value: String(Number(n.toFixed(6))) };
|
|
766
|
+
const amount = n.toFixed(Math.min(6, Math.max(l.places, r.places)));
|
|
767
|
+
return { ok: true, value: "money" in of ? { $money: amount, currency: of.money } : { $qty: amount, unit: of.qty } };
|
|
768
|
+
};
|
|
769
|
+
/** Whether a `when` holds as the input stands: every comparison of it. A term that reads no number holds nowhere. */
|
|
770
|
+
export const whenHolds = (when, on) => {
|
|
771
|
+
if (!when)
|
|
772
|
+
return true;
|
|
773
|
+
return [when, ...(when.and ?? [])].every((c) => {
|
|
774
|
+
if ("has" in c) {
|
|
775
|
+
const v = valueOf(c.has, on);
|
|
776
|
+
// Revision 54: against a word, the value as written; a value not set is not the word.
|
|
777
|
+
if (c.is !== undefined)
|
|
778
|
+
return ((typeof v === "string" || typeof v === "number" ? String(v) : undefined) === c.is) !== !!c.lacks;
|
|
779
|
+
const holds = v !== undefined && v !== null && v !== "" && !(Array.isArray(v) && !v.length);
|
|
780
|
+
return holds !== !!c.lacks;
|
|
781
|
+
}
|
|
782
|
+
const la = termAmount(c.left, on);
|
|
783
|
+
const ra = termAmount(c.right, on);
|
|
784
|
+
// Revision 48: money against money of another currency, or a quantity of another unit, holds nowhere.
|
|
785
|
+
if (!la || !ra || !sameOf(la.of, ra.of))
|
|
786
|
+
return false;
|
|
787
|
+
const l = la.n;
|
|
788
|
+
const r = ra.n;
|
|
789
|
+
return c.op === "=" ? l === r : c.op === "!=" ? l !== r : c.op === "<" ? l < r : c.op === ">" ? l > r : c.op === "<=" ? l <= r : l >= r;
|
|
790
|
+
});
|
|
791
|
+
};
|
|
792
|
+
const asWords = (v) => (v === undefined || v === null ? "" : typeof v === "object" ? (typeof v.$link === "string" ? String(v.$link) : JSON.stringify(v)) : String(v));
|
|
793
|
+
/** The rows a route reads, as the record carries them: a compound's rows, each an object. */
|
|
794
|
+
export function routeRowsOf(r, i) {
|
|
795
|
+
const v = valueOf(r.rows, i);
|
|
796
|
+
return Array.isArray(v) ? v.filter((x) => !!x && typeof x === "object" && !Array.isArray(x)) : [];
|
|
797
|
+
}
|
|
798
|
+
/** The picks of a route's count for one row, resolved for the host to run (the row's columns read as the row
|
|
799
|
+
* being weighed). */
|
|
800
|
+
export function routeCountPicksOf(r, i, row) {
|
|
801
|
+
if (!r.keep)
|
|
802
|
+
return [];
|
|
803
|
+
const at = { record: i.record, context: i.context, found: i.found ?? {}, route: { index: -1, row } };
|
|
804
|
+
return r.keep.where.map((p) => ({ facet: p.facet.includes("::") ? p.facet.replace(/^\w+::/, "") : p.facet, value: "literal" in p.value ? p.value.literal : "caller" in p.value ? i.caller : "self" in p.value ? i.record.id : valueOf(p.value, at) }));
|
|
805
|
+
}
|
|
806
|
+
/** The row a route takes (15.1): the rack in order, each row's condition over the values laid as `over` says; the
|
|
807
|
+
* last row the catch-all; a row whose count is its keep or more passed over. `counts` are the host's answers to
|
|
808
|
+
* `routeCountPicksOf` per row, when the route keeps. Pure. */
|
|
809
|
+
export function routeOf(r, i, counts = []) {
|
|
810
|
+
const rows = routeRowsOf(r, { record: i.record, context: i.context, found: i.found ?? {} });
|
|
811
|
+
if (!rows.length)
|
|
812
|
+
return { ok: false, why: `no rows to route by` };
|
|
813
|
+
let values = {};
|
|
814
|
+
for (const o of r.over) {
|
|
815
|
+
if ("record" in o && !o.record)
|
|
816
|
+
values = { ...values, ...i.record.values };
|
|
817
|
+
else if ("record" in o)
|
|
818
|
+
values = { ...values, ...(i.linked?.[bare(o.record)]?.values ?? {}) };
|
|
819
|
+
}
|
|
820
|
+
// The record in hand's values lie over the linked record's whatever the order written: its own word wins.
|
|
821
|
+
values = { ...values, ...i.record.values };
|
|
822
|
+
for (const [n, row] of rows.entries()) {
|
|
823
|
+
const last = n === rows.length - 1;
|
|
824
|
+
if (!last) {
|
|
825
|
+
const rule = row[r.by];
|
|
826
|
+
if (typeof rule === "string" && rule.trim()) {
|
|
827
|
+
let truth;
|
|
828
|
+
try {
|
|
829
|
+
truth = evaluate(parseCondition(rule), values, undefined, i.now);
|
|
830
|
+
}
|
|
831
|
+
catch (e) {
|
|
832
|
+
return { ok: false, why: `the rule of row ${n + 1} can't be read: ${e instanceof Error ? e.message : String(e)}` };
|
|
833
|
+
}
|
|
834
|
+
if (truth !== true)
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
if (r.keep) {
|
|
839
|
+
const cap = Number(row[r.keep.column]);
|
|
840
|
+
const have = counts[n];
|
|
841
|
+
if (Number.isFinite(cap) && cap > 0 && have !== undefined && have >= cap)
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
const rule = typeof row[r.by] === "string" ? String(row[r.by]) : "";
|
|
845
|
+
return { ok: true, index: n, row, why: last ? (n === 0 ? "the one row takes everything" : "nothing above claimed it: the last row") : `row ${n + 1} took it: ${rule || "no rule"}` };
|
|
846
|
+
}
|
|
847
|
+
return { ok: false, why: "no row took it" };
|
|
848
|
+
}
|
|
849
|
+
/** The next label in a series (15.1): the prefix, one more than the highest number carried after it, three
|
|
850
|
+
* digits, the suffix when given. Pure. */
|
|
851
|
+
export function nextInSeries(prefix, suffix, labels) {
|
|
852
|
+
const re = new RegExp(`^${prefix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")} (\\d+)(?: |$)`);
|
|
853
|
+
let n = 0;
|
|
854
|
+
for (const l of labels) {
|
|
855
|
+
const m = l.match(re);
|
|
856
|
+
if (m)
|
|
857
|
+
n = Math.max(n, Number(m[1]));
|
|
858
|
+
}
|
|
859
|
+
return [prefix, String(n + 1).padStart(3, "0"), suffix ?? ""].filter(Boolean).join(" ");
|
|
860
|
+
}
|
|
861
|
+
const sourceValue = (s, i) => {
|
|
862
|
+
const src = s.source;
|
|
863
|
+
if ("literal" in src)
|
|
864
|
+
return { ok: true, value: src.literal };
|
|
865
|
+
if ("now" in src)
|
|
866
|
+
return { ok: true, value: i.now };
|
|
867
|
+
if ("none" in src)
|
|
868
|
+
return { ok: true, value: undefined };
|
|
869
|
+
if ("caller" in src) {
|
|
870
|
+
if (!i.caller)
|
|
871
|
+
return { ok: false, why: `no caller to link ${s.facet} to` };
|
|
872
|
+
return { ok: true, value: { $link: i.caller } };
|
|
873
|
+
}
|
|
874
|
+
if ("person" in src) {
|
|
875
|
+
if (!i.caller)
|
|
876
|
+
return { ok: false, why: `no caller to set ${s.facet} to` };
|
|
877
|
+
return { ok: true, value: i.caller };
|
|
878
|
+
}
|
|
879
|
+
if ("owner" in src) {
|
|
880
|
+
if (!i.owner)
|
|
881
|
+
return { ok: false, why: `no owner to set ${s.facet} to` };
|
|
882
|
+
return { ok: true, value: i.owner };
|
|
883
|
+
}
|
|
884
|
+
if ("run" in src) {
|
|
885
|
+
if (!i.run)
|
|
886
|
+
return { ok: false, why: `no act run to set ${s.facet} to` };
|
|
887
|
+
return { ok: true, value: i.run };
|
|
888
|
+
}
|
|
889
|
+
if ("link" in src) {
|
|
890
|
+
const id = src.link === "record" ? i.record.id : (i.found[src.link]?.[0]?.id ?? i.context[src.link]?.id);
|
|
891
|
+
// Revision 48: a link to a record the act makes is carried as `{$made: <name>}` for the host to resolve.
|
|
892
|
+
if (!id && i.made?.includes(src.link))
|
|
893
|
+
return { ok: true, value: { $made: src.link } };
|
|
894
|
+
if (!id)
|
|
895
|
+
return { ok: false, why: `nothing to link ${s.facet} to: ${src.link}` };
|
|
896
|
+
return { ok: true, value: { $link: id } };
|
|
897
|
+
}
|
|
898
|
+
if ("sum" in src)
|
|
899
|
+
return sumValue(src.sum, i);
|
|
900
|
+
if ("next" in src) {
|
|
901
|
+
const prefix = ("literal" in src.next.prefix ? src.next.prefix.literal : asWords(valueOf(src.next.prefix, i))).trim();
|
|
902
|
+
if (!prefix)
|
|
903
|
+
return { ok: false, why: `no prefix to number ${s.facet} by` };
|
|
904
|
+
const suffix = src.next.suffix ? asWords(valueOf(src.next.suffix, i)).trim() || undefined : undefined;
|
|
905
|
+
return { ok: true, value: nextInSeries(prefix, suffix, i.labels?.[`${src.next.in.kind}.${bare(src.next.in.facet)}`] ?? []) };
|
|
906
|
+
}
|
|
907
|
+
return { ok: true, value: valueOf(src, i) };
|
|
908
|
+
};
|
|
909
|
+
/** The values a mint makes (15.1), given the row taken, the context and the labels its series reads. Pure. */
|
|
910
|
+
export function mintValuesOf(f, i) {
|
|
911
|
+
if (!f.mint)
|
|
912
|
+
return { ok: false, why: `${f.name} mints nothing` };
|
|
913
|
+
const values = {};
|
|
914
|
+
for (const s of f.mint.sets) {
|
|
915
|
+
const v = sourceValue(s, i);
|
|
916
|
+
if (!v.ok)
|
|
917
|
+
return v;
|
|
918
|
+
if (v.value !== undefined && v.value !== "")
|
|
919
|
+
values[bare(s.facet)] = v.value;
|
|
920
|
+
}
|
|
921
|
+
return { ok: true, kind: f.mint.kind, values };
|
|
922
|
+
}
|
|
923
|
+
/** The post an act tells when it lands followed (15.1, revision 31): the kind, the aspect and the values, given the
|
|
924
|
+
* owner and the act run's id. The app asks `followersOn` first; with no follower on the record or its ancestors,
|
|
925
|
+
* nothing is made. Pure. */
|
|
926
|
+
export function tellsValuesOf(a, i) {
|
|
927
|
+
if (!a.tells)
|
|
928
|
+
return { ok: false, why: `${a.name} tells nothing` };
|
|
929
|
+
const values = {};
|
|
930
|
+
for (const s of a.tells.sets) {
|
|
931
|
+
const v = sourceValue(s, i);
|
|
932
|
+
if (!v.ok)
|
|
933
|
+
return v;
|
|
934
|
+
if (v.value !== undefined && v.value !== "")
|
|
935
|
+
values[bare(s.facet)] = v.value;
|
|
936
|
+
}
|
|
937
|
+
return { ok: true, kind: a.tells.kind, aspect: a.tells.aspect, values };
|
|
938
|
+
}
|
|
939
|
+
const said = (v) => v !== undefined && v !== null && v !== "";
|
|
940
|
+
/** What an act is given (15.1, revision 44), from what the person said: each given takes what was said, else its
|
|
941
|
+
* default. `asks` names the ones still to ask (no word said and no default); `opens` is what each question opens
|
|
942
|
+
* on (what was said, else its `from` as the record and the context stand, else its default). With the facets at
|
|
943
|
+
* hand (`facetOf`), a value its facet refuses is `refused` with its words, and is not among the values: a word
|
|
944
|
+
* where a number goes, a number outside the facet's range, a choice the facet lacks. Pure. */
|
|
945
|
+
export function givenOf(a, told, i, facetOf) {
|
|
946
|
+
const values = {};
|
|
947
|
+
const opens = {};
|
|
948
|
+
const asks = [];
|
|
949
|
+
const refused = [];
|
|
950
|
+
const problem = (g, v) => {
|
|
951
|
+
const f = facetOf?.(bare(g.facet));
|
|
952
|
+
if (!f)
|
|
953
|
+
return undefined;
|
|
954
|
+
const c = checkScalar(f, v);
|
|
955
|
+
if (c.flag === "range" && f.range)
|
|
956
|
+
return `${g.name} is ${f.range.min} to ${f.range.max}`;
|
|
957
|
+
if (c.outcome === "contradiction")
|
|
958
|
+
return `${g.name} is a ${f.type}${f.type === "choice" && f.values?.length ? `, one of ${f.values.map((x) => x.value).join(", ")}` : ""}`;
|
|
959
|
+
if (f.type === "choice" && !f.open && typeof v === "string" && !(f.values ?? []).some((x) => x.value === v || x.aliases.includes(v)))
|
|
960
|
+
return `${g.name} is one of ${(f.values ?? []).map((x) => x.value).join(", ")}`;
|
|
961
|
+
return undefined;
|
|
962
|
+
};
|
|
963
|
+
for (const g of a.given ?? []) {
|
|
964
|
+
const t = told[g.name];
|
|
965
|
+
const v = said(t) ? t : g.default;
|
|
966
|
+
const why = said(v) ? problem(g, v) : undefined;
|
|
967
|
+
if (why)
|
|
968
|
+
refused.push({ name: g.name, why });
|
|
969
|
+
else if (said(v))
|
|
970
|
+
values[g.name] = v;
|
|
971
|
+
else
|
|
972
|
+
asks.push(g.name);
|
|
973
|
+
const from = g.from ? valueOf(g.from, { ...i, found: {} }) : undefined;
|
|
974
|
+
const o = said(t) ? t : said(from) ? from : g.default;
|
|
975
|
+
if (said(o))
|
|
976
|
+
opens[g.name] = o;
|
|
977
|
+
}
|
|
978
|
+
return { values, asks, opens, refused };
|
|
979
|
+
}
|
|
980
|
+
/** The writes an act makes when it lands (15): pure, given what the finds answered or minted. A `first` find
|
|
981
|
+
* that answered nothing stops the act with the reason, before any write. */
|
|
982
|
+
export function actWrites(a, i) {
|
|
983
|
+
// Revision 42: what the record in hand must hold or lack, read before anything else.
|
|
984
|
+
for (const n of a.needs ?? []) {
|
|
985
|
+
// One hop: `<link facet>.<facet>` reads the record the record in hand links to, as the host handed it.
|
|
986
|
+
const [first = "", second] = n.facet.split(".");
|
|
987
|
+
const v = second === undefined ? i.record.values[bare(first)] : i.linked?.[bare(first)]?.values[second];
|
|
988
|
+
const holds = v !== undefined && v !== null && v !== "" && !(Array.isArray(v) && !v.length);
|
|
989
|
+
if (n.is) {
|
|
990
|
+
// Revision 51: the value as text beside the literal; a value not set is no value, so `=` fails and `!=` holds.
|
|
991
|
+
const same = holds && (typeof v === "string" || typeof v === "number" ? String(v) : serialize(v)) === n.is.value;
|
|
992
|
+
if (same !== (n.is.op === "="))
|
|
993
|
+
return { ok: false, why: n.words ?? `${second ?? bare(first)} is ${n.is.op === "=" ? "not " : ""}${n.is.value}` };
|
|
994
|
+
continue;
|
|
995
|
+
}
|
|
996
|
+
if (holds === !!n.lacks)
|
|
997
|
+
return { ok: false, why: n.words ?? (n.lacks ? `${second ?? bare(first)} is already set` : `${second ?? bare(first)} is not set`) };
|
|
998
|
+
}
|
|
999
|
+
// Revision 44: what the act is given is in hand before any write, a default standing in where nothing was said.
|
|
1000
|
+
if (a.given?.length) {
|
|
1001
|
+
const g = givenOf(a, i.given ?? {}, i, i.facetOf);
|
|
1002
|
+
if (g.refused[0])
|
|
1003
|
+
return { ok: false, why: g.refused[0].why };
|
|
1004
|
+
if (g.asks.length)
|
|
1005
|
+
return { ok: false, why: `${g.asks[0]} is not given` };
|
|
1006
|
+
i = { ...i, given: g.values };
|
|
1007
|
+
}
|
|
1008
|
+
for (const f of a.finds)
|
|
1009
|
+
if (f.take === "first" && !i.found[f.name]?.length)
|
|
1010
|
+
return { ok: false, why: `no ${f.kind} for ${f.name}` };
|
|
1011
|
+
if (a.makes?.length)
|
|
1012
|
+
i = { ...i, made: a.makes.map((m) => m.name) };
|
|
1013
|
+
const record = {};
|
|
1014
|
+
const clear = [];
|
|
1015
|
+
for (const s of a.sets) {
|
|
1016
|
+
if ("none" in s.source) {
|
|
1017
|
+
clear.push(bare(s.facet));
|
|
1018
|
+
continue;
|
|
1019
|
+
}
|
|
1020
|
+
const v = sourceValue(s, i);
|
|
1021
|
+
if (!v.ok)
|
|
1022
|
+
return v;
|
|
1023
|
+
if (v.value !== undefined)
|
|
1024
|
+
record[bare(s.facet)] = v.value;
|
|
1025
|
+
}
|
|
1026
|
+
const then = [];
|
|
1027
|
+
const stands = [];
|
|
1028
|
+
const sends = [];
|
|
1029
|
+
for (const t of a.thens) {
|
|
1030
|
+
const when = t.when;
|
|
1031
|
+
const holdsOn = (on) => whenHolds(when, on);
|
|
1032
|
+
// Revision 42: a term that reads a found record the then writes on is read on each of those records, and
|
|
1033
|
+
// the write is made on those where it holds. Any other condition is read once, as it always was.
|
|
1034
|
+
const reads = new Set(termsOf(when).flatMap((x) => ("ref" in x && "found" in x.ref ? [x.ref.found] : [])));
|
|
1035
|
+
const each = t.sets.some((s) => reads.has(s.found));
|
|
1036
|
+
if (t.stand) {
|
|
1037
|
+
// Revision 47: the standing of the record in hand or of each record found, where the condition holds on it.
|
|
1038
|
+
const on = t.stand.on;
|
|
1039
|
+
const perRecord = on !== "record" && reads.has(on);
|
|
1040
|
+
if (!perRecord && !holdsOn(i))
|
|
1041
|
+
continue;
|
|
1042
|
+
for (const rec of on === "record" ? [i.record] : (i.found[on] ?? [])) {
|
|
1043
|
+
if (perRecord && !holdsOn({ ...i, found: { ...i.found, [on]: [rec] } }))
|
|
1044
|
+
continue;
|
|
1045
|
+
const at = stands.findIndex((x) => x.id === rec.id);
|
|
1046
|
+
const next = { id: rec.id, to: t.stand.to, ...(t.stand.reason ? { reason: t.stand.reason } : {}) };
|
|
1047
|
+
if (at >= 0)
|
|
1048
|
+
stands[at] = next;
|
|
1049
|
+
else
|
|
1050
|
+
stands.push(next);
|
|
1051
|
+
}
|
|
1052
|
+
continue;
|
|
1053
|
+
}
|
|
1054
|
+
let held = each ? false : holdsOn(i);
|
|
1055
|
+
if (!each && !held)
|
|
1056
|
+
continue;
|
|
1057
|
+
for (const s of t.sets) {
|
|
1058
|
+
// Revision 39: a then's value is a set's: a link, a value copied, the facet cleared.
|
|
1059
|
+
const v = s.value;
|
|
1060
|
+
let value;
|
|
1061
|
+
if ("now" in v)
|
|
1062
|
+
value = i.now;
|
|
1063
|
+
else if ("literal" in v)
|
|
1064
|
+
value = v.literal;
|
|
1065
|
+
else if ("none" in v)
|
|
1066
|
+
value = undefined;
|
|
1067
|
+
else if ("caller" in v) {
|
|
1068
|
+
if (!i.caller)
|
|
1069
|
+
return { ok: false, why: `no caller to link ${s.found}.${s.facet} to` };
|
|
1070
|
+
value = { $link: i.caller };
|
|
1071
|
+
}
|
|
1072
|
+
else if ("person" in v) {
|
|
1073
|
+
if (!i.caller)
|
|
1074
|
+
return { ok: false, why: `no caller to set ${s.found}.${s.facet} to` };
|
|
1075
|
+
value = i.caller;
|
|
1076
|
+
}
|
|
1077
|
+
else if ("link" in v) {
|
|
1078
|
+
const id = v.link === "record" ? i.record.id : (i.found[v.link]?.[0]?.id ?? i.context[v.link]?.id);
|
|
1079
|
+
if (!id && !i.made?.includes(v.link))
|
|
1080
|
+
return { ok: false, why: `nothing to link ${s.found}.${s.facet} to: ${v.link}` };
|
|
1081
|
+
value = id ? { $link: id } : { $made: v.link };
|
|
1082
|
+
}
|
|
1083
|
+
else if ("sum" in v) {
|
|
1084
|
+
const n = sumValue(v.sum, i);
|
|
1085
|
+
if (!n.ok)
|
|
1086
|
+
return n;
|
|
1087
|
+
value = n.value;
|
|
1088
|
+
}
|
|
1089
|
+
else
|
|
1090
|
+
value = valueOf(v, i);
|
|
1091
|
+
const k = bare(s.facet);
|
|
1092
|
+
// Revision 45: `record.<facet>` writes on the record in hand, over what its sets wrote.
|
|
1093
|
+
if (s.found === "record") {
|
|
1094
|
+
if ("none" in v) {
|
|
1095
|
+
delete record[k];
|
|
1096
|
+
if (!clear.includes(k))
|
|
1097
|
+
clear.push(k);
|
|
1098
|
+
}
|
|
1099
|
+
else if (value !== undefined) {
|
|
1100
|
+
record[k] = value;
|
|
1101
|
+
const at = clear.indexOf(k);
|
|
1102
|
+
if (at >= 0)
|
|
1103
|
+
clear.splice(at, 1);
|
|
1104
|
+
}
|
|
1105
|
+
continue;
|
|
1106
|
+
}
|
|
1107
|
+
for (const rec of i.found[s.found] ?? []) {
|
|
1108
|
+
if (each) {
|
|
1109
|
+
if (!holdsOn(reads.has(s.found) ? { ...i, found: { ...i.found, [s.found]: [rec] } } : i))
|
|
1110
|
+
continue;
|
|
1111
|
+
held = true;
|
|
1112
|
+
}
|
|
1113
|
+
let have = then.find((x) => x.id === rec.id);
|
|
1114
|
+
if (!have)
|
|
1115
|
+
then.push((have = { id: rec.id, values: {} }));
|
|
1116
|
+
if ("none" in v) {
|
|
1117
|
+
delete have.values[k];
|
|
1118
|
+
have.clear = [...(have.clear ?? []).filter((c) => c !== k), k];
|
|
1119
|
+
}
|
|
1120
|
+
else if (value !== undefined) {
|
|
1121
|
+
have.values[k] = value;
|
|
1122
|
+
if (have.clear)
|
|
1123
|
+
have.clear = have.clear.filter((c) => c !== k);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
if (t.send && held)
|
|
1128
|
+
sends.push({ ...t.send });
|
|
1129
|
+
}
|
|
1130
|
+
// A mark is taken once (15.2, revision 34): a write to a mark that holds a real value stops the act before
|
|
1131
|
+
// any write, with the reason; clearing one (`none`) is how an act gives it back.
|
|
1132
|
+
const held = (r, k) => { const v = r.values[k]; return v !== undefined && v !== "" && !(Array.isArray(v) && !v.length); };
|
|
1133
|
+
for (const m of i.marks ?? []) {
|
|
1134
|
+
const k = bare(m);
|
|
1135
|
+
if (k in record && held(i.record, k))
|
|
1136
|
+
return { ok: false, why: `${k} is taken: a mark is set once` };
|
|
1137
|
+
for (const t of then) {
|
|
1138
|
+
const rec = Object.values(i.found).flat().find((x) => x.id === t.id);
|
|
1139
|
+
if (k in t.values && rec && held(rec, k))
|
|
1140
|
+
return { ok: false, why: `${k} is taken on ${t.id}: a mark is set once` };
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
// Revision 45: the records made, each read as the record in hand stood before the act's writes.
|
|
1144
|
+
const makes = [];
|
|
1145
|
+
for (const m of a.makes ?? []) {
|
|
1146
|
+
if (!whenHolds(m.when, i))
|
|
1147
|
+
continue;
|
|
1148
|
+
const values = {};
|
|
1149
|
+
for (const s of m.sets) {
|
|
1150
|
+
const v = sourceValue(s, i);
|
|
1151
|
+
if (!v.ok)
|
|
1152
|
+
return v;
|
|
1153
|
+
if (v.value !== undefined && v.value !== "")
|
|
1154
|
+
values[bare(s.facet)] = v.value;
|
|
1155
|
+
}
|
|
1156
|
+
makes.push({ name: m.name, kind: m.kind, values });
|
|
1157
|
+
}
|
|
1158
|
+
// Revision 48: a link to a make that was not made (its `when` did not hold) writes nothing.
|
|
1159
|
+
if (a.makes?.length) {
|
|
1160
|
+
const was = new Set(makes.map((m) => m.name));
|
|
1161
|
+
const sweep = (values) => {
|
|
1162
|
+
for (const [k, v] of Object.entries(values))
|
|
1163
|
+
if (v && typeof v === "object" && !Array.isArray(v) && typeof v.$made === "string" && !was.has(String(v.$made)))
|
|
1164
|
+
delete values[k];
|
|
1165
|
+
};
|
|
1166
|
+
sweep(record);
|
|
1167
|
+
for (const t of then)
|
|
1168
|
+
sweep(t.values);
|
|
1169
|
+
for (const m of makes)
|
|
1170
|
+
sweep(m.values);
|
|
1171
|
+
}
|
|
1172
|
+
// Revision 47: the acts run once this one has landed, each read as the record in hand stood.
|
|
1173
|
+
const also = runsOf(a.also, i);
|
|
1174
|
+
return { ok: true, record, clear, then, ...(a.makes?.length ? { makes } : {}), ...(a.thens.some((t) => t.stand) ? { stands } : {}), ...(a.also?.length ? { also } : {}), sends };
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* A share of a record (revision 35): `take` of the quantity its level declares. All of it is the record itself;
|
|
1178
|
+
* fewer is a split, the count the new record takes and the count the first keeps, for the host to write in the
|
|
1179
|
+
* act's own entry (a new record of the same level under the same parent, carrying every value of the first).
|
|
1180
|
+
* None, more than it holds, or a count that is no whole number is refused.
|
|
1181
|
+
*/
|
|
1182
|
+
export function shareOf(record, facet, take) {
|
|
1183
|
+
const raw = record.values[bare(facet)];
|
|
1184
|
+
const have = raw === undefined || raw === null || raw === "" ? 1 : Number(typeof raw === "object" && !Array.isArray(raw) && "$qty" in raw ? raw.$qty : raw);
|
|
1185
|
+
if (!Number.isInteger(have) || have < 1)
|
|
1186
|
+
return { ok: false, why: `${record.id} holds no whole quantity to take from` };
|
|
1187
|
+
if (!Number.isInteger(take) || take < 1 || take > have)
|
|
1188
|
+
return { ok: false, why: `take 1 to ${have} of ${record.id}` };
|
|
1189
|
+
if (take === have)
|
|
1190
|
+
return { ok: true, whole: true };
|
|
1191
|
+
return { ok: true, whole: false, taken: String(take), rest: String(have - take) };
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* The facets that are marks (12.3, revision 34): those a marked filter names. A host hands them to `actWrites`
|
|
1195
|
+
* so a second write to a mark is refused where the act lands.
|
|
1196
|
+
*/
|
|
1197
|
+
export function markFacets(filters) {
|
|
1198
|
+
const out = new Set();
|
|
1199
|
+
for (const f of filters)
|
|
1200
|
+
if (f.marked)
|
|
1201
|
+
out.add(f.facet);
|
|
1202
|
+
return [...out];
|
|
1203
|
+
}
|
|
1204
|
+
/** The acts a list of run lines names, each on its record with what it is given, read as the act stands. */
|
|
1205
|
+
function runsOf(list, i) {
|
|
1206
|
+
const out = [];
|
|
1207
|
+
for (const x of list ?? []) {
|
|
1208
|
+
const perRecord = x.on !== "record" && termsOf(x.when).some((t) => "ref" in t && "found" in t.ref && t.ref.found === x.on);
|
|
1209
|
+
if (!perRecord && !whenHolds(x.when, i))
|
|
1210
|
+
continue;
|
|
1211
|
+
for (const rec of x.on === "record" ? [i.record] : (i.found[x.on] ?? [])) {
|
|
1212
|
+
const at = x.on === "record" ? i : { ...i, found: { ...i.found, [x.on]: [rec] } };
|
|
1213
|
+
if (perRecord && !whenHolds(x.when, at))
|
|
1214
|
+
continue;
|
|
1215
|
+
const given = {};
|
|
1216
|
+
for (const g of x.with ?? []) {
|
|
1217
|
+
const v = "literal" in g.value ? g.value.literal : valueOf(g.value, at);
|
|
1218
|
+
if (v !== undefined && v !== null)
|
|
1219
|
+
given[g.name] = v;
|
|
1220
|
+
}
|
|
1221
|
+
out.push({ ...(x.utility ? { utility: x.utility } : {}), act: x.act, id: rec.id, ...(x.asPlace ? { asPlace: true } : {}), ...(x.with?.length ? { given } : {}) });
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
return out;
|
|
1225
|
+
}
|
|
1226
|
+
/** Revision 49: the acts to run before this one lands, each on its record. The host runs them in order, stops
|
|
1227
|
+
* the act with the reason of the first that refuses, and reads the act's finds again before it lands. */
|
|
1228
|
+
export function firstOf(a, i) {
|
|
1229
|
+
return runsOf(a.first, i);
|
|
1230
|
+
}
|
|
1231
|
+
/** The picks of a find or a count resolved for the host to run: a literal, or the referenced value as it stands.
|
|
1232
|
+
* A pick's facet may be `<link>.<facet>`, carried as written for the host to hop. */
|
|
1233
|
+
export function findPicksOf(f, i) {
|
|
1234
|
+
const at = { record: i.record, context: i.context, found: i.found ?? {}, ...(i.route ? { route: i.route } : {}) };
|
|
1235
|
+
return f.where.map((p) => ({ facet: p.facet.includes("::") ? p.facet.replace(/^\w+::/, "") : p.facet, value: "literal" in p.value ? p.value.literal : "caller" in p.value ? i.caller : "self" in p.value ? i.record.id : valueOf(p.value, at) }));
|
|
1236
|
+
}
|
|
1237
|
+
// ─── JSON and text ──────────────────────────────────────────────────────────────
|
|
1238
|
+
const refJson = (r) => "given" in r ? { given: r.given } : "record" in r ? { record: r.record } : "context" in r ? { context: r.context, ...(r.facet ? { facet: r.facet } : {}) } : "route" in r ? { route: r.route, column: r.column } : { found: r.found, ...(r.facet ? { facet: r.facet } : {}) };
|
|
1239
|
+
const refText = (r) => "given" in r ? `given.${r.given}` : "record" in r ? (r.record ? `record.${r.record}` : "record") : "context" in r ? `context.${r.context}${r.facet ? `.${r.facet}` : ""}` : "route" in r ? `${r.route}.${r.column}` : r.facet ? `${r.found}.${r.facet}` : r.found;
|
|
1240
|
+
const operandJson = (o) => ("number" in o ? { number: o.number } : refJson(o));
|
|
1241
|
+
const sumJson = (x) => ({ sum: { left: operandJson(x.left), op: x.op, right: operandJson(x.right) } });
|
|
1242
|
+
const condJson = (c) => ("has" in c ? { has: refJson(c.has), ...(c.lacks ? { lacks: true } : {}), ...(c.is !== undefined ? { is: c.is } : {}) } : { left: termJson(c.left), op: c.op, right: termJson(c.right) });
|
|
1243
|
+
export const whenJson = (w) => ({ ...condJson(w), ...(w.and?.length ? { and: w.and.map(condJson) } : {}) });
|
|
1244
|
+
const termJson = (t) => ("number" in t ? { number: t.number } : "count" in t ? { count: t.count, ...(t.plus !== undefined ? { plus: t.plus } : {}) } : { ref: refJson(t.ref), ...(t.plus !== undefined ? { plus: t.plus } : {}) });
|
|
1245
|
+
const termText = (t) => ("number" in t ? String(t.number) : `${"count" in t ? t.count : refText(t.ref)}${t.plus !== undefined ? ` + ${t.plus}` : ""}`);
|
|
1246
|
+
/** The quoting the text form needs for a literal (2.5): words with a space, a comma or a colon are quoted. */
|
|
1247
|
+
const q = (s) => (/[\s,:]|^$/.test(s) ? JSON.stringify(s) : s);
|
|
1248
|
+
const runJson = (x) => ({ ...(x.utility ? { utility: x.utility } : {}), act: x.act, on: x.on, ...(x.asPlace ? { as: "place" } : {}), ...(x.with?.length ? { with: x.with.map((g) => ({ name: g.name, value: "literal" in g.value ? { literal: g.value.literal } : refJson(g.value) })) } : {}), ...(x.when ? { when: whenJson(x.when) } : {}) });
|
|
1249
|
+
const pickJson = (p) => ({ facet: p.facet, value: "literal" in p.value ? { literal: p.value.literal } : "caller" in p.value ? { caller: true } : "self" in p.value ? { self: true } : refJson(p.value) });
|
|
1250
|
+
const thenValueJson = (v) => "literal" in v ? { literal: v.literal } : "now" in v ? { now: true } : "none" in v ? { none: true } : "caller" in v ? { caller: true } : "person" in v ? { person: true } : "link" in v ? { link: v.link } : "sum" in v ? sumJson(v.sum) : refJson(v);
|
|
1251
|
+
const sourceJson = (s) => "literal" in s ? { literal: s.literal } : "link" in s ? { link: s.link } : "now" in s ? { now: true } : "caller" in s ? { caller: true } : "person" in s ? { person: true } : "none" in s ? { none: true } : "owner" in s ? { owner: true } : "run" in s ? { run: true } : "sum" in s ? sumJson(s.sum) : "next" in s ? { next: { prefix: "literal" in s.next.prefix ? { literal: s.next.prefix.literal } : refJson(s.next.prefix), in: { kind: s.next.in.kind, facet: s.next.in.facet }, ...(s.next.suffix ? { suffix: refJson(s.next.suffix) } : {}) } } : refJson(s);
|
|
1252
|
+
const setJson = (s) => ({ facet: s.facet, source: sourceJson(s.source) });
|
|
1253
|
+
export function actJson(a) {
|
|
1254
|
+
return {
|
|
1255
|
+
act: a.name,
|
|
1256
|
+
version: a.version,
|
|
1257
|
+
utility: a.utility,
|
|
1258
|
+
...(a.replaces ? { replaces: a.replaces } : {}),
|
|
1259
|
+
...(a.label ? { label: a.label } : {}),
|
|
1260
|
+
...(a.describe ? { describe: a.describe } : {}),
|
|
1261
|
+
on: a.on,
|
|
1262
|
+
...(a.takes ? { takes: a.takes } : {}),
|
|
1263
|
+
...(a.context.length ? { context: [...a.context] } : {}),
|
|
1264
|
+
...(a.needs?.length ? { needs: a.needs.map((n) => ({ facet: n.facet, ...(n.lacks ? { lacks: true } : {}), ...(n.is ? { is: { op: n.is.op, value: n.is.value } } : {}), ...(n.words ? { words: n.words } : {}) })) } : {}),
|
|
1265
|
+
...(a.given?.length ? { given: a.given.map((g) => ({ name: g.name, facet: g.facet, ...(g.words ? { words: g.words } : {}), ...(g.from ? { from: refJson(g.from) } : {}), ...(g.default !== undefined ? { default: g.default } : {}) })) } : {}),
|
|
1266
|
+
...(a.route
|
|
1267
|
+
? {
|
|
1268
|
+
route: {
|
|
1269
|
+
name: a.route.name,
|
|
1270
|
+
rows: refJson(a.route.rows),
|
|
1271
|
+
by: a.route.by,
|
|
1272
|
+
over: a.route.over.map(refJson),
|
|
1273
|
+
...(a.route.keep ? { keep: { column: a.route.keep.column, kind: a.route.keep.kind, where: a.route.keep.where.map(pickJson) } } : {}),
|
|
1274
|
+
},
|
|
1275
|
+
}
|
|
1276
|
+
: {}),
|
|
1277
|
+
finds: a.finds.map((f) => ({
|
|
1278
|
+
name: f.name,
|
|
1279
|
+
take: f.take,
|
|
1280
|
+
kind: f.kind,
|
|
1281
|
+
where: f.where.map(pickJson),
|
|
1282
|
+
...(f.order ? { order: { facet: f.order.facet, dir: f.order.dir } } : {}),
|
|
1283
|
+
...(f.mint ? { mint: { kind: f.mint.kind, sets: f.mint.sets.map(setJson) } } : {}),
|
|
1284
|
+
})),
|
|
1285
|
+
...(a.counts.length ? { counts: a.counts.map((k) => ({ name: k.name, kind: k.kind, where: k.where.map(pickJson) })) } : {}),
|
|
1286
|
+
sets: a.sets.map(setJson),
|
|
1287
|
+
thens: a.thens.map((t) => ({
|
|
1288
|
+
...(t.when ? { when: whenJson(t.when) } : {}),
|
|
1289
|
+
sets: t.sets.map((s) => ({ found: s.found, facet: s.facet, value: thenValueJson(s.value) })),
|
|
1290
|
+
...(t.stand ? { stand: { to: t.stand.to, on: t.stand.on, ...(t.stand.reason ? { reason: t.stand.reason } : {}) } } : {}),
|
|
1291
|
+
...(t.send ? { send: { verb: t.send.verb, words: t.send.words } } : {}),
|
|
1292
|
+
})),
|
|
1293
|
+
...(a.makes?.length ? { makes: a.makes.map((m) => ({ name: m.name, kind: m.kind, ...(m.when ? { when: whenJson(m.when) } : {}), sets: m.sets.map(setJson) })) } : {}),
|
|
1294
|
+
...(a.also?.length ? { also: a.also.map(runJson) } : {}),
|
|
1295
|
+
...(a.first?.length ? { first: a.first.map(runJson) } : {}),
|
|
1296
|
+
...(a.send ? { send: { verb: a.send.verb, words: a.send.words } } : {}),
|
|
1297
|
+
lands: a.lands,
|
|
1298
|
+
...(a.tells ? { tells: { kind: a.tells.kind, aspect: a.tells.aspect, sets: a.tells.sets.map(setJson) } } : {}),
|
|
1299
|
+
};
|
|
1300
|
+
}
|
|
1301
|
+
const refOfJson = (o) => typeof o.given === "string"
|
|
1302
|
+
? { given: o.given }
|
|
1303
|
+
: typeof o.record === "string"
|
|
1304
|
+
? { record: o.record }
|
|
1305
|
+
: typeof o.context === "string"
|
|
1306
|
+
? { context: o.context, ...(typeof o.facet === "string" ? { facet: o.facet } : {}) }
|
|
1307
|
+
: typeof o.route === "string"
|
|
1308
|
+
? { route: o.route, column: String(o.column) }
|
|
1309
|
+
: { found: String(o.found), ...(typeof o.facet === "string" ? { facet: o.facet } : {}) };
|
|
1310
|
+
const termOfJson = (o) => typeof o.number === "number" ? { number: o.number } : typeof o.count === "string" ? { count: o.count, ...(typeof o.plus === "number" ? { plus: o.plus } : {}) } : { ref: refOfJson(o.ref), ...(typeof o.plus === "number" ? { plus: o.plus } : {}) };
|
|
1311
|
+
const pickText = (p) => {
|
|
1312
|
+
const v = p.value;
|
|
1313
|
+
// The words `record` and `caller` as literals are printed quoted, so they read back as the words (revision 39).
|
|
1314
|
+
return `${p.facet} = ${typeof v.literal === "string" ? (v.literal === "record" || v.literal === "caller" ? JSON.stringify(v.literal) : q(v.literal)) : v.caller === true ? "caller" : v.self === true ? "record" : refText(refOfJson(v))}`;
|
|
1315
|
+
};
|
|
1316
|
+
const operandText = (o) => (typeof o.number === "number" ? String(o.number) : refText(refOfJson(o)));
|
|
1317
|
+
const sumText = (x) => `${operandText(x.left)} ${x.op} ${operandText(x.right)}`;
|
|
1318
|
+
export const whenText = (w) => [w, ...(w.and ?? [])].map((c) => c.has && typeof c.has === "object" ? (typeof c.is === "string" ? `${refText(refOfJson(c.has))} is ${c.lacks === true ? "not " : ""}${c.is}` : `${c.lacks === true ? "no" : "has"} ${refText(refOfJson(c.has))}`) : `${termText(termOfJson(c.left))} ${c.op} ${termText(termOfJson(c.right))}`).join(" and ");
|
|
1319
|
+
const sourceText = (src) => {
|
|
1320
|
+
if (typeof src.literal === "string")
|
|
1321
|
+
return src.literal === "caller" ? JSON.stringify(src.literal) : q(src.literal);
|
|
1322
|
+
if (src.sum && typeof src.sum === "object")
|
|
1323
|
+
return sumText(src.sum);
|
|
1324
|
+
if (typeof src.link === "string")
|
|
1325
|
+
return `link ${src.link}`;
|
|
1326
|
+
if (src.now === true)
|
|
1327
|
+
return "now";
|
|
1328
|
+
if (src.caller === true)
|
|
1329
|
+
return "link caller";
|
|
1330
|
+
if (src.person === true)
|
|
1331
|
+
return "caller";
|
|
1332
|
+
if (src.none === true)
|
|
1333
|
+
return "none";
|
|
1334
|
+
if (src.owner === true)
|
|
1335
|
+
return "owner";
|
|
1336
|
+
if (src.run === true)
|
|
1337
|
+
return "run";
|
|
1338
|
+
if (src.next && typeof src.next === "object") {
|
|
1339
|
+
const n = src.next;
|
|
1340
|
+
const into = n.in;
|
|
1341
|
+
return `next ${typeof n.prefix.literal === "string" ? JSON.stringify(n.prefix.literal) : refText(refOfJson(n.prefix))} in ${into.kind}.${into.facet}${n.suffix ? `, ${refText(refOfJson(n.suffix))}` : ""}`;
|
|
1342
|
+
}
|
|
1343
|
+
return refText(refOfJson(src));
|
|
1344
|
+
};
|
|
1345
|
+
/** The text of an act's JSON form, as the bundle prints it back. */
|
|
1346
|
+
export function actText(a) {
|
|
1347
|
+
const out = [`act: ${a.act} /${a.version}`, ` utility: ${a.utility}`];
|
|
1348
|
+
if (typeof a.replaces === "string")
|
|
1349
|
+
out.push(` replaces: ${a.replaces}`);
|
|
1350
|
+
if (typeof a.label === "string")
|
|
1351
|
+
out.push(` label: ${a.label}`);
|
|
1352
|
+
if (typeof a.describe === "string")
|
|
1353
|
+
out.push(` describe: ${a.describe}`);
|
|
1354
|
+
out.push(` on: ${a.on}`);
|
|
1355
|
+
if (a.takes === "share")
|
|
1356
|
+
out.push(" takes: share");
|
|
1357
|
+
if (Array.isArray(a.context) && a.context.length)
|
|
1358
|
+
out.push(` context: ${a.context.join(", ")}`);
|
|
1359
|
+
if (Array.isArray(a.needs) && a.needs.length) {
|
|
1360
|
+
out.push(" needs:");
|
|
1361
|
+
for (const n of a.needs)
|
|
1362
|
+
out.push(` ${n.lacks === true ? "no " : ""}${n.facet}${n.is && typeof n.is === "object" ? ` ${n.is.op} ${n.is.value}` : ""}${typeof n.words === "string" ? ` "${n.words}"` : ""}`);
|
|
1363
|
+
}
|
|
1364
|
+
if (Array.isArray(a.given) && a.given.length) {
|
|
1365
|
+
out.push(" given:");
|
|
1366
|
+
for (const g of a.given)
|
|
1367
|
+
out.push(` ${g.name} as ${g.facet}${typeof g.words === "string" ? ` "${g.words}"` : ""}${g.from && typeof g.from === "object" ? ` from ${refText(refOfJson(g.from))}` : typeof g.default === "string" ? ` default ${q(g.default)}` : ""}`);
|
|
1368
|
+
}
|
|
1369
|
+
if (a.route && typeof a.route === "object") {
|
|
1370
|
+
const r = a.route;
|
|
1371
|
+
const keep = r.keep;
|
|
1372
|
+
const over = (r.over ?? []).map((o) => refText(refOfJson(o))).join(", ");
|
|
1373
|
+
out.push(" route:");
|
|
1374
|
+
out.push(` ${r.name} -> row of ${refText(refOfJson(r.rows))} by ${r.by} over ${over}${keep ? ` keep ${keep.column} by ${keep.kind}${(keep.where ?? []).length ? ` where ${keep.where.map(pickText).join(", ")}` : ""}` : ""}`);
|
|
1375
|
+
}
|
|
1376
|
+
const finds = a.finds ?? [];
|
|
1377
|
+
if (finds.length) {
|
|
1378
|
+
out.push(" find:");
|
|
1379
|
+
for (const f of finds) {
|
|
1380
|
+
const where = (f.where ?? []).map(pickText);
|
|
1381
|
+
const order = f.order;
|
|
1382
|
+
out.push(` ${f.name} -> ${f.take} ${f.kind}${where.length ? ` where ${where.join(", ")}` : ""}${order ? ` order ${order.facet}${order.dir === "before" ? " before" : ""}` : ""}`);
|
|
1383
|
+
}
|
|
1384
|
+
const mints = finds.filter((f) => f.mint && typeof f.mint === "object");
|
|
1385
|
+
if (mints.length) {
|
|
1386
|
+
out.push(" mint:");
|
|
1387
|
+
for (const f of mints) {
|
|
1388
|
+
const m = f.mint;
|
|
1389
|
+
out.push(` ${f.name} -> ${m.kind} when none`);
|
|
1390
|
+
for (const s of m.sets ?? [])
|
|
1391
|
+
out.push(` ${s.facet} -> ${sourceText(s.source)}`);
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
const counts = a.counts ?? [];
|
|
1396
|
+
if (counts.length) {
|
|
1397
|
+
out.push(" count:");
|
|
1398
|
+
for (const k of counts) {
|
|
1399
|
+
const where = (k.where ?? []).map(pickText);
|
|
1400
|
+
out.push(` ${k.name} -> ${k.kind}${where.length ? ` where ${where.join(", ")}` : ""}`);
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
const sets = a.sets ?? [];
|
|
1404
|
+
if (sets.length) {
|
|
1405
|
+
out.push(" set:");
|
|
1406
|
+
for (const s of sets)
|
|
1407
|
+
out.push(` ${s.facet} -> ${sourceText(s.source)}`);
|
|
1408
|
+
}
|
|
1409
|
+
const thens = a.thens ?? [];
|
|
1410
|
+
if (thens.length) {
|
|
1411
|
+
out.push(" then:");
|
|
1412
|
+
for (const t of thens) {
|
|
1413
|
+
const w = t.when;
|
|
1414
|
+
// Revision 25 carried one set as `set`; revision 26 a list as `sets`.
|
|
1415
|
+
const one = t.set;
|
|
1416
|
+
const list = t.sets ?? (one ? [{ found: one.found, facet: one.facet, value: { literal: String(one.value) } }] : []);
|
|
1417
|
+
const words = list.map((s) => {
|
|
1418
|
+
const v = s.value;
|
|
1419
|
+
return `${s.found}.${s.facet} = ${v.now === true ? "now" : v.none === true ? "none" : v.caller === true ? "link caller" : v.person === true ? "caller" : typeof v.link === "string" ? `link ${v.link}` : v.sum && typeof v.sum === "object" ? sumText(v.sum) : typeof v.literal === "string" ? (/^(now|none|caller)$|^link\s|^[\w-]+\.[\w:.-]+$/.test(v.literal) ? JSON.stringify(v.literal) : q(v.literal)) : refText(refOfJson(v))}`;
|
|
1420
|
+
});
|
|
1421
|
+
const send = t.send;
|
|
1422
|
+
const stand = t.stand;
|
|
1423
|
+
if (stand) {
|
|
1424
|
+
out.push(` ${w ? `when ${whenText(w)} ` : ""}${stand.to === "archived" ? "archive" : "restore"} ${stand.on}${typeof stand.reason === "string" ? ` as ${stand.reason}` : ""}`);
|
|
1425
|
+
continue;
|
|
1426
|
+
}
|
|
1427
|
+
out.push(` ${w ? `when ${whenText(w)} ` : ""}set ${words.join(", ")}${send ? ` send ${send.verb}${send.words ? ` ${send.words}` : ""}` : ""}`);
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
const makes = a.makes ?? [];
|
|
1431
|
+
if (makes.length) {
|
|
1432
|
+
out.push(" make:");
|
|
1433
|
+
for (const m of makes) {
|
|
1434
|
+
out.push(` ${m.name} -> ${m.kind}${m.when && typeof m.when === "object" ? ` when ${whenText(m.when)}` : ""}`);
|
|
1435
|
+
for (const s of m.sets ?? [])
|
|
1436
|
+
out.push(` ${s.facet} -> ${sourceText(s.source)}`);
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
for (const key of ["first", "also"]) {
|
|
1440
|
+
const runs = a[key] ?? [];
|
|
1441
|
+
if (!runs.length)
|
|
1442
|
+
continue;
|
|
1443
|
+
out.push(` ${key}:`);
|
|
1444
|
+
for (const x of runs) {
|
|
1445
|
+
const gives = (x.with ?? []).map((g) => {
|
|
1446
|
+
const v = g.value;
|
|
1447
|
+
return `${g.name} = ${typeof v.literal === "string" ? (isRef(v.literal) ? JSON.stringify(v.literal) : q(v.literal)) : refText(refOfJson(v))}`;
|
|
1448
|
+
});
|
|
1449
|
+
out.push(` ${typeof x.utility === "string" ? `${x.utility}/` : ""}${x.act} on ${x.on}${x.as === "place" ? " as place" : ""}${gives.length ? ` with ${gives.join(", ")}` : ""}${x.when && typeof x.when === "object" ? ` when ${whenText(x.when)}` : ""}`);
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
if (a.send && typeof a.send === "object") {
|
|
1453
|
+
const s = a.send;
|
|
1454
|
+
out.push(` send: ${s.verb}${s.words ? ` ${s.words}` : ""}`);
|
|
1455
|
+
}
|
|
1456
|
+
if (a.lands === "reports")
|
|
1457
|
+
out.push(" lands: when the send reports");
|
|
1458
|
+
if (a.tells && typeof a.tells === "object") {
|
|
1459
|
+
const t = a.tells;
|
|
1460
|
+
out.push(` tells: ${t.kind} as ${t.aspect}`);
|
|
1461
|
+
for (const s of t.sets ?? [])
|
|
1462
|
+
out.push(` ${s.facet} -> ${sourceText(s.source)}`);
|
|
1463
|
+
}
|
|
1464
|
+
return out;
|
|
1465
|
+
}
|
|
1466
|
+
//# sourceMappingURL=acts.js.map
|