gdp-wired 0.23.0 → 0.25.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 +12 -2
- package/dist/core/accel.d.ts +44 -0
- package/dist/core/accel.js +720 -0
- package/dist/core/bmc.d.ts +13 -2
- package/dist/core/bmc.js +390 -18
- package/dist/core/cycles.d.ts +23 -0
- package/dist/core/cycles.js +527 -146
- package/dist/core/data.d.ts +17 -0
- package/dist/core/data.js +1177 -213
- package/dist/core/discover.js +8 -3
- package/dist/core/dsl.d.ts +7 -2
- package/dist/core/dsl.js +47 -9
- package/dist/core/extract-check.d.ts +77 -0
- package/dist/core/extract-check.js +785 -0
- package/dist/core/extract-ir.d.ts +139 -0
- package/dist/core/extract-ir.js +198 -0
- package/dist/core/extract-print.d.ts +30 -0
- package/dist/core/extract-print.js +426 -0
- package/dist/core/extract-read.d.ts +298 -0
- package/dist/core/extract-read.js +1298 -0
- package/dist/core/extract-shapes.d.ts +13 -0
- package/dist/core/extract-shapes.js +993 -0
- package/dist/core/extract-xstate.d.ts +34 -0
- package/dist/core/extract-xstate.js +320 -0
- package/dist/core/extract.d.ts +31 -0
- package/dist/core/extract.js +249 -0
- package/dist/core/infer.d.ts +1 -0
- package/dist/core/infer.js +298 -28
- package/dist/core/kind.d.ts +30 -0
- package/dist/core/kind.js +81 -0
- package/dist/core/nonterm.d.ts +4 -1
- package/dist/core/nonterm.js +98 -2
- package/dist/core/relation.js +82 -7
- package/dist/core/transfer.js +1 -1
- package/dist/core/xstate.d.ts +6 -0
- package/dist/core/xstate.js +70 -12
- package/dist/kernel/cdcl.d.ts +15 -0
- package/dist/kernel/cdcl.js +331 -244
- package/dist/kernel/kernel.d.ts +15 -0
- package/dist/kernel/kernel.js +32 -0
- package/dist/kernel/prove.js +20 -7
- package/dist/kernel/reader.d.ts +14 -0
- package/dist/kernel/reader.js +102 -14
- package/dist/lean-kernel/emit.d.ts +28 -1
- package/dist/lean-kernel/emit.js +56 -15
- package/dist/lean-kernel/library.js +2 -1
- package/package.json +12 -3
|
@@ -0,0 +1,1298 @@
|
|
|
1
|
+
// The extractor's reader (core/extract.ts): TypeScript (or JavaScript) read through the compiler's own parser and type
|
|
2
|
+
// checker, and one step of it executed symbolically. A path is a condition (the branches taken), the locals, a heap of
|
|
3
|
+
// the objects and maps the step can reach, and the writes so far; branches fork paths and merge them again where they
|
|
4
|
+
// meet, so a step is one term per state variable, built by cases. What isn't in the subset is refused where it is, with
|
|
5
|
+
// the reason: nothing outside it is approximated. Nothing here is trusted either: the rule printed from what this reads
|
|
6
|
+
// is run against the code itself (core/extract-check.ts) before it is handed out.
|
|
7
|
+
import ts from 'typescript';
|
|
8
|
+
import * as I from "./extract-ir.js";
|
|
9
|
+
export class Refusal extends Error {
|
|
10
|
+
where;
|
|
11
|
+
what;
|
|
12
|
+
constructor(where, what) {
|
|
13
|
+
super(`${where}: ${what}`);
|
|
14
|
+
this.where = where;
|
|
15
|
+
this.what = what;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// An untyped input read as one sort and used as another (a field of a JavaScript action, written to a string): the
|
|
19
|
+
// read starts again with the input of that sort.
|
|
20
|
+
export class Retype extends Error {
|
|
21
|
+
path;
|
|
22
|
+
sort;
|
|
23
|
+
constructor(path, sort) {
|
|
24
|
+
super(`${path} is a ${sort.k}`);
|
|
25
|
+
this.path = path;
|
|
26
|
+
this.sort = sort;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const where = (n) => {
|
|
30
|
+
const sf = n.getSourceFile(), { line, character } = sf.getLineAndCharacterOfPosition(n.getStart());
|
|
31
|
+
return `${sf.fileName.split('/').pop()}:${line + 1}:${character + 1}`;
|
|
32
|
+
};
|
|
33
|
+
const refuse = (n, what) => { throw new Refusal(where(n), what); };
|
|
34
|
+
const text = (n) => n.getText().replace(/\s+/g, ' ').slice(0, 60);
|
|
35
|
+
// --- the program ----------------------------------------------------------------------------------------------------
|
|
36
|
+
// A path, normalized (posix: the files are a repository's).
|
|
37
|
+
export const normalize = (path) => {
|
|
38
|
+
const out = [];
|
|
39
|
+
for (const seg of path.split('/')) {
|
|
40
|
+
if (seg === '..')
|
|
41
|
+
out.pop();
|
|
42
|
+
else if (seg !== '.' && seg !== '')
|
|
43
|
+
out.push(seg);
|
|
44
|
+
}
|
|
45
|
+
return `${path.startsWith('/') ? '/' : ''}${out.join('/')}`;
|
|
46
|
+
};
|
|
47
|
+
// The file a relative import names among those given: as TypeScript resolves it (the extensions, then an index).
|
|
48
|
+
export const resolveIn = (files, from, spec) => {
|
|
49
|
+
const base = normalize(`${from.split('/').slice(0, -1).join('/')}/${spec}`), bare = base.replace(/\.(js|jsx|mjs|cjs)$/, '');
|
|
50
|
+
return [base, ...['.ts', '.tsx', '.js', '.jsx', '.mjs'].map(x => `${bare}${x}`), ...['/index.ts', '/index.tsx', '/index.js'].map(x => `${base}${x}`)].find(c => c in files);
|
|
51
|
+
};
|
|
52
|
+
export function program(code, file, extra = {}) {
|
|
53
|
+
const options = { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext, moduleResolution: ts.ModuleResolutionKind.Bundler, strict: true, noEmit: true, allowJs: true, checkJs: false, skipLibCheck: true, lib: ['lib.es2022.d.ts'], types: [], jsx: ts.JsxEmit.Preserve };
|
|
54
|
+
const host = ts.createCompilerHost(options), getSourceFile = host.getSourceFile.bind(host), readFile = host.readFile.bind(host), fileExists = host.fileExists.bind(host);
|
|
55
|
+
host.getSourceFile = (name, lang, ...rest) => (name === file ? ts.createSourceFile(name, code, lang, true, scriptKind(file)) : name in extra ? ts.createSourceFile(name, extra[name], lang, true, scriptKind(name)) : getSourceFile(name, lang, ...rest));
|
|
56
|
+
host.readFile = name => (name === file ? code : name in extra ? extra[name] : readFile(name));
|
|
57
|
+
host.fileExists = name => name === file || name in extra || fileExists(name);
|
|
58
|
+
const dirExists = host.directoryExists?.bind(host), dirs = new Set(Object.keys(extra).flatMap(f => f.split('/').slice(1, -1).map((_, i, a) => `/${a.slice(0, i + 1).join('/')}`)));
|
|
59
|
+
host.directoryExists = d => dirs.has(d.replace(/\/$/, '')) || (dirExists ? dirExists(d) : true);
|
|
60
|
+
const p = ts.createProgram([file], options, host);
|
|
61
|
+
return { sf: p.getSourceFile(file), checker: p.getTypeChecker() };
|
|
62
|
+
}
|
|
63
|
+
export const scriptKind = (file) => (/\.jsx$/.test(file) ? ts.ScriptKind.JSX : /\.tsx$/.test(file) ? ts.ScriptKind.TSX : /\.[cm]?js$/.test(file) ? ts.ScriptKind.JS : ts.ScriptKind.TS);
|
|
64
|
+
// The shape a type has in the subset.
|
|
65
|
+
export function shapeOf(t, checker, at) {
|
|
66
|
+
if (t.flags & (ts.TypeFlags.NumberLike))
|
|
67
|
+
return { k: 'num' };
|
|
68
|
+
if (t.flags & ts.TypeFlags.BooleanLike)
|
|
69
|
+
return { k: 'bool' };
|
|
70
|
+
if (t.flags & ts.TypeFlags.StringLiteral)
|
|
71
|
+
return { k: 'choice', values: [t.value] };
|
|
72
|
+
if (t.flags & ts.TypeFlags.String)
|
|
73
|
+
return { k: 'str' };
|
|
74
|
+
if (t.isUnion()) {
|
|
75
|
+
const parts = t.types;
|
|
76
|
+
if (parts.every(p => p.flags & ts.TypeFlags.BooleanLiteral))
|
|
77
|
+
return { k: 'bool' };
|
|
78
|
+
if (parts.every(p => p.flags & ts.TypeFlags.NumberLiteral))
|
|
79
|
+
return { k: 'num' };
|
|
80
|
+
if (parts.every(p => p.flags & ts.TypeFlags.StringLiteral))
|
|
81
|
+
return { k: 'choice', values: parts.map(p => p.value) };
|
|
82
|
+
if (parts.some(p => p.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null)))
|
|
83
|
+
return refuse(at, `${checker.typeToString(t)}: a value that may be missing (a map's cell is how the subset holds one)`);
|
|
84
|
+
return refuse(at, `${checker.typeToString(t)}: a union of other than string literals`);
|
|
85
|
+
}
|
|
86
|
+
if (t.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown))
|
|
87
|
+
return refuse(at, `${text(at)} has no type the subset reads (any or unknown): annotate it`);
|
|
88
|
+
if (t.flags & ts.TypeFlags.Object) {
|
|
89
|
+
const sym = t.getSymbol() ?? t.aliasSymbol, name = sym?.getName();
|
|
90
|
+
if (name === 'Map') {
|
|
91
|
+
const [kt, vt] = checker.getTypeArguments(t);
|
|
92
|
+
if (!kt || !vt)
|
|
93
|
+
return refuse(at, 'a Map without its key and value types');
|
|
94
|
+
return { k: 'map', key: keySort(shapeOf(kt, checker, at), at), val: shapeOf(vt, checker, at), js: 'Map' };
|
|
95
|
+
}
|
|
96
|
+
if (checker.isArrayType(t) || checker.isTupleType(t))
|
|
97
|
+
return refuse(at, `${checker.typeToString(t)}: an array (lists are not in the subset yet)`);
|
|
98
|
+
if (name === 'Set' || name === 'Date' || name === 'Promise')
|
|
99
|
+
return refuse(at, `a ${name}`);
|
|
100
|
+
const sidx = checker.getIndexInfoOfType(t, ts.IndexKind.String), nidx = checker.getIndexInfoOfType(t, ts.IndexKind.Number);
|
|
101
|
+
if (sidx || nidx) {
|
|
102
|
+
if (checker.getPropertiesOfType(t).length)
|
|
103
|
+
return refuse(at, `${checker.typeToString(t)}: named fields beside an index signature`);
|
|
104
|
+
return { k: 'map', key: sidx ? 'str' : 'num', val: shapeOf((sidx ?? nidx).type, checker, at), js: 'record' };
|
|
105
|
+
}
|
|
106
|
+
if (checker.getSignaturesOfType(t, ts.SignatureKind.Call).length)
|
|
107
|
+
return refuse(at, `${checker.typeToString(t)}: a function value`);
|
|
108
|
+
const fields = checker.getPropertiesOfType(t).map(p => {
|
|
109
|
+
if (p.flags & ts.SymbolFlags.Optional)
|
|
110
|
+
refuse(at, `the optional field ${p.getName()} (a value that may be missing)`);
|
|
111
|
+
if (p.flags & ts.SymbolFlags.Method)
|
|
112
|
+
refuse(at, `the method ${p.getName()} in a value`);
|
|
113
|
+
return [p.getName(), shapeOf(checker.getTypeOfSymbolAtLocation(p, at), checker, at)];
|
|
114
|
+
});
|
|
115
|
+
return { k: 'rec', fields };
|
|
116
|
+
}
|
|
117
|
+
return refuse(at, `${checker.typeToString(t)} is not in the subset`);
|
|
118
|
+
}
|
|
119
|
+
const keySort = (s, at) => (s.k === 'num' ? 'num' : s.k === 'str' || s.k === 'choice' ? 'str' : refuse(at, 'a map keyed by other than numbers or strings'));
|
|
120
|
+
export class Exec {
|
|
121
|
+
s;
|
|
122
|
+
throws = [];
|
|
123
|
+
depth = 0;
|
|
124
|
+
ids = 0;
|
|
125
|
+
constructor(s) {
|
|
126
|
+
this.s = s;
|
|
127
|
+
}
|
|
128
|
+
fresh(base) {
|
|
129
|
+
const clean = base.replace(/^#/, '').replace(/[^A-Za-z0-9_]/g, '_').replace(/^(\d)/, '_$1') || 'x';
|
|
130
|
+
let n = clean, i = 2;
|
|
131
|
+
while (this.s.names.has(n) || RESERVED.has(n))
|
|
132
|
+
n = `${clean}_${i++}`;
|
|
133
|
+
this.s.names.add(n);
|
|
134
|
+
return n;
|
|
135
|
+
}
|
|
136
|
+
newVar(base, sort, role, note) {
|
|
137
|
+
const v = { name: this.fresh(base), sort, role, ...(note ? { note } : {}) };
|
|
138
|
+
this.s.vars.set(v.name, v);
|
|
139
|
+
return v;
|
|
140
|
+
}
|
|
141
|
+
id(prefix) { return `${prefix}#${++this.ids}`; }
|
|
142
|
+
// A variable's value by its sort.
|
|
143
|
+
static valueOf(v) {
|
|
144
|
+
switch (v.sort.k) {
|
|
145
|
+
case 'num': return { t: 'num', e: I.nvar(v.name) };
|
|
146
|
+
case 'bool': return { t: 'bool', e: I.bvar(v.name) };
|
|
147
|
+
case 'choice': return { t: 'choice', e: { op: 'var', name: v.name, values: v.sort.values }, values: v.sort.values };
|
|
148
|
+
case 'str': return { t: 'str', e: { op: 'svar', name: v.name } };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// A map's cell field (before the step) by its sort.
|
|
152
|
+
static cellOf(m, key, f) {
|
|
153
|
+
switch (f.sort.k) {
|
|
154
|
+
case 'num': return { t: 'num', e: { op: 'cell', map: m.name, key, field: f.name } };
|
|
155
|
+
case 'bool': return { t: 'bool', e: { op: 'cell', map: m.name, key, field: f.name } };
|
|
156
|
+
case 'choice': return { t: 'choice', e: { op: 'cell', map: m.name, key, field: f.name, values: f.sort.values }, values: f.sort.values };
|
|
157
|
+
case 'str': throw new Error('a string in a map cell');
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
static defaultOf(sort) {
|
|
161
|
+
switch (sort.k) {
|
|
162
|
+
case 'num': return { t: 'num', e: I.num(0) };
|
|
163
|
+
case 'bool': return { t: 'bool', e: I.F };
|
|
164
|
+
case 'choice': return { t: 'choice', e: I.llit(sort.values[0], sort.values), values: sort.values };
|
|
165
|
+
case 'str': return { t: 'str', e: { op: 'slit', value: '' } };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// --- paths -----------------------------------------------------------------------------------------------------
|
|
169
|
+
withCond(p, c) { return { ...p, pc: c.op === 'const' && c.v ? p.pc : [...p.pc, c] }; }
|
|
170
|
+
feasible(p) { return !p.pc.some(c => c.op === 'const' && !c.v); }
|
|
171
|
+
// What holds on a path, by its conditions: a condition known there (one of them, or a part of one).
|
|
172
|
+
known(p, c) {
|
|
173
|
+
if (c.op === 'const')
|
|
174
|
+
return c.v;
|
|
175
|
+
const flat = (x) => (x.op === 'and' ? x.xs.flatMap(flat) : x.op === 'not' && x.x.op === 'or' ? x.x.xs.map(y => I.not(y)).flatMap(flat) : [x]);
|
|
176
|
+
const parts = p.pc.flatMap(flat);
|
|
177
|
+
if (c.op === 'and')
|
|
178
|
+
return c.xs.every(x => this.known(p, x));
|
|
179
|
+
return parts.some(x => I.same(x, c));
|
|
180
|
+
}
|
|
181
|
+
merge(a, b, at) {
|
|
182
|
+
if (!a || !this.feasible(a))
|
|
183
|
+
return b && this.feasible(b) ? b : undefined;
|
|
184
|
+
if (!b || !this.feasible(b))
|
|
185
|
+
return a;
|
|
186
|
+
let i = 0;
|
|
187
|
+
while (i < a.pc.length && i < b.pc.length && a.pc[i] === b.pc[i])
|
|
188
|
+
i++;
|
|
189
|
+
const ra = a.pc.slice(i), rb = b.pc.slice(i), prefix = a.pc.slice(0, i);
|
|
190
|
+
const c = ra.length ? I.and(...ra) : I.not(I.and(...rb));
|
|
191
|
+
const complementary = (ra.length === 1 && rb.length === 1 && I.same(ra[0], I.not(rb[0]))) || !ra.length || !rb.length;
|
|
192
|
+
const pc = complementary ? prefix : [...prefix, I.or(I.and(...ra), I.and(...rb))];
|
|
193
|
+
const pairs = new Map(), heap = new Map();
|
|
194
|
+
const locals = new Map();
|
|
195
|
+
for (const [n, va] of a.locals) {
|
|
196
|
+
const vb = b.locals.get(n);
|
|
197
|
+
if (vb)
|
|
198
|
+
locals.set(n, this.mergeV(c, va, vb, a, b, heap, pairs, at));
|
|
199
|
+
}
|
|
200
|
+
for (const id of new Set([...a.heap.keys(), ...b.heap.keys()])) {
|
|
201
|
+
if (heap.has(id))
|
|
202
|
+
continue;
|
|
203
|
+
const oa = a.heap.get(id), ob = b.heap.get(id);
|
|
204
|
+
heap.set(id, !oa ? ob : !ob ? oa : this.mergeObj(c, oa, ob, a, b, heap, pairs, at));
|
|
205
|
+
}
|
|
206
|
+
return { pc, locals, heap, wrote: [...new Set([...a.wrote, ...b.wrote])], nows: Math.max(a.nows, b.nows) };
|
|
207
|
+
}
|
|
208
|
+
deref(p, v) {
|
|
209
|
+
while (v.t === 'ref') {
|
|
210
|
+
const o = p.heap.get(v.id);
|
|
211
|
+
if (o?.k !== 'alias')
|
|
212
|
+
return v;
|
|
213
|
+
v = o.to;
|
|
214
|
+
}
|
|
215
|
+
return v;
|
|
216
|
+
}
|
|
217
|
+
mergeV(c, va, vb, a, b, heap, pairs, at) {
|
|
218
|
+
va = this.deref(a, va);
|
|
219
|
+
vb = this.deref(b, vb);
|
|
220
|
+
if (va.t === 'num' && vb.t === 'num')
|
|
221
|
+
return { t: 'num', e: I.nite(c, va.e, vb.e) };
|
|
222
|
+
if (va.t === 'bool' && vb.t === 'bool')
|
|
223
|
+
return { t: 'bool', e: I.bite(c, va.e, vb.e) };
|
|
224
|
+
if ((va.t === 'choice' || va.t === 'str') && (vb.t === 'choice' || vb.t === 'str')) {
|
|
225
|
+
if (va.t === 'str' && vb.t === 'str')
|
|
226
|
+
return { t: 'str', e: I.kite(c, va.e, vb.e) };
|
|
227
|
+
const values = va.t === 'choice' ? va.values : vb.values;
|
|
228
|
+
return { t: 'choice', e: I.lite(c, this.asChoice(va, values, at), this.asChoice(vb, values, at)), values };
|
|
229
|
+
}
|
|
230
|
+
if (va.t === 'undef' && vb.t === 'undef')
|
|
231
|
+
return va;
|
|
232
|
+
if (va.t === 'opt' || vb.t === 'opt' || va.t === 'undef' || vb.t === 'undef') {
|
|
233
|
+
const [ha, xa] = va.t === 'opt' ? [va.has, va.v] : va.t === 'undef' ? [I.F, undefined] : [I.T, va];
|
|
234
|
+
const [hb, xb] = vb.t === 'opt' ? [vb.has, vb.v] : vb.t === 'undef' ? [I.F, undefined] : [I.T, vb];
|
|
235
|
+
const v = !xa ? xb : !xb ? xa : this.mergeV(c, xa, xb, a, b, heap, pairs, at);
|
|
236
|
+
return { t: 'opt', has: I.bite(c, ha, hb), v };
|
|
237
|
+
}
|
|
238
|
+
if (va.t === 'cell' && vb.t === 'cell' && va.map === vb.map) {
|
|
239
|
+
// Current on both paths (the map not set since it was read on either): current in the map merged.
|
|
240
|
+
const ma = a.heap.get(va.map), mb = b.heap.get(vb.map);
|
|
241
|
+
if (va.gen !== ma.gen || vb.gen !== mb.gen)
|
|
242
|
+
return refuse(at, 'a reference into a map read before the map was set');
|
|
243
|
+
return { t: 'cell', map: va.map, key: I.kite(c, va.key, vb.key), gen: mergedGen(ma, mb) };
|
|
244
|
+
}
|
|
245
|
+
if (va.t === 'ref' && vb.t === 'ref' && va.id === vb.id)
|
|
246
|
+
return va;
|
|
247
|
+
// Two objects by the path taken: a reference to each by the condition.
|
|
248
|
+
const refs = ['ref', 'cell', 'phi'];
|
|
249
|
+
if (refs.includes(va.t) && refs.includes(vb.t))
|
|
250
|
+
return { t: 'phi', c, a: va, b: vb };
|
|
251
|
+
return refuse(at, `a value that is a ${va.t} on one path and a ${vb.t} on another`);
|
|
252
|
+
}
|
|
253
|
+
mergeObj(c, oa, ob, a, b, heap, pairs, at, two = false) {
|
|
254
|
+
if (oa.k === 'alias' && ob.k === 'alias')
|
|
255
|
+
return { k: 'alias', to: this.mergeV(c, oa.to, ob.to, a, b, heap, pairs, at) };
|
|
256
|
+
if (oa.k === 'rec' && ob.k === 'rec') {
|
|
257
|
+
const fa = [...oa.fields.keys()].sort().join(), fb = [...ob.fields.keys()].sort().join();
|
|
258
|
+
if (fa !== fb)
|
|
259
|
+
return refuse(at, 'an object with other fields by the path taken');
|
|
260
|
+
const origin = oa.origin === 'input' || ob.origin === 'input' ? 'input' : oa.origin === 'shared' || ob.origin === 'shared' ? 'shared' : oa.origin;
|
|
261
|
+
return { k: 'rec', origin, fields: new Map([...oa.fields].map(([f, v]) => [f, this.mergeV(c, v, ob.fields.get(f), a, b, heap, pairs, at)])), ...(oa.lazy ? { lazy: oa.lazy } : {}) };
|
|
262
|
+
}
|
|
263
|
+
if (oa.k === 'map' && ob.k === 'map') {
|
|
264
|
+
if (oa.base !== ob.base)
|
|
265
|
+
return refuse(at, 'one of two maps by the path taken');
|
|
266
|
+
const origin = oa.origin === 'input' || ob.origin === 'input' ? 'input' : oa.origin;
|
|
267
|
+
void two;
|
|
268
|
+
return { ...oa, origin, gen: mergedGen(oa, ob), w: this.mergeW(c, oa, ob, at) };
|
|
269
|
+
}
|
|
270
|
+
return refuse(at, 'an object that is of another kind by the path taken');
|
|
271
|
+
}
|
|
272
|
+
mergeW(c, oa, ob, at) { return this.mergeWrites(c, oa.decl, oa.w, ob.w, at); }
|
|
273
|
+
mergeWrites(c, decl, wa, wb, at) {
|
|
274
|
+
if (!wa && !wb)
|
|
275
|
+
return undefined;
|
|
276
|
+
const base = (key) => this.baseCell(decl, key);
|
|
277
|
+
const fill = (w, key) => w ?? { key, ...base(key) };
|
|
278
|
+
const xa = fill(wa, (wa ?? wb).key), xb = fill(wb, (wb ?? wa).key);
|
|
279
|
+
const vals = new Map([...xa.vals].map(([f, v]) => [f, this.mergeScalar(c, v, xb.vals.get(f), at)]));
|
|
280
|
+
return { key: I.kite(c, xa.key, xb.key), has: I.bite(c, xa.has, xb.has), vals };
|
|
281
|
+
}
|
|
282
|
+
mergeScalar(c, a, b, at) {
|
|
283
|
+
if (a.t === 'num' && b.t === 'num')
|
|
284
|
+
return { t: 'num', e: I.nite(c, a.e, b.e) };
|
|
285
|
+
if (a.t === 'bool' && b.t === 'bool')
|
|
286
|
+
return { t: 'bool', e: I.bite(c, a.e, b.e) };
|
|
287
|
+
if (a.t === 'choice' && b.t === 'choice')
|
|
288
|
+
return { t: 'choice', e: I.lite(c, a.e, b.e), values: a.values };
|
|
289
|
+
if (a.t === 'str' && b.t === 'str')
|
|
290
|
+
return { t: 'str', e: I.kite(c, a.e, b.e) };
|
|
291
|
+
return refuse(at, 'a value of another kind by the path taken');
|
|
292
|
+
}
|
|
293
|
+
baseCell(m, key) {
|
|
294
|
+
return { has: { op: 'cell', map: m.name, key, field: 'has' }, vals: new Map(m.fields.map(f => [f.name, Exec.cellOf(m, key, f)])) };
|
|
295
|
+
}
|
|
296
|
+
asChoice(v, values, at) {
|
|
297
|
+
if (v.t === 'choice')
|
|
298
|
+
return v.e;
|
|
299
|
+
if (v.t === 'str' && v.e.op === 'slit') {
|
|
300
|
+
if (!values.includes(v.e.value))
|
|
301
|
+
return refuse(at, `'${v.e.value}' is not one of ${values.map(x => `'${x}'`).join(', ')}`);
|
|
302
|
+
return I.llit(v.e.value, values);
|
|
303
|
+
}
|
|
304
|
+
return refuse(at, 'a string that is not one of a union\'s literals');
|
|
305
|
+
}
|
|
306
|
+
// --- statements ------------------------------------------------------------------------------------------------
|
|
307
|
+
block(stmts, p) {
|
|
308
|
+
const declared = [];
|
|
309
|
+
let live = p;
|
|
310
|
+
const rets = [], breaks = [];
|
|
311
|
+
for (const st of stmts) {
|
|
312
|
+
if (!live)
|
|
313
|
+
break;
|
|
314
|
+
if (ts.isVariableStatement(st))
|
|
315
|
+
for (const d of st.declarationList.declarations)
|
|
316
|
+
for (const n of boundNames(d.name)) {
|
|
317
|
+
if (live.locals.has(n) && !declared.includes(n))
|
|
318
|
+
refuse(d, `${n} shadows another ${n}`);
|
|
319
|
+
declared.push(n);
|
|
320
|
+
}
|
|
321
|
+
if (ts.isFunctionDeclaration(st))
|
|
322
|
+
refuse(st, 'a function declared inside a step');
|
|
323
|
+
const f = this.stmt(st, live);
|
|
324
|
+
rets.push(...f.rets);
|
|
325
|
+
breaks.push(...f.breaks);
|
|
326
|
+
live = f.live;
|
|
327
|
+
}
|
|
328
|
+
if (live) {
|
|
329
|
+
live = { ...live, locals: new Map(live.locals) };
|
|
330
|
+
for (const n of declared)
|
|
331
|
+
live.locals.delete(n);
|
|
332
|
+
}
|
|
333
|
+
return { ...(live ? { live } : {}), rets, breaks };
|
|
334
|
+
}
|
|
335
|
+
stmt(st, p) {
|
|
336
|
+
const none = { rets: [], breaks: [] };
|
|
337
|
+
if (ts.isBlock(st))
|
|
338
|
+
return this.block(st.statements, p);
|
|
339
|
+
if (ts.isEmptyStatement(st))
|
|
340
|
+
return { ...none, live: p };
|
|
341
|
+
if (ts.isExpressionStatement(st)) {
|
|
342
|
+
const [, q] = this.ev(st.expression, p);
|
|
343
|
+
return { ...none, live: q };
|
|
344
|
+
}
|
|
345
|
+
if (ts.isVariableStatement(st)) {
|
|
346
|
+
if (!(st.declarationList.flags & (ts.NodeFlags.Const | ts.NodeFlags.Let)))
|
|
347
|
+
refuse(st, 'var (let or const are in the subset)');
|
|
348
|
+
let q = p;
|
|
349
|
+
for (const d of st.declarationList.declarations) {
|
|
350
|
+
let v = { t: 'undef' };
|
|
351
|
+
if (d.initializer)
|
|
352
|
+
[v, q] = this.ev(d.initializer, q);
|
|
353
|
+
q = this.bind(d.name, v, q);
|
|
354
|
+
}
|
|
355
|
+
return { ...none, live: q };
|
|
356
|
+
}
|
|
357
|
+
if (ts.isReturnStatement(st)) {
|
|
358
|
+
if (!st.expression)
|
|
359
|
+
return { ...none, rets: [[p, { t: 'undef' }]] };
|
|
360
|
+
const [v, q] = this.ev(st.expression, p);
|
|
361
|
+
return { ...none, rets: [[q, v]] };
|
|
362
|
+
}
|
|
363
|
+
if (ts.isThrowStatement(st)) {
|
|
364
|
+
if (impure(st.expression))
|
|
365
|
+
refuse(st, 'a throw whose argument has effects');
|
|
366
|
+
if (p.wrote.length)
|
|
367
|
+
refuse(st, `throws after writing ${p.wrote.join(', ')}: the code leaves its state changed where a rule's refusal leaves it as it was`);
|
|
368
|
+
this.throws.push({ pc: p.pc });
|
|
369
|
+
return none;
|
|
370
|
+
}
|
|
371
|
+
if (ts.isIfStatement(st)) {
|
|
372
|
+
const [cv, q] = this.ev(st.expression, p), c = this.truthy(cv, st.expression);
|
|
373
|
+
const [pt, pe] = this.narrow(st.expression, this.withCond(q, c), this.withCond(q, I.not(c)));
|
|
374
|
+
const ft = this.feasible(pt) ? this.stmt(st.thenStatement, pt) : none;
|
|
375
|
+
const fe = !this.feasible(pe) ? none : st.elseStatement ? this.stmt(st.elseStatement, pe) : { ...none, live: pe };
|
|
376
|
+
const live = this.merge(ft.live, fe.live, st);
|
|
377
|
+
return { ...(live ? { live } : {}), rets: [...ft.rets, ...fe.rets], breaks: [...ft.breaks, ...fe.breaks] };
|
|
378
|
+
}
|
|
379
|
+
if (ts.isSwitchStatement(st))
|
|
380
|
+
return this.switch(st, p);
|
|
381
|
+
if (ts.isBreakStatement(st)) {
|
|
382
|
+
if (st.label)
|
|
383
|
+
refuse(st, 'a labeled break');
|
|
384
|
+
return { ...none, breaks: [p] };
|
|
385
|
+
}
|
|
386
|
+
if (ts.isForStatement(st) || ts.isForOfStatement(st) || ts.isForInStatement(st) || ts.isWhileStatement(st) || ts.isDoStatement(st))
|
|
387
|
+
return refuse(st, 'a loop (a rule\'s step is one bounded computation; a loop over the state is not in the subset)');
|
|
388
|
+
if (ts.isTryStatement(st))
|
|
389
|
+
return refuse(st, 'try/catch (a refusal caught inside the step)');
|
|
390
|
+
return refuse(st, `${ts.SyntaxKind[st.kind]} is not in the subset`);
|
|
391
|
+
}
|
|
392
|
+
switch(st, p) {
|
|
393
|
+
const [dv, q] = this.ev(st.expression, p);
|
|
394
|
+
const matches = [];
|
|
395
|
+
const conds = st.caseBlock.clauses.map(cl => {
|
|
396
|
+
if (ts.isDefaultClause(cl))
|
|
397
|
+
return undefined;
|
|
398
|
+
if (impure(cl.expression))
|
|
399
|
+
refuse(cl, 'a case with effects');
|
|
400
|
+
const [cv] = this.ev(cl.expression, q);
|
|
401
|
+
const m = this.equal(dv, cv, cl.expression);
|
|
402
|
+
const c = I.and(...matches.map(I.not), m);
|
|
403
|
+
matches.push(m);
|
|
404
|
+
return c;
|
|
405
|
+
});
|
|
406
|
+
const none = I.not(I.or(...matches));
|
|
407
|
+
let fall;
|
|
408
|
+
const rets = [], ends = [];
|
|
409
|
+
st.caseBlock.clauses.forEach((cl, i) => {
|
|
410
|
+
const enter = this.merge(fall, this.withCond(q, conds[i] ?? none), cl);
|
|
411
|
+
if (!enter) {
|
|
412
|
+
fall = undefined;
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const f = this.block(cl.statements, enter);
|
|
416
|
+
rets.push(...f.rets);
|
|
417
|
+
ends.push(...f.breaks);
|
|
418
|
+
fall = f.live;
|
|
419
|
+
});
|
|
420
|
+
if (fall)
|
|
421
|
+
ends.push(fall);
|
|
422
|
+
if (!st.caseBlock.clauses.some(ts.isDefaultClause))
|
|
423
|
+
ends.push(this.withCond(q, none));
|
|
424
|
+
const live = ends.reduceRight((acc, x) => this.merge(x, acc, st), undefined);
|
|
425
|
+
return { ...(live ? { live } : {}), rets, breaks: [] };
|
|
426
|
+
}
|
|
427
|
+
// `if (!x)` and `if (x)` (x === undefined, x !== undefined) on a value that may be missing: x is known in the branch
|
|
428
|
+
// where it is there.
|
|
429
|
+
narrow(e, pt, pe) {
|
|
430
|
+
const strip = (x) => (ts.isParenthesizedExpression(x) ? strip(x.expression) : x);
|
|
431
|
+
let x = strip(e), present = true;
|
|
432
|
+
if (ts.isPrefixUnaryExpression(x) && x.operator === ts.SyntaxKind.ExclamationToken) {
|
|
433
|
+
x = strip(x.operand);
|
|
434
|
+
present = false;
|
|
435
|
+
}
|
|
436
|
+
else if (ts.isBinaryExpression(x) && (x.right.kind === ts.SyntaxKind.UndefinedKeyword || (ts.isIdentifier(x.right) && x.right.text === 'undefined') || x.right.kind === ts.SyntaxKind.NullKeyword)) {
|
|
437
|
+
const op = x.operatorToken.kind;
|
|
438
|
+
if (op === ts.SyntaxKind.EqualsEqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsToken)
|
|
439
|
+
present = false;
|
|
440
|
+
else if (op !== ts.SyntaxKind.ExclamationEqualsEqualsToken && op !== ts.SyntaxKind.ExclamationEqualsToken)
|
|
441
|
+
return [pt, pe];
|
|
442
|
+
x = strip(x.left);
|
|
443
|
+
}
|
|
444
|
+
if (!ts.isIdentifier(x))
|
|
445
|
+
return [pt, pe];
|
|
446
|
+
const v = pt.locals.get(x.text);
|
|
447
|
+
if (v?.t !== 'opt')
|
|
448
|
+
return [pt, pe];
|
|
449
|
+
const set = (p, there) => ({ ...p, locals: new Map(p.locals).set(x.text, there ? v.v : { t: 'undef' }) });
|
|
450
|
+
return present ? [set(pt, true), set(pe, false)] : [set(pt, false), set(pe, true)];
|
|
451
|
+
}
|
|
452
|
+
bind(name, v, p) {
|
|
453
|
+
if (ts.isIdentifier(name)) {
|
|
454
|
+
this.hint(v, name.text);
|
|
455
|
+
return { ...p, locals: new Map(p.locals).set(name.text, v) };
|
|
456
|
+
}
|
|
457
|
+
if (ts.isObjectBindingPattern(name)) {
|
|
458
|
+
let q = p;
|
|
459
|
+
for (const el of name.elements) {
|
|
460
|
+
if (el.dotDotDotToken || el.initializer)
|
|
461
|
+
refuse(el, 'a rest or default in a destructuring');
|
|
462
|
+
const key = el.propertyName ? (ts.isIdentifier(el.propertyName) ? el.propertyName.text : refuse(el, 'a computed name in a destructuring')) : el.name.text;
|
|
463
|
+
const [fv, q2] = this.field(v, key, el, q);
|
|
464
|
+
q = this.bind(el.name, fv, q2);
|
|
465
|
+
}
|
|
466
|
+
return q;
|
|
467
|
+
}
|
|
468
|
+
return refuse(name, 'an array destructuring');
|
|
469
|
+
}
|
|
470
|
+
hint(v, name) {
|
|
471
|
+
const e = v.t === 'num' || v.t === 'bool' || v.t === 'choice' ? v.e : undefined;
|
|
472
|
+
if (e && e.op !== 'var' && e.op !== 'const' && e.op !== 'lit' && !this.s.hints.has(e))
|
|
473
|
+
this.s.hints.set(e, name);
|
|
474
|
+
}
|
|
475
|
+
// --- expressions -----------------------------------------------------------------------------------------------
|
|
476
|
+
truthy(v, at) {
|
|
477
|
+
switch (v.t) {
|
|
478
|
+
case 'bool': return v.e;
|
|
479
|
+
case 'num': return I.not(I.cmp('=', v.e, I.num(0)));
|
|
480
|
+
case 'choice': return v.values.includes('') ? I.not(I.is(v.e, '')) : I.T;
|
|
481
|
+
case 'str': return v.e.op === 'slit' ? (v.e.value ? I.T : I.F) : refuse(at, 'a string as a condition');
|
|
482
|
+
case 'ref':
|
|
483
|
+
case 'cell':
|
|
484
|
+
case 'phi': return I.T;
|
|
485
|
+
case 'opt': return I.and(v.has, this.truthy(v.v, at));
|
|
486
|
+
case 'undef': return I.F;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
num(v, at, p) {
|
|
490
|
+
if (v.t === 'num')
|
|
491
|
+
return v.e;
|
|
492
|
+
if (v.t === 'opt' && this.known(p, v.has))
|
|
493
|
+
return this.num(v.v, at, p);
|
|
494
|
+
if (v.t === 'opt')
|
|
495
|
+
return refuse(at, `${text(at)} may be missing (undefined): give a default (?? 0) or check for it first`);
|
|
496
|
+
return refuse(at, `${text(at)} is a ${v.t === 'bool' ? 'boolean' : v.t === 'choice' || v.t === 'str' ? 'string' : v.t === 'undef' ? 'undefined' : 'object'} where a number is read`);
|
|
497
|
+
}
|
|
498
|
+
bool(v, at) { return v.t === 'bool' ? v.e : refuse(at, `${text(at)} where a boolean is read`); }
|
|
499
|
+
equal(a, b, at) {
|
|
500
|
+
if (a.t === 'num' && b.t === 'num')
|
|
501
|
+
return I.cmp('=', a.e, b.e);
|
|
502
|
+
if (a.t === 'bool' && b.t === 'bool')
|
|
503
|
+
return I.or(I.and(a.e, b.e), I.and(I.not(a.e), I.not(b.e)));
|
|
504
|
+
if (a.t === 'choice' && (b.t === 'choice' || b.t === 'str'))
|
|
505
|
+
return I.leq(a.e, b.t === 'str' && b.e.op === 'slit' && !a.values.includes(b.e.value) ? I.llit(b.e.value, [...a.values, b.e.value]) : this.asChoice(b, a.values, at));
|
|
506
|
+
if (b.t === 'choice' && a.t === 'str')
|
|
507
|
+
return this.equal(b, a, at);
|
|
508
|
+
if (a.t === 'str' && b.t === 'str')
|
|
509
|
+
return I.keq(a.e, b.e);
|
|
510
|
+
if (a.t === 'undef' || b.t === 'undef') {
|
|
511
|
+
const o = a.t === 'undef' ? b : a;
|
|
512
|
+
return o.t === 'undef' ? I.T : o.t === 'opt' ? I.not(o.has) : I.F;
|
|
513
|
+
}
|
|
514
|
+
return refuse(at, `a comparison of a ${a.t} with a ${b.t}`);
|
|
515
|
+
}
|
|
516
|
+
ev(e, p) {
|
|
517
|
+
if (ts.isParenthesizedExpression(e) || ts.isAsExpression(e) || ts.isSatisfiesExpression(e) || ts.isTypeAssertionExpression(e))
|
|
518
|
+
return this.ev(e.expression, p);
|
|
519
|
+
if (ts.isNonNullExpression(e)) {
|
|
520
|
+
const [v, q] = this.ev(e.expression, p);
|
|
521
|
+
if (v.t !== 'opt')
|
|
522
|
+
return [v, q];
|
|
523
|
+
if (!this.known(q, v.has))
|
|
524
|
+
return refuse(e, `${text(e.expression)}! where it may be missing`);
|
|
525
|
+
return [v.v, q];
|
|
526
|
+
}
|
|
527
|
+
if (ts.isNumericLiteral(e)) {
|
|
528
|
+
const x = Number(e.text);
|
|
529
|
+
if (!Number.isSafeInteger(x))
|
|
530
|
+
return refuse(e, `${e.text}: a number that isn't a safe integer (the subset's numbers are integers)`);
|
|
531
|
+
return [{ t: 'num', e: I.num(x) }, p];
|
|
532
|
+
}
|
|
533
|
+
if (ts.isStringLiteral(e) || ts.isNoSubstitutionTemplateLiteral(e))
|
|
534
|
+
return [{ t: 'str', e: { op: 'slit', value: e.text } }, p];
|
|
535
|
+
if (e.kind === ts.SyntaxKind.TrueKeyword)
|
|
536
|
+
return [{ t: 'bool', e: I.T }, p];
|
|
537
|
+
if (e.kind === ts.SyntaxKind.FalseKeyword)
|
|
538
|
+
return [{ t: 'bool', e: I.F }, p];
|
|
539
|
+
if (e.kind === ts.SyntaxKind.NullKeyword)
|
|
540
|
+
return refuse(e, 'null');
|
|
541
|
+
if (e.kind === ts.SyntaxKind.ThisKeyword) {
|
|
542
|
+
if (!p.heap.has('this'))
|
|
543
|
+
refuse(e, 'this outside a class');
|
|
544
|
+
return [{ t: 'ref', id: 'this' }, p];
|
|
545
|
+
}
|
|
546
|
+
if (ts.isIdentifier(e))
|
|
547
|
+
return this.name(e, p);
|
|
548
|
+
if (ts.isPropertyAccessExpression(e) || ts.isElementAccessExpression(e))
|
|
549
|
+
return this.access(e, p);
|
|
550
|
+
if (ts.isPrefixUnaryExpression(e))
|
|
551
|
+
return this.prefix(e, p);
|
|
552
|
+
if (ts.isPostfixUnaryExpression(e)) {
|
|
553
|
+
const [old, q] = this.ev(e.operand, p), n = this.num(old, e.operand, q);
|
|
554
|
+
const q2 = this.assign(e.operand, { t: 'num', e: e.operator === ts.SyntaxKind.PlusPlusToken ? I.add(n, I.num(1)) : I.sub(n, I.num(1)) }, q);
|
|
555
|
+
return [{ t: 'num', e: n }, q2];
|
|
556
|
+
}
|
|
557
|
+
if (ts.isBinaryExpression(e))
|
|
558
|
+
return this.binary(e, p);
|
|
559
|
+
if (ts.isConditionalExpression(e)) {
|
|
560
|
+
const [cv, q] = this.ev(e.condition, p), c = this.truthy(cv, e.condition);
|
|
561
|
+
const [pt, pe] = this.narrow(e.condition, this.withCond(q, c), this.withCond(q, I.not(c)));
|
|
562
|
+
if (!this.feasible(pt))
|
|
563
|
+
return this.ev(e.whenFalse, pe);
|
|
564
|
+
if (!this.feasible(pe))
|
|
565
|
+
return this.ev(e.whenTrue, pt);
|
|
566
|
+
const [va, qa] = this.ev(e.whenTrue, pt), [vb, qb] = this.ev(e.whenFalse, pe);
|
|
567
|
+
return this.joinValues(c, qa, va, qb, vb, e);
|
|
568
|
+
}
|
|
569
|
+
if (ts.isObjectLiteralExpression(e))
|
|
570
|
+
return this.object(e, p);
|
|
571
|
+
if (ts.isCallExpression(e))
|
|
572
|
+
return this.call(e, p);
|
|
573
|
+
if (ts.isNewExpression(e))
|
|
574
|
+
return this.newExpr(e, p);
|
|
575
|
+
if (ts.isArrayLiteralExpression(e))
|
|
576
|
+
return refuse(e, 'an array (lists are not in the subset yet)');
|
|
577
|
+
if (ts.isTemplateExpression(e))
|
|
578
|
+
return refuse(e, 'a template string');
|
|
579
|
+
if (ts.isArrowFunction(e) || ts.isFunctionExpression(e))
|
|
580
|
+
return refuse(e, 'a function value');
|
|
581
|
+
if (ts.isTypeOfExpression(e))
|
|
582
|
+
return refuse(e, 'typeof');
|
|
583
|
+
if (ts.isAwaitExpression(e))
|
|
584
|
+
return refuse(e, 'await (a step that waits interleaves with others)');
|
|
585
|
+
return refuse(e, `${ts.SyntaxKind[e.kind]} is not in the subset`);
|
|
586
|
+
}
|
|
587
|
+
// Two values each on its path, as one value on the merged path.
|
|
588
|
+
joinValues(c, qa, va, qb, vb, at) {
|
|
589
|
+
const tmp = '$join';
|
|
590
|
+
const m = this.merge({ ...qa, locals: new Map(qa.locals).set(tmp, va) }, { ...qb, locals: new Map(qb.locals).set(tmp, vb) }, at);
|
|
591
|
+
const v = m.locals.get(tmp);
|
|
592
|
+
m.locals.delete(tmp);
|
|
593
|
+
void c;
|
|
594
|
+
return [v, m];
|
|
595
|
+
}
|
|
596
|
+
name(e, p) {
|
|
597
|
+
const local = p.locals.get(e.text);
|
|
598
|
+
if (local)
|
|
599
|
+
return [local, p];
|
|
600
|
+
if (e.text === 'undefined')
|
|
601
|
+
return [{ t: 'undef' }, p];
|
|
602
|
+
if (e.text === 'Infinity' || e.text === 'NaN')
|
|
603
|
+
return refuse(e, `${e.text} (the subset's numbers are integers)`);
|
|
604
|
+
const sym = this.resolved(e);
|
|
605
|
+
const decl = sym?.valueDeclaration ?? sym?.declarations?.[0];
|
|
606
|
+
if (decl && ts.isVariableDeclaration(decl) && decl.initializer && this.s.ours(decl.getSourceFile()) && ts.isVariableStatement(decl.parent.parent) && ts.isSourceFile(decl.parent.parent.parent)) {
|
|
607
|
+
if (!(decl.parent.flags & ts.NodeFlags.Const))
|
|
608
|
+
return refuse(e, `${e.text}: a module-level let (state outside the step)`);
|
|
609
|
+
if (ts.isArrowFunction(decl.initializer) || ts.isFunctionExpression(decl.initializer))
|
|
610
|
+
return refuse(e, `${e.text}: a function used as a value`);
|
|
611
|
+
// A module-level const: its value, an object read-only (every call shares it).
|
|
612
|
+
const id = `shared.${decl.getSourceFile().fileName}:${e.text}`;
|
|
613
|
+
if (p.heap.has(id))
|
|
614
|
+
return [{ t: 'ref', id }, p];
|
|
615
|
+
const [v, q] = this.ev(decl.initializer, { pc: [], locals: new Map(), heap: p.heap, wrote: [], nows: 0 });
|
|
616
|
+
if (v.t !== 'ref')
|
|
617
|
+
return [v, p];
|
|
618
|
+
const share = (heap, x) => { if (x.t !== 'ref')
|
|
619
|
+
return; const o = heap.get(x.id); if (o?.k === 'rec') {
|
|
620
|
+
heap.set(x.id, { ...o, origin: 'shared' });
|
|
621
|
+
for (const y of o.fields.values())
|
|
622
|
+
share(heap, y);
|
|
623
|
+
}
|
|
624
|
+
else if (o?.k === 'map')
|
|
625
|
+
heap.set(x.id, { ...o, origin: 'shared' }); };
|
|
626
|
+
const heap = new Map(q.heap);
|
|
627
|
+
share(heap, v);
|
|
628
|
+
heap.set(id, { k: 'alias', to: v });
|
|
629
|
+
return [{ t: 'ref', id }, { ...p, heap }];
|
|
630
|
+
}
|
|
631
|
+
if (decl && ts.isEnumMember(decl))
|
|
632
|
+
return refuse(e, 'an enum');
|
|
633
|
+
if (!decl || sym?.declarations?.some(d => ts.isImportSpecifier(d) || ts.isImportClause(d) || ts.isNamespaceImport(d))) {
|
|
634
|
+
const imp = this.s.checker.getSymbolAtLocation(e)?.declarations?.find(d => ts.isImportSpecifier(d) || ts.isImportClause(d) || ts.isNamespaceImport(d));
|
|
635
|
+
if (imp)
|
|
636
|
+
return refuse(e, `${e.text}: a name from another module${moduleOf(imp)} (not among the files read)`);
|
|
637
|
+
}
|
|
638
|
+
if (decl && !this.s.ours(decl.getSourceFile()))
|
|
639
|
+
return refuse(e, `${e.text}: a name from another module (not among the files read)`);
|
|
640
|
+
return refuse(e, `${e.text}: a name the step can't read`);
|
|
641
|
+
}
|
|
642
|
+
prefix(e, p) {
|
|
643
|
+
if (e.operator === ts.SyntaxKind.PlusPlusToken || e.operator === ts.SyntaxKind.MinusMinusToken) {
|
|
644
|
+
const [old, q] = this.ev(e.operand, p), n = this.num(old, e.operand, q);
|
|
645
|
+
const v = { t: 'num', e: e.operator === ts.SyntaxKind.PlusPlusToken ? I.add(n, I.num(1)) : I.sub(n, I.num(1)) };
|
|
646
|
+
return [v, this.assign(e.operand, v, q)];
|
|
647
|
+
}
|
|
648
|
+
const [v, q] = this.ev(e.operand, p);
|
|
649
|
+
if (e.operator === ts.SyntaxKind.ExclamationToken)
|
|
650
|
+
return [{ t: 'bool', e: I.not(this.truthy(v, e.operand)) }, q];
|
|
651
|
+
if (e.operator === ts.SyntaxKind.MinusToken)
|
|
652
|
+
return [{ t: 'num', e: I.neg(this.num(v, e.operand, q)) }, q];
|
|
653
|
+
if (e.operator === ts.SyntaxKind.PlusToken)
|
|
654
|
+
return [{ t: 'num', e: this.num(v, e.operand, q) }, q];
|
|
655
|
+
return refuse(e, 'a bitwise operator (it works on 32-bit integers)');
|
|
656
|
+
}
|
|
657
|
+
binary(e, p) {
|
|
658
|
+
const op = e.operatorToken.kind, S = ts.SyntaxKind;
|
|
659
|
+
if (op === S.EqualsToken) {
|
|
660
|
+
const [v, q] = this.ev(e.right, p);
|
|
661
|
+
return [v, this.assign(e.left, v, q)];
|
|
662
|
+
}
|
|
663
|
+
const compound = { [S.PlusEqualsToken]: S.PlusToken, [S.MinusEqualsToken]: S.MinusToken, [S.AsteriskEqualsToken]: S.AsteriskToken };
|
|
664
|
+
if (compound[op] !== undefined) {
|
|
665
|
+
const [l, q] = this.ev(e.left, p), [r, q2] = this.ev(e.right, q);
|
|
666
|
+
const v = this.arith(compound[op], l, r, e, q2);
|
|
667
|
+
return [v, this.assign(e.left, v, q2)];
|
|
668
|
+
}
|
|
669
|
+
if (op === S.QuestionQuestionEqualsToken || op === S.BarBarEqualsToken || op === S.AmpersandAmpersandEqualsToken)
|
|
670
|
+
return refuse(e, `${e.operatorToken.getText()} (an assignment by cases)`);
|
|
671
|
+
if (op === S.AmpersandAmpersandToken || op === S.BarBarToken || op === S.QuestionQuestionToken) {
|
|
672
|
+
const [l, q] = this.ev(e.left, p);
|
|
673
|
+
const c = op === S.QuestionQuestionToken ? (l.t === 'opt' ? l.has : l.t === 'undef' ? I.F : I.T) : this.truthy(l, e.left);
|
|
674
|
+
// The right side runs only where the left doesn't decide: its effects are on that path alone.
|
|
675
|
+
const [runs, keeps] = op === S.AmpersandAmpersandToken ? [c, I.not(c)] : [I.not(c), c];
|
|
676
|
+
if (runs.op === 'const' && !runs.v)
|
|
677
|
+
return [op === S.QuestionQuestionToken && l.t === 'opt' ? l.v : l, q];
|
|
678
|
+
const [r, qr] = this.ev(e.right, this.withCond(q, runs));
|
|
679
|
+
// The left kept: by ?? where it is there, by || where it is truthy (so there too).
|
|
680
|
+
const kept = (op === S.QuestionQuestionToken || op === S.BarBarToken) && l.t === 'opt' ? l.v : l;
|
|
681
|
+
if (keeps.op === 'const' && !keeps.v)
|
|
682
|
+
return [r, { ...qr, pc: q.pc }];
|
|
683
|
+
return this.joinValues(runs, qr, r, this.withCond(q, keeps), kept, e);
|
|
684
|
+
}
|
|
685
|
+
const [l, q] = this.ev(e.left, p), [r, q2] = this.ev(e.right, q);
|
|
686
|
+
const n = (x, at) => this.num(x, at, q2);
|
|
687
|
+
if (op === S.LessThanToken)
|
|
688
|
+
return [{ t: 'bool', e: I.cmp('<', n(l, e.left), n(r, e.right)) }, q2];
|
|
689
|
+
if (op === S.LessThanEqualsToken)
|
|
690
|
+
return [{ t: 'bool', e: I.cmp('<=', n(l, e.left), n(r, e.right)) }, q2];
|
|
691
|
+
if (op === S.GreaterThanToken)
|
|
692
|
+
return [{ t: 'bool', e: I.cmp('<', n(r, e.right), n(l, e.left)) }, q2];
|
|
693
|
+
if (op === S.GreaterThanEqualsToken)
|
|
694
|
+
return [{ t: 'bool', e: I.cmp('<=', n(r, e.right), n(l, e.left)) }, q2];
|
|
695
|
+
if (op === S.EqualsEqualsEqualsToken || op === S.EqualsEqualsToken)
|
|
696
|
+
return [{ t: 'bool', e: this.equal(l, r, e) }, q2];
|
|
697
|
+
if (op === S.ExclamationEqualsEqualsToken || op === S.ExclamationEqualsToken)
|
|
698
|
+
return [{ t: 'bool', e: I.not(this.equal(l, r, e)) }, q2];
|
|
699
|
+
if (op === S.PlusToken || op === S.MinusToken || op === S.AsteriskToken || op === S.PercentToken)
|
|
700
|
+
return [this.arith(op, l, r, e, q2), q2];
|
|
701
|
+
if (op === S.SlashToken)
|
|
702
|
+
return refuse(e, 'a division (its result may be a fraction): Math.floor(x / k) for a constant k is in the subset');
|
|
703
|
+
if (op === S.AsteriskAsteriskToken)
|
|
704
|
+
return refuse(e, 'a power');
|
|
705
|
+
if (op === S.CommaToken)
|
|
706
|
+
return [r, q2];
|
|
707
|
+
if (op === S.InKeyword || op === S.InstanceOfKeyword)
|
|
708
|
+
return refuse(e, e.operatorToken.getText());
|
|
709
|
+
return refuse(e, `${e.operatorToken.getText()} (bitwise operators work on 32-bit integers)`);
|
|
710
|
+
}
|
|
711
|
+
arith(op, l, r, e, p) {
|
|
712
|
+
const S = ts.SyntaxKind;
|
|
713
|
+
if (op === S.PlusToken && (l.t === 'str' || r.t === 'str' || l.t === 'choice' || r.t === 'choice'))
|
|
714
|
+
return refuse(e, 'a string concatenation');
|
|
715
|
+
const a = this.num(l, e.left, p), b = this.num(r, e.right, p);
|
|
716
|
+
if (op === S.PlusToken)
|
|
717
|
+
return { t: 'num', e: I.add(a, b) };
|
|
718
|
+
if (op === S.MinusToken)
|
|
719
|
+
return { t: 'num', e: I.sub(a, b) };
|
|
720
|
+
if (op === S.AsteriskToken)
|
|
721
|
+
return { t: 'num', e: I.mul(a, b) };
|
|
722
|
+
if (op !== S.PercentToken)
|
|
723
|
+
return refuse(e, 'an operator not in the subset');
|
|
724
|
+
if (b.op !== 'const' || b.v < 1)
|
|
725
|
+
return refuse(e, 'a remainder by other than a positive constant');
|
|
726
|
+
return { t: 'num', e: I.modk(b.v, a) };
|
|
727
|
+
}
|
|
728
|
+
object(e, p) {
|
|
729
|
+
const t = this.s.checker.getTypeAtLocation(e);
|
|
730
|
+
let isMap = !!(this.s.checker.getIndexInfoOfType(t, ts.IndexKind.String) || this.s.checker.getIndexInfoOfType(t, ts.IndexKind.Number)) && !this.s.checker.getPropertiesOfType(t).length;
|
|
731
|
+
// Untyped (JavaScript, `{}`): a record keyed by strings where what it spreads first is one.
|
|
732
|
+
const first = e.properties[0];
|
|
733
|
+
if (!isMap && first && ts.isSpreadAssignment(first) && !impure(first.expression) && e.properties.some(x => ts.isPropertyAssignment(x) && ts.isComputedPropertyName(x.name))) {
|
|
734
|
+
const [v0, q0] = this.ev(first.expression, p);
|
|
735
|
+
if (v0.t === 'ref' && this.obj(q0, v0.id)?.k === 'map')
|
|
736
|
+
isMap = true;
|
|
737
|
+
}
|
|
738
|
+
let q = p;
|
|
739
|
+
if (isMap) {
|
|
740
|
+
// A record keyed by strings or numbers, made by spreading another and setting keys: a copy of that map.
|
|
741
|
+
let m;
|
|
742
|
+
for (const prop of e.properties) {
|
|
743
|
+
if (ts.isSpreadAssignment(prop)) {
|
|
744
|
+
const [v, q2] = this.ev(prop.expression, q);
|
|
745
|
+
q = q2;
|
|
746
|
+
const src = v.t === 'ref' ? q.heap.get(v.id) : undefined;
|
|
747
|
+
if (src?.k !== 'map' || m)
|
|
748
|
+
return refuse(prop, 'a record made from other than one record it copies');
|
|
749
|
+
m = { ...src, origin: 'fresh' };
|
|
750
|
+
}
|
|
751
|
+
else if (ts.isPropertyAssignment(prop) && ts.isComputedPropertyName(prop.name)) {
|
|
752
|
+
if (!m)
|
|
753
|
+
return refuse(prop, 'a key set on a record that copies none');
|
|
754
|
+
const [k, q2] = this.ev(prop.name.expression, q), [v, q3] = this.ev(prop.initializer, q2);
|
|
755
|
+
q = q3;
|
|
756
|
+
const id = this.id('map');
|
|
757
|
+
q = { ...q, heap: new Map(q.heap).set(id, m) };
|
|
758
|
+
q = this.mapSet(id, this.key(k, prop.name, m.decl), v, prop, q, false);
|
|
759
|
+
m = q.heap.get(id);
|
|
760
|
+
}
|
|
761
|
+
else
|
|
762
|
+
return refuse(prop, 'a record entry not in the subset');
|
|
763
|
+
}
|
|
764
|
+
if (!m)
|
|
765
|
+
return refuse(e, 'an empty record as a value (a map that starts with no entries is the state\'s, not a step\'s)');
|
|
766
|
+
const id = this.id('map');
|
|
767
|
+
return [{ t: 'ref', id }, { ...q, heap: new Map(q.heap).set(id, m) }];
|
|
768
|
+
}
|
|
769
|
+
const fields = new Map();
|
|
770
|
+
for (const prop of e.properties) {
|
|
771
|
+
if (ts.isSpreadAssignment(prop)) {
|
|
772
|
+
const [v, q2] = this.ev(prop.expression, q);
|
|
773
|
+
q = q2;
|
|
774
|
+
const src = v.t === 'ref' ? this.obj(q, v.id) : undefined;
|
|
775
|
+
if (src?.k !== 'rec')
|
|
776
|
+
return refuse(prop, 'a spread of other than an object');
|
|
777
|
+
if (src.lazy)
|
|
778
|
+
return refuse(prop, 'a spread of an untyped value');
|
|
779
|
+
for (const [f, fv] of src.fields)
|
|
780
|
+
fields.set(f, fv);
|
|
781
|
+
}
|
|
782
|
+
else if (ts.isPropertyAssignment(prop)) {
|
|
783
|
+
const name = ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name) ? prop.name.text : refuse(prop, 'a computed field name on an object');
|
|
784
|
+
const [v, q2] = this.ev(prop.initializer, q);
|
|
785
|
+
q = q2;
|
|
786
|
+
fields.set(name, v);
|
|
787
|
+
}
|
|
788
|
+
else if (ts.isShorthandPropertyAssignment(prop)) {
|
|
789
|
+
const [v, q2] = this.ev(prop.name, q);
|
|
790
|
+
q = q2;
|
|
791
|
+
fields.set(prop.name.text, v);
|
|
792
|
+
}
|
|
793
|
+
else
|
|
794
|
+
return refuse(prop, 'a method or accessor in an object');
|
|
795
|
+
}
|
|
796
|
+
const id = this.id('obj');
|
|
797
|
+
return [{ t: 'ref', id }, { ...q, heap: new Map(q.heap).set(id, { k: 'rec', fields, origin: 'fresh' }) }];
|
|
798
|
+
}
|
|
799
|
+
obj(p, id) {
|
|
800
|
+
let o = p.heap.get(id);
|
|
801
|
+
while (o?.k === 'alias') {
|
|
802
|
+
const to = o.to;
|
|
803
|
+
if (to.t !== 'ref')
|
|
804
|
+
return o;
|
|
805
|
+
o = p.heap.get(to.id);
|
|
806
|
+
}
|
|
807
|
+
return o;
|
|
808
|
+
}
|
|
809
|
+
key(v, at, m) {
|
|
810
|
+
// An untyped record ({} in JavaScript) read at a number: keyed by numbers, and the step read again.
|
|
811
|
+
if (m.untyped && m.key === 'str' && v.t === 'num')
|
|
812
|
+
throw new Retype(`$key:${m.name}`, { k: 'num' });
|
|
813
|
+
if (m.key === 'num') {
|
|
814
|
+
if (v.t !== 'num')
|
|
815
|
+
return refuse(at, 'a key of other than a number for a map keyed by numbers');
|
|
816
|
+
return { op: 'num', e: v.e };
|
|
817
|
+
}
|
|
818
|
+
if (v.t === 'str')
|
|
819
|
+
return v.e;
|
|
820
|
+
if (v.t === 'choice')
|
|
821
|
+
return refuse(at, 'a key from a union of literals (give the key as a string input)');
|
|
822
|
+
return refuse(at, 'a key of other than a string for a map keyed by strings');
|
|
823
|
+
}
|
|
824
|
+
// Two values within one path, by a condition.
|
|
825
|
+
phiV(c, x, y, at) {
|
|
826
|
+
if (x.t === 'num' && y.t === 'num')
|
|
827
|
+
return { t: 'num', e: I.nite(c, x.e, y.e) };
|
|
828
|
+
if (x.t === 'bool' && y.t === 'bool')
|
|
829
|
+
return { t: 'bool', e: I.bite(c, x.e, y.e) };
|
|
830
|
+
if (x.t === 'choice' && y.t === 'choice')
|
|
831
|
+
return { t: 'choice', e: I.lite(c, x.e, y.e), values: x.values };
|
|
832
|
+
if ((x.t === 'choice' || x.t === 'str') && (y.t === 'choice' || y.t === 'str')) {
|
|
833
|
+
if (x.t === 'str' && y.t === 'str')
|
|
834
|
+
return { t: 'str', e: I.kite(c, x.e, y.e) };
|
|
835
|
+
const values = x.t === 'choice' ? x.values : y.values;
|
|
836
|
+
return { t: 'choice', e: I.lite(c, this.asChoice(x, values, at), this.asChoice(y, values, at)), values };
|
|
837
|
+
}
|
|
838
|
+
if (x.t === 'undef' && y.t === 'undef')
|
|
839
|
+
return x;
|
|
840
|
+
if (x.t === 'opt' || y.t === 'opt' || x.t === 'undef' || y.t === 'undef') {
|
|
841
|
+
const [hx, vx] = x.t === 'opt' ? [x.has, x.v] : x.t === 'undef' ? [I.F, undefined] : [I.T, x];
|
|
842
|
+
const [hy, vy] = y.t === 'opt' ? [y.has, y.v] : y.t === 'undef' ? [I.F, undefined] : [I.T, y];
|
|
843
|
+
return { t: 'opt', has: I.bite(c, hx, hy), v: !vx ? vy : !vy ? vx : this.phiV(c, vx, vy, at) };
|
|
844
|
+
}
|
|
845
|
+
if (x.t === 'ref' && y.t === 'ref' && x.id === y.id)
|
|
846
|
+
return x;
|
|
847
|
+
const refs = ['ref', 'cell', 'phi'];
|
|
848
|
+
if (refs.includes(x.t) && refs.includes(y.t))
|
|
849
|
+
return { t: 'phi', c, a: x, b: y };
|
|
850
|
+
return refuse(at, `a value that is a ${x.t} or a ${y.t} by a condition`);
|
|
851
|
+
}
|
|
852
|
+
// A field of a value: a record's, a cell's (through its map), or one an untyped record grows.
|
|
853
|
+
field(v, f, at, p) {
|
|
854
|
+
v = this.deref(p, v);
|
|
855
|
+
if (v.t === 'phi') {
|
|
856
|
+
const [fa, p1] = this.field(v.a, f, at, p), [fb, p2] = this.field(v.b, f, at, p1);
|
|
857
|
+
return [this.phiV(v.c, fa, fb, at), p2];
|
|
858
|
+
}
|
|
859
|
+
if (v.t === 'opt') {
|
|
860
|
+
if (this.known(p, v.has))
|
|
861
|
+
return this.field(v.v, f, at, p);
|
|
862
|
+
return refuse(at, `${text(at)} reads a field of a value that may be missing`);
|
|
863
|
+
}
|
|
864
|
+
if (v.t === 'cell') {
|
|
865
|
+
const mo = p.heap.get(v.map);
|
|
866
|
+
if (mo.gen !== v.gen)
|
|
867
|
+
return refuse(at, `${text(at)}: a reference into ${mo.decl.name} read before the map was set`);
|
|
868
|
+
const cell = this.readCell(mo, v.key);
|
|
869
|
+
const x = cell.vals.get(f);
|
|
870
|
+
return x ? [x, p] : refuse(at, `no field ${f}`);
|
|
871
|
+
}
|
|
872
|
+
if (v.t !== 'ref')
|
|
873
|
+
return refuse(at, `${text(at)}: a field of a ${v.t}`);
|
|
874
|
+
const o = this.obj(p, v.id);
|
|
875
|
+
if (o?.k !== 'rec')
|
|
876
|
+
return refuse(at, `${text(at)}: a field of a map`);
|
|
877
|
+
const x = o.fields.get(f);
|
|
878
|
+
if (x)
|
|
879
|
+
return [x, p];
|
|
880
|
+
if (o.lazy) {
|
|
881
|
+
// Its sort: compared with literals, a choice among them; else its type where it has one (an XState event's
|
|
882
|
+
// payload, typed by setup's types); else a number.
|
|
883
|
+
const typed = (() => { const t = this.s.checker.getTypeAtLocation(at); if (t.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown))
|
|
884
|
+
return undefined; const sh = shapeOf(t, this.s.checker, at); return sh.k === 'num' || sh.k === 'bool' || sh.k === 'choice' || sh.k === 'str' ? sh : undefined; })();
|
|
885
|
+
const path = `${o.lazy.prefix}${f}`, sort = this.s.lazySorts.get(path) ?? typed ?? { k: 'num' };
|
|
886
|
+
// The same field read again (in another guard or action, say) is the same input.
|
|
887
|
+
const seen = [...this.s.lazy].find(([, l]) => l.prefix === o.lazy.prefix && l.field === f)?.[0];
|
|
888
|
+
const nvar = seen ? this.s.vars.get(seen) : this.newVar(path, sort, 'input'), nv = Exec.valueOf(nvar);
|
|
889
|
+
this.s.lazy.set(nvar.name, { prefix: o.lazy.prefix, field: f });
|
|
890
|
+
const fields = new Map(o.fields).set(f, nv);
|
|
891
|
+
return [nv, { ...p, heap: new Map(p.heap).set(v.id, { ...o, fields }) }];
|
|
892
|
+
}
|
|
893
|
+
if (v.id === 'this' && this.s.cls) {
|
|
894
|
+
const member = this.s.cls.members.find(m => m.name && memberName(m.name) === f);
|
|
895
|
+
if (member && ts.isGetAccessorDeclaration(member))
|
|
896
|
+
return this.inline({ decl: member, self: 'this' }, [], at, p);
|
|
897
|
+
if (member && ts.isMethodDeclaration(member))
|
|
898
|
+
return refuse(at, `the method ${f} as a value`);
|
|
899
|
+
}
|
|
900
|
+
return refuse(at, `${text(at)}: no field ${f} the step can read`);
|
|
901
|
+
}
|
|
902
|
+
readCell(m, key) {
|
|
903
|
+
const base = this.baseCell(m.decl, key);
|
|
904
|
+
if (!m.w)
|
|
905
|
+
return base;
|
|
906
|
+
const c = I.keq(key, m.w.key);
|
|
907
|
+
return { has: I.bite(c, m.w.has, base.has), vals: new Map([...base.vals].map(([f, v]) => [f, this.mergeScalar(c, m.w.vals.get(f), v, undefined)])) };
|
|
908
|
+
}
|
|
909
|
+
access(e, p) {
|
|
910
|
+
if (e.questionDotToken)
|
|
911
|
+
return refuse(e, 'optional chaining');
|
|
912
|
+
const [base, q] = this.ev(e.expression, p);
|
|
913
|
+
if (ts.isPropertyAccessExpression(e)) {
|
|
914
|
+
const f = memberName(e.name);
|
|
915
|
+
if (base.t === 'ref') {
|
|
916
|
+
const o = this.obj(q, base.id);
|
|
917
|
+
if (o?.k === 'map')
|
|
918
|
+
return refuse(e, `${f} of a map (get, set, has and delete are in the subset)`);
|
|
919
|
+
}
|
|
920
|
+
if (f === 'length')
|
|
921
|
+
return refuse(e, 'length (lists and strings are not in the subset yet)');
|
|
922
|
+
return this.field(base, f, e, q);
|
|
923
|
+
}
|
|
924
|
+
const arg = e.argumentExpression;
|
|
925
|
+
const b = this.deref(q, base);
|
|
926
|
+
if (b.t === 'ref') {
|
|
927
|
+
const o = this.obj(q, b.id);
|
|
928
|
+
if (o?.k === 'map') {
|
|
929
|
+
if (o.decl.js !== 'record')
|
|
930
|
+
return refuse(e, 'a Map read with [ ] (get is how a Map is read)');
|
|
931
|
+
const [k, q2] = this.ev(arg, q);
|
|
932
|
+
return [this.mapGet(b.id, this.key(k, arg, o.decl), q2), q2];
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
if (ts.isStringLiteral(arg))
|
|
936
|
+
return this.field(base, arg.text, e, q);
|
|
937
|
+
return refuse(e, 'an index into other than a record keyed by strings or numbers');
|
|
938
|
+
}
|
|
939
|
+
mapGet(id, key, p) {
|
|
940
|
+
const m = this.obj(p, id), cell = this.readCell(m, key);
|
|
941
|
+
if (m.decl.scalar)
|
|
942
|
+
return { t: 'opt', has: cell.has, v: cell.vals.get('value') };
|
|
943
|
+
return { t: 'opt', has: cell.has, v: { t: 'cell', map: this.mapId(p, id), key, gen: m.gen } };
|
|
944
|
+
}
|
|
945
|
+
mapId(p, id) {
|
|
946
|
+
let o = p.heap.get(id);
|
|
947
|
+
while (o?.k === 'alias' && o.to.t === 'ref') {
|
|
948
|
+
id = o.to.id;
|
|
949
|
+
o = p.heap.get(id);
|
|
950
|
+
}
|
|
951
|
+
return id;
|
|
952
|
+
}
|
|
953
|
+
// A map written at a key: one key a step (a rule's map takes one write a step).
|
|
954
|
+
mapSet(id, key, v, at, p, track = true) {
|
|
955
|
+
id = this.mapId(p, id);
|
|
956
|
+
const m = p.heap.get(id);
|
|
957
|
+
if (m.origin === 'input')
|
|
958
|
+
return refuse(at, `writes to ${m.decl.name} in the state it was given (React and Redux keep the old state; copy it first)`);
|
|
959
|
+
if (m.origin === 'shared')
|
|
960
|
+
return refuse(at, 'writes to a module-level map (every call shares it)');
|
|
961
|
+
if (m.w && !I.same(m.w.key, key))
|
|
962
|
+
return refuse(at, `writes ${m.decl.name} at a second key in one step (a rule's map takes one write a step)`);
|
|
963
|
+
let vals, has = I.T;
|
|
964
|
+
if (v === undefined) {
|
|
965
|
+
vals = new Map(m.decl.fields.map(f => [f.name, Exec.defaultOf(f.sort)]));
|
|
966
|
+
has = I.F;
|
|
967
|
+
}
|
|
968
|
+
else if (m.decl.scalar)
|
|
969
|
+
vals = new Map([['value', this.cellValue(v, m.decl.fields[0], at, p)]]);
|
|
970
|
+
else {
|
|
971
|
+
const src = this.deref(p, v);
|
|
972
|
+
let fields;
|
|
973
|
+
if (src.t === 'ref') {
|
|
974
|
+
const o = this.obj(p, src.id);
|
|
975
|
+
if (o?.k !== 'rec')
|
|
976
|
+
return refuse(at, 'a map set to other than an object');
|
|
977
|
+
fields = o.fields;
|
|
978
|
+
}
|
|
979
|
+
else if (src.t === 'cell')
|
|
980
|
+
fields = this.readCell(p.heap.get(src.map), src.key).vals;
|
|
981
|
+
else
|
|
982
|
+
return refuse(at, 'a map of objects set to other than an object');
|
|
983
|
+
vals = new Map(m.decl.fields.map(f => { const x = fields.get(f.name); return [f.name, x ? this.cellValue(x, f, at, p) : refuse(at, `the value set has no ${f.name}`)]; }));
|
|
984
|
+
if (fields.size !== m.decl.fields.length)
|
|
985
|
+
refuse(at, 'the value set has fields the map\'s values don\'t');
|
|
986
|
+
}
|
|
987
|
+
const gen = m.gen + 1, heap = new Map(p.heap).set(id, { ...m, gen, w: { key, has, vals } });
|
|
988
|
+
// The object set becomes the cell (a write through it later writes the map).
|
|
989
|
+
if (v && !m.decl.scalar) {
|
|
990
|
+
const src = v.t === 'ref' ? this.mapId(p, v.id) : undefined;
|
|
991
|
+
if (src && heap.get(src)?.k === 'rec' && heap.get(src).origin === 'fresh')
|
|
992
|
+
heap.set(src, { k: 'alias', to: { t: 'cell', map: id, key, gen } });
|
|
993
|
+
}
|
|
994
|
+
return { ...p, heap, wrote: track && m.origin === 'this' ? [...new Set([...p.wrote, m.decl.name])] : p.wrote };
|
|
995
|
+
}
|
|
996
|
+
cellValue(v, f, at, p) {
|
|
997
|
+
switch (f.sort.k) {
|
|
998
|
+
case 'num': return { t: 'num', e: this.num(v, at, p) };
|
|
999
|
+
case 'bool': return { t: 'bool', e: this.bool(v, at) };
|
|
1000
|
+
case 'choice': return { t: 'choice', e: this.asChoice(v, f.sort.values, at), values: f.sort.values };
|
|
1001
|
+
case 'str': return refuse(at, 'a string in a map');
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
// Assignment: to a local, a field (of this, or of an object the step made), a map's key, or through a cell.
|
|
1005
|
+
assign(target, v, p) {
|
|
1006
|
+
if (ts.isParenthesizedExpression(target))
|
|
1007
|
+
return this.assign(target.expression, v, p);
|
|
1008
|
+
if (ts.isIdentifier(target)) {
|
|
1009
|
+
if (!p.locals.has(target.text))
|
|
1010
|
+
return refuse(target, `${target.text}: an assignment to a name outside the step`);
|
|
1011
|
+
const sym = this.s.checker.getSymbolAtLocation(target), decl = sym?.valueDeclaration;
|
|
1012
|
+
if (decl && ts.isVariableDeclaration(decl) && decl.parent.flags & ts.NodeFlags.Const)
|
|
1013
|
+
return refuse(target, `${target.text} is a const`);
|
|
1014
|
+
this.hint(v, target.text);
|
|
1015
|
+
return { ...p, locals: new Map(p.locals).set(target.text, v) };
|
|
1016
|
+
}
|
|
1017
|
+
if (ts.isPropertyAccessExpression(target) || ts.isElementAccessExpression(target)) {
|
|
1018
|
+
const [base, q] = this.ev(target.expression, p);
|
|
1019
|
+
const b = this.deref(q, base);
|
|
1020
|
+
if (ts.isElementAccessExpression(target) && b.t === 'ref') {
|
|
1021
|
+
const o = this.obj(q, b.id);
|
|
1022
|
+
if (o?.k === 'map') {
|
|
1023
|
+
const [k, q2] = this.ev(target.argumentExpression, q);
|
|
1024
|
+
return this.mapSet(b.id, this.key(k, target.argumentExpression, o.decl), v, target, q2);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
const f = ts.isPropertyAccessExpression(target) ? memberName(target.name) : ts.isStringLiteral(target.argumentExpression) ? target.argumentExpression.text : refuse(target, 'an assignment through a computed key');
|
|
1028
|
+
return this.writeField(b, f, v, q, target);
|
|
1029
|
+
}
|
|
1030
|
+
return refuse(target, 'an assignment to other than a name, a field or a key');
|
|
1031
|
+
}
|
|
1032
|
+
// A field written: an object's, a cell's (its map's at the key), or each of two objects' where it is the one.
|
|
1033
|
+
writeField(b, f, v, q, target) {
|
|
1034
|
+
b = this.deref(q, b);
|
|
1035
|
+
if (b.t === 'opt') {
|
|
1036
|
+
if (!this.known(q, b.has))
|
|
1037
|
+
return refuse(target, `${text(target)} writes a field of a value that may be missing`);
|
|
1038
|
+
return this.writeField(b.v, f, v, q, target);
|
|
1039
|
+
}
|
|
1040
|
+
if (b.t === 'phi') {
|
|
1041
|
+
const [oldA] = this.field(b.a, f, target, q), q1 = this.writeField(b.a, f, this.phiV(b.c, v, oldA, target), q, target);
|
|
1042
|
+
const [oldB] = this.field(b.b, f, target, q1);
|
|
1043
|
+
return this.writeField(b.b, f, this.phiV(b.c, oldB, v, target), q1, target);
|
|
1044
|
+
}
|
|
1045
|
+
if (b.t === 'cell') {
|
|
1046
|
+
const m = q.heap.get(b.map);
|
|
1047
|
+
if (m.gen !== b.gen)
|
|
1048
|
+
return refuse(target, `a write through a reference into ${m.decl.name} read before the map was set`);
|
|
1049
|
+
const cell = this.readCell(m, b.key), fv = m.decl.fields.find(x => x.name === f) ?? refuse(target, `no field ${f}`);
|
|
1050
|
+
const vals = new Map(cell.vals).set(f, this.cellValue(v, fv, target, q));
|
|
1051
|
+
if (m.origin === 'input' || m.origin === 'shared')
|
|
1052
|
+
return refuse(target, `writes to ${m.decl.name} in ${m.origin === 'input' ? 'the state it was given (React and Redux keep the old state; copy it first)' : 'a module-level map every call shares'}`);
|
|
1053
|
+
if (m.w && !I.same(m.w.key, b.key))
|
|
1054
|
+
return refuse(target, `writes ${m.decl.name} at a second key in one step`);
|
|
1055
|
+
const gen = m.gen;
|
|
1056
|
+
return { ...q, heap: new Map(q.heap).set(b.map, { ...m, gen, w: { key: b.key, has: I.T, vals } }), wrote: m.origin === 'this' ? [...new Set([...q.wrote, m.decl.name])] : q.wrote };
|
|
1057
|
+
}
|
|
1058
|
+
if (b.t !== 'ref')
|
|
1059
|
+
return refuse(target, `an assignment to a field of a ${b.t}`);
|
|
1060
|
+
const id = this.mapId(q, b.id), o = q.heap.get(id);
|
|
1061
|
+
if (o?.k !== 'rec')
|
|
1062
|
+
return refuse(target, 'an assignment to a map\'s field');
|
|
1063
|
+
if (o.origin === 'input')
|
|
1064
|
+
return refuse(target, `writes ${text(target)} in the state it was given (React and Redux keep the old state, and a state returned as the same object reads as unchanged)`);
|
|
1065
|
+
if (o.origin === 'shared')
|
|
1066
|
+
return refuse(target, `writes ${text(target)}, a module-level object every call shares`);
|
|
1067
|
+
if (!o.fields.has(f))
|
|
1068
|
+
return refuse(target, `a field ${f} the object doesn't have`);
|
|
1069
|
+
const old = o.fields.get(f);
|
|
1070
|
+
if (old.t === 'ref' && v.t !== 'ref')
|
|
1071
|
+
return refuse(target, `${f} set to other than an object`);
|
|
1072
|
+
if (old.t === 'ref' && v.t === 'ref') {
|
|
1073
|
+
const om = this.obj(q, old.id);
|
|
1074
|
+
if (om?.k === 'map')
|
|
1075
|
+
return refuse(target, `${f}: a map replaced in a step (clearing every key is not in the subset yet)`);
|
|
1076
|
+
}
|
|
1077
|
+
const heap = new Map(q.heap).set(id, { ...o, fields: new Map(o.fields).set(f, v) });
|
|
1078
|
+
return { ...q, heap, wrote: o.origin === 'this' ? [...new Set([...q.wrote, f])] : q.wrote };
|
|
1079
|
+
}
|
|
1080
|
+
// A name's symbol, through an import to what it names.
|
|
1081
|
+
resolved(e) {
|
|
1082
|
+
const sym = this.s.checker.getSymbolAtLocation(e);
|
|
1083
|
+
if (sym && sym.flags & ts.SymbolFlags.Alias) {
|
|
1084
|
+
try {
|
|
1085
|
+
return this.s.checker.getAliasedSymbol(sym);
|
|
1086
|
+
}
|
|
1087
|
+
catch {
|
|
1088
|
+
return sym;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
return sym;
|
|
1092
|
+
}
|
|
1093
|
+
// --- calls -----------------------------------------------------------------------------------------------------
|
|
1094
|
+
args(xs, p) {
|
|
1095
|
+
const out = [];
|
|
1096
|
+
let q = p;
|
|
1097
|
+
for (const x of xs) {
|
|
1098
|
+
if (ts.isSpreadElement(x))
|
|
1099
|
+
refuse(x, 'a spread argument');
|
|
1100
|
+
const [v, q2] = this.ev(x, q);
|
|
1101
|
+
out.push(v);
|
|
1102
|
+
q = q2;
|
|
1103
|
+
}
|
|
1104
|
+
return [out, q];
|
|
1105
|
+
}
|
|
1106
|
+
call(e, p) {
|
|
1107
|
+
if (e.questionDotToken)
|
|
1108
|
+
return refuse(e, 'optional chaining');
|
|
1109
|
+
const callee = e.expression;
|
|
1110
|
+
if (ts.isPropertyAccessExpression(callee)) {
|
|
1111
|
+
const obj = callee.expression, m = memberName(callee.name);
|
|
1112
|
+
if (ts.isIdentifier(obj) && obj.text === 'Math' && !p.locals.has('Math'))
|
|
1113
|
+
return this.math(m, e, p);
|
|
1114
|
+
if (ts.isIdentifier(obj) && obj.text === 'Date' && m === 'now' && !p.locals.has('Date'))
|
|
1115
|
+
return this.now(e, p);
|
|
1116
|
+
if (ts.isIdentifier(obj) && obj.text === 'console' && !p.locals.has('console')) {
|
|
1117
|
+
if (e.arguments.some(impure))
|
|
1118
|
+
return refuse(e, 'console output with effects');
|
|
1119
|
+
this.s.notes.add('console output is left out (it writes nothing the rule keeps)');
|
|
1120
|
+
return [{ t: 'undef' }, p];
|
|
1121
|
+
}
|
|
1122
|
+
if (ts.isIdentifier(obj) && ['Number', 'Object', 'JSON', 'Array', 'String', 'Promise', 'performance'].includes(obj.text))
|
|
1123
|
+
return refuse(e, `${obj.text}.${m}`);
|
|
1124
|
+
if (obj.kind === ts.SyntaxKind.ThisKeyword && this.s.cls) {
|
|
1125
|
+
const member = this.s.cls.members.find(x => x.name && memberName(x.name) === m);
|
|
1126
|
+
if (member && ts.isMethodDeclaration(member)) {
|
|
1127
|
+
const [args, q] = this.args(e.arguments, p);
|
|
1128
|
+
return this.inline({ decl: member, self: 'this' }, args, e, q);
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
const [base, q] = this.ev(obj, p), b = this.deref(q, base);
|
|
1132
|
+
const o = b.t === 'ref' ? this.obj(q, b.id) : undefined;
|
|
1133
|
+
if (o?.k === 'map' && b.t === 'ref') {
|
|
1134
|
+
const [args, q2] = this.args(e.arguments, q);
|
|
1135
|
+
const k = () => this.key(args[0] ?? refuse(e, `${m} with no key`), e.arguments[0], o.decl);
|
|
1136
|
+
switch (m) {
|
|
1137
|
+
case 'get': return [this.mapGet(b.id, k(), q2), q2];
|
|
1138
|
+
case 'has': return [{ t: 'bool', e: this.readCell(o, k()).has }, q2];
|
|
1139
|
+
case 'set': return [b, this.mapSet(b.id, k(), args[1] ?? refuse(e, 'set with no value'), e, q2)];
|
|
1140
|
+
case 'delete': {
|
|
1141
|
+
const had = this.readCell(o, k()).has;
|
|
1142
|
+
return [{ t: 'bool', e: had }, this.mapSet(b.id, k(), undefined, e, q2)];
|
|
1143
|
+
}
|
|
1144
|
+
default: return refuse(e, `the map method ${m} (get, set, has and delete are in the subset)`);
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
return refuse(e, `${text(callee)}: a method the subset doesn't read`);
|
|
1148
|
+
}
|
|
1149
|
+
if (ts.isIdentifier(callee)) {
|
|
1150
|
+
const sym = this.resolved(callee), decl = sym?.valueDeclaration;
|
|
1151
|
+
const fn = decl && ts.isFunctionDeclaration(decl) ? decl : decl && ts.isVariableDeclaration(decl) && decl.initializer && (ts.isArrowFunction(decl.initializer) || ts.isFunctionExpression(decl.initializer)) ? decl.initializer : undefined;
|
|
1152
|
+
if (fn && this.s.ours(fn.getSourceFile())) {
|
|
1153
|
+
const [args, q] = this.args(e.arguments, p);
|
|
1154
|
+
return this.inline({ decl: fn }, args, e, q);
|
|
1155
|
+
}
|
|
1156
|
+
if (!decl || !this.s.ours(decl.getSourceFile()))
|
|
1157
|
+
return refuse(e, `${callee.text}: a function from another module (not among the files read)`);
|
|
1158
|
+
return refuse(e, `${callee.text}: a call the subset doesn't read`);
|
|
1159
|
+
}
|
|
1160
|
+
return refuse(e, 'a call the subset doesn\'t read');
|
|
1161
|
+
}
|
|
1162
|
+
math(m, e, p) {
|
|
1163
|
+
if (m === 'floor' || m === 'ceil') {
|
|
1164
|
+
const a = e.arguments[0];
|
|
1165
|
+
const strip = (x) => (ts.isParenthesizedExpression(x) ? strip(x.expression) : x);
|
|
1166
|
+
const d = a && strip(a);
|
|
1167
|
+
if (!d || e.arguments.length !== 1)
|
|
1168
|
+
return refuse(e, `Math.${m} of other than one argument`);
|
|
1169
|
+
if (ts.isBinaryExpression(d) && d.operatorToken.kind === ts.SyntaxKind.SlashToken) {
|
|
1170
|
+
const [x, q] = this.ev(d.left, p), [k, q2] = this.ev(d.right, q);
|
|
1171
|
+
const kn = this.num(k, d.right, q2);
|
|
1172
|
+
if (kn.op !== 'const' || kn.v < 1)
|
|
1173
|
+
return refuse(d.right, `Math.${m}(x / k) by other than a positive constant`);
|
|
1174
|
+
const xn = this.num(x, d.left, q2);
|
|
1175
|
+
return [{ t: 'num', e: m === 'floor' ? I.divk(kn.v, xn) : I.neg(I.divk(kn.v, I.neg(xn))) }, q2];
|
|
1176
|
+
}
|
|
1177
|
+
// Of an integer, its value.
|
|
1178
|
+
const [x, q] = this.ev(d, p);
|
|
1179
|
+
return [{ t: 'num', e: this.num(x, d, q) }, q];
|
|
1180
|
+
}
|
|
1181
|
+
if (m === 'min' || m === 'max') {
|
|
1182
|
+
if (e.arguments.length < 1)
|
|
1183
|
+
return refuse(e, `Math.${m}() of nothing`);
|
|
1184
|
+
const [args, q] = this.args(e.arguments, p), ns = args.map((a, i) => this.num(a, e.arguments[i], q));
|
|
1185
|
+
return [{ t: 'num', e: ns.slice(1).reduce((acc, n) => (m === 'min' ? I.nmin(acc, n) : I.nmax(acc, n)), ns[0]) }, q];
|
|
1186
|
+
}
|
|
1187
|
+
if (m === 'abs') {
|
|
1188
|
+
const [args, q] = this.args(e.arguments, p), x = this.num(args[0] ?? refuse(e, 'Math.abs()'), e.arguments[0], q);
|
|
1189
|
+
return [{ t: 'num', e: I.nite(I.cmp('<', x, I.num(0)), I.neg(x), x) }, q];
|
|
1190
|
+
}
|
|
1191
|
+
if (m === 'random')
|
|
1192
|
+
return refuse(e, 'Math.random (a step the code takes differently each time)');
|
|
1193
|
+
return refuse(e, `Math.${m} (Math.min, Math.max, Math.abs, and Math.floor or Math.ceil of x / k are in the subset)`);
|
|
1194
|
+
}
|
|
1195
|
+
now(e, p) {
|
|
1196
|
+
if (p.nows > 0)
|
|
1197
|
+
return refuse(e, 'Date.now() read twice in one step (the two reads may differ: read it once into a local)');
|
|
1198
|
+
if (!this.s.now) {
|
|
1199
|
+
this.s.now = this.fresh(this.s.nowRole === 'param' ? 'now0' : 'now');
|
|
1200
|
+
this.s.vars.set(this.s.now, { name: this.s.now, sort: { k: 'num' }, role: this.s.nowRole, note: this.s.nowRole === 'param' ? 'Date.now() when the object is made' : 'Date.now(): any time, not only one later than the last (the proof covers every clock)' });
|
|
1201
|
+
}
|
|
1202
|
+
return [{ t: 'num', e: I.nvar(this.s.now) }, { ...p, nows: p.nows + 1 }];
|
|
1203
|
+
}
|
|
1204
|
+
newExpr(e, p) {
|
|
1205
|
+
const c = e.expression;
|
|
1206
|
+
if (ts.isIdentifier(c) && c.text === 'Map') {
|
|
1207
|
+
const a = e.arguments ?? [];
|
|
1208
|
+
if (a.length !== 1)
|
|
1209
|
+
return refuse(e, 'a new Map in a step (clearing every key is not in the subset yet)');
|
|
1210
|
+
const [v, q] = this.ev(a[0], p), src = v.t === 'ref' ? this.obj(q, v.id) : undefined;
|
|
1211
|
+
if (src?.k !== 'map')
|
|
1212
|
+
return refuse(e, 'a new Map of other than a map');
|
|
1213
|
+
const id = this.id('map');
|
|
1214
|
+
return [{ t: 'ref', id }, { ...q, heap: new Map(q.heap).set(id, { ...src, origin: 'fresh' }) }];
|
|
1215
|
+
}
|
|
1216
|
+
if (ts.isIdentifier(c) && c.text === 'Date')
|
|
1217
|
+
return refuse(e, 'new Date() (Date.now() is how the subset reads the time)');
|
|
1218
|
+
return refuse(e, `new ${text(c)}`);
|
|
1219
|
+
}
|
|
1220
|
+
// A function of the same module, or a method or getter of the class, run where it is called.
|
|
1221
|
+
inline(f, args, at, p) {
|
|
1222
|
+
if (++this.depth > 24)
|
|
1223
|
+
return refuse(at, 'calls nested too deep (recursion is not in the subset)');
|
|
1224
|
+
if (f.decl.getSourceFile() === this.s.sf)
|
|
1225
|
+
(this.s.spans ??= []).push([f.decl.getStart(), f.decl.end]);
|
|
1226
|
+
try {
|
|
1227
|
+
const d = f.decl;
|
|
1228
|
+
if (ts.isMethodDeclaration(d) || ts.isFunctionDeclaration(d))
|
|
1229
|
+
if (d.modifiers?.some(m => m.kind === ts.SyntaxKind.AsyncKeyword) || d.asteriskToken)
|
|
1230
|
+
return refuse(at, 'an async or generator function');
|
|
1231
|
+
if (!d.body)
|
|
1232
|
+
return refuse(at, 'a function without a body');
|
|
1233
|
+
let q = { ...p, locals: new Map() };
|
|
1234
|
+
d.parameters.forEach((param, i) => {
|
|
1235
|
+
if (param.dotDotDotToken)
|
|
1236
|
+
refuse(param, 'a rest parameter');
|
|
1237
|
+
let v = args[i];
|
|
1238
|
+
if (v === undefined) {
|
|
1239
|
+
if (!param.initializer)
|
|
1240
|
+
refuse(at, 'a call with fewer arguments than the function takes');
|
|
1241
|
+
const [dv, q2] = this.ev(param.initializer, q);
|
|
1242
|
+
v = dv;
|
|
1243
|
+
q = q2;
|
|
1244
|
+
}
|
|
1245
|
+
q = this.bind(param.name, v, q);
|
|
1246
|
+
});
|
|
1247
|
+
if (!ts.isBlock(d.body)) {
|
|
1248
|
+
const [v, q2] = this.ev(d.body, q);
|
|
1249
|
+
return [v, { ...q2, locals: p.locals }];
|
|
1250
|
+
}
|
|
1251
|
+
const flow = this.block(d.body.statements, q);
|
|
1252
|
+
const outs = [...flow.rets, ...(flow.live ? [[flow.live, { t: 'undef' }]] : [])];
|
|
1253
|
+
if (!outs.length)
|
|
1254
|
+
return [{ t: 'undef' }, { ...p, pc: [...p.pc, I.F] }];
|
|
1255
|
+
const [first, ...rest] = outs;
|
|
1256
|
+
let [mp, mv] = first;
|
|
1257
|
+
for (const [rp, rv] of rest)
|
|
1258
|
+
[mv, mp] = this.joinValues(I.T, mp, mv, rp, rv, at);
|
|
1259
|
+
return [mv, { ...mp, locals: p.locals }];
|
|
1260
|
+
}
|
|
1261
|
+
finally {
|
|
1262
|
+
this.depth--;
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
// A map set on one path and not the other: a generation of its own, so a reference read before either is stale.
|
|
1267
|
+
const mergedGen = (a, b) => (a.gen === b.gen ? a.gen : Math.max(a.gen, b.gen) + 1);
|
|
1268
|
+
// The module an import names, for a refusal.
|
|
1269
|
+
const moduleOf = (d) => { let n = d; while (n && !ts.isImportDeclaration(n))
|
|
1270
|
+
n = n.parent; return n && ts.isImportDeclaration(n) && ts.isStringLiteral(n.moduleSpecifier) ? ` ('${n.moduleSpecifier.text}')` : ''; };
|
|
1271
|
+
// Names JavaScript itself declares, and those a rule's builders hand out beside the code's.
|
|
1272
|
+
const RESERVED = new Set(['event', 'in', 'last', 'constructor', 'prototype', '__proto__', 'Math', 'Date', 'Int', 'when', 'min', 'max', 'all', 'any', 'not', 'yes', 'no', 'lit', 'intern', 'nat', 'int', 'bool', 'oneOf', 'str', 'record', 'rule', 'p', 'fromXState']);
|
|
1273
|
+
export const memberName = (n) => (ts.isIdentifier(n) || ts.isPrivateIdentifier(n) || ts.isStringLiteral(n) || ts.isNumericLiteral(n) ? n.text : n.getText());
|
|
1274
|
+
const boundNames = (n) => (ts.isIdentifier(n) ? [n.text] : n.elements.flatMap(el => (ts.isOmittedExpression(el) ? [] : boundNames(el.name))));
|
|
1275
|
+
// Whether an expression may have effects (an assignment, an increment, a call other than an error's constructor).
|
|
1276
|
+
export function impure(e) {
|
|
1277
|
+
let found = false;
|
|
1278
|
+
const visit = (n) => {
|
|
1279
|
+
if (found)
|
|
1280
|
+
return;
|
|
1281
|
+
if (ts.isBinaryExpression(n) && n.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && n.operatorToken.kind <= ts.SyntaxKind.LastAssignment)
|
|
1282
|
+
found = true;
|
|
1283
|
+
else if ((ts.isPrefixUnaryExpression(n) || ts.isPostfixUnaryExpression(n)) && (n.operator === ts.SyntaxKind.PlusPlusToken || n.operator === ts.SyntaxKind.MinusMinusToken))
|
|
1284
|
+
found = true;
|
|
1285
|
+
else if (ts.isCallExpression(n) && !(ts.isIdentifier(n.expression) && /Error$/.test(n.expression.text)))
|
|
1286
|
+
found = true;
|
|
1287
|
+
else if (ts.isNewExpression(n) && !(ts.isIdentifier(n.expression) && /Error$/.test(n.expression.text)))
|
|
1288
|
+
found = true;
|
|
1289
|
+
else if (ts.isPropertyAccessExpression(n) && !ts.isCallExpression(n.parent)) {
|
|
1290
|
+
// A getter may run code: only reads of plain names, `this.x` and `obj.x` are taken as pure.
|
|
1291
|
+
ts.forEachChild(n, visit);
|
|
1292
|
+
}
|
|
1293
|
+
else
|
|
1294
|
+
ts.forEachChild(n, visit);
|
|
1295
|
+
};
|
|
1296
|
+
visit(e);
|
|
1297
|
+
return found;
|
|
1298
|
+
}
|