@tsplc/tmp-compiler 0.0.0-stage → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6558 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16076 -0
- package/dist/index.js.map +61 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1078 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1336 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2171 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +609 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +651 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {
|
|
2
|
+
latestCalendarSecond,
|
|
3
|
+
latestTimeNanoseconds,
|
|
4
|
+
nanosecondsPerMillisecond,
|
|
5
|
+
} from "@tsplc/iec";
|
|
6
|
+
import { isComparison } from "@tsplc/iec/arithmetic";
|
|
7
|
+
import type { Constant, Expression } from "./model";
|
|
8
|
+
import { canonicalInteger, exactDecimal } from "./numeric";
|
|
9
|
+
import type { PlcType } from "./types";
|
|
10
|
+
|
|
11
|
+
const latestDateTime = new Date(Number(latestCalendarSecond) * 1000)
|
|
12
|
+
.toISOString()
|
|
13
|
+
.slice(0, "YYYY-MM-DDTHH:mm:ss".length);
|
|
14
|
+
const latestDate = latestDateTime.slice(0, "YYYY-MM-DD".length);
|
|
15
|
+
|
|
16
|
+
/** Each calendar domain's zero value, which is also its earliest. */
|
|
17
|
+
export const calendarZero: Record<
|
|
18
|
+
Extract<PlcType, { kind: "calendar" }>["domain"],
|
|
19
|
+
string
|
|
20
|
+
> = {
|
|
21
|
+
date: "1970-01-01",
|
|
22
|
+
timeOfDay: "00:00:00.000",
|
|
23
|
+
dateTime: "1970-01-01T00:00:00",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function durationLiteral(
|
|
27
|
+
text: string,
|
|
28
|
+
scaleNanoseconds: bigint,
|
|
29
|
+
): Constant | undefined {
|
|
30
|
+
const clean = text
|
|
31
|
+
.replaceAll("_", "")
|
|
32
|
+
.replace(/^(-?)\./, (_, sign: string) => `${sign}0.`);
|
|
33
|
+
const value = exactDecimal(clean, scaleNanoseconds);
|
|
34
|
+
return typeof value === "bigint" && validDuration(value)
|
|
35
|
+
? { kind: "duration", nanoseconds: String(value) }
|
|
36
|
+
: undefined;
|
|
37
|
+
}
|
|
38
|
+
function validDuration(nanoseconds: bigint): boolean {
|
|
39
|
+
return (
|
|
40
|
+
nanoseconds >= 0n &&
|
|
41
|
+
nanoseconds <= latestTimeNanoseconds &&
|
|
42
|
+
nanoseconds % nanosecondsPerMillisecond === 0n
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
function validDate(text: string): boolean {
|
|
46
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
|
|
47
|
+
if (!match) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
const year = Number(match[1]);
|
|
51
|
+
const month = Number(match[2]);
|
|
52
|
+
const day = Number(match[3]);
|
|
53
|
+
const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
54
|
+
const length = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][
|
|
55
|
+
month - 1
|
|
56
|
+
];
|
|
57
|
+
return Boolean(
|
|
58
|
+
length &&
|
|
59
|
+
day >= 1 &&
|
|
60
|
+
day <= length &&
|
|
61
|
+
text >= calendarZero.date &&
|
|
62
|
+
text <= latestDate,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
function validClock(text: string, milliseconds: boolean): boolean {
|
|
66
|
+
const match = (
|
|
67
|
+
milliseconds
|
|
68
|
+
? /^(\d{2}):(\d{2}):(\d{2})\.(\d{3})$/
|
|
69
|
+
: /^(\d{2}):(\d{2}):(\d{2})$/
|
|
70
|
+
).exec(text);
|
|
71
|
+
return Boolean(
|
|
72
|
+
match &&
|
|
73
|
+
Number(match[1]) < 24 &&
|
|
74
|
+
Number(match[2]) < 60 &&
|
|
75
|
+
Number(match[3]) < 60,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
export function calendarLiteral(
|
|
79
|
+
domain: Extract<PlcType, { kind: "calendar" }>["domain"],
|
|
80
|
+
text: string,
|
|
81
|
+
): Constant | undefined {
|
|
82
|
+
if (domain === "date") {
|
|
83
|
+
return validDate(text) ? { kind: "calendar", value: text } : undefined;
|
|
84
|
+
}
|
|
85
|
+
if (domain === "timeOfDay") {
|
|
86
|
+
const canonical = /^\d{2}:\d{2}:\d{2}$/.test(text) ? `${text}.000` : text;
|
|
87
|
+
return validClock(canonical, true)
|
|
88
|
+
? { kind: "calendar", value: canonical }
|
|
89
|
+
: undefined;
|
|
90
|
+
}
|
|
91
|
+
return text.length === 19 &&
|
|
92
|
+
text[10] === "T" &&
|
|
93
|
+
validDate(text.slice(0, 10)) &&
|
|
94
|
+
validClock(text.slice(11), false) &&
|
|
95
|
+
text <= latestDateTime
|
|
96
|
+
? { kind: "calendar", value: text }
|
|
97
|
+
: undefined;
|
|
98
|
+
}
|
|
99
|
+
export function domainConstantFits(value: Constant, type: PlcType): boolean {
|
|
100
|
+
if (type.kind === "duration") {
|
|
101
|
+
return (
|
|
102
|
+
value.kind === "duration" &&
|
|
103
|
+
canonicalInteger.test(value.nanoseconds) &&
|
|
104
|
+
validDuration(BigInt(value.nanoseconds))
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
if (type.kind === "calendar" && value.kind === "calendar") {
|
|
108
|
+
const parsed = calendarLiteral(type.domain, value.value);
|
|
109
|
+
return parsed?.kind === "calendar" && parsed.value === value.value;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
export function domainIssue(
|
|
114
|
+
value: Expression,
|
|
115
|
+
types: ReadonlyMap<string, PlcType>,
|
|
116
|
+
): string | undefined {
|
|
117
|
+
const type = types.get(value.type);
|
|
118
|
+
if (value.kind === "constant") {
|
|
119
|
+
return type && domainConstantFits(value.value, type)
|
|
120
|
+
? undefined
|
|
121
|
+
: "Invalid TIME/calendar constant.";
|
|
122
|
+
}
|
|
123
|
+
if (
|
|
124
|
+
value.kind === "load" ||
|
|
125
|
+
value.kind === "call" ||
|
|
126
|
+
value.kind === "conditional"
|
|
127
|
+
) {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
if (value.kind === "binary" && isComparison(value.operator)) {
|
|
131
|
+
return comparedType(value, types, ["duration", "calendar"])
|
|
132
|
+
? undefined
|
|
133
|
+
: "Time comparisons require matching domains.";
|
|
134
|
+
}
|
|
135
|
+
return "TIME and calendar domains permit storage, typed copies and comparisons; implicit numeric arithmetic is unsupported.";
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The operands' type of a BOOL comparison of two operands of one type, when that type is one of `kinds`. */
|
|
139
|
+
export function comparedType(
|
|
140
|
+
value: Extract<Expression, { kind: "binary" }>,
|
|
141
|
+
types: ReadonlyMap<string, PlcType>,
|
|
142
|
+
kinds: readonly PlcType["kind"][],
|
|
143
|
+
): PlcType | undefined {
|
|
144
|
+
const left = types.get(value.left.type);
|
|
145
|
+
return value.type === "BOOL" &&
|
|
146
|
+
left &&
|
|
147
|
+
kinds.includes(left.kind) &&
|
|
148
|
+
value.left.type === value.right.type
|
|
149
|
+
? left
|
|
150
|
+
: undefined;
|
|
151
|
+
}
|
package/src/ir/fold.ts
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyIntegerPolicy,
|
|
3
|
+
booleanOperation,
|
|
4
|
+
compare,
|
|
5
|
+
floatArithmetic,
|
|
6
|
+
floatUnary,
|
|
7
|
+
integerArithmetic,
|
|
8
|
+
integerUnary,
|
|
9
|
+
roundFloat,
|
|
10
|
+
saturateFloat,
|
|
11
|
+
saturateFloatToInteger,
|
|
12
|
+
} from "@tsplc/iec/arithmetic";
|
|
13
|
+
import {
|
|
14
|
+
type Constant,
|
|
15
|
+
type Expression,
|
|
16
|
+
isLogicalIo,
|
|
17
|
+
type Variable,
|
|
18
|
+
} from "./model";
|
|
19
|
+
import { integerType, type PlcType } from "./types";
|
|
20
|
+
|
|
21
|
+
type Types = ReadonlyMap<string, PlcType>;
|
|
22
|
+
|
|
23
|
+
export const fold = (
|
|
24
|
+
expression: Expression,
|
|
25
|
+
variables: ReadonlyMap<string, Variable>,
|
|
26
|
+
types: Types,
|
|
27
|
+
): Constant | undefined => {
|
|
28
|
+
switch (expression.kind) {
|
|
29
|
+
case "constant":
|
|
30
|
+
return expression.value;
|
|
31
|
+
case "copy":
|
|
32
|
+
return fold(expression.operand, variables, types);
|
|
33
|
+
case "length":
|
|
34
|
+
return foldLength(expression, variables, types);
|
|
35
|
+
case "record":
|
|
36
|
+
return foldRecord(expression, variables, types);
|
|
37
|
+
case "array": {
|
|
38
|
+
const items = expression.items.map((item) =>
|
|
39
|
+
fold(item, variables, types),
|
|
40
|
+
);
|
|
41
|
+
return items.every((item): item is Constant => item !== undefined)
|
|
42
|
+
? { kind: "array", items }
|
|
43
|
+
: undefined;
|
|
44
|
+
}
|
|
45
|
+
case "load": {
|
|
46
|
+
if (expression.place.path.length) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const variable = variables.get(expression.place.root);
|
|
50
|
+
return variable?.storage === "constant" ? variable.initial : undefined;
|
|
51
|
+
}
|
|
52
|
+
case "convert":
|
|
53
|
+
return foldConvert(
|
|
54
|
+
expression,
|
|
55
|
+
fold(expression.operand, variables, types),
|
|
56
|
+
types.get(expression.type),
|
|
57
|
+
);
|
|
58
|
+
case "unary":
|
|
59
|
+
return foldUnary(
|
|
60
|
+
expression,
|
|
61
|
+
fold(expression.operand, variables, types),
|
|
62
|
+
types.get(expression.type),
|
|
63
|
+
);
|
|
64
|
+
case "binary": {
|
|
65
|
+
const left = fold(expression.left, variables, types);
|
|
66
|
+
const right = fold(expression.right, variables, types);
|
|
67
|
+
return left && right
|
|
68
|
+
? foldBinary(expression, left, right, types.get(expression.type))
|
|
69
|
+
: undefined;
|
|
70
|
+
}
|
|
71
|
+
default:
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const foldLength = (
|
|
77
|
+
expression: Extract<Expression, { kind: "length" }>,
|
|
78
|
+
variables: ReadonlyMap<string, Variable>,
|
|
79
|
+
types: Types,
|
|
80
|
+
): Constant | undefined => {
|
|
81
|
+
const type = types.get(expression.operand.type);
|
|
82
|
+
const receiver =
|
|
83
|
+
expression.operand.kind === "load"
|
|
84
|
+
? variables.get(expression.operand.place.root)
|
|
85
|
+
: undefined;
|
|
86
|
+
const pure =
|
|
87
|
+
expression.operand.kind === "load" &&
|
|
88
|
+
expression.operand.place.path.every(
|
|
89
|
+
(segment) => segment.kind === "field",
|
|
90
|
+
) &&
|
|
91
|
+
Boolean(receiver && (receiver.initial || isLogicalIo(receiver)));
|
|
92
|
+
if (
|
|
93
|
+
type?.kind === "array" &&
|
|
94
|
+
(pure || fold(expression.operand, variables, types))
|
|
95
|
+
) {
|
|
96
|
+
return { kind: "integer", value: String(type.length) };
|
|
97
|
+
}
|
|
98
|
+
return undefined;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const foldRecord = (
|
|
102
|
+
expression: Extract<Expression, { kind: "record" }>,
|
|
103
|
+
variables: ReadonlyMap<string, Variable>,
|
|
104
|
+
types: Types,
|
|
105
|
+
): Constant | undefined => {
|
|
106
|
+
const fields: Record<string, Constant> = Object.create(null);
|
|
107
|
+
for (const [name, operand] of Object.entries(expression.fields)) {
|
|
108
|
+
const value = fold(operand, variables, types);
|
|
109
|
+
if (!value) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
fields[name] = value;
|
|
113
|
+
}
|
|
114
|
+
return { kind: "record", fields };
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const foldConvert = (
|
|
118
|
+
expression: Extract<Expression, { kind: "convert" }>,
|
|
119
|
+
value: Constant | undefined,
|
|
120
|
+
target: PlcType | undefined,
|
|
121
|
+
): Constant | undefined => {
|
|
122
|
+
if (value?.kind === "integer" && target && integerType(target)) {
|
|
123
|
+
const integer = BigInt(value.value);
|
|
124
|
+
return {
|
|
125
|
+
kind: "integer",
|
|
126
|
+
value: String(
|
|
127
|
+
expression.policy === "exact"
|
|
128
|
+
? integer
|
|
129
|
+
: applyIntegerPolicy(integer, target, expression.policy),
|
|
130
|
+
),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (value?.kind === "integer" && target?.kind === "float") {
|
|
134
|
+
return { kind: "float", value: roundFloat(Number(value.value), target) };
|
|
135
|
+
}
|
|
136
|
+
if (
|
|
137
|
+
value?.kind === "float" &&
|
|
138
|
+
target &&
|
|
139
|
+
integerType(target) &&
|
|
140
|
+
expression.policy === "saturate"
|
|
141
|
+
) {
|
|
142
|
+
return {
|
|
143
|
+
kind: "integer",
|
|
144
|
+
value: String(saturateFloatToInteger(value.value, target)),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (value?.kind === "float" && target?.kind === "float") {
|
|
148
|
+
return {
|
|
149
|
+
kind: "float",
|
|
150
|
+
value:
|
|
151
|
+
expression.policy === "saturate"
|
|
152
|
+
? saturateFloat(value.value, target)
|
|
153
|
+
: roundFloat(value.value, target),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const foldUnary = (
|
|
160
|
+
{ operator }: Extract<Expression, { kind: "unary" }>,
|
|
161
|
+
value: Constant | undefined,
|
|
162
|
+
target: PlcType | undefined,
|
|
163
|
+
): Constant | undefined => {
|
|
164
|
+
if (value?.kind === "bool") {
|
|
165
|
+
return operator === "not"
|
|
166
|
+
? { kind: "bool", value: !value.value }
|
|
167
|
+
: undefined;
|
|
168
|
+
}
|
|
169
|
+
if (value?.kind === "float") {
|
|
170
|
+
const result = floatUnary(operator, value.value);
|
|
171
|
+
return result === undefined ? undefined : finiteFloat(result, target);
|
|
172
|
+
}
|
|
173
|
+
if (value?.kind === "duration") {
|
|
174
|
+
const result = integerUnary(operator, BigInt(value.nanoseconds));
|
|
175
|
+
return operator === "timeToMilliseconds" && result !== undefined
|
|
176
|
+
? { kind: "integer", value: String(result) }
|
|
177
|
+
: undefined;
|
|
178
|
+
}
|
|
179
|
+
if (value?.kind !== "integer" || operator === "timeToMilliseconds") {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
const result = integerUnary(operator, BigInt(value.value));
|
|
183
|
+
if (result === undefined) {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
// Negation is left unwrapped: its proof requires the result to fit.
|
|
187
|
+
if (operator !== "bitNot") {
|
|
188
|
+
return { kind: "integer", value: String(result) };
|
|
189
|
+
}
|
|
190
|
+
return target?.kind === "bits"
|
|
191
|
+
? {
|
|
192
|
+
kind: "integer",
|
|
193
|
+
value: String(applyIntegerPolicy(result, target, "wrap")),
|
|
194
|
+
}
|
|
195
|
+
: undefined;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
type Ordered = Exclude<Constant, { kind: "bool" | "array" | "record" }>;
|
|
199
|
+
|
|
200
|
+
const ordered = (value: Constant): value is Ordered =>
|
|
201
|
+
value.kind !== "bool" && value.kind !== "array" && value.kind !== "record";
|
|
202
|
+
|
|
203
|
+
/** The value two constants of one kind compare as; durations compare by nanoseconds. */
|
|
204
|
+
const comparable = (value: Ordered): bigint | number | string => {
|
|
205
|
+
switch (value.kind) {
|
|
206
|
+
case "integer":
|
|
207
|
+
return BigInt(value.value);
|
|
208
|
+
case "duration":
|
|
209
|
+
return BigInt(value.nanoseconds);
|
|
210
|
+
default:
|
|
211
|
+
return value.value;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const foldBinary = (
|
|
216
|
+
expression: Extract<Expression, { kind: "binary" }>,
|
|
217
|
+
left: Constant,
|
|
218
|
+
right: Constant,
|
|
219
|
+
target: PlcType | undefined,
|
|
220
|
+
): Constant | undefined => {
|
|
221
|
+
if (left.kind === "bool" && right.kind === "bool") {
|
|
222
|
+
const value = booleanOperation(
|
|
223
|
+
expression.operator,
|
|
224
|
+
left.value,
|
|
225
|
+
right.value,
|
|
226
|
+
);
|
|
227
|
+
return value === undefined ? undefined : { kind: "bool", value };
|
|
228
|
+
}
|
|
229
|
+
if (left.kind !== right.kind || !ordered(left) || !ordered(right)) {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
const compared = compare(
|
|
233
|
+
expression.operator,
|
|
234
|
+
comparable(left),
|
|
235
|
+
comparable(right),
|
|
236
|
+
);
|
|
237
|
+
if (compared !== undefined) {
|
|
238
|
+
return { kind: "bool", value: compared };
|
|
239
|
+
}
|
|
240
|
+
if (left.kind === "integer" && right.kind === "integer") {
|
|
241
|
+
const value = integerArithmetic(
|
|
242
|
+
expression.operator,
|
|
243
|
+
BigInt(left.value),
|
|
244
|
+
BigInt(right.value),
|
|
245
|
+
);
|
|
246
|
+
if (value === undefined) {
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
const wrapped =
|
|
250
|
+
target &&
|
|
251
|
+
integerType(target) &&
|
|
252
|
+
(expression.policy || target.kind === "bits")
|
|
253
|
+
? applyIntegerPolicy(value, target, expression.policy ?? "wrap")
|
|
254
|
+
: value;
|
|
255
|
+
return { kind: "integer", value: String(wrapped) };
|
|
256
|
+
}
|
|
257
|
+
if (left.kind === "float" && right.kind === "float") {
|
|
258
|
+
const value = floatArithmetic(expression.operator, left.value, right.value);
|
|
259
|
+
if (value === undefined) {
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
kind: "float",
|
|
264
|
+
value: target?.kind === "float" ? roundFloat(value, target) : value,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
return undefined;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/** A float result as its type holds it, or nothing when it isn't finite, as NaN and infinities aren't constants. */
|
|
271
|
+
const finiteFloat = (
|
|
272
|
+
value: number,
|
|
273
|
+
target: PlcType | undefined,
|
|
274
|
+
): Constant | undefined => {
|
|
275
|
+
const rounded = target?.kind === "float" ? roundFloat(value, target) : value;
|
|
276
|
+
return Number.isFinite(rounded)
|
|
277
|
+
? { kind: "float", value: rounded }
|
|
278
|
+
: undefined;
|
|
279
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generated names are a fixed function of the identity of what they stand for, so the same source
|
|
5
|
+
* names the same things whatever else changes: an online change then finds every piece of state
|
|
6
|
+
* where it left it. A digest keeps the name short and its identity out of the PLC's namespace.
|
|
7
|
+
*/
|
|
8
|
+
export const identityDigest = (identity: string): string =>
|
|
9
|
+
createHash("sha256").update(identity).digest("hex").slice(0, 8);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Text in PascalCase, as generated names use it: `bin_full` and `bin full` become `BinFull`.
|
|
13
|
+
* Capitals inside a word are kept, so `PlantIO` stays as written while `plant-io` is `PlantIo`.
|
|
14
|
+
*/
|
|
15
|
+
export const pascalCase = (text: string): string =>
|
|
16
|
+
text
|
|
17
|
+
.split(/[^A-Za-z0-9]+/)
|
|
18
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
19
|
+
.join("");
|
package/src/ir/index.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
export { nanosecondsPerMillisecond } from "@tsplc/iec";
|
|
2
|
+
export {
|
|
3
|
+
aggregate,
|
|
4
|
+
aggregateIssue,
|
|
5
|
+
defaultValue,
|
|
6
|
+
expressionIssue,
|
|
7
|
+
placeType,
|
|
8
|
+
valueFits,
|
|
9
|
+
} from "./aggregates";
|
|
10
|
+
export {
|
|
11
|
+
type LoopConditions,
|
|
12
|
+
type PathCondition,
|
|
13
|
+
pathConditions,
|
|
14
|
+
projectReconstruction,
|
|
15
|
+
type ReconstructedSegment,
|
|
16
|
+
type Reconstruction,
|
|
17
|
+
type RoutineConditions,
|
|
18
|
+
type StatementConditions,
|
|
19
|
+
staticNames,
|
|
20
|
+
} from "./conditions";
|
|
21
|
+
export {
|
|
22
|
+
type CheckedPlace,
|
|
23
|
+
type CoreExpression,
|
|
24
|
+
type CoreFunction,
|
|
25
|
+
type CoreFunctionBlock,
|
|
26
|
+
type CorePath,
|
|
27
|
+
type CoreStatement,
|
|
28
|
+
constantIndex,
|
|
29
|
+
directWrites,
|
|
30
|
+
freeze,
|
|
31
|
+
instanceFields,
|
|
32
|
+
isBlockRoutine,
|
|
33
|
+
isTemporary,
|
|
34
|
+
isVerifiedCore,
|
|
35
|
+
nestedBodies,
|
|
36
|
+
type VerifiedCoreModule,
|
|
37
|
+
} from "./core-model";
|
|
38
|
+
export {
|
|
39
|
+
type Diagnostic,
|
|
40
|
+
error,
|
|
41
|
+
formatDiagnostic,
|
|
42
|
+
hasErrors,
|
|
43
|
+
type SourceSpan,
|
|
44
|
+
} from "./diagnostics";
|
|
45
|
+
export {
|
|
46
|
+
calendarLiteral,
|
|
47
|
+
domainConstantFits,
|
|
48
|
+
domainIssue,
|
|
49
|
+
durationLiteral,
|
|
50
|
+
} from "./domains";
|
|
51
|
+
export { fold } from "./fold";
|
|
52
|
+
export { identityDigest, pascalCase } from "./identity";
|
|
53
|
+
export {
|
|
54
|
+
errorName,
|
|
55
|
+
failureCode,
|
|
56
|
+
failureName,
|
|
57
|
+
httpErrors,
|
|
58
|
+
jobError,
|
|
59
|
+
jobErrors,
|
|
60
|
+
reservedErrors,
|
|
61
|
+
} from "./job-errors";
|
|
62
|
+
export {
|
|
63
|
+
type LayoutBudget,
|
|
64
|
+
layoutIssue,
|
|
65
|
+
maxNestingDepth,
|
|
66
|
+
maxScalarElements,
|
|
67
|
+
maxScalarElementsText,
|
|
68
|
+
maxStringCapacity,
|
|
69
|
+
} from "./layouts";
|
|
70
|
+
export {
|
|
71
|
+
type Artifact,
|
|
72
|
+
type Constant,
|
|
73
|
+
comparisonOperators,
|
|
74
|
+
type Deployment,
|
|
75
|
+
type EmitResult,
|
|
76
|
+
type Expression,
|
|
77
|
+
type FunctionBlock,
|
|
78
|
+
type GeneratedObject,
|
|
79
|
+
type HttpMethod,
|
|
80
|
+
httpMethods,
|
|
81
|
+
isLogicalIo,
|
|
82
|
+
type MethodOwner,
|
|
83
|
+
optionalRecord,
|
|
84
|
+
type Place,
|
|
85
|
+
type PlcFunction,
|
|
86
|
+
type PlcModule,
|
|
87
|
+
type PlcProgram,
|
|
88
|
+
type Retention,
|
|
89
|
+
type StandardBlock,
|
|
90
|
+
type Statement,
|
|
91
|
+
standardBlocks,
|
|
92
|
+
type Task,
|
|
93
|
+
type TelemetryCall,
|
|
94
|
+
telemetryOperands,
|
|
95
|
+
type Variable,
|
|
96
|
+
} from "./model";
|
|
97
|
+
export { normalizeModule } from "./normalize";
|
|
98
|
+
export {
|
|
99
|
+
canAssign,
|
|
100
|
+
constantFits,
|
|
101
|
+
exactIntegerLimit,
|
|
102
|
+
type IntegerInterval,
|
|
103
|
+
isRadixLiteral,
|
|
104
|
+
literalOutsideType,
|
|
105
|
+
parseFloatLiteral,
|
|
106
|
+
parseInteger,
|
|
107
|
+
wrapsNatively,
|
|
108
|
+
} from "./numeric";
|
|
109
|
+
export {
|
|
110
|
+
pathIndexes,
|
|
111
|
+
statementExpressions,
|
|
112
|
+
subexpressions,
|
|
113
|
+
} from "./operands";
|
|
114
|
+
export {
|
|
115
|
+
interval,
|
|
116
|
+
loopBounds,
|
|
117
|
+
maxLoopIterations,
|
|
118
|
+
narrowRanges,
|
|
119
|
+
} from "./ranges";
|
|
120
|
+
export { scalarIssue } from "./scalars";
|
|
121
|
+
export {
|
|
122
|
+
foldConstantRoutines,
|
|
123
|
+
type SharedRoutines,
|
|
124
|
+
shareRoutines,
|
|
125
|
+
} from "./share";
|
|
126
|
+
export { type CoreTables, coreTables } from "./tables";
|
|
127
|
+
export { forwardTemporaries } from "./temporaries";
|
|
128
|
+
export {
|
|
129
|
+
type Field,
|
|
130
|
+
instancedBlock,
|
|
131
|
+
integerRange,
|
|
132
|
+
integerType,
|
|
133
|
+
type PlcType,
|
|
134
|
+
primitiveTypes,
|
|
135
|
+
type SymbolId,
|
|
136
|
+
type TypeId,
|
|
137
|
+
} from "./types";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** Job error codes by meaning, as generated async blocks and service calls set them. */
|
|
2
|
+
export const jobError = {
|
|
3
|
+
cancelled: 1,
|
|
4
|
+
timeout: 2,
|
|
5
|
+
tooLarge: 3,
|
|
6
|
+
malformedResponse: 4,
|
|
7
|
+
network: 5,
|
|
8
|
+
tls: 6,
|
|
9
|
+
noRequestSlot: 7,
|
|
10
|
+
/**
|
|
11
|
+
* An online change replaced the code of a job that was running: its step and stored state
|
|
12
|
+
* were made for the old code, so it stops instead of resuming in the new. Like `cancelled`,
|
|
13
|
+
* it skips `catch`, so a retry can't send a request twice.
|
|
14
|
+
*/
|
|
15
|
+
codeChanged: 11001,
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
const httpBase = 10000;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* An HTTP response fails a job with `base` + its status, up to `last`. `refused` is a 400: the
|
|
22
|
+
* server can't take the request, so sending it again cannot succeed.
|
|
23
|
+
*/
|
|
24
|
+
export const httpErrors = {
|
|
25
|
+
base: httpBase,
|
|
26
|
+
last: 10999,
|
|
27
|
+
refused: httpBase + 400,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Codes tsPLC itself reports from 11000 on, which a program's own `throw` may not use. */
|
|
31
|
+
export const reservedErrors = { first: 11000, last: 11099 };
|
|
32
|
+
|
|
33
|
+
const errorMeanings: Record<number, string> = {
|
|
34
|
+
[jobError.cancelled]: "cancelled",
|
|
35
|
+
[jobError.timeout]: "timeout",
|
|
36
|
+
[jobError.tooLarge]: "request or response too large to encode",
|
|
37
|
+
[jobError.malformedResponse]: "malformed response",
|
|
38
|
+
[jobError.network]: "network error or server restarted",
|
|
39
|
+
[jobError.tls]: "TLS error",
|
|
40
|
+
[jobError.noRequestSlot]: "no free request slot",
|
|
41
|
+
[jobError.codeChanged]:
|
|
42
|
+
"an online change replaced the job's code while it ran",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Job error codes, as every generated async block and service call reports them. */
|
|
46
|
+
export const jobErrors: Record<string, string> = {
|
|
47
|
+
...errorMeanings,
|
|
48
|
+
[httpErrors.base]: `HTTP status: ${httpErrors.base} + status code`,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** What a telemetry exception record calls an error code: its meaning when it is a job error. */
|
|
52
|
+
export const errorName = (code: number): string | undefined => {
|
|
53
|
+
if (code > httpErrors.base && code <= httpErrors.last) {
|
|
54
|
+
return `HTTP ${code - httpErrors.base}`;
|
|
55
|
+
}
|
|
56
|
+
return errorMeanings[code];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** The code a failure reports: its job error code, or 0 for anything else thrown, such as an Error under Bun. */
|
|
60
|
+
export const failureCode = (reason: unknown): number =>
|
|
61
|
+
typeof reason === "number" ? reason : 0;
|
|
62
|
+
|
|
63
|
+
/** What an exception record calls a failure: a job error's meaning, or a thrown Error's message. */
|
|
64
|
+
export const failureName = (reason: unknown): string | undefined => {
|
|
65
|
+
if (typeof reason === "number") {
|
|
66
|
+
return errorName(reason);
|
|
67
|
+
}
|
|
68
|
+
return reason instanceof Error ? reason.message : String(reason);
|
|
69
|
+
};
|