@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
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 +6563 -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 +16198 -0
- package/dist/index.js.map +62 -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 +1090 -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-enums.ts +196 -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 +1344 -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 +2177 -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 +612 -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 +659 -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,124 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { valueSymbol } from "../sdk";
|
|
3
|
+
import { SpecializeError, type Value } from "./values";
|
|
4
|
+
|
|
5
|
+
/** Whether `node` names something the JavaScript runtime provides: its library declares it, and the program doesn't. */
|
|
6
|
+
export const providedByRuntime = (
|
|
7
|
+
checker: ts.TypeChecker,
|
|
8
|
+
node: ts.Node,
|
|
9
|
+
): boolean => {
|
|
10
|
+
const declaration = checker.getSymbolAtLocation(node)?.valueDeclaration;
|
|
11
|
+
return (
|
|
12
|
+
declaration !== undefined &&
|
|
13
|
+
(declaration.getSourceFile().isDeclarationFile ||
|
|
14
|
+
(ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Ambient) !==
|
|
15
|
+
0)
|
|
16
|
+
);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Whether `node` is the runtime's global `name`, such as `Math` or `structuredClone`, rather than one the program declares. */
|
|
20
|
+
export const isGlobal = (
|
|
21
|
+
checker: ts.TypeChecker,
|
|
22
|
+
node: ts.Expression,
|
|
23
|
+
name: string,
|
|
24
|
+
): boolean =>
|
|
25
|
+
ts.isIdentifier(node) &&
|
|
26
|
+
node.text === name &&
|
|
27
|
+
providedByRuntime(checker, node);
|
|
28
|
+
|
|
29
|
+
/** The symbol a declared name binds. */
|
|
30
|
+
export const symbolOf = (
|
|
31
|
+
checker: ts.TypeChecker,
|
|
32
|
+
name: ts.Identifier,
|
|
33
|
+
): ts.Symbol => {
|
|
34
|
+
const symbol = valueSymbol(checker, name);
|
|
35
|
+
if (!symbol) {
|
|
36
|
+
throw new SpecializeError(name, `${name.text} is not declared.`);
|
|
37
|
+
}
|
|
38
|
+
return symbol;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** A value only the run decides is undefined or not. */
|
|
42
|
+
export type RunTimeOptional =
|
|
43
|
+
| Extract<Value, { kind: "choice" }>
|
|
44
|
+
| (Extract<Value, { kind: "code" }> & { declared: ts.Type });
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* `value` when only the run decides whether it is undefined: optional code, or a reference chosen
|
|
48
|
+
* at run time that may be none where TypeScript has not narrowed it to defined.
|
|
49
|
+
*/
|
|
50
|
+
export const runTimeOptional = (
|
|
51
|
+
checker: ts.TypeChecker,
|
|
52
|
+
value: Value,
|
|
53
|
+
): RunTimeOptional | undefined => {
|
|
54
|
+
if (value.kind === "choice") {
|
|
55
|
+
return value.optional && !value.narrowed ? value : undefined;
|
|
56
|
+
}
|
|
57
|
+
if (
|
|
58
|
+
value.kind !== "code" ||
|
|
59
|
+
!value.declared ||
|
|
60
|
+
!(value.declared.flags & ts.TypeFlags.Union) ||
|
|
61
|
+
checker.getNonNullableType(value.declared) === value.declared
|
|
62
|
+
) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
return { ...value, declared: value.declared };
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The default a parameter takes for `arg`: its initializer when the argument is missing or passed
|
|
70
|
+
* as undefined. A run-time argument that may be undefined is refused, since only the run would
|
|
71
|
+
* decide whether the default applies.
|
|
72
|
+
*/
|
|
73
|
+
export const parameterDefault = (
|
|
74
|
+
checker: ts.TypeChecker,
|
|
75
|
+
arg: Value,
|
|
76
|
+
parameter: ts.ParameterDeclaration,
|
|
77
|
+
): ts.Expression | undefined => {
|
|
78
|
+
if (!parameter.initializer) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
if (arg.kind === "const") {
|
|
82
|
+
return arg.value === undefined ? parameter.initializer : undefined;
|
|
83
|
+
}
|
|
84
|
+
if (runTimeOptional(checker, arg)) {
|
|
85
|
+
throw new SpecializeError(
|
|
86
|
+
parameter,
|
|
87
|
+
"Pass a defined value or leave the argument out: the default applies to a missing argument.",
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** The elements an array pattern names, each with the index it reads; a rest element is refused. */
|
|
94
|
+
export const namedElements = (
|
|
95
|
+
pattern: ts.ArrayBindingPattern,
|
|
96
|
+
declared: "parameter" | "pattern",
|
|
97
|
+
): { element: ts.BindingElement; index: number }[] =>
|
|
98
|
+
pattern.elements.flatMap((element, index) => {
|
|
99
|
+
if (ts.isOmittedExpression(element)) {
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
if (element.dotDotDotToken) {
|
|
103
|
+
throw new SpecializeError(
|
|
104
|
+
element,
|
|
105
|
+
`Name each element of an array ${declared}; a rest element has no length here.`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return [{ element, index }];
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
/** The elements an object pattern names, each with the field it reads; a rest element is refused. */
|
|
112
|
+
export const namedFields = (
|
|
113
|
+
pattern: ts.ObjectBindingPattern,
|
|
114
|
+
declared: "parameter" | "pattern",
|
|
115
|
+
): { element: ts.BindingElement; field: string }[] =>
|
|
116
|
+
pattern.elements.map((element) => {
|
|
117
|
+
if (element.dotDotDotToken) {
|
|
118
|
+
throw new SpecializeError(
|
|
119
|
+
element,
|
|
120
|
+
`Name each field of an object ${declared}; a rest element has no record here.`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return { element, field: (element.propertyName ?? element.name).getText() };
|
|
124
|
+
});
|
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
import { canAssign, integerType, primitiveTypes } from "@tsplc/compiler/ir";
|
|
2
|
+
import { elementaryType, fitsNumber, isIntegerValued } from "@tsplc/iec";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { joined, type Mapped, mapped } from "../positions";
|
|
5
|
+
import { sdkModules } from "../sdk";
|
|
6
|
+
import { defaultNumericTypes } from "../types";
|
|
7
|
+
import type { EntryImports } from "./imports";
|
|
8
|
+
import type { Initializers } from "./initializers";
|
|
9
|
+
import {
|
|
10
|
+
definedOptional,
|
|
11
|
+
emptyOptional,
|
|
12
|
+
type Layouts,
|
|
13
|
+
optionalHas,
|
|
14
|
+
optionalValue,
|
|
15
|
+
} from "./layouts";
|
|
16
|
+
import type { Model } from "./model";
|
|
17
|
+
import { holds, present, referenceIndex, referenceType } from "./references";
|
|
18
|
+
import type { Storage } from "./storage";
|
|
19
|
+
import { calendarConstructor } from "./time-constructors";
|
|
20
|
+
import type { Value } from "./values";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Specialized values as dialect text, in a place of a declared type: literals as enum members,
|
|
24
|
+
* optionals as `{ has, value }`, records with every field, and numbers converted as IEC does.
|
|
25
|
+
*/
|
|
26
|
+
export class DialectText {
|
|
27
|
+
constructor(
|
|
28
|
+
private readonly model: Model,
|
|
29
|
+
private readonly checker: ts.TypeChecker,
|
|
30
|
+
private readonly layouts: Layouts,
|
|
31
|
+
private readonly storage: Storage,
|
|
32
|
+
private readonly initializers: Initializers,
|
|
33
|
+
private readonly imports: EntryImports,
|
|
34
|
+
) {}
|
|
35
|
+
|
|
36
|
+
/** Dialect text for a value, in a place of `expected` type when known. */
|
|
37
|
+
code(value: Value, expected: ts.Type | undefined): string | Mapped {
|
|
38
|
+
// Where a reference is held, a value is the id of the object it is.
|
|
39
|
+
const index =
|
|
40
|
+
expected && referenceType(expected) ? referenceIndex(value) : undefined;
|
|
41
|
+
if (index !== undefined) {
|
|
42
|
+
return index;
|
|
43
|
+
}
|
|
44
|
+
switch (value.kind) {
|
|
45
|
+
case "code":
|
|
46
|
+
if (
|
|
47
|
+
expected &&
|
|
48
|
+
value.declared &&
|
|
49
|
+
this.layouts.isOptionalRecord(value.declared) &&
|
|
50
|
+
!this.layouts.isOptionalRecord(expected)
|
|
51
|
+
) {
|
|
52
|
+
return optionalValue(value.text);
|
|
53
|
+
}
|
|
54
|
+
return value.text;
|
|
55
|
+
case "const": {
|
|
56
|
+
const literal = expected && this.layouts.literalEnum(expected);
|
|
57
|
+
if (
|
|
58
|
+
literal &&
|
|
59
|
+
(typeof value.value === "string" || value.value === undefined)
|
|
60
|
+
) {
|
|
61
|
+
return this.layouts.member(literal, value.value);
|
|
62
|
+
}
|
|
63
|
+
if (
|
|
64
|
+
expected &&
|
|
65
|
+
this.layouts.isOptionalRecord(expected) &&
|
|
66
|
+
value.value === undefined
|
|
67
|
+
) {
|
|
68
|
+
return this.emptyOptional(expected);
|
|
69
|
+
}
|
|
70
|
+
if (expected && typeof value.value === "number") {
|
|
71
|
+
const member = this.layouts.enumMember(expected, value.value);
|
|
72
|
+
if (member) {
|
|
73
|
+
return member;
|
|
74
|
+
}
|
|
75
|
+
// Setup's TIME is a number of milliseconds; the dialect spells a TIME constant as a duration.
|
|
76
|
+
if (expected.aliasSymbol?.name === "TIME") {
|
|
77
|
+
this.imports.addSdk(sdkModules.types, "ms");
|
|
78
|
+
return `ms(${value.value})`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (value.value === undefined) {
|
|
82
|
+
throw new Error("undefined has no PLC value here.");
|
|
83
|
+
}
|
|
84
|
+
if (typeof value.value === "string") {
|
|
85
|
+
return this.stringText(value.value, expected);
|
|
86
|
+
}
|
|
87
|
+
if (typeof value.value === "bigint") {
|
|
88
|
+
return `${value.value}n`;
|
|
89
|
+
}
|
|
90
|
+
return String(value.value);
|
|
91
|
+
}
|
|
92
|
+
case "static": {
|
|
93
|
+
const node = value.node;
|
|
94
|
+
if (this.storage.storedNode(node)) {
|
|
95
|
+
return this.storage.pathText(node);
|
|
96
|
+
}
|
|
97
|
+
if (node.type) {
|
|
98
|
+
return this.initializers.valueText(
|
|
99
|
+
{ kind: "node", id: node.id },
|
|
100
|
+
expected ?? node.type,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
throw new Error("This object has no PLC value.");
|
|
104
|
+
}
|
|
105
|
+
case "virtual": {
|
|
106
|
+
if (value.fields) {
|
|
107
|
+
const type = expected && this.checker.getNonNullableType(expected);
|
|
108
|
+
if (!type) {
|
|
109
|
+
throw new Error("A record literal needs a declared type.");
|
|
110
|
+
}
|
|
111
|
+
return this.recordLiteral(value.fields, type);
|
|
112
|
+
}
|
|
113
|
+
if (value.items) {
|
|
114
|
+
const element = expected && this.model.elementType(expected);
|
|
115
|
+
return mapped`[${joined(
|
|
116
|
+
value.items.map((item) => this.code(item, element)),
|
|
117
|
+
", ",
|
|
118
|
+
)}]`;
|
|
119
|
+
}
|
|
120
|
+
throw new Error("Empty value.");
|
|
121
|
+
}
|
|
122
|
+
case "function":
|
|
123
|
+
throw new Error("A function has no PLC value; call it.");
|
|
124
|
+
case "counted":
|
|
125
|
+
throw new Error(
|
|
126
|
+
"An array whose length is decided at run time has no PLC value: read it with for…of, its length, an index or an array method.",
|
|
127
|
+
);
|
|
128
|
+
case "choice":
|
|
129
|
+
return expected ? this.chosenCopy(value, expected) : value.index;
|
|
130
|
+
case "masked":
|
|
131
|
+
throw new Error(
|
|
132
|
+
"An array filtered or sorted at run time has no PLC value: read its length, or pick from it.",
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** A record chosen at run time, where the PLC holds a record's value: a copy of whichever it is. */
|
|
138
|
+
private chosenCopy(
|
|
139
|
+
choice: Value & { kind: "choice" },
|
|
140
|
+
expected: ts.Type,
|
|
141
|
+
): string | Mapped {
|
|
142
|
+
const copies = choice.options.map((option) => ({
|
|
143
|
+
test: holds(choice, option),
|
|
144
|
+
text: this.code({ kind: "static", node: option }, expected),
|
|
145
|
+
}));
|
|
146
|
+
const last = copies.pop();
|
|
147
|
+
if (!last) {
|
|
148
|
+
throw new Error("A reference to no object has no PLC value.");
|
|
149
|
+
}
|
|
150
|
+
return copies.reduceRight<string | Mapped>(
|
|
151
|
+
(otherwise, copy) => mapped`(${copy.test} ? ${copy.text} : ${otherwise})`,
|
|
152
|
+
last.text,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** A record literal of `type`, every field written; a tagged union fills the other variants' fields. */
|
|
157
|
+
private recordLiteral(fields: Map<string, Value>, type: ts.Type): Mapped {
|
|
158
|
+
const tagged = this.layouts.tagged(type);
|
|
159
|
+
const fieldTypes: [string, ts.Type][] = tagged
|
|
160
|
+
? this.layouts
|
|
161
|
+
.taggedFields(tagged)
|
|
162
|
+
.map((name) => [
|
|
163
|
+
name,
|
|
164
|
+
name === tagged.tag
|
|
165
|
+
? this.layouts.tagType(type, tagged)
|
|
166
|
+
: this.layouts.taggedFieldType(type, tagged, name),
|
|
167
|
+
])
|
|
168
|
+
: this.checker
|
|
169
|
+
.getPropertiesOfType(type)
|
|
170
|
+
.map((property) => [
|
|
171
|
+
property.name,
|
|
172
|
+
this.checker.getTypeOfSymbol(property),
|
|
173
|
+
]);
|
|
174
|
+
return mapped`{ ${joined(
|
|
175
|
+
fieldTypes.map(([name, fieldType]) => {
|
|
176
|
+
const value = fields.get(name);
|
|
177
|
+
if (!value) {
|
|
178
|
+
return `${name}: ${this.initializers.defaultOf(fieldType)}`;
|
|
179
|
+
}
|
|
180
|
+
const property = tagged
|
|
181
|
+
? this.layouts.taggedField(type, tagged, name)
|
|
182
|
+
: this.checker.getPropertyOfType(type, name);
|
|
183
|
+
// A record field holds exactly its type, so a narrower integer widens explicitly, as a result does.
|
|
184
|
+
return mapped`${name}: ${this.returnText(value, fieldType, this.layouts.iecName(property?.valueDeclaration))}`;
|
|
185
|
+
}),
|
|
186
|
+
", ",
|
|
187
|
+
)} }`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A routine's result: stored as an assignment is, and a narrower integer widened explicitly, as
|
|
192
|
+
* Structured Text's typed functions return exactly their declared type.
|
|
193
|
+
*/
|
|
194
|
+
returnText(
|
|
195
|
+
value: Value,
|
|
196
|
+
declared: ts.Type | undefined,
|
|
197
|
+
iec: string | undefined,
|
|
198
|
+
): string | Mapped {
|
|
199
|
+
const target = storedWidth(declared, iec);
|
|
200
|
+
const from = value.kind === "code" ? value.iec : undefined;
|
|
201
|
+
if (
|
|
202
|
+
target &&
|
|
203
|
+
from &&
|
|
204
|
+
from !== target &&
|
|
205
|
+
integerTypes.has(from) &&
|
|
206
|
+
integerTypes.has(target) &&
|
|
207
|
+
!needsConversion(from, target)
|
|
208
|
+
) {
|
|
209
|
+
this.imports.addSdk(sdkModules.types, "exact");
|
|
210
|
+
return mapped`exact<${target}>(${this.code(value, undefined)})`;
|
|
211
|
+
}
|
|
212
|
+
return this.assignmentText(value, declared, iec);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Whether storing `value` into a place of `declared` type converts it, as IEC assignment narrows. */
|
|
216
|
+
converts(
|
|
217
|
+
value: Value,
|
|
218
|
+
declared: ts.Type | undefined,
|
|
219
|
+
iec: string | undefined,
|
|
220
|
+
): boolean {
|
|
221
|
+
const target = storedWidth(declared, iec);
|
|
222
|
+
return (
|
|
223
|
+
target !== undefined &&
|
|
224
|
+
value.kind === "code" &&
|
|
225
|
+
needsConversion(value.iec ?? defaultNumericTypes.number, target)
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** The text stored into a place of `declared` type, wrapping an optional's value. */
|
|
230
|
+
assignmentText(
|
|
231
|
+
value: Value,
|
|
232
|
+
declared: ts.Type | undefined,
|
|
233
|
+
iec: string | undefined,
|
|
234
|
+
): string | Mapped {
|
|
235
|
+
const index =
|
|
236
|
+
declared && referenceType(declared) ? referenceIndex(value) : undefined;
|
|
237
|
+
if (index !== undefined) {
|
|
238
|
+
return index;
|
|
239
|
+
}
|
|
240
|
+
const target = storedWidth(declared, iec);
|
|
241
|
+
// A whole float fits an integer as TypeScript's number holds it, unless it is out of range.
|
|
242
|
+
const family = target && elementaryType(target)?.family;
|
|
243
|
+
if (
|
|
244
|
+
target &&
|
|
245
|
+
(family === "integer" || family === "bits") &&
|
|
246
|
+
value.kind === "code" &&
|
|
247
|
+
value.whole
|
|
248
|
+
) {
|
|
249
|
+
this.imports.addSdk(sdkModules.types, "saturate");
|
|
250
|
+
return mapped`saturate<${target}>(${this.code(value, undefined)})`;
|
|
251
|
+
}
|
|
252
|
+
if (target && this.converts(value, declared, iec)) {
|
|
253
|
+
// A TypeScript number holds any of these; in the PLC a value converts as an IEC assignment does.
|
|
254
|
+
this.imports.addSdk(sdkModules.types, "wrap");
|
|
255
|
+
return mapped`wrap<${target}>(${this.code(value, undefined)})`;
|
|
256
|
+
}
|
|
257
|
+
if (declared && this.layouts.isOptionalRecord(declared)) {
|
|
258
|
+
if (value.kind === "const" && value.value === undefined) {
|
|
259
|
+
return this.emptyOptional(declared);
|
|
260
|
+
}
|
|
261
|
+
if (
|
|
262
|
+
value.kind === "code" &&
|
|
263
|
+
value.declared &&
|
|
264
|
+
this.layouts.isOptionalRecord(value.declared)
|
|
265
|
+
) {
|
|
266
|
+
return value.text;
|
|
267
|
+
}
|
|
268
|
+
const defined = this.wrapDefined(
|
|
269
|
+
this.code(value, this.checker.getNonNullableType(declared)),
|
|
270
|
+
declared,
|
|
271
|
+
);
|
|
272
|
+
// A record chosen at run time may be none, which the optional says.
|
|
273
|
+
return value.kind === "choice" && value.optional
|
|
274
|
+
? mapped`(${present(value.index)} ? ${defined} : ${this.emptyOptional(declared)})`
|
|
275
|
+
: defined;
|
|
276
|
+
}
|
|
277
|
+
return this.code(value, declared);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* An optional of `declared` type holding nothing. A record literal takes the place's own optional
|
|
282
|
+
* record, whose value may have the IEC width the place's declaration names.
|
|
283
|
+
*/
|
|
284
|
+
private emptyOptional(declared: ts.Type): Mapped {
|
|
285
|
+
return emptyOptional(
|
|
286
|
+
this.initializers.defaultOf(this.checker.getNonNullableType(declared)),
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
wrapDefined(
|
|
291
|
+
text: string | Mapped,
|
|
292
|
+
declared: ts.Type | undefined,
|
|
293
|
+
): string | Mapped {
|
|
294
|
+
return declared && this.layouts.isOptionalRecord(declared)
|
|
295
|
+
? definedOptional(text)
|
|
296
|
+
: text;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
isUndefinedText(
|
|
300
|
+
text: string | Mapped,
|
|
301
|
+
declared: ts.Type | undefined,
|
|
302
|
+
): Mapped {
|
|
303
|
+
if (declared && this.layouts.isOptionalRecord(declared)) {
|
|
304
|
+
return mapped`!${optionalHas(text)}`;
|
|
305
|
+
}
|
|
306
|
+
const literal = declared && this.layouts.literalEnum(declared);
|
|
307
|
+
if (literal) {
|
|
308
|
+
return mapped`${text} === ${this.layouts.member(literal, undefined)}`;
|
|
309
|
+
}
|
|
310
|
+
throw new Error("Only optional values can be undefined.");
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** A boolean condition, including JavaScript truthiness of optionals and enums. */
|
|
314
|
+
condition(value: Value, at: ts.Node): string | Mapped {
|
|
315
|
+
if (value.kind === "choice") {
|
|
316
|
+
return value.optional ? mapped`(${present(value.index)})` : "true";
|
|
317
|
+
}
|
|
318
|
+
if (value.kind === "code" && value.declared) {
|
|
319
|
+
const declared = value.declared;
|
|
320
|
+
if (
|
|
321
|
+
this.layouts.isOptionalRecord(declared) &&
|
|
322
|
+
!ts.isPrefixUnaryExpression(at)
|
|
323
|
+
) {
|
|
324
|
+
const type = this.checker.getTypeAtLocation(at);
|
|
325
|
+
if (!(type.flags & ts.TypeFlags.BooleanLike)) {
|
|
326
|
+
return optionalHas(value.text);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
const literal = this.layouts.literalEnum(declared);
|
|
330
|
+
if (literal?.optional) {
|
|
331
|
+
return mapped`${value.text} !== ${this.layouts.member(literal, undefined)}`;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return this.code(value, undefined);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** A string setup gave, as a calendar constant where a calendar type is expected. */
|
|
338
|
+
private stringText(text: string, expected: ts.Type | undefined): string {
|
|
339
|
+
const calendar = calendarConstructor(expected?.aliasSymbol?.name ?? "");
|
|
340
|
+
if (!calendar) {
|
|
341
|
+
return JSON.stringify(text);
|
|
342
|
+
}
|
|
343
|
+
this.imports.addSdk(sdkModules.types, calendar);
|
|
344
|
+
return `${calendar}(${JSON.stringify(text)})`;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Both sides of a comparison as text. Integers of different signedness, which TypeScript
|
|
349
|
+
* compares as numbers, both widen to LINT so the PLC compares them exactly.
|
|
350
|
+
*/
|
|
351
|
+
comparable(
|
|
352
|
+
left: Value,
|
|
353
|
+
right: Value,
|
|
354
|
+
declared: ts.Type | undefined,
|
|
355
|
+
): [string | Mapped, string | Mapped] {
|
|
356
|
+
// A constant compared with code takes the code's type, as a calendar constant needs.
|
|
357
|
+
const leftText = this.code(
|
|
358
|
+
left,
|
|
359
|
+
right.kind === "code" ? (right.declared ?? declared) : declared,
|
|
360
|
+
);
|
|
361
|
+
const rightText = this.code(
|
|
362
|
+
right,
|
|
363
|
+
left.kind === "code" ? (left.declared ?? declared) : declared,
|
|
364
|
+
);
|
|
365
|
+
if (left.kind !== "code" || right.kind !== "code") {
|
|
366
|
+
return [
|
|
367
|
+
bigintBeside(left, right, leftText),
|
|
368
|
+
bigintBeside(right, left, rightText),
|
|
369
|
+
];
|
|
370
|
+
}
|
|
371
|
+
const from = left.iec ?? defaultNumericTypes.number;
|
|
372
|
+
const to = right.iec ?? defaultNumericTypes.number;
|
|
373
|
+
if (!needsConversion(from, to) || !needsConversion(to, from)) {
|
|
374
|
+
return [leftText, rightText];
|
|
375
|
+
}
|
|
376
|
+
this.imports.addSdk(sdkModules.types, "wrap");
|
|
377
|
+
return [mapped`wrap<LINT>(${leftText})`, mapped`wrap<LINT>(${rightText})`];
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Operands of integer arithmetic at one width, as TypeScript numbers are: the narrower widens to
|
|
382
|
+
* the other, or both to LINT when neither holds the other. `iec` is the width of the result.
|
|
383
|
+
*/
|
|
384
|
+
arithmetic(
|
|
385
|
+
left: Value,
|
|
386
|
+
right: Value,
|
|
387
|
+
): { texts: [string | Mapped, string | Mapped]; iec: string | undefined } {
|
|
388
|
+
const leftText = this.code(left, undefined);
|
|
389
|
+
const rightText = this.code(right, undefined);
|
|
390
|
+
// A literal takes the other operand's width, so the result keeps it.
|
|
391
|
+
if (left.kind !== "code" || right.kind !== "code") {
|
|
392
|
+
const operand = left.kind === "code" ? left : right;
|
|
393
|
+
return {
|
|
394
|
+
texts: [
|
|
395
|
+
bigintBeside(left, right, leftText),
|
|
396
|
+
bigintBeside(right, left, rightText),
|
|
397
|
+
],
|
|
398
|
+
iec: operand.kind === "code" ? operand.iec : undefined,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
const from = left.iec ?? defaultNumericTypes.number;
|
|
402
|
+
const to = right.iec ?? defaultNumericTypes.number;
|
|
403
|
+
if (from === to || !integerTypes.has(from) || !integerTypes.has(to)) {
|
|
404
|
+
return { texts: [leftText, rightText], iec: left.iec };
|
|
405
|
+
}
|
|
406
|
+
const common = commonWidth(from, to);
|
|
407
|
+
this.imports.addSdk(sdkModules.types, "exact");
|
|
408
|
+
const widened = (text: string | Mapped, width: string) =>
|
|
409
|
+
width === common ? text : mapped`exact<${common}>(${text})`;
|
|
410
|
+
return {
|
|
411
|
+
texts: [widened(leftText, from), widened(rightText, to)],
|
|
412
|
+
iec: common === defaultNumericTypes.number ? undefined : common,
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Whether TypeScript holds an integer of this IEC width as a bigint: the 64-bit ones. */
|
|
418
|
+
const bigintWidth = (iec: string | undefined): boolean => {
|
|
419
|
+
const type = iec === undefined ? undefined : elementaryType(iec);
|
|
420
|
+
return isIntegerValued(type) && !fitsNumber(type);
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* A whole number constant beside a 64-bit integer, which TypeScript holds as a bigint, written as one: a widened
|
|
425
|
+
* difference compared with or offset by a literal would otherwise mix a bigint and a number.
|
|
426
|
+
*/
|
|
427
|
+
const bigintBeside = (
|
|
428
|
+
value: Value,
|
|
429
|
+
other: Value,
|
|
430
|
+
text: string | Mapped,
|
|
431
|
+
): string | Mapped =>
|
|
432
|
+
value.kind === "const" &&
|
|
433
|
+
typeof value.value === "number" &&
|
|
434
|
+
Number.isInteger(value.value) &&
|
|
435
|
+
other.kind === "code" &&
|
|
436
|
+
bigintWidth(other.iec)
|
|
437
|
+
? `${value.value}n`
|
|
438
|
+
: text;
|
|
439
|
+
|
|
440
|
+
/** IEC integer and bit-string types by name. */
|
|
441
|
+
const integerTypes = new Map(
|
|
442
|
+
primitiveTypes.filter(integerType).map((type) => [type.id, type]),
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
/** The IEC width a value is stored at: the one its place declares, or a TypeScript number's. */
|
|
446
|
+
const storedWidth = (
|
|
447
|
+
declared: ts.Type | undefined,
|
|
448
|
+
iec: string | undefined,
|
|
449
|
+
): string | undefined =>
|
|
450
|
+
iec ??
|
|
451
|
+
(declared && declared.flags & ts.TypeFlags.Number
|
|
452
|
+
? defaultNumericTypes.number
|
|
453
|
+
: undefined);
|
|
454
|
+
|
|
455
|
+
/** The width two integer widths widen to: the wider, or LINT when neither holds the other. */
|
|
456
|
+
const commonWidth = (from: string, to: string): string => {
|
|
457
|
+
if (!needsConversion(from, to)) {
|
|
458
|
+
return to;
|
|
459
|
+
}
|
|
460
|
+
if (!needsConversion(to, from)) {
|
|
461
|
+
return from;
|
|
462
|
+
}
|
|
463
|
+
return "LINT";
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
/** Whether IEC needs an explicit conversion from one integer type to another; widening is implicit. */
|
|
467
|
+
const needsConversion = (from: string, to: string): boolean => {
|
|
468
|
+
const source = integerTypes.get(from);
|
|
469
|
+
const target = integerTypes.get(to);
|
|
470
|
+
return (
|
|
471
|
+
source !== undefined && target !== undefined && !canAssign(source, target)
|
|
472
|
+
);
|
|
473
|
+
};
|