@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,535 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import {
|
|
3
|
+
copied,
|
|
4
|
+
from,
|
|
5
|
+
joined,
|
|
6
|
+
mapped,
|
|
7
|
+
type Mapped,
|
|
8
|
+
type Part,
|
|
9
|
+
} from "../positions.ts";
|
|
10
|
+
import { declaredBySdk, inSdk, symbolAt } from "../sdk.ts";
|
|
11
|
+
import type {
|
|
12
|
+
ExpressionSpecializer,
|
|
13
|
+
SpecializeContext,
|
|
14
|
+
} from "./expressions.ts";
|
|
15
|
+
import type { DialectText } from "./dialect.ts";
|
|
16
|
+
import type { Frame } from "./frame.ts";
|
|
17
|
+
import type { EntryImports } from "./imports.ts";
|
|
18
|
+
import type { Model } from "./model.ts";
|
|
19
|
+
import type { Storage } from "./storage.ts";
|
|
20
|
+
import {
|
|
21
|
+
constant,
|
|
22
|
+
repeatable,
|
|
23
|
+
SpecializeError,
|
|
24
|
+
type FunctionLike,
|
|
25
|
+
type Value,
|
|
26
|
+
} from "./values.ts";
|
|
27
|
+
|
|
28
|
+
/** Specializes calls: to what the language and SDK provide, to the service client, and to functions. */
|
|
29
|
+
export class CallSpecializer {
|
|
30
|
+
private readonly model: Model;
|
|
31
|
+
private readonly checker: ts.TypeChecker;
|
|
32
|
+
private readonly dialect: DialectText;
|
|
33
|
+
private readonly storage: Storage;
|
|
34
|
+
private readonly imports: EntryImports;
|
|
35
|
+
private readonly sdkFile: string;
|
|
36
|
+
private readonly sources: readonly ts.SourceFile[];
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
context: SpecializeContext,
|
|
40
|
+
private readonly expressions: ExpressionSpecializer,
|
|
41
|
+
) {
|
|
42
|
+
this.model = context.model;
|
|
43
|
+
this.checker = context.checker;
|
|
44
|
+
this.dialect = context.dialect;
|
|
45
|
+
this.storage = context.storage;
|
|
46
|
+
this.imports = context.imports;
|
|
47
|
+
this.sdkFile = context.sdkFile;
|
|
48
|
+
this.sources = context.sources;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
call(node: ts.CallExpression, frame: Frame, expected?: ts.Type): Value {
|
|
52
|
+
const callee = node.expression;
|
|
53
|
+
// Array methods and other calls the language provides.
|
|
54
|
+
if (
|
|
55
|
+
ts.isPropertyAccessExpression(callee) ||
|
|
56
|
+
ts.isPropertyAccessChain(callee)
|
|
57
|
+
) {
|
|
58
|
+
const name = callee.name.text;
|
|
59
|
+
if (
|
|
60
|
+
ts.isIdentifier(callee.expression) &&
|
|
61
|
+
callee.expression.text === "Math"
|
|
62
|
+
) {
|
|
63
|
+
return this.math(name, node, frame);
|
|
64
|
+
}
|
|
65
|
+
if (
|
|
66
|
+
ts.isIdentifier(callee.expression) &&
|
|
67
|
+
callee.expression.text === "Object" &&
|
|
68
|
+
name === "values"
|
|
69
|
+
) {
|
|
70
|
+
const [argument] = node.arguments;
|
|
71
|
+
const object = argument && this.expressions.expression(argument, frame);
|
|
72
|
+
if (
|
|
73
|
+
object?.kind === "static" &&
|
|
74
|
+
object.node.captured.kind === "object"
|
|
75
|
+
) {
|
|
76
|
+
return {
|
|
77
|
+
kind: "virtual",
|
|
78
|
+
items: Object.entries(object.node.captured.fields).map(
|
|
79
|
+
([field, value]) =>
|
|
80
|
+
this.storage.fromCaptured(value, object.node, {
|
|
81
|
+
kind: "field",
|
|
82
|
+
name: field,
|
|
83
|
+
}),
|
|
84
|
+
),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
throw new SpecializeError(
|
|
88
|
+
node,
|
|
89
|
+
"Object.values needs a record setup built.",
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
if (this.telemetryCall(callee)) {
|
|
93
|
+
return {
|
|
94
|
+
kind: "code",
|
|
95
|
+
text: this.telemetry(node, callee, frame),
|
|
96
|
+
declared: this.checker.getTypeAtLocation(node),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const receiver = this.expressions.expression(callee.expression, frame);
|
|
100
|
+
const items = this.expressions.items(receiver);
|
|
101
|
+
if (items && arrayMethods.has(name)) {
|
|
102
|
+
return this.arrayMethod(name, items, node, frame);
|
|
103
|
+
}
|
|
104
|
+
if (
|
|
105
|
+
receiver.kind === "static" &&
|
|
106
|
+
receiver.node.captured.kind === "service"
|
|
107
|
+
) {
|
|
108
|
+
const path = receiver.node.captured.path
|
|
109
|
+
? `${receiver.node.captured.path}.${name}`
|
|
110
|
+
: name;
|
|
111
|
+
return this.serviceCall(path, node, frame);
|
|
112
|
+
}
|
|
113
|
+
const method = this.expressions.member(receiver, name, callee, frame);
|
|
114
|
+
if (method.kind === "static" && method.node.captured.kind === "service") {
|
|
115
|
+
return this.serviceCall(method.node.captured.path, node, frame);
|
|
116
|
+
}
|
|
117
|
+
return this.expressions.invoke(
|
|
118
|
+
method,
|
|
119
|
+
node.arguments.map((argument, index) =>
|
|
120
|
+
this.argument(argument, method, index, frame),
|
|
121
|
+
),
|
|
122
|
+
node,
|
|
123
|
+
frame,
|
|
124
|
+
expected,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const target = this.expressions.expression(callee, frame);
|
|
128
|
+
if (target.kind === "static" && target.node.captured.kind === "service") {
|
|
129
|
+
return this.serviceCall(target.node.captured.path, node, frame);
|
|
130
|
+
}
|
|
131
|
+
if (target.kind === "static" && target.node.captured.kind === "function") {
|
|
132
|
+
const fn = this.functionDeclaration(
|
|
133
|
+
target.node.captured.file,
|
|
134
|
+
target.node.captured.name,
|
|
135
|
+
);
|
|
136
|
+
return this.expressions.invoke(
|
|
137
|
+
{ kind: "function", declaration: fn },
|
|
138
|
+
node.arguments.map((argument, index) =>
|
|
139
|
+
this.argument(
|
|
140
|
+
argument,
|
|
141
|
+
{ kind: "function", declaration: fn },
|
|
142
|
+
index,
|
|
143
|
+
frame,
|
|
144
|
+
),
|
|
145
|
+
),
|
|
146
|
+
node,
|
|
147
|
+
frame,
|
|
148
|
+
expected,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
if (ts.isIdentifier(callee) && this.sdkCall(callee)) {
|
|
152
|
+
return this.sdk(callee.text, node, frame);
|
|
153
|
+
}
|
|
154
|
+
return this.expressions.invoke(
|
|
155
|
+
target,
|
|
156
|
+
node.arguments.map((argument, index) =>
|
|
157
|
+
this.argument(argument, target, index, frame),
|
|
158
|
+
),
|
|
159
|
+
node,
|
|
160
|
+
frame,
|
|
161
|
+
expected,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private argument(
|
|
166
|
+
node: ts.Expression,
|
|
167
|
+
callee: Value,
|
|
168
|
+
index: number,
|
|
169
|
+
frame: Frame,
|
|
170
|
+
): Value {
|
|
171
|
+
const parameter =
|
|
172
|
+
callee.kind === "function" &&
|
|
173
|
+
!ts.isGetAccessorDeclaration(callee.declaration)
|
|
174
|
+
? callee.declaration.parameters[index]
|
|
175
|
+
: undefined;
|
|
176
|
+
return this.expressions.expression(
|
|
177
|
+
node,
|
|
178
|
+
frame,
|
|
179
|
+
parameter && this.checker.getTypeAtLocation(parameter),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private functionDeclaration(file: string, name: string): FunctionLike {
|
|
184
|
+
const source = this.sources.find((item) => item.fileName === file);
|
|
185
|
+
for (const statement of source?.statements ?? []) {
|
|
186
|
+
if (
|
|
187
|
+
ts.isFunctionDeclaration(statement) &&
|
|
188
|
+
statement.name?.text === name
|
|
189
|
+
) {
|
|
190
|
+
return statement;
|
|
191
|
+
}
|
|
192
|
+
if (ts.isVariableStatement(statement)) {
|
|
193
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
194
|
+
if (
|
|
195
|
+
ts.isIdentifier(declaration.name) &&
|
|
196
|
+
declaration.name.text === name &&
|
|
197
|
+
declaration.initializer &&
|
|
198
|
+
(ts.isArrowFunction(declaration.initializer) ||
|
|
199
|
+
ts.isFunctionExpression(declaration.initializer))
|
|
200
|
+
) {
|
|
201
|
+
return declaration.initializer;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
throw new Error(`${file}: no function ${name}.`);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** SDK functions scan code calls, such as the PLC clock. */
|
|
210
|
+
private sdkCall(callee: ts.Identifier): boolean {
|
|
211
|
+
const symbol = symbolAt(this.checker, callee);
|
|
212
|
+
return symbol !== undefined && declaredBySdk(this.sdkFile, symbol);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** A method of `@tsplc/compiler/telemetry`'s `telemetry`. */
|
|
216
|
+
private telemetryCall(callee: ts.PropertyAccessExpression): boolean {
|
|
217
|
+
const file = this.checker
|
|
218
|
+
.getSymbolAtLocation(callee.name)
|
|
219
|
+
?.declarations?.[0]?.getSourceFile().fileName;
|
|
220
|
+
return (
|
|
221
|
+
file !== undefined &&
|
|
222
|
+
inSdk(this.sdkFile, file) &&
|
|
223
|
+
file.endsWith("/telemetry.d.ts")
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Keeps a telemetry call as written, with its values specialized: the analyzer checks and
|
|
229
|
+
* lowers it, and needs its name, message and property names as they are.
|
|
230
|
+
*/
|
|
231
|
+
private telemetry(
|
|
232
|
+
node: ts.CallExpression,
|
|
233
|
+
callee: ts.PropertyAccessExpression,
|
|
234
|
+
frame: Frame,
|
|
235
|
+
): Mapped {
|
|
236
|
+
const value = (argument: ts.Expression): Part =>
|
|
237
|
+
ts.isCallExpression(argument) &&
|
|
238
|
+
ts.isPropertyAccessExpression(argument.expression) &&
|
|
239
|
+
this.telemetryCall(argument.expression)
|
|
240
|
+
? this.telemetry(argument, argument.expression, frame)
|
|
241
|
+
: // A folded enum field keeps its type, so it is sent as its member's name.
|
|
242
|
+
this.dialect.code(
|
|
243
|
+
this.expressions.expression(argument, frame),
|
|
244
|
+
this.checker.getTypeAtLocation(argument),
|
|
245
|
+
);
|
|
246
|
+
const args = node.arguments.map((argument): Part => {
|
|
247
|
+
if (
|
|
248
|
+
ts.isStringLiteral(argument) ||
|
|
249
|
+
ts.isNoSubstitutionTemplateLiteral(argument)
|
|
250
|
+
) {
|
|
251
|
+
return copied(argument);
|
|
252
|
+
}
|
|
253
|
+
if (!ts.isObjectLiteralExpression(argument)) {
|
|
254
|
+
return value(argument);
|
|
255
|
+
}
|
|
256
|
+
const properties = argument.properties.map((property) => {
|
|
257
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
258
|
+
return mapped`${copied(property.name)}: ${value(property.name)}`;
|
|
259
|
+
}
|
|
260
|
+
if (!ts.isPropertyAssignment(property)) {
|
|
261
|
+
throw new SpecializeError(property, "Name each telemetry property.");
|
|
262
|
+
}
|
|
263
|
+
return mapped`${copied(property.name)}: ${value(property.initializer)}`;
|
|
264
|
+
});
|
|
265
|
+
return mapped`{ ${joined(properties, ", ")} }`;
|
|
266
|
+
});
|
|
267
|
+
this.imports.telemetry = true;
|
|
268
|
+
return from(
|
|
269
|
+
node,
|
|
270
|
+
mapped`telemetry.${callee.name.text}(${joined(args, ", ")})`,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private sdk(name: string, node: ts.CallExpression, frame: Frame): Value {
|
|
275
|
+
const args = node.arguments.map((argument) =>
|
|
276
|
+
this.dialect.code(
|
|
277
|
+
this.expressions.expression(argument, frame),
|
|
278
|
+
undefined,
|
|
279
|
+
),
|
|
280
|
+
);
|
|
281
|
+
this.imports.clock.add(name);
|
|
282
|
+
return {
|
|
283
|
+
kind: "code",
|
|
284
|
+
text: mapped`${name}(${joined(args, ", ")})`,
|
|
285
|
+
declared: this.checker.getTypeAtLocation(node),
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private math(name: string, node: ts.CallExpression, frame: Frame): Value {
|
|
290
|
+
const args = node.arguments.map((argument) =>
|
|
291
|
+
this.expressions.expression(argument, frame),
|
|
292
|
+
);
|
|
293
|
+
const values = args.flatMap((arg) =>
|
|
294
|
+
arg.kind === "const" && typeof arg.value === "number" ? [arg.value] : [],
|
|
295
|
+
);
|
|
296
|
+
if (values.length === args.length) {
|
|
297
|
+
const folded = (
|
|
298
|
+
Math as unknown as Record<string, (...items: number[]) => number>
|
|
299
|
+
)[name]?.(...values);
|
|
300
|
+
if (typeof folded === "number") {
|
|
301
|
+
return constant(folded);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const texts = args.map((arg) => this.dialect.code(arg, undefined));
|
|
305
|
+
const declared = this.checker.getTypeAtLocation(node);
|
|
306
|
+
switch (name) {
|
|
307
|
+
case "abs": {
|
|
308
|
+
const local = frame.emitter.temporary();
|
|
309
|
+
frame.emitter.line(mapped`const ${local}: number = ${texts[0]!};`);
|
|
310
|
+
return {
|
|
311
|
+
kind: "code",
|
|
312
|
+
text: `(${local} < 0 ? 0 - ${local} : ${local})`,
|
|
313
|
+
declared,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
case "min":
|
|
317
|
+
case "max": {
|
|
318
|
+
const [left, right, ...rest] = texts;
|
|
319
|
+
if (left === undefined || right === undefined || rest.length) {
|
|
320
|
+
throw new SpecializeError(node, `Math.${name} takes two values.`);
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
kind: "code",
|
|
324
|
+
text: mapped`(${left} ${name === "min" ? "<" : ">"} ${right} ? ${left} : ${right})`,
|
|
325
|
+
declared,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
default:
|
|
329
|
+
throw new SpecializeError(node, `Math.${name} is not supported.`);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private serviceCall(
|
|
334
|
+
path: string,
|
|
335
|
+
node: ts.CallExpression,
|
|
336
|
+
frame: Frame,
|
|
337
|
+
): Value {
|
|
338
|
+
const client = this.model.roots.find((root) => {
|
|
339
|
+
const value = this.model.node(root.value);
|
|
340
|
+
return value?.captured.kind === "service" && value.captured.path === "";
|
|
341
|
+
});
|
|
342
|
+
if (!client) {
|
|
343
|
+
throw new SpecializeError(
|
|
344
|
+
node,
|
|
345
|
+
"Create the service client in a module of its own.",
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
this.imports.kept.add(client.file, client.name);
|
|
349
|
+
const args = node.arguments.map((argument) =>
|
|
350
|
+
this.dialect.code(
|
|
351
|
+
this.expressions.expression(
|
|
352
|
+
argument,
|
|
353
|
+
frame,
|
|
354
|
+
this.checker.getContextualType(argument),
|
|
355
|
+
),
|
|
356
|
+
this.checker.getContextualType(argument),
|
|
357
|
+
),
|
|
358
|
+
);
|
|
359
|
+
return {
|
|
360
|
+
kind: "code",
|
|
361
|
+
text: mapped`${client.name}.${path}(${joined(args, ", ")})`,
|
|
362
|
+
declared: this.checker.getTypeAtLocation(node),
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private arrayMethod(
|
|
367
|
+
name: string,
|
|
368
|
+
items: Value[],
|
|
369
|
+
node: ts.CallExpression,
|
|
370
|
+
frame: Frame,
|
|
371
|
+
): Value {
|
|
372
|
+
const callback = node.arguments[0]
|
|
373
|
+
? this.expressions.expression(node.arguments[0], frame)
|
|
374
|
+
: undefined;
|
|
375
|
+
const apply = (args: Value[]): Value => {
|
|
376
|
+
if (!callback) {
|
|
377
|
+
throw new SpecializeError(node, `${name} needs a callback.`);
|
|
378
|
+
}
|
|
379
|
+
return this.expressions.invoke(callback, args, node, frame);
|
|
380
|
+
};
|
|
381
|
+
switch (name) {
|
|
382
|
+
case "entries":
|
|
383
|
+
return {
|
|
384
|
+
kind: "virtual",
|
|
385
|
+
items: items.map((item, index) => ({
|
|
386
|
+
kind: "virtual",
|
|
387
|
+
items: [constant(index), item],
|
|
388
|
+
})),
|
|
389
|
+
};
|
|
390
|
+
case "slice": {
|
|
391
|
+
const [start, end] = node.arguments.map((argument) =>
|
|
392
|
+
this.expressions.expression(argument, frame),
|
|
393
|
+
);
|
|
394
|
+
if (
|
|
395
|
+
(start && start.kind !== "const") ||
|
|
396
|
+
(end && end.kind !== "const")
|
|
397
|
+
) {
|
|
398
|
+
throw new SpecializeError(node, "slice needs constant bounds.");
|
|
399
|
+
}
|
|
400
|
+
return {
|
|
401
|
+
kind: "virtual",
|
|
402
|
+
items: items.slice(
|
|
403
|
+
start?.value as number | undefined,
|
|
404
|
+
end?.value as number | undefined,
|
|
405
|
+
),
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
case "map":
|
|
409
|
+
return {
|
|
410
|
+
kind: "virtual",
|
|
411
|
+
items: items.map((item, index) =>
|
|
412
|
+
this.settle(apply([item, constant(index)]), frame),
|
|
413
|
+
),
|
|
414
|
+
};
|
|
415
|
+
case "filter": {
|
|
416
|
+
const kept: Value[] = [];
|
|
417
|
+
items.forEach((item, index) => {
|
|
418
|
+
const test = apply([item, constant(index)]);
|
|
419
|
+
if (test.kind !== "const") {
|
|
420
|
+
throw new SpecializeError(
|
|
421
|
+
node,
|
|
422
|
+
"filter must decide from what setup built.",
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
if (test.value) {
|
|
426
|
+
kept.push(item);
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
return { kind: "virtual", items: kept };
|
|
430
|
+
}
|
|
431
|
+
case "forEach":
|
|
432
|
+
items.forEach((item, index) => {
|
|
433
|
+
const result = apply([item, constant(index)]);
|
|
434
|
+
if (result.kind === "code") {
|
|
435
|
+
frame.emitter.line(mapped`${result.text};`);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
return constant(undefined);
|
|
439
|
+
case "some":
|
|
440
|
+
case "every": {
|
|
441
|
+
const some = name === "some";
|
|
442
|
+
const tests = items.map((item, index) =>
|
|
443
|
+
apply([item, constant(index)]),
|
|
444
|
+
);
|
|
445
|
+
const runtime: (string | Mapped)[] = [];
|
|
446
|
+
for (const test of tests) {
|
|
447
|
+
if (test.kind === "const") {
|
|
448
|
+
if (Boolean(test.value) === some) {
|
|
449
|
+
return constant(some);
|
|
450
|
+
}
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
runtime.push(this.dialect.condition(test, node.arguments[0] ?? node));
|
|
454
|
+
}
|
|
455
|
+
if (!runtime.length) {
|
|
456
|
+
return constant(!some);
|
|
457
|
+
}
|
|
458
|
+
return {
|
|
459
|
+
kind: "code",
|
|
460
|
+
text: mapped`(${joined(runtime, some ? " || " : " && ")})`,
|
|
461
|
+
declared: this.checker.getBooleanType(),
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
case "reduce":
|
|
465
|
+
case "reduceRight": {
|
|
466
|
+
const order = name === "reduce" ? items : [...items].reverse();
|
|
467
|
+
let accumulator = node.arguments[1]
|
|
468
|
+
? this.expressions.expression(node.arguments[1], frame)
|
|
469
|
+
: order.shift()!;
|
|
470
|
+
for (const item of order) {
|
|
471
|
+
accumulator = this.settle(apply([accumulator, item]), frame);
|
|
472
|
+
}
|
|
473
|
+
return accumulator;
|
|
474
|
+
}
|
|
475
|
+
case "find": {
|
|
476
|
+
for (const [index, item] of items.entries()) {
|
|
477
|
+
const test = apply([item, constant(index)]);
|
|
478
|
+
if (test.kind !== "const") {
|
|
479
|
+
throw new SpecializeError(
|
|
480
|
+
node,
|
|
481
|
+
"find must decide from what setup built.",
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
if (test.value) {
|
|
485
|
+
return item;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return constant(undefined);
|
|
489
|
+
}
|
|
490
|
+
case "includes": {
|
|
491
|
+
const target = this.expressions.expression(node.arguments[0]!, frame);
|
|
492
|
+
const tests = items.map((item) => this.expressions.equal(item, target));
|
|
493
|
+
if (tests.some((test) => test === true)) {
|
|
494
|
+
return constant(true);
|
|
495
|
+
}
|
|
496
|
+
if (tests.every((test) => test === false)) {
|
|
497
|
+
return constant(false);
|
|
498
|
+
}
|
|
499
|
+
throw new SpecializeError(
|
|
500
|
+
node,
|
|
501
|
+
"includes must decide from what setup built.",
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
default:
|
|
505
|
+
throw new SpecializeError(node, `${name} is not supported.`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/** Keeps a runtime result in a local, so later code reads it rather than repeating it. */
|
|
510
|
+
private settle(value: Value, frame: Frame): Value {
|
|
511
|
+
if (value.kind !== "code" || repeatable(value.text)) {
|
|
512
|
+
return value;
|
|
513
|
+
}
|
|
514
|
+
const local = frame.emitter.temporary();
|
|
515
|
+
frame.emitter.line(
|
|
516
|
+
mapped`const ${local}: ${value.declared ? this.expressions.localType(value.declared) : "boolean"} = ${value.text};`,
|
|
517
|
+
);
|
|
518
|
+
return { kind: "code", text: local, declared: value.declared, place: true };
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** Array methods scan code calls on arrays setup built, which unroll. */
|
|
523
|
+
const arrayMethods = new Set([
|
|
524
|
+
"entries",
|
|
525
|
+
"slice",
|
|
526
|
+
"map",
|
|
527
|
+
"filter",
|
|
528
|
+
"forEach",
|
|
529
|
+
"some",
|
|
530
|
+
"every",
|
|
531
|
+
"reduce",
|
|
532
|
+
"reduceRight",
|
|
533
|
+
"find",
|
|
534
|
+
"includes",
|
|
535
|
+
]);
|