@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6563 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16198 -0
- package/dist/index.js.map +62 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1090 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1344 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2177 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +612 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +659 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { joined, type Mapped, mapped, type Part } from "../positions";
|
|
3
|
+
import { callbackArgument, ordered } from "./array-searches";
|
|
4
|
+
import { providedByRuntime } from "./declarations";
|
|
5
|
+
import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
|
|
6
|
+
import type { Frame } from "./frame";
|
|
7
|
+
import type { ModelNode } from "./model";
|
|
8
|
+
import {
|
|
9
|
+
absent,
|
|
10
|
+
noReference,
|
|
11
|
+
referable,
|
|
12
|
+
referenceLayout,
|
|
13
|
+
settled,
|
|
14
|
+
union,
|
|
15
|
+
} from "./references";
|
|
16
|
+
import { constant, repeatable, SpecializeError, type Value } from "./values";
|
|
17
|
+
|
|
18
|
+
type Masked = Extract<Value, { kind: "masked" }>;
|
|
19
|
+
type Entry = Masked["entries"][number];
|
|
20
|
+
|
|
21
|
+
const isTrue = (value: Value): boolean =>
|
|
22
|
+
value.kind === "const" && Boolean(value.value);
|
|
23
|
+
|
|
24
|
+
const isFalse = (value: Value): boolean =>
|
|
25
|
+
value.kind === "const" && !value.value;
|
|
26
|
+
|
|
27
|
+
const whole = (items: readonly Value[]): Entry[] =>
|
|
28
|
+
items.map((item) => ({ item, present: constant(true) }));
|
|
29
|
+
|
|
30
|
+
const objects = (items: readonly Value[]): boolean =>
|
|
31
|
+
items.every((item) => item.kind === "static" && referable(item.node));
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Arrays of known values whose contents or order scan code decides. A filter with a test decided
|
|
35
|
+
* at run time keeps every candidate with a flag for whether the array holds it, and `toSorted`
|
|
36
|
+
* keeps its comparator; neither builds an array. What reads them counts the flags, or picks the
|
|
37
|
+
* object a search or the sort's order puts first, as a reference.
|
|
38
|
+
*/
|
|
39
|
+
export class FilteredArrays {
|
|
40
|
+
constructor(
|
|
41
|
+
private readonly context: SpecializeContext,
|
|
42
|
+
private readonly expressions: Pick<
|
|
43
|
+
ExpressionSpecializer,
|
|
44
|
+
"expression" | "invoke" | "held" | "items" | "references"
|
|
45
|
+
>,
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* An array method decided at run time: on a filtered array, or one over an array setup built
|
|
50
|
+
* that filters, sorts or picks an object from it. Undefined for a call setup decides.
|
|
51
|
+
*/
|
|
52
|
+
call(
|
|
53
|
+
name: string,
|
|
54
|
+
receiver: Value,
|
|
55
|
+
items: readonly Value[] | undefined,
|
|
56
|
+
node: ts.CallExpression,
|
|
57
|
+
frame: Frame,
|
|
58
|
+
): Value | undefined {
|
|
59
|
+
if (receiver.kind === "masked") {
|
|
60
|
+
return this.method(receiver, name, node, frame);
|
|
61
|
+
}
|
|
62
|
+
if (!items) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
switch (name) {
|
|
66
|
+
case "toSorted":
|
|
67
|
+
return {
|
|
68
|
+
kind: "masked",
|
|
69
|
+
entries: whole(items),
|
|
70
|
+
order: callbackArgument(node, frame, this.expressions),
|
|
71
|
+
};
|
|
72
|
+
case "filter": {
|
|
73
|
+
const { entries } = this.filtered(
|
|
74
|
+
{ kind: "masked", entries: whole(items) },
|
|
75
|
+
this.test(node, frame),
|
|
76
|
+
node,
|
|
77
|
+
frame,
|
|
78
|
+
);
|
|
79
|
+
// A filter whose every test setup decides is the elements it keeps.
|
|
80
|
+
return entries.every(({ present }) => present.kind === "const")
|
|
81
|
+
? {
|
|
82
|
+
kind: "virtual",
|
|
83
|
+
items: entries
|
|
84
|
+
.filter(({ present }) => isTrue(present))
|
|
85
|
+
.map(({ item }) => item),
|
|
86
|
+
}
|
|
87
|
+
: { kind: "masked", entries };
|
|
88
|
+
}
|
|
89
|
+
case "find":
|
|
90
|
+
case "findLast":
|
|
91
|
+
return objects(items)
|
|
92
|
+
? this.search(
|
|
93
|
+
whole(items),
|
|
94
|
+
{ last: name === "findLast" },
|
|
95
|
+
this.test(node, frame),
|
|
96
|
+
node,
|
|
97
|
+
frame,
|
|
98
|
+
)
|
|
99
|
+
: undefined;
|
|
100
|
+
case "at": {
|
|
101
|
+
const [argument] = node.arguments;
|
|
102
|
+
return argument && objects(items)
|
|
103
|
+
? this.indexed(
|
|
104
|
+
items,
|
|
105
|
+
this.expressions.expression(argument, frame),
|
|
106
|
+
{ fromEnd: true },
|
|
107
|
+
node,
|
|
108
|
+
frame,
|
|
109
|
+
)
|
|
110
|
+
: undefined;
|
|
111
|
+
}
|
|
112
|
+
default:
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private method(
|
|
118
|
+
masked: Masked,
|
|
119
|
+
name: string,
|
|
120
|
+
node: ts.CallExpression,
|
|
121
|
+
frame: Frame,
|
|
122
|
+
): Value {
|
|
123
|
+
switch (name) {
|
|
124
|
+
case "filter":
|
|
125
|
+
return this.filtered(masked, this.test(node, frame), node, frame);
|
|
126
|
+
case "some":
|
|
127
|
+
case "every":
|
|
128
|
+
return this.quantified(
|
|
129
|
+
masked,
|
|
130
|
+
{ some: name === "some" },
|
|
131
|
+
this.test(node, frame),
|
|
132
|
+
node,
|
|
133
|
+
frame,
|
|
134
|
+
);
|
|
135
|
+
case "find":
|
|
136
|
+
case "findLast": {
|
|
137
|
+
const last = name === "findLast";
|
|
138
|
+
const test = this.test(node, frame);
|
|
139
|
+
// In a sorted array the first to pass is the least of those that pass.
|
|
140
|
+
return masked.order
|
|
141
|
+
? this.extreme(
|
|
142
|
+
this.filtered(masked, test, node, frame),
|
|
143
|
+
{ last },
|
|
144
|
+
node,
|
|
145
|
+
frame,
|
|
146
|
+
)
|
|
147
|
+
: this.search(masked.entries, { last }, test, node, frame);
|
|
148
|
+
}
|
|
149
|
+
case "at": {
|
|
150
|
+
const [argument] = node.arguments;
|
|
151
|
+
const index = argument && this.expressions.expression(argument, frame);
|
|
152
|
+
return this.end(masked, index, node, frame);
|
|
153
|
+
}
|
|
154
|
+
case "toSorted":
|
|
155
|
+
if (masked.order) {
|
|
156
|
+
throw new SpecializeError(
|
|
157
|
+
node,
|
|
158
|
+
"Sort an array once, with one comparator: a second toSorted would leave ties in the first one's order, which isn't kept.",
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
...masked,
|
|
163
|
+
order: callbackArgument(node, frame, this.expressions),
|
|
164
|
+
};
|
|
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.context.dialect.condition(entry.present, at)}) { ${total} = ${total} + 1; }`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return this.number(total);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Whether the expression is JavaScript's `array.filter(test)`, whose length counts the elements that pass. */
|
|
199
|
+
isFilter(
|
|
200
|
+
node: ts.Expression,
|
|
201
|
+
): node is ts.CallExpression & { expression: ts.PropertyAccessExpression } {
|
|
202
|
+
return (
|
|
203
|
+
ts.isCallExpression(node) &&
|
|
204
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
205
|
+
node.expression.name.text === "filter" &&
|
|
206
|
+
node.arguments.length === 1 &&
|
|
207
|
+
providedByRuntime(this.context.checker, node.expression.name)
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* `array.filter(test).length`: how many elements pass. A filter decided at run time has no array
|
|
213
|
+
* to build, since its length isn't fixed, but its count is a number.
|
|
214
|
+
*/
|
|
215
|
+
count(
|
|
216
|
+
node: ts.CallExpression & { expression: ts.PropertyAccessExpression },
|
|
217
|
+
frame: Frame,
|
|
218
|
+
): Value {
|
|
219
|
+
const receiver = this.expressions.expression(
|
|
220
|
+
node.expression.expression,
|
|
221
|
+
frame,
|
|
222
|
+
);
|
|
223
|
+
if (receiver.kind === "masked") {
|
|
224
|
+
return this.member(
|
|
225
|
+
this.filtered(receiver, this.test(node, frame), node, frame),
|
|
226
|
+
"length",
|
|
227
|
+
node,
|
|
228
|
+
frame,
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
const items = this.expressions.items(receiver);
|
|
232
|
+
const [testNode] = node.arguments;
|
|
233
|
+
if (!items || !testNode) {
|
|
234
|
+
throw new SpecializeError(
|
|
235
|
+
node,
|
|
236
|
+
"filter needs an array setup built and a test.",
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
const test = this.test(node, frame);
|
|
240
|
+
const results = items.map((item, index) => test(item, index));
|
|
241
|
+
if (results.every((result) => result.kind === "const")) {
|
|
242
|
+
return constant(results.filter(isTrue).length);
|
|
243
|
+
}
|
|
244
|
+
const total = frame.emitter.temporary();
|
|
245
|
+
frame.emitter.line(`let ${total}: number = 0;`);
|
|
246
|
+
for (const result of results) {
|
|
247
|
+
if (result.kind === "const") {
|
|
248
|
+
if (result.value) {
|
|
249
|
+
frame.emitter.line(`${total} = ${total} + 1;`);
|
|
250
|
+
}
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
frame.emitter.line(
|
|
254
|
+
mapped`if (${this.context.dialect.condition(result, testNode)}) { ${total} = ${total} + 1; }`,
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
return this.number(total);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** `array[0]` of an array filtered or sorted at run time: its first element. */
|
|
261
|
+
element(masked: Masked, index: Value, at: ts.Node, frame: Frame): Value {
|
|
262
|
+
if (index.kind === "const" && index.value === 0) {
|
|
263
|
+
return this.extreme(masked, { last: false }, at, frame);
|
|
264
|
+
}
|
|
265
|
+
throw new SpecializeError(
|
|
266
|
+
at,
|
|
267
|
+
"An array filtered or sorted at run time has no storage to index: read [0], at(0) or at(-1).",
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** `at(0)` and `at(-1)` of an array filtered or sorted at run time. */
|
|
272
|
+
private end(
|
|
273
|
+
masked: Masked,
|
|
274
|
+
index: Value | undefined,
|
|
275
|
+
at: ts.Node,
|
|
276
|
+
frame: Frame,
|
|
277
|
+
): Value {
|
|
278
|
+
if (index?.kind === "const" && (index.value === 0 || index.value === -1)) {
|
|
279
|
+
return this.extreme(masked, { last: index.value === -1 }, at, frame);
|
|
280
|
+
}
|
|
281
|
+
throw new SpecializeError(
|
|
282
|
+
at,
|
|
283
|
+
"An array filtered or sorted at run time has no storage to index: read at(0) or at(-1).",
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** An array method's callback as a test of one element. */
|
|
288
|
+
private test(
|
|
289
|
+
node: ts.CallExpression,
|
|
290
|
+
frame: Frame,
|
|
291
|
+
): (item: Value, index: number) => Value {
|
|
292
|
+
const callback = callbackArgument(node, frame, this.expressions);
|
|
293
|
+
return (item, index) =>
|
|
294
|
+
this.truth(
|
|
295
|
+
this.expressions.invoke(callback, [item, constant(index)], node, frame),
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** A callback's result as a condition: an object is truthy, and a reference is when it holds one. */
|
|
300
|
+
private truth(value: Value): Value {
|
|
301
|
+
if (value.kind === "choice") {
|
|
302
|
+
return this.expressions.references.present(value);
|
|
303
|
+
}
|
|
304
|
+
return value.kind === "const" || value.kind === "code"
|
|
305
|
+
? value
|
|
306
|
+
: constant(true);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private flag(text: string | Mapped): Value {
|
|
310
|
+
return {
|
|
311
|
+
kind: "code",
|
|
312
|
+
text,
|
|
313
|
+
declared: this.context.checker.getBooleanType(),
|
|
314
|
+
place: true,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private number(text: string): Value {
|
|
319
|
+
return {
|
|
320
|
+
kind: "code",
|
|
321
|
+
text,
|
|
322
|
+
declared: this.context.checker.getNumberType(),
|
|
323
|
+
place: true,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* `filter(test)` decided at run time: the same candidates, each present when it was and its
|
|
329
|
+
* test passes. A test runs only on a candidate the array holds.
|
|
330
|
+
*/
|
|
331
|
+
private filtered(
|
|
332
|
+
masked: Masked,
|
|
333
|
+
test: (item: Value, index: number) => Value,
|
|
334
|
+
at: ts.Node,
|
|
335
|
+
frame: Frame,
|
|
336
|
+
): Masked {
|
|
337
|
+
const tested = masked.entries.map((entry, index) => {
|
|
338
|
+
if (isFalse(entry.present)) {
|
|
339
|
+
return { entry, passes: constant(false), lines: [] };
|
|
340
|
+
}
|
|
341
|
+
const { result, lines } = frame.emitter.detached(
|
|
342
|
+
{ nested: entry.present.kind !== "const" },
|
|
343
|
+
() => test(entry.item, index),
|
|
344
|
+
);
|
|
345
|
+
return { entry, passes: result, lines };
|
|
346
|
+
});
|
|
347
|
+
const entries = tested.map(({ entry, passes, lines }): Entry => {
|
|
348
|
+
const { item, present } = entry;
|
|
349
|
+
if (present.kind === "const") {
|
|
350
|
+
frame.emitter.append(lines);
|
|
351
|
+
if (passes.kind === "const") {
|
|
352
|
+
return { item, present: constant(isTrue(passes)) };
|
|
353
|
+
}
|
|
354
|
+
const text = this.context.dialect.condition(passes, at);
|
|
355
|
+
if (repeatable(text)) {
|
|
356
|
+
return { item, present: passes };
|
|
357
|
+
}
|
|
358
|
+
const held = frame.emitter.temporary();
|
|
359
|
+
frame.emitter.line(mapped`const ${held}: boolean = ${text};`);
|
|
360
|
+
return { item, present: this.flag(held) };
|
|
361
|
+
}
|
|
362
|
+
if (passes.kind === "const" && lines.length === 0) {
|
|
363
|
+
return { item, present: passes.value ? present : constant(false) };
|
|
364
|
+
}
|
|
365
|
+
const held = frame.emitter.temporary();
|
|
366
|
+
frame.emitter.line(`let ${held}: boolean = false;`);
|
|
367
|
+
frame.emitter.open(
|
|
368
|
+
mapped`if (${this.context.dialect.condition(present, at)}) {`,
|
|
369
|
+
);
|
|
370
|
+
frame.emitter.append(lines);
|
|
371
|
+
frame.emitter.line(
|
|
372
|
+
mapped`${held} = ${this.context.dialect.condition(passes, at)};`,
|
|
373
|
+
);
|
|
374
|
+
frame.emitter.close();
|
|
375
|
+
return { item, present: this.flag(held) };
|
|
376
|
+
});
|
|
377
|
+
return { ...masked, entries };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** `some(test)` and `every(test)` over the candidates an array filtered at run time holds; tests stop once one decides. */
|
|
381
|
+
private quantified(
|
|
382
|
+
masked: Masked,
|
|
383
|
+
{ some }: { some: boolean },
|
|
384
|
+
test: (item: Value, index: number) => Value,
|
|
385
|
+
at: ts.Node,
|
|
386
|
+
frame: Frame,
|
|
387
|
+
): Value {
|
|
388
|
+
const result = frame.emitter.temporary();
|
|
389
|
+
frame.emitter.line(`let ${result}: boolean = ${!some};`);
|
|
390
|
+
for (const [index, entry] of masked.entries.entries()) {
|
|
391
|
+
if (isFalse(entry.present)) {
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
const guards: Part[] = [
|
|
395
|
+
some ? `!${result}` : result,
|
|
396
|
+
...(entry.present.kind === "const"
|
|
397
|
+
? []
|
|
398
|
+
: [this.context.dialect.condition(entry.present, at)]),
|
|
399
|
+
];
|
|
400
|
+
frame.emitter.open(mapped`if (${joined(guards, " && ")}) {`);
|
|
401
|
+
const passes = test(entry.item, index);
|
|
402
|
+
if (passes.kind !== "const") {
|
|
403
|
+
frame.emitter.line(
|
|
404
|
+
mapped`${result} = ${this.context.dialect.condition(passes, at)};`,
|
|
405
|
+
);
|
|
406
|
+
} else if (Boolean(passes.value) === some) {
|
|
407
|
+
frame.emitter.line(`${result} = ${some};`);
|
|
408
|
+
}
|
|
409
|
+
frame.emitter.close();
|
|
410
|
+
}
|
|
411
|
+
return this.flag(result);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* `find` and `findLast` among objects setup built, and the first or last an unsorted filtered
|
|
416
|
+
* array holds: the first candidate present whose test passes, tests stopping once one has.
|
|
417
|
+
*/
|
|
418
|
+
private search(
|
|
419
|
+
entries: readonly Entry[],
|
|
420
|
+
{ last }: { last: boolean },
|
|
421
|
+
test: ((item: Value, index: number) => Value) | undefined,
|
|
422
|
+
at: ts.Node,
|
|
423
|
+
frame: Frame,
|
|
424
|
+
): Value {
|
|
425
|
+
const arms: {
|
|
426
|
+
node: ModelNode;
|
|
427
|
+
present: Value;
|
|
428
|
+
passes: Value;
|
|
429
|
+
lines: Mapped[];
|
|
430
|
+
}[] = [];
|
|
431
|
+
for (const [index, entry] of ordered(entries, { last })) {
|
|
432
|
+
if (isFalse(entry.present)) {
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const node = this.object(entry.item, at);
|
|
436
|
+
const { result: passes, lines } = test
|
|
437
|
+
? frame.emitter.detached({ nested: true }, () =>
|
|
438
|
+
test(entry.item, index),
|
|
439
|
+
)
|
|
440
|
+
: { result: constant(true), lines: [] };
|
|
441
|
+
if (isFalse(passes) && lines.length === 0) {
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
arms.push({ node, present: entry.present, passes, lines });
|
|
445
|
+
if (isTrue(entry.present) && isTrue(passes)) {
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
const final = arms.at(-1);
|
|
450
|
+
if (!final) {
|
|
451
|
+
return constant(undefined);
|
|
452
|
+
}
|
|
453
|
+
const certain = isTrue(final.present) && isTrue(final.passes);
|
|
454
|
+
if (arms.length === 1 && certain && final.lines.length === 0) {
|
|
455
|
+
return { kind: "static", node: final.node };
|
|
456
|
+
}
|
|
457
|
+
const result = frame.emitter.temporary();
|
|
458
|
+
frame.emitter.line(`let ${result}: ${referenceLayout} = ${noReference};`);
|
|
459
|
+
for (const arm of arms) {
|
|
460
|
+
const guards: Part[] = [
|
|
461
|
+
absent(result),
|
|
462
|
+
...(arm.present.kind === "const"
|
|
463
|
+
? []
|
|
464
|
+
: [this.context.dialect.condition(arm.present, at)]),
|
|
465
|
+
];
|
|
466
|
+
frame.emitter.open(mapped`if (${joined(guards, " && ")}) {`);
|
|
467
|
+
frame.emitter.append(arm.lines);
|
|
468
|
+
if (isTrue(arm.passes)) {
|
|
469
|
+
frame.emitter.line(`${result} = ${arm.node.reference};`);
|
|
470
|
+
} else if (arm.passes.kind !== "const") {
|
|
471
|
+
frame.emitter.line(
|
|
472
|
+
mapped`if (${this.context.dialect.condition(arm.passes, at)}) { ${result} = ${arm.node.reference}; }`,
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
frame.emitter.close();
|
|
476
|
+
}
|
|
477
|
+
return settled({
|
|
478
|
+
kind: "choice",
|
|
479
|
+
options: union(
|
|
480
|
+
arms.filter((arm) => !isFalse(arm.passes)).map((arm) => arm.node),
|
|
481
|
+
),
|
|
482
|
+
index: result,
|
|
483
|
+
optional: !certain,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* The first or last element of an array filtered at run time, in the order its `toSorted`
|
|
489
|
+
* gives: the least, or the greatest, of the candidates present. Each candidate is compared with
|
|
490
|
+
* the best before it, and wins only when strictly less (or, for the last, not less), so equal
|
|
491
|
+
* candidates keep their order as a stable sort does.
|
|
492
|
+
*/
|
|
493
|
+
private extreme(
|
|
494
|
+
masked: Masked,
|
|
495
|
+
{ last }: { last: boolean },
|
|
496
|
+
at: ts.Node,
|
|
497
|
+
frame: Frame,
|
|
498
|
+
): Value {
|
|
499
|
+
const order = masked.order;
|
|
500
|
+
if (!order) {
|
|
501
|
+
return this.search(masked.entries, { last }, undefined, at, frame);
|
|
502
|
+
}
|
|
503
|
+
const live = masked.entries
|
|
504
|
+
.filter((entry) => !isFalse(entry.present))
|
|
505
|
+
.map((entry) => ({ ...entry, node: this.object(entry.item, at) }));
|
|
506
|
+
const [only, ...others] = live;
|
|
507
|
+
if (!only) {
|
|
508
|
+
return constant(undefined);
|
|
509
|
+
}
|
|
510
|
+
if (others.length === 0 && isTrue(only.present)) {
|
|
511
|
+
return only.item;
|
|
512
|
+
}
|
|
513
|
+
const best = frame.emitter.temporary();
|
|
514
|
+
frame.emitter.line(`let ${best}: ${referenceLayout} = ${noReference};`);
|
|
515
|
+
live.forEach((entry, position) => {
|
|
516
|
+
const before = live.slice(0, position);
|
|
517
|
+
const guarded = entry.present.kind !== "const";
|
|
518
|
+
if (guarded) {
|
|
519
|
+
frame.emitter.open(
|
|
520
|
+
mapped`if (${this.context.dialect.condition(entry.present, at)}) {`,
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
const take = `${best} = ${entry.node.reference};`;
|
|
524
|
+
const rivals = union(before.map((rival) => rival.node))
|
|
525
|
+
.filter((rival) => rival !== entry.node)
|
|
526
|
+
.map((rival) => {
|
|
527
|
+
const { result, lines } = frame.emitter.detached(
|
|
528
|
+
{ nested: true },
|
|
529
|
+
() =>
|
|
530
|
+
this.wins(
|
|
531
|
+
this.expressions.invoke(
|
|
532
|
+
order,
|
|
533
|
+
[entry.item, { kind: "static", node: rival }],
|
|
534
|
+
at,
|
|
535
|
+
frame,
|
|
536
|
+
),
|
|
537
|
+
{ last },
|
|
538
|
+
at,
|
|
539
|
+
),
|
|
540
|
+
);
|
|
541
|
+
return { rival, wins: result, lines };
|
|
542
|
+
});
|
|
543
|
+
frame.emitter.chain([
|
|
544
|
+
// Until a candidate is surely present, none may have been taken yet.
|
|
545
|
+
...(before.some((rival) => isTrue(rival.present))
|
|
546
|
+
? []
|
|
547
|
+
: [{ test: absent(best), body: () => frame.emitter.line(take) }]),
|
|
548
|
+
...rivals
|
|
549
|
+
.filter(({ wins, lines }) => !isFalse(wins) || lines.length > 0)
|
|
550
|
+
.map(({ rival, wins, lines }) => ({
|
|
551
|
+
test: `${best} === ${rival.reference}`,
|
|
552
|
+
body: () => {
|
|
553
|
+
frame.emitter.append(lines);
|
|
554
|
+
if (isTrue(wins)) {
|
|
555
|
+
frame.emitter.line(take);
|
|
556
|
+
} else if (wins.kind !== "const") {
|
|
557
|
+
frame.emitter.line(
|
|
558
|
+
mapped`if (${this.context.dialect.condition(wins, at)}) { ${take} }`,
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
},
|
|
562
|
+
})),
|
|
563
|
+
]);
|
|
564
|
+
if (guarded) {
|
|
565
|
+
frame.emitter.close();
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
return settled({
|
|
569
|
+
kind: "choice",
|
|
570
|
+
options: union(live.map((entry) => entry.node)),
|
|
571
|
+
index: best,
|
|
572
|
+
optional: !live.some((entry) => isTrue(entry.present)),
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** Whether a comparator's result puts its first argument ahead of the best so far. */
|
|
577
|
+
private wins(
|
|
578
|
+
compared: Value,
|
|
579
|
+
{ last }: { last: boolean },
|
|
580
|
+
at: ts.Node,
|
|
581
|
+
): Value {
|
|
582
|
+
if (compared.kind === "const" && typeof compared.value === "number") {
|
|
583
|
+
return constant(last ? compared.value >= 0 : compared.value < 0);
|
|
584
|
+
}
|
|
585
|
+
if (compared.kind !== "code") {
|
|
586
|
+
throw new SpecializeError(at, "A comparator returns a number.");
|
|
587
|
+
}
|
|
588
|
+
return {
|
|
589
|
+
kind: "code",
|
|
590
|
+
text: mapped`(${compared.text} ${last ? ">=" : "<"} 0)`,
|
|
591
|
+
declared: this.context.checker.getBooleanType(),
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* `array[index]` and `array.at(index)` with an index decided at run time, among objects setup
|
|
597
|
+
* built: the object there, `at` counting back when negative. Undefined when setup decides the
|
|
598
|
+
* index, or the elements are values.
|
|
599
|
+
*/
|
|
600
|
+
indexed(
|
|
601
|
+
items: readonly Value[],
|
|
602
|
+
index: Value,
|
|
603
|
+
{ fromEnd }: { fromEnd: boolean },
|
|
604
|
+
at: ts.Node,
|
|
605
|
+
frame: Frame,
|
|
606
|
+
): Value | undefined {
|
|
607
|
+
if (index.kind !== "code" || !objects(items)) {
|
|
608
|
+
return undefined;
|
|
609
|
+
}
|
|
610
|
+
const held = this.context.dialect.code(
|
|
611
|
+
this.expressions.held(index, frame),
|
|
612
|
+
undefined,
|
|
613
|
+
);
|
|
614
|
+
const result = frame.emitter.temporary();
|
|
615
|
+
frame.emitter.line(`let ${result}: ${referenceLayout} = ${noReference};`);
|
|
616
|
+
const nodes = items.map((item) => this.object(item, at));
|
|
617
|
+
frame.emitter.chain(
|
|
618
|
+
nodes.map((item, position) => ({
|
|
619
|
+
test: fromEnd
|
|
620
|
+
? mapped`${held} === ${position} || ${held} === ${position - nodes.length}`
|
|
621
|
+
: mapped`${held} === ${position}`,
|
|
622
|
+
body: () => frame.emitter.line(`${result} = ${item.reference};`),
|
|
623
|
+
})),
|
|
624
|
+
);
|
|
625
|
+
return settled({
|
|
626
|
+
kind: "choice",
|
|
627
|
+
options: union(nodes),
|
|
628
|
+
index: result,
|
|
629
|
+
optional: true,
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/** The object an array element is, for a reference to it; values have no identity to refer to. */
|
|
634
|
+
private object(item: Value, at: ts.Node): ModelNode {
|
|
635
|
+
if (item.kind !== "static" || !referable(item.node)) {
|
|
636
|
+
throw new SpecializeError(
|
|
637
|
+
at,
|
|
638
|
+
"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.",
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
return item.node;
|
|
642
|
+
}
|
|
643
|
+
}
|