@tsplc/compiler 0.2.17 → 0.2.18
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,855 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import {
|
|
3
|
+
aggregate,
|
|
4
|
+
integerRange,
|
|
5
|
+
loopBounds,
|
|
6
|
+
loopIterations,
|
|
7
|
+
maxLoopIterations,
|
|
8
|
+
type Constant,
|
|
9
|
+
type Expression,
|
|
10
|
+
type IntegerInterval,
|
|
11
|
+
type PlcFunction,
|
|
12
|
+
type PlcType,
|
|
13
|
+
type Statement,
|
|
14
|
+
type Variable,
|
|
15
|
+
} from "@tsplc/compiler/ir";
|
|
16
|
+
import type { Positions } from "./positions.ts";
|
|
17
|
+
import type { AnalysisContext, ExpressionLowering } from "./expressions.ts";
|
|
18
|
+
import { telemetryStatement } from "./telemetry.ts";
|
|
19
|
+
import { numericLiteral, type Fail, type TypeResolver } from "./types.ts";
|
|
20
|
+
|
|
21
|
+
type IterationForm = "elements" | "entries" | "recordValues" | "recordEntries";
|
|
22
|
+
|
|
23
|
+
/** The counter a for header initializes, and where the header stands. */
|
|
24
|
+
interface LoopHeader {
|
|
25
|
+
header: ts.ForInitializer;
|
|
26
|
+
counter: Variable;
|
|
27
|
+
initializer: ts.Expression;
|
|
28
|
+
existing: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Lowers one routine's statements. Expressions, and the scope facts that
|
|
33
|
+
* declarations, branches and loops establish, go through its ExpressionLowering.
|
|
34
|
+
*/
|
|
35
|
+
export class StatementLowering {
|
|
36
|
+
private readonly resolver: TypeResolver;
|
|
37
|
+
private readonly positions: Positions;
|
|
38
|
+
private readonly fail: Fail;
|
|
39
|
+
private readonly breakTargets: ("loop" | "switch")[] = [];
|
|
40
|
+
private readonly locals: Variable[] = [];
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
context: AnalysisContext,
|
|
44
|
+
private readonly expressions: ExpressionLowering,
|
|
45
|
+
private readonly routine: PlcFunction,
|
|
46
|
+
) {
|
|
47
|
+
this.resolver = context.resolver;
|
|
48
|
+
this.positions = context.positions;
|
|
49
|
+
this.fail = context.fail;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Lowers a routine's statements, collecting the locals they declare. */
|
|
53
|
+
lowerRoutine(body: ts.Block): { locals: Variable[]; body: Statement[] } {
|
|
54
|
+
return { locals: this.locals, body: this.statements(body.statements) };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private statements(nodes: readonly ts.Statement[]): Statement[] {
|
|
58
|
+
return nodes.flatMap((node) => this.statement(node));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private body(node: ts.Statement): Statement[] {
|
|
62
|
+
return ts.isBlock(node)
|
|
63
|
+
? this.statements(node.statements)
|
|
64
|
+
: this.statement(node);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private statement(node: ts.Statement): Statement[] {
|
|
68
|
+
const origin = { span: this.positions.node(node) };
|
|
69
|
+
if (ts.isBlock(node)) {
|
|
70
|
+
return this.statements(node.statements);
|
|
71
|
+
}
|
|
72
|
+
if (ts.isEmptyStatement(node)) {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
if (ts.isIfStatement(node)) {
|
|
76
|
+
const condition = this.expressions.expression(node.expression, "BOOL");
|
|
77
|
+
const branch = (body: ts.Statement | undefined, truth: boolean) =>
|
|
78
|
+
this.expressions.narrowed(condition, truth, () =>
|
|
79
|
+
body ? this.body(body) : [],
|
|
80
|
+
);
|
|
81
|
+
return [
|
|
82
|
+
{
|
|
83
|
+
...origin,
|
|
84
|
+
kind: "if",
|
|
85
|
+
condition,
|
|
86
|
+
then: branch(node.thenStatement, true),
|
|
87
|
+
else: branch(node.elseStatement, false),
|
|
88
|
+
},
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
if (ts.isReturnStatement(node)) {
|
|
92
|
+
const result = this.routine.result;
|
|
93
|
+
if (!result && node.expression) {
|
|
94
|
+
this.fail(
|
|
95
|
+
node,
|
|
96
|
+
"PLC2001",
|
|
97
|
+
"Void helpers and program entries cannot return a value.",
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
if (result && !node.expression) {
|
|
101
|
+
this.fail(
|
|
102
|
+
node,
|
|
103
|
+
"PLC2001",
|
|
104
|
+
"Typed helpers must return their declared PLC result.",
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
const value =
|
|
108
|
+
node.expression && result
|
|
109
|
+
? this.expressions.implicitCopy(
|
|
110
|
+
this.expressions.expression(node.expression, result),
|
|
111
|
+
)
|
|
112
|
+
: undefined;
|
|
113
|
+
return [{ ...origin, kind: "return", ...(value && { value }) }];
|
|
114
|
+
}
|
|
115
|
+
if (ts.isBreakStatement(node) || ts.isContinueStatement(node)) {
|
|
116
|
+
if (ts.isBreakStatement(node) && this.breakTargets.at(-1) === "switch") {
|
|
117
|
+
this.fail(
|
|
118
|
+
node,
|
|
119
|
+
"PLC1001",
|
|
120
|
+
"Switch break must terminate its case; nested conditional switch breaks are unsupported.",
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
if (node.label || !this.breakTargets.includes("loop")) {
|
|
124
|
+
this.fail(
|
|
125
|
+
node,
|
|
126
|
+
"PLC1001",
|
|
127
|
+
"Loop control requires an enclosing bounded loop and cannot use a label.",
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return [
|
|
131
|
+
{ ...origin, kind: ts.isBreakStatement(node) ? "break" : "continue" },
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
if (ts.isVariableStatement(node)) {
|
|
135
|
+
return this.localDeclarations(node);
|
|
136
|
+
}
|
|
137
|
+
if (ts.isForStatement(node)) {
|
|
138
|
+
return this.loop(node);
|
|
139
|
+
}
|
|
140
|
+
if (ts.isForOfStatement(node)) {
|
|
141
|
+
return this.forOf(node);
|
|
142
|
+
}
|
|
143
|
+
if (ts.isSwitchStatement(node)) {
|
|
144
|
+
return this.switchStatement(node);
|
|
145
|
+
}
|
|
146
|
+
if (ts.isExpressionStatement(node)) {
|
|
147
|
+
const expression = node.expression;
|
|
148
|
+
if (ts.isCallExpression(expression)) {
|
|
149
|
+
const telemetry = telemetryStatement(this.expressions, expression);
|
|
150
|
+
if (telemetry) {
|
|
151
|
+
return [telemetry];
|
|
152
|
+
}
|
|
153
|
+
const update = this.expressions.update(expression);
|
|
154
|
+
if (update) {
|
|
155
|
+
return [update];
|
|
156
|
+
}
|
|
157
|
+
const call = this.expressions.call(expression);
|
|
158
|
+
return [
|
|
159
|
+
{
|
|
160
|
+
...origin,
|
|
161
|
+
kind: "invoke",
|
|
162
|
+
function: call.fn.id,
|
|
163
|
+
arguments: call.arguments,
|
|
164
|
+
...(call.instance && { instance: call.instance }),
|
|
165
|
+
...(call.index && { index: call.index }),
|
|
166
|
+
},
|
|
167
|
+
];
|
|
168
|
+
}
|
|
169
|
+
if (ts.isBinaryExpression(expression)) {
|
|
170
|
+
const variable = this.expressions.target(expression.left);
|
|
171
|
+
if (expression.operatorToken.kind !== ts.SyntaxKind.EqualsToken) {
|
|
172
|
+
this.fail(
|
|
173
|
+
expression,
|
|
174
|
+
"PLC2001",
|
|
175
|
+
"Use explicit assignment and arithmetic policies.",
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
const value = this.expressions.expression(
|
|
179
|
+
expression.right,
|
|
180
|
+
variable.type,
|
|
181
|
+
);
|
|
182
|
+
this.expressions.recordCopySource(value);
|
|
183
|
+
return [
|
|
184
|
+
{
|
|
185
|
+
...origin,
|
|
186
|
+
kind: "assign",
|
|
187
|
+
destination: variable.place,
|
|
188
|
+
value: this.expressions.implicitCopy(value),
|
|
189
|
+
},
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
this.fail(
|
|
194
|
+
node,
|
|
195
|
+
"PLC1001",
|
|
196
|
+
`${ts.SyntaxKind[node.kind]} is not supported in PLC scan code.`,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** A routine-local variable, declared where `declaration` stands. */
|
|
201
|
+
private local(
|
|
202
|
+
declaration: ts.Node,
|
|
203
|
+
name: string,
|
|
204
|
+
type: string,
|
|
205
|
+
{ mutable }: { mutable: boolean },
|
|
206
|
+
): Variable {
|
|
207
|
+
const variable: Variable = {
|
|
208
|
+
id: this.expressions.storageId(declaration, name),
|
|
209
|
+
name,
|
|
210
|
+
type,
|
|
211
|
+
storage: "local",
|
|
212
|
+
mutable,
|
|
213
|
+
retain: false,
|
|
214
|
+
span: this.positions.node(declaration),
|
|
215
|
+
};
|
|
216
|
+
this.locals.push(variable);
|
|
217
|
+
return variable;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Grouped labels share the body of the clause that ends them; a default label's body is the fallback. */
|
|
221
|
+
private switchStatement(node: ts.SwitchStatement): Statement[] {
|
|
222
|
+
const origin = { span: this.positions.node(node) };
|
|
223
|
+
this.breakTargets.push("switch");
|
|
224
|
+
try {
|
|
225
|
+
const value = this.expressions.expression(node.expression);
|
|
226
|
+
const cases: { values: Constant[]; body: Statement[] }[] = [];
|
|
227
|
+
const labels: Constant[] = [];
|
|
228
|
+
let fallback: Statement[] = [];
|
|
229
|
+
let defaultLabel = false;
|
|
230
|
+
for (const clause of node.caseBlock.clauses) {
|
|
231
|
+
if (ts.isDefaultClause(clause)) {
|
|
232
|
+
defaultLabel = true;
|
|
233
|
+
} else {
|
|
234
|
+
labels.push(this.caseValue(clause, value.type));
|
|
235
|
+
}
|
|
236
|
+
if (!clause.statements.length) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
const body = this.caseBody(clause);
|
|
240
|
+
if (labels.length) {
|
|
241
|
+
cases.push({ values: labels.splice(0), body });
|
|
242
|
+
}
|
|
243
|
+
if (defaultLabel) {
|
|
244
|
+
fallback = body;
|
|
245
|
+
}
|
|
246
|
+
defaultLabel = false;
|
|
247
|
+
}
|
|
248
|
+
if (labels.length) {
|
|
249
|
+
cases.push({ values: labels, body: [] });
|
|
250
|
+
}
|
|
251
|
+
return [{ ...origin, kind: "switch", value, cases, fallback }];
|
|
252
|
+
} finally {
|
|
253
|
+
this.breakTargets.pop();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private caseValue(clause: ts.CaseClause, type: string): Constant {
|
|
258
|
+
const constant = this.expressions.constant(
|
|
259
|
+
this.expressions.expression(clause.expression, type),
|
|
260
|
+
);
|
|
261
|
+
if (!constant) {
|
|
262
|
+
this.fail(
|
|
263
|
+
clause,
|
|
264
|
+
"PLC2001",
|
|
265
|
+
"Case values must be compile-time constants.",
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
return constant;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** A case's statements without the break that ends it. */
|
|
272
|
+
private caseBody(clause: ts.CaseOrDefaultClause): Statement[] {
|
|
273
|
+
const last = clause.statements.at(-1);
|
|
274
|
+
if (last && ts.isBreakStatement(last) && !last.label) {
|
|
275
|
+
return this.statements(clause.statements.slice(0, -1));
|
|
276
|
+
}
|
|
277
|
+
if (!(last && ts.isReturnStatement(last))) {
|
|
278
|
+
this.fail(
|
|
279
|
+
clause,
|
|
280
|
+
"PLC1001",
|
|
281
|
+
"Switch cases must end with break or return; fallthrough is prohibited.",
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
return this.statements(clause.statements);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private localDeclarations(node: ts.VariableStatement): Statement[] {
|
|
288
|
+
const origin = { span: this.positions.node(node) };
|
|
289
|
+
if (
|
|
290
|
+
!(node.declarationList.flags & (ts.NodeFlags.Let | ts.NodeFlags.Const))
|
|
291
|
+
) {
|
|
292
|
+
this.fail(node, "PLC1001", "Use let or const for invocation locals.");
|
|
293
|
+
}
|
|
294
|
+
const immutable = Boolean(node.declarationList.flags & ts.NodeFlags.Const);
|
|
295
|
+
return node.declarationList.declarations.flatMap((declaration) => {
|
|
296
|
+
if (!ts.isIdentifier(declaration.name)) {
|
|
297
|
+
this.fail(declaration, "PLC1001", "Destructuring is not supported.");
|
|
298
|
+
}
|
|
299
|
+
if (!declaration.initializer) {
|
|
300
|
+
this.fail(
|
|
301
|
+
declaration,
|
|
302
|
+
"PLC3001",
|
|
303
|
+
"Initialize invocation-local PLC variables explicitly.",
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const annotation = declaration.type
|
|
307
|
+
? this.expressions.type(declaration.type)
|
|
308
|
+
: undefined;
|
|
309
|
+
if (annotation && !this.resolver.isValue(annotation)) {
|
|
310
|
+
this.fail(
|
|
311
|
+
declaration,
|
|
312
|
+
"PLC2001",
|
|
313
|
+
"Invocation locals require value types, not instances or borrow schemas.",
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
if (annotation?.io) {
|
|
317
|
+
this.fail(
|
|
318
|
+
declaration,
|
|
319
|
+
"PLC4002",
|
|
320
|
+
"Logical I/O cannot be invocation-local.",
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
if (
|
|
324
|
+
!annotation &&
|
|
325
|
+
!immutable &&
|
|
326
|
+
numericLiteral(declaration.initializer) === undefined &&
|
|
327
|
+
![ts.SyntaxKind.TrueKeyword, ts.SyntaxKind.FalseKeyword].includes(
|
|
328
|
+
declaration.initializer.kind,
|
|
329
|
+
)
|
|
330
|
+
) {
|
|
331
|
+
this.fail(
|
|
332
|
+
declaration,
|
|
333
|
+
"PLC2001",
|
|
334
|
+
"Annotate mutable numeric storage explicitly.",
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
const initial = this.expressions.expression(
|
|
338
|
+
declaration.initializer,
|
|
339
|
+
annotation?.type,
|
|
340
|
+
);
|
|
341
|
+
if (
|
|
342
|
+
immutable &&
|
|
343
|
+
initial.kind === "load" &&
|
|
344
|
+
aggregate(this.resolver.types.get(initial.type))
|
|
345
|
+
) {
|
|
346
|
+
for (const segment of initial.place.path) {
|
|
347
|
+
if (
|
|
348
|
+
segment.kind === "index" &&
|
|
349
|
+
!this.expressions.unchanging(segment.value)
|
|
350
|
+
) {
|
|
351
|
+
this.fail(
|
|
352
|
+
declaration,
|
|
353
|
+
"PLC3003",
|
|
354
|
+
"An alias index must keep its value while the alias is in scope; use copy(value) for a snapshot.",
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
this.expressions.alias(
|
|
359
|
+
this.resolver.symbol(declaration.name),
|
|
360
|
+
initial.place,
|
|
361
|
+
);
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
this.expressions.recordCopySource(initial);
|
|
365
|
+
const value = this.expressions.implicitCopy(initial);
|
|
366
|
+
const variable = this.local(
|
|
367
|
+
declaration,
|
|
368
|
+
declaration.name.text,
|
|
369
|
+
value.type,
|
|
370
|
+
{ mutable: !immutable },
|
|
371
|
+
);
|
|
372
|
+
this.expressions.bind(this.resolver.symbol(declaration.name), variable);
|
|
373
|
+
// A const is assigned once, so its initializer's range holds wherever it is read.
|
|
374
|
+
if (!variable.mutable) {
|
|
375
|
+
this.expressions.holdRange(variable.id, value);
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
...origin,
|
|
379
|
+
kind: "assign",
|
|
380
|
+
destination: { root: variable.id, path: [] },
|
|
381
|
+
value,
|
|
382
|
+
};
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
private loop(node: ts.ForStatement): Statement[] {
|
|
387
|
+
const { header, counter, initializer, existing } = this.loopHeader(node);
|
|
388
|
+
const type = counter.type;
|
|
389
|
+
const plcType = this.resolver.types.get(type);
|
|
390
|
+
if (plcType?.kind !== "integer") {
|
|
391
|
+
this.fail(header, "PLC2001", "Loop counters require integer types.");
|
|
392
|
+
}
|
|
393
|
+
const startExpression = this.expressions.expression(initializer, type);
|
|
394
|
+
const start = this.expressions.constant(startExpression);
|
|
395
|
+
const isCounter = (expression: ts.Expression): boolean => {
|
|
396
|
+
const place = this.expressions.place(expression);
|
|
397
|
+
return place.root === counter.id && place.path.length === 0;
|
|
398
|
+
};
|
|
399
|
+
const condition = node.condition;
|
|
400
|
+
if (
|
|
401
|
+
!condition ||
|
|
402
|
+
!ts.isBinaryExpression(condition) ||
|
|
403
|
+
!isCounter(condition.left)
|
|
404
|
+
) {
|
|
405
|
+
this.fail(
|
|
406
|
+
node,
|
|
407
|
+
"PLC1001",
|
|
408
|
+
"Loop condition must compare its counter to a bounded integer.",
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
const boundExpression = this.expressions.expression(condition.right, type);
|
|
412
|
+
const bound = this.expressions.constant(boundExpression);
|
|
413
|
+
const step = this.loopStep(node, type, isCounter);
|
|
414
|
+
const operator = condition.operatorToken.kind;
|
|
415
|
+
const ascending = step > 0n;
|
|
416
|
+
const comparisons = ascending
|
|
417
|
+
? [ts.SyntaxKind.LessThanToken, ts.SyntaxKind.LessThanEqualsToken]
|
|
418
|
+
: [ts.SyntaxKind.GreaterThanToken, ts.SyntaxKind.GreaterThanEqualsToken];
|
|
419
|
+
if (!comparisons.includes(operator)) {
|
|
420
|
+
this.fail(
|
|
421
|
+
condition,
|
|
422
|
+
"PLC2001",
|
|
423
|
+
"Loop direction and comparison must agree.",
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
const exclusive =
|
|
427
|
+
operator === ts.SyntaxKind.LessThanToken ||
|
|
428
|
+
operator === ts.SyntaxKind.GreaterThanToken;
|
|
429
|
+
if (start?.kind !== "integer" || bound?.kind !== "integer") {
|
|
430
|
+
const startRange = this.expressions.interval(startExpression);
|
|
431
|
+
const limitRange = this.expressions.interval(boundExpression);
|
|
432
|
+
if (!startRange || !limitRange) {
|
|
433
|
+
this.fail(
|
|
434
|
+
node,
|
|
435
|
+
"PLC2001",
|
|
436
|
+
"Loop bounds require finite integer ranges.",
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
const proof = loopBounds(startRange, limitRange, step, exclusive);
|
|
440
|
+
this.checkIterations(node, proof.count);
|
|
441
|
+
if (existing && proof.count > 0n) {
|
|
442
|
+
this.checkFinalIncrement(
|
|
443
|
+
node,
|
|
444
|
+
plcType,
|
|
445
|
+
(step > 0n ? proof.range[1] : proof.range[0]) + step,
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
return this.inLoop([counter.id], counter.id, proof.range, () => [
|
|
449
|
+
{
|
|
450
|
+
span: this.positions.node(node),
|
|
451
|
+
kind: "for",
|
|
452
|
+
counter: counter.id,
|
|
453
|
+
...(existing && { counterScope: "existing" as const }),
|
|
454
|
+
start: startExpression,
|
|
455
|
+
limit: boundExpression,
|
|
456
|
+
step: String(step),
|
|
457
|
+
...(exclusive && { exclusive: true as const }),
|
|
458
|
+
maxIterations: Number(proof.count),
|
|
459
|
+
body: this.body(node.statement),
|
|
460
|
+
},
|
|
461
|
+
]);
|
|
462
|
+
}
|
|
463
|
+
// A constant exclusive bound becomes the inclusive limit one unit inside it.
|
|
464
|
+
const direction = ascending ? 1n : -1n;
|
|
465
|
+
const finish = BigInt(bound.value) - (exclusive ? direction : 0n);
|
|
466
|
+
const first = BigInt(start.value);
|
|
467
|
+
const count = loopIterations(first, finish, step);
|
|
468
|
+
this.checkIterations(node, count);
|
|
469
|
+
if (existing) {
|
|
470
|
+
this.checkFinalIncrement(node, plcType, first + count * step);
|
|
471
|
+
}
|
|
472
|
+
const body = this.inLoop(
|
|
473
|
+
[counter.id],
|
|
474
|
+
counter.id,
|
|
475
|
+
first <= finish ? [first, finish] : [finish, first],
|
|
476
|
+
() => this.body(node.statement),
|
|
477
|
+
);
|
|
478
|
+
const startConstant: Expression = {
|
|
479
|
+
kind: "constant",
|
|
480
|
+
type,
|
|
481
|
+
value: start,
|
|
482
|
+
span: this.positions.node(initializer),
|
|
483
|
+
};
|
|
484
|
+
if (!count) {
|
|
485
|
+
// The body never runs, but an existing counter still takes its start value.
|
|
486
|
+
return existing
|
|
487
|
+
? [
|
|
488
|
+
{
|
|
489
|
+
kind: "assign",
|
|
490
|
+
destination: { root: counter.id, path: [] },
|
|
491
|
+
value: startConstant,
|
|
492
|
+
span: this.positions.node(header),
|
|
493
|
+
},
|
|
494
|
+
]
|
|
495
|
+
: [];
|
|
496
|
+
}
|
|
497
|
+
return [
|
|
498
|
+
{
|
|
499
|
+
span: this.positions.node(node),
|
|
500
|
+
kind: "for",
|
|
501
|
+
counter: counter.id,
|
|
502
|
+
...(existing && { counterScope: "existing" as const }),
|
|
503
|
+
start: startConstant,
|
|
504
|
+
limit: {
|
|
505
|
+
kind: "constant",
|
|
506
|
+
type,
|
|
507
|
+
value: { kind: "integer", value: String(finish) },
|
|
508
|
+
span: this.positions.node(condition.right),
|
|
509
|
+
},
|
|
510
|
+
step: String(step),
|
|
511
|
+
maxIterations: Number(count),
|
|
512
|
+
body,
|
|
513
|
+
},
|
|
514
|
+
];
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** A new `let` counter, or an assigned local, module variable or private block field. */
|
|
518
|
+
private loopHeader(node: ts.ForStatement): LoopHeader {
|
|
519
|
+
const header = node.initializer;
|
|
520
|
+
if (header && ts.isVariableDeclarationList(header)) {
|
|
521
|
+
const [declaration, ...others] = header.declarations;
|
|
522
|
+
if (!(header.flags & ts.NodeFlags.Let) || !declaration || others.length) {
|
|
523
|
+
this.fail(
|
|
524
|
+
node,
|
|
525
|
+
"PLC1001",
|
|
526
|
+
"A bounded for loop requires one let counter or an existing counter assignment.",
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
if (!ts.isIdentifier(declaration.name) || !declaration.initializer) {
|
|
530
|
+
this.fail(node, "PLC2001", "Initialize the integer loop counter.");
|
|
531
|
+
}
|
|
532
|
+
const annotation = declaration.type
|
|
533
|
+
? this.expressions.type(declaration.type)
|
|
534
|
+
: { type: "DINT" };
|
|
535
|
+
if (annotation.io) {
|
|
536
|
+
this.fail(
|
|
537
|
+
declaration,
|
|
538
|
+
"PLC4002",
|
|
539
|
+
"Loop counters cannot use I/O wrappers.",
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
const counter = this.local(
|
|
543
|
+
declaration,
|
|
544
|
+
declaration.name.text,
|
|
545
|
+
annotation.type,
|
|
546
|
+
{ mutable: true },
|
|
547
|
+
);
|
|
548
|
+
this.expressions.bind(this.resolver.symbol(declaration.name), counter);
|
|
549
|
+
return {
|
|
550
|
+
header,
|
|
551
|
+
counter,
|
|
552
|
+
initializer: declaration.initializer,
|
|
553
|
+
existing: false,
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
if (
|
|
557
|
+
header &&
|
|
558
|
+
ts.isBinaryExpression(header) &&
|
|
559
|
+
header.operatorToken.kind === ts.SyntaxKind.EqualsToken
|
|
560
|
+
) {
|
|
561
|
+
const target = this.expressions.target(header.left);
|
|
562
|
+
const counter = this.expressions.byId().get(target.place.root)!;
|
|
563
|
+
if (
|
|
564
|
+
target.place.path.length ||
|
|
565
|
+
!["local", "module", "field"].includes(counter.storage)
|
|
566
|
+
) {
|
|
567
|
+
this.fail(
|
|
568
|
+
header,
|
|
569
|
+
"PLC1001",
|
|
570
|
+
"An existing loop counter must be a local, module variable or private block field.",
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
return { header, counter, initializer: header.right, existing: true };
|
|
574
|
+
}
|
|
575
|
+
this.fail(
|
|
576
|
+
node,
|
|
577
|
+
"PLC1001",
|
|
578
|
+
"Initialize one let counter or assign an existing counter in the for header.",
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/** The header's constant step: `i++`, `i--`, `i += step` or `i -= step`. */
|
|
583
|
+
private loopStep(
|
|
584
|
+
node: ts.ForStatement,
|
|
585
|
+
type: string,
|
|
586
|
+
isCounter: (expression: ts.Expression) => boolean,
|
|
587
|
+
): bigint {
|
|
588
|
+
const increment = node.incrementor;
|
|
589
|
+
if (
|
|
590
|
+
increment &&
|
|
591
|
+
(ts.isPostfixUnaryExpression(increment) ||
|
|
592
|
+
ts.isPrefixUnaryExpression(increment)) &&
|
|
593
|
+
isCounter(increment.operand) &&
|
|
594
|
+
[ts.SyntaxKind.PlusPlusToken, ts.SyntaxKind.MinusMinusToken].includes(
|
|
595
|
+
increment.operator,
|
|
596
|
+
)
|
|
597
|
+
) {
|
|
598
|
+
return increment.operator === ts.SyntaxKind.PlusPlusToken ? 1n : -1n;
|
|
599
|
+
}
|
|
600
|
+
if (
|
|
601
|
+
increment &&
|
|
602
|
+
ts.isBinaryExpression(increment) &&
|
|
603
|
+
isCounter(increment.left) &&
|
|
604
|
+
[ts.SyntaxKind.PlusEqualsToken, ts.SyntaxKind.MinusEqualsToken].includes(
|
|
605
|
+
increment.operatorToken.kind,
|
|
606
|
+
)
|
|
607
|
+
) {
|
|
608
|
+
const amount = this.expressions.constant(
|
|
609
|
+
this.expressions.expression(increment.right, type),
|
|
610
|
+
);
|
|
611
|
+
if (amount?.kind !== "integer" || BigInt(amount.value) <= 0n) {
|
|
612
|
+
this.fail(
|
|
613
|
+
increment,
|
|
614
|
+
"PLC2001",
|
|
615
|
+
"Loop step must be a positive constant integer.",
|
|
616
|
+
);
|
|
617
|
+
}
|
|
618
|
+
const magnitude = BigInt(amount.value);
|
|
619
|
+
return increment.operatorToken.kind === ts.SyntaxKind.PlusEqualsToken
|
|
620
|
+
? magnitude
|
|
621
|
+
: -magnitude;
|
|
622
|
+
}
|
|
623
|
+
this.fail(
|
|
624
|
+
node,
|
|
625
|
+
"PLC1001",
|
|
626
|
+
"Use i++, i--, i += step or i -= step in the for header.",
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
private checkIterations(node: ts.ForStatement, count: bigint): void {
|
|
631
|
+
if (count > maxLoopIterations) {
|
|
632
|
+
this.fail(
|
|
633
|
+
node,
|
|
634
|
+
"PLC2001",
|
|
635
|
+
"A scan loop cannot exceed 1,000,000 iterations.",
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/** An existing counter keeps its value after the loop, so its final increment must fit. */
|
|
641
|
+
private checkFinalIncrement(
|
|
642
|
+
node: ts.ForStatement,
|
|
643
|
+
type: Extract<PlcType, { kind: "integer" }>,
|
|
644
|
+
terminal: bigint,
|
|
645
|
+
): void {
|
|
646
|
+
const [minimum, maximum] = integerRange(type);
|
|
647
|
+
if (terminal < minimum || terminal > maximum) {
|
|
648
|
+
this.fail(
|
|
649
|
+
node,
|
|
650
|
+
"PLC2001",
|
|
651
|
+
"The existing loop counter would overflow on its final increment.",
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* `for (const element of array)` is a bounded FOR over the array's declared bounds whose
|
|
658
|
+
* element binding aliases `array[counter]`. `array.entries()` also binds the counter. An
|
|
659
|
+
* enum-keyed Record iterates with `Object.values(record)`, or SDK `entries(record)` for its key.
|
|
660
|
+
*/
|
|
661
|
+
private forOf(node: ts.ForOfStatement): Statement[] {
|
|
662
|
+
const list = node.initializer;
|
|
663
|
+
const [declaration, ...others] = ts.isVariableDeclarationList(list)
|
|
664
|
+
? list.declarations
|
|
665
|
+
: [];
|
|
666
|
+
if (
|
|
667
|
+
node.awaitModifier ||
|
|
668
|
+
!ts.isVariableDeclarationList(list) ||
|
|
669
|
+
!(list.flags & ts.NodeFlags.Const) ||
|
|
670
|
+
!declaration ||
|
|
671
|
+
others.length
|
|
672
|
+
) {
|
|
673
|
+
this.fail(
|
|
674
|
+
node,
|
|
675
|
+
"PLC1001",
|
|
676
|
+
"for…of binds one const element of a PLC array.",
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
const { form, iterated } = this.iteration(node.expression);
|
|
680
|
+
const binding = declaration.name;
|
|
681
|
+
const keyed = form === "entries" || form === "recordEntries";
|
|
682
|
+
const names = ts.isArrayBindingPattern(binding)
|
|
683
|
+
? binding.elements.map((element) =>
|
|
684
|
+
ts.isBindingElement(element) &&
|
|
685
|
+
!element.dotDotDotToken &&
|
|
686
|
+
!element.initializer &&
|
|
687
|
+
ts.isIdentifier(element.name)
|
|
688
|
+
? element.name
|
|
689
|
+
: undefined,
|
|
690
|
+
)
|
|
691
|
+
: [undefined, ts.isIdentifier(binding) ? binding : undefined];
|
|
692
|
+
const [indexName, elementName] = names;
|
|
693
|
+
if (names.length !== 2 || !elementName || Boolean(indexName) !== keyed) {
|
|
694
|
+
this.fail(
|
|
695
|
+
binding,
|
|
696
|
+
"PLC1001",
|
|
697
|
+
"Bind one element, or destructure [index, element] from array.entries() or entries(record).",
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
const place = this.expressions.place(iterated);
|
|
701
|
+
const iteratedType = this.expressions.placeType(place).type;
|
|
702
|
+
const array =
|
|
703
|
+
iteratedType === undefined
|
|
704
|
+
? undefined
|
|
705
|
+
: this.resolver.types.get(iteratedType);
|
|
706
|
+
if (
|
|
707
|
+
array?.kind !== "array" ||
|
|
708
|
+
Boolean(array.index) !== form.startsWith("record")
|
|
709
|
+
) {
|
|
710
|
+
this.fail(
|
|
711
|
+
node.expression,
|
|
712
|
+
"PLC2001",
|
|
713
|
+
"for…of iterates a FixedArray, array.entries(), or an enum-keyed Record through Object.values(record) or entries(record).",
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
for (const segment of place.path) {
|
|
717
|
+
if (
|
|
718
|
+
segment.kind === "index" &&
|
|
719
|
+
!this.expressions.unchanging(segment.value)
|
|
720
|
+
) {
|
|
721
|
+
this.fail(
|
|
722
|
+
node.expression,
|
|
723
|
+
"PLC3003",
|
|
724
|
+
"An iterated array's index must keep its value.",
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
const origin = { span: this.positions.node(node) };
|
|
730
|
+
const constant = (type: string, value: string): Expression => ({
|
|
731
|
+
...origin,
|
|
732
|
+
kind: "constant",
|
|
733
|
+
type,
|
|
734
|
+
value: { kind: "integer", value },
|
|
735
|
+
});
|
|
736
|
+
const counter = this.local(
|
|
737
|
+
node,
|
|
738
|
+
form === "entries" && indexName ? indexName.text : "index",
|
|
739
|
+
"DINT",
|
|
740
|
+
{ mutable: true },
|
|
741
|
+
);
|
|
742
|
+
const index: Expression = {
|
|
743
|
+
...origin,
|
|
744
|
+
kind: "load",
|
|
745
|
+
type: "DINT",
|
|
746
|
+
place: { root: counter.id, path: [] },
|
|
747
|
+
};
|
|
748
|
+
const before: Statement[] = [];
|
|
749
|
+
const prefix: Statement[] = [];
|
|
750
|
+
const counters = [counter.id];
|
|
751
|
+
const enumeration = array.index
|
|
752
|
+
? this.resolver.types.get(array.index)
|
|
753
|
+
: undefined;
|
|
754
|
+
if (indexName && enumeration?.kind === "enum") {
|
|
755
|
+
// The enum key follows the counter: a CASE at the top of each iteration selects its member.
|
|
756
|
+
const key = this.local(indexName, indexName.text, enumeration.id, {
|
|
757
|
+
mutable: true,
|
|
758
|
+
});
|
|
759
|
+
const assign = (value: string): Statement => ({
|
|
760
|
+
...origin,
|
|
761
|
+
kind: "assign",
|
|
762
|
+
destination: { root: key.id, path: [] },
|
|
763
|
+
value: constant(key.type, value),
|
|
764
|
+
});
|
|
765
|
+
before.push(assign(String(array.lowerBound)));
|
|
766
|
+
prefix.push({
|
|
767
|
+
...origin,
|
|
768
|
+
kind: "switch",
|
|
769
|
+
value: index,
|
|
770
|
+
cases: enumeration.members.map((member) => ({
|
|
771
|
+
values: [{ kind: "integer", value: member.value }],
|
|
772
|
+
body: [assign(member.value)],
|
|
773
|
+
})),
|
|
774
|
+
fallback: [],
|
|
775
|
+
});
|
|
776
|
+
counters.push(key.id);
|
|
777
|
+
this.expressions.bind(this.resolver.symbol(indexName), key);
|
|
778
|
+
} else if (indexName) {
|
|
779
|
+
this.expressions.bind(this.resolver.symbol(indexName), counter);
|
|
780
|
+
}
|
|
781
|
+
this.expressions.alias(this.resolver.symbol(elementName), {
|
|
782
|
+
root: place.root,
|
|
783
|
+
path: [...place.path, { kind: "index", value: index }],
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
const upper = array.lowerBound + array.length - 1;
|
|
787
|
+
return this.inLoop(
|
|
788
|
+
counters,
|
|
789
|
+
counter.id,
|
|
790
|
+
[BigInt(array.lowerBound), BigInt(upper)],
|
|
791
|
+
() => [
|
|
792
|
+
...before,
|
|
793
|
+
{
|
|
794
|
+
...origin,
|
|
795
|
+
kind: "for",
|
|
796
|
+
counter: counter.id,
|
|
797
|
+
start: constant("DINT", String(array.lowerBound)),
|
|
798
|
+
limit: constant("DINT", String(upper)),
|
|
799
|
+
step: "1",
|
|
800
|
+
maxIterations: array.length,
|
|
801
|
+
body: [...prefix, ...this.body(node.statement)],
|
|
802
|
+
},
|
|
803
|
+
],
|
|
804
|
+
);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
/** What a for…of iterates: an array's elements or `entries()`, or a record's `Object.values` or SDK `entries`. */
|
|
808
|
+
private iteration(expression: ts.Expression): {
|
|
809
|
+
form: IterationForm;
|
|
810
|
+
iterated: ts.Expression;
|
|
811
|
+
} {
|
|
812
|
+
if (!ts.isCallExpression(expression)) {
|
|
813
|
+
return { form: "elements", iterated: expression };
|
|
814
|
+
}
|
|
815
|
+
const callee = expression.expression;
|
|
816
|
+
if (
|
|
817
|
+
ts.isPropertyAccessExpression(callee) &&
|
|
818
|
+
callee.name.text === "entries" &&
|
|
819
|
+
!expression.arguments.length
|
|
820
|
+
) {
|
|
821
|
+
return { form: "entries", iterated: callee.expression };
|
|
822
|
+
}
|
|
823
|
+
const [record, ...rest] = expression.arguments;
|
|
824
|
+
if (!record || rest.length) {
|
|
825
|
+
return { form: "elements", iterated: expression };
|
|
826
|
+
}
|
|
827
|
+
if (this.expressions.sdkCall(expression) === "entries") {
|
|
828
|
+
return { form: "recordEntries", iterated: record };
|
|
829
|
+
}
|
|
830
|
+
if (
|
|
831
|
+
ts.isPropertyAccessExpression(callee) &&
|
|
832
|
+
callee.name.text === "values" &&
|
|
833
|
+
ts.isIdentifier(callee.expression) &&
|
|
834
|
+
this.resolver.standardLibrary(this.resolver.symbol(callee.expression))
|
|
835
|
+
) {
|
|
836
|
+
return { form: "recordValues", iterated: record };
|
|
837
|
+
}
|
|
838
|
+
return { form: "elements", iterated: expression };
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/** Lowers a loop body with `counters` read-only and `counter` proved to lie in `range`. */
|
|
842
|
+
private inLoop<T>(
|
|
843
|
+
counters: readonly string[],
|
|
844
|
+
counter: string,
|
|
845
|
+
range: IntegerInterval,
|
|
846
|
+
lower: () => T,
|
|
847
|
+
): T {
|
|
848
|
+
this.breakTargets.push("loop");
|
|
849
|
+
try {
|
|
850
|
+
return this.expressions.counting(counters, counter, range, lower);
|
|
851
|
+
} finally {
|
|
852
|
+
this.breakTargets.pop();
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|