@tsplc/tmp-compiler 0.0.0-stage → 0.6.2
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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6558 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16076 -0
- package/dist/index.js.map +61 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1078 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1336 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2171 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +609 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +651 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,1342 @@
|
|
|
1
|
+
import { calendarLiteral } from "@tsplc/compiler/ir";
|
|
2
|
+
import { elementaryType } from "@tsplc/iec";
|
|
3
|
+
import {
|
|
4
|
+
type ElementaryFunction,
|
|
5
|
+
elementaryFunctions,
|
|
6
|
+
elementaryValue,
|
|
7
|
+
} from "@tsplc/iec/arithmetic";
|
|
8
|
+
import ts from "typescript";
|
|
9
|
+
import {
|
|
10
|
+
copied,
|
|
11
|
+
from,
|
|
12
|
+
joined,
|
|
13
|
+
type Mapped,
|
|
14
|
+
mapped,
|
|
15
|
+
type Part,
|
|
16
|
+
} from "../positions";
|
|
17
|
+
import {
|
|
18
|
+
declaredBySdk,
|
|
19
|
+
inSdk,
|
|
20
|
+
isTelemetryDeclaration,
|
|
21
|
+
sdkModule,
|
|
22
|
+
sdkModules,
|
|
23
|
+
symbolAt,
|
|
24
|
+
unaliased,
|
|
25
|
+
valueSymbol,
|
|
26
|
+
} from "../sdk";
|
|
27
|
+
import { ArraySearches, callbackArgument, ordered } from "./array-searches";
|
|
28
|
+
import { isGlobal } from "./declarations";
|
|
29
|
+
import {
|
|
30
|
+
type ExpressionSpecializer,
|
|
31
|
+
type Holding,
|
|
32
|
+
isPromiseLink,
|
|
33
|
+
type SpecializeContext,
|
|
34
|
+
} from "./expressions";
|
|
35
|
+
import type { Frame } from "./frame";
|
|
36
|
+
import type { ModelNode } from "./model";
|
|
37
|
+
import { calendarFormats, durationNames } from "./time-constructors";
|
|
38
|
+
import {
|
|
39
|
+
constant,
|
|
40
|
+
type FunctionLike,
|
|
41
|
+
finite,
|
|
42
|
+
SpecializeError,
|
|
43
|
+
unwrapped,
|
|
44
|
+
type Value,
|
|
45
|
+
} from "./values";
|
|
46
|
+
|
|
47
|
+
/** What calls read from the expression specializer: evaluation, invocation, and the specializers for array values. */
|
|
48
|
+
type CallParts = Pick<
|
|
49
|
+
ExpressionSpecializer,
|
|
50
|
+
| "expression"
|
|
51
|
+
| "invoke"
|
|
52
|
+
| "inOrder"
|
|
53
|
+
| "member"
|
|
54
|
+
| "superMember"
|
|
55
|
+
| "recordFields"
|
|
56
|
+
| "shorthand"
|
|
57
|
+
| "items"
|
|
58
|
+
| "element"
|
|
59
|
+
| "canceller"
|
|
60
|
+
| "chained"
|
|
61
|
+
| "held"
|
|
62
|
+
| "hold"
|
|
63
|
+
| "localType"
|
|
64
|
+
| "equality"
|
|
65
|
+
| "assignments"
|
|
66
|
+
| "references"
|
|
67
|
+
| "filteredArrays"
|
|
68
|
+
| "iterators"
|
|
69
|
+
>;
|
|
70
|
+
|
|
71
|
+
/** Specializes calls: to what the language and SDK provide, to the service client, and to functions. */
|
|
72
|
+
export class CallSpecializer {
|
|
73
|
+
private readonly searches: ArraySearches;
|
|
74
|
+
|
|
75
|
+
constructor(
|
|
76
|
+
private readonly context: SpecializeContext,
|
|
77
|
+
private readonly expressions: CallParts,
|
|
78
|
+
) {
|
|
79
|
+
this.searches = new ArraySearches(context, expressions);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
call(node: ts.CallExpression, frame: Frame, expected?: ts.Type): Value {
|
|
83
|
+
const callee = node.expression;
|
|
84
|
+
if (
|
|
85
|
+
ts.isPropertyAccessExpression(callee) ||
|
|
86
|
+
ts.isPropertyAccessChain(callee)
|
|
87
|
+
) {
|
|
88
|
+
return this.method(node, callee, frame, expected);
|
|
89
|
+
}
|
|
90
|
+
if (isGlobal(this.context.checker, callee, "structuredClone")) {
|
|
91
|
+
return this.cloned(node, frame);
|
|
92
|
+
}
|
|
93
|
+
const target = this.expressions.expression(callee, frame);
|
|
94
|
+
// `f?.()` of a function setup left undefined calls nothing.
|
|
95
|
+
if (
|
|
96
|
+
node.questionDotToken &&
|
|
97
|
+
target.kind === "const" &&
|
|
98
|
+
target.value === undefined
|
|
99
|
+
) {
|
|
100
|
+
return constant(undefined);
|
|
101
|
+
}
|
|
102
|
+
if (target.kind === "static" && target.node.captured.kind === "task") {
|
|
103
|
+
return this.taskCall(target.node, null, node, frame);
|
|
104
|
+
}
|
|
105
|
+
if (target.kind === "static" && target.node.captured.kind === "service") {
|
|
106
|
+
return this.serviceCall(target.node.captured.path, node, frame);
|
|
107
|
+
}
|
|
108
|
+
if (target.kind === "static" && target.node.captured.kind === "function") {
|
|
109
|
+
const declaration = this.functionDeclaration(
|
|
110
|
+
target.node.captured.file,
|
|
111
|
+
target.node.captured.name,
|
|
112
|
+
);
|
|
113
|
+
return this.invokeCall(
|
|
114
|
+
{ kind: "function", declaration },
|
|
115
|
+
node,
|
|
116
|
+
frame,
|
|
117
|
+
expected,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const sdkFile = ts.isIdentifier(callee) && this.sdkDeclaration(callee);
|
|
121
|
+
if (
|
|
122
|
+
ts.isIdentifier(callee) &&
|
|
123
|
+
sdkFile &&
|
|
124
|
+
callee.text === "cancelAsync" &&
|
|
125
|
+
sdkModule(sdkFile) === sdkModules.async &&
|
|
126
|
+
node.arguments.length
|
|
127
|
+
) {
|
|
128
|
+
return this.cancelOwner(node, frame);
|
|
129
|
+
}
|
|
130
|
+
if (ts.isIdentifier(callee) && sdkFile) {
|
|
131
|
+
return this.sdk(callee.text, sdkModule(sdkFile), node, frame);
|
|
132
|
+
}
|
|
133
|
+
return this.invokeCall(target, node, frame, expected);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** `receiver.name(…)`: what the language and SDK provide on the receiver, or its method. */
|
|
137
|
+
private method(
|
|
138
|
+
node: ts.CallExpression,
|
|
139
|
+
callee: ts.PropertyAccessExpression,
|
|
140
|
+
frame: Frame,
|
|
141
|
+
expected: ts.Type | undefined,
|
|
142
|
+
): Value {
|
|
143
|
+
const name = callee.name.text;
|
|
144
|
+
const checker = this.context.checker;
|
|
145
|
+
if (name === "from" && isGlobal(checker, callee.expression, "Array")) {
|
|
146
|
+
return this.arrayFrom(node, frame);
|
|
147
|
+
}
|
|
148
|
+
if (callee.expression.kind === ts.SyntaxKind.SuperKeyword) {
|
|
149
|
+
return this.invokeCall(
|
|
150
|
+
this.expressions.superMember(callee, frame),
|
|
151
|
+
node,
|
|
152
|
+
frame,
|
|
153
|
+
expected,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
if (isGlobal(checker, callee.expression, "Math")) {
|
|
157
|
+
return this.math(name, node, frame);
|
|
158
|
+
}
|
|
159
|
+
if (name === "values" && isGlobal(checker, callee.expression, "Object")) {
|
|
160
|
+
return this.objectValues(node, frame);
|
|
161
|
+
}
|
|
162
|
+
if (this.telemetryCall(callee)) {
|
|
163
|
+
return {
|
|
164
|
+
kind: "code",
|
|
165
|
+
text: this.telemetry(node, callee, frame),
|
|
166
|
+
declared: checker.getTypeAtLocation(node),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
if (isPromiseLink(node) && this.isPromise(callee.expression)) {
|
|
170
|
+
return this.promiseLink(node, callee, frame);
|
|
171
|
+
}
|
|
172
|
+
const iterated = this.expressions.iterators.call(node, frame);
|
|
173
|
+
if (iterated) {
|
|
174
|
+
return iterated;
|
|
175
|
+
}
|
|
176
|
+
const receiver = this.receiver(callee.expression, frame);
|
|
177
|
+
if (receiver.kind === "static" && receiver.node.captured.kind === "task") {
|
|
178
|
+
return this.taskCall(receiver.node, name, node, frame);
|
|
179
|
+
}
|
|
180
|
+
const counted = this.expressions.iterators.arrayCall(
|
|
181
|
+
name,
|
|
182
|
+
receiver,
|
|
183
|
+
node,
|
|
184
|
+
frame,
|
|
185
|
+
);
|
|
186
|
+
if (counted) {
|
|
187
|
+
return counted;
|
|
188
|
+
}
|
|
189
|
+
if (receiver.kind === "choice") {
|
|
190
|
+
const call = () =>
|
|
191
|
+
this.expressions.references.call(receiver, name, node, frame, expected);
|
|
192
|
+
return callee.questionDotToken && receiver.optional
|
|
193
|
+
? this.expressions.chained(receiver, call, node, frame)
|
|
194
|
+
: call();
|
|
195
|
+
}
|
|
196
|
+
const items = this.expressions.items(receiver);
|
|
197
|
+
const filtered = this.expressions.filteredArrays.call(
|
|
198
|
+
name,
|
|
199
|
+
receiver,
|
|
200
|
+
items,
|
|
201
|
+
node,
|
|
202
|
+
frame,
|
|
203
|
+
);
|
|
204
|
+
if (filtered) {
|
|
205
|
+
return filtered;
|
|
206
|
+
}
|
|
207
|
+
if (items && isArrayMethod(name)) {
|
|
208
|
+
return this.arrayMethod(name, receiver, items, node, frame);
|
|
209
|
+
}
|
|
210
|
+
const method = this.expressions.member(receiver, name, callee, frame);
|
|
211
|
+
if (
|
|
212
|
+
node.questionDotToken &&
|
|
213
|
+
method.kind === "const" &&
|
|
214
|
+
method.value === undefined
|
|
215
|
+
) {
|
|
216
|
+
return constant(undefined);
|
|
217
|
+
}
|
|
218
|
+
if (method.kind === "static" && method.node.captured.kind === "service") {
|
|
219
|
+
return this.serviceCall(method.node.captured.path, node, frame);
|
|
220
|
+
}
|
|
221
|
+
return this.invokeCall(method, node, frame, expected);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* What a method is called on. An element of an array of objects setup built, at an index decided
|
|
226
|
+
* at run time, is a reference to whichever it is, so the call runs that object's method.
|
|
227
|
+
*/
|
|
228
|
+
private receiver(node: ts.Expression, frame: Frame): Value {
|
|
229
|
+
const inner = unwrapped(node);
|
|
230
|
+
if (!ts.isElementAccessExpression(inner)) {
|
|
231
|
+
return this.expressions.expression(node, frame);
|
|
232
|
+
}
|
|
233
|
+
const object = this.expressions.expression(inner.expression, frame);
|
|
234
|
+
const index = this.expressions.expression(inner.argumentExpression, frame);
|
|
235
|
+
const items =
|
|
236
|
+
object.kind === "static" && object.node.captured.kind === "array"
|
|
237
|
+
? this.expressions.items(object)
|
|
238
|
+
: undefined;
|
|
239
|
+
const chosen =
|
|
240
|
+
items?.every(
|
|
241
|
+
(item) =>
|
|
242
|
+
item.kind === "static" && item.node.captured.kind === "instance",
|
|
243
|
+
) &&
|
|
244
|
+
this.expressions.filteredArrays.indexed(
|
|
245
|
+
items,
|
|
246
|
+
index,
|
|
247
|
+
{ fromEnd: false },
|
|
248
|
+
inner,
|
|
249
|
+
frame,
|
|
250
|
+
);
|
|
251
|
+
return chosen || this.expressions.element(object, index, inner, frame);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** `cancelAsync(object)`: cancels the pending async calls of that object's methods. */
|
|
255
|
+
private cancelOwner(node: ts.CallExpression, frame: Frame): Value {
|
|
256
|
+
const [argument, ...rest] = node.arguments;
|
|
257
|
+
const owner = argument && this.expressions.expression(argument, frame);
|
|
258
|
+
if (
|
|
259
|
+
rest.length ||
|
|
260
|
+
owner?.kind !== "static" ||
|
|
261
|
+
owner.node.captured.kind !== "instance"
|
|
262
|
+
) {
|
|
263
|
+
throw new SpecializeError(
|
|
264
|
+
node,
|
|
265
|
+
"cancelAsync takes an object setup built, such as this, whose pending async calls it cancels, or no argument to cancel every pending async call.",
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
frame.emitter.line(`${this.expressions.canceller(owner.node)}();`);
|
|
269
|
+
return constant(undefined);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Whether an expression is a promise: an async call TypeScript types as one. */
|
|
273
|
+
private isPromise(node: ts.Expression): boolean {
|
|
274
|
+
const type = this.context.checker.getTypeAtLocation(node);
|
|
275
|
+
return this.context.checker.getAwaitedType(type) !== type;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* `promise.then(fulfilled, rejected)` or `promise.catch(rejected)`, awaited in an async routine:
|
|
280
|
+
* the promise awaited into a local and the callback run on its value, or on its error code in the
|
|
281
|
+
* catch block of a try around both. The link's value is what the await of it gives.
|
|
282
|
+
*/
|
|
283
|
+
private promiseLink(
|
|
284
|
+
node: ts.CallExpression,
|
|
285
|
+
callee: ts.PropertyAccessExpression,
|
|
286
|
+
frame: Frame,
|
|
287
|
+
): Value {
|
|
288
|
+
const name = callee.name.text;
|
|
289
|
+
if (!frame.async || !awaitedLink(node)) {
|
|
290
|
+
throw new SpecializeError(
|
|
291
|
+
node,
|
|
292
|
+
`.${name} runs only where an async function awaits it: in an async function, write const value = await call(); inside a try, and handle the value, or the error in catch.`,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
const { checker, initializers } = this.context;
|
|
296
|
+
const [first, second] = node.arguments;
|
|
297
|
+
const fulfilled = name === "then" ? first : undefined;
|
|
298
|
+
const rejected = name === "then" ? second : first;
|
|
299
|
+
const result = checker.getAwaitedType(checker.getTypeAtLocation(node));
|
|
300
|
+
const resultType =
|
|
301
|
+
result && !(result.flags & ts.TypeFlags.Void) ? result : undefined;
|
|
302
|
+
const fulfil = (): Value => {
|
|
303
|
+
const value = this.awaitedValue(
|
|
304
|
+
this.expressions.expression(callee.expression, frame),
|
|
305
|
+
frame,
|
|
306
|
+
);
|
|
307
|
+
return fulfilled ? this.handled(fulfilled, [value], frame) : value;
|
|
308
|
+
};
|
|
309
|
+
if (!rejected) {
|
|
310
|
+
return fulfil();
|
|
311
|
+
}
|
|
312
|
+
const local = resultType && frame.emitter.temporary();
|
|
313
|
+
if (local && resultType) {
|
|
314
|
+
frame.emitter.line(
|
|
315
|
+
`let ${local}: ${this.expressions.localType(resultType)} = ${initializers.defaultOf(resultType)};`,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
const store = (value: Value) => {
|
|
319
|
+
if (local) {
|
|
320
|
+
frame.emitter.line(
|
|
321
|
+
mapped`${local} = ${this.context.dialect.assignmentText(value, resultType, undefined)};`,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
frame.emitter.open("try {");
|
|
326
|
+
store(fulfil());
|
|
327
|
+
const error = frame.emitter.local("error");
|
|
328
|
+
frame.emitter.reopen(`} catch (${error}) {`);
|
|
329
|
+
// The callback is given the error code, as errorCode reads it from the catch binding.
|
|
330
|
+
const code = frame.emitter.local("code");
|
|
331
|
+
this.context.imports.addSdk(sdkModules.async, "errorCode");
|
|
332
|
+
frame.emitter.line(`const ${code}: UDINT = errorCode(${error});`);
|
|
333
|
+
store(
|
|
334
|
+
this.handled(
|
|
335
|
+
rejected,
|
|
336
|
+
[
|
|
337
|
+
{
|
|
338
|
+
kind: "code",
|
|
339
|
+
text: code,
|
|
340
|
+
declared: checker.getNumberType(),
|
|
341
|
+
place: true,
|
|
342
|
+
iec: "UDINT",
|
|
343
|
+
},
|
|
344
|
+
],
|
|
345
|
+
frame,
|
|
346
|
+
),
|
|
347
|
+
);
|
|
348
|
+
frame.emitter.close();
|
|
349
|
+
return local
|
|
350
|
+
? { kind: "code", text: local, declared: resultType, place: true }
|
|
351
|
+
: constant(undefined);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** What awaiting a promise gives: the call awaited into a local, or nothing for a void one. */
|
|
355
|
+
private awaitedValue(promise: Value, frame: Frame): Value {
|
|
356
|
+
if (promise.kind !== "code") {
|
|
357
|
+
return promise;
|
|
358
|
+
}
|
|
359
|
+
if (!promise.declared || promise.declared.flags & ts.TypeFlags.Void) {
|
|
360
|
+
frame.emitter.line(mapped`await ${promise.text};`);
|
|
361
|
+
return constant(undefined);
|
|
362
|
+
}
|
|
363
|
+
const local = frame.emitter.temporary();
|
|
364
|
+
frame.emitter.line(mapped`const ${local} = await ${promise.text};`);
|
|
365
|
+
return {
|
|
366
|
+
kind: "code",
|
|
367
|
+
text: local,
|
|
368
|
+
declared: promise.declared,
|
|
369
|
+
place: true,
|
|
370
|
+
iec: promise.iec,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** A link's callback run on `args`; an async callback's promise is awaited too. */
|
|
375
|
+
private handled(callback: ts.Expression, args: Value[], frame: Frame): Value {
|
|
376
|
+
const target = this.expressions.expression(callback, frame);
|
|
377
|
+
const value = this.expressions.invoke(target, args, callback, frame);
|
|
378
|
+
const isAsync =
|
|
379
|
+
target.kind === "function" &&
|
|
380
|
+
ts.canHaveModifiers(target.declaration) &&
|
|
381
|
+
ts
|
|
382
|
+
.getModifiers(target.declaration)
|
|
383
|
+
?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword);
|
|
384
|
+
return isAsync ? this.awaitedValue(value, frame) : value;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Calls a function value with the call's arguments, each evaluated for the parameter it is passed to. */
|
|
388
|
+
private invokeCall(
|
|
389
|
+
target: Value,
|
|
390
|
+
node: ts.CallExpression,
|
|
391
|
+
frame: Frame,
|
|
392
|
+
expected: ts.Type | undefined,
|
|
393
|
+
): Value {
|
|
394
|
+
return this.expressions.invoke(
|
|
395
|
+
target,
|
|
396
|
+
this.expressions.inOrder(
|
|
397
|
+
node.arguments,
|
|
398
|
+
(argument, index) => this.argument(argument, target, index, frame),
|
|
399
|
+
frame,
|
|
400
|
+
),
|
|
401
|
+
node,
|
|
402
|
+
frame,
|
|
403
|
+
expected,
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private argument(
|
|
408
|
+
node: ts.Expression,
|
|
409
|
+
callee: Value,
|
|
410
|
+
index: number,
|
|
411
|
+
frame: Frame,
|
|
412
|
+
): Value {
|
|
413
|
+
const parameter =
|
|
414
|
+
callee.kind === "function" &&
|
|
415
|
+
!ts.isGetAccessorDeclaration(callee.declaration)
|
|
416
|
+
? callee.declaration.parameters[index]
|
|
417
|
+
: undefined;
|
|
418
|
+
return this.expressions.expression(
|
|
419
|
+
node,
|
|
420
|
+
frame,
|
|
421
|
+
parameter && this.context.checker.getTypeAtLocation(parameter),
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
private functionDeclaration(file: string, name: string): FunctionLike {
|
|
426
|
+
const source = this.context.sources.find((item) => item.fileName === file);
|
|
427
|
+
for (const statement of source?.statements ?? []) {
|
|
428
|
+
if (
|
|
429
|
+
ts.isFunctionDeclaration(statement) &&
|
|
430
|
+
statement.name?.text === name
|
|
431
|
+
) {
|
|
432
|
+
return statement;
|
|
433
|
+
}
|
|
434
|
+
if (ts.isVariableStatement(statement)) {
|
|
435
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
436
|
+
if (
|
|
437
|
+
ts.isIdentifier(declaration.name) &&
|
|
438
|
+
declaration.name.text === name &&
|
|
439
|
+
declaration.initializer &&
|
|
440
|
+
(ts.isArrowFunction(declaration.initializer) ||
|
|
441
|
+
ts.isFunctionExpression(declaration.initializer))
|
|
442
|
+
) {
|
|
443
|
+
return declaration.initializer;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
throw new Error(`${file}: no function ${name}.`);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** The SDK declaration file of the function `callee` names, or null when the SDK doesn't declare it. */
|
|
452
|
+
private sdkDeclaration(callee: ts.Identifier): string | null {
|
|
453
|
+
const sdkFile = this.context.sdkFile;
|
|
454
|
+
const symbol = symbolAt(this.context.checker, callee);
|
|
455
|
+
if (symbol === undefined || !declaredBySdk(sdkFile, symbol)) {
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
return (
|
|
459
|
+
symbol.declarations
|
|
460
|
+
?.map((declaration) => declaration.getSourceFile().fileName)
|
|
461
|
+
.find((file) => inSdk(sdkFile, file)) ?? null
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** A method of `@tsplc/compiler/telemetry`'s `telemetry`. */
|
|
466
|
+
private telemetryCall(callee: ts.PropertyAccessExpression): boolean {
|
|
467
|
+
const declaration = this.context.checker.getSymbolAtLocation(callee.name)
|
|
468
|
+
?.declarations?.[0];
|
|
469
|
+
return (
|
|
470
|
+
declaration !== undefined &&
|
|
471
|
+
isTelemetryDeclaration(this.context.sdkFile, declaration)
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Keeps a telemetry call as written, with its values specialized: the analyzer checks and
|
|
477
|
+
* lowers it, and needs its name, message and property names as they are.
|
|
478
|
+
*/
|
|
479
|
+
private telemetry(
|
|
480
|
+
node: ts.CallExpression,
|
|
481
|
+
callee: ts.PropertyAccessExpression,
|
|
482
|
+
frame: Frame,
|
|
483
|
+
): Mapped {
|
|
484
|
+
const { checker, dialect } = this.context;
|
|
485
|
+
const value = (argument: ts.Expression): Part =>
|
|
486
|
+
ts.isCallExpression(argument) &&
|
|
487
|
+
ts.isPropertyAccessExpression(argument.expression) &&
|
|
488
|
+
this.telemetryCall(argument.expression)
|
|
489
|
+
? this.telemetry(argument, argument.expression, frame)
|
|
490
|
+
: // A folded enum field keeps its type, so it is sent as its member's name.
|
|
491
|
+
dialect.code(
|
|
492
|
+
this.expressions.expression(argument, frame),
|
|
493
|
+
checker.getTypeAtLocation(argument),
|
|
494
|
+
);
|
|
495
|
+
const args = node.arguments.map((argument): Part => {
|
|
496
|
+
if (
|
|
497
|
+
ts.isStringLiteral(argument) ||
|
|
498
|
+
ts.isNoSubstitutionTemplateLiteral(argument)
|
|
499
|
+
) {
|
|
500
|
+
return copied(argument);
|
|
501
|
+
}
|
|
502
|
+
if (ts.isObjectLiteralExpression(argument)) {
|
|
503
|
+
return this.telemetryProperties(argument, value, frame);
|
|
504
|
+
}
|
|
505
|
+
const argumentType = checker.getTypeAtLocation(argument);
|
|
506
|
+
if (
|
|
507
|
+
argumentType.flags & ts.TypeFlags.Object &&
|
|
508
|
+
!checker.isArrayType(argumentType)
|
|
509
|
+
) {
|
|
510
|
+
const fields = this.expressions
|
|
511
|
+
.recordFields(argument, frame)
|
|
512
|
+
.map(
|
|
513
|
+
([name, field]) =>
|
|
514
|
+
mapped`${name}: ${dialect.code(field, undefined)}`,
|
|
515
|
+
);
|
|
516
|
+
return mapped`{ ${joined(fields, ", ")} }`;
|
|
517
|
+
}
|
|
518
|
+
return value(argument);
|
|
519
|
+
});
|
|
520
|
+
this.context.imports.addSdk(sdkModules.telemetry, "telemetry");
|
|
521
|
+
return from(
|
|
522
|
+
node,
|
|
523
|
+
mapped`telemetry.${callee.name.text}(${joined(args, ", ")})`,
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
private telemetryProperties(
|
|
528
|
+
argument: ts.ObjectLiteralExpression,
|
|
529
|
+
value: (argument: ts.Expression) => Part,
|
|
530
|
+
frame: Frame,
|
|
531
|
+
): Mapped {
|
|
532
|
+
const { checker, dialect } = this.context;
|
|
533
|
+
const properties = argument.properties.flatMap((property): Part[] => {
|
|
534
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
535
|
+
return [
|
|
536
|
+
mapped`${copied(property.name)}: ${dialect.code(this.expressions.shorthand(property, frame), checker.getTypeAtLocation(property.name))}`,
|
|
537
|
+
];
|
|
538
|
+
}
|
|
539
|
+
if (ts.isSpreadAssignment(property)) {
|
|
540
|
+
return this.expressions
|
|
541
|
+
.recordFields(property.expression, frame)
|
|
542
|
+
.map(
|
|
543
|
+
([name, field]) =>
|
|
544
|
+
mapped`${name}: ${dialect.code(field, undefined)}`,
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
if (!ts.isPropertyAssignment(property)) {
|
|
548
|
+
throw new SpecializeError(property, "Name each telemetry property.");
|
|
549
|
+
}
|
|
550
|
+
return [mapped`${copied(property.name)}: ${value(property.initializer)}`];
|
|
551
|
+
});
|
|
552
|
+
return mapped`{ ${joined(properties, ", ")} }`;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** An SDK function scan code calls, such as the PLC clock. */
|
|
556
|
+
private sdk(
|
|
557
|
+
name: string,
|
|
558
|
+
module: string,
|
|
559
|
+
node: ts.CallExpression,
|
|
560
|
+
frame: Frame,
|
|
561
|
+
): Value {
|
|
562
|
+
// A record built in scan code, such as fetch's `json`, is written as the type it was declared.
|
|
563
|
+
const code = (argument: ts.Expression) => {
|
|
564
|
+
const value = this.expressions.expression(argument, frame);
|
|
565
|
+
if (module === sdkModules.types) {
|
|
566
|
+
refuseTimeArgument(name, value, argument);
|
|
567
|
+
}
|
|
568
|
+
return this.context.dialect.code(
|
|
569
|
+
value,
|
|
570
|
+
value.kind === "virtual"
|
|
571
|
+
? this.context.checker.getTypeAtLocation(argument)
|
|
572
|
+
: undefined,
|
|
573
|
+
);
|
|
574
|
+
};
|
|
575
|
+
// An options record, such as fetch's, is the SDK's own type: its fields are written as given.
|
|
576
|
+
const args = node.arguments.map((argument) =>
|
|
577
|
+
ts.isObjectLiteralExpression(argument)
|
|
578
|
+
? mapped`{ ${joined(
|
|
579
|
+
argument.properties.map((property) => {
|
|
580
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
581
|
+
return mapped`${copied(property.name)}: ${this.context.dialect.code(this.expressions.shorthand(property, frame), undefined)}`;
|
|
582
|
+
}
|
|
583
|
+
if (!ts.isPropertyAssignment(property)) {
|
|
584
|
+
throw new SpecializeError(property, "Name each option.");
|
|
585
|
+
}
|
|
586
|
+
return mapped`${copied(property.name)}: ${code(property.initializer)}`;
|
|
587
|
+
}),
|
|
588
|
+
", ",
|
|
589
|
+
)} }`
|
|
590
|
+
: code(argument),
|
|
591
|
+
);
|
|
592
|
+
// A type argument names the operation's IEC type, which no resolved TypeScript type keeps.
|
|
593
|
+
const typeArguments = (node.typeArguments ?? []).map((argument) =>
|
|
594
|
+
elementaryType(argument.getText())
|
|
595
|
+
? argument.getText()
|
|
596
|
+
: this.context.layouts.text(
|
|
597
|
+
this.context.checker.getTypeFromTypeNode(argument),
|
|
598
|
+
),
|
|
599
|
+
);
|
|
600
|
+
const generic = typeArguments.length ? `<${typeArguments.join(", ")}>` : "";
|
|
601
|
+
this.context.imports.addSdk(module, name);
|
|
602
|
+
const text = mapped`${name}${generic}(${joined(args, ", ")})`;
|
|
603
|
+
const declared = this.context.checker.getTypeAtLocation(node);
|
|
604
|
+
// A record the SDK returns is held in a const its own declaration types, so its fields are read
|
|
605
|
+
// from storage and keep the widths the SDK gives them.
|
|
606
|
+
if (module === sdkModules.async && name === "asyncDiagnostics") {
|
|
607
|
+
const local = frame.emitter.temporary();
|
|
608
|
+
frame.emitter.line(mapped`const ${local} = ${text};`);
|
|
609
|
+
return { kind: "code", text: local, declared, place: true };
|
|
610
|
+
}
|
|
611
|
+
// A numeric operation's result is of the IEC type its type argument names, as `div<UDINT>` is,
|
|
612
|
+
// or else the one its declaration names, as errorCode's UDINT.
|
|
613
|
+
const [width] = typeArguments;
|
|
614
|
+
const iec =
|
|
615
|
+
width && elementaryType(width)
|
|
616
|
+
? width
|
|
617
|
+
: this.context.layouts.iecName(
|
|
618
|
+
symbolAt(this.context.checker, node.expression)?.valueDeclaration,
|
|
619
|
+
);
|
|
620
|
+
return {
|
|
621
|
+
kind: "code",
|
|
622
|
+
text,
|
|
623
|
+
declared,
|
|
624
|
+
...(iec && declared.flags & ts.TypeFlags.NumberLike && { iec }),
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
private math(name: string, node: ts.CallExpression, frame: Frame): Value {
|
|
629
|
+
const args = node.arguments.map((argument) =>
|
|
630
|
+
this.expressions.expression(argument, frame),
|
|
631
|
+
);
|
|
632
|
+
const values = args.flatMap((arg) =>
|
|
633
|
+
arg.kind === "const" && typeof arg.value === "number" ? [arg.value] : [],
|
|
634
|
+
);
|
|
635
|
+
const fold = mathFolds.get(name);
|
|
636
|
+
if (fold && values.length === args.length) {
|
|
637
|
+
return constant(finite(fold(...values), node));
|
|
638
|
+
}
|
|
639
|
+
if (name === "random") {
|
|
640
|
+
throw new SpecializeError(
|
|
641
|
+
node,
|
|
642
|
+
"Math.random has no PLC equivalent: keep a pseudo-random sequence in a UDINT field instead, stepped each time with wrapAdd and wrapMultiply from @tsplc/compiler/types.",
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
const declared = this.context.checker.getTypeAtLocation(node);
|
|
646
|
+
switch (name) {
|
|
647
|
+
case "abs": {
|
|
648
|
+
const [operand] = args;
|
|
649
|
+
if (!operand) {
|
|
650
|
+
throw new SpecializeError(node, "Math.abs needs a value.");
|
|
651
|
+
}
|
|
652
|
+
const held =
|
|
653
|
+
operand.kind === "code"
|
|
654
|
+
? this.expressions.hold(operand, frame, comparedOperand)
|
|
655
|
+
: operand;
|
|
656
|
+
const text = this.context.dialect.code(held, undefined);
|
|
657
|
+
// A float negates exactly; an integer's most negative value wraps to itself, as `0 - x` does.
|
|
658
|
+
const negated = floatIec(held) ? mapped`-${text}` : mapped`0 - ${text}`;
|
|
659
|
+
return {
|
|
660
|
+
kind: "code",
|
|
661
|
+
text: mapped`(${text} < 0 ? ${negated} : ${text})`,
|
|
662
|
+
declared,
|
|
663
|
+
iec: held.kind === "code" ? held.iec : undefined,
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
case "min":
|
|
667
|
+
case "max": {
|
|
668
|
+
const [left, right, ...rest] = args;
|
|
669
|
+
if (left === undefined || right === undefined || rest.length) {
|
|
670
|
+
throw new SpecializeError(node, `Math.${name} takes two values.`);
|
|
671
|
+
}
|
|
672
|
+
// Each operand is read twice, so one that computes is held and runs once.
|
|
673
|
+
const operand = (value: Value) =>
|
|
674
|
+
value.kind === "code"
|
|
675
|
+
? this.expressions.hold(value, frame, comparedOperand)
|
|
676
|
+
: value;
|
|
677
|
+
// Both operands at one width, which the result keeps, so its range proof follows them.
|
|
678
|
+
const {
|
|
679
|
+
texts: [first, second],
|
|
680
|
+
iec,
|
|
681
|
+
} = this.context.dialect.arithmetic(operand(left), operand(right));
|
|
682
|
+
return {
|
|
683
|
+
kind: "code",
|
|
684
|
+
text: mapped`(${first} ${name === "min" ? "<" : ">"} ${second} ? ${first} : ${second})`,
|
|
685
|
+
declared,
|
|
686
|
+
iec,
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
default:
|
|
690
|
+
return this.floatMath(name, args, node, frame);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* A call of a task's `start`, `snapshot`, `cancel` or `reset`, through the binding a kept module
|
|
696
|
+
* defines the task in, which the async lowering compiles as the dialect's task.
|
|
697
|
+
*/
|
|
698
|
+
private taskCall(
|
|
699
|
+
task: ModelNode,
|
|
700
|
+
name: string | null,
|
|
701
|
+
node: ts.CallExpression,
|
|
702
|
+
frame: Frame,
|
|
703
|
+
): Value {
|
|
704
|
+
const { model, checker, dialect, imports } = this.context;
|
|
705
|
+
const root = model.roots.find(
|
|
706
|
+
(item) => item.value.kind === "node" && item.value.id === task.id,
|
|
707
|
+
);
|
|
708
|
+
if (!root || !this.context.isKept(root.file)) {
|
|
709
|
+
throw new SpecializeError(
|
|
710
|
+
node,
|
|
711
|
+
"Define a task in a module-level const of a module that holds only constants, tasks and the service client.",
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
const work = this.workFunction(root.declaration);
|
|
715
|
+
if (work) {
|
|
716
|
+
this.refuseStateInWork(work);
|
|
717
|
+
}
|
|
718
|
+
const local = imports.kept.add(root.file, root.name);
|
|
719
|
+
const args = node.arguments.map((argument) => {
|
|
720
|
+
const contextual = checker.getContextualType(argument);
|
|
721
|
+
return dialect.code(
|
|
722
|
+
this.expressions.expression(argument, frame, contextual),
|
|
723
|
+
contextual,
|
|
724
|
+
);
|
|
725
|
+
});
|
|
726
|
+
// Calling a background task itself runs it and gives a promise of its result, as awaiting does.
|
|
727
|
+
return {
|
|
728
|
+
kind: "code",
|
|
729
|
+
text:
|
|
730
|
+
name === null
|
|
731
|
+
? mapped`${local}(${joined(args, ", ")})`
|
|
732
|
+
: mapped`${local}.${name}(${joined(args, ", ")})`,
|
|
733
|
+
declared:
|
|
734
|
+
name === null
|
|
735
|
+
? checker.getAwaitedType(checker.getTypeAtLocation(node))
|
|
736
|
+
: checker.getTypeAtLocation(node),
|
|
737
|
+
typedByTask: true,
|
|
738
|
+
...(name === null && { iec: this.context.layouts.iecName(work) }),
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/** The named async function a task definition runs. */
|
|
743
|
+
private workFunction(
|
|
744
|
+
declaration: ts.VariableDeclaration | ts.BindingElement,
|
|
745
|
+
): ts.FunctionDeclaration | undefined {
|
|
746
|
+
const init = declaration.initializer;
|
|
747
|
+
const work =
|
|
748
|
+
init && ts.isCallExpression(init) ? init.arguments[0] : undefined;
|
|
749
|
+
const symbol =
|
|
750
|
+
work && ts.isIdentifier(work)
|
|
751
|
+
? this.context.checker.getSymbolAtLocation(work)
|
|
752
|
+
: undefined;
|
|
753
|
+
const fn =
|
|
754
|
+
symbol && unaliased(this.context.checker, symbol).valueDeclaration;
|
|
755
|
+
return fn && ts.isFunctionDeclaration(fn) && fn.body ? fn : undefined;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* A task's work runs on its own, given only its arguments; the program's state is compiled into
|
|
760
|
+
* storage the kept module the task lives in can't import, so the work can't read it.
|
|
761
|
+
*/
|
|
762
|
+
private refuseStateInWork(fn: ts.FunctionDeclaration): void {
|
|
763
|
+
const visit = (node: ts.Node): void => {
|
|
764
|
+
if (ts.isIdentifier(node)) {
|
|
765
|
+
const read = valueSymbol(this.context.checker, node);
|
|
766
|
+
const declared =
|
|
767
|
+
read && unaliased(this.context.checker, read).valueDeclaration;
|
|
768
|
+
const file = declared?.getSourceFile().fileName;
|
|
769
|
+
if (
|
|
770
|
+
declared &&
|
|
771
|
+
file &&
|
|
772
|
+
moduleLevel(declared) &&
|
|
773
|
+
this.context.sources.some((source) => source.fileName === file) &&
|
|
774
|
+
!this.context.isKept(file)
|
|
775
|
+
) {
|
|
776
|
+
throw new SpecializeError(
|
|
777
|
+
node,
|
|
778
|
+
`A task's work runs on its own, given only its arguments, so it can't read ${node.text} from a module of program state: pass what it needs as arguments, and use its result in scan code.`,
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
ts.forEachChild(node, visit);
|
|
783
|
+
};
|
|
784
|
+
ts.forEachChild(fn, visit);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
private serviceCall(
|
|
788
|
+
path: string,
|
|
789
|
+
node: ts.CallExpression,
|
|
790
|
+
frame: Frame,
|
|
791
|
+
): Value {
|
|
792
|
+
const { model, checker, dialect, imports } = this.context;
|
|
793
|
+
const client = model.roots.find((root) => {
|
|
794
|
+
const value = model.node(root.value);
|
|
795
|
+
return value?.captured.kind === "service" && value.captured.path === "";
|
|
796
|
+
});
|
|
797
|
+
if (!client) {
|
|
798
|
+
throw new SpecializeError(
|
|
799
|
+
node,
|
|
800
|
+
"Create the service client in a module-level const, and import it where scan code calls it: the PLC calls the server through that one client, with the types the server's router gives each call.",
|
|
801
|
+
);
|
|
802
|
+
}
|
|
803
|
+
const local = imports.kept.add(client.file, client.name);
|
|
804
|
+
const args = node.arguments.map((argument) => {
|
|
805
|
+
const contextual = checker.getContextualType(argument);
|
|
806
|
+
return dialect.code(
|
|
807
|
+
this.expressions.expression(argument, frame, contextual),
|
|
808
|
+
contextual,
|
|
809
|
+
);
|
|
810
|
+
});
|
|
811
|
+
return {
|
|
812
|
+
kind: "code",
|
|
813
|
+
text: mapped`${local}.${path}(${joined(args, ", ")})`,
|
|
814
|
+
declared: checker.getTypeAtLocation(node),
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/** An array method on elements known now, which unrolls into code for each element. */
|
|
819
|
+
private arrayMethod(
|
|
820
|
+
name: ArrayMethod,
|
|
821
|
+
receiver: Value,
|
|
822
|
+
items: Value[],
|
|
823
|
+
node: ts.CallExpression,
|
|
824
|
+
frame: Frame,
|
|
825
|
+
): Value {
|
|
826
|
+
switch (name) {
|
|
827
|
+
case "entries":
|
|
828
|
+
return {
|
|
829
|
+
kind: "virtual",
|
|
830
|
+
items: items.map((item, index) => ({
|
|
831
|
+
kind: "virtual",
|
|
832
|
+
items: [constant(index), item],
|
|
833
|
+
})),
|
|
834
|
+
};
|
|
835
|
+
case "slice":
|
|
836
|
+
return this.slice(items, node, frame);
|
|
837
|
+
case "map": {
|
|
838
|
+
const apply = this.callback(node, frame);
|
|
839
|
+
return {
|
|
840
|
+
kind: "virtual",
|
|
841
|
+
items: items.map((item, index) =>
|
|
842
|
+
this.settle(apply([item, constant(index)]), frame),
|
|
843
|
+
),
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
case "forEach": {
|
|
847
|
+
const apply = this.callback(node, frame);
|
|
848
|
+
items.forEach((item, index) => {
|
|
849
|
+
const result = apply([item, constant(index)]);
|
|
850
|
+
if (result.kind === "code") {
|
|
851
|
+
frame.emitter.line(mapped`${result.text};`);
|
|
852
|
+
}
|
|
853
|
+
});
|
|
854
|
+
return constant(undefined);
|
|
855
|
+
}
|
|
856
|
+
case "some":
|
|
857
|
+
case "every":
|
|
858
|
+
case "includes":
|
|
859
|
+
return this.expressions.iterators.unrolled(name, items, node, frame);
|
|
860
|
+
case "reduce":
|
|
861
|
+
case "reduceRight":
|
|
862
|
+
return this.reduce(
|
|
863
|
+
name,
|
|
864
|
+
items,
|
|
865
|
+
node,
|
|
866
|
+
frame,
|
|
867
|
+
this.callback(node, frame),
|
|
868
|
+
);
|
|
869
|
+
case "find":
|
|
870
|
+
case "findLast":
|
|
871
|
+
return this.searches.find(
|
|
872
|
+
items,
|
|
873
|
+
node,
|
|
874
|
+
frame,
|
|
875
|
+
{ last: name === "findLast" },
|
|
876
|
+
this.callback(node, frame),
|
|
877
|
+
);
|
|
878
|
+
case "findIndex":
|
|
879
|
+
case "findLastIndex":
|
|
880
|
+
return this.searches.findIndex(
|
|
881
|
+
items,
|
|
882
|
+
frame,
|
|
883
|
+
{ last: name === "findLastIndex" },
|
|
884
|
+
this.callback(node, frame),
|
|
885
|
+
);
|
|
886
|
+
case "indexOf":
|
|
887
|
+
case "lastIndexOf":
|
|
888
|
+
return this.searches.indexOf(items, node, frame, {
|
|
889
|
+
last: name === "lastIndexOf",
|
|
890
|
+
});
|
|
891
|
+
case "fill":
|
|
892
|
+
return this.searches.fill(receiver, items, node, frame);
|
|
893
|
+
case "at":
|
|
894
|
+
return this.searches.at(items, node, frame);
|
|
895
|
+
case "concat":
|
|
896
|
+
return {
|
|
897
|
+
kind: "virtual",
|
|
898
|
+
items: [
|
|
899
|
+
...items,
|
|
900
|
+
...node.arguments.flatMap((argument) => {
|
|
901
|
+
const value = this.expressions.expression(argument, frame);
|
|
902
|
+
return this.expressions.items(value) ?? [value];
|
|
903
|
+
}),
|
|
904
|
+
],
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/** An array method's callback as the step it runs on elements. */
|
|
910
|
+
private callback(
|
|
911
|
+
node: ts.CallExpression,
|
|
912
|
+
frame: Frame,
|
|
913
|
+
): (args: Value[]) => Value {
|
|
914
|
+
const callback = callbackArgument(node, frame, this.expressions);
|
|
915
|
+
return (args) => this.expressions.invoke(callback, args, node, frame);
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/** `slice(start, end)` with bounds setup fixed picks elements now; one decided at run time is read in place. */
|
|
919
|
+
private slice(
|
|
920
|
+
items: readonly Value[],
|
|
921
|
+
node: ts.CallExpression,
|
|
922
|
+
frame: Frame,
|
|
923
|
+
): Value {
|
|
924
|
+
const [start, end] = node.arguments.map((argument) =>
|
|
925
|
+
this.expressions.expression(argument, frame),
|
|
926
|
+
);
|
|
927
|
+
const startIndex = sliceBound(start);
|
|
928
|
+
const endIndex = sliceBound(end);
|
|
929
|
+
if (startIndex === null || endIndex === null) {
|
|
930
|
+
return this.expressions.iterators.sliced(
|
|
931
|
+
items,
|
|
932
|
+
{ start, end },
|
|
933
|
+
node,
|
|
934
|
+
frame,
|
|
935
|
+
);
|
|
936
|
+
}
|
|
937
|
+
return { kind: "virtual", items: items.slice(startIndex, endIndex) };
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/** `reduce(step, initial)` and `reduceRight`: each step's result is the next one's accumulator. */
|
|
941
|
+
private reduce(
|
|
942
|
+
name: "reduce" | "reduceRight",
|
|
943
|
+
items: readonly Value[],
|
|
944
|
+
node: ts.CallExpression,
|
|
945
|
+
frame: Frame,
|
|
946
|
+
apply: (args: Value[]) => Value,
|
|
947
|
+
): Value {
|
|
948
|
+
const order = ordered(items, { last: name === "reduceRight" });
|
|
949
|
+
const [, initialNode] = node.arguments;
|
|
950
|
+
const [first] = order;
|
|
951
|
+
const initial = initialNode
|
|
952
|
+
? this.expressions.expression(initialNode, frame)
|
|
953
|
+
: first?.[1];
|
|
954
|
+
if (!initial) {
|
|
955
|
+
throw new SpecializeError(
|
|
956
|
+
node,
|
|
957
|
+
`${name} of an array with no elements needs an initial value.`,
|
|
958
|
+
);
|
|
959
|
+
}
|
|
960
|
+
return (initialNode ? order : order.slice(1)).reduce(
|
|
961
|
+
(accumulator, [index, item]) =>
|
|
962
|
+
this.settle(apply([accumulator, item, constant(index)]), frame),
|
|
963
|
+
initial,
|
|
964
|
+
);
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* Math on floating values, through the SDK's math functions. An integer argument becomes LREAL
|
|
969
|
+
* exactly, so its proved range carries over; the result is floating, and converts as IEC assignment
|
|
970
|
+
* does where it is stored as a number.
|
|
971
|
+
*/
|
|
972
|
+
floatMath(
|
|
973
|
+
name: string,
|
|
974
|
+
args: readonly Value[],
|
|
975
|
+
node: ts.Expression,
|
|
976
|
+
frame: Frame,
|
|
977
|
+
): Value {
|
|
978
|
+
const declared = this.context.checker.getTypeAtLocation(node);
|
|
979
|
+
const [first, second] = args;
|
|
980
|
+
if (!first) {
|
|
981
|
+
throw new SpecializeError(node, `Math.${name} needs a value.`);
|
|
982
|
+
}
|
|
983
|
+
const type = floatIec(first) ?? (second && floatIec(second)) ?? "LREAL";
|
|
984
|
+
const float = (value: Value) => this.asFloat(value, type);
|
|
985
|
+
const sdk = (fn: string, ...operands: readonly Part[]): Value => {
|
|
986
|
+
this.context.imports.addSdk(sdkModules.types, fn);
|
|
987
|
+
return {
|
|
988
|
+
kind: "code",
|
|
989
|
+
text: mapped`${fn}<${type}>(${joined(operands, ", ")})`,
|
|
990
|
+
declared,
|
|
991
|
+
iec: type,
|
|
992
|
+
};
|
|
993
|
+
};
|
|
994
|
+
const elementary = elementaryNames.get(name);
|
|
995
|
+
if (elementary) {
|
|
996
|
+
return sdk(elementary, float(first));
|
|
997
|
+
}
|
|
998
|
+
if (name === "pow" && second) {
|
|
999
|
+
return sdk("expt", float(first), float(second));
|
|
1000
|
+
}
|
|
1001
|
+
const rounding = roundings.get(name);
|
|
1002
|
+
if (rounding) {
|
|
1003
|
+
if (!floatIec(first)) {
|
|
1004
|
+
return first;
|
|
1005
|
+
}
|
|
1006
|
+
return this.rounded(name, rounding, first, type, frame, declared);
|
|
1007
|
+
}
|
|
1008
|
+
if (name === "sign") {
|
|
1009
|
+
const held = this.expressions.held(first, frame);
|
|
1010
|
+
const zero = floatIec(first) ? "0.0" : "0";
|
|
1011
|
+
const text = this.context.dialect.code(held, undefined);
|
|
1012
|
+
return {
|
|
1013
|
+
kind: "code",
|
|
1014
|
+
text: mapped`(${text} > ${zero} ? 1 : (${text} < ${zero} ? -1 : 0))`,
|
|
1015
|
+
declared,
|
|
1016
|
+
};
|
|
1017
|
+
}
|
|
1018
|
+
throw new SpecializeError(
|
|
1019
|
+
node,
|
|
1020
|
+
`Math.${name} has no PLC function whose result can be proved finite.`,
|
|
1021
|
+
);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
/** A floating value toward zero, down, up or to the nearest whole value, as floating. */
|
|
1025
|
+
private rounded(
|
|
1026
|
+
name: string,
|
|
1027
|
+
rounding: Rounding,
|
|
1028
|
+
value: Value,
|
|
1029
|
+
type: string,
|
|
1030
|
+
frame: Frame,
|
|
1031
|
+
declared: ts.Type,
|
|
1032
|
+
): Value {
|
|
1033
|
+
this.context.imports.addSdk(sdkModules.types, "trunc");
|
|
1034
|
+
const text = this.context.dialect.code(value, undefined);
|
|
1035
|
+
// Math.round takes halves up: the nearest whole value is the floor of the value plus a half.
|
|
1036
|
+
const source = name === "round" ? mapped`(${text} + 0.5)` : text;
|
|
1037
|
+
const held = frame.emitter.temporary();
|
|
1038
|
+
frame.emitter.line(mapped`const ${held}: ${type} = ${source};`);
|
|
1039
|
+
const whole = frame.emitter.temporary();
|
|
1040
|
+
frame.emitter.line(`const ${whole}: ${type} = trunc<${type}>(${held});`);
|
|
1041
|
+
return {
|
|
1042
|
+
kind: "code",
|
|
1043
|
+
text: rounding(whole, held),
|
|
1044
|
+
declared,
|
|
1045
|
+
iec: type,
|
|
1046
|
+
whole: true,
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
/** A value as the floating type: as it is when it already is, else converted exactly. */
|
|
1051
|
+
private asFloat(value: Value, type: string): Part {
|
|
1052
|
+
if (floatIec(value) || value.kind === "const") {
|
|
1053
|
+
return this.context.dialect.code(value, undefined);
|
|
1054
|
+
}
|
|
1055
|
+
this.context.imports.addSdk(sdkModules.types, "exact");
|
|
1056
|
+
return mapped`exact<${type}>(${this.context.dialect.code(value, undefined)})`;
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
/**
|
|
1060
|
+
* `Array.from({ length }, make)` with a length known now: an array of that many values, as setup
|
|
1061
|
+
* builds one. Its length is fixed, so it needs no storage beyond its own.
|
|
1062
|
+
*/
|
|
1063
|
+
private arrayFrom(node: ts.CallExpression, frame: Frame): Value {
|
|
1064
|
+
const [shape, make] = node.arguments;
|
|
1065
|
+
const length =
|
|
1066
|
+
shape &&
|
|
1067
|
+
ts.isObjectLiteralExpression(shape) &&
|
|
1068
|
+
this.expressions.member(
|
|
1069
|
+
this.expressions.expression(shape, frame),
|
|
1070
|
+
"length",
|
|
1071
|
+
shape,
|
|
1072
|
+
frame,
|
|
1073
|
+
);
|
|
1074
|
+
if (
|
|
1075
|
+
!length ||
|
|
1076
|
+
length.kind !== "const" ||
|
|
1077
|
+
typeof length.value !== "number"
|
|
1078
|
+
) {
|
|
1079
|
+
throw new SpecializeError(
|
|
1080
|
+
node,
|
|
1081
|
+
"Array.from needs a length known when the program compiles.",
|
|
1082
|
+
);
|
|
1083
|
+
}
|
|
1084
|
+
const maker = make && this.expressions.expression(make, frame);
|
|
1085
|
+
return {
|
|
1086
|
+
kind: "virtual",
|
|
1087
|
+
items: Array.from({ length: length.value }, (_, index) =>
|
|
1088
|
+
maker
|
|
1089
|
+
? this.expressions.invoke(
|
|
1090
|
+
maker,
|
|
1091
|
+
[constant(undefined), constant(index)],
|
|
1092
|
+
node,
|
|
1093
|
+
frame,
|
|
1094
|
+
)
|
|
1095
|
+
: constant(undefined),
|
|
1096
|
+
),
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
/** `Object.values(record)` of a record setup built: its fields' values, in its order. */
|
|
1101
|
+
private objectValues(node: ts.CallExpression, frame: Frame): Value {
|
|
1102
|
+
const [argument] = node.arguments;
|
|
1103
|
+
const object = argument && this.expressions.expression(argument, frame);
|
|
1104
|
+
if (object?.kind !== "static" || object.node.captured.kind !== "object") {
|
|
1105
|
+
throw new SpecializeError(
|
|
1106
|
+
node,
|
|
1107
|
+
"Object.values needs a record setup built.",
|
|
1108
|
+
);
|
|
1109
|
+
}
|
|
1110
|
+
return {
|
|
1111
|
+
kind: "virtual",
|
|
1112
|
+
items: Object.entries(object.node.captured.fields).map(([field, value]) =>
|
|
1113
|
+
this.context.storage.fromCaptured(value, object.node, {
|
|
1114
|
+
kind: "field",
|
|
1115
|
+
name: field,
|
|
1116
|
+
}),
|
|
1117
|
+
),
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
/**
|
|
1122
|
+
* `structuredClone(value)`: a record or array of its own, as the dialect's `copy` makes one, for
|
|
1123
|
+
* code that changes it and stores it back.
|
|
1124
|
+
*/
|
|
1125
|
+
private cloned(node: ts.CallExpression, frame: Frame): Value {
|
|
1126
|
+
const [argument] = node.arguments;
|
|
1127
|
+
if (!argument) {
|
|
1128
|
+
throw new SpecializeError(node, "structuredClone needs a value.");
|
|
1129
|
+
}
|
|
1130
|
+
const value = this.expressions.expression(argument, frame);
|
|
1131
|
+
// A primitive is a value already: its clone is itself.
|
|
1132
|
+
if (
|
|
1133
|
+
value.kind !== "code" ||
|
|
1134
|
+
(value.declared && !(value.declared.flags & ts.TypeFlags.Object))
|
|
1135
|
+
) {
|
|
1136
|
+
return value;
|
|
1137
|
+
}
|
|
1138
|
+
this.context.imports.addSdk(sdkModules.types, "copy");
|
|
1139
|
+
return {
|
|
1140
|
+
kind: "code",
|
|
1141
|
+
text: mapped`copy(${value.text})`,
|
|
1142
|
+
declared: value.declared,
|
|
1143
|
+
iec: value.iec,
|
|
1144
|
+
layout: value.layout,
|
|
1145
|
+
length: value.length,
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/** Keeps a runtime result in a local, so later code reads it rather than repeating it. */
|
|
1150
|
+
private settle(value: Value, frame: Frame): Value {
|
|
1151
|
+
return value.kind === "code"
|
|
1152
|
+
? this.expressions.hold(value, frame, settledResult)
|
|
1153
|
+
: value;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/** Whether a promise link is awaited, directly or as the promise of an outer link that is. */
|
|
1158
|
+
const awaitedLink = (node: ts.CallExpression): boolean => {
|
|
1159
|
+
const parent = node.parent;
|
|
1160
|
+
if (ts.isAwaitExpression(parent)) {
|
|
1161
|
+
return true;
|
|
1162
|
+
}
|
|
1163
|
+
if (ts.isParenthesizedExpression(parent)) {
|
|
1164
|
+
return ts.isAwaitExpression(parent.parent);
|
|
1165
|
+
}
|
|
1166
|
+
return (
|
|
1167
|
+
ts.isPropertyAccessExpression(parent) &&
|
|
1168
|
+
isPromiseLink(parent.parent) &&
|
|
1169
|
+
awaitedLink(parent.parent)
|
|
1170
|
+
);
|
|
1171
|
+
};
|
|
1172
|
+
|
|
1173
|
+
/** Whether a declaration is a module's own: a top-level variable, function or class. */
|
|
1174
|
+
const moduleLevel = (declaration: ts.Declaration): boolean => {
|
|
1175
|
+
if (ts.isVariableDeclaration(declaration)) {
|
|
1176
|
+
return ts.isSourceFile(declaration.parent.parent.parent);
|
|
1177
|
+
}
|
|
1178
|
+
return (
|
|
1179
|
+
(ts.isFunctionDeclaration(declaration) ||
|
|
1180
|
+
ts.isClassDeclaration(declaration)) &&
|
|
1181
|
+
ts.isSourceFile(declaration.parent)
|
|
1182
|
+
);
|
|
1183
|
+
};
|
|
1184
|
+
|
|
1185
|
+
/**
|
|
1186
|
+
* A TIME or calendar constructor's argument, which the PLC stores as a constant: a duration known
|
|
1187
|
+
* when the program compiles, or a calendar text in its format.
|
|
1188
|
+
*/
|
|
1189
|
+
const refuseTimeArgument = (
|
|
1190
|
+
name: string,
|
|
1191
|
+
value: Value,
|
|
1192
|
+
argument: ts.Expression,
|
|
1193
|
+
): void => {
|
|
1194
|
+
const calendar = calendarFormats.get(name);
|
|
1195
|
+
if (!calendar) {
|
|
1196
|
+
if (durationNames.has(name) && value.kind !== "const") {
|
|
1197
|
+
throw new SpecializeError(
|
|
1198
|
+
argument,
|
|
1199
|
+
`${name}() makes a TIME constant, so its value must be known when the program compiles: a literal, a constant or a field only setup writes. To wait a time scan code computes, sleep in steps until now() has passed it.`,
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
if (
|
|
1205
|
+
value.kind !== "const" ||
|
|
1206
|
+
typeof value.value !== "string" ||
|
|
1207
|
+
!calendarLiteral(calendar.domain, value.value)
|
|
1208
|
+
) {
|
|
1209
|
+
throw new SpecializeError(
|
|
1210
|
+
argument,
|
|
1211
|
+
`${name}() takes a ${calendar.type} written as "${calendar.format}", known when the program compiles.`,
|
|
1212
|
+
);
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
|
|
1216
|
+
/** Array methods scan code calls on arrays setup built, which unroll. */
|
|
1217
|
+
const arrayMethods = [
|
|
1218
|
+
"entries",
|
|
1219
|
+
"slice",
|
|
1220
|
+
"map",
|
|
1221
|
+
"forEach",
|
|
1222
|
+
"some",
|
|
1223
|
+
"every",
|
|
1224
|
+
"reduce",
|
|
1225
|
+
"reduceRight",
|
|
1226
|
+
"find",
|
|
1227
|
+
"findLast",
|
|
1228
|
+
"findIndex",
|
|
1229
|
+
"findLastIndex",
|
|
1230
|
+
"indexOf",
|
|
1231
|
+
"lastIndexOf",
|
|
1232
|
+
"includes",
|
|
1233
|
+
"fill",
|
|
1234
|
+
"at",
|
|
1235
|
+
"concat",
|
|
1236
|
+
] as const;
|
|
1237
|
+
|
|
1238
|
+
type ArrayMethod = (typeof arrayMethods)[number];
|
|
1239
|
+
|
|
1240
|
+
const isArrayMethod = (name: string): name is ArrayMethod =>
|
|
1241
|
+
arrayMethods.some((method) => method === name);
|
|
1242
|
+
|
|
1243
|
+
/** A `map` or `reduce` step's result, held as its local type, or as a boolean when nothing declares one. */
|
|
1244
|
+
const settledResult: Holding = {
|
|
1245
|
+
width: false,
|
|
1246
|
+
widen: true,
|
|
1247
|
+
untyped: "boolean",
|
|
1248
|
+
repeatable: false,
|
|
1249
|
+
};
|
|
1250
|
+
|
|
1251
|
+
/** An operand Math.min or Math.max reads twice, held at its own width. */
|
|
1252
|
+
const comparedOperand: Holding = {
|
|
1253
|
+
width: true,
|
|
1254
|
+
widen: true,
|
|
1255
|
+
untyped: "number",
|
|
1256
|
+
repeatable: false,
|
|
1257
|
+
};
|
|
1258
|
+
|
|
1259
|
+
/** The Math function each of the SDK's elementary functions is called as in scan code. */
|
|
1260
|
+
const mathNames: Readonly<
|
|
1261
|
+
Record<ElementaryFunction, Extract<keyof Math, string>>
|
|
1262
|
+
> = {
|
|
1263
|
+
sqrt: "sqrt",
|
|
1264
|
+
ln: "log",
|
|
1265
|
+
log10: "log10",
|
|
1266
|
+
exp: "exp",
|
|
1267
|
+
sin: "sin",
|
|
1268
|
+
cos: "cos",
|
|
1269
|
+
tan: "tan",
|
|
1270
|
+
asin: "asin",
|
|
1271
|
+
acos: "acos",
|
|
1272
|
+
atan: "atan",
|
|
1273
|
+
};
|
|
1274
|
+
|
|
1275
|
+
const elementaryNames = new Map<string, ElementaryFunction>(
|
|
1276
|
+
elementaryFunctions.map((name) => [mathNames[name], name]),
|
|
1277
|
+
);
|
|
1278
|
+
|
|
1279
|
+
/** The Math functions scan code calls, as a constant call folds: the elementary ones as iec computes them. */
|
|
1280
|
+
const mathFolds = new Map<string, (...values: number[]) => number>([
|
|
1281
|
+
["abs", Math.abs],
|
|
1282
|
+
["min", Math.min],
|
|
1283
|
+
["max", Math.max],
|
|
1284
|
+
["floor", Math.floor],
|
|
1285
|
+
["ceil", Math.ceil],
|
|
1286
|
+
["round", Math.round],
|
|
1287
|
+
["trunc", Math.trunc],
|
|
1288
|
+
["sign", Math.sign],
|
|
1289
|
+
["pow", Math.pow],
|
|
1290
|
+
["hypot", Math.hypot],
|
|
1291
|
+
["atan2", Math.atan2],
|
|
1292
|
+
["log2", Math.log2],
|
|
1293
|
+
["log1p", Math.log1p],
|
|
1294
|
+
["expm1", Math.expm1],
|
|
1295
|
+
["cbrt", Math.cbrt],
|
|
1296
|
+
["sinh", Math.sinh],
|
|
1297
|
+
["cosh", Math.cosh],
|
|
1298
|
+
["tanh", Math.tanh],
|
|
1299
|
+
["asinh", Math.asinh],
|
|
1300
|
+
["acosh", Math.acosh],
|
|
1301
|
+
["atanh", Math.atanh],
|
|
1302
|
+
["clz32", Math.clz32],
|
|
1303
|
+
["imul", Math.imul],
|
|
1304
|
+
["fround", Math.fround],
|
|
1305
|
+
...elementaryFunctions.map((name): [string, (value: number) => number] => [
|
|
1306
|
+
mathNames[name],
|
|
1307
|
+
elementaryValue[name],
|
|
1308
|
+
]),
|
|
1309
|
+
]);
|
|
1310
|
+
|
|
1311
|
+
/** A rounding's result from a value's whole part toward zero, `whole`, and the value, `held`. */
|
|
1312
|
+
type Rounding = (whole: string, held: string) => string;
|
|
1313
|
+
|
|
1314
|
+
const downward: Rounding = (whole, held) =>
|
|
1315
|
+
`(${whole} > ${held} ? ${whole} - 1.0 : ${whole})`;
|
|
1316
|
+
|
|
1317
|
+
/** Math's roundings of a floating value; round is the floor of the value plus a half. */
|
|
1318
|
+
const roundings = new Map<string, Rounding>([
|
|
1319
|
+
["floor", downward],
|
|
1320
|
+
["round", downward],
|
|
1321
|
+
["ceil", (whole, held) => `(${whole} < ${held} ? ${whole} + 1.0 : ${whole})`],
|
|
1322
|
+
["trunc", (whole) => whole],
|
|
1323
|
+
]);
|
|
1324
|
+
|
|
1325
|
+
/** The floating IEC type a value has, if it is floating. */
|
|
1326
|
+
export const floatIec = (value: Value): string | undefined =>
|
|
1327
|
+
value.kind === "code" &&
|
|
1328
|
+
value.iec !== undefined &&
|
|
1329
|
+
elementaryType(value.iec)?.family === "real"
|
|
1330
|
+
? value.iec
|
|
1331
|
+
: undefined;
|
|
1332
|
+
|
|
1333
|
+
/** A slice bound setup fixed, undefined when left out, or null when it is decided at run time. */
|
|
1334
|
+
const sliceBound = (bound: Value | undefined): number | undefined | null => {
|
|
1335
|
+
if (!bound) {
|
|
1336
|
+
return undefined;
|
|
1337
|
+
}
|
|
1338
|
+
if (bound.kind !== "const") {
|
|
1339
|
+
return null;
|
|
1340
|
+
}
|
|
1341
|
+
return typeof bound.value === "number" ? bound.value : undefined;
|
|
1342
|
+
};
|