@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
package/src/frontend/exposure.ts
CHANGED
|
@@ -1,149 +1,181 @@
|
|
|
1
1
|
import { runtimeSources } from "./module-graph.ts";
|
|
2
|
+
import { namesSdk, symbolAt } from "./sdk.ts";
|
|
2
3
|
import ts from "typescript";
|
|
3
4
|
import { createHash } from "node:crypto";
|
|
4
|
-
import {
|
|
5
|
+
import { elementaryType, isIntegerValued } from "@tsplc/iec";
|
|
5
6
|
import { error, type Diagnostic } from "@tsplc/compiler/ir";
|
|
6
7
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
copied,
|
|
9
|
+
edited,
|
|
10
|
+
from,
|
|
11
|
+
joined,
|
|
12
|
+
mapped,
|
|
13
|
+
type Edit,
|
|
14
|
+
type Mapped,
|
|
15
|
+
type Positions,
|
|
15
16
|
} from "./positions.ts";
|
|
16
17
|
|
|
18
|
+
/** Telemetry carries a BOOL, an integer or bit string of up to 32 bits, or a bounded STRING. */
|
|
19
|
+
const telemetryType = (annotation: string): boolean => {
|
|
20
|
+
const type = elementaryType(annotation);
|
|
21
|
+
return (
|
|
22
|
+
type?.family === "bool" ||
|
|
23
|
+
(isIntegerValued(type) && type.bits <= 32) ||
|
|
24
|
+
/^STRING<\d+>$/.test(annotation)
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
|
|
17
28
|
/** Telemetry is sampled by its authored task and published read-only by the backend. */
|
|
18
|
-
export const lowerExposure = (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
29
|
+
export const lowerExposure = (
|
|
30
|
+
program: ts.Program,
|
|
31
|
+
sdk: string,
|
|
32
|
+
positions: Positions,
|
|
33
|
+
) => {
|
|
34
|
+
const checker = program.getTypeChecker();
|
|
35
|
+
const sources = new Map<string, Mapped>();
|
|
36
|
+
const diagnostics: Diagnostic[] = [];
|
|
37
|
+
const variables = new Map<string, { name: string; version: string }>();
|
|
38
|
+
const names = new Set<string>();
|
|
39
|
+
for (const source of runtimeSources(program)) {
|
|
40
|
+
if (source.isDeclarationFile) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const edits: Edit[] = [];
|
|
44
|
+
const extra: Mapped[] = [];
|
|
45
|
+
const visit = (node: ts.Node): void => {
|
|
46
|
+
if (
|
|
47
|
+
!ts.isCallExpression(node) ||
|
|
48
|
+
!namesSdk(checker, sdk, node.expression, "expose")
|
|
49
|
+
) {
|
|
50
|
+
ts.forEachChild(node, visit);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const fail = (message: string) =>
|
|
54
|
+
diagnostics.push(error("PLC1014", message, positions.node(node)));
|
|
55
|
+
const [label, values] = node.arguments;
|
|
56
|
+
if (
|
|
57
|
+
!label ||
|
|
58
|
+
!ts.isStringLiteral(label) ||
|
|
59
|
+
!/^[A-Za-z_][A-Za-z0-9_]*$/.test(label.text) ||
|
|
60
|
+
label.text === "__proto__" ||
|
|
61
|
+
!values ||
|
|
62
|
+
!ts.isObjectLiteralExpression(values) ||
|
|
63
|
+
!ts.isExpressionStatement(node.parent)
|
|
64
|
+
) {
|
|
65
|
+
fail(
|
|
66
|
+
"expose requires a literal machine name and a flat telemetry object in a synchronous cyclic routine.",
|
|
67
|
+
);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
let owner: ts.Node | undefined = node.parent;
|
|
71
|
+
while (owner && !ts.isFunctionLike(owner)) {
|
|
72
|
+
owner = owner.parent;
|
|
73
|
+
}
|
|
74
|
+
if (
|
|
75
|
+
!owner ||
|
|
76
|
+
(ts.canHaveModifiers(owner) &&
|
|
77
|
+
ts
|
|
78
|
+
.getModifiers(owner)
|
|
79
|
+
?.some((value) => value.kind === ts.SyntaxKind.AsyncKeyword))
|
|
80
|
+
) {
|
|
81
|
+
fail(
|
|
82
|
+
"Sample telemetry from a synchronous cyclic routine, outside async continuations.",
|
|
83
|
+
);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (names.has(label.text)) {
|
|
87
|
+
fail(`Duplicate machine exposure ${label.text}.`);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
names.add(label.text);
|
|
91
|
+
const fields: { name: string; type: string; value: Mapped }[] = [];
|
|
92
|
+
for (const property of values.properties) {
|
|
93
|
+
if (
|
|
94
|
+
!ts.isPropertyAssignment(property) ||
|
|
95
|
+
!ts.isIdentifier(property.name) ||
|
|
96
|
+
["contractVersion", "__proto__"].includes(property.name.text)
|
|
97
|
+
) {
|
|
98
|
+
fail(
|
|
99
|
+
"Telemetry uses named scalar fields; contractVersion and __proto__ are reserved.",
|
|
100
|
+
);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const value = property.initializer;
|
|
104
|
+
const type = checker.getTypeAtLocation(value);
|
|
105
|
+
let annotation: string | undefined;
|
|
106
|
+
if (type.flags & ts.TypeFlags.BooleanLike) {
|
|
107
|
+
annotation = "BOOL";
|
|
108
|
+
}
|
|
109
|
+
const declaration = symbolAt(
|
|
110
|
+
checker,
|
|
111
|
+
ts.isPropertyAccessExpression(value) ? value.name : value,
|
|
112
|
+
)?.valueDeclaration;
|
|
113
|
+
if (
|
|
114
|
+
declaration &&
|
|
115
|
+
(ts.isPropertySignature(declaration) ||
|
|
116
|
+
ts.isPropertyDeclaration(declaration) ||
|
|
117
|
+
ts.isVariableDeclaration(declaration) ||
|
|
118
|
+
ts.isParameter(declaration)) &&
|
|
119
|
+
declaration.type
|
|
120
|
+
) {
|
|
121
|
+
const text = declaration.type.getText(declaration.getSourceFile());
|
|
122
|
+
if (telemetryType(text)) {
|
|
123
|
+
annotation = text;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (!annotation) {
|
|
127
|
+
fail(
|
|
128
|
+
`Telemetry ${property.name.text} needs a concrete BOOL, integer up to 32 bits or bounded STRING annotation.`,
|
|
129
|
+
);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
fields.push({
|
|
133
|
+
name: property.name.text,
|
|
134
|
+
type: annotation,
|
|
135
|
+
value: copied(value),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if (!fields.length) {
|
|
139
|
+
fail("Expose at least one telemetry field.");
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const name = `tsplcExposed_${label.text}`;
|
|
143
|
+
const record = `TsplcTelemetry_${label.text}`;
|
|
144
|
+
const version = createHash("sha256")
|
|
145
|
+
.update(
|
|
146
|
+
JSON.stringify({
|
|
147
|
+
abi: 1,
|
|
148
|
+
name: label.text,
|
|
149
|
+
fields: fields.map(({ name, type }) => ({ name, type })),
|
|
150
|
+
}),
|
|
151
|
+
)
|
|
152
|
+
.digest("hex");
|
|
153
|
+
extra.push(
|
|
154
|
+
from(
|
|
155
|
+
node,
|
|
156
|
+
`export interface ${record}{contractVersion:STRING<64>;${fields.map((field) => `${field.name}:${field.type}`).join(";")}}\nexport let ${name}:${record}=defaultValue<${record}>();`,
|
|
157
|
+
),
|
|
158
|
+
);
|
|
159
|
+
variables.set(`${source.fileName}:${name}`, {
|
|
160
|
+
name: label.text,
|
|
161
|
+
version,
|
|
162
|
+
});
|
|
163
|
+
edits.push({
|
|
164
|
+
start: node.getStart(source),
|
|
165
|
+
end: node.end,
|
|
166
|
+
text: mapped`{${name}.contractVersion=${JSON.stringify(version)};${joined(
|
|
167
|
+
fields.map((field) => mapped`${name}.${field.name}=${field.value};`),
|
|
168
|
+
"",
|
|
169
|
+
)}}`,
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
visit(source);
|
|
173
|
+
if (edits.length) {
|
|
174
|
+
sources.set(
|
|
175
|
+
source.fileName,
|
|
176
|
+
mapped`${edited(source, edits)}\n${joined(extra, "\n")}`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return { sources, diagnostics, variables };
|
|
149
181
|
};
|