@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,310 @@
|
|
|
1
|
+
import type { Constant, GeneratedObject, PlcType } from "@tsplc/compiler/ir";
|
|
2
|
+
import { instancedBlock } from "@tsplc/compiler/ir";
|
|
3
|
+
import {
|
|
4
|
+
arrayTypeText,
|
|
5
|
+
nanosecondsPerMillisecond,
|
|
6
|
+
stringTypeText,
|
|
7
|
+
} from "@tsplc/iec";
|
|
8
|
+
import { escapeString } from "@tsplc/iec/st";
|
|
9
|
+
import { Names } from "./names";
|
|
10
|
+
import { scalarLiteral } from "./numeric";
|
|
11
|
+
import { required } from "./required";
|
|
12
|
+
|
|
13
|
+
/** A type's name as its source declares it: the last segment of its id. */
|
|
14
|
+
export const sourceTypeName = (id: string) => id.slice(id.lastIndexOf(":") + 1);
|
|
15
|
+
|
|
16
|
+
/** The elementary type that holds a string type's text. */
|
|
17
|
+
export const stringTypeName = (type: Extract<PlcType, { kind: "string" }>) =>
|
|
18
|
+
type.encoding === "utf8" ? "STRING" : "WSTRING";
|
|
19
|
+
|
|
20
|
+
const stEscapes: ReadonlyMap<string, string> = new Map([
|
|
21
|
+
["$", "$$"],
|
|
22
|
+
["'", "$'"],
|
|
23
|
+
["\r", "$R"],
|
|
24
|
+
["\n", "$N"],
|
|
25
|
+
]);
|
|
26
|
+
/**
|
|
27
|
+
* An ST STRING literal of text the compiler generates, such as an HTTP request line. Program
|
|
28
|
+
* constants go through `CodesysTypes.literal`, which writes every control character as its code.
|
|
29
|
+
*/
|
|
30
|
+
export const stStringLiteral = (text: string) =>
|
|
31
|
+
`'${[...text].map((character) => stEscapes.get(character) ?? character).join("")}'`;
|
|
32
|
+
|
|
33
|
+
/** The text of a string or array type: `STRING(n)`, or `ARRAY[l..u] OF T` of its element's name. */
|
|
34
|
+
const typeText = (
|
|
35
|
+
type: Extract<PlcType, { kind: "string" | "array" }>,
|
|
36
|
+
element: (id: string) => string,
|
|
37
|
+
) =>
|
|
38
|
+
type.kind === "string"
|
|
39
|
+
? stringTypeText({
|
|
40
|
+
wide: type.encoding !== "utf8",
|
|
41
|
+
capacity: type.capacity,
|
|
42
|
+
})
|
|
43
|
+
: arrayTypeText({
|
|
44
|
+
lower: type.lowerBound,
|
|
45
|
+
upper: type.lowerBound + type.length - 1,
|
|
46
|
+
element: element(type.element),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export class CodesysTypes {
|
|
50
|
+
readonly objects: GeneratedObject[] = [];
|
|
51
|
+
private readonly names = new Map<string, string>();
|
|
52
|
+
private readonly fields = new Map<string, Map<string, string>>();
|
|
53
|
+
|
|
54
|
+
constructor(
|
|
55
|
+
private readonly types: ReadonlyMap<string, PlcType>,
|
|
56
|
+
names: Names,
|
|
57
|
+
blockNames: ReadonlyMap<string, string>,
|
|
58
|
+
) {
|
|
59
|
+
// Register every type and member name before following layout dependencies.
|
|
60
|
+
for (const type of types.values()) {
|
|
61
|
+
switch (type.kind) {
|
|
62
|
+
case "block":
|
|
63
|
+
this.names.set(type.id, required(blockNames.get(type.block)));
|
|
64
|
+
break;
|
|
65
|
+
case "record":
|
|
66
|
+
case "enum": {
|
|
67
|
+
const prefix = type.kind === "record" ? "ST" : "E";
|
|
68
|
+
this.names.set(
|
|
69
|
+
type.id,
|
|
70
|
+
names.allocateObject(prefix, sourceTypeName(type.id)),
|
|
71
|
+
);
|
|
72
|
+
const members = new Names();
|
|
73
|
+
this.fields.set(
|
|
74
|
+
type.id,
|
|
75
|
+
new Map(
|
|
76
|
+
(type.kind === "record" ? type.fields : type.members).map(
|
|
77
|
+
(member) => [member.name, members.allocate(member.name)],
|
|
78
|
+
),
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case "array":
|
|
84
|
+
case "string":
|
|
85
|
+
if (!this.inline(type)) {
|
|
86
|
+
this.names.set(
|
|
87
|
+
type.id,
|
|
88
|
+
names.allocate(`T_${sourceTypeName(type.id)}`),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Verified layouts are acyclic.
|
|
96
|
+
const emitted = new Set<string>();
|
|
97
|
+
const emit = (type: PlcType): void => {
|
|
98
|
+
if (type.kind === "array" && this.inline(type)) {
|
|
99
|
+
emit(this.type(type.element));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (
|
|
103
|
+
instancedBlock(type.id, types) ||
|
|
104
|
+
!this.names.has(type.id) ||
|
|
105
|
+
emitted.has(type.id)
|
|
106
|
+
) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (type.kind === "array") {
|
|
110
|
+
emit(this.type(type.element));
|
|
111
|
+
}
|
|
112
|
+
if (type.kind === "record") {
|
|
113
|
+
for (const field of type.fields) {
|
|
114
|
+
emit(this.type(field.type));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
emitted.add(type.id);
|
|
118
|
+
const name = this.name(type.id);
|
|
119
|
+
const declaration = this.dutDeclaration(type, name);
|
|
120
|
+
this.objects.push({ id: type.id, kind: "dataType", name, declaration });
|
|
121
|
+
};
|
|
122
|
+
for (const type of types.values()) {
|
|
123
|
+
emit(type);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The declaration of a data type's DUT. */
|
|
128
|
+
private dutDeclaration(type: PlcType, name: string): string {
|
|
129
|
+
if (type.kind === "record") {
|
|
130
|
+
const fields = type.fields
|
|
131
|
+
.map(
|
|
132
|
+
(field) =>
|
|
133
|
+
` ${this.field(type.id, field.name)} : ${this.name(field.type)};`,
|
|
134
|
+
)
|
|
135
|
+
.join("\n");
|
|
136
|
+
return `TYPE ${name} :
|
|
137
|
+
STRUCT
|
|
138
|
+
${fields}
|
|
139
|
+
END_STRUCT
|
|
140
|
+
END_TYPE
|
|
141
|
+
`;
|
|
142
|
+
}
|
|
143
|
+
if (type.kind === "array") {
|
|
144
|
+
// Typegen publishes each key of a `Record<Enum, T>` as its own contract variable.
|
|
145
|
+
return `${type.index ? "{attribute 'tsplc_keyed'}\n" : ""}TYPE ${name} : ${typeText(type, (id) => this.name(id))};
|
|
146
|
+
END_TYPE
|
|
147
|
+
`;
|
|
148
|
+
}
|
|
149
|
+
if (type.kind === "string") {
|
|
150
|
+
return `TYPE ${name} : ${typeText(type, (id) => this.name(id))};
|
|
151
|
+
END_TYPE
|
|
152
|
+
`;
|
|
153
|
+
}
|
|
154
|
+
if (type.kind === "enum") {
|
|
155
|
+
const members = type.members
|
|
156
|
+
.map(
|
|
157
|
+
(member) =>
|
|
158
|
+
` ${this.field(type.id, member.name)} := ${member.value}`,
|
|
159
|
+
)
|
|
160
|
+
.join(",\n");
|
|
161
|
+
return `{attribute 'qualified_only'}
|
|
162
|
+
{attribute 'strict'}
|
|
163
|
+
TYPE ${name} : (
|
|
164
|
+
${members}
|
|
165
|
+
) DINT;
|
|
166
|
+
END_TYPE
|
|
167
|
+
`;
|
|
168
|
+
}
|
|
169
|
+
throw new Error("Unsupported verified data type");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private type(id: string): PlcType {
|
|
173
|
+
return required(this.types.get(id));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
name(id: string): string {
|
|
177
|
+
const named = this.names.get(id);
|
|
178
|
+
if (named !== undefined) {
|
|
179
|
+
return named;
|
|
180
|
+
}
|
|
181
|
+
const type = this.types.get(id);
|
|
182
|
+
if (
|
|
183
|
+
(type?.kind === "string" || type?.kind === "array") &&
|
|
184
|
+
this.inline(type)
|
|
185
|
+
) {
|
|
186
|
+
return typeText(type, (element) => this.name(element));
|
|
187
|
+
}
|
|
188
|
+
return id;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Whether a type is declared in place rather than as a data type of its own. A named alias of
|
|
193
|
+
* an array or a string would name its length, so a length change would change the variable's
|
|
194
|
+
* type, which an online change refuses; in place it changes only the variable's layout. A block
|
|
195
|
+
* array is always in place: a function block cannot be a data type's element. Keyed arrays keep
|
|
196
|
+
* their alias, whose attribute publishes each key.
|
|
197
|
+
*/
|
|
198
|
+
private inline(type: PlcType): boolean {
|
|
199
|
+
if (type.kind === "string") {
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
return (
|
|
203
|
+
type.kind === "array" &&
|
|
204
|
+
(!type.index || instancedBlock(type.id, this.types) !== undefined)
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
field(id: string, name: string): string {
|
|
209
|
+
return required(this.fields.get(id)?.get(name));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
declarationInitializer(value: Constant, id: string): string {
|
|
213
|
+
const type = this.type(id);
|
|
214
|
+
// Our DUT fields have no custom initializers. CODESYS initializes their
|
|
215
|
+
// numbers/BOOL/strings to zero/false/empty, including nested arrays/records.
|
|
216
|
+
// Keep explicit nonzero enum members and signed floating zero: omission must
|
|
217
|
+
// not change the initial value selected by PLC semantics.
|
|
218
|
+
if (
|
|
219
|
+
(type.kind === "record" || type.kind === "array") &&
|
|
220
|
+
this.isZero(value)
|
|
221
|
+
) {
|
|
222
|
+
return "";
|
|
223
|
+
}
|
|
224
|
+
return ` := ${this.initializer(value, id)}`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* A declaration's initial value. A record initializer names only the fields that are not zero,
|
|
229
|
+
* which CODESYS initializes as it would an omitted one; an enum field is always named. A record
|
|
230
|
+
* that is all zero but must still be written, as an element of an array, is written in full.
|
|
231
|
+
*/
|
|
232
|
+
private initializer(value: Constant, id: string): string {
|
|
233
|
+
const type = this.type(id);
|
|
234
|
+
if (type.kind === "array" && value.kind === "array") {
|
|
235
|
+
return `[${value.items.map((item) => this.initializer(item, type.element)).join(", ")}]`;
|
|
236
|
+
}
|
|
237
|
+
if (type.kind !== "record" || value.kind !== "record") {
|
|
238
|
+
return this.literal(value, id);
|
|
239
|
+
}
|
|
240
|
+
const fields = type.fields.flatMap((field) => {
|
|
241
|
+
const initial = required(value.fields[field.name]);
|
|
242
|
+
return this.type(field.type).kind !== "enum" && this.isZero(initial)
|
|
243
|
+
? []
|
|
244
|
+
: [
|
|
245
|
+
`${this.field(id, field.name)} := ${this.initializer(initial, field.type)}`,
|
|
246
|
+
];
|
|
247
|
+
});
|
|
248
|
+
return fields.length ? `(${fields.join(", ")})` : this.literal(value, id);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private isZero(value: Constant): boolean {
|
|
252
|
+
if (value.kind === "bool") {
|
|
253
|
+
return !value.value;
|
|
254
|
+
}
|
|
255
|
+
if (value.kind === "integer") {
|
|
256
|
+
return value.value === "0";
|
|
257
|
+
}
|
|
258
|
+
if (value.kind === "float") {
|
|
259
|
+
return Object.is(value.value, 0);
|
|
260
|
+
}
|
|
261
|
+
if (value.kind === "string") {
|
|
262
|
+
return value.value === "";
|
|
263
|
+
}
|
|
264
|
+
if (value.kind === "array") {
|
|
265
|
+
return value.items.every((item) => this.isZero(item));
|
|
266
|
+
}
|
|
267
|
+
if (value.kind === "record") {
|
|
268
|
+
return Object.values(value.fields).every((item) => this.isZero(item));
|
|
269
|
+
}
|
|
270
|
+
// Time/calendar values remain explicit without relying on target defaults.
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
literal(value: Constant, id: string): string {
|
|
275
|
+
const type = this.type(id);
|
|
276
|
+
if (type.kind === "enum" && value.kind === "integer") {
|
|
277
|
+
const member = required(
|
|
278
|
+
type.members.find((member) => member.value === value.value),
|
|
279
|
+
);
|
|
280
|
+
return `${this.name(id)}.${this.field(id, member.name)}`;
|
|
281
|
+
}
|
|
282
|
+
if (type.kind === "string" && value.kind === "string") {
|
|
283
|
+
const delimiter = type.encoding === "utf16" ? '"' : "'";
|
|
284
|
+
return `${delimiter}${escapeString(value.value, delimiter)}${delimiter}`;
|
|
285
|
+
}
|
|
286
|
+
if (type.kind === "array" && value.kind === "array") {
|
|
287
|
+
return `[${value.items.map((item) => this.literal(item, type.element)).join(", ")}]`;
|
|
288
|
+
}
|
|
289
|
+
if (type.kind === "record" && value.kind === "record") {
|
|
290
|
+
const fields = type.fields.map(
|
|
291
|
+
(field) =>
|
|
292
|
+
`${this.field(id, field.name)} := ${this.literal(required(value.fields[field.name]), field.type)}`,
|
|
293
|
+
);
|
|
294
|
+
return `(${fields.join(", ")})`;
|
|
295
|
+
}
|
|
296
|
+
if (type.kind === "duration" && value.kind === "duration") {
|
|
297
|
+
return `T#${BigInt(value.nanoseconds) / nanosecondsPerMillisecond}ms`;
|
|
298
|
+
}
|
|
299
|
+
if (type.kind === "calendar" && value.kind === "calendar") {
|
|
300
|
+
if (type.domain === "date") {
|
|
301
|
+
return `D#${value.value}`;
|
|
302
|
+
}
|
|
303
|
+
if (type.domain === "timeOfDay") {
|
|
304
|
+
return `TOD#${value.value}`;
|
|
305
|
+
}
|
|
306
|
+
return `DT#${value.value.replace("T", "-")}`;
|
|
307
|
+
}
|
|
308
|
+
return scalarLiteral(value, id);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { CodesysCompilerError } from "@tsplc/build";
|
|
2
|
+
import type { Diagnostic } from "@tsplc/compiler/ir";
|
|
3
|
+
import type { CodesysBundle } from "./emit";
|
|
4
|
+
|
|
5
|
+
type MappedBundle = Pick<CodesysBundle, "objects" | "mappings">;
|
|
6
|
+
|
|
7
|
+
/** An object's path below the Application, when the vendor's identity of it proves one. */
|
|
8
|
+
const applicationObjectPath = (
|
|
9
|
+
identity: NonNullable<CodesysCompilerError["diagnostics"][number]["object"]>,
|
|
10
|
+
): string | undefined => {
|
|
11
|
+
if (identity.path.at(-1) !== identity.name) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const relative = identity.applicationPath;
|
|
15
|
+
if (
|
|
16
|
+
relative?.length &&
|
|
17
|
+
identity.isApplicationChild === (relative.length === 1) &&
|
|
18
|
+
identity.path.length > relative.length &&
|
|
19
|
+
identity.path
|
|
20
|
+
.slice(-relative.length)
|
|
21
|
+
.every((part, index) => part === relative[index])
|
|
22
|
+
) {
|
|
23
|
+
return relative.join("/");
|
|
24
|
+
}
|
|
25
|
+
if (
|
|
26
|
+
relative === undefined &&
|
|
27
|
+
identity.isApplicationChild === true &&
|
|
28
|
+
identity.path.length >= 2
|
|
29
|
+
) {
|
|
30
|
+
return identity.name;
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Map verified vendor locations; preserve raw context for everything else. */
|
|
36
|
+
export function diagnoseCodesys(
|
|
37
|
+
cause: unknown,
|
|
38
|
+
bundle: MappedBundle,
|
|
39
|
+
): readonly Diagnostic[] {
|
|
40
|
+
const notes: string[] = [];
|
|
41
|
+
let current = cause;
|
|
42
|
+
while (current instanceof Error) {
|
|
43
|
+
if (current instanceof CodesysCompilerError && current.diagnostics.length) {
|
|
44
|
+
return current.diagnostics.map((item) => {
|
|
45
|
+
// Observed in CODESYS 3.5 SP22. position itself is an opaque vendor value.
|
|
46
|
+
const location =
|
|
47
|
+
item.positionText &&
|
|
48
|
+
/^Line ([1-9][0-9]*)(?:, Column ([1-9][0-9]*))? \((Decl|Impl)\)$/.exec(
|
|
49
|
+
item.positionText,
|
|
50
|
+
);
|
|
51
|
+
const identity = item.object;
|
|
52
|
+
const objectPath = identity && applicationObjectPath(identity);
|
|
53
|
+
const objects = objectPath
|
|
54
|
+
? bundle.objects.filter(
|
|
55
|
+
(object) =>
|
|
56
|
+
object.name === identity?.name &&
|
|
57
|
+
object.nativePath === objectPath,
|
|
58
|
+
)
|
|
59
|
+
: [];
|
|
60
|
+
const match = objects.length === 1 ? objects[0] : undefined;
|
|
61
|
+
const path =
|
|
62
|
+
location?.[3] === "Decl"
|
|
63
|
+
? match?.sourcePath
|
|
64
|
+
: match?.implementationPath;
|
|
65
|
+
const mappings =
|
|
66
|
+
location && path
|
|
67
|
+
? bundle.mappings.filter(
|
|
68
|
+
(mapping) =>
|
|
69
|
+
mapping.path === path && mapping.line === Number(location[1]),
|
|
70
|
+
)
|
|
71
|
+
: [];
|
|
72
|
+
const mapping = mappings.length === 1 ? mappings[0] : undefined;
|
|
73
|
+
return {
|
|
74
|
+
origin: "vendor",
|
|
75
|
+
code: "PLC8001",
|
|
76
|
+
severity: "error",
|
|
77
|
+
message: item.text,
|
|
78
|
+
...(mapping && { span: mapping.span }),
|
|
79
|
+
notes: [
|
|
80
|
+
...notes,
|
|
81
|
+
...(location && path
|
|
82
|
+
? [
|
|
83
|
+
"Generated ST: " +
|
|
84
|
+
path +
|
|
85
|
+
":" +
|
|
86
|
+
location[1] +
|
|
87
|
+
(location[2] ? `:${location[2]}` : ""),
|
|
88
|
+
]
|
|
89
|
+
: []),
|
|
90
|
+
`CODESYS diagnostic: ${JSON.stringify(item)}`,
|
|
91
|
+
],
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
notes.push(current.message);
|
|
96
|
+
if (current.cause === undefined) {
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
current = current.cause;
|
|
100
|
+
if (!(current instanceof Error)) {
|
|
101
|
+
notes.push(String(current));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return [
|
|
105
|
+
{
|
|
106
|
+
origin: "vendor",
|
|
107
|
+
code: "PLC8001",
|
|
108
|
+
severity: "error",
|
|
109
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
110
|
+
...(notes.length > 1 && { notes: notes.slice(1) }),
|
|
111
|
+
},
|
|
112
|
+
];
|
|
113
|
+
}
|