@tsplc/compiler 0.2.17 → 0.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -0,0 +1,992 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { from, joined, mapped, type Mapped } from "../positions.ts";
|
|
3
|
+
import { unaliased } from "../sdk.ts";
|
|
4
|
+
import { CallSpecializer } from "./calls.ts";
|
|
5
|
+
import type { DialectText } from "./dialect.ts";
|
|
6
|
+
import type { Frame } from "./frame.ts";
|
|
7
|
+
import type { EntryImports } from "./imports.ts";
|
|
8
|
+
import type { Initializers } from "./initializers.ts";
|
|
9
|
+
import { recordTypes, type Layouts } from "./layouts.ts";
|
|
10
|
+
import type { Model, ModelNode } from "./model.ts";
|
|
11
|
+
import type { Storage } from "./storage.ts";
|
|
12
|
+
import {
|
|
13
|
+
constant,
|
|
14
|
+
repeatable,
|
|
15
|
+
SpecializeError,
|
|
16
|
+
type Env,
|
|
17
|
+
type FunctionLike,
|
|
18
|
+
type Value,
|
|
19
|
+
} from "./values.ts";
|
|
20
|
+
import { isAssignment } from "./writes.ts";
|
|
21
|
+
|
|
22
|
+
/** What every part of scan-code specialization shares. */
|
|
23
|
+
export interface SpecializeContext {
|
|
24
|
+
model: Model;
|
|
25
|
+
checker: ts.TypeChecker;
|
|
26
|
+
layouts: Layouts;
|
|
27
|
+
storage: Storage;
|
|
28
|
+
initializers: Initializers;
|
|
29
|
+
imports: EntryImports;
|
|
30
|
+
dialect: DialectText;
|
|
31
|
+
/** The SDK's globals declaration; its other declarations sit beside it. */
|
|
32
|
+
sdkFile: string;
|
|
33
|
+
/** The program's runtime sources, where the functions setup stored are declared. */
|
|
34
|
+
sources: readonly ts.SourceFile[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A call to a specialized routine: its name, the runtime arguments it takes and its result type. */
|
|
38
|
+
export interface RoutineCall {
|
|
39
|
+
name: string;
|
|
40
|
+
args: (string | Mapped)[];
|
|
41
|
+
returns: ts.Type | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Specializes a called function for its receiver and known arguments. */
|
|
45
|
+
export interface RoutineTable {
|
|
46
|
+
routine(
|
|
47
|
+
declaration: FunctionLike,
|
|
48
|
+
self: ModelNode | undefined,
|
|
49
|
+
args: Value[],
|
|
50
|
+
closure?: Env,
|
|
51
|
+
): RoutineCall;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Evaluates expressions against the setup graph, and writes what remains as dialect text. */
|
|
55
|
+
export class ExpressionSpecializer {
|
|
56
|
+
private readonly calls: CallSpecializer;
|
|
57
|
+
private readonly services = new Map<string, ModelNode>();
|
|
58
|
+
private readonly tables = new Map<number, string>();
|
|
59
|
+
readonly tableDeclarations: Mapped[] = [];
|
|
60
|
+
private readonly model: Model;
|
|
61
|
+
private readonly checker: ts.TypeChecker;
|
|
62
|
+
private readonly dialect: DialectText;
|
|
63
|
+
private readonly layouts: Layouts;
|
|
64
|
+
private readonly storage: Storage;
|
|
65
|
+
private readonly initializers: Initializers;
|
|
66
|
+
|
|
67
|
+
constructor(
|
|
68
|
+
context: SpecializeContext,
|
|
69
|
+
private readonly routineTable: RoutineTable,
|
|
70
|
+
) {
|
|
71
|
+
this.model = context.model;
|
|
72
|
+
this.checker = context.checker;
|
|
73
|
+
this.dialect = context.dialect;
|
|
74
|
+
this.layouts = context.layouts;
|
|
75
|
+
this.storage = context.storage;
|
|
76
|
+
this.initializers = context.initializers;
|
|
77
|
+
this.calls = new CallSpecializer(context, this);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
expression(node: ts.Expression, frame: Frame, expected?: ts.Type): Value {
|
|
81
|
+
const value = this.evaluate(node, frame, expected);
|
|
82
|
+
if (value.kind !== "code") {
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
// A name maps to where it is read, not to where its value came from.
|
|
86
|
+
return {
|
|
87
|
+
...value,
|
|
88
|
+
text: from(node, ts.isIdentifier(node) ? String(value.text) : value.text),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private evaluate(
|
|
93
|
+
node: ts.Expression,
|
|
94
|
+
frame: Frame,
|
|
95
|
+
expected?: ts.Type,
|
|
96
|
+
): Value {
|
|
97
|
+
if (
|
|
98
|
+
ts.isParenthesizedExpression(node) ||
|
|
99
|
+
ts.isNonNullExpression(node) ||
|
|
100
|
+
ts.isAsExpression(node) ||
|
|
101
|
+
ts.isSatisfiesExpression(node)
|
|
102
|
+
) {
|
|
103
|
+
return this.expression(node.expression, frame, expected);
|
|
104
|
+
}
|
|
105
|
+
if (ts.isNumericLiteral(node)) {
|
|
106
|
+
return constant(Number(node.text));
|
|
107
|
+
}
|
|
108
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
109
|
+
return constant(node.text);
|
|
110
|
+
}
|
|
111
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword) {
|
|
112
|
+
return constant(true);
|
|
113
|
+
}
|
|
114
|
+
if (node.kind === ts.SyntaxKind.FalseKeyword) {
|
|
115
|
+
return constant(false);
|
|
116
|
+
}
|
|
117
|
+
if (ts.isBigIntLiteral(node)) {
|
|
118
|
+
return constant(BigInt(node.text.slice(0, -1)));
|
|
119
|
+
}
|
|
120
|
+
if (node.kind === ts.SyntaxKind.ThisKeyword) {
|
|
121
|
+
if (!frame.self) {
|
|
122
|
+
throw new SpecializeError(node, "`this` outside a method.");
|
|
123
|
+
}
|
|
124
|
+
return { kind: "static", node: frame.self };
|
|
125
|
+
}
|
|
126
|
+
if (ts.isIdentifier(node)) {
|
|
127
|
+
return this.identifier(node, frame);
|
|
128
|
+
}
|
|
129
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
130
|
+
const member = this.checker.getSymbolAtLocation(
|
|
131
|
+
node.name,
|
|
132
|
+
)?.valueDeclaration;
|
|
133
|
+
if (member && ts.isEnumMember(member)) {
|
|
134
|
+
return constant(this.checker.getConstantValue(member));
|
|
135
|
+
}
|
|
136
|
+
const object = this.expression(node.expression, frame);
|
|
137
|
+
if (
|
|
138
|
+
node.questionDotToken &&
|
|
139
|
+
object.kind === "const" &&
|
|
140
|
+
object.value === undefined
|
|
141
|
+
) {
|
|
142
|
+
return constant(undefined);
|
|
143
|
+
}
|
|
144
|
+
return this.narrowed(
|
|
145
|
+
this.member(object, node.name.text, node, frame),
|
|
146
|
+
node,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
if (ts.isElementAccessExpression(node)) {
|
|
150
|
+
const object = this.expression(node.expression, frame);
|
|
151
|
+
const index = this.expression(node.argumentExpression, frame);
|
|
152
|
+
return this.element(object, index, node, frame);
|
|
153
|
+
}
|
|
154
|
+
if (ts.isCallExpression(node)) {
|
|
155
|
+
return this.calls.call(node, frame, expected);
|
|
156
|
+
}
|
|
157
|
+
if (ts.isAwaitExpression(node)) {
|
|
158
|
+
const value = this.expression(node.expression, frame);
|
|
159
|
+
return {
|
|
160
|
+
kind: "code",
|
|
161
|
+
text: mapped`await ${this.dialect.code(value, undefined)}`,
|
|
162
|
+
declared: this.checker.getTypeAtLocation(node),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
if (ts.isPrefixUnaryExpression(node)) {
|
|
166
|
+
return this.unary(node, frame);
|
|
167
|
+
}
|
|
168
|
+
if (ts.isBinaryExpression(node)) {
|
|
169
|
+
return this.binary(node, frame, expected);
|
|
170
|
+
}
|
|
171
|
+
if (ts.isConditionalExpression(node)) {
|
|
172
|
+
const condition = this.expression(node.condition, frame);
|
|
173
|
+
if (condition.kind === "const") {
|
|
174
|
+
return this.expression(
|
|
175
|
+
condition.value ? node.whenTrue : node.whenFalse,
|
|
176
|
+
frame,
|
|
177
|
+
expected,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
const declared = expected ?? this.checker.getTypeAtLocation(node);
|
|
181
|
+
const whenTrue = this.expression(node.whenTrue, frame, declared);
|
|
182
|
+
const whenFalse = this.expression(node.whenFalse, frame, declared);
|
|
183
|
+
return {
|
|
184
|
+
kind: "code",
|
|
185
|
+
text: mapped`(${this.dialect.condition(condition, node.condition)} ? ${this.dialect.code(whenTrue, declared)} : ${this.dialect.code(whenFalse, declared)})`,
|
|
186
|
+
declared,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
|
|
190
|
+
return {
|
|
191
|
+
kind: "function",
|
|
192
|
+
declaration: node,
|
|
193
|
+
self: frame.self,
|
|
194
|
+
env: frame.env,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
198
|
+
return this.objectLiteral(node, frame);
|
|
199
|
+
}
|
|
200
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
201
|
+
const items: Value[] = [];
|
|
202
|
+
for (const element of node.elements) {
|
|
203
|
+
if (ts.isSpreadElement(element)) {
|
|
204
|
+
const spread = this.items(this.expression(element.expression, frame));
|
|
205
|
+
if (!spread) {
|
|
206
|
+
throw new SpecializeError(element, "Spread an array setup built.");
|
|
207
|
+
}
|
|
208
|
+
items.push(...spread);
|
|
209
|
+
} else {
|
|
210
|
+
items.push(this.expression(element, frame));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return { kind: "virtual", items };
|
|
214
|
+
}
|
|
215
|
+
if (ts.isTemplateExpression(node)) {
|
|
216
|
+
throw new SpecializeError(
|
|
217
|
+
node,
|
|
218
|
+
"Template strings are not supported in scan code.",
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
throw new SpecializeError(
|
|
222
|
+
node,
|
|
223
|
+
`${ts.SyntaxKind[node.kind]} is not supported in scan code.`,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private identifier(node: ts.Identifier, frame: Frame): Value {
|
|
228
|
+
if (node.text === "undefined") {
|
|
229
|
+
return constant(undefined);
|
|
230
|
+
}
|
|
231
|
+
const symbol = this.checker.getSymbolAtLocation(node);
|
|
232
|
+
if (!symbol) {
|
|
233
|
+
throw new SpecializeError(node, `${node.text} is not declared.`);
|
|
234
|
+
}
|
|
235
|
+
const local = frame.env.get(symbol);
|
|
236
|
+
if (local) {
|
|
237
|
+
return this.narrowed(local, node);
|
|
238
|
+
}
|
|
239
|
+
const target = unaliased(this.checker, symbol);
|
|
240
|
+
const own = frame.env.get(target);
|
|
241
|
+
if (own) {
|
|
242
|
+
return own;
|
|
243
|
+
}
|
|
244
|
+
const declaration = target.valueDeclaration ?? target.declarations?.[0];
|
|
245
|
+
if (!declaration) {
|
|
246
|
+
throw new SpecializeError(node, `${node.text} has no declaration.`);
|
|
247
|
+
}
|
|
248
|
+
if (ts.isFunctionDeclaration(declaration)) {
|
|
249
|
+
return { kind: "function", declaration };
|
|
250
|
+
}
|
|
251
|
+
if (ts.isVariableDeclaration(declaration)) {
|
|
252
|
+
const initializer = declaration.initializer;
|
|
253
|
+
if (
|
|
254
|
+
initializer &&
|
|
255
|
+
(ts.isArrowFunction(initializer) ||
|
|
256
|
+
ts.isFunctionExpression(initializer))
|
|
257
|
+
) {
|
|
258
|
+
return { kind: "function", declaration: initializer };
|
|
259
|
+
}
|
|
260
|
+
const root = this.model.roots.find(
|
|
261
|
+
(item) => item.declaration === declaration,
|
|
262
|
+
);
|
|
263
|
+
if (root) {
|
|
264
|
+
return this.storage.rootValue(root);
|
|
265
|
+
}
|
|
266
|
+
if (
|
|
267
|
+
ts.isVariableStatement(declaration.parent.parent) &&
|
|
268
|
+
declaration.parent.parent.parent &&
|
|
269
|
+
ts.isSourceFile(declaration.parent.parent.parent)
|
|
270
|
+
) {
|
|
271
|
+
if (initializer) {
|
|
272
|
+
return this.expression(initializer, { ...frame, env: new Map() });
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (ts.isEnumMember(declaration)) {
|
|
277
|
+
return constant(this.checker.getConstantValue(declaration));
|
|
278
|
+
}
|
|
279
|
+
throw new SpecializeError(
|
|
280
|
+
node,
|
|
281
|
+
`${node.text} is not available in scan code.`,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
member(object: Value, name: string, at: ts.Node, frame: Frame): Value {
|
|
286
|
+
if (object.kind === "static") {
|
|
287
|
+
const node = object.node;
|
|
288
|
+
const captured = node.captured;
|
|
289
|
+
if (captured.kind === "service") {
|
|
290
|
+
return {
|
|
291
|
+
kind: "static",
|
|
292
|
+
node: this.serviceNode(
|
|
293
|
+
captured.path ? `${captured.path}.${name}` : name,
|
|
294
|
+
),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
if (captured.kind === "instance") {
|
|
298
|
+
const declaration = this.model.classOf(node);
|
|
299
|
+
const accessor = declaration.members.find(
|
|
300
|
+
(member): member is ts.GetAccessorDeclaration =>
|
|
301
|
+
ts.isGetAccessorDeclaration(member) &&
|
|
302
|
+
member.name.getText() === name,
|
|
303
|
+
);
|
|
304
|
+
if (accessor) {
|
|
305
|
+
return this.invoke(
|
|
306
|
+
{ kind: "function", declaration: accessor, self: node },
|
|
307
|
+
[],
|
|
308
|
+
at,
|
|
309
|
+
frame,
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
const method = declaration.members.find(
|
|
313
|
+
(member): member is ts.MethodDeclaration =>
|
|
314
|
+
ts.isMethodDeclaration(member) && member.name.getText() === name,
|
|
315
|
+
);
|
|
316
|
+
if (method) {
|
|
317
|
+
return { kind: "function", declaration: method, self: node };
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (captured.kind === "array" && name === "length") {
|
|
321
|
+
return constant(captured.items.length);
|
|
322
|
+
}
|
|
323
|
+
if (captured.kind === "object" || captured.kind === "instance") {
|
|
324
|
+
const field = captured.fields[name];
|
|
325
|
+
if (!field) {
|
|
326
|
+
if (captured.kind === "object") {
|
|
327
|
+
return constant(undefined);
|
|
328
|
+
}
|
|
329
|
+
throw new SpecializeError(at, `${name} is not set during setup.`);
|
|
330
|
+
}
|
|
331
|
+
return this.storage.fromCaptured(field, node, { kind: "field", name });
|
|
332
|
+
}
|
|
333
|
+
throw new SpecializeError(at, `${name} cannot be read from this value.`);
|
|
334
|
+
}
|
|
335
|
+
if (object.kind === "virtual") {
|
|
336
|
+
if (name === "length" && object.items) {
|
|
337
|
+
return constant(object.items.length);
|
|
338
|
+
}
|
|
339
|
+
const field = object.fields?.get(name);
|
|
340
|
+
if (field) {
|
|
341
|
+
return field;
|
|
342
|
+
}
|
|
343
|
+
throw new SpecializeError(at, `${name} is not set.`);
|
|
344
|
+
}
|
|
345
|
+
if (object.kind === "code") {
|
|
346
|
+
const declared =
|
|
347
|
+
object.declared && this.fieldDeclared(object.declared, name, at);
|
|
348
|
+
const property =
|
|
349
|
+
object.declared &&
|
|
350
|
+
this.checker.getPropertyOfType(
|
|
351
|
+
this.checker.getNonNullableType(object.declared),
|
|
352
|
+
name,
|
|
353
|
+
);
|
|
354
|
+
return {
|
|
355
|
+
kind: "code",
|
|
356
|
+
text: mapped`${object.text}.${name}`,
|
|
357
|
+
declared,
|
|
358
|
+
place: object.place,
|
|
359
|
+
iec: this.layouts.iecName(property?.valueDeclaration),
|
|
360
|
+
elementIec: this.layouts.elementIec(property?.valueDeclaration),
|
|
361
|
+
layout:
|
|
362
|
+
property && declared && this.checker.isArrayType(declared)
|
|
363
|
+
? this.layouts.fieldText(property)
|
|
364
|
+
: undefined,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
if (object.kind === "const" && object.value === undefined) {
|
|
368
|
+
throw new SpecializeError(at, `Reading ${name} of undefined.`);
|
|
369
|
+
}
|
|
370
|
+
throw new SpecializeError(at, `${name} cannot be read from this value.`);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** An optional's value where TypeScript has narrowed out undefined, as `x` reads after `x !== undefined`. */
|
|
374
|
+
private narrowed(value: Value, at: ts.Node): Value {
|
|
375
|
+
if (
|
|
376
|
+
value.kind !== "code" ||
|
|
377
|
+
!value.declared ||
|
|
378
|
+
!this.layouts.isOptionalRecord(value.declared)
|
|
379
|
+
) {
|
|
380
|
+
return value;
|
|
381
|
+
}
|
|
382
|
+
const type = this.checker.getTypeAtLocation(at);
|
|
383
|
+
if (
|
|
384
|
+
type.isUnion() &&
|
|
385
|
+
type.types.some((part) => part.flags & ts.TypeFlags.Undefined)
|
|
386
|
+
) {
|
|
387
|
+
return value;
|
|
388
|
+
}
|
|
389
|
+
if (type.flags & ts.TypeFlags.Undefined) {
|
|
390
|
+
return value;
|
|
391
|
+
}
|
|
392
|
+
return {
|
|
393
|
+
kind: "code",
|
|
394
|
+
text: mapped`${value.text}.value`,
|
|
395
|
+
declared: this.checker.getNonNullableType(value.declared),
|
|
396
|
+
place: value.place,
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** The declared type of a field of a runtime record, through optionals and tagged unions. */
|
|
401
|
+
private fieldDeclared(
|
|
402
|
+
type: ts.Type,
|
|
403
|
+
name: string,
|
|
404
|
+
at: ts.Node,
|
|
405
|
+
): ts.Type | undefined {
|
|
406
|
+
const defined = this.checker.getNonNullableType(type);
|
|
407
|
+
const tagged = this.layouts.tagged(defined);
|
|
408
|
+
if (tagged) {
|
|
409
|
+
return name === tagged.tag
|
|
410
|
+
? this.layouts.tagType(defined, tagged)
|
|
411
|
+
: this.layouts.taggedFieldType(defined, tagged, name);
|
|
412
|
+
}
|
|
413
|
+
const property = this.checker.getPropertyOfType(defined, name);
|
|
414
|
+
if (!property) {
|
|
415
|
+
throw new SpecializeError(
|
|
416
|
+
at,
|
|
417
|
+
`${name} is not a field of ${this.checker.typeToString(type)}.`,
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
return this.checker.getTypeOfSymbol(property);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
private serviceNode(path: string): ModelNode {
|
|
424
|
+
const known = this.services.get(path);
|
|
425
|
+
if (known) {
|
|
426
|
+
return known;
|
|
427
|
+
}
|
|
428
|
+
const node: ModelNode = {
|
|
429
|
+
id: -1 - this.services.size,
|
|
430
|
+
captured: { kind: "service", path },
|
|
431
|
+
};
|
|
432
|
+
this.services.set(path, node);
|
|
433
|
+
return node;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private element(
|
|
437
|
+
object: Value,
|
|
438
|
+
index: Value,
|
|
439
|
+
at: ts.Node,
|
|
440
|
+
frame: Frame,
|
|
441
|
+
): Value {
|
|
442
|
+
if (index.kind === "const" && typeof index.value === "number") {
|
|
443
|
+
if (object.kind === "virtual" && object.items) {
|
|
444
|
+
const item = object.items[index.value];
|
|
445
|
+
if (!item) {
|
|
446
|
+
throw new SpecializeError(
|
|
447
|
+
at,
|
|
448
|
+
`Index ${index.value} is past the end.`,
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
return item;
|
|
452
|
+
}
|
|
453
|
+
if (object.kind === "static" && object.node.captured.kind === "array") {
|
|
454
|
+
const item = object.node.captured.items[index.value];
|
|
455
|
+
if (!item) {
|
|
456
|
+
return constant(undefined);
|
|
457
|
+
}
|
|
458
|
+
return this.storage.fromCaptured(item, object.node, {
|
|
459
|
+
kind: "index",
|
|
460
|
+
index: index.value,
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
if (object.kind === "static" && object.node.captured.kind === "object") {
|
|
464
|
+
const field = object.node.captured.fields[String(index.value)];
|
|
465
|
+
if (!field) {
|
|
466
|
+
return constant(undefined);
|
|
467
|
+
}
|
|
468
|
+
return this.storage.fromCaptured(field, object.node, {
|
|
469
|
+
kind: "field",
|
|
470
|
+
name: String(index.value),
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
if (object.kind === "static") {
|
|
475
|
+
const node = object.node;
|
|
476
|
+
// A runtime index into stored data, or into a fixed table of plain values.
|
|
477
|
+
const declared =
|
|
478
|
+
node.type &&
|
|
479
|
+
(node.captured.kind === "array"
|
|
480
|
+
? this.model.elementType(node.type)
|
|
481
|
+
: recordTypes(node.type)?.value);
|
|
482
|
+
const text = this.storage.storedNode(node)
|
|
483
|
+
? this.storage.pathText(node)
|
|
484
|
+
: this.table(node, at);
|
|
485
|
+
return {
|
|
486
|
+
kind: "code",
|
|
487
|
+
text: mapped`${text}[${this.dialect.code(index, undefined)}]`,
|
|
488
|
+
declared,
|
|
489
|
+
place: true,
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
if (object.kind === "code") {
|
|
493
|
+
const declared =
|
|
494
|
+
object.declared &&
|
|
495
|
+
(this.model.elementType(object.declared) ??
|
|
496
|
+
recordTypes(object.declared)?.value);
|
|
497
|
+
return {
|
|
498
|
+
kind: "code",
|
|
499
|
+
text: mapped`${object.text}[${this.dialect.code(index, undefined)}]`,
|
|
500
|
+
declared,
|
|
501
|
+
place: object.place,
|
|
502
|
+
iec: object.elementIec,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
if (object.kind === "virtual" && object.items) {
|
|
506
|
+
const text = this.materialize(object, at, frame);
|
|
507
|
+
return {
|
|
508
|
+
kind: "code",
|
|
509
|
+
text: mapped`${text}[${this.dialect.code(index, undefined)}]`,
|
|
510
|
+
declared: this.checker.getTypeAtLocation(at),
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
throw new SpecializeError(at, "This value cannot be indexed.");
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** A fixed table setup built, emitted once, where scan code first indexes it at runtime. */
|
|
517
|
+
private table(node: ModelNode, at: ts.Node): string {
|
|
518
|
+
const known = this.tables.get(node.id);
|
|
519
|
+
if (known) {
|
|
520
|
+
return known;
|
|
521
|
+
}
|
|
522
|
+
if (!node.type) {
|
|
523
|
+
throw new Error("A table needs a declared type.");
|
|
524
|
+
}
|
|
525
|
+
const name = `tsplcTable${this.tables.size + 1}`;
|
|
526
|
+
this.tables.set(node.id, name);
|
|
527
|
+
const type = this.layouts.text(node.type, { kind: "node", id: node.id });
|
|
528
|
+
this.tableDeclarations.push(
|
|
529
|
+
from(
|
|
530
|
+
at,
|
|
531
|
+
`const ${name}: ${type} = ${this.initializers.valueText({ kind: "node", id: node.id }, node.type, type)};`,
|
|
532
|
+
),
|
|
533
|
+
);
|
|
534
|
+
return name;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** Stores a scan-built array in a local, for indexing at runtime. */
|
|
538
|
+
private materialize(value: Value, at: ts.Node, frame: Frame): string {
|
|
539
|
+
const type = this.checker.getTypeAtLocation(at);
|
|
540
|
+
const element = this.model.elementType(type);
|
|
541
|
+
if (!element || value.kind !== "virtual" || !value.items) {
|
|
542
|
+
throw new SpecializeError(at, "Not an array.");
|
|
543
|
+
}
|
|
544
|
+
const local = frame.emitter.temporary();
|
|
545
|
+
frame.emitter.line(
|
|
546
|
+
mapped`const ${local}: FixedArray<${this.layouts.text(element)}, ${Math.max(value.items.length, 1)}> = [${joined(
|
|
547
|
+
value.items.map((item) => this.dialect.code(item, element)),
|
|
548
|
+
", ",
|
|
549
|
+
)}];`,
|
|
550
|
+
);
|
|
551
|
+
return local;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
private objectLiteral(node: ts.ObjectLiteralExpression, frame: Frame): Value {
|
|
555
|
+
const fields = new Map<string, Value>();
|
|
556
|
+
for (const property of node.properties) {
|
|
557
|
+
if (ts.isPropertyAssignment(property)) {
|
|
558
|
+
fields.set(
|
|
559
|
+
property.name.getText(),
|
|
560
|
+
this.expression(
|
|
561
|
+
property.initializer,
|
|
562
|
+
frame,
|
|
563
|
+
this.checker.getContextualType(property.initializer),
|
|
564
|
+
),
|
|
565
|
+
);
|
|
566
|
+
} else if (ts.isShorthandPropertyAssignment(property)) {
|
|
567
|
+
const symbol = this.checker.getShorthandAssignmentValueSymbol(property);
|
|
568
|
+
const local = symbol && frame.env.get(symbol);
|
|
569
|
+
fields.set(
|
|
570
|
+
property.name.text,
|
|
571
|
+
local ?? this.expression(property.name, frame),
|
|
572
|
+
);
|
|
573
|
+
} else {
|
|
574
|
+
throw new SpecializeError(property, "Write each field of a record.");
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
return { kind: "virtual", fields };
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
private unary(node: ts.PrefixUnaryExpression, frame: Frame): Value {
|
|
581
|
+
const operand = this.expression(node.operand, frame);
|
|
582
|
+
switch (node.operator) {
|
|
583
|
+
case ts.SyntaxKind.ExclamationToken:
|
|
584
|
+
if (operand.kind === "const") {
|
|
585
|
+
return constant(!operand.value);
|
|
586
|
+
}
|
|
587
|
+
if (operand.kind === "static" || operand.kind === "function") {
|
|
588
|
+
return constant(false);
|
|
589
|
+
}
|
|
590
|
+
return {
|
|
591
|
+
kind: "code",
|
|
592
|
+
text: mapped`!${this.dialect.condition(operand, node.operand)}`,
|
|
593
|
+
declared: this.checker.getBooleanType(),
|
|
594
|
+
};
|
|
595
|
+
case ts.SyntaxKind.MinusToken:
|
|
596
|
+
if (operand.kind === "const" && typeof operand.value === "number") {
|
|
597
|
+
return constant(-operand.value);
|
|
598
|
+
}
|
|
599
|
+
return {
|
|
600
|
+
kind: "code",
|
|
601
|
+
text: mapped`-${this.dialect.code(operand, undefined)}`,
|
|
602
|
+
declared: this.checker.getTypeAtLocation(node),
|
|
603
|
+
};
|
|
604
|
+
case ts.SyntaxKind.PlusToken:
|
|
605
|
+
return operand;
|
|
606
|
+
default:
|
|
607
|
+
throw new SpecializeError(
|
|
608
|
+
node,
|
|
609
|
+
`${ts.tokenToString(node.operator)} is not supported.`,
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
private binary(
|
|
615
|
+
node: ts.BinaryExpression,
|
|
616
|
+
frame: Frame,
|
|
617
|
+
expected?: ts.Type,
|
|
618
|
+
): Value {
|
|
619
|
+
const kind = node.operatorToken.kind;
|
|
620
|
+
if (
|
|
621
|
+
kind === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
622
|
+
kind === ts.SyntaxKind.BarBarToken
|
|
623
|
+
) {
|
|
624
|
+
const left = this.expression(node.left, frame);
|
|
625
|
+
const and = kind === ts.SyntaxKind.AmpersandAmpersandToken;
|
|
626
|
+
if (left.kind === "const") {
|
|
627
|
+
if (Boolean(left.value) !== and) {
|
|
628
|
+
return left;
|
|
629
|
+
}
|
|
630
|
+
return this.expression(node.right, frame, expected);
|
|
631
|
+
}
|
|
632
|
+
if (left.kind === "static" || left.kind === "function") {
|
|
633
|
+
return and ? this.expression(node.right, frame, expected) : left;
|
|
634
|
+
}
|
|
635
|
+
const right = this.expression(node.right, frame, expected);
|
|
636
|
+
if (right.kind === "const" && typeof right.value === "boolean") {
|
|
637
|
+
if (right.value === and) {
|
|
638
|
+
return {
|
|
639
|
+
kind: "code",
|
|
640
|
+
text: this.dialect.condition(left, node.left),
|
|
641
|
+
declared: this.checker.getBooleanType(),
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
return constant(!and);
|
|
645
|
+
}
|
|
646
|
+
return {
|
|
647
|
+
kind: "code",
|
|
648
|
+
text: mapped`(${this.dialect.condition(left, node.left)} ${and ? "&&" : "||"} ${this.dialect.condition(right, node.right)})`,
|
|
649
|
+
declared: this.checker.getBooleanType(),
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
if (kind === ts.SyntaxKind.QuestionQuestionToken) {
|
|
653
|
+
const left = this.expression(node.left, frame);
|
|
654
|
+
if (left.kind === "const") {
|
|
655
|
+
return left.value === undefined
|
|
656
|
+
? this.expression(node.right, frame, expected)
|
|
657
|
+
: left;
|
|
658
|
+
}
|
|
659
|
+
if (left.kind === "static") {
|
|
660
|
+
return left;
|
|
661
|
+
}
|
|
662
|
+
const declared = left.kind === "code" ? left.declared : undefined;
|
|
663
|
+
const right = this.expression(
|
|
664
|
+
node.right,
|
|
665
|
+
frame,
|
|
666
|
+
declared && this.checker.getNonNullableType(declared),
|
|
667
|
+
);
|
|
668
|
+
return {
|
|
669
|
+
kind: "code",
|
|
670
|
+
text: mapped`(${this.dialect.isUndefinedText(this.dialect.code(left, declared), declared)} ? ${this.dialect.code(right, declared && this.checker.getNonNullableType(declared))} : ${this.dialect.code(left, undefined)}${declared && this.layouts.isOptionalRecord(declared) ? ".value" : ""})`,
|
|
671
|
+
declared: declared && this.checker.getNonNullableType(declared),
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
if (isAssignment(kind)) {
|
|
675
|
+
throw new SpecializeError(node, "Assign in a statement of its own.");
|
|
676
|
+
}
|
|
677
|
+
const equality = [
|
|
678
|
+
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
679
|
+
ts.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
680
|
+
ts.SyntaxKind.EqualsEqualsToken,
|
|
681
|
+
ts.SyntaxKind.ExclamationEqualsToken,
|
|
682
|
+
].includes(kind);
|
|
683
|
+
const left = this.expression(node.left, frame);
|
|
684
|
+
const right = this.expression(
|
|
685
|
+
node.right,
|
|
686
|
+
frame,
|
|
687
|
+
left.kind === "code" ? left.declared : undefined,
|
|
688
|
+
);
|
|
689
|
+
const negate =
|
|
690
|
+
kind === ts.SyntaxKind.ExclamationEqualsEqualsToken ||
|
|
691
|
+
kind === ts.SyntaxKind.ExclamationEqualsToken;
|
|
692
|
+
if (equality) {
|
|
693
|
+
const folded = this.equal(left, right);
|
|
694
|
+
if (folded !== undefined) {
|
|
695
|
+
return constant(negate ? !folded : folded);
|
|
696
|
+
}
|
|
697
|
+
const [code, other] =
|
|
698
|
+
left.kind === "code" ? [left, right] : [right, left];
|
|
699
|
+
if (
|
|
700
|
+
code.kind === "code" &&
|
|
701
|
+
other.kind === "const" &&
|
|
702
|
+
other.value === undefined
|
|
703
|
+
) {
|
|
704
|
+
const test = this.dialect.isUndefinedText(code.text, code.declared);
|
|
705
|
+
return {
|
|
706
|
+
kind: "code",
|
|
707
|
+
text: negate ? mapped`!(${test})` : test,
|
|
708
|
+
declared: this.checker.getBooleanType(),
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
const declared = code.kind === "code" ? code.declared : undefined;
|
|
712
|
+
const [leftText, rightText] = this.dialect.comparable(
|
|
713
|
+
left,
|
|
714
|
+
right,
|
|
715
|
+
declared,
|
|
716
|
+
);
|
|
717
|
+
const text = mapped`${leftText} ${negate ? "!==" : "==="} ${rightText}`;
|
|
718
|
+
return {
|
|
719
|
+
kind: "code",
|
|
720
|
+
text: mapped`(${text})`,
|
|
721
|
+
declared: this.checker.getBooleanType(),
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
if (
|
|
725
|
+
left.kind === "const" &&
|
|
726
|
+
right.kind === "const" &&
|
|
727
|
+
typeof left.value === "number" &&
|
|
728
|
+
typeof right.value === "number"
|
|
729
|
+
) {
|
|
730
|
+
const folded = foldNumber(kind, left.value, right.value);
|
|
731
|
+
if (folded !== undefined) {
|
|
732
|
+
return constant(folded);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
const operator = binaryOperators.get(kind);
|
|
736
|
+
if (!operator) {
|
|
737
|
+
throw new SpecializeError(
|
|
738
|
+
node,
|
|
739
|
+
`${node.operatorToken.getText()} is not supported.`,
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
const declared = this.checker.getTypeAtLocation(node);
|
|
743
|
+
const [leftText, rightText] = ["<", "<=", ">", ">="].includes(operator)
|
|
744
|
+
? this.dialect.comparable(left, right, undefined)
|
|
745
|
+
: [
|
|
746
|
+
this.dialect.code(left, undefined),
|
|
747
|
+
this.dialect.code(right, undefined),
|
|
748
|
+
];
|
|
749
|
+
return {
|
|
750
|
+
kind: "code",
|
|
751
|
+
text: mapped`(${leftText} ${operator} ${rightText})`,
|
|
752
|
+
declared,
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/** Equality known now: two known objects, or a known object against undefined. */
|
|
757
|
+
equal(left: Value, right: Value): boolean | undefined {
|
|
758
|
+
if (left.kind === "const" && right.kind === "const") {
|
|
759
|
+
return left.value === right.value;
|
|
760
|
+
}
|
|
761
|
+
if (left.kind === "static" && right.kind === "static") {
|
|
762
|
+
return left.node === right.node;
|
|
763
|
+
}
|
|
764
|
+
if (
|
|
765
|
+
(left.kind === "static" && right.kind === "const") ||
|
|
766
|
+
(left.kind === "const" && right.kind === "static")
|
|
767
|
+
) {
|
|
768
|
+
return false;
|
|
769
|
+
}
|
|
770
|
+
return undefined;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/** Calls a function value: specializes it, or inlines a one-expression arrow. */
|
|
774
|
+
invoke(
|
|
775
|
+
target: Value,
|
|
776
|
+
args: Value[],
|
|
777
|
+
at: ts.Node,
|
|
778
|
+
frame: Frame,
|
|
779
|
+
expected?: ts.Type,
|
|
780
|
+
): Value {
|
|
781
|
+
if (target.kind !== "function") {
|
|
782
|
+
throw new SpecializeError(at, "Only functions can be called.");
|
|
783
|
+
}
|
|
784
|
+
const declaration = target.declaration;
|
|
785
|
+
if (
|
|
786
|
+
(ts.isArrowFunction(declaration) ||
|
|
787
|
+
ts.isFunctionExpression(declaration)) &&
|
|
788
|
+
target.env &&
|
|
789
|
+
ts.isArrowFunction(declaration) &&
|
|
790
|
+
!ts.isBlock(declaration.body)
|
|
791
|
+
) {
|
|
792
|
+
// A callback with one expression inlines with its arguments bound.
|
|
793
|
+
const inline: Frame = {
|
|
794
|
+
...frame,
|
|
795
|
+
env: new Map(target.env),
|
|
796
|
+
self: target.self ?? frame.self,
|
|
797
|
+
};
|
|
798
|
+
declaration.parameters.forEach((parameter, index) => {
|
|
799
|
+
const arg = args[index] ?? constant(undefined);
|
|
800
|
+
const value =
|
|
801
|
+
arg.kind === "code" && !arg.place
|
|
802
|
+
? this.temporaryOf(arg, frame)
|
|
803
|
+
: arg;
|
|
804
|
+
this.bind(
|
|
805
|
+
parameter.name,
|
|
806
|
+
value,
|
|
807
|
+
false,
|
|
808
|
+
inline,
|
|
809
|
+
this.checker.getTypeAtLocation(parameter),
|
|
810
|
+
);
|
|
811
|
+
});
|
|
812
|
+
return this.expression(declaration.body, inline, expected);
|
|
813
|
+
}
|
|
814
|
+
const called = this.routineTable.routine(
|
|
815
|
+
declaration,
|
|
816
|
+
target.self,
|
|
817
|
+
args,
|
|
818
|
+
target.env,
|
|
819
|
+
);
|
|
820
|
+
const text = mapped`${called.name}(${joined(called.args, ", ")})`;
|
|
821
|
+
const returns = called.returns;
|
|
822
|
+
if (!returns || returns.flags & ts.TypeFlags.Void) {
|
|
823
|
+
if (ts.isExpressionStatement(at.parent)) {
|
|
824
|
+
return { kind: "code", text, declared: returns };
|
|
825
|
+
}
|
|
826
|
+
frame.emitter.line(mapped`${text};`);
|
|
827
|
+
return constant(undefined);
|
|
828
|
+
}
|
|
829
|
+
return { kind: "code", text, declared: returns };
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
/** A runtime value held in a const, so inlining doesn't evaluate it twice. */
|
|
833
|
+
private temporaryOf(value: Value & { kind: "code" }, frame: Frame): Value {
|
|
834
|
+
if (repeatable(value.text)) {
|
|
835
|
+
return value;
|
|
836
|
+
}
|
|
837
|
+
const local = frame.emitter.temporary();
|
|
838
|
+
const type = value.declared ? this.localType(value.declared) : "number";
|
|
839
|
+
frame.emitter.line(mapped`const ${local}: ${type} = ${value.text};`);
|
|
840
|
+
return { kind: "code", text: local, declared: value.declared, place: true };
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/** Binds a local, or each name of a destructuring pattern, to a value. */
|
|
844
|
+
bind(
|
|
845
|
+
name: ts.BindingName,
|
|
846
|
+
value: Value,
|
|
847
|
+
mutable: boolean,
|
|
848
|
+
frame: Frame,
|
|
849
|
+
declared: ts.Type,
|
|
850
|
+
): void {
|
|
851
|
+
// A pattern reads its value once per name, so a computed value is held first.
|
|
852
|
+
const bound =
|
|
853
|
+
!ts.isIdentifier(name) && value.kind === "code" && !value.place
|
|
854
|
+
? this.temporaryOf(value, frame)
|
|
855
|
+
: value;
|
|
856
|
+
if (ts.isObjectBindingPattern(name)) {
|
|
857
|
+
for (const element of name.elements) {
|
|
858
|
+
const field = (element.propertyName ?? element.name).getText();
|
|
859
|
+
const inner = this.member(bound, field, element, frame);
|
|
860
|
+
this.bind(
|
|
861
|
+
element.name,
|
|
862
|
+
inner,
|
|
863
|
+
mutable,
|
|
864
|
+
frame,
|
|
865
|
+
this.checker.getTypeAtLocation(element.name),
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
if (ts.isArrayBindingPattern(name)) {
|
|
871
|
+
name.elements.forEach((element, index) => {
|
|
872
|
+
if (ts.isOmittedExpression(element)) {
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
const inner = this.element(bound, constant(index), element, frame);
|
|
876
|
+
this.bind(
|
|
877
|
+
element.name,
|
|
878
|
+
inner,
|
|
879
|
+
mutable,
|
|
880
|
+
frame,
|
|
881
|
+
this.checker.getTypeAtLocation(element.name),
|
|
882
|
+
);
|
|
883
|
+
});
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
const symbol = this.checker.getSymbolAtLocation(name)!;
|
|
887
|
+
if (!mutable && bound.kind !== "code") {
|
|
888
|
+
frame.env.set(symbol, bound);
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
if (!mutable && bound.kind === "code" && bound.place) {
|
|
892
|
+
// A const naming a place is an alias, as an object binding is in TypeScript.
|
|
893
|
+
if (this.isAggregate(declared)) {
|
|
894
|
+
frame.env.set(symbol, bound);
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
const local = frame.emitter.local(name.text);
|
|
899
|
+
const type =
|
|
900
|
+
bound.kind === "code" && bound.iec ? bound.iec : this.localType(declared);
|
|
901
|
+
frame.emitter.line(
|
|
902
|
+
mapped`${mutable ? "let" : "const"} ${local}: ${type} = ${this.dialect.code(bound, declared)};`,
|
|
903
|
+
);
|
|
904
|
+
frame.env.set(symbol, {
|
|
905
|
+
kind: "code",
|
|
906
|
+
text: local,
|
|
907
|
+
declared,
|
|
908
|
+
place: true,
|
|
909
|
+
iec: bound.kind === "code" ? bound.iec : undefined,
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/** A local's dialect type: a literal union keeps its enum; a lone literal widens to its base type. */
|
|
914
|
+
localType(declared: ts.Type): string {
|
|
915
|
+
if (this.layouts.literalEnum(declared)) {
|
|
916
|
+
return this.layouts.text(declared);
|
|
917
|
+
}
|
|
918
|
+
return this.layouts.text(this.checker.getBaseTypeOfLiteralType(declared));
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
private isAggregate(type: ts.Type): boolean {
|
|
922
|
+
return (
|
|
923
|
+
Boolean(type.flags & ts.TypeFlags.Object) &&
|
|
924
|
+
!this.layouts.literalEnum(type)
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* The elements of an array setup built or scan code assembled from known parts; undefined for
|
|
930
|
+
* a runtime array. Known arrays unroll, so each element's identity stays known.
|
|
931
|
+
*/
|
|
932
|
+
items(value: Value): Value[] | undefined {
|
|
933
|
+
if (value.kind === "virtual" && value.items) {
|
|
934
|
+
return value.items;
|
|
935
|
+
}
|
|
936
|
+
if (value.kind !== "static") {
|
|
937
|
+
return undefined;
|
|
938
|
+
}
|
|
939
|
+
const array = value.node;
|
|
940
|
+
const captured = array.captured;
|
|
941
|
+
if (captured.kind !== "array") {
|
|
942
|
+
return undefined;
|
|
943
|
+
}
|
|
944
|
+
return captured.items.map((item, index) =>
|
|
945
|
+
this.storage.fromCaptured(item, array, { kind: "index", index }),
|
|
946
|
+
);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/** Binary operators the dialect writes as TypeScript does. */
|
|
951
|
+
const binaryOperators = new Map<ts.SyntaxKind, string>([
|
|
952
|
+
[ts.SyntaxKind.PlusToken, "+"],
|
|
953
|
+
[ts.SyntaxKind.MinusToken, "-"],
|
|
954
|
+
[ts.SyntaxKind.AsteriskToken, "*"],
|
|
955
|
+
[ts.SyntaxKind.PercentToken, "%"],
|
|
956
|
+
[ts.SyntaxKind.LessThanToken, "<"],
|
|
957
|
+
[ts.SyntaxKind.LessThanEqualsToken, "<="],
|
|
958
|
+
[ts.SyntaxKind.GreaterThanToken, ">"],
|
|
959
|
+
[ts.SyntaxKind.GreaterThanEqualsToken, ">="],
|
|
960
|
+
[ts.SyntaxKind.AmpersandToken, "&"],
|
|
961
|
+
[ts.SyntaxKind.BarToken, "|"],
|
|
962
|
+
[ts.SyntaxKind.CaretToken, "^"],
|
|
963
|
+
[ts.SyntaxKind.LessThanLessThanToken, "<<"],
|
|
964
|
+
[ts.SyntaxKind.GreaterThanGreaterThanToken, ">>"],
|
|
965
|
+
]);
|
|
966
|
+
|
|
967
|
+
const foldNumber = (
|
|
968
|
+
kind: ts.SyntaxKind,
|
|
969
|
+
left: number,
|
|
970
|
+
right: number,
|
|
971
|
+
): number | boolean | undefined => {
|
|
972
|
+
switch (kind) {
|
|
973
|
+
case ts.SyntaxKind.PlusToken:
|
|
974
|
+
return left + right;
|
|
975
|
+
case ts.SyntaxKind.MinusToken:
|
|
976
|
+
return left - right;
|
|
977
|
+
case ts.SyntaxKind.AsteriskToken:
|
|
978
|
+
return left * right;
|
|
979
|
+
case ts.SyntaxKind.PercentToken:
|
|
980
|
+
return right === 0 ? undefined : left % right;
|
|
981
|
+
case ts.SyntaxKind.LessThanToken:
|
|
982
|
+
return left < right;
|
|
983
|
+
case ts.SyntaxKind.LessThanEqualsToken:
|
|
984
|
+
return left <= right;
|
|
985
|
+
case ts.SyntaxKind.GreaterThanToken:
|
|
986
|
+
return left > right;
|
|
987
|
+
case ts.SyntaxKind.GreaterThanEqualsToken:
|
|
988
|
+
return left >= right;
|
|
989
|
+
default:
|
|
990
|
+
return undefined;
|
|
991
|
+
}
|
|
992
|
+
};
|