@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/ir/link.ts
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { telemetryOperands, type PlcModule } from "./model.ts";
|
|
2
|
-
import {
|
|
3
|
-
nestedBodies,
|
|
4
|
-
type CoreFunction,
|
|
5
|
-
type CoreFunctionBlock,
|
|
6
|
-
type CoreStatement,
|
|
7
|
-
} from "./core-model.ts";
|
|
8
|
-
import type { ModuleDeclarations } from "./declarations.ts";
|
|
9
|
-
import { reject } from "./normalization-error.ts";
|
|
10
|
-
import { instancedBlock } from "./types.ts";
|
|
11
|
-
|
|
12
|
-
// Validate the complete authored call graph before pruning unreachable code.
|
|
13
|
-
// Static instances keep their child layouts even when a child has no scan call.
|
|
14
|
-
export function linkModule(
|
|
15
|
-
module: PlcModule,
|
|
16
|
-
declarations: ModuleDeclarations,
|
|
17
|
-
functions: CoreFunction[],
|
|
18
|
-
normalizedBlocks: CoreFunctionBlock[],
|
|
19
|
-
edges: ReadonlyMap<string, ReadonlySet<string>>,
|
|
20
|
-
) {
|
|
21
|
-
const { signatures, blocks, types } = declarations;
|
|
22
|
-
const names = new Set<string>(),
|
|
23
|
-
entries = new Set<string>();
|
|
24
|
-
for (const program of module.programs) {
|
|
25
|
-
declarations.claimId(program.id);
|
|
26
|
-
const entry = signatures.get(program.entry);
|
|
27
|
-
if (!entry || entry.owner || entry.parameters.length || entry.result)
|
|
28
|
-
reject("Program entry must be a parameterless void helper.");
|
|
29
|
-
if (names.has(program.name)) reject(`Duplicate program ${program.name}.`);
|
|
30
|
-
names.add(program.name);
|
|
31
|
-
entries.add(program.entry);
|
|
32
|
-
}
|
|
33
|
-
if (!module.programs.length) reject("At least one program is required.");
|
|
34
|
-
|
|
35
|
-
// Authored execution cycles are invalid even in unreachable routines. Static
|
|
36
|
-
// allocation has its own cycle check because it does not depend on scan calls.
|
|
37
|
-
const visited = new Set<string>(),
|
|
38
|
-
active = new Set<string>();
|
|
39
|
-
const visit = (id: string): void => {
|
|
40
|
-
if (active.has(id)) reject("Recursive scan calls are prohibited.", signatures.get(id)?.span);
|
|
41
|
-
if (visited.has(id)) return;
|
|
42
|
-
active.add(id);
|
|
43
|
-
for (const child of edges.get(id) ?? []) {
|
|
44
|
-
if (entries.has(child))
|
|
45
|
-
reject("Program entries cannot be called as ordinary helpers.", signatures.get(id)?.span);
|
|
46
|
-
visit(child);
|
|
47
|
-
}
|
|
48
|
-
active.delete(id);
|
|
49
|
-
visited.add(id);
|
|
50
|
-
};
|
|
51
|
-
for (const fn of [...functions, ...normalizedBlocks]) visit(fn.id);
|
|
52
|
-
|
|
53
|
-
// A bounded loop can only reconstruct its counter if called helpers leave
|
|
54
|
-
// that storage alone. Include transitive calls, not just direct assignments.
|
|
55
|
-
const routines = new Map([...functions, ...normalizedBlocks].map((fn) => [fn.id, fn]));
|
|
56
|
-
const variables = new Map(
|
|
57
|
-
[
|
|
58
|
-
...module.variables,
|
|
59
|
-
...functions.flatMap((fn) => [...fn.parameters, ...fn.locals]),
|
|
60
|
-
...normalizedBlocks.flatMap((block) => [
|
|
61
|
-
...block.inputs,
|
|
62
|
-
...block.outputs,
|
|
63
|
-
...block.state,
|
|
64
|
-
...block.locals,
|
|
65
|
-
]),
|
|
66
|
-
].map((variable) => [variable.id, variable]),
|
|
67
|
-
);
|
|
68
|
-
const calledRoutine = (item: CoreStatement): string | undefined => {
|
|
69
|
-
if (item.kind === "invoke") return item.function;
|
|
70
|
-
if (item.kind !== "blockUpdate") return undefined;
|
|
71
|
-
const variable = variables.get(item.instance);
|
|
72
|
-
return variable && instancedBlock(variable.type, types)?.block;
|
|
73
|
-
};
|
|
74
|
-
const writes = new Map<string, Set<string>>();
|
|
75
|
-
const routineWrites = (id: string): Set<string> => {
|
|
76
|
-
const cached = writes.get(id);
|
|
77
|
-
if (cached) return cached;
|
|
78
|
-
const result = new Set<string>();
|
|
79
|
-
const inspect = (body: readonly CoreStatement[]) => {
|
|
80
|
-
for (const item of body) {
|
|
81
|
-
if (item.kind === "assign") result.add(item.root);
|
|
82
|
-
if (item.kind === "for") result.add(item.counter);
|
|
83
|
-
if (item.kind === "invoke") {
|
|
84
|
-
if (item.destination) result.add(item.destination);
|
|
85
|
-
for (const argument of item.arguments)
|
|
86
|
-
if (argument.kind === "borrow") result.add(argument.place.root);
|
|
87
|
-
}
|
|
88
|
-
if (item.kind === "blockUpdate")
|
|
89
|
-
for (const input of Object.values(item.inputs))
|
|
90
|
-
if (input.kind === "borrow") result.add(input.place.root);
|
|
91
|
-
for (const nested of nestedBodies(item)) inspect(nested);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
inspect(routines.get(id)!.body);
|
|
95
|
-
for (const child of edges.get(id) ?? [])
|
|
96
|
-
for (const root of routineWrites(child)) result.add(root);
|
|
97
|
-
writes.set(id, result);
|
|
98
|
-
return result;
|
|
99
|
-
};
|
|
100
|
-
const checkCalls = (body: readonly CoreStatement[], counters: ReadonlySet<string>) => {
|
|
101
|
-
for (const item of body) {
|
|
102
|
-
const called = calledRoutine(item);
|
|
103
|
-
if (called && [...counters].some((counter) => routineWrites(called).has(counter)))
|
|
104
|
-
reject("A called routine may mutate an active loop counter.", item.span);
|
|
105
|
-
const nestedCounters = item.kind === "for" ? new Set([...counters, item.counter]) : counters;
|
|
106
|
-
for (const nested of nestedBodies(item)) checkCalls(nested, nestedCounters);
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
for (const routine of routines.values()) checkCalls(routine.body, new Set());
|
|
110
|
-
|
|
111
|
-
// Only after the complete graph is valid may emission discard unused helpers.
|
|
112
|
-
const reachable = new Set<string>();
|
|
113
|
-
const link = (id: string): void => {
|
|
114
|
-
if (reachable.has(id)) return;
|
|
115
|
-
reachable.add(id);
|
|
116
|
-
for (const child of edges.get(id) ?? []) link(child);
|
|
117
|
-
const owner = signatures.get(id)?.owner;
|
|
118
|
-
if (owner) link(owner.block);
|
|
119
|
-
for (const method of functions.filter((fn) => fn.owner?.block === id)) link(method.id);
|
|
120
|
-
for (const instance of blocks
|
|
121
|
-
.get(id)
|
|
122
|
-
?.state.filter((variable) => variable.storage === "instance") ?? []) {
|
|
123
|
-
const type = instancedBlock(instance.type, types);
|
|
124
|
-
if (type) link(type.block);
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
for (const entry of entries) link(entry);
|
|
128
|
-
for (const variable of module.variables) {
|
|
129
|
-
const type = instancedBlock(variable.type, types);
|
|
130
|
-
if (type) link(type.block);
|
|
131
|
-
}
|
|
132
|
-
const linkedFunctions = functions.filter((fn) => reachable.has(fn.id)),
|
|
133
|
-
linkedBlocks = normalizedBlocks.filter((block) => reachable.has(block.id));
|
|
134
|
-
|
|
135
|
-
// Admission has already validated every referenced type; these lookups are
|
|
136
|
-
// closed over the declaration registry, including dependencies kept by DUTs.
|
|
137
|
-
const usedTypes = new Set<string>();
|
|
138
|
-
const useType = (id: string): void => {
|
|
139
|
-
if (usedTypes.has(id)) return;
|
|
140
|
-
usedTypes.add(id);
|
|
141
|
-
const type = types.get(id)!;
|
|
142
|
-
if (type.kind === "record") {
|
|
143
|
-
for (const field of type.fields) useType(field.type);
|
|
144
|
-
}
|
|
145
|
-
if (type.kind === "array") {
|
|
146
|
-
useType(type.element);
|
|
147
|
-
if (type.index) useType(type.index);
|
|
148
|
-
}
|
|
149
|
-
if (type.kind === "enum") useType(type.base);
|
|
150
|
-
if (type.kind === "block" && type.output) useType(type.output);
|
|
151
|
-
};
|
|
152
|
-
// Primitive definitions also support backend-generated numeric helpers.
|
|
153
|
-
types.forEach((type) => {
|
|
154
|
-
if (["bool", "integer", "bits", "float", "duration", "calendar"].includes(type.kind))
|
|
155
|
-
useType(type.id);
|
|
156
|
-
});
|
|
157
|
-
for (const variable of module.variables) useType(variable.type);
|
|
158
|
-
for (const fn of linkedFunctions) {
|
|
159
|
-
if (fn.result) useType(fn.result);
|
|
160
|
-
for (const variable of [...fn.parameters, ...fn.locals]) useType(variable.type);
|
|
161
|
-
}
|
|
162
|
-
for (const block of linkedBlocks) {
|
|
163
|
-
if (block.result) useType(block.result);
|
|
164
|
-
for (const variable of [...block.inputs, ...block.outputs, ...block.state, ...block.locals]) {
|
|
165
|
-
useType(variable.type);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
// A telemetry property can be its type's only use, such as an enum field the elaborator folded.
|
|
169
|
-
const useTelemetryTypes = (body: readonly CoreStatement[]): void => {
|
|
170
|
-
for (const item of body) {
|
|
171
|
-
if (item.kind === "telemetry")
|
|
172
|
-
for (const operand of telemetryOperands(item)) useType(operand.type);
|
|
173
|
-
for (const nested of nestedBodies(item)) useTelemetryTypes(nested);
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
for (const routine of [...linkedFunctions, ...linkedBlocks]) useTelemetryTypes(routine.body);
|
|
177
|
-
return {
|
|
178
|
-
types: module.types.filter((type) => usedTypes.has(type.id)),
|
|
179
|
-
variables: module.variables,
|
|
180
|
-
functions: linkedFunctions,
|
|
181
|
-
blocks: linkedBlocks,
|
|
182
|
-
programs: module.programs,
|
|
183
|
-
...(module.folded && { folded: module.folded }),
|
|
184
|
-
};
|
|
185
|
-
}
|
package/ir/model.ts
DELETED
|
@@ -1,346 +0,0 @@
|
|
|
1
|
-
import type { SourceSpan, Diagnostic } from "./diagnostics.ts";
|
|
2
|
-
import type { PlcType, SymbolId, TypeId } from "./types.ts";
|
|
3
|
-
|
|
4
|
-
export type Constant =
|
|
5
|
-
| { kind: "bool"; value: boolean }
|
|
6
|
-
| { kind: "integer"; value: string }
|
|
7
|
-
| { kind: "float"; value: number }
|
|
8
|
-
| { kind: "duration"; nanoseconds: string }
|
|
9
|
-
| { kind: "calendar"; value: string }
|
|
10
|
-
| { kind: "string"; value: string }
|
|
11
|
-
| { kind: "array"; items: readonly Constant[] }
|
|
12
|
-
| { kind: "record"; fields: Readonly<Record<string, Constant>> };
|
|
13
|
-
|
|
14
|
-
export interface Variable {
|
|
15
|
-
id: SymbolId;
|
|
16
|
-
name: string;
|
|
17
|
-
type: TypeId;
|
|
18
|
-
storage:
|
|
19
|
-
| "module"
|
|
20
|
-
| "local"
|
|
21
|
-
| "parameter"
|
|
22
|
-
| "input"
|
|
23
|
-
| "output"
|
|
24
|
-
| "field"
|
|
25
|
-
| "instance"
|
|
26
|
-
| "constant"
|
|
27
|
-
| "blockOutput";
|
|
28
|
-
mutable: boolean;
|
|
29
|
-
retain: boolean;
|
|
30
|
-
exported?: true;
|
|
31
|
-
exposure?: { name: string; version: string };
|
|
32
|
-
mode?: "inOut";
|
|
33
|
-
initial?: Constant;
|
|
34
|
-
span: SourceSpan;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface Place {
|
|
38
|
-
root: SymbolId;
|
|
39
|
-
path: readonly ({ kind: "field"; name: string } | { kind: "index"; value: Expression })[];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type BinaryOperator =
|
|
43
|
-
| "add"
|
|
44
|
-
| "subtract"
|
|
45
|
-
| "multiply"
|
|
46
|
-
| "divide"
|
|
47
|
-
| "remainder"
|
|
48
|
-
| "equal"
|
|
49
|
-
| "notEqual"
|
|
50
|
-
| "less"
|
|
51
|
-
| "lessEqual"
|
|
52
|
-
| "greater"
|
|
53
|
-
| "greaterEqual"
|
|
54
|
-
| "and"
|
|
55
|
-
| "or"
|
|
56
|
-
| "bitAnd"
|
|
57
|
-
| "bitOr"
|
|
58
|
-
| "bitXor"
|
|
59
|
-
| "shiftLeft"
|
|
60
|
-
| "shiftRight";
|
|
61
|
-
|
|
62
|
-
export const comparisonOperators: ReadonlySet<BinaryOperator> = new Set([
|
|
63
|
-
"equal",
|
|
64
|
-
"notEqual",
|
|
65
|
-
"less",
|
|
66
|
-
"lessEqual",
|
|
67
|
-
"greater",
|
|
68
|
-
"greaterEqual",
|
|
69
|
-
]);
|
|
70
|
-
|
|
71
|
-
export type Expression = { type: TypeId; span: SourceSpan } & (
|
|
72
|
-
| { kind: "constant"; value: Constant }
|
|
73
|
-
| { kind: "load"; place: Place }
|
|
74
|
-
| {
|
|
75
|
-
kind: "unary";
|
|
76
|
-
operator: "not" | "negate" | "bitNot" | "timeToMilliseconds" | "clock";
|
|
77
|
-
operand: Expression;
|
|
78
|
-
}
|
|
79
|
-
| {
|
|
80
|
-
kind: "binary";
|
|
81
|
-
operator: BinaryOperator;
|
|
82
|
-
left: Expression;
|
|
83
|
-
right: Expression;
|
|
84
|
-
policy?: "wrap" | "saturate";
|
|
85
|
-
}
|
|
86
|
-
| {
|
|
87
|
-
kind: "call";
|
|
88
|
-
function: SymbolId;
|
|
89
|
-
instance?: SymbolId;
|
|
90
|
-
index?: Expression;
|
|
91
|
-
arguments: readonly Expression[];
|
|
92
|
-
}
|
|
93
|
-
| { kind: "conditional"; condition: Expression; then: Expression; else: Expression }
|
|
94
|
-
| { kind: "convert"; operand: Expression; policy: "exact" | "wrap" | "saturate" }
|
|
95
|
-
| { kind: "borrow"; place: Place }
|
|
96
|
-
| { kind: "length"; operand: Expression }
|
|
97
|
-
| { kind: "copy"; operand: Expression }
|
|
98
|
-
| { kind: "record"; fields: Readonly<Record<string, Expression>> }
|
|
99
|
-
| { kind: "array"; items: readonly Expression[] }
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
export type Statement = { span: SourceSpan } & (
|
|
103
|
-
| { kind: "assign"; destination: Place; value: Expression }
|
|
104
|
-
| { kind: "if"; condition: Expression; then: readonly Statement[]; else: readonly Statement[] }
|
|
105
|
-
| {
|
|
106
|
-
kind: "switch";
|
|
107
|
-
value: Expression;
|
|
108
|
-
cases: readonly { values: readonly Constant[]; body: readonly Statement[] }[];
|
|
109
|
-
fallback: readonly Statement[];
|
|
110
|
-
}
|
|
111
|
-
| {
|
|
112
|
-
kind: "for";
|
|
113
|
-
counter: SymbolId;
|
|
114
|
-
/** Existing storage remains observable after the loop exits. */
|
|
115
|
-
counterScope?: "existing";
|
|
116
|
-
start: Expression;
|
|
117
|
-
limit: Expression;
|
|
118
|
-
step: string;
|
|
119
|
-
exclusive?: true;
|
|
120
|
-
maxIterations: number;
|
|
121
|
-
body: readonly Statement[];
|
|
122
|
-
}
|
|
123
|
-
| {
|
|
124
|
-
kind: "invoke";
|
|
125
|
-
function: SymbolId;
|
|
126
|
-
instance?: SymbolId;
|
|
127
|
-
index?: Expression;
|
|
128
|
-
arguments: readonly Expression[];
|
|
129
|
-
destination?: Place;
|
|
130
|
-
}
|
|
131
|
-
| {
|
|
132
|
-
kind: "blockUpdate";
|
|
133
|
-
instance: SymbolId;
|
|
134
|
-
index?: Expression;
|
|
135
|
-
inputs: Readonly<Record<string, Expression>>;
|
|
136
|
-
}
|
|
137
|
-
| { kind: "return"; value?: Expression }
|
|
138
|
-
| { kind: "break" | "continue" }
|
|
139
|
-
| ({ kind: "telemetry" } & TelemetryCall<Expression>)
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* A `@tsplc/compiler/telemetry` call. `record` is what the record says besides its properties;
|
|
144
|
-
* a property with a `length` is a byte array, borrowed, sent as text up to that length.
|
|
145
|
-
*/
|
|
146
|
-
export interface TelemetryCall<E> {
|
|
147
|
-
record:
|
|
148
|
-
| { kind: "event"; event: string }
|
|
149
|
-
| { kind: "log"; level: "info" | "warn" | "error"; message: string }
|
|
150
|
-
| { kind: "exception"; code: E };
|
|
151
|
-
properties: readonly { name: string; value: E; length?: E }[];
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** Every value a telemetry call reads, in evaluation order. */
|
|
155
|
-
export const telemetryOperands = <E>({ record, properties }: TelemetryCall<E>): E[] => [
|
|
156
|
-
...(record.kind === "exception" ? [record.code] : []),
|
|
157
|
-
...properties.flatMap(({ value, length }) => (length ? [value, length] : [value])),
|
|
158
|
-
];
|
|
159
|
-
|
|
160
|
-
export interface MethodOwner {
|
|
161
|
-
block: SymbolId;
|
|
162
|
-
access: "public" | "private" | "protected";
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export interface PlcFunction {
|
|
166
|
-
owner?: MethodOwner;
|
|
167
|
-
id: SymbolId;
|
|
168
|
-
name: string;
|
|
169
|
-
parameters: readonly Variable[];
|
|
170
|
-
locals: readonly Variable[];
|
|
171
|
-
result?: TypeId;
|
|
172
|
-
/** A `@tsplc/compiler/math` helper, whose result range follows from its arguments' ranges. */
|
|
173
|
-
bounds?: MathHelper;
|
|
174
|
-
/** The source method this function was specialized from, as `Class.method`, and the TypeScript path of its `this`. */
|
|
175
|
-
origin?: { name: string; instance?: string };
|
|
176
|
-
body: readonly Statement[];
|
|
177
|
-
span: SourceSpan;
|
|
178
|
-
}
|
|
179
|
-
export type MathHelper = "min" | "max" | "clamp";
|
|
180
|
-
|
|
181
|
-
// Native vendor blocks under their TypeScript names. Ports are [name, native name, type];
|
|
182
|
-
// every output starts at its type's default, matching the vendor library blocks.
|
|
183
|
-
const timer = (input: string, output: string) =>
|
|
184
|
-
({
|
|
185
|
-
inputs: [
|
|
186
|
-
[input, "IN", "BOOL"],
|
|
187
|
-
["duration", "PT", "TIME"],
|
|
188
|
-
],
|
|
189
|
-
outputs: [
|
|
190
|
-
[output, "Q", "BOOL"],
|
|
191
|
-
["elapsed", "ET", "TIME"],
|
|
192
|
-
],
|
|
193
|
-
}) as const;
|
|
194
|
-
export const standardBlocks = {
|
|
195
|
-
OnDelayTimer: { native: "TON", ...timer("enabled", "done") },
|
|
196
|
-
OffDelayTimer: { native: "TOF", ...timer("enabled", "active") },
|
|
197
|
-
PulseTimer: { native: "TP", ...timer("start", "active") },
|
|
198
|
-
RisingEdge: {
|
|
199
|
-
native: "R_TRIG",
|
|
200
|
-
inputs: [["signal", "CLK", "BOOL"]],
|
|
201
|
-
outputs: [["rose", "Q", "BOOL"]],
|
|
202
|
-
},
|
|
203
|
-
FallingEdge: {
|
|
204
|
-
native: "F_TRIG",
|
|
205
|
-
inputs: [["signal", "CLK", "BOOL"]],
|
|
206
|
-
outputs: [["fell", "Q", "BOOL"]],
|
|
207
|
-
},
|
|
208
|
-
UpCounter: {
|
|
209
|
-
native: "CTU",
|
|
210
|
-
inputs: [
|
|
211
|
-
["countUp", "CU", "BOOL"],
|
|
212
|
-
["reset", "RESET", "BOOL"],
|
|
213
|
-
["preset", "PV", "WORD"],
|
|
214
|
-
],
|
|
215
|
-
outputs: [
|
|
216
|
-
["reachedPreset", "Q", "BOOL"],
|
|
217
|
-
["value", "CV", "WORD"],
|
|
218
|
-
],
|
|
219
|
-
},
|
|
220
|
-
DownCounter: {
|
|
221
|
-
native: "CTD",
|
|
222
|
-
inputs: [
|
|
223
|
-
["countDown", "CD", "BOOL"],
|
|
224
|
-
["load", "LOAD", "BOOL"],
|
|
225
|
-
["preset", "PV", "WORD"],
|
|
226
|
-
],
|
|
227
|
-
outputs: [
|
|
228
|
-
["atZero", "Q", "BOOL"],
|
|
229
|
-
["value", "CV", "WORD"],
|
|
230
|
-
],
|
|
231
|
-
},
|
|
232
|
-
UpDownCounter: {
|
|
233
|
-
native: "CTUD",
|
|
234
|
-
inputs: [
|
|
235
|
-
["countUp", "CU", "BOOL"],
|
|
236
|
-
["countDown", "CD", "BOOL"],
|
|
237
|
-
["reset", "RESET", "BOOL"],
|
|
238
|
-
["load", "LOAD", "BOOL"],
|
|
239
|
-
["preset", "PV", "WORD"],
|
|
240
|
-
],
|
|
241
|
-
outputs: [
|
|
242
|
-
["reachedPreset", "QU", "BOOL"],
|
|
243
|
-
["atZero", "QD", "BOOL"],
|
|
244
|
-
["value", "CV", "WORD"],
|
|
245
|
-
],
|
|
246
|
-
},
|
|
247
|
-
Blinker: {
|
|
248
|
-
native: "Util.BLINK",
|
|
249
|
-
inputs: [
|
|
250
|
-
["enabled", "ENABLE", "BOOL"],
|
|
251
|
-
["offTime", "TIMELOW", "TIME"],
|
|
252
|
-
["onTime", "TIMEHIGH", "TIME"],
|
|
253
|
-
],
|
|
254
|
-
outputs: [["on", "OUT", "BOOL"]],
|
|
255
|
-
},
|
|
256
|
-
} as const;
|
|
257
|
-
export type StandardBlock = keyof typeof standardBlocks;
|
|
258
|
-
|
|
259
|
-
export interface FunctionBlock {
|
|
260
|
-
id: SymbolId;
|
|
261
|
-
name: string;
|
|
262
|
-
inputs: readonly Variable[];
|
|
263
|
-
outputs: readonly Variable[];
|
|
264
|
-
state: readonly Variable[];
|
|
265
|
-
locals: readonly Variable[];
|
|
266
|
-
body: readonly Statement[];
|
|
267
|
-
result?: TypeId;
|
|
268
|
-
standard?: StandardBlock;
|
|
269
|
-
runtime?:
|
|
270
|
-
| { kind: "mailbox" }
|
|
271
|
-
| { kind: "backgroundHost" }
|
|
272
|
-
| { kind: "rpc"; path: string }
|
|
273
|
-
| {
|
|
274
|
-
kind: "http";
|
|
275
|
-
url: string;
|
|
276
|
-
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
277
|
-
json: boolean;
|
|
278
|
-
};
|
|
279
|
-
/** The async function lowered to this block, and the source each `step` value stands for. */
|
|
280
|
-
job?: {
|
|
281
|
-
name: string;
|
|
282
|
-
/** The TypeScript path of `this`, for a method specialized to one instance. */
|
|
283
|
-
instance?: string;
|
|
284
|
-
span: SourceSpan;
|
|
285
|
-
steps: readonly {
|
|
286
|
-
step: number;
|
|
287
|
-
kind: "await" | "return" | "throw" | "statement";
|
|
288
|
-
span: SourceSpan;
|
|
289
|
-
text: string;
|
|
290
|
-
}[];
|
|
291
|
-
};
|
|
292
|
-
span: SourceSpan;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
export interface PlcProgram {
|
|
296
|
-
id: SymbolId;
|
|
297
|
-
name: string;
|
|
298
|
-
entry: SymbolId;
|
|
299
|
-
continuation?: { instance: string; file: string };
|
|
300
|
-
background?: { intervalNanoseconds: string; priority: number; mailbox: string; worker: string };
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
export interface PlcModule {
|
|
304
|
-
/**
|
|
305
|
-
* Fields the elaborator folded into constants: no storage, but a known value at each TypeScript
|
|
306
|
-
* path. `type` is the scalar's PLC type; strings, often enum members, have none.
|
|
307
|
-
*/
|
|
308
|
-
folded?: readonly { path: string; value: Constant; type?: TypeId; span: SourceSpan }[];
|
|
309
|
-
types: readonly PlcType[];
|
|
310
|
-
variables: readonly Variable[];
|
|
311
|
-
functions: readonly PlcFunction[];
|
|
312
|
-
blocks: readonly FunctionBlock[];
|
|
313
|
-
programs: readonly PlcProgram[];
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
export interface Task {
|
|
317
|
-
name: string;
|
|
318
|
-
intervalNanoseconds: string;
|
|
319
|
-
priority: number;
|
|
320
|
-
programs: readonly string[];
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
export interface Deployment {
|
|
324
|
-
tasks: readonly Task[];
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
export interface Artifact {
|
|
328
|
-
path: string;
|
|
329
|
-
content: string;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
export interface GeneratedObject {
|
|
333
|
-
parent?: SymbolId;
|
|
334
|
-
sourcePath?: string;
|
|
335
|
-
id: SymbolId;
|
|
336
|
-
kind: "program" | "function" | "functionBlock" | "method" | "globalList" | "dataType";
|
|
337
|
-
name: string;
|
|
338
|
-
declaration: string;
|
|
339
|
-
implementation?: string;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
export interface EmitResult {
|
|
343
|
-
artifacts: readonly Artifact[];
|
|
344
|
-
objects: readonly GeneratedObject[];
|
|
345
|
-
diagnostics: readonly Diagnostic[];
|
|
346
|
-
}
|