@tsplc/compiler 0.2.17 → 0.2.19
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 +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { copied, mapped, type Mapped, type Part } from "../positions.ts";
|
|
3
|
+
import type {
|
|
4
|
+
ExpressionSpecializer,
|
|
5
|
+
SpecializeContext,
|
|
6
|
+
} from "./expressions.ts";
|
|
7
|
+
import type { DialectText } from "./dialect.ts";
|
|
8
|
+
import type { Frame } from "./frame.ts";
|
|
9
|
+
import type { Initializers } from "./initializers.ts";
|
|
10
|
+
import type { Layouts } from "./layouts.ts";
|
|
11
|
+
import { SpecializeError, type Value } from "./values.ts";
|
|
12
|
+
import { isAssignment, isUpdate } from "./writes.ts";
|
|
13
|
+
|
|
14
|
+
/** Specializes one routine's statements; expressions in them go through its ExpressionSpecializer. */
|
|
15
|
+
export class StatementSpecializer {
|
|
16
|
+
private readonly checker: ts.TypeChecker;
|
|
17
|
+
private readonly dialect: DialectText;
|
|
18
|
+
private readonly layouts: Layouts;
|
|
19
|
+
private readonly initializers: Initializers;
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
context: SpecializeContext,
|
|
23
|
+
private readonly expressions: ExpressionSpecializer,
|
|
24
|
+
) {
|
|
25
|
+
this.checker = context.checker;
|
|
26
|
+
this.dialect = context.dialect;
|
|
27
|
+
this.layouts = context.layouts;
|
|
28
|
+
this.initializers = context.initializers;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
body(node: ts.ConciseBody, frame: Frame): void {
|
|
32
|
+
if (ts.isBlock(node)) {
|
|
33
|
+
for (const statement of node.statements) {
|
|
34
|
+
this.statement(statement, frame);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
frame.emitter.origin = node;
|
|
38
|
+
frame.emitter.line(
|
|
39
|
+
mapped`${this.dialect.code(this.expressions.expression(node, frame), undefined)};`,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private statement(node: ts.Statement, frame: Frame): void {
|
|
45
|
+
const enclosing = frame.emitter.origin;
|
|
46
|
+
frame.emitter.origin = node;
|
|
47
|
+
this.specializeStatement(node, frame);
|
|
48
|
+
frame.emitter.origin = enclosing;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private specializeStatement(node: ts.Statement, frame: Frame): void {
|
|
52
|
+
const { emitter } = frame;
|
|
53
|
+
if (ts.isBlock(node)) {
|
|
54
|
+
emitter.open("{");
|
|
55
|
+
for (const statement of node.statements) {
|
|
56
|
+
this.statement(statement, frame);
|
|
57
|
+
}
|
|
58
|
+
emitter.close();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (ts.isVariableStatement(node)) {
|
|
62
|
+
const mutable = !(node.declarationList.flags & ts.NodeFlags.Const);
|
|
63
|
+
for (const declaration of node.declarationList.declarations) {
|
|
64
|
+
this.declare(declaration, mutable, frame);
|
|
65
|
+
}
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (ts.isExpressionStatement(node)) {
|
|
69
|
+
this.effect(node.expression, frame);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (ts.isIfStatement(node)) {
|
|
73
|
+
this.ifStatement(node, frame);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (ts.isReturnStatement(node)) {
|
|
77
|
+
this.returnStatement(node, frame);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (ts.isForOfStatement(node)) {
|
|
81
|
+
this.forOf(node, frame);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (ts.isSwitchStatement(node)) {
|
|
85
|
+
this.switch(node, frame);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (ts.isForStatement(node)) {
|
|
89
|
+
this.forLoop(node, frame);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (ts.isWhileStatement(node)) {
|
|
93
|
+
throw new SpecializeError(
|
|
94
|
+
node,
|
|
95
|
+
"Use a counted for loop or for…of; while loops have no bound to prove.",
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
if (ts.isTryStatement(node)) {
|
|
99
|
+
this.tryStatement(node, frame);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (ts.isBreakStatement(node) || ts.isContinueStatement(node)) {
|
|
103
|
+
emitter.line(copied(node));
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (ts.isEmptyStatement(node)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
throw new SpecializeError(
|
|
110
|
+
node,
|
|
111
|
+
`${ts.SyntaxKind[node.kind]} is not supported in scan code.`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** An if on a value setup fixed keeps only the branch it takes. */
|
|
116
|
+
private ifStatement(node: ts.IfStatement, frame: Frame): void {
|
|
117
|
+
const condition = this.expressions.expression(node.expression, frame);
|
|
118
|
+
if (
|
|
119
|
+
condition.kind === "const" ||
|
|
120
|
+
condition.kind === "static" ||
|
|
121
|
+
condition.kind === "function"
|
|
122
|
+
) {
|
|
123
|
+
const truthy =
|
|
124
|
+
condition.kind === "const" ? Boolean(condition.value) : true;
|
|
125
|
+
if (truthy) {
|
|
126
|
+
this.statement(node.thenStatement, frame);
|
|
127
|
+
} else if (node.elseStatement) {
|
|
128
|
+
this.statement(node.elseStatement, frame);
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
frame.emitter.open(
|
|
133
|
+
mapped`if (${this.dialect.condition(condition, node.expression)}) {`,
|
|
134
|
+
);
|
|
135
|
+
this.inner(node.thenStatement, frame);
|
|
136
|
+
if (node.elseStatement) {
|
|
137
|
+
frame.emitter.reopen("} else {");
|
|
138
|
+
this.inner(node.elseStatement, frame);
|
|
139
|
+
}
|
|
140
|
+
frame.emitter.close();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** `return x satisfies never` marks an unreachable end, which returns the result type's default. */
|
|
144
|
+
private returnStatement(node: ts.ReturnStatement, frame: Frame): void {
|
|
145
|
+
if (!node.expression) {
|
|
146
|
+
frame.emitter.line("return;");
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (
|
|
150
|
+
ts.isSatisfiesExpression(node.expression) &&
|
|
151
|
+
this.isNever(node.expression.type)
|
|
152
|
+
) {
|
|
153
|
+
frame.emitter.line(
|
|
154
|
+
`return ${this.initializers.defaultOf(frame.returns)};`,
|
|
155
|
+
);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const value = this.expressions.expression(
|
|
159
|
+
node.expression,
|
|
160
|
+
frame,
|
|
161
|
+
frame.returns,
|
|
162
|
+
);
|
|
163
|
+
frame.emitter.line(
|
|
164
|
+
mapped`return ${this.dialect.code(value, frame.returns)};`,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** A try keeps its blocks, for the service calls in it. */
|
|
169
|
+
private tryStatement(node: ts.TryStatement, frame: Frame): void {
|
|
170
|
+
frame.emitter.open("try {");
|
|
171
|
+
for (const statement of node.tryBlock.statements) {
|
|
172
|
+
this.statement(statement, frame);
|
|
173
|
+
}
|
|
174
|
+
if (node.catchClause) {
|
|
175
|
+
const binding = node.catchClause.variableDeclaration?.name;
|
|
176
|
+
frame.emitter.reopen(
|
|
177
|
+
`} catch${binding ? ` (${binding.getText()})` : ""} {`,
|
|
178
|
+
);
|
|
179
|
+
// The binding holds the error code, which a catch block can only pass on as it is.
|
|
180
|
+
if (binding && ts.isIdentifier(binding)) {
|
|
181
|
+
frame.env.set(this.checker.getSymbolAtLocation(binding)!, {
|
|
182
|
+
kind: "code",
|
|
183
|
+
text: binding.text,
|
|
184
|
+
declared: this.checker.getTypeAtLocation(binding),
|
|
185
|
+
place: true,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
for (const statement of node.catchClause.block.statements) {
|
|
189
|
+
this.statement(statement, frame);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
frame.emitter.close();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A branch body: blocks keep their statements without another brace level. */
|
|
196
|
+
private inner(node: ts.Statement, frame: Frame): void {
|
|
197
|
+
if (ts.isBlock(node)) {
|
|
198
|
+
for (const statement of node.statements) {
|
|
199
|
+
this.statement(statement, frame);
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
this.statement(node, frame);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private isNever(type: ts.TypeNode): boolean {
|
|
207
|
+
return type.kind === ts.SyntaxKind.NeverKeyword;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private declare(
|
|
211
|
+
declaration: ts.VariableDeclaration,
|
|
212
|
+
mutable: boolean,
|
|
213
|
+
frame: Frame,
|
|
214
|
+
): void {
|
|
215
|
+
const initializer = declaration.initializer;
|
|
216
|
+
if (!initializer) {
|
|
217
|
+
throw new SpecializeError(
|
|
218
|
+
declaration,
|
|
219
|
+
"Initialize locals where they are declared.",
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
const value = this.expressions.expression(
|
|
223
|
+
initializer,
|
|
224
|
+
frame,
|
|
225
|
+
declaration.type && this.checker.getTypeAtLocation(declaration.type),
|
|
226
|
+
);
|
|
227
|
+
this.expressions.bind(
|
|
228
|
+
declaration.name,
|
|
229
|
+
value,
|
|
230
|
+
mutable,
|
|
231
|
+
frame,
|
|
232
|
+
this.checker.getTypeAtLocation(declaration.name),
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private forOf(node: ts.ForOfStatement, frame: Frame): void {
|
|
237
|
+
const call = node.expression;
|
|
238
|
+
if (
|
|
239
|
+
ts.isCallExpression(call) &&
|
|
240
|
+
ts.isPropertyAccessExpression(call.expression) &&
|
|
241
|
+
call.expression.name.text === "entries" &&
|
|
242
|
+
!call.arguments.length
|
|
243
|
+
) {
|
|
244
|
+
const array = this.expressions.expression(
|
|
245
|
+
call.expression.expression,
|
|
246
|
+
frame,
|
|
247
|
+
);
|
|
248
|
+
if (array.kind === "code") {
|
|
249
|
+
this.forEntries(node, array.text, frame);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const iterable = this.expressions.expression(node.expression, frame);
|
|
254
|
+
const items = this.expressions.items(iterable);
|
|
255
|
+
const declaration = onlyDeclaration(node.initializer);
|
|
256
|
+
if (!declaration) {
|
|
257
|
+
throw new SpecializeError(
|
|
258
|
+
node.initializer,
|
|
259
|
+
"Declare one const in for…of.",
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
const name = declaration.name;
|
|
263
|
+
const declared = this.checker.getTypeAtLocation(name);
|
|
264
|
+
if (items) {
|
|
265
|
+
for (const item of items) {
|
|
266
|
+
frame.emitter.open("{");
|
|
267
|
+
this.expressions.bind(name, item, false, frame, declared);
|
|
268
|
+
this.inner(node.statement, frame);
|
|
269
|
+
frame.emitter.close();
|
|
270
|
+
}
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (iterable.kind !== "code") {
|
|
274
|
+
throw new SpecializeError(node.expression, "for…of needs an array.");
|
|
275
|
+
}
|
|
276
|
+
if (!ts.isIdentifier(name)) {
|
|
277
|
+
throw new SpecializeError(
|
|
278
|
+
name,
|
|
279
|
+
"Destructure a runtime element inside the loop.",
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
const local = frame.emitter.local(name.text);
|
|
283
|
+
frame.emitter.open(mapped`for (const ${local} of ${iterable.text}) {`);
|
|
284
|
+
frame.env.set(this.checker.getSymbolAtLocation(name)!, {
|
|
285
|
+
kind: "code",
|
|
286
|
+
text: local,
|
|
287
|
+
declared,
|
|
288
|
+
place: true,
|
|
289
|
+
});
|
|
290
|
+
this.inner(node.statement, frame);
|
|
291
|
+
frame.emitter.close();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** `for (const [index, item] of array.entries())` over a runtime array stays a loop, as the backend lowers it. */
|
|
295
|
+
private forEntries(
|
|
296
|
+
node: ts.ForOfStatement,
|
|
297
|
+
array: string | Mapped,
|
|
298
|
+
frame: Frame,
|
|
299
|
+
): void {
|
|
300
|
+
const binding = onlyDeclaration(node.initializer)?.name;
|
|
301
|
+
const names =
|
|
302
|
+
binding && ts.isArrayBindingPattern(binding)
|
|
303
|
+
? binding.elements.map((element) =>
|
|
304
|
+
ts.isBindingElement(element) && ts.isIdentifier(element.name)
|
|
305
|
+
? element.name
|
|
306
|
+
: undefined,
|
|
307
|
+
)
|
|
308
|
+
: [];
|
|
309
|
+
const [indexName, itemName] = names;
|
|
310
|
+
if (names.length !== 2 || !indexName || !itemName) {
|
|
311
|
+
throw new SpecializeError(
|
|
312
|
+
node.initializer,
|
|
313
|
+
"Destructure [index, item] from array.entries().",
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
const [index, item] = [indexName, itemName].map((name) => {
|
|
317
|
+
const local = frame.emitter.local(name.text);
|
|
318
|
+
frame.env.set(this.checker.getSymbolAtLocation(name)!, {
|
|
319
|
+
kind: "code",
|
|
320
|
+
text: local,
|
|
321
|
+
declared: this.checker.getTypeAtLocation(name),
|
|
322
|
+
place: true,
|
|
323
|
+
});
|
|
324
|
+
return local;
|
|
325
|
+
});
|
|
326
|
+
frame.emitter.open(
|
|
327
|
+
mapped`for (const [${[index, item].join(", ")}] of ${array}.entries()) {`,
|
|
328
|
+
);
|
|
329
|
+
this.inner(node.statement, frame);
|
|
330
|
+
frame.emitter.close();
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** A counted loop stays a loop, in the header shape the backend proves bounded: `let i = start; i < bound; i++`. */
|
|
334
|
+
private forLoop(node: ts.ForStatement, frame: Frame): void {
|
|
335
|
+
const { condition, incrementor, initializer } = node;
|
|
336
|
+
const declaration = onlyDeclaration(initializer);
|
|
337
|
+
if (!declaration || !(declaration.parent.flags & ts.NodeFlags.Let)) {
|
|
338
|
+
throw new SpecializeError(
|
|
339
|
+
node,
|
|
340
|
+
"Declare one let counter in the for header.",
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
const name = declaration.name;
|
|
344
|
+
if (!ts.isIdentifier(name) || !declaration.initializer) {
|
|
345
|
+
throw new SpecializeError(declaration, "Initialize the loop counter.");
|
|
346
|
+
}
|
|
347
|
+
if (!condition || !ts.isBinaryExpression(condition) || !incrementor) {
|
|
348
|
+
throw new SpecializeError(
|
|
349
|
+
node,
|
|
350
|
+
"Compare the counter to a bound and step it in the for header.",
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
const declared = this.checker.getTypeAtLocation(name);
|
|
354
|
+
// An annotation such as UINT keeps the counter's width, to index alongside unsigned fields.
|
|
355
|
+
const iec = this.layouts.iecName(declaration);
|
|
356
|
+
const start = this.dialect.code(
|
|
357
|
+
this.expressions.expression(declaration.initializer, frame, declared),
|
|
358
|
+
declared,
|
|
359
|
+
);
|
|
360
|
+
const local = frame.emitter.local(name.text);
|
|
361
|
+
frame.env.set(this.checker.getSymbolAtLocation(name)!, {
|
|
362
|
+
kind: "code",
|
|
363
|
+
text: local,
|
|
364
|
+
declared,
|
|
365
|
+
place: true,
|
|
366
|
+
iec,
|
|
367
|
+
});
|
|
368
|
+
const test = mapped`${this.dialect.code(this.expressions.expression(condition.left, frame), declared)} ${condition.operatorToken.getText()} ${this.dialect.code(this.expressions.expression(condition.right, frame), declared)}`;
|
|
369
|
+
const step = this.loopStep(incrementor, local, declared, frame);
|
|
370
|
+
frame.emitter.open(
|
|
371
|
+
mapped`for (let ${local}: ${iec ?? this.expressions.localType(declared)} = ${start}; ${test}; ${step}) {`,
|
|
372
|
+
);
|
|
373
|
+
this.inner(node.statement, frame);
|
|
374
|
+
frame.emitter.close();
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** The step in a counted loop's header, as `i++` or `i += n` on the counter's local. */
|
|
378
|
+
private loopStep(
|
|
379
|
+
incrementor: ts.Expression,
|
|
380
|
+
local: string,
|
|
381
|
+
declared: ts.Type,
|
|
382
|
+
frame: Frame,
|
|
383
|
+
): Part {
|
|
384
|
+
if (
|
|
385
|
+
ts.isPrefixUnaryExpression(incrementor) ||
|
|
386
|
+
ts.isPostfixUnaryExpression(incrementor)
|
|
387
|
+
) {
|
|
388
|
+
return `${local}${ts.tokenToString(incrementor.operator)}`;
|
|
389
|
+
}
|
|
390
|
+
if (ts.isBinaryExpression(incrementor)) {
|
|
391
|
+
return mapped`${local} ${incrementor.operatorToken.getText()} ${this.dialect.code(this.expressions.expression(incrementor.right, frame), declared)}`;
|
|
392
|
+
}
|
|
393
|
+
throw new SpecializeError(
|
|
394
|
+
incrementor,
|
|
395
|
+
"Use i++, i--, i += step or i -= step in the for header.",
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
private switch(node: ts.SwitchStatement, frame: Frame): void {
|
|
400
|
+
const subject = this.expressions.expression(node.expression, frame);
|
|
401
|
+
const declared = subject.kind === "code" ? subject.declared : undefined;
|
|
402
|
+
if (subject.kind === "const") {
|
|
403
|
+
this.constantSwitch(node, subject, frame);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
frame.emitter.open(
|
|
407
|
+
mapped`switch (${this.dialect.code(subject, declared)}) {`,
|
|
408
|
+
);
|
|
409
|
+
for (const clause of node.caseBlock.clauses) {
|
|
410
|
+
if (ts.isDefaultClause(clause)) {
|
|
411
|
+
const unreachable = clause.statements.some(
|
|
412
|
+
(statement) =>
|
|
413
|
+
ts.isReturnStatement(statement) &&
|
|
414
|
+
statement.expression &&
|
|
415
|
+
ts.isSatisfiesExpression(statement.expression) &&
|
|
416
|
+
this.isNever(statement.expression.type),
|
|
417
|
+
);
|
|
418
|
+
frame.emitter.open("default: {");
|
|
419
|
+
if (unreachable) {
|
|
420
|
+
frame.emitter.line(
|
|
421
|
+
frame.returns && !(frame.returns.flags & ts.TypeFlags.Void)
|
|
422
|
+
? `return ${this.initializers.defaultOf(frame.returns)};`
|
|
423
|
+
: "break;",
|
|
424
|
+
);
|
|
425
|
+
} else {
|
|
426
|
+
for (const statement of clause.statements) {
|
|
427
|
+
this.statement(statement, frame);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
frame.emitter.close();
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
const label = this.expressions.expression(
|
|
434
|
+
clause.expression,
|
|
435
|
+
frame,
|
|
436
|
+
declared,
|
|
437
|
+
);
|
|
438
|
+
frame.emitter.open(mapped`case ${this.dialect.code(label, declared)}: {`);
|
|
439
|
+
for (const statement of clause.statements) {
|
|
440
|
+
this.statement(statement, frame);
|
|
441
|
+
}
|
|
442
|
+
frame.emitter.close();
|
|
443
|
+
}
|
|
444
|
+
frame.emitter.close();
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** A switch on a value setup fixed runs the clause it matches, falling through as JavaScript does. */
|
|
448
|
+
private constantSwitch(
|
|
449
|
+
node: ts.SwitchStatement,
|
|
450
|
+
subject: Value & { kind: "const" },
|
|
451
|
+
frame: Frame,
|
|
452
|
+
): void {
|
|
453
|
+
const clauses = node.caseBlock.clauses;
|
|
454
|
+
const start = clauses.findIndex((clause) => {
|
|
455
|
+
if (ts.isDefaultClause(clause)) {
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
const label = this.expressions.expression(clause.expression, frame);
|
|
459
|
+
return label.kind === "const" && label.value === subject.value;
|
|
460
|
+
});
|
|
461
|
+
if (start < 0) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
for (const clause of clauses.slice(start)) {
|
|
465
|
+
for (const statement of clause.statements) {
|
|
466
|
+
if (ts.isBreakStatement(statement)) {
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
this.statement(statement, frame);
|
|
470
|
+
}
|
|
471
|
+
if (
|
|
472
|
+
clause.statements.some((statement) => ts.isReturnStatement(statement))
|
|
473
|
+
) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/** An expression evaluated for its effect. */
|
|
480
|
+
private effect(node: ts.Expression, frame: Frame): void {
|
|
481
|
+
if (ts.isVoidExpression(node)) {
|
|
482
|
+
const value = this.expressions.expression(node.expression, frame);
|
|
483
|
+
if (value.kind === "code") {
|
|
484
|
+
frame.emitter.line(mapped`void ${value.text};`);
|
|
485
|
+
}
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
if (ts.isBinaryExpression(node) && isAssignment(node.operatorToken.kind)) {
|
|
489
|
+
this.assign(node, frame);
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
if (isUpdate(node)) {
|
|
493
|
+
const target = this.expressions.expression(node.operand, frame);
|
|
494
|
+
const text = this.placeText(target, node.operand);
|
|
495
|
+
frame.emitter.line(
|
|
496
|
+
this.store(
|
|
497
|
+
target,
|
|
498
|
+
mapped`${text} ${node.operator === ts.SyntaxKind.PlusPlusToken ? "+" : "-"} 1`,
|
|
499
|
+
),
|
|
500
|
+
);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const value = this.expressions.expression(node, frame);
|
|
504
|
+
if (value.kind === "code" && String(value.text).includes("(")) {
|
|
505
|
+
frame.emitter.line(mapped`${value.text};`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
private assign(node: ts.BinaryExpression, frame: Frame): void {
|
|
510
|
+
const target = this.expressions.expression(node.left, frame);
|
|
511
|
+
const text = this.placeText(target, node.left);
|
|
512
|
+
const declared = target.kind === "code" ? target.declared : undefined;
|
|
513
|
+
const operator = node.operatorToken.kind;
|
|
514
|
+
if (operator === ts.SyntaxKind.QuestionQuestionEqualsToken) {
|
|
515
|
+
const value = this.expressions.expression(
|
|
516
|
+
node.right,
|
|
517
|
+
frame,
|
|
518
|
+
declared && this.checker.getNonNullableType(declared),
|
|
519
|
+
);
|
|
520
|
+
frame.emitter.open(
|
|
521
|
+
mapped`if (${this.dialect.isUndefinedText(text, declared)}) {`,
|
|
522
|
+
);
|
|
523
|
+
frame.emitter.line(
|
|
524
|
+
this.store(
|
|
525
|
+
target,
|
|
526
|
+
this.dialect.wrapDefined(
|
|
527
|
+
this.dialect.code(
|
|
528
|
+
value,
|
|
529
|
+
declared && this.checker.getNonNullableType(declared),
|
|
530
|
+
),
|
|
531
|
+
declared,
|
|
532
|
+
),
|
|
533
|
+
),
|
|
534
|
+
);
|
|
535
|
+
frame.emitter.close();
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
const value = this.expressions.expression(node.right, frame, declared);
|
|
539
|
+
if (operator === ts.SyntaxKind.EqualsToken) {
|
|
540
|
+
frame.emitter.line(
|
|
541
|
+
this.store(
|
|
542
|
+
target,
|
|
543
|
+
this.dialect.assignmentText(
|
|
544
|
+
value,
|
|
545
|
+
declared,
|
|
546
|
+
target.kind === "code" ? target.iec : undefined,
|
|
547
|
+
),
|
|
548
|
+
),
|
|
549
|
+
);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
const binary = compoundOperators.get(operator);
|
|
553
|
+
if (!binary) {
|
|
554
|
+
throw new SpecializeError(
|
|
555
|
+
node,
|
|
556
|
+
`${node.operatorToken.getText()} is not supported.`,
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
frame.emitter.line(
|
|
560
|
+
this.store(
|
|
561
|
+
target,
|
|
562
|
+
mapped`${text} ${binary} ${this.dialect.code(value, declared)}`,
|
|
563
|
+
),
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** A statement storing `text` into a place, through its module's setter when it has one. */
|
|
568
|
+
private store(target: Value, text: Part): Mapped {
|
|
569
|
+
if (target.kind !== "code") {
|
|
570
|
+
throw new Error("Not a place.");
|
|
571
|
+
}
|
|
572
|
+
return target.setter
|
|
573
|
+
? mapped`${target.setter}(${text});`
|
|
574
|
+
: mapped`${target.text} = ${text};`;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
private placeText(value: Value, node: ts.Node): string | Mapped {
|
|
578
|
+
if (value.kind === "code" && value.place) {
|
|
579
|
+
return value.text;
|
|
580
|
+
}
|
|
581
|
+
throw new SpecializeError(
|
|
582
|
+
node,
|
|
583
|
+
"Assign to stored state: a field scan code owns, or a let.",
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** The operator each compound assignment applies. */
|
|
589
|
+
const compoundOperators = new Map<ts.SyntaxKind, string>([
|
|
590
|
+
[ts.SyntaxKind.PlusEqualsToken, "+"],
|
|
591
|
+
[ts.SyntaxKind.MinusEqualsToken, "-"],
|
|
592
|
+
[ts.SyntaxKind.AsteriskEqualsToken, "*"],
|
|
593
|
+
[ts.SyntaxKind.BarEqualsToken, "|"],
|
|
594
|
+
[ts.SyntaxKind.AmpersandEqualsToken, "&"],
|
|
595
|
+
]);
|
|
596
|
+
|
|
597
|
+
/** The one variable a loop header declares; undefined when it declares none or several. */
|
|
598
|
+
const onlyDeclaration = (
|
|
599
|
+
initializer: ts.ForInitializer | undefined,
|
|
600
|
+
): ts.VariableDeclaration | undefined => {
|
|
601
|
+
if (!initializer || !ts.isVariableDeclarationList(initializer)) {
|
|
602
|
+
return undefined;
|
|
603
|
+
}
|
|
604
|
+
const [declaration, ...others] = initializer.declarations;
|
|
605
|
+
return others.length ? undefined : declaration;
|
|
606
|
+
};
|