@tsplc/compiler 0.5.4 → 0.5.6
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/dist/bun-runtime.js +14 -2
- package/dist/bun-runtime.js.map +4 -4
- package/dist/codesys.js +46 -2
- package/dist/codesys.js.map +3 -3
- package/dist/collections.js +395 -0
- package/dist/collections.js.map +10 -0
- package/dist/elaborate-capture.js +64 -5
- package/dist/elaborate-capture.js.map +4 -4
- package/dist/index.js +5345 -1074
- package/dist/index.js.map +32 -25
- package/dist/ir.js +122 -1
- package/dist/ir.js.map +6 -6
- package/package.json +14 -9
- package/src/codesys/numeric.ts +61 -0
- package/src/frontend/control-flow.ts +752 -0
- package/src/frontend/coroutines.ts +171 -7
- package/src/frontend/elaborate/array-searches.ts +216 -0
- package/src/frontend/elaborate/assignments.ts +352 -0
- package/src/frontend/elaborate/calls.ts +488 -49
- package/src/frontend/elaborate/capture-protocol.ts +19 -0
- package/src/frontend/elaborate/capture.ts +82 -3
- package/src/frontend/elaborate/dialect.ts +118 -4
- package/src/frontend/elaborate/expressions.ts +927 -80
- package/src/frontend/elaborate/filtered-arrays.ts +607 -0
- package/src/frontend/elaborate/frame.ts +44 -1
- package/src/frontend/elaborate/generics.ts +137 -0
- package/src/frontend/elaborate/graph.ts +91 -0
- package/src/frontend/elaborate/imports.ts +14 -15
- package/src/frontend/elaborate/index.ts +30 -5
- package/src/frontend/elaborate/initializers.ts +15 -1
- package/src/frontend/elaborate/iterators.ts +1677 -0
- package/src/frontend/elaborate/layouts.ts +41 -19
- package/src/frontend/elaborate/model.ts +64 -8
- package/src/frontend/elaborate/references.ts +749 -0
- package/src/frontend/elaborate/specialize.ts +459 -29
- package/src/frontend/elaborate/statements.ts +552 -133
- package/src/frontend/elaborate/storage.ts +151 -10
- package/src/frontend/elaborate/values.ts +72 -0
- package/src/frontend/elaborate/writes.ts +302 -1
- package/src/frontend/intrinsics.ts +32 -0
- package/src/frontend/module-graph.ts +2 -1
- package/src/frontend/positions.ts +12 -5
- package/src/frontend/sdk.ts +7 -1
- package/src/frontend/typescript.ts +5 -1
- package/src/ir/core-model.ts +2 -6
- package/src/ir/fold.ts +30 -1
- package/src/ir/index.ts +5 -0
- package/src/ir/model.ts +53 -2
- package/src/ir/scalars.ts +126 -0
- package/src/runtime/.runtime-VaUSOu/server/router.ts +17 -0
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +45 -0
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +58 -0
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +6 -0
- package/src/runtime/.runtime-VaUSOu/src/main.ts +11 -0
- package/src/runtime/.runtime-VaUSOu/src/services.ts +8 -0
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +13 -0
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +31 -0
- package/src/runtime/.runtime-ybIxYh/server/router.ts +17 -0
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +45 -0
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +58 -0
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +6 -0
- package/src/runtime/.runtime-ybIxYh/src/main.ts +11 -0
- package/src/runtime/.runtime-ybIxYh/src/services.ts +8 -0
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +13 -0
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +31 -0
- package/stdlib/collections.ts +514 -0
- package/types/index.d.ts +18 -0
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
import { joined, type Mapped, mapped, type Part } from "../positions";
|
|
3
|
+
import type { DialectText } from "./dialect";
|
|
4
|
+
import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
|
|
5
|
+
import type { Frame } from "./frame";
|
|
6
|
+
import type { ModelNode } from "./model";
|
|
7
|
+
import {
|
|
8
|
+
chain,
|
|
9
|
+
referable,
|
|
10
|
+
referenceLayout,
|
|
11
|
+
settled,
|
|
12
|
+
union,
|
|
13
|
+
} from "./references";
|
|
14
|
+
import { constant, repeatable, SpecializeError, type Value } from "./values";
|
|
15
|
+
|
|
16
|
+
type Masked = Extract<Value, { kind: "masked" }>;
|
|
17
|
+
type Entry = Masked["entries"][number];
|
|
18
|
+
|
|
19
|
+
const isTrue = (value: Value): boolean =>
|
|
20
|
+
value.kind === "const" && Boolean(value.value);
|
|
21
|
+
|
|
22
|
+
const isFalse = (value: Value): boolean =>
|
|
23
|
+
value.kind === "const" && !value.value;
|
|
24
|
+
|
|
25
|
+
/** Entries with their indexes, first to last or, for a search from the end, last to first. */
|
|
26
|
+
const ordered = (
|
|
27
|
+
entries: readonly Entry[],
|
|
28
|
+
{ last }: { last: boolean },
|
|
29
|
+
): [number, Entry][] => {
|
|
30
|
+
const indexed = [...entries.entries()];
|
|
31
|
+
return last ? indexed.reverse() : indexed;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const whole = (items: readonly Value[]): Entry[] =>
|
|
35
|
+
items.map((item) => ({ item, present: constant(true) }));
|
|
36
|
+
|
|
37
|
+
const objects = (items: readonly Value[]): boolean =>
|
|
38
|
+
items.every((item) => item.kind === "static" && referable(item.node));
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Arrays of known values whose contents or order scan code decides. A filter with a test decided
|
|
42
|
+
* at run time keeps every candidate with a flag for whether the array holds it, and `toSorted`
|
|
43
|
+
* keeps its comparator; neither builds an array. What reads them counts the flags, or picks the
|
|
44
|
+
* object a search or the sort's order puts first, as a reference.
|
|
45
|
+
*/
|
|
46
|
+
export class FilteredArrays {
|
|
47
|
+
private readonly checker: ts.TypeChecker;
|
|
48
|
+
private readonly dialect: DialectText;
|
|
49
|
+
|
|
50
|
+
constructor(
|
|
51
|
+
context: SpecializeContext,
|
|
52
|
+
private readonly expressions: ExpressionSpecializer,
|
|
53
|
+
) {
|
|
54
|
+
this.checker = context.checker;
|
|
55
|
+
this.dialect = context.dialect;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* An array method decided at run time: on a filtered array, or one over an array setup built
|
|
60
|
+
* that filters, sorts or picks an object from it. Undefined for a call setup decides.
|
|
61
|
+
*/
|
|
62
|
+
call(
|
|
63
|
+
name: string,
|
|
64
|
+
receiver: Value,
|
|
65
|
+
items: readonly Value[] | undefined,
|
|
66
|
+
node: ts.CallExpression,
|
|
67
|
+
frame: Frame,
|
|
68
|
+
): Value | undefined {
|
|
69
|
+
if (receiver.kind === "masked") {
|
|
70
|
+
return this.method(receiver, name, node, frame);
|
|
71
|
+
}
|
|
72
|
+
if (!items) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
switch (name) {
|
|
76
|
+
case "toSorted":
|
|
77
|
+
return {
|
|
78
|
+
kind: "masked",
|
|
79
|
+
entries: whole(items),
|
|
80
|
+
order: this.callback(node, frame),
|
|
81
|
+
};
|
|
82
|
+
case "filter":
|
|
83
|
+
return this.filtered(
|
|
84
|
+
{ kind: "masked", entries: whole(items) },
|
|
85
|
+
this.test(node, frame),
|
|
86
|
+
node,
|
|
87
|
+
frame,
|
|
88
|
+
{ decided: "defer" },
|
|
89
|
+
);
|
|
90
|
+
case "find":
|
|
91
|
+
case "findLast":
|
|
92
|
+
return objects(items)
|
|
93
|
+
? this.search(
|
|
94
|
+
whole(items),
|
|
95
|
+
{ last: name === "findLast" },
|
|
96
|
+
this.test(node, frame),
|
|
97
|
+
node,
|
|
98
|
+
frame,
|
|
99
|
+
)
|
|
100
|
+
: undefined;
|
|
101
|
+
case "at": {
|
|
102
|
+
const [argument] = node.arguments;
|
|
103
|
+
return argument && objects(items)
|
|
104
|
+
? this.indexed(
|
|
105
|
+
items,
|
|
106
|
+
this.expressions.expression(argument, frame),
|
|
107
|
+
{ fromEnd: true },
|
|
108
|
+
node,
|
|
109
|
+
frame,
|
|
110
|
+
)
|
|
111
|
+
: undefined;
|
|
112
|
+
}
|
|
113
|
+
default:
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private method(
|
|
119
|
+
masked: Masked,
|
|
120
|
+
name: string,
|
|
121
|
+
node: ts.CallExpression,
|
|
122
|
+
frame: Frame,
|
|
123
|
+
): Value {
|
|
124
|
+
switch (name) {
|
|
125
|
+
case "filter":
|
|
126
|
+
return this.filtered(masked, this.test(node, frame), node, frame, {
|
|
127
|
+
decided: "keep",
|
|
128
|
+
});
|
|
129
|
+
case "some":
|
|
130
|
+
case "every":
|
|
131
|
+
return this.quantified(
|
|
132
|
+
masked,
|
|
133
|
+
{ some: name === "some" },
|
|
134
|
+
this.test(node, frame),
|
|
135
|
+
node,
|
|
136
|
+
frame,
|
|
137
|
+
);
|
|
138
|
+
case "find":
|
|
139
|
+
case "findLast": {
|
|
140
|
+
const last = name === "findLast";
|
|
141
|
+
const test = this.test(node, frame);
|
|
142
|
+
// In a sorted array the first to pass is the least of those that pass.
|
|
143
|
+
return masked.order
|
|
144
|
+
? this.extreme(
|
|
145
|
+
this.filtered(masked, test, node, frame, { decided: "keep" }),
|
|
146
|
+
{ last },
|
|
147
|
+
node,
|
|
148
|
+
frame,
|
|
149
|
+
)
|
|
150
|
+
: this.search(masked.entries, { last }, test, node, frame);
|
|
151
|
+
}
|
|
152
|
+
case "at": {
|
|
153
|
+
const [argument] = node.arguments;
|
|
154
|
+
const index = argument && this.expressions.expression(argument, frame);
|
|
155
|
+
return this.end(masked, index, node, frame);
|
|
156
|
+
}
|
|
157
|
+
case "toSorted":
|
|
158
|
+
if (masked.order) {
|
|
159
|
+
throw new SpecializeError(
|
|
160
|
+
node,
|
|
161
|
+
"Sort an array once, with one comparator: a second toSorted would leave ties in the first one's order, which isn't kept.",
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
return { ...masked, order: this.callback(node, frame) };
|
|
165
|
+
default:
|
|
166
|
+
throw new SpecializeError(
|
|
167
|
+
node,
|
|
168
|
+
`${name} is not available on an array filtered or sorted at run time, which has no storage: use its length, some, every, filter, find, toSorted, at(0) or at(-1).`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** `.length` of an array filtered at run time; it has nothing else to read. */
|
|
174
|
+
member(masked: Masked, name: string, at: ts.Node, frame: Frame): Value {
|
|
175
|
+
if (name !== "length") {
|
|
176
|
+
throw new SpecializeError(
|
|
177
|
+
at,
|
|
178
|
+
`${name} cannot be read from an array filtered or sorted at run time.`,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
const live = masked.entries.filter((entry) => !isFalse(entry.present));
|
|
182
|
+
const certain = live.filter((entry) => isTrue(entry.present)).length;
|
|
183
|
+
if (certain === live.length) {
|
|
184
|
+
return constant(certain);
|
|
185
|
+
}
|
|
186
|
+
const total = frame.emitter.temporary();
|
|
187
|
+
frame.emitter.line(`let ${total}: number = ${certain};`);
|
|
188
|
+
for (const entry of live) {
|
|
189
|
+
if (entry.present.kind !== "const") {
|
|
190
|
+
frame.emitter.line(
|
|
191
|
+
mapped`if (${this.dialect.condition(entry.present, at)}) { ${total} = ${total} + 1; }`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
kind: "code",
|
|
197
|
+
text: total,
|
|
198
|
+
declared: this.checker.getNumberType(),
|
|
199
|
+
place: true,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** `array[0]` of an array filtered or sorted at run time: its first element. */
|
|
204
|
+
element(masked: Masked, index: Value, at: ts.Node, frame: Frame): Value {
|
|
205
|
+
if (index.kind === "const" && index.value === 0) {
|
|
206
|
+
return this.extreme(masked, { last: false }, at, frame);
|
|
207
|
+
}
|
|
208
|
+
throw new SpecializeError(
|
|
209
|
+
at,
|
|
210
|
+
"An array filtered or sorted at run time has no storage to index: read [0], at(0) or at(-1).",
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** `at(0)` and `at(-1)` of an array filtered or sorted at run time. */
|
|
215
|
+
private end(
|
|
216
|
+
masked: Masked,
|
|
217
|
+
index: Value | undefined,
|
|
218
|
+
at: ts.Node,
|
|
219
|
+
frame: Frame,
|
|
220
|
+
): Value {
|
|
221
|
+
if (index?.kind === "const" && (index.value === 0 || index.value === -1)) {
|
|
222
|
+
return this.extreme(masked, { last: index.value === -1 }, at, frame);
|
|
223
|
+
}
|
|
224
|
+
throw new SpecializeError(
|
|
225
|
+
at,
|
|
226
|
+
"An array filtered or sorted at run time has no storage to index: read at(0) or at(-1).",
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private callback(node: ts.CallExpression, frame: Frame): Value {
|
|
231
|
+
const [argument] = node.arguments;
|
|
232
|
+
if (!argument) {
|
|
233
|
+
throw new SpecializeError(node, "Pass a function.");
|
|
234
|
+
}
|
|
235
|
+
return this.expressions.expression(argument, frame);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** An array method's callback as a test of one element. */
|
|
239
|
+
private test(
|
|
240
|
+
node: ts.CallExpression,
|
|
241
|
+
frame: Frame,
|
|
242
|
+
): (item: Value, index: number) => Value {
|
|
243
|
+
const callback = this.callback(node, frame);
|
|
244
|
+
return (item, index) =>
|
|
245
|
+
this.truth(
|
|
246
|
+
this.expressions.invoke(callback, [item, constant(index)], node, frame),
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** A callback's result as a condition: an object is truthy, and a reference is when it holds one. */
|
|
251
|
+
private truth(value: Value): Value {
|
|
252
|
+
if (value.kind === "choice") {
|
|
253
|
+
return this.expressions.references.present(value);
|
|
254
|
+
}
|
|
255
|
+
return value.kind === "const" || value.kind === "code"
|
|
256
|
+
? value
|
|
257
|
+
: constant(true);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private flag(text: string | Mapped): Value {
|
|
261
|
+
return {
|
|
262
|
+
kind: "code",
|
|
263
|
+
text,
|
|
264
|
+
declared: this.checker.getBooleanType(),
|
|
265
|
+
place: true,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* `filter(test)` decided at run time: the same candidates, each present when it was and its
|
|
271
|
+
* test passes. A test runs only on a candidate the array holds. A filter setup decides is left
|
|
272
|
+
* to unroll when `decided` is "defer".
|
|
273
|
+
*/
|
|
274
|
+
private filtered(
|
|
275
|
+
masked: Masked,
|
|
276
|
+
test: (item: Value, index: number) => Value,
|
|
277
|
+
at: ts.Node,
|
|
278
|
+
frame: Frame,
|
|
279
|
+
{ decided }: { decided: "keep" },
|
|
280
|
+
): Masked;
|
|
281
|
+
private filtered(
|
|
282
|
+
masked: Masked,
|
|
283
|
+
test: (item: Value, index: number) => Value,
|
|
284
|
+
at: ts.Node,
|
|
285
|
+
frame: Frame,
|
|
286
|
+
{ decided }: { decided: "defer" },
|
|
287
|
+
): Masked | undefined;
|
|
288
|
+
private filtered(
|
|
289
|
+
masked: Masked,
|
|
290
|
+
test: (item: Value, index: number) => Value,
|
|
291
|
+
at: ts.Node,
|
|
292
|
+
frame: Frame,
|
|
293
|
+
{ decided }: { decided: "defer" | "keep" },
|
|
294
|
+
): Masked | undefined {
|
|
295
|
+
const tested = masked.entries.map((entry, index) => {
|
|
296
|
+
if (isFalse(entry.present)) {
|
|
297
|
+
return { entry, passes: constant(false), lines: [] };
|
|
298
|
+
}
|
|
299
|
+
const { result, lines } = frame.emitter.detached(
|
|
300
|
+
{ nested: entry.present.kind !== "const" },
|
|
301
|
+
() => test(entry.item, index),
|
|
302
|
+
);
|
|
303
|
+
return { entry, passes: result, lines };
|
|
304
|
+
});
|
|
305
|
+
if (
|
|
306
|
+
decided === "defer" &&
|
|
307
|
+
tested.every(({ passes }) => passes.kind === "const")
|
|
308
|
+
) {
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
const entries = tested.map(({ entry, passes, lines }): Entry => {
|
|
312
|
+
const { item, present } = entry;
|
|
313
|
+
if (present.kind === "const") {
|
|
314
|
+
frame.emitter.append(lines);
|
|
315
|
+
if (passes.kind === "const") {
|
|
316
|
+
return { item, present: constant(isTrue(passes)) };
|
|
317
|
+
}
|
|
318
|
+
const text = this.dialect.condition(passes, at);
|
|
319
|
+
if (repeatable(text)) {
|
|
320
|
+
return { item, present: passes };
|
|
321
|
+
}
|
|
322
|
+
const held = frame.emitter.temporary();
|
|
323
|
+
frame.emitter.line(mapped`const ${held}: boolean = ${text};`);
|
|
324
|
+
return { item, present: this.flag(held) };
|
|
325
|
+
}
|
|
326
|
+
if (passes.kind === "const" && lines.length === 0) {
|
|
327
|
+
return { item, present: passes.value ? present : constant(false) };
|
|
328
|
+
}
|
|
329
|
+
const held = frame.emitter.temporary();
|
|
330
|
+
frame.emitter.line(`let ${held}: boolean = false;`);
|
|
331
|
+
frame.emitter.open(mapped`if (${this.dialect.condition(present, at)}) {`);
|
|
332
|
+
frame.emitter.append(lines);
|
|
333
|
+
frame.emitter.line(
|
|
334
|
+
mapped`${held} = ${this.dialect.condition(passes, at)};`,
|
|
335
|
+
);
|
|
336
|
+
frame.emitter.close();
|
|
337
|
+
return { item, present: this.flag(held) };
|
|
338
|
+
});
|
|
339
|
+
return { ...masked, entries };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** `some(test)` and `every(test)` over the candidates an array filtered at run time holds; tests stop once one decides. */
|
|
343
|
+
private quantified(
|
|
344
|
+
masked: Masked,
|
|
345
|
+
{ some }: { some: boolean },
|
|
346
|
+
test: (item: Value, index: number) => Value,
|
|
347
|
+
at: ts.Node,
|
|
348
|
+
frame: Frame,
|
|
349
|
+
): Value {
|
|
350
|
+
const result = frame.emitter.temporary();
|
|
351
|
+
frame.emitter.line(`let ${result}: boolean = ${!some};`);
|
|
352
|
+
for (const [index, entry] of masked.entries.entries()) {
|
|
353
|
+
if (isFalse(entry.present)) {
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
const guards: Part[] = [
|
|
357
|
+
some ? `!${result}` : result,
|
|
358
|
+
...(entry.present.kind === "const"
|
|
359
|
+
? []
|
|
360
|
+
: [this.dialect.condition(entry.present, at)]),
|
|
361
|
+
];
|
|
362
|
+
frame.emitter.open(mapped`if (${joined(guards, " && ")}) {`);
|
|
363
|
+
const passes = test(entry.item, index);
|
|
364
|
+
if (passes.kind !== "const") {
|
|
365
|
+
frame.emitter.line(
|
|
366
|
+
mapped`${result} = ${this.dialect.condition(passes, at)};`,
|
|
367
|
+
);
|
|
368
|
+
} else if (Boolean(passes.value) === some) {
|
|
369
|
+
frame.emitter.line(`${result} = ${some};`);
|
|
370
|
+
}
|
|
371
|
+
frame.emitter.close();
|
|
372
|
+
}
|
|
373
|
+
return this.flag(result);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* `find` and `findLast` among objects setup built, and the first or last an unsorted filtered
|
|
378
|
+
* array holds: the first candidate present whose test passes, tests stopping once one has.
|
|
379
|
+
*/
|
|
380
|
+
private search(
|
|
381
|
+
entries: readonly Entry[],
|
|
382
|
+
{ last }: { last: boolean },
|
|
383
|
+
test: ((item: Value, index: number) => Value) | undefined,
|
|
384
|
+
at: ts.Node,
|
|
385
|
+
frame: Frame,
|
|
386
|
+
): Value {
|
|
387
|
+
const arms: {
|
|
388
|
+
node: ModelNode;
|
|
389
|
+
present: Value;
|
|
390
|
+
passes: Value;
|
|
391
|
+
lines: Mapped[];
|
|
392
|
+
}[] = [];
|
|
393
|
+
for (const [index, entry] of ordered(entries, { last })) {
|
|
394
|
+
if (isFalse(entry.present)) {
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
const node = this.object(entry.item, at);
|
|
398
|
+
const { result: passes, lines } = test
|
|
399
|
+
? frame.emitter.detached({ nested: true }, () =>
|
|
400
|
+
test(entry.item, index),
|
|
401
|
+
)
|
|
402
|
+
: { result: constant(true), lines: [] };
|
|
403
|
+
if (isFalse(passes) && lines.length === 0) {
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
arms.push({ node, present: entry.present, passes, lines });
|
|
407
|
+
// Nothing after a candidate that is surely there and surely passes is reached.
|
|
408
|
+
if (isTrue(entry.present) && isTrue(passes)) {
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
const final = arms.at(-1);
|
|
413
|
+
if (!final) {
|
|
414
|
+
return constant(undefined);
|
|
415
|
+
}
|
|
416
|
+
const certain = isTrue(final.present) && isTrue(final.passes);
|
|
417
|
+
if (arms.length === 1 && certain && final.lines.length === 0) {
|
|
418
|
+
return { kind: "static", node: final.node };
|
|
419
|
+
}
|
|
420
|
+
const result = frame.emitter.temporary();
|
|
421
|
+
frame.emitter.line(`let ${result}: ${referenceLayout} = -1;`);
|
|
422
|
+
for (const arm of arms) {
|
|
423
|
+
const guards: Part[] = [
|
|
424
|
+
`${result} < 0`,
|
|
425
|
+
...(arm.present.kind === "const"
|
|
426
|
+
? []
|
|
427
|
+
: [this.dialect.condition(arm.present, at)]),
|
|
428
|
+
];
|
|
429
|
+
frame.emitter.open(mapped`if (${joined(guards, " && ")}) {`);
|
|
430
|
+
frame.emitter.append(arm.lines);
|
|
431
|
+
if (isTrue(arm.passes)) {
|
|
432
|
+
frame.emitter.line(`${result} = ${arm.node.id};`);
|
|
433
|
+
} else if (arm.passes.kind !== "const") {
|
|
434
|
+
frame.emitter.line(
|
|
435
|
+
mapped`if (${this.dialect.condition(arm.passes, at)}) { ${result} = ${arm.node.id}; }`,
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
frame.emitter.close();
|
|
439
|
+
}
|
|
440
|
+
return settled({
|
|
441
|
+
kind: "choice",
|
|
442
|
+
options: union(
|
|
443
|
+
arms.filter((arm) => !isFalse(arm.passes)).map((arm) => arm.node),
|
|
444
|
+
),
|
|
445
|
+
index: result,
|
|
446
|
+
optional: !certain,
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* The first or last element of an array filtered at run time, in the order its `toSorted`
|
|
452
|
+
* gives: the least, or the greatest, of the candidates present. Each candidate is compared with
|
|
453
|
+
* the best before it, and wins only when strictly less (or, for the last, not less), so equal
|
|
454
|
+
* candidates keep their order as a stable sort does.
|
|
455
|
+
*/
|
|
456
|
+
private extreme(
|
|
457
|
+
masked: Masked,
|
|
458
|
+
{ last }: { last: boolean },
|
|
459
|
+
at: ts.Node,
|
|
460
|
+
frame: Frame,
|
|
461
|
+
): Value {
|
|
462
|
+
const order = masked.order;
|
|
463
|
+
if (!order) {
|
|
464
|
+
return this.search(masked.entries, { last }, undefined, at, frame);
|
|
465
|
+
}
|
|
466
|
+
const live = masked.entries
|
|
467
|
+
.filter((entry) => !isFalse(entry.present))
|
|
468
|
+
.map((entry) => ({ ...entry, node: this.object(entry.item, at) }));
|
|
469
|
+
const [only, ...others] = live;
|
|
470
|
+
if (!only) {
|
|
471
|
+
return constant(undefined);
|
|
472
|
+
}
|
|
473
|
+
if (others.length === 0 && isTrue(only.present)) {
|
|
474
|
+
return only.item;
|
|
475
|
+
}
|
|
476
|
+
const best = frame.emitter.temporary();
|
|
477
|
+
frame.emitter.line(`let ${best}: ${referenceLayout} = -1;`);
|
|
478
|
+
live.forEach((entry, position) => {
|
|
479
|
+
const before = live.slice(0, position);
|
|
480
|
+
const guarded = entry.present.kind !== "const";
|
|
481
|
+
if (guarded) {
|
|
482
|
+
frame.emitter.open(
|
|
483
|
+
mapped`if (${this.dialect.condition(entry.present, at)}) {`,
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
const take = `${best} = ${entry.node.id};`;
|
|
487
|
+
const rivals = union(before.map((rival) => rival.node))
|
|
488
|
+
.filter((rival) => rival !== entry.node)
|
|
489
|
+
.map((rival) => {
|
|
490
|
+
const { result, lines } = frame.emitter.detached(
|
|
491
|
+
{ nested: true },
|
|
492
|
+
() =>
|
|
493
|
+
this.wins(
|
|
494
|
+
this.expressions.invoke(
|
|
495
|
+
order,
|
|
496
|
+
[entry.item, { kind: "static", node: rival }],
|
|
497
|
+
at,
|
|
498
|
+
frame,
|
|
499
|
+
),
|
|
500
|
+
{ last },
|
|
501
|
+
at,
|
|
502
|
+
),
|
|
503
|
+
);
|
|
504
|
+
return { rival, wins: result, lines };
|
|
505
|
+
});
|
|
506
|
+
chain(frame, [
|
|
507
|
+
// Until a candidate is surely present, none may have been taken yet.
|
|
508
|
+
...(before.some((rival) => isTrue(rival.present))
|
|
509
|
+
? []
|
|
510
|
+
: [{ test: `${best} < 0`, body: () => frame.emitter.line(take) }]),
|
|
511
|
+
...rivals
|
|
512
|
+
.filter(({ wins, lines }) => !isFalse(wins) || lines.length > 0)
|
|
513
|
+
.map(({ rival, wins, lines }) => ({
|
|
514
|
+
test: `${best} === ${rival.id}`,
|
|
515
|
+
body: () => {
|
|
516
|
+
frame.emitter.append(lines);
|
|
517
|
+
if (isTrue(wins)) {
|
|
518
|
+
frame.emitter.line(take);
|
|
519
|
+
} else if (wins.kind !== "const") {
|
|
520
|
+
frame.emitter.line(
|
|
521
|
+
mapped`if (${this.dialect.condition(wins, at)}) { ${take} }`,
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
},
|
|
525
|
+
})),
|
|
526
|
+
]);
|
|
527
|
+
if (guarded) {
|
|
528
|
+
frame.emitter.close();
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
return settled({
|
|
532
|
+
kind: "choice",
|
|
533
|
+
options: union(live.map((entry) => entry.node)),
|
|
534
|
+
index: best,
|
|
535
|
+
optional: !live.some((entry) => isTrue(entry.present)),
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** Whether a comparator's result puts its first argument ahead of the best so far. */
|
|
540
|
+
private wins(
|
|
541
|
+
compared: Value,
|
|
542
|
+
{ last }: { last: boolean },
|
|
543
|
+
at: ts.Node,
|
|
544
|
+
): Value {
|
|
545
|
+
if (compared.kind === "const" && typeof compared.value === "number") {
|
|
546
|
+
return constant(last ? compared.value >= 0 : compared.value < 0);
|
|
547
|
+
}
|
|
548
|
+
if (compared.kind !== "code") {
|
|
549
|
+
throw new SpecializeError(at, "A comparator returns a number.");
|
|
550
|
+
}
|
|
551
|
+
return {
|
|
552
|
+
kind: "code",
|
|
553
|
+
text: mapped`(${compared.text} ${last ? ">=" : "<"} 0)`,
|
|
554
|
+
declared: this.checker.getBooleanType(),
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* `array[index]` and `array.at(index)` with an index decided at run time, among objects setup
|
|
560
|
+
* built: the object there, `at` counting back when negative. Undefined when setup decides the
|
|
561
|
+
* index, or the elements are values.
|
|
562
|
+
*/
|
|
563
|
+
indexed(
|
|
564
|
+
items: readonly Value[],
|
|
565
|
+
index: Value,
|
|
566
|
+
{ fromEnd }: { fromEnd: boolean },
|
|
567
|
+
at: ts.Node,
|
|
568
|
+
frame: Frame,
|
|
569
|
+
): Value | undefined {
|
|
570
|
+
if (index.kind !== "code" || !objects(items)) {
|
|
571
|
+
return undefined;
|
|
572
|
+
}
|
|
573
|
+
const held = this.dialect.code(
|
|
574
|
+
this.expressions.held(index, frame),
|
|
575
|
+
undefined,
|
|
576
|
+
);
|
|
577
|
+
const result = frame.emitter.temporary();
|
|
578
|
+
frame.emitter.line(`let ${result}: ${referenceLayout} = -1;`);
|
|
579
|
+
const nodes = items.map((item) => this.object(item, at));
|
|
580
|
+
chain(
|
|
581
|
+
frame,
|
|
582
|
+
nodes.map((item, position) => ({
|
|
583
|
+
test: fromEnd
|
|
584
|
+
? mapped`${held} === ${position} || ${held} === ${position - nodes.length}`
|
|
585
|
+
: mapped`${held} === ${position}`,
|
|
586
|
+
body: () => frame.emitter.line(`${result} = ${item.id};`),
|
|
587
|
+
})),
|
|
588
|
+
);
|
|
589
|
+
return settled({
|
|
590
|
+
kind: "choice",
|
|
591
|
+
options: union(nodes),
|
|
592
|
+
index: result,
|
|
593
|
+
optional: true,
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/** The object an array element is, for a reference to it; values have no identity to refer to. */
|
|
598
|
+
private object(item: Value, at: ts.Node): ModelNode {
|
|
599
|
+
if (item.kind !== "static" || !referable(item.node)) {
|
|
600
|
+
throw new SpecializeError(
|
|
601
|
+
at,
|
|
602
|
+
"Only objects setup built can be picked at run time: an array of values filtered or sorted then has no element to hand back. Use findIndex, or loop over the array.",
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
return item.node;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
@@ -2,7 +2,7 @@ import type ts from "typescript";
|
|
|
2
2
|
import { from, type Mapped, mapped, type Part } from "../positions";
|
|
3
3
|
import { unusedName } from "./layouts";
|
|
4
4
|
import type { ModelNode } from "./model";
|
|
5
|
-
import type { Env } from "./values";
|
|
5
|
+
import type { Env, Value } from "./values";
|
|
6
6
|
|
|
7
7
|
/** Indented lines of one generated routine, with the local names it has used. */
|
|
8
8
|
export class Emitter {
|
|
@@ -41,6 +41,28 @@ export class Emitter {
|
|
|
41
41
|
temporary(): string {
|
|
42
42
|
return this.local(`tsplcTemp${++this.temporaries}`);
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Runs `write` and takes the lines it wrote instead of keeping them, for a caller that places
|
|
46
|
+
* them once it knows what surrounds them. `nested` indents them for a block the caller opens.
|
|
47
|
+
*/
|
|
48
|
+
detached<T>(
|
|
49
|
+
{ nested }: { nested: boolean },
|
|
50
|
+
write: () => T,
|
|
51
|
+
): { result: T; lines: Mapped[] } {
|
|
52
|
+
const start = this.lines.length;
|
|
53
|
+
const depth = this.depth;
|
|
54
|
+
this.depth += nested ? 1 : 0;
|
|
55
|
+
try {
|
|
56
|
+
const result = write();
|
|
57
|
+
return { result, lines: this.lines.splice(start) };
|
|
58
|
+
} finally {
|
|
59
|
+
this.depth = depth;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Places lines `detached` took. */
|
|
63
|
+
append(lines: readonly Mapped[]): void {
|
|
64
|
+
this.lines.push(...lines);
|
|
65
|
+
}
|
|
44
66
|
}
|
|
45
67
|
|
|
46
68
|
export interface Frame {
|
|
@@ -50,4 +72,25 @@ export interface Frame {
|
|
|
50
72
|
/** The routine's declared return type, for literal and optional results. */
|
|
51
73
|
returns?: ts.Type;
|
|
52
74
|
async: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* In a generator run inline for a for…of: what each yield runs, the loop's body over the value,
|
|
77
|
+
* and the labelled block a return leaves.
|
|
78
|
+
*/
|
|
79
|
+
generator?: { yields: (value: Value) => void; exit: string };
|
|
80
|
+
/** An array result's layout, at the length the routine's return statements give it. */
|
|
81
|
+
resultLayout?: string;
|
|
82
|
+
resultLength?: number;
|
|
83
|
+
/** Whether a return statement returned a stored record or array, which a caller only gets a copy of. */
|
|
84
|
+
returnsStored?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* In a routine whose result is a reference: the objects its return statements return, and
|
|
87
|
+
* whether one returns undefined.
|
|
88
|
+
*/
|
|
89
|
+
referents?: Referents;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** What a reference a routine returns can be. */
|
|
93
|
+
export interface Referents {
|
|
94
|
+
options: Set<ModelNode>;
|
|
95
|
+
optional: boolean;
|
|
53
96
|
}
|