@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
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { type Diagnostic, error } from "@tsplc/compiler/ir";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { plcSources } from "./module-graph";
|
|
4
|
+
import type { Positions } from "./positions";
|
|
5
|
+
import { dialectFor, inSdk, sdkModules, unaliased } from "./sdk";
|
|
6
|
+
|
|
7
|
+
/** The SDK module of timers and edges, plain source a program imports like its own. */
|
|
8
|
+
const signals = "@tsplc/compiler/signals";
|
|
9
|
+
|
|
10
|
+
/** Dialect names a program has no use for, with what to write instead. */
|
|
11
|
+
const unsupported: Readonly<Record<string, string>> = {
|
|
12
|
+
FunctionBlock:
|
|
13
|
+
"write a plain class, whose instances keep their fields from one scan to the next",
|
|
14
|
+
Input:
|
|
15
|
+
"keep the input as a field of the program's state and bind its path in tsplc.config.ts io",
|
|
16
|
+
Output:
|
|
17
|
+
"keep the output as a field of the program's state and bind its path in tsplc.config.ts io",
|
|
18
|
+
InOut:
|
|
19
|
+
"pass the object that holds the value; a callee changes it through that object",
|
|
20
|
+
ref: "pass the object that holds the value; a callee changes it through that object",
|
|
21
|
+
OnDelayTimer: `import OnDelayTimer from ${signals}`,
|
|
22
|
+
OffDelayTimer: `import OffDelayTimer from ${signals}`,
|
|
23
|
+
PulseTimer: `import PulseTimer from ${signals}`,
|
|
24
|
+
RisingEdge: `import RisingEdge from ${signals}`,
|
|
25
|
+
FallingEdge: `import FallingEdge from ${signals}`,
|
|
26
|
+
Blinker:
|
|
27
|
+
"write it as a class, as the timers of @tsplc/compiler/signals are written",
|
|
28
|
+
UpCounter: "count in a field of a class",
|
|
29
|
+
DownCounter: "count in a field of a class",
|
|
30
|
+
UpDownCounter: "count in a field of a class",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** The SDK's internal exports, which the compiler's own generated code calls. */
|
|
34
|
+
const internal = new Set(["cooperativeTasks", "opcuaCall"]);
|
|
35
|
+
|
|
36
|
+
/** The module a program imports a dialect global from, when it may use it at all. */
|
|
37
|
+
const importedFrom = (name: string): string =>
|
|
38
|
+
name === "retain" || name === "persist"
|
|
39
|
+
? sdkModules.retain
|
|
40
|
+
: sdkModules.types;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Where a program's own source uses the tsPLC dialect: its function blocks, IO declarations,
|
|
44
|
+
* references and standard blocks, or a global that a plain program imports instead. The compiler
|
|
45
|
+
* lowers programs into the dialect; a program writes plain TypeScript.
|
|
46
|
+
*/
|
|
47
|
+
export const dialectUses = (
|
|
48
|
+
program: ts.Program,
|
|
49
|
+
sdk: string,
|
|
50
|
+
positions: Positions,
|
|
51
|
+
): Diagnostic[] => {
|
|
52
|
+
const checker = program.getTypeChecker();
|
|
53
|
+
const dialect = dialectFor(sdk);
|
|
54
|
+
const diagnostics: Diagnostic[] = [];
|
|
55
|
+
for (const source of plcSources(program)) {
|
|
56
|
+
if (inSdk(sdk, source.fileName)) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const reported = new Set<string>();
|
|
60
|
+
const visit = (node: ts.Node): void => {
|
|
61
|
+
if (ts.isIdentifier(node) && !reported.has(node.text)) {
|
|
62
|
+
const message = dialectUse(checker, sdk, dialect, node);
|
|
63
|
+
if (message) {
|
|
64
|
+
reported.add(node.text);
|
|
65
|
+
diagnostics.push(error("PLC1015", message, positions.node(node)));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
ts.forEachChild(node, visit);
|
|
69
|
+
};
|
|
70
|
+
visit(source);
|
|
71
|
+
}
|
|
72
|
+
return diagnostics;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const dialectUse = (
|
|
76
|
+
checker: ts.TypeChecker,
|
|
77
|
+
sdk: string,
|
|
78
|
+
dialect: string,
|
|
79
|
+
node: ts.Identifier,
|
|
80
|
+
): string | undefined => {
|
|
81
|
+
const symbol = checker.getSymbolAtLocation(node);
|
|
82
|
+
const declaration = symbol?.declarations?.[0];
|
|
83
|
+
if (!symbol || !declaration) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
const ambient = declaration.getSourceFile().fileName === dialect;
|
|
87
|
+
const named = unaliased(checker, symbol).declarations?.[0];
|
|
88
|
+
const fromSdk =
|
|
89
|
+
ambient ||
|
|
90
|
+
(named !== undefined && inSdk(sdk, named.getSourceFile().fileName));
|
|
91
|
+
if (!fromSdk) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
const instead = unsupported[node.text];
|
|
95
|
+
if (instead) {
|
|
96
|
+
return `${node.text} is not part of PLC TypeScript: ${instead}.`;
|
|
97
|
+
}
|
|
98
|
+
if (internal.has(node.text)) {
|
|
99
|
+
return `${node.text} is the compiler's own; a program uses tasks, backgroundTasks and createServiceClient.`;
|
|
100
|
+
}
|
|
101
|
+
return ambient
|
|
102
|
+
? `${node.text} is not a global: import it from ${importedFrom(node.text)}.`
|
|
103
|
+
: undefined;
|
|
104
|
+
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type ts from "typescript";
|
|
2
|
-
import {
|
|
3
|
-
import type { DialectText } from "./dialect";
|
|
2
|
+
import { mapped } from "../positions";
|
|
4
3
|
import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
|
|
5
4
|
import type { Frame } from "./frame";
|
|
6
5
|
import { constant, SpecializeError, type Value } from "./values";
|
|
@@ -10,44 +9,13 @@ import { constant, SpecializeError, type Value } from "./values";
|
|
|
10
9
|
* length, so each runs as one test or store per element, in the order JavaScript visits them.
|
|
11
10
|
*/
|
|
12
11
|
export class ArraySearches {
|
|
13
|
-
private readonly checker: ts.TypeChecker;
|
|
14
|
-
private readonly dialect: DialectText;
|
|
15
|
-
|
|
16
12
|
constructor(
|
|
17
|
-
context: SpecializeContext,
|
|
18
|
-
private readonly expressions:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/** `includes(target)`: whether any element equals it. */
|
|
25
|
-
includes(
|
|
26
|
-
items: readonly Value[],
|
|
27
|
-
node: ts.CallExpression,
|
|
28
|
-
frame: Frame,
|
|
29
|
-
): Value {
|
|
30
|
-
const target = this.target(node, frame);
|
|
31
|
-
const tests: (string | Mapped)[] = [];
|
|
32
|
-
for (const item of items) {
|
|
33
|
-
const test = this.expressions.equality(item, target, { negate: false });
|
|
34
|
-
if (test.kind === "const") {
|
|
35
|
-
if (test.value) {
|
|
36
|
-
return constant(true);
|
|
37
|
-
}
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
tests.push(this.dialect.condition(test, node));
|
|
41
|
-
}
|
|
42
|
-
if (!tests.length) {
|
|
43
|
-
return constant(false);
|
|
44
|
-
}
|
|
45
|
-
return {
|
|
46
|
-
kind: "code",
|
|
47
|
-
text: mapped`(${joined(tests, " || ")})`,
|
|
48
|
-
declared: this.checker.getBooleanType(),
|
|
49
|
-
};
|
|
50
|
-
}
|
|
13
|
+
private readonly context: SpecializeContext,
|
|
14
|
+
private readonly expressions: Pick<
|
|
15
|
+
ExpressionSpecializer,
|
|
16
|
+
"expression" | "held" | "localType" | "equality" | "assignments"
|
|
17
|
+
>,
|
|
18
|
+
) {}
|
|
51
19
|
|
|
52
20
|
/** `indexOf(target)` and `lastIndexOf(target)`: the first or last index of an equal element, or -1. */
|
|
53
21
|
indexOf(
|
|
@@ -56,7 +24,7 @@ export class ArraySearches {
|
|
|
56
24
|
frame: Frame,
|
|
57
25
|
{ last }: { last: boolean },
|
|
58
26
|
): Value {
|
|
59
|
-
const target =
|
|
27
|
+
const target = searchTarget(node, frame, this.expressions);
|
|
60
28
|
return this.firstIndex(items, frame, { last }, (item) =>
|
|
61
29
|
this.expressions.equality(item, target, { negate: false }),
|
|
62
30
|
);
|
|
@@ -74,7 +42,10 @@ export class ArraySearches {
|
|
|
74
42
|
);
|
|
75
43
|
}
|
|
76
44
|
|
|
77
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* `find(test)` and `findLast(test)`: the element that passed, or undefined. Among objects setup
|
|
47
|
+
* built the one that passes is chosen now; among values, at run time.
|
|
48
|
+
*/
|
|
78
49
|
find(
|
|
79
50
|
items: readonly Value[],
|
|
80
51
|
node: ts.CallExpression,
|
|
@@ -82,29 +53,29 @@ export class ArraySearches {
|
|
|
82
53
|
{ last }: { last: boolean },
|
|
83
54
|
apply: (args: Value[]) => Value,
|
|
84
55
|
): Value {
|
|
85
|
-
|
|
86
|
-
|
|
56
|
+
if (
|
|
57
|
+
items.some((item) => item.kind === "static" || item.kind === "function")
|
|
58
|
+
) {
|
|
59
|
+
return decidedFind(items, node, { last }, apply);
|
|
60
|
+
}
|
|
61
|
+
const { checker, dialect } = this.context;
|
|
62
|
+
const declared = checker.getTypeAtLocation(node);
|
|
63
|
+
const element = checker.getNonNullableType(declared);
|
|
87
64
|
const found = frame.emitter.temporary();
|
|
88
65
|
const result = frame.emitter.temporary();
|
|
89
66
|
frame.emitter.line(`let ${found}: boolean = false;`);
|
|
90
67
|
frame.emitter.line(
|
|
91
|
-
mapped`let ${result}: ${this.expressions.localType(declared)} = ${
|
|
68
|
+
mapped`let ${result}: ${this.expressions.localType(declared)} = ${dialect.assignmentText(constant(undefined), declared, undefined)};`,
|
|
92
69
|
);
|
|
93
70
|
for (const [index, item] of ordered(items, { last })) {
|
|
94
|
-
if (item.kind === "static" || item.kind === "function") {
|
|
95
|
-
throw new SpecializeError(
|
|
96
|
-
node,
|
|
97
|
-
"find picks one of the objects setup built, which scan code can't hold; use findIndex and index the array.",
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
71
|
frame.emitter.open(`if (!${found}) {`);
|
|
101
72
|
const test = apply([item, constant(index)]);
|
|
102
73
|
if (test.kind !== "const" || test.value) {
|
|
103
74
|
const passed =
|
|
104
|
-
test.kind === "const" ? "true" :
|
|
75
|
+
test.kind === "const" ? "true" : dialect.condition(test, node);
|
|
105
76
|
frame.emitter.open(mapped`if (${passed}) {`);
|
|
106
77
|
frame.emitter.line(
|
|
107
|
-
mapped`${result} = ${
|
|
78
|
+
mapped`${result} = ${dialect.wrapDefined(dialect.code(item, element), declared)};`,
|
|
108
79
|
);
|
|
109
80
|
frame.emitter.line(`${found} = true;`);
|
|
110
81
|
frame.emitter.close();
|
|
@@ -166,7 +137,7 @@ export class ArraySearches {
|
|
|
166
137
|
}
|
|
167
138
|
} else {
|
|
168
139
|
frame.emitter.line(
|
|
169
|
-
mapped`if (${this.dialect.condition(passed, frame.emitter.origin)}) { ${result} = ${index}; }`,
|
|
140
|
+
mapped`if (${this.context.dialect.condition(passed, frame.emitter.origin)}) { ${result} = ${index}; }`,
|
|
170
141
|
);
|
|
171
142
|
}
|
|
172
143
|
frame.emitter.close();
|
|
@@ -174,22 +145,11 @@ export class ArraySearches {
|
|
|
174
145
|
return {
|
|
175
146
|
kind: "code",
|
|
176
147
|
text: result,
|
|
177
|
-
declared: this.checker.getNumberType(),
|
|
148
|
+
declared: this.context.checker.getNumberType(),
|
|
178
149
|
place: true,
|
|
179
150
|
};
|
|
180
151
|
}
|
|
181
152
|
|
|
182
|
-
private target(node: ts.CallExpression, frame: Frame): Value {
|
|
183
|
-
const [argument] = node.arguments;
|
|
184
|
-
if (!argument) {
|
|
185
|
-
throw new SpecializeError(node, "Pass the value to look for.");
|
|
186
|
-
}
|
|
187
|
-
return this.expressions.held(
|
|
188
|
-
this.expressions.expression(argument, frame),
|
|
189
|
-
frame,
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
153
|
private constantIndex(
|
|
194
154
|
node: ts.Expression,
|
|
195
155
|
length: number,
|
|
@@ -206,11 +166,59 @@ export class ArraySearches {
|
|
|
206
166
|
}
|
|
207
167
|
}
|
|
208
168
|
|
|
209
|
-
/** Elements with their indexes, first to last or, for the
|
|
210
|
-
const ordered = (
|
|
211
|
-
items: readonly
|
|
169
|
+
/** Elements with their indexes, first to last or, for a search from the end, last to first. */
|
|
170
|
+
export const ordered = <T>(
|
|
171
|
+
items: readonly T[],
|
|
212
172
|
{ last }: { last: boolean },
|
|
213
|
-
): [number,
|
|
173
|
+
): [number, T][] => {
|
|
214
174
|
const indexed = [...items.entries()];
|
|
215
175
|
return last ? indexed.reverse() : indexed;
|
|
216
176
|
};
|
|
177
|
+
|
|
178
|
+
/** An array method's callback: its first argument, the test or step it runs on elements. */
|
|
179
|
+
export const callbackArgument = (
|
|
180
|
+
node: ts.CallExpression,
|
|
181
|
+
frame: Frame,
|
|
182
|
+
expressions: Pick<ExpressionSpecializer, "expression">,
|
|
183
|
+
): Value => {
|
|
184
|
+
const [argument] = node.arguments;
|
|
185
|
+
if (!argument) {
|
|
186
|
+
throw new SpecializeError(node, "Pass a callback.");
|
|
187
|
+
}
|
|
188
|
+
return expressions.expression(argument, frame);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/** What `includes` and `indexOf` look for, held so each element is compared with the value it had at the call. */
|
|
192
|
+
export const searchTarget = (
|
|
193
|
+
node: ts.CallExpression,
|
|
194
|
+
frame: Frame,
|
|
195
|
+
expressions: Pick<ExpressionSpecializer, "expression" | "held">,
|
|
196
|
+
): Value => {
|
|
197
|
+
const [argument] = node.arguments;
|
|
198
|
+
if (!argument) {
|
|
199
|
+
throw new SpecializeError(node, "Pass the value to look for.");
|
|
200
|
+
}
|
|
201
|
+
return expressions.held(expressions.expression(argument, frame), frame);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/** `find` and `findLast` among objects setup built, whose tests are known now: the object that passes, or undefined. */
|
|
205
|
+
const decidedFind = (
|
|
206
|
+
items: readonly Value[],
|
|
207
|
+
node: ts.CallExpression,
|
|
208
|
+
{ last }: { last: boolean },
|
|
209
|
+
apply: (args: Value[]) => Value,
|
|
210
|
+
): Value => {
|
|
211
|
+
for (const [index, item] of ordered(items, { last })) {
|
|
212
|
+
const test = apply([item, constant(index)]);
|
|
213
|
+
if (test.kind !== "const") {
|
|
214
|
+
throw new SpecializeError(
|
|
215
|
+
node,
|
|
216
|
+
"find picks one of the objects setup built, which scan code can't hold; use findIndex and index the array.",
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
if (test.value) {
|
|
220
|
+
return item;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return constant(undefined);
|
|
224
|
+
};
|
|
@@ -4,7 +4,7 @@ import type { DialectText } from "./dialect";
|
|
|
4
4
|
import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
|
|
5
5
|
import type { Frame } from "./frame";
|
|
6
6
|
import { constant, SpecializeError, type Value } from "./values";
|
|
7
|
-
import { isAssignment, isUpdate } from "./writes";
|
|
7
|
+
import { isAssignment, isUpdate, occursOutsideFunctions } from "./writes";
|
|
8
8
|
|
|
9
9
|
/** The operator each compound assignment applies. */
|
|
10
10
|
const compoundOperators = new Map<ts.SyntaxKind, string>([
|
|
@@ -17,24 +17,34 @@ const compoundOperators = new Map<ts.SyntaxKind, string>([
|
|
|
17
17
|
|
|
18
18
|
/** Whether evaluating the expression writes a place: an assignment or `++`/`--` in it, outside functions it declares. */
|
|
19
19
|
export const containsWrite = (node: ts.Node): boolean =>
|
|
20
|
-
(
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
occursOutsideFunctions(
|
|
21
|
+
node,
|
|
22
|
+
(inner) =>
|
|
23
|
+
(ts.isBinaryExpression(inner) &&
|
|
24
|
+
isAssignment(inner.operatorToken.kind)) ||
|
|
25
|
+
isUpdate(inner),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
/** A statement storing `value` into `place`. */
|
|
29
|
+
export const storeStatement = (place: Part, value: Part): Mapped =>
|
|
30
|
+
mapped`${place} = ${value};`;
|
|
23
31
|
|
|
24
32
|
/**
|
|
25
33
|
* Assignments and steps. Structured Text assigns only in statements, so one used as a value is a
|
|
26
34
|
* statement first, and the expression reads the place afterwards.
|
|
27
35
|
*/
|
|
28
36
|
export class AssignmentSpecializer {
|
|
29
|
-
private readonly checker: ts.TypeChecker;
|
|
30
|
-
private readonly dialect: DialectText;
|
|
31
|
-
|
|
32
37
|
constructor(
|
|
33
|
-
context: SpecializeContext,
|
|
38
|
+
private readonly context: SpecializeContext,
|
|
34
39
|
private readonly expressions: ExpressionSpecializer,
|
|
35
|
-
) {
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
) {}
|
|
41
|
+
|
|
42
|
+
private get checker(): ts.TypeChecker {
|
|
43
|
+
return this.context.checker;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private get dialect(): DialectText {
|
|
47
|
+
return this.context.dialect;
|
|
38
48
|
}
|
|
39
49
|
|
|
40
50
|
/** Stores the assignment and returns what the target then holds. */
|
|
@@ -94,16 +104,7 @@ export class AssignmentSpecializer {
|
|
|
94
104
|
}
|
|
95
105
|
const value = this.expressions.expression(node.right, frame, declared);
|
|
96
106
|
if (operator === ts.SyntaxKind.EqualsToken) {
|
|
97
|
-
frame.emitter.line(
|
|
98
|
-
this.store(
|
|
99
|
-
target,
|
|
100
|
-
this.dialect.assignmentText(
|
|
101
|
-
value,
|
|
102
|
-
declared,
|
|
103
|
-
target.kind === "code" ? target.iec : undefined,
|
|
104
|
-
),
|
|
105
|
-
),
|
|
106
|
-
);
|
|
107
|
+
frame.emitter.line(this.stored(target, value));
|
|
107
108
|
return target;
|
|
108
109
|
}
|
|
109
110
|
const binary = compoundOperators.get(operator);
|
|
@@ -209,14 +210,16 @@ export class AssignmentSpecializer {
|
|
|
209
210
|
property,
|
|
210
211
|
frame,
|
|
211
212
|
);
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
213
|
+
this.collectTarget(
|
|
214
|
+
property.name,
|
|
215
|
+
this.expressions.defaulted(
|
|
216
|
+
part,
|
|
217
|
+
property.objectAssignmentInitializer,
|
|
218
|
+
frame,
|
|
219
|
+
),
|
|
220
|
+
frame,
|
|
221
|
+
writes,
|
|
222
|
+
);
|
|
220
223
|
continue;
|
|
221
224
|
}
|
|
222
225
|
if (!ts.isPropertyAssignment(property)) {
|
|
@@ -255,7 +258,7 @@ export class AssignmentSpecializer {
|
|
|
255
258
|
) {
|
|
256
259
|
this.collectTarget(
|
|
257
260
|
target.left,
|
|
258
|
-
this.
|
|
261
|
+
this.expressions.defaulted(part, target.right, frame),
|
|
259
262
|
frame,
|
|
260
263
|
writes,
|
|
261
264
|
);
|
|
@@ -270,68 +273,39 @@ export class AssignmentSpecializer {
|
|
|
270
273
|
}
|
|
271
274
|
const read = this.expressions.held(part, frame);
|
|
272
275
|
writes.push(() => {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
this.dialect.assignmentText(
|
|
279
|
-
read,
|
|
280
|
-
declared,
|
|
281
|
-
place.kind === "code" ? place.iec : undefined,
|
|
282
|
-
),
|
|
283
|
-
),
|
|
276
|
+
this.storeValue(
|
|
277
|
+
this.expressions.expression(target, frame),
|
|
278
|
+
read,
|
|
279
|
+
target,
|
|
280
|
+
frame,
|
|
284
281
|
);
|
|
285
282
|
});
|
|
286
283
|
}
|
|
287
284
|
|
|
288
|
-
private withDefault(
|
|
289
|
-
part: Value,
|
|
290
|
-
fallback: ts.Expression,
|
|
291
|
-
frame: Frame,
|
|
292
|
-
): Value {
|
|
293
|
-
if (part.kind === "const" && part.value === undefined) {
|
|
294
|
-
return this.expressions.expression(fallback, frame);
|
|
295
|
-
}
|
|
296
|
-
if (part.kind !== "code") {
|
|
297
|
-
return part;
|
|
298
|
-
}
|
|
299
|
-
const declared = part.declared;
|
|
300
|
-
if (!declared || !(declared.flags & ts.TypeFlags.Union)) {
|
|
301
|
-
return part;
|
|
302
|
-
}
|
|
303
|
-
const defined = this.checker.getNonNullableType(declared);
|
|
304
|
-
const otherwise = this.expressions.expression(fallback, frame, defined);
|
|
305
|
-
return {
|
|
306
|
-
kind: "code",
|
|
307
|
-
text: mapped`(${this.dialect.isUndefinedText(part.text, declared)} ? ${this.dialect.code(otherwise, defined)} : ${this.dialect.code(part, undefined)}${this.expressions.optionalValueSuffix(declared)})`,
|
|
308
|
-
declared: defined,
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
|
|
312
285
|
/** Stores a value into a place, converting it as an IEC assignment does. */
|
|
313
286
|
storeValue(place: Value, value: Value, at: ts.Node, frame: Frame): void {
|
|
314
287
|
this.placeText(place, at);
|
|
315
|
-
frame.emitter.line(
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
288
|
+
frame.emitter.line(this.stored(place, value));
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** A statement storing a value into a place, converted as an IEC assignment does. */
|
|
292
|
+
private stored(place: Value, value: Value): Mapped {
|
|
293
|
+
return this.store(
|
|
294
|
+
place,
|
|
295
|
+
this.dialect.assignmentText(
|
|
296
|
+
value,
|
|
297
|
+
place.kind === "code" ? place.declared : undefined,
|
|
298
|
+
place.kind === "code" ? place.iec : undefined,
|
|
323
299
|
),
|
|
324
300
|
);
|
|
325
301
|
}
|
|
326
302
|
|
|
327
|
-
/** A statement storing `text` into a place
|
|
303
|
+
/** A statement storing `text` into a place. */
|
|
328
304
|
private store(target: Value, text: Part): Mapped {
|
|
329
305
|
if (target.kind !== "code") {
|
|
330
306
|
throw new Error("Not a place.");
|
|
331
307
|
}
|
|
332
|
-
return target.
|
|
333
|
-
? mapped`${target.setter}(${text});`
|
|
334
|
-
: mapped`${target.text} = ${text};`;
|
|
308
|
+
return storeStatement(target.text, text);
|
|
335
309
|
}
|
|
336
310
|
|
|
337
311
|
private placeText(value: Value, node: ts.Node): string | Mapped {
|
|
@@ -341,9 +315,26 @@ export class AssignmentSpecializer {
|
|
|
341
315
|
`This changes a copy: ${value.copyOf} returns stored state, which TypeScript would change in place but the PLC copies. Change it where it is stored, as a collection's update does.`,
|
|
342
316
|
);
|
|
343
317
|
}
|
|
318
|
+
if (value.kind === "code" && value.input) {
|
|
319
|
+
throw new SpecializeError(
|
|
320
|
+
node,
|
|
321
|
+
`${value.input.path} is bound to ${value.input.address}, and the hardware overwrites it every scan: keep what scan code computes in a field of its own.`,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
344
324
|
if (value.kind === "code" && value.place) {
|
|
345
325
|
return value.text;
|
|
346
326
|
}
|
|
327
|
+
if (
|
|
328
|
+
value.kind === "function" ||
|
|
329
|
+
(value.kind === "static" &&
|
|
330
|
+
(value.node.captured.kind === "closure" ||
|
|
331
|
+
value.node.captured.kind === "function"))
|
|
332
|
+
) {
|
|
333
|
+
throw new SpecializeError(
|
|
334
|
+
node,
|
|
335
|
+
"This assigns a function, and the PLC calls only functions known when the program compiles: keep one function, and choose what it does with an if.",
|
|
336
|
+
);
|
|
337
|
+
}
|
|
347
338
|
throw new SpecializeError(
|
|
348
339
|
node,
|
|
349
340
|
"Assign to stored state: a field scan code owns, or a let.",
|