@tsplc/compiler 0.5.13 → 0.5.15
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 +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- 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 +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import ts from "typescript";
|
|
2
2
|
import { joined, type Mapped, mapped, type Part } from "../positions";
|
|
3
|
-
import
|
|
3
|
+
import { callbackArgument, ordered } from "./array-searches";
|
|
4
|
+
import { providedByRuntime } from "./declarations";
|
|
4
5
|
import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
|
|
5
6
|
import type { Frame } from "./frame";
|
|
6
7
|
import type { ModelNode } from "./model";
|
|
7
8
|
import {
|
|
8
|
-
|
|
9
|
+
absent,
|
|
10
|
+
noReference,
|
|
9
11
|
referable,
|
|
10
12
|
referenceLayout,
|
|
11
13
|
settled,
|
|
@@ -22,15 +24,6 @@ const isTrue = (value: Value): boolean =>
|
|
|
22
24
|
const isFalse = (value: Value): boolean =>
|
|
23
25
|
value.kind === "const" && !value.value;
|
|
24
26
|
|
|
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
27
|
const whole = (items: readonly Value[]): Entry[] =>
|
|
35
28
|
items.map((item) => ({ item, present: constant(true) }));
|
|
36
29
|
|
|
@@ -44,16 +37,13 @@ const objects = (items: readonly Value[]): boolean =>
|
|
|
44
37
|
* object a search or the sort's order puts first, as a reference.
|
|
45
38
|
*/
|
|
46
39
|
export class FilteredArrays {
|
|
47
|
-
private readonly checker: ts.TypeChecker;
|
|
48
|
-
private readonly dialect: DialectText;
|
|
49
|
-
|
|
50
40
|
constructor(
|
|
51
|
-
context: SpecializeContext,
|
|
52
|
-
private readonly expressions:
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
41
|
+
private readonly context: SpecializeContext,
|
|
42
|
+
private readonly expressions: Pick<
|
|
43
|
+
ExpressionSpecializer,
|
|
44
|
+
"expression" | "invoke" | "held" | "items" | "references"
|
|
45
|
+
>,
|
|
46
|
+
) {}
|
|
57
47
|
|
|
58
48
|
/**
|
|
59
49
|
* An array method decided at run time: on a filtered array, or one over an array setup built
|
|
@@ -77,16 +67,25 @@ export class FilteredArrays {
|
|
|
77
67
|
return {
|
|
78
68
|
kind: "masked",
|
|
79
69
|
entries: whole(items),
|
|
80
|
-
order:
|
|
70
|
+
order: callbackArgument(node, frame, this.expressions),
|
|
81
71
|
};
|
|
82
|
-
case "filter":
|
|
83
|
-
|
|
72
|
+
case "filter": {
|
|
73
|
+
const { entries } = this.filtered(
|
|
84
74
|
{ kind: "masked", entries: whole(items) },
|
|
85
75
|
this.test(node, frame),
|
|
86
76
|
node,
|
|
87
77
|
frame,
|
|
88
|
-
{ decided: "defer" },
|
|
89
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
|
+
}
|
|
90
89
|
case "find":
|
|
91
90
|
case "findLast":
|
|
92
91
|
return objects(items)
|
|
@@ -123,9 +122,7 @@ export class FilteredArrays {
|
|
|
123
122
|
): Value {
|
|
124
123
|
switch (name) {
|
|
125
124
|
case "filter":
|
|
126
|
-
return this.filtered(masked, this.test(node, frame), node, frame
|
|
127
|
-
decided: "keep",
|
|
128
|
-
});
|
|
125
|
+
return this.filtered(masked, this.test(node, frame), node, frame);
|
|
129
126
|
case "some":
|
|
130
127
|
case "every":
|
|
131
128
|
return this.quantified(
|
|
@@ -142,7 +139,7 @@ export class FilteredArrays {
|
|
|
142
139
|
// In a sorted array the first to pass is the least of those that pass.
|
|
143
140
|
return masked.order
|
|
144
141
|
? this.extreme(
|
|
145
|
-
this.filtered(masked, test, node, frame
|
|
142
|
+
this.filtered(masked, test, node, frame),
|
|
146
143
|
{ last },
|
|
147
144
|
node,
|
|
148
145
|
frame,
|
|
@@ -161,7 +158,10 @@ export class FilteredArrays {
|
|
|
161
158
|
"Sort an array once, with one comparator: a second toSorted would leave ties in the first one's order, which isn't kept.",
|
|
162
159
|
);
|
|
163
160
|
}
|
|
164
|
-
return {
|
|
161
|
+
return {
|
|
162
|
+
...masked,
|
|
163
|
+
order: callbackArgument(node, frame, this.expressions),
|
|
164
|
+
};
|
|
165
165
|
default:
|
|
166
166
|
throw new SpecializeError(
|
|
167
167
|
node,
|
|
@@ -188,16 +188,73 @@ export class FilteredArrays {
|
|
|
188
188
|
for (const entry of live) {
|
|
189
189
|
if (entry.present.kind !== "const") {
|
|
190
190
|
frame.emitter.line(
|
|
191
|
-
mapped`if (${this.dialect.condition(entry.present, at)}) { ${total} = ${total} + 1; }`,
|
|
191
|
+
mapped`if (${this.context.dialect.condition(entry.present, at)}) { ${total} = ${total} + 1; }`,
|
|
192
192
|
);
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
|
-
return
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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);
|
|
201
258
|
}
|
|
202
259
|
|
|
203
260
|
/** `array[0]` of an array filtered or sorted at run time: its first element. */
|
|
@@ -227,20 +284,12 @@ export class FilteredArrays {
|
|
|
227
284
|
);
|
|
228
285
|
}
|
|
229
286
|
|
|
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
287
|
/** An array method's callback as a test of one element. */
|
|
239
288
|
private test(
|
|
240
289
|
node: ts.CallExpression,
|
|
241
290
|
frame: Frame,
|
|
242
291
|
): (item: Value, index: number) => Value {
|
|
243
|
-
const callback =
|
|
292
|
+
const callback = callbackArgument(node, frame, this.expressions);
|
|
244
293
|
return (item, index) =>
|
|
245
294
|
this.truth(
|
|
246
295
|
this.expressions.invoke(callback, [item, constant(index)], node, frame),
|
|
@@ -261,37 +310,30 @@ export class FilteredArrays {
|
|
|
261
310
|
return {
|
|
262
311
|
kind: "code",
|
|
263
312
|
text,
|
|
264
|
-
declared: this.checker.getBooleanType(),
|
|
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(),
|
|
265
323
|
place: true,
|
|
266
324
|
};
|
|
267
325
|
}
|
|
268
326
|
|
|
269
327
|
/**
|
|
270
328
|
* `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.
|
|
272
|
-
* to unroll when `decided` is "defer".
|
|
329
|
+
* test passes. A test runs only on a candidate the array holds.
|
|
273
330
|
*/
|
|
274
331
|
private filtered(
|
|
275
332
|
masked: Masked,
|
|
276
333
|
test: (item: Value, index: number) => Value,
|
|
277
334
|
at: ts.Node,
|
|
278
335
|
frame: Frame,
|
|
279
|
-
|
|
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 {
|
|
336
|
+
): Masked {
|
|
295
337
|
const tested = masked.entries.map((entry, index) => {
|
|
296
338
|
if (isFalse(entry.present)) {
|
|
297
339
|
return { entry, passes: constant(false), lines: [] };
|
|
@@ -302,12 +344,6 @@ export class FilteredArrays {
|
|
|
302
344
|
);
|
|
303
345
|
return { entry, passes: result, lines };
|
|
304
346
|
});
|
|
305
|
-
if (
|
|
306
|
-
decided === "defer" &&
|
|
307
|
-
tested.every(({ passes }) => passes.kind === "const")
|
|
308
|
-
) {
|
|
309
|
-
return undefined;
|
|
310
|
-
}
|
|
311
347
|
const entries = tested.map(({ entry, passes, lines }): Entry => {
|
|
312
348
|
const { item, present } = entry;
|
|
313
349
|
if (present.kind === "const") {
|
|
@@ -315,7 +351,7 @@ export class FilteredArrays {
|
|
|
315
351
|
if (passes.kind === "const") {
|
|
316
352
|
return { item, present: constant(isTrue(passes)) };
|
|
317
353
|
}
|
|
318
|
-
const text = this.dialect.condition(passes, at);
|
|
354
|
+
const text = this.context.dialect.condition(passes, at);
|
|
319
355
|
if (repeatable(text)) {
|
|
320
356
|
return { item, present: passes };
|
|
321
357
|
}
|
|
@@ -328,10 +364,12 @@ export class FilteredArrays {
|
|
|
328
364
|
}
|
|
329
365
|
const held = frame.emitter.temporary();
|
|
330
366
|
frame.emitter.line(`let ${held}: boolean = false;`);
|
|
331
|
-
frame.emitter.open(
|
|
367
|
+
frame.emitter.open(
|
|
368
|
+
mapped`if (${this.context.dialect.condition(present, at)}) {`,
|
|
369
|
+
);
|
|
332
370
|
frame.emitter.append(lines);
|
|
333
371
|
frame.emitter.line(
|
|
334
|
-
mapped`${held} = ${this.dialect.condition(passes, at)};`,
|
|
372
|
+
mapped`${held} = ${this.context.dialect.condition(passes, at)};`,
|
|
335
373
|
);
|
|
336
374
|
frame.emitter.close();
|
|
337
375
|
return { item, present: this.flag(held) };
|
|
@@ -357,13 +395,13 @@ export class FilteredArrays {
|
|
|
357
395
|
some ? `!${result}` : result,
|
|
358
396
|
...(entry.present.kind === "const"
|
|
359
397
|
? []
|
|
360
|
-
: [this.dialect.condition(entry.present, at)]),
|
|
398
|
+
: [this.context.dialect.condition(entry.present, at)]),
|
|
361
399
|
];
|
|
362
400
|
frame.emitter.open(mapped`if (${joined(guards, " && ")}) {`);
|
|
363
401
|
const passes = test(entry.item, index);
|
|
364
402
|
if (passes.kind !== "const") {
|
|
365
403
|
frame.emitter.line(
|
|
366
|
-
mapped`${result} = ${this.dialect.condition(passes, at)};`,
|
|
404
|
+
mapped`${result} = ${this.context.dialect.condition(passes, at)};`,
|
|
367
405
|
);
|
|
368
406
|
} else if (Boolean(passes.value) === some) {
|
|
369
407
|
frame.emitter.line(`${result} = ${some};`);
|
|
@@ -404,7 +442,6 @@ export class FilteredArrays {
|
|
|
404
442
|
continue;
|
|
405
443
|
}
|
|
406
444
|
arms.push({ node, present: entry.present, passes, lines });
|
|
407
|
-
// Nothing after a candidate that is surely there and surely passes is reached.
|
|
408
445
|
if (isTrue(entry.present) && isTrue(passes)) {
|
|
409
446
|
break;
|
|
410
447
|
}
|
|
@@ -418,13 +455,13 @@ export class FilteredArrays {
|
|
|
418
455
|
return { kind: "static", node: final.node };
|
|
419
456
|
}
|
|
420
457
|
const result = frame.emitter.temporary();
|
|
421
|
-
frame.emitter.line(`let ${result}: ${referenceLayout} =
|
|
458
|
+
frame.emitter.line(`let ${result}: ${referenceLayout} = ${noReference};`);
|
|
422
459
|
for (const arm of arms) {
|
|
423
460
|
const guards: Part[] = [
|
|
424
|
-
|
|
461
|
+
absent(result),
|
|
425
462
|
...(arm.present.kind === "const"
|
|
426
463
|
? []
|
|
427
|
-
: [this.dialect.condition(arm.present, at)]),
|
|
464
|
+
: [this.context.dialect.condition(arm.present, at)]),
|
|
428
465
|
];
|
|
429
466
|
frame.emitter.open(mapped`if (${joined(guards, " && ")}) {`);
|
|
430
467
|
frame.emitter.append(arm.lines);
|
|
@@ -432,7 +469,7 @@ export class FilteredArrays {
|
|
|
432
469
|
frame.emitter.line(`${result} = ${arm.node.reference};`);
|
|
433
470
|
} else if (arm.passes.kind !== "const") {
|
|
434
471
|
frame.emitter.line(
|
|
435
|
-
mapped`if (${this.dialect.condition(arm.passes, at)}) { ${result} = ${arm.node.reference}; }`,
|
|
472
|
+
mapped`if (${this.context.dialect.condition(arm.passes, at)}) { ${result} = ${arm.node.reference}; }`,
|
|
436
473
|
);
|
|
437
474
|
}
|
|
438
475
|
frame.emitter.close();
|
|
@@ -474,13 +511,13 @@ export class FilteredArrays {
|
|
|
474
511
|
return only.item;
|
|
475
512
|
}
|
|
476
513
|
const best = frame.emitter.temporary();
|
|
477
|
-
frame.emitter.line(`let ${best}: ${referenceLayout} =
|
|
514
|
+
frame.emitter.line(`let ${best}: ${referenceLayout} = ${noReference};`);
|
|
478
515
|
live.forEach((entry, position) => {
|
|
479
516
|
const before = live.slice(0, position);
|
|
480
517
|
const guarded = entry.present.kind !== "const";
|
|
481
518
|
if (guarded) {
|
|
482
519
|
frame.emitter.open(
|
|
483
|
-
mapped`if (${this.dialect.condition(entry.present, at)}) {`,
|
|
520
|
+
mapped`if (${this.context.dialect.condition(entry.present, at)}) {`,
|
|
484
521
|
);
|
|
485
522
|
}
|
|
486
523
|
const take = `${best} = ${entry.node.reference};`;
|
|
@@ -503,11 +540,11 @@ export class FilteredArrays {
|
|
|
503
540
|
);
|
|
504
541
|
return { rival, wins: result, lines };
|
|
505
542
|
});
|
|
506
|
-
chain(
|
|
543
|
+
frame.emitter.chain([
|
|
507
544
|
// Until a candidate is surely present, none may have been taken yet.
|
|
508
545
|
...(before.some((rival) => isTrue(rival.present))
|
|
509
546
|
? []
|
|
510
|
-
: [{ test:
|
|
547
|
+
: [{ test: absent(best), body: () => frame.emitter.line(take) }]),
|
|
511
548
|
...rivals
|
|
512
549
|
.filter(({ wins, lines }) => !isFalse(wins) || lines.length > 0)
|
|
513
550
|
.map(({ rival, wins, lines }) => ({
|
|
@@ -518,7 +555,7 @@ export class FilteredArrays {
|
|
|
518
555
|
frame.emitter.line(take);
|
|
519
556
|
} else if (wins.kind !== "const") {
|
|
520
557
|
frame.emitter.line(
|
|
521
|
-
mapped`if (${this.dialect.condition(wins, at)}) { ${take} }`,
|
|
558
|
+
mapped`if (${this.context.dialect.condition(wins, at)}) { ${take} }`,
|
|
522
559
|
);
|
|
523
560
|
}
|
|
524
561
|
},
|
|
@@ -551,7 +588,7 @@ export class FilteredArrays {
|
|
|
551
588
|
return {
|
|
552
589
|
kind: "code",
|
|
553
590
|
text: mapped`(${compared.text} ${last ? ">=" : "<"} 0)`,
|
|
554
|
-
declared: this.checker.getBooleanType(),
|
|
591
|
+
declared: this.context.checker.getBooleanType(),
|
|
555
592
|
};
|
|
556
593
|
}
|
|
557
594
|
|
|
@@ -570,15 +607,14 @@ export class FilteredArrays {
|
|
|
570
607
|
if (index.kind !== "code" || !objects(items)) {
|
|
571
608
|
return undefined;
|
|
572
609
|
}
|
|
573
|
-
const held = this.dialect.code(
|
|
610
|
+
const held = this.context.dialect.code(
|
|
574
611
|
this.expressions.held(index, frame),
|
|
575
612
|
undefined,
|
|
576
613
|
);
|
|
577
614
|
const result = frame.emitter.temporary();
|
|
578
|
-
frame.emitter.line(`let ${result}: ${referenceLayout} =
|
|
615
|
+
frame.emitter.line(`let ${result}: ${referenceLayout} = ${noReference};`);
|
|
579
616
|
const nodes = items.map((item) => this.object(item, at));
|
|
580
|
-
chain(
|
|
581
|
-
frame,
|
|
617
|
+
frame.emitter.chain(
|
|
582
618
|
nodes.map((item, position) => ({
|
|
583
619
|
test: fromEnd
|
|
584
620
|
? mapped`${held} === ${position} || ${held} === ${position - nodes.length}`
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type ts from "typescript";
|
|
2
2
|
import { from, type Mapped, mapped, type Part } from "../positions";
|
|
3
|
-
import { unusedName } from "./layouts";
|
|
3
|
+
import { type ArrayLayout, unusedName } from "./layouts";
|
|
4
4
|
import type { ModelNode } from "./model";
|
|
5
5
|
import type { Env, Value } from "./values";
|
|
6
6
|
|
|
@@ -41,6 +41,14 @@ export class Emitter {
|
|
|
41
41
|
temporary(): string {
|
|
42
42
|
return this.local(`tsplcTemp${++this.temporaries}`);
|
|
43
43
|
}
|
|
44
|
+
/** Whether code reads a local this routine has named. */
|
|
45
|
+
names(text: Part): boolean {
|
|
46
|
+
return namesIn(text).some((name) => this.used.has(name));
|
|
47
|
+
}
|
|
48
|
+
/** Whether a line this routine has written reads `name`. */
|
|
49
|
+
reads(name: string): boolean {
|
|
50
|
+
return this.lines.some((line) => namesIn(line).includes(name));
|
|
51
|
+
}
|
|
44
52
|
/**
|
|
45
53
|
* Runs `write` and takes the lines it wrote instead of keeping them, for a caller that places
|
|
46
54
|
* them once it knows what surrounds them. `nested` indents them for a block the caller opens.
|
|
@@ -63,11 +71,34 @@ export class Emitter {
|
|
|
63
71
|
append(lines: readonly Mapped[]): void {
|
|
64
72
|
this.lines.push(...lines);
|
|
65
73
|
}
|
|
74
|
+
/** An if…else if chain, one arm for each test. */
|
|
75
|
+
chain(arms: readonly { test: Part; body: VoidFunction }[]): void {
|
|
76
|
+
arms.forEach((arm, index) => {
|
|
77
|
+
if (index === 0) {
|
|
78
|
+
this.open(mapped`if (${arm.test}) {`);
|
|
79
|
+
} else {
|
|
80
|
+
this.reopen(mapped`} else if (${arm.test}) {`);
|
|
81
|
+
}
|
|
82
|
+
arm.body();
|
|
83
|
+
});
|
|
84
|
+
if (arms.length > 0) {
|
|
85
|
+
this.close();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
66
88
|
}
|
|
67
89
|
|
|
90
|
+
/** The names code reads: not a member's, nor the digits of a typed literal such as `16#FF`. */
|
|
91
|
+
const namesIn = (text: Part): string[] =>
|
|
92
|
+
String(text).match(/(?<![\w.#])[A-Za-z_]\w*/g) ?? [];
|
|
93
|
+
|
|
68
94
|
export interface Frame {
|
|
69
95
|
emitter: Emitter;
|
|
70
96
|
env: Env;
|
|
97
|
+
/**
|
|
98
|
+
* How many times the body of each PLC loop being written can run, outermost first; undefined for
|
|
99
|
+
* a loop with no counted bound. Frames within one routine share it.
|
|
100
|
+
*/
|
|
101
|
+
loops: (number | undefined)[];
|
|
71
102
|
self?: ModelNode;
|
|
72
103
|
/** The routine's declared return type, for literal and optional results. */
|
|
73
104
|
returns?: ts.Type;
|
|
@@ -78,7 +109,7 @@ export interface Frame {
|
|
|
78
109
|
*/
|
|
79
110
|
generator?: { yields: (value: Value) => void; exit: string };
|
|
80
111
|
/** An array result's layout, at the length the routine's return statements give it. */
|
|
81
|
-
resultLayout?:
|
|
112
|
+
resultLayout?: ArrayLayout;
|
|
82
113
|
resultLength?: number;
|
|
83
114
|
/** Whether a return statement returned a stored record or array, which a caller only gets a copy of. */
|
|
84
115
|
returnsStored?: boolean;
|
|
@@ -89,6 +120,23 @@ export interface Frame {
|
|
|
89
120
|
referents?: Referents;
|
|
90
121
|
}
|
|
91
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Writes the body of a loop that stays a loop in the PLC. A yield in it runs up to `runs` times,
|
|
125
|
+
* which sizes a snapshot of the generator; an unrolled loop needs none, as each step is written out.
|
|
126
|
+
*/
|
|
127
|
+
export const looping = <T>(
|
|
128
|
+
frame: Frame,
|
|
129
|
+
runs: number | undefined,
|
|
130
|
+
body: () => T,
|
|
131
|
+
): T => {
|
|
132
|
+
frame.loops.push(runs);
|
|
133
|
+
try {
|
|
134
|
+
return body();
|
|
135
|
+
} finally {
|
|
136
|
+
frame.loops.pop();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
92
140
|
/** What a reference a routine returns can be. */
|
|
93
141
|
export interface Referents {
|
|
94
142
|
options: Set<ModelNode>;
|
|
@@ -1,18 +1,5 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
|
|
3
|
-
/** The checker's queries whose answers can name a generic class's type parameters. */
|
|
4
|
-
const typeQueries = new Set<string | symbol>([
|
|
5
|
-
"getTypeAtLocation",
|
|
6
|
-
"getTypeOfSymbol",
|
|
7
|
-
"getTypeOfSymbolAtLocation",
|
|
8
|
-
"getReturnTypeOfSignature",
|
|
9
|
-
"getTypeFromTypeNode",
|
|
10
|
-
"getAwaitedType",
|
|
11
|
-
"getNonNullableType",
|
|
12
|
-
"getIndexTypeOfType",
|
|
13
|
-
"getBaseTypeOfLiteralType",
|
|
14
|
-
]);
|
|
15
|
-
|
|
16
3
|
const isTypeReference = (type: ts.Type): type is ts.TypeReference =>
|
|
17
4
|
Boolean(type.flags & ts.TypeFlags.Object) &&
|
|
18
5
|
"objectFlags" in type &&
|
|
@@ -35,12 +22,16 @@ export class GenericTypes {
|
|
|
35
22
|
if (typeof value !== "function") {
|
|
36
23
|
return value;
|
|
37
24
|
}
|
|
38
|
-
|
|
39
|
-
return value.bind(target);
|
|
40
|
-
}
|
|
25
|
+
// Any answer that is a type, or a list of them, can name a type parameter.
|
|
41
26
|
return (...args: unknown[]) => {
|
|
42
27
|
const result: unknown = value.apply(target, args);
|
|
43
|
-
|
|
28
|
+
if (isType(result)) {
|
|
29
|
+
return this.substitute(result);
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(result) && result.every(isType)) {
|
|
32
|
+
return result.map((type) => this.substitute(type));
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
44
35
|
};
|
|
45
36
|
},
|
|
46
37
|
});
|
|
@@ -73,11 +64,19 @@ export class GenericTypes {
|
|
|
73
64
|
}
|
|
74
65
|
}
|
|
75
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Calls one of the checker's type constructors. TypeScript 6 has them on every checker but leaves
|
|
69
|
+
* them out of its public declarations, so they are looked up by name.
|
|
70
|
+
*/
|
|
76
71
|
private construct(
|
|
77
72
|
name: "getUnionType" | "createArrayType",
|
|
78
73
|
argument: ts.Type | readonly ts.Type[],
|
|
79
74
|
): ts.Type {
|
|
80
|
-
const
|
|
75
|
+
const make: unknown = Reflect.get(this.base, name);
|
|
76
|
+
if (typeof make !== "function") {
|
|
77
|
+
throw new Error(`This TypeScript's checker has no ${name}.`);
|
|
78
|
+
}
|
|
79
|
+
const made: unknown = make.call(this.base, argument);
|
|
81
80
|
if (!isType(made)) {
|
|
82
81
|
throw new Error(`${name} made no type.`);
|
|
83
82
|
}
|
|
@@ -115,23 +114,10 @@ export class GenericTypes {
|
|
|
115
114
|
}
|
|
116
115
|
}
|
|
117
116
|
|
|
118
|
-
/**
|
|
119
|
-
* Calls one of the checker's type constructors. TypeScript 6 has them on every checker but leaves
|
|
120
|
-
* them out of its public declarations, so they are looked up by name.
|
|
121
|
-
*/
|
|
122
|
-
const typeConstructor = (
|
|
123
|
-
checker: ts.TypeChecker,
|
|
124
|
-
name: "getUnionType" | "createArrayType",
|
|
125
|
-
): ((argument: unknown) => unknown) => {
|
|
126
|
-
const found: unknown = Reflect.get(checker, name);
|
|
127
|
-
if (typeof found !== "function") {
|
|
128
|
-
throw new Error(`This TypeScript's checker has no ${name}.`);
|
|
129
|
-
}
|
|
130
|
-
return (argument) => found.call(checker, argument);
|
|
131
|
-
};
|
|
132
|
-
|
|
117
|
+
/** Whether a checker's answer is a type; a signature also has flags and its checker, but no type's methods. */
|
|
133
118
|
const isType = (value: unknown): value is ts.Type =>
|
|
134
119
|
typeof value === "object" &&
|
|
135
120
|
value !== null &&
|
|
136
121
|
"flags" in value &&
|
|
137
|
-
"checker" in value
|
|
122
|
+
"checker" in value &&
|
|
123
|
+
"isUnion" in value;
|