@tsplc/compiler 0.2.17 → 0.2.18
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/allocation.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import type { PlcModule } from "./model.ts";
|
|
2
|
-
import type { ModuleDeclarations } from "./declarations.ts";
|
|
3
|
-
import { reject } from "./normalization-error.ts";
|
|
4
|
-
import { instancedBlock } from "./types.ts";
|
|
5
|
-
|
|
6
|
-
export function checkStaticAllocation(module: PlcModule, declarations: ModuleDeclarations): void {
|
|
7
|
-
const { types, blocks } = declarations;
|
|
8
|
-
|
|
9
|
-
// Declaration admission closes these registries over every referenced type
|
|
10
|
-
// and block before allocation starts; a missing entry cannot be authored here.
|
|
11
|
-
// Static allocation is a separate graph from scan calls: even an unused child
|
|
12
|
-
// consumes memory, and a recursive child cannot be allocated by value.
|
|
13
|
-
const payloads = new Map<string, number>();
|
|
14
|
-
const payload = (id: string): number => {
|
|
15
|
-
const cached = payloads.get(id);
|
|
16
|
-
if (cached !== undefined) return cached;
|
|
17
|
-
const type = types.get(id)!;
|
|
18
|
-
const size =
|
|
19
|
-
type.kind === "array"
|
|
20
|
-
? type.length * payload(type.element)
|
|
21
|
-
: type.kind === "record"
|
|
22
|
-
? type.fields.reduce((sum, field) => sum + payload(field.type), 0)
|
|
23
|
-
: 1;
|
|
24
|
-
payloads.set(id, size);
|
|
25
|
-
return size;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const instanceSize = (id: string): number => {
|
|
29
|
-
const type = types.get(id)!;
|
|
30
|
-
return type.kind === "array"
|
|
31
|
-
? type.length * instanceSize(type.element)
|
|
32
|
-
: allocation(type.kind === "block" ? type.block : "").size;
|
|
33
|
-
};
|
|
34
|
-
const allocations = new Map<string, { size: number; height: number }>(),
|
|
35
|
-
allocating = new Set<string>();
|
|
36
|
-
const allocation = (id: string): { size: number; height: number } => {
|
|
37
|
-
if (allocating.size >= 64)
|
|
38
|
-
reject("Static block allocation nesting exceeds 64 levels.", blocks.get(id)?.span);
|
|
39
|
-
if (allocating.has(id)) reject("Recursive static block allocation.", blocks.get(id)?.span);
|
|
40
|
-
const cached = allocations.get(id);
|
|
41
|
-
if (cached) return cached;
|
|
42
|
-
allocating.add(id);
|
|
43
|
-
const block = blocks.get(id)!;
|
|
44
|
-
const children = block.state
|
|
45
|
-
.filter((item) => item.storage === "instance")
|
|
46
|
-
.map((item) => ({
|
|
47
|
-
height: allocation(instancedBlock(item.type, types)!.block).height,
|
|
48
|
-
size: instanceSize(item.type),
|
|
49
|
-
}));
|
|
50
|
-
const own = [
|
|
51
|
-
...block.inputs,
|
|
52
|
-
...block.outputs,
|
|
53
|
-
...block.state.filter((item) => item.storage !== "instance"),
|
|
54
|
-
].reduce((sum, item) => sum + (item.mode === "inOut" ? 1 : payload(item.type)), 1);
|
|
55
|
-
const size = own + children.reduce((sum, child) => sum + child.size, 0),
|
|
56
|
-
height = children.length ? 1 + Math.max(...children.map((child) => child.height)) : 1;
|
|
57
|
-
if (size > 1_000_000 || height > 64)
|
|
58
|
-
reject("Static block allocation exceeds its finite budget.", block.span);
|
|
59
|
-
allocating.delete(id);
|
|
60
|
-
const result = { size, height };
|
|
61
|
-
allocations.set(id, result);
|
|
62
|
-
return result;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
for (const block of module.blocks) allocation(block.id);
|
|
66
|
-
const staticPayload = module.variables.reduce((sum, variable) => {
|
|
67
|
-
return (
|
|
68
|
-
sum +
|
|
69
|
-
(instancedBlock(variable.type, types) ? instanceSize(variable.type) : payload(variable.type))
|
|
70
|
-
);
|
|
71
|
-
}, 0);
|
|
72
|
-
if (staticPayload > 1_000_000)
|
|
73
|
-
reject("Static module storage exceeds one million scalar elements.");
|
|
74
|
-
}
|
package/ir/core-model.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import type { SourceSpan } from "./diagnostics.ts";
|
|
2
|
-
import type {
|
|
3
|
-
Variable,
|
|
4
|
-
Constant,
|
|
5
|
-
BinaryOperator,
|
|
6
|
-
FunctionBlock,
|
|
7
|
-
PlcProgram,
|
|
8
|
-
PlcFunction,
|
|
9
|
-
PlcModule,
|
|
10
|
-
MethodOwner,
|
|
11
|
-
TelemetryCall,
|
|
12
|
-
} from "./model.ts";
|
|
13
|
-
import type { PlcType } from "./types.ts";
|
|
14
|
-
|
|
15
|
-
// Core expressions contain no source-level calls. Their effects have already
|
|
16
|
-
// become ordered statements, and every place has passed storage/bounds checks.
|
|
17
|
-
export type CorePath = readonly (
|
|
18
|
-
| { readonly kind: "field"; readonly name: string }
|
|
19
|
-
| { readonly kind: "index"; readonly value: CoreExpression }
|
|
20
|
-
)[];
|
|
21
|
-
export interface CheckedPlace {
|
|
22
|
-
readonly root: string;
|
|
23
|
-
readonly path: CorePath;
|
|
24
|
-
}
|
|
25
|
-
export type CoreExpression = { readonly type: string; readonly span: SourceSpan } & (
|
|
26
|
-
| { readonly kind: "borrow"; readonly place: CheckedPlace }
|
|
27
|
-
| { readonly kind: "constant"; readonly value: Constant }
|
|
28
|
-
| { readonly kind: "load"; readonly root: string; readonly path?: CorePath }
|
|
29
|
-
| {
|
|
30
|
-
readonly kind: "unary";
|
|
31
|
-
readonly operator: "not" | "negate" | "bitNot" | "timeToMilliseconds" | "clock";
|
|
32
|
-
readonly operand: CoreExpression;
|
|
33
|
-
}
|
|
34
|
-
| {
|
|
35
|
-
readonly kind: "binary";
|
|
36
|
-
readonly operator: BinaryOperator;
|
|
37
|
-
readonly left: CoreExpression;
|
|
38
|
-
readonly right: CoreExpression;
|
|
39
|
-
readonly policy?: "wrap" | "saturate";
|
|
40
|
-
}
|
|
41
|
-
| {
|
|
42
|
-
readonly kind: "convert";
|
|
43
|
-
readonly operand: CoreExpression;
|
|
44
|
-
readonly policy: "exact" | "wrap" | "saturate";
|
|
45
|
-
}
|
|
46
|
-
);
|
|
47
|
-
export type CoreStatement = { readonly span: SourceSpan } & (
|
|
48
|
-
| {
|
|
49
|
-
readonly kind: "assign";
|
|
50
|
-
readonly root: string;
|
|
51
|
-
readonly path?: CorePath;
|
|
52
|
-
readonly value: CoreExpression;
|
|
53
|
-
}
|
|
54
|
-
| {
|
|
55
|
-
readonly kind: "if";
|
|
56
|
-
readonly condition: CoreExpression;
|
|
57
|
-
readonly then: readonly CoreStatement[];
|
|
58
|
-
readonly else: readonly CoreStatement[];
|
|
59
|
-
}
|
|
60
|
-
| {
|
|
61
|
-
readonly kind: "switch";
|
|
62
|
-
readonly value: CoreExpression;
|
|
63
|
-
readonly cases: readonly { values: readonly Constant[]; body: readonly CoreStatement[] }[];
|
|
64
|
-
readonly fallback: readonly CoreStatement[];
|
|
65
|
-
}
|
|
66
|
-
| {
|
|
67
|
-
readonly kind: "for";
|
|
68
|
-
readonly counter: string;
|
|
69
|
-
readonly counterScope?: "existing";
|
|
70
|
-
readonly start: Constant;
|
|
71
|
-
readonly limit: Constant;
|
|
72
|
-
readonly step: string;
|
|
73
|
-
readonly dynamic?: {
|
|
74
|
-
readonly start: CoreExpression;
|
|
75
|
-
readonly limit: CoreExpression;
|
|
76
|
-
readonly exclusive?: true;
|
|
77
|
-
};
|
|
78
|
-
readonly maxIterations: number;
|
|
79
|
-
readonly body: readonly CoreStatement[];
|
|
80
|
-
}
|
|
81
|
-
| {
|
|
82
|
-
readonly kind: "invoke";
|
|
83
|
-
readonly function: string;
|
|
84
|
-
readonly instance?: string;
|
|
85
|
-
readonly index?: CoreExpression;
|
|
86
|
-
readonly arguments: readonly CoreExpression[];
|
|
87
|
-
readonly destination?: string;
|
|
88
|
-
}
|
|
89
|
-
| {
|
|
90
|
-
readonly kind: "blockUpdate";
|
|
91
|
-
readonly instance: string;
|
|
92
|
-
readonly index?: CoreExpression;
|
|
93
|
-
readonly inputs: Readonly<Record<string, CoreExpression>>;
|
|
94
|
-
}
|
|
95
|
-
| { readonly kind: "return"; readonly value?: CoreExpression }
|
|
96
|
-
| { readonly kind: "break" | "continue" }
|
|
97
|
-
| ({ readonly kind: "telemetry" } & TelemetryCall<CoreExpression>)
|
|
98
|
-
);
|
|
99
|
-
/** The statement lists nested directly inside a statement. */
|
|
100
|
-
export const nestedBodies = (item: CoreStatement): readonly (readonly CoreStatement[])[] => {
|
|
101
|
-
if (item.kind === "if") return [item.then, item.else];
|
|
102
|
-
if (item.kind === "switch") return [...item.cases.map((branch) => branch.body), item.fallback];
|
|
103
|
-
if (item.kind === "for") return [item.body];
|
|
104
|
-
return [];
|
|
105
|
-
};
|
|
106
|
-
export interface CoreFunction {
|
|
107
|
-
readonly owner?: MethodOwner;
|
|
108
|
-
readonly origin?: PlcFunction["origin"];
|
|
109
|
-
readonly id: string;
|
|
110
|
-
readonly name: string;
|
|
111
|
-
readonly parameters: readonly Variable[];
|
|
112
|
-
readonly result?: string;
|
|
113
|
-
readonly locals: readonly Variable[];
|
|
114
|
-
readonly body: readonly CoreStatement[];
|
|
115
|
-
readonly span: SourceSpan;
|
|
116
|
-
}
|
|
117
|
-
export interface CoreFunctionBlock extends Readonly<Omit<FunctionBlock, "body">> {
|
|
118
|
-
readonly body: readonly CoreStatement[];
|
|
119
|
-
}
|
|
120
|
-
export const verified: unique symbol = Symbol("verified core");
|
|
121
|
-
export interface VerifiedCoreModule {
|
|
122
|
-
readonly [verified]: true;
|
|
123
|
-
readonly folded?: PlcModule["folded"];
|
|
124
|
-
readonly types: readonly PlcType[];
|
|
125
|
-
readonly variables: readonly Variable[];
|
|
126
|
-
readonly functions: readonly CoreFunction[];
|
|
127
|
-
readonly blocks: readonly CoreFunctionBlock[];
|
|
128
|
-
readonly programs: readonly PlcProgram[];
|
|
129
|
-
}
|
package/ir/core.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import type { Diagnostic } from "./diagnostics.ts";
|
|
2
|
-
import type { PlcModule } from "./model.ts";
|
|
3
|
-
import {
|
|
4
|
-
verified,
|
|
5
|
-
type VerifiedCoreModule,
|
|
6
|
-
type CoreFunction,
|
|
7
|
-
type CoreFunctionBlock,
|
|
8
|
-
} from "./core-model.ts";
|
|
9
|
-
import { ModuleDeclarations } from "./declarations.ts";
|
|
10
|
-
import { checkStaticAllocation } from "./allocation.ts";
|
|
11
|
-
import { RoutineNormalizer } from "./routine.ts";
|
|
12
|
-
import { linkModule } from "./link.ts";
|
|
13
|
-
import { InvalidCore } from "./normalization-error.ts";
|
|
14
|
-
|
|
15
|
-
export type {
|
|
16
|
-
CorePath,
|
|
17
|
-
CheckedPlace,
|
|
18
|
-
CoreExpression,
|
|
19
|
-
CoreStatement,
|
|
20
|
-
CoreFunction,
|
|
21
|
-
CoreFunctionBlock,
|
|
22
|
-
VerifiedCoreModule,
|
|
23
|
-
} from "./core-model.ts";
|
|
24
|
-
export { nestedBodies } from "./core-model.ts";
|
|
25
|
-
|
|
26
|
-
const verifiedModules = new WeakSet<object>();
|
|
27
|
-
export function isVerifiedCore(value: unknown): value is VerifiedCoreModule {
|
|
28
|
-
return typeof value === "object" && value !== null && verifiedModules.has(value);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function freeze(value: unknown): void {
|
|
32
|
-
if (typeof value !== "object" || value === null || Object.isFrozen(value)) return;
|
|
33
|
-
for (const child of Object.values(value)) freeze(child);
|
|
34
|
-
Object.freeze(value);
|
|
35
|
-
}
|
|
36
|
-
// The module crosses the trust boundary only after admission, lowering and linking.
|
|
37
|
-
export function normalizeModule(module: PlcModule): {
|
|
38
|
-
core?: VerifiedCoreModule;
|
|
39
|
-
diagnostics: readonly Diagnostic[];
|
|
40
|
-
} {
|
|
41
|
-
try {
|
|
42
|
-
const declarations = new ModuleDeclarations(module);
|
|
43
|
-
checkStaticAllocation(module, declarations);
|
|
44
|
-
|
|
45
|
-
const functions: CoreFunction[] = [],
|
|
46
|
-
blocks: CoreFunctionBlock[] = [];
|
|
47
|
-
const edges = new Map<string, ReadonlySet<string>>();
|
|
48
|
-
for (const source of [...module.functions, ...module.blocks]) {
|
|
49
|
-
const normalized = new RoutineNormalizer(source, declarations).normalize();
|
|
50
|
-
edges.set(source.id, normalized.callees);
|
|
51
|
-
if ("inputs" in normalized.routine) blocks.push(normalized.routine);
|
|
52
|
-
else functions.push(normalized.routine);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const linked = linkModule(module, declarations, functions, blocks, edges);
|
|
56
|
-
const core: VerifiedCoreModule = { ...structuredClone(linked), [verified]: true };
|
|
57
|
-
freeze(core);
|
|
58
|
-
verifiedModules.add(core);
|
|
59
|
-
return { core, diagnostics: [] };
|
|
60
|
-
} catch (cause) {
|
|
61
|
-
if (cause instanceof InvalidCore) return { diagnostics: [cause.diagnostic] };
|
|
62
|
-
throw cause;
|
|
63
|
-
}
|
|
64
|
-
}
|
package/ir/declarations.ts
DELETED
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import type { SourceSpan } from "./diagnostics.ts";
|
|
2
|
-
import {
|
|
3
|
-
standardBlocks,
|
|
4
|
-
type PlcModule,
|
|
5
|
-
type Variable,
|
|
6
|
-
type FunctionBlock,
|
|
7
|
-
type PlcFunction,
|
|
8
|
-
} from "./model.ts";
|
|
9
|
-
import { instancedBlock, type PlcType } from "./types.ts";
|
|
10
|
-
import { layoutIssue, valueFits } from "./aggregates.ts";
|
|
11
|
-
import { reject } from "./normalization-error.ts";
|
|
12
|
-
|
|
13
|
-
// This index owns the module namespace and storage declarations. Routine
|
|
14
|
-
// lowering may claim temporary identifiers, but cannot weaken admission rules.
|
|
15
|
-
export class ModuleDeclarations {
|
|
16
|
-
private readonly ids = new Set<string>();
|
|
17
|
-
readonly types = new Map<string, PlcType>();
|
|
18
|
-
readonly globals = new Map<string, Variable>();
|
|
19
|
-
readonly blocks: Map<string, FunctionBlock>;
|
|
20
|
-
readonly signatures: Map<string, PlcFunction>;
|
|
21
|
-
|
|
22
|
-
constructor(module: PlcModule) {
|
|
23
|
-
for (const type of module.types) {
|
|
24
|
-
if (this.types.has(type.id)) reject(`Duplicate type ${type.id}.`);
|
|
25
|
-
this.types.set(type.id, type);
|
|
26
|
-
}
|
|
27
|
-
const invalidLayout = layoutIssue(this.types);
|
|
28
|
-
if (invalidLayout) reject(invalidLayout);
|
|
29
|
-
if (this.types.get("BOOL")?.kind !== "bool") reject("Missing canonical BOOL type.");
|
|
30
|
-
|
|
31
|
-
this.blocks = new Map(module.blocks.map((block) => [block.id, block]));
|
|
32
|
-
for (const item of module.variables) {
|
|
33
|
-
this.checkVariable(item, ["module", "instance", "input", "output", "constant"]);
|
|
34
|
-
this.globals.set(item.id, item);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
this.signatures = new Map(module.functions.map((fn) => [fn.id, fn]));
|
|
38
|
-
for (const fn of module.functions) {
|
|
39
|
-
this.claimId(fn.id, fn.span);
|
|
40
|
-
if (
|
|
41
|
-
fn.owner &&
|
|
42
|
-
(!this.blocks.has(fn.owner.block) ||
|
|
43
|
-
this.blocks.get(fn.owner.block)?.standard ||
|
|
44
|
-
!["public", "private", "protected"].includes(fn.owner.access))
|
|
45
|
-
)
|
|
46
|
-
reject("Methods require a user block owner and a valid access modifier.", fn.span);
|
|
47
|
-
if (fn.result && !this.isValueType(fn.result))
|
|
48
|
-
reject("Unknown or non-value function result type.", fn.span);
|
|
49
|
-
for (const parameter of fn.parameters) {
|
|
50
|
-
this.checkVariable(parameter, ["parameter"], true);
|
|
51
|
-
if (parameter.mutable && !parameter.mode)
|
|
52
|
-
reject("Value parameters are readonly.", parameter.span);
|
|
53
|
-
}
|
|
54
|
-
for (const local of fn.locals) this.checkVariable(local, ["local"]);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
for (const block of module.blocks) {
|
|
58
|
-
this.claimId(block.id, block.span);
|
|
59
|
-
for (const input of block.inputs) {
|
|
60
|
-
this.checkVariable(input, ["parameter"], true);
|
|
61
|
-
if (!input.mode && input.mutable) reject("Value block inputs are readonly.", input.span);
|
|
62
|
-
}
|
|
63
|
-
for (const output of block.outputs) this.checkVariable(output, ["blockOutput"]);
|
|
64
|
-
for (const state of block.state) this.checkVariable(state, ["field", "instance"]);
|
|
65
|
-
for (const local of block.locals) this.checkVariable(local, ["local"]);
|
|
66
|
-
for (const ports of [block.inputs, block.outputs]) {
|
|
67
|
-
if (new Set(ports.map((variable) => variable.name)).size !== ports.length)
|
|
68
|
-
reject(
|
|
69
|
-
"Block input and output names must each be unique within their schema.",
|
|
70
|
-
block.span,
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
const result = block.result ? this.types.get(block.result) : undefined;
|
|
74
|
-
if (
|
|
75
|
-
block.outputs.length
|
|
76
|
-
? result?.kind !== "record" ||
|
|
77
|
-
!this.isValueType(result.id) ||
|
|
78
|
-
result.fields.length !== block.outputs.length ||
|
|
79
|
-
result.fields.some(
|
|
80
|
-
(field) =>
|
|
81
|
-
!block.outputs.some(
|
|
82
|
-
(output) => output.name === field.name && output.type === field.type,
|
|
83
|
-
),
|
|
84
|
-
)
|
|
85
|
-
: Boolean(block.result)
|
|
86
|
-
)
|
|
87
|
-
reject("Block output schema differs from its result record.", block.span);
|
|
88
|
-
if (block.standard) {
|
|
89
|
-
const expected = Object.hasOwn(standardBlocks, block.standard)
|
|
90
|
-
? standardBlocks[block.standard]
|
|
91
|
-
: undefined;
|
|
92
|
-
const matches = (
|
|
93
|
-
variables: readonly Variable[],
|
|
94
|
-
schema: readonly (readonly [string, string, string])[],
|
|
95
|
-
): boolean =>
|
|
96
|
-
variables.length === schema.length &&
|
|
97
|
-
schema.every(([name, , type]) =>
|
|
98
|
-
variables.some(
|
|
99
|
-
(variable) =>
|
|
100
|
-
variable.name === name &&
|
|
101
|
-
variable.type === type &&
|
|
102
|
-
!variable.mode &&
|
|
103
|
-
!variable.retain,
|
|
104
|
-
),
|
|
105
|
-
);
|
|
106
|
-
if (
|
|
107
|
-
!expected ||
|
|
108
|
-
block.name !== block.standard ||
|
|
109
|
-
block.body.length ||
|
|
110
|
-
block.state.length ||
|
|
111
|
-
block.locals.length ||
|
|
112
|
-
!matches(block.inputs, expected.inputs) ||
|
|
113
|
-
!matches(block.outputs, expected.outputs) ||
|
|
114
|
-
block.outputs.some(({ initial }) => {
|
|
115
|
-
switch (initial?.kind) {
|
|
116
|
-
case "bool":
|
|
117
|
-
return initial.value !== false;
|
|
118
|
-
case "duration":
|
|
119
|
-
return initial.nanoseconds !== "0";
|
|
120
|
-
case "integer":
|
|
121
|
-
return initial.value !== "0";
|
|
122
|
-
default:
|
|
123
|
-
return true;
|
|
124
|
-
}
|
|
125
|
-
})
|
|
126
|
-
)
|
|
127
|
-
reject("Invalid canonical standard block definition.", block.span);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
for (const type of this.types.values())
|
|
132
|
-
if (type.kind === "block") {
|
|
133
|
-
const definition = this.blocks.get(type.block);
|
|
134
|
-
if (!definition || definition.result !== type.output)
|
|
135
|
-
reject("Block type differs from its definition.");
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
hasId(id: string): boolean {
|
|
140
|
-
return this.ids.has(id);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public claimId(id: string, span?: SourceSpan): void {
|
|
144
|
-
if (this.ids.has(id)) reject(`Duplicate symbol ${id}.`, span);
|
|
145
|
-
this.ids.add(id);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
public isValueType(id: string, seen = new Set<string>()): boolean {
|
|
149
|
-
if (seen.has(id)) return true;
|
|
150
|
-
seen.add(id);
|
|
151
|
-
const type = this.types.get(id);
|
|
152
|
-
return Boolean(
|
|
153
|
-
type &&
|
|
154
|
-
type.kind !== "block" &&
|
|
155
|
-
(type.kind !== "record" ||
|
|
156
|
-
type.fields.every((field) => !field.mode && this.isValueType(field.type, seen))) &&
|
|
157
|
-
(type.kind !== "array" || this.isValueType(type.element, seen)),
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private checkVariable(
|
|
162
|
-
item: Variable,
|
|
163
|
-
storage: readonly Variable["storage"][],
|
|
164
|
-
allowBorrow = false,
|
|
165
|
-
): void {
|
|
166
|
-
this.claimId(item.id, item.span);
|
|
167
|
-
const type = this.types.get(item.type);
|
|
168
|
-
if (!type || !storage.includes(item.storage))
|
|
169
|
-
reject("Invalid storage type or class.", item.span);
|
|
170
|
-
if (
|
|
171
|
-
item.mode &&
|
|
172
|
-
(item.mode !== "inOut" || !allowBorrow || !item.mutable || item.storage !== "parameter")
|
|
173
|
-
)
|
|
174
|
-
reject("InOut belongs only on mutable parameters.", item.span);
|
|
175
|
-
const mutableState =
|
|
176
|
-
item.mutable ||
|
|
177
|
-
type.kind === "array" ||
|
|
178
|
-
(type.kind === "record" && type.fields.some((field) => !field.readonly));
|
|
179
|
-
if (
|
|
180
|
-
item.retain &&
|
|
181
|
-
!(item.storage === "instance" || (["module", "field"].includes(item.storage) && mutableState))
|
|
182
|
-
)
|
|
183
|
-
reject("Only mutable static state or block instances may retain.", item.span);
|
|
184
|
-
if (item.storage === "instance") {
|
|
185
|
-
const instanced = instancedBlock(item.type, this.types);
|
|
186
|
-
if (!instanced || !this.blocks.has(instanced.block) || item.initial || item.mode)
|
|
187
|
-
reject(
|
|
188
|
-
"Instances require a known static block and cannot have value initializers.",
|
|
189
|
-
item.span,
|
|
190
|
-
);
|
|
191
|
-
} else if (
|
|
192
|
-
!this.isValueType(item.type) &&
|
|
193
|
-
!(allowBorrow && item.mode === "inOut" && type.kind === "block")
|
|
194
|
-
)
|
|
195
|
-
reject("Block instances and reference schemas cannot be stored as values.", item.span);
|
|
196
|
-
if (item.storage === "parameter" && item.initial)
|
|
197
|
-
reject(
|
|
198
|
-
"Inputs are supplied at each invocation and cannot have stored initializers.",
|
|
199
|
-
item.span,
|
|
200
|
-
);
|
|
201
|
-
if (item.initial && !valueFits(item.initial, type, this.types))
|
|
202
|
-
reject("Initializer has the wrong type or is out of range.", item.span);
|
|
203
|
-
if (["module", "field", "constant", "blockOutput"].includes(item.storage) && !item.initial)
|
|
204
|
-
reject("Persistent storage requires initialization.", item.span);
|
|
205
|
-
if (["input", "constant", "blockOutput"].includes(item.storage) && item.mutable)
|
|
206
|
-
reject("Readonly storage marked mutable.", item.span);
|
|
207
|
-
}
|
|
208
|
-
}
|
package/ir/diagnostics.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
export interface SourceSpan {
|
|
2
|
-
file: string;
|
|
3
|
-
start: number;
|
|
4
|
-
end: number;
|
|
5
|
-
line: number;
|
|
6
|
-
column: number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface Diagnostic {
|
|
10
|
-
origin: "typescript" | "plc" | "config" | "backend" | "vendor";
|
|
11
|
-
code: string;
|
|
12
|
-
severity: "error" | "warning";
|
|
13
|
-
message: string;
|
|
14
|
-
span?: SourceSpan;
|
|
15
|
-
notes?: readonly string[];
|
|
16
|
-
related?: readonly { span: SourceSpan; message: string }[];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function error(code: string, message: string, span?: SourceSpan): Diagnostic {
|
|
20
|
-
return { origin: "plc", code, severity: "error", message, ...(span && { span }) };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function hasErrors(diagnostics: readonly Diagnostic[]): boolean {
|
|
24
|
-
return diagnostics.some((diagnostic) => diagnostic.severity === "error");
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function formatDiagnostic(diagnostic: Diagnostic, source?: string): string {
|
|
28
|
-
const { span } = diagnostic;
|
|
29
|
-
const location = span ? `${span.file}:${span.line}:${span.column}: ` : "";
|
|
30
|
-
let result = `${location}${diagnostic.code}: ${diagnostic.message}`;
|
|
31
|
-
if (source !== undefined && span) {
|
|
32
|
-
const line = source.split(/\r?\n/)[span.line - 1];
|
|
33
|
-
if (line !== undefined) {
|
|
34
|
-
const width = Math.max(1, Math.min(span.end - span.start, line.length - span.column + 2));
|
|
35
|
-
result += `\n${span.line} | ${line}\n${" ".repeat(String(span.line).length + 3 + span.column - 1)}${"^".repeat(width)}`;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
for (const related of diagnostic.related ?? []) {
|
|
39
|
-
result += `\n ${related.span.file}:${related.span.line}:${related.span.column}: ${related.message}`;
|
|
40
|
-
}
|
|
41
|
-
for (const note of diagnostic.notes ?? []) result += `\n ${note}`;
|
|
42
|
-
return result;
|
|
43
|
-
}
|
package/ir/domains.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { comparisonOperators, type Constant, type Expression } from "./model.ts";
|
|
2
|
-
import type { PlcType } from "./types.ts";
|
|
3
|
-
|
|
4
|
-
export function durationLiteral(text: string, scaleNanoseconds: bigint): Constant | undefined {
|
|
5
|
-
const clean = text.replaceAll("_", "").replace(/^(-?)\./, (_, sign: string) => `${sign}0.`);
|
|
6
|
-
const match = /^(-?)(\d+)(?:\.(\d*))?(?:e([+-]?\d+))?$/i.exec(clean);
|
|
7
|
-
if (!match) return undefined;
|
|
8
|
-
const fraction = match[3] ?? "",
|
|
9
|
-
exponent = Number(match[4] ?? 0) - fraction.length;
|
|
10
|
-
if (!Number.isSafeInteger(exponent) || Math.abs(exponent) > 400) return undefined;
|
|
11
|
-
let value = BigInt(match[2]! + fraction) * scaleNanoseconds;
|
|
12
|
-
if (exponent >= 0) value *= 10n ** BigInt(exponent);
|
|
13
|
-
else {
|
|
14
|
-
const divisor = 10n ** BigInt(-exponent);
|
|
15
|
-
if (value % divisor) return undefined;
|
|
16
|
-
value /= divisor;
|
|
17
|
-
}
|
|
18
|
-
if (match[1]) value = -value;
|
|
19
|
-
return validDuration(value) ? { kind: "duration", nanoseconds: String(value) } : undefined;
|
|
20
|
-
}
|
|
21
|
-
// TIME stores unsigned 32-bit milliseconds.
|
|
22
|
-
function validDuration(nanoseconds: bigint): boolean {
|
|
23
|
-
return (
|
|
24
|
-
nanoseconds >= 0n && nanoseconds <= 4_294_967_295_000_000n && nanoseconds % 1_000_000n === 0n
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
function validDate(text: string): boolean {
|
|
28
|
-
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
|
|
29
|
-
if (!match) return false;
|
|
30
|
-
const year = Number(match[1]),
|
|
31
|
-
month = Number(match[2]),
|
|
32
|
-
day = Number(match[3]);
|
|
33
|
-
const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
34
|
-
const length = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
|
|
35
|
-
return Boolean(
|
|
36
|
-
length && day >= 1 && day <= length && text >= "1970-01-01" && text <= "2106-02-07",
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
function validClock(text: string, milliseconds: boolean): boolean {
|
|
40
|
-
const match = (
|
|
41
|
-
milliseconds ? /^(\d{2}):(\d{2}):(\d{2})\.(\d{3})$/ : /^(\d{2}):(\d{2}):(\d{2})$/
|
|
42
|
-
).exec(text);
|
|
43
|
-
return Boolean(match && Number(match[1]) < 24 && Number(match[2]) < 60 && Number(match[3]) < 60);
|
|
44
|
-
}
|
|
45
|
-
export function calendarLiteral(
|
|
46
|
-
domain: Extract<PlcType, { kind: "calendar" }>["domain"],
|
|
47
|
-
text: string,
|
|
48
|
-
): Constant | undefined {
|
|
49
|
-
if (domain === "date") return validDate(text) ? { kind: "calendar", value: text } : undefined;
|
|
50
|
-
if (domain === "timeOfDay") {
|
|
51
|
-
const canonical = /^\d{2}:\d{2}:\d{2}$/.test(text) ? `${text}.000` : text;
|
|
52
|
-
return validClock(canonical, true) ? { kind: "calendar", value: canonical } : undefined;
|
|
53
|
-
}
|
|
54
|
-
return text.length === 19 &&
|
|
55
|
-
text[10] === "T" &&
|
|
56
|
-
validDate(text.slice(0, 10)) &&
|
|
57
|
-
validClock(text.slice(11), false) &&
|
|
58
|
-
text <= "2106-02-07T06:28:15"
|
|
59
|
-
? { kind: "calendar", value: text }
|
|
60
|
-
: undefined;
|
|
61
|
-
}
|
|
62
|
-
export function domainConstantFits(value: Constant, type: PlcType): boolean {
|
|
63
|
-
if (type.kind === "duration")
|
|
64
|
-
return (
|
|
65
|
-
value.kind === "duration" &&
|
|
66
|
-
/^(?:0|[1-9][0-9]*)$/.test(value.nanoseconds) &&
|
|
67
|
-
validDuration(BigInt(value.nanoseconds))
|
|
68
|
-
);
|
|
69
|
-
if (type.kind === "calendar" && value.kind === "calendar") {
|
|
70
|
-
const parsed = calendarLiteral(type.domain, value.value);
|
|
71
|
-
return parsed?.kind === "calendar" && parsed.value === value.value;
|
|
72
|
-
}
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
export function domainIssue(
|
|
76
|
-
value: Expression,
|
|
77
|
-
types: ReadonlyMap<string, PlcType>,
|
|
78
|
-
): string | undefined {
|
|
79
|
-
const type = types.get(value.type);
|
|
80
|
-
if (value.kind === "constant")
|
|
81
|
-
return type && domainConstantFits(value.value, type)
|
|
82
|
-
? undefined
|
|
83
|
-
: "Invalid TIME/calendar constant.";
|
|
84
|
-
if (value.kind === "load" || value.kind === "call" || value.kind === "conditional")
|
|
85
|
-
return undefined;
|
|
86
|
-
if (value.kind === "binary" && comparisonOperators.has(value.operator)) {
|
|
87
|
-
const left = types.get(value.left.type);
|
|
88
|
-
return value.type === "BOOL" &&
|
|
89
|
-
left &&
|
|
90
|
-
["duration", "calendar"].includes(left.kind) &&
|
|
91
|
-
value.left.type === value.right.type
|
|
92
|
-
? undefined
|
|
93
|
-
: "Time comparisons require matching domains.";
|
|
94
|
-
}
|
|
95
|
-
return "TIME and calendar domains permit storage, typed copies and comparisons; implicit numeric arithmetic is unsupported.";
|
|
96
|
-
}
|
package/ir/index.ts
DELETED