@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,149 @@
|
|
|
1
|
+
import {
|
|
2
|
+
coreTables,
|
|
3
|
+
isLogicalIo,
|
|
4
|
+
type PlcType,
|
|
5
|
+
type Variable,
|
|
6
|
+
type VerifiedCoreModule,
|
|
7
|
+
} from "@tsplc/compiler/ir";
|
|
8
|
+
import { arrayTypeText } from "@tsplc/iec";
|
|
9
|
+
import { type IoTarget, ioLayout } from "@tsplc/typegen/hardware";
|
|
10
|
+
import { moduleName, storagePath } from "./storage";
|
|
11
|
+
|
|
12
|
+
type StorageSegment =
|
|
13
|
+
| { kind: "field"; record: string; name: string }
|
|
14
|
+
| { kind: "index"; value: number };
|
|
15
|
+
|
|
16
|
+
/** An `io` key that names a field of authored module storage, e.g. `PlantIO.Motor.Output.Run`. */
|
|
17
|
+
export interface StorageBinding {
|
|
18
|
+
key: string;
|
|
19
|
+
root: Variable;
|
|
20
|
+
segments: readonly StorageSegment[];
|
|
21
|
+
type: PlcType;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const pathPattern =
|
|
25
|
+
/^([\w$-]+)\.([A-Za-z_$][\w$]*)((?:\.[A-Za-z_$][\w$]*|\[-?\d+\])*)$/;
|
|
26
|
+
|
|
27
|
+
export const isStoragePath = (key: string): boolean =>
|
|
28
|
+
key.includes(".") || key.includes("[");
|
|
29
|
+
|
|
30
|
+
/** Resolve a storage path against verified types; the error explains the first unresolvable segment. */
|
|
31
|
+
export function resolveStoragePath(
|
|
32
|
+
core: VerifiedCoreModule,
|
|
33
|
+
key: string,
|
|
34
|
+
): StorageBinding | { error: string } {
|
|
35
|
+
const match = pathPattern.exec(key);
|
|
36
|
+
if (!match) {
|
|
37
|
+
return {
|
|
38
|
+
error: `${key}: expected Module.variable followed by .field or [index].`,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const [, module, name, rest = ""] = match;
|
|
42
|
+
const roots = core.variables.filter(
|
|
43
|
+
(item) =>
|
|
44
|
+
item.storage === "module" &&
|
|
45
|
+
item.name === name &&
|
|
46
|
+
moduleName(item) === module,
|
|
47
|
+
);
|
|
48
|
+
const [root] = roots;
|
|
49
|
+
if (!root || roots.length !== 1) {
|
|
50
|
+
return {
|
|
51
|
+
error: root
|
|
52
|
+
? `${key}: ${module}.${name} is declared by more than one module named ${module}.`
|
|
53
|
+
: `${key}: no mutable module variable ${module}.${name}.`,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const { typeOf } = coreTables(core);
|
|
57
|
+
let type = typeOf(root.type);
|
|
58
|
+
const segments: StorageSegment[] = [];
|
|
59
|
+
for (const [, field, index] of rest.matchAll(
|
|
60
|
+
/\.([A-Za-z_$][\w$]*)|\[(-?\d+)\]/g,
|
|
61
|
+
)) {
|
|
62
|
+
if (field !== undefined) {
|
|
63
|
+
const member =
|
|
64
|
+
type.kind === "record"
|
|
65
|
+
? type.fields.find((f) => f.name === field)
|
|
66
|
+
: undefined;
|
|
67
|
+
if (!member) {
|
|
68
|
+
return { error: `${key}: no field ${field}.` };
|
|
69
|
+
}
|
|
70
|
+
segments.push({ kind: "field", record: type.id, name: field });
|
|
71
|
+
type = typeOf(member.type);
|
|
72
|
+
} else {
|
|
73
|
+
const value = Number(index);
|
|
74
|
+
if (type.kind !== "array") {
|
|
75
|
+
return { error: `${key}: ${index} indexes a non-array value.` };
|
|
76
|
+
}
|
|
77
|
+
if (value < type.lowerBound || value >= type.lowerBound + type.length) {
|
|
78
|
+
return {
|
|
79
|
+
error: `${key}: index ${value} is outside ${type.lowerBound}..${type.lowerBound + type.length - 1}.`,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
segments.push({ kind: "index", value });
|
|
83
|
+
type = typeOf(type.element);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return { key, root, segments, type };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** IEC spelling of a bindable type, as typegen spells channels; undefined for records, enums, strings. */
|
|
90
|
+
export function iecSpelling(
|
|
91
|
+
type: PlcType,
|
|
92
|
+
types: ReadonlyMap<string, PlcType>,
|
|
93
|
+
): string | undefined {
|
|
94
|
+
if (
|
|
95
|
+
type.kind === "bool" ||
|
|
96
|
+
type.kind === "integer" ||
|
|
97
|
+
type.kind === "bits" ||
|
|
98
|
+
type.kind === "float" ||
|
|
99
|
+
type.kind === "duration" ||
|
|
100
|
+
type.kind === "calendar"
|
|
101
|
+
) {
|
|
102
|
+
return type.id;
|
|
103
|
+
}
|
|
104
|
+
if (type.kind !== "array") {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
const element = types.get(type.element);
|
|
108
|
+
const spelling = element && iecSpelling(element, types);
|
|
109
|
+
return spelling && element.kind !== "array"
|
|
110
|
+
? arrayTypeText({
|
|
111
|
+
lower: type.lowerBound,
|
|
112
|
+
upper: type.lowerBound + type.length - 1,
|
|
113
|
+
element: spelling,
|
|
114
|
+
})
|
|
115
|
+
: undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Every key an `io` mapping may use: logical IO names and paths into mutable module storage. */
|
|
119
|
+
export function ioTargets(core: VerifiedCoreModule): IoTarget[] {
|
|
120
|
+
const { types, typeOf } = coreTables(core);
|
|
121
|
+
const targets: IoTarget[] = [];
|
|
122
|
+
const add = (key: string, type: PlcType) => {
|
|
123
|
+
const spelling = iecSpelling(type, types);
|
|
124
|
+
const layout = spelling && ioLayout(spelling);
|
|
125
|
+
if (layout) {
|
|
126
|
+
targets.push({ key, layout });
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const visit = (key: string, type: PlcType): void => {
|
|
130
|
+
add(key, type);
|
|
131
|
+
if (type.kind === "record") {
|
|
132
|
+
for (const field of type.fields) {
|
|
133
|
+
visit(`${key}.${field.name}`, typeOf(field.type));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Typegen writes keys as template literal types, so any index matches here.
|
|
137
|
+
if (type.kind === "array") {
|
|
138
|
+
visit(`${key}[\${number}]`, typeOf(type.element));
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
for (const variable of core.variables) {
|
|
142
|
+
if (isLogicalIo(variable)) {
|
|
143
|
+
add(variable.name, typeOf(variable.type));
|
|
144
|
+
} else if (variable.storage === "module") {
|
|
145
|
+
visit(storagePath(variable), typeOf(variable.type));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return targets;
|
|
149
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { CoreStatement } from "@tsplc/compiler/ir";
|
|
2
|
+
import { expressionText, storageName } from "./expressions";
|
|
3
|
+
import type { RoutineWriter } from "./statements";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The ST that counts a range's iterations from `first` to `last`, both of type `wide`: `condition`
|
|
7
|
+
* holds when the range runs at all, and `count` is then the number of iterations as a DINT.
|
|
8
|
+
*/
|
|
9
|
+
export const iterationCount = ({
|
|
10
|
+
first,
|
|
11
|
+
last,
|
|
12
|
+
ascending,
|
|
13
|
+
exclusive,
|
|
14
|
+
step,
|
|
15
|
+
wide,
|
|
16
|
+
}: {
|
|
17
|
+
first: string;
|
|
18
|
+
last: string;
|
|
19
|
+
ascending: boolean;
|
|
20
|
+
exclusive: boolean;
|
|
21
|
+
/** The step's magnitude. */
|
|
22
|
+
step: bigint;
|
|
23
|
+
wide: "LINT" | "ULINT";
|
|
24
|
+
}) => {
|
|
25
|
+
const distance = ascending ? `${last} - ${first}` : `${first} - ${last}`;
|
|
26
|
+
const excluded = exclusive ? ` - ${wide}#1` : "";
|
|
27
|
+
return {
|
|
28
|
+
condition: `${first} ${ascending ? "<" : ">"}${exclusive ? "" : "="} ${last}`,
|
|
29
|
+
count: `${wide}_TO_DINT(((${distance}${excluded}) / ${wide}#${step}) + ${wide}#1)`,
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A counted FOR loop. It iterates a DINT ordinal rather than the authored counter, which would
|
|
35
|
+
* overflow at a width boundary, and reconstructs the counter's typed value each scan.
|
|
36
|
+
*/
|
|
37
|
+
export function writeLoop(
|
|
38
|
+
routine: RoutineWriter,
|
|
39
|
+
item: Extract<CoreStatement, { kind: "for" }>,
|
|
40
|
+
depth: number,
|
|
41
|
+
writeBody: (items: readonly CoreStatement[], depth: number) => void,
|
|
42
|
+
): void {
|
|
43
|
+
const { storage, write, locals, declare } = routine;
|
|
44
|
+
const { targetTypes } = routine.context.symbols;
|
|
45
|
+
const counterType = storage.variableOf(item.counter).type;
|
|
46
|
+
if (item.start.kind !== "integer") {
|
|
47
|
+
throw new Error("Invalid verified loop");
|
|
48
|
+
}
|
|
49
|
+
const { dynamic } = item;
|
|
50
|
+
if (!item.maxIterations) {
|
|
51
|
+
if (item.counterScope === "existing") {
|
|
52
|
+
write(
|
|
53
|
+
`${storageName(storage, item.counter)} := ${dynamic ? expressionText(storage, dynamic.start) : targetTypes.literal(item.start, counterType)};`,
|
|
54
|
+
depth,
|
|
55
|
+
item.span,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const ordinal = locals.allocate("iteration");
|
|
61
|
+
declare(` ${ordinal} : DINT;`, item.span);
|
|
62
|
+
const wide = counterType === "ULINT" ? "ULINT" : "LINT";
|
|
63
|
+
const ascending = BigInt(item.step) > 0n;
|
|
64
|
+
const step = ascending ? BigInt(item.step) : -BigInt(item.step);
|
|
65
|
+
const first = dynamic
|
|
66
|
+
? `TO_${wide}(${expressionText(storage, dynamic.start)})`
|
|
67
|
+
: `${wide}#${item.start.value}`;
|
|
68
|
+
const countIterations = ({
|
|
69
|
+
limit,
|
|
70
|
+
exclusive,
|
|
71
|
+
}: NonNullable<typeof dynamic>): string => {
|
|
72
|
+
const counted = iterationCount({
|
|
73
|
+
first,
|
|
74
|
+
last: `TO_${wide}(${expressionText(storage, limit)})`,
|
|
75
|
+
ascending,
|
|
76
|
+
exclusive: exclusive === true,
|
|
77
|
+
step,
|
|
78
|
+
wide,
|
|
79
|
+
});
|
|
80
|
+
const count = locals.allocate("iterationCount");
|
|
81
|
+
declare(` ${count} : DINT;`, item.span);
|
|
82
|
+
write(`${count} := DINT#0;`, depth, item.span);
|
|
83
|
+
write(`IF ${counted.condition} THEN`, depth, item.span);
|
|
84
|
+
write(`${count} := ${counted.count};`, depth + 1, item.span);
|
|
85
|
+
write("END_IF;", depth, item.span);
|
|
86
|
+
return count;
|
|
87
|
+
};
|
|
88
|
+
const lastOrdinal = dynamic
|
|
89
|
+
? `${countIterations(dynamic)} - DINT#1`
|
|
90
|
+
: `DINT#${item.maxIterations - 1}`;
|
|
91
|
+
write(`FOR ${ordinal} := DINT#0 TO ${lastOrdinal} DO`, depth, item.span);
|
|
92
|
+
const counterValue = `${storageName(storage, item.counter)} := TO_${counterType}(${first} ${ascending ? "+" : "-"} (${wide}#${step} * DINT_TO_${wide}(${ordinal})));`;
|
|
93
|
+
write(counterValue, depth + 1, item.span);
|
|
94
|
+
writeBody(item.body, depth + 1);
|
|
95
|
+
write("END_FOR;", depth, item.span);
|
|
96
|
+
// CODESYS keeps the ordinal at EXIT and advances it on normal
|
|
97
|
+
// completion/CONTINUE. Reconstructing here preserves that distinction.
|
|
98
|
+
if (item.counterScope === "existing") {
|
|
99
|
+
write(counterValue, depth, item.span);
|
|
100
|
+
}
|
|
101
|
+
}
|