@tsplc/compiler 0.5.13 → 0.5.15
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 +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- 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 +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {
|
|
2
|
+
coreTables,
|
|
3
|
+
type Task,
|
|
4
|
+
type VerifiedCoreModule,
|
|
5
|
+
} from "@tsplc/compiler/ir";
|
|
6
|
+
import { nanosecondsPerMillisecond } from "@tsplc/iec";
|
|
7
|
+
import type { Report } from "./plan";
|
|
8
|
+
import { accessedStorage } from "./task-access";
|
|
9
|
+
|
|
10
|
+
/** The deployment's tasks, then one per background interval and priority, with detached continuations added after their callers. */
|
|
11
|
+
export function schedulePrograms(
|
|
12
|
+
core: VerifiedCoreModule,
|
|
13
|
+
tasks: readonly Task[],
|
|
14
|
+
report: Report,
|
|
15
|
+
): readonly Task[] {
|
|
16
|
+
const background = new Map<string, Task & { programs: string[] }>();
|
|
17
|
+
for (const program of core.programs) {
|
|
18
|
+
if (!program.background) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const { priority, intervalNanoseconds } = program.background;
|
|
22
|
+
const name = `TsplcBackground_${priority}_${BigInt(intervalNanoseconds) / nanosecondsPerMillisecond}`;
|
|
23
|
+
const existing = background.get(name);
|
|
24
|
+
if (existing) {
|
|
25
|
+
existing.programs.push(program.name);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
background.set(name, {
|
|
29
|
+
name,
|
|
30
|
+
priority,
|
|
31
|
+
intervalNanoseconds,
|
|
32
|
+
programs: [program.name],
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return appendContinuations(core, [...tasks, ...background.values()], report);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Detached continuations run after their caller programs in the same cyclic task. */
|
|
39
|
+
function appendContinuations(
|
|
40
|
+
core: VerifiedCoreModule,
|
|
41
|
+
tasks: readonly Task[],
|
|
42
|
+
report: Report,
|
|
43
|
+
): readonly Task[] {
|
|
44
|
+
const continuations = core.programs.flatMap((program) =>
|
|
45
|
+
program.continuation
|
|
46
|
+
? [{ name: program.name, scope: program.continuation }]
|
|
47
|
+
: [],
|
|
48
|
+
);
|
|
49
|
+
if (!continuations.length) {
|
|
50
|
+
return tasks;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const { functionOf } = coreTables(core);
|
|
54
|
+
// The deployment's program names are checked after scheduling, so an unknown one is skipped here.
|
|
55
|
+
const programs = new Map(
|
|
56
|
+
core.programs.map((program) => [program.name, program]),
|
|
57
|
+
);
|
|
58
|
+
const placements = tasks.map(
|
|
59
|
+
(task): { task: Task; roots: Set<string>; additions: string[] } => ({
|
|
60
|
+
task,
|
|
61
|
+
roots: new Set(
|
|
62
|
+
task.programs.flatMap((name) => {
|
|
63
|
+
const program = programs.get(name);
|
|
64
|
+
return !program || program.background
|
|
65
|
+
? []
|
|
66
|
+
: [...accessedStorage(functionOf(program.entry), core)];
|
|
67
|
+
}),
|
|
68
|
+
),
|
|
69
|
+
additions: [],
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
for (const { name, scope } of continuations) {
|
|
73
|
+
const root = core.variables.find(
|
|
74
|
+
(variable) =>
|
|
75
|
+
variable.name === scope.instance && variable.span.file === scope.file,
|
|
76
|
+
);
|
|
77
|
+
if (!root) {
|
|
78
|
+
report(`Missing continuation owner ${scope.instance}.`);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const owners = placements.filter(({ roots }) => roots.has(root.id));
|
|
82
|
+
const [owner] = owners;
|
|
83
|
+
if (!owner || owners.length !== 1) {
|
|
84
|
+
report(
|
|
85
|
+
`Continuation owner ${scope.instance} must belong to exactly one caller task.`,
|
|
86
|
+
root.span,
|
|
87
|
+
);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
owner.additions.push(name);
|
|
91
|
+
}
|
|
92
|
+
return placements.map(({ task, additions }) => ({
|
|
93
|
+
...task,
|
|
94
|
+
programs: task.programs.concat(additions),
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { EmitResult } from "@tsplc/compiler/ir";
|
|
2
|
+
import { emittedFiles } from "@tsplc/config/project-files";
|
|
3
|
+
import { type ServiceChannel, serviceChannelSchema } from "@tsplc/server/wire";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
|
|
6
|
+
/** `machines.ts`, which lists the service channels for the server and the Bun runtime to read back. */
|
|
7
|
+
export const serviceChannelsFile = (
|
|
8
|
+
channels: readonly ServiceChannel[],
|
|
9
|
+
): EmitResult["artifacts"][number] => ({
|
|
10
|
+
path: emittedFiles.machines,
|
|
11
|
+
content: `// Generated service channels. Do not edit.\nexport const serviceChannels=${JSON.stringify(channels, null, 2)} as const;\n`,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/** The service channels a `machines.ts` lists. */
|
|
15
|
+
export const parseServiceChannels = (machines: string): ServiceChannel[] => {
|
|
16
|
+
const listed = /export const serviceChannels=([\s\S]*?) as const;/.exec(
|
|
17
|
+
machines,
|
|
18
|
+
);
|
|
19
|
+
if (!listed?.[1]) {
|
|
20
|
+
throw new Error(`${emittedFiles.machines} lists no service channels.`);
|
|
21
|
+
}
|
|
22
|
+
return z.array(serviceChannelSchema).parse(JSON.parse(listed[1]));
|
|
23
|
+
};
|
package/src/codesys/sources.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
// biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
|
|
2
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
3
2
|
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
4
3
|
import type { SourceSpan } from "@tsplc/compiler/ir";
|
|
5
4
|
import type { SourceRange } from "@tsplc/debug/manifest";
|
|
5
|
+
import { required } from "./required";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Whether a compiled file is the project's own source: anything but an installed dependency. A
|
|
9
9
|
* monorepo's workspace packages count wherever they sit, as their files resolve to their real paths.
|
|
10
10
|
*/
|
|
11
|
-
|
|
11
|
+
const projectSource = (root: string, file: string): boolean => {
|
|
12
12
|
const path = relative(root, file);
|
|
13
13
|
return !isAbsolute(path) && !path.split(/[\\/]/).includes("node_modules");
|
|
14
14
|
};
|
|
@@ -46,6 +46,27 @@ export const sourceFolders = (
|
|
|
46
46
|
return folders[0] === "src" ? folders.slice(1) : folders;
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
+
/** A file's text and the offset each of its lines starts at. */
|
|
50
|
+
const lineStarts = (text: string) => ({
|
|
51
|
+
text,
|
|
52
|
+
lines: [0, ...[...text.matchAll(/\n/g)].map(({ index }) => index + 1)],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/** The index of the last line that starts at or before `offset`, by bisection. */
|
|
56
|
+
const lineIndex = (starts: readonly number[], offset: number): number => {
|
|
57
|
+
let low = 0;
|
|
58
|
+
let high = starts.length - 1;
|
|
59
|
+
while (low < high) {
|
|
60
|
+
const middle = (low + high + 1) >> 1;
|
|
61
|
+
if (required(starts[middle]) <= offset) {
|
|
62
|
+
low = middle;
|
|
63
|
+
} else {
|
|
64
|
+
high = middle - 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return low;
|
|
68
|
+
};
|
|
69
|
+
|
|
49
70
|
/**
|
|
50
71
|
* Project files by index, as paths relative to the project root, so a workspace package's start with
|
|
51
72
|
* `../`. Spans outside the project's own sources, such as virtual modules and dependencies, have no range.
|
|
@@ -63,17 +84,10 @@ export class Sources {
|
|
|
63
84
|
if (this.loaded.has(file)) {
|
|
64
85
|
return this.loaded.get(file);
|
|
65
86
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
for (let index = 0; index < text.length; index++) {
|
|
71
|
-
if (text[index] === "\n") {
|
|
72
|
-
lines.push(index + 1);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
source = { text, lines };
|
|
76
|
-
}
|
|
87
|
+
const source =
|
|
88
|
+
projectSource(this.root, file) && existsSync(file)
|
|
89
|
+
? lineStarts(readFileSync(file, "utf8"))
|
|
90
|
+
: undefined;
|
|
77
91
|
this.loaded.set(file, source);
|
|
78
92
|
return source;
|
|
79
93
|
}
|
|
@@ -84,23 +98,15 @@ export class Sources {
|
|
|
84
98
|
return undefined;
|
|
85
99
|
}
|
|
86
100
|
const position = (offset: number) => {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
low = middle;
|
|
93
|
-
} else {
|
|
94
|
-
high = middle - 1;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return { line: low + 1, column: offset - source.lines[low]! + 1 };
|
|
101
|
+
const line = lineIndex(source.lines, offset);
|
|
102
|
+
return {
|
|
103
|
+
line: line + 1,
|
|
104
|
+
column: offset - required(source.lines[line]) + 1,
|
|
105
|
+
};
|
|
98
106
|
};
|
|
99
107
|
const path = relative(this.root, span.file).replaceAll("\\", "/");
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
file = this.files.push(path) - 1;
|
|
103
|
-
}
|
|
108
|
+
const known = this.files.indexOf(path);
|
|
109
|
+
const file = known < 0 ? this.files.push(path) - 1 : known;
|
|
104
110
|
const start = position(span.start);
|
|
105
111
|
const end = position(span.end);
|
|
106
112
|
return { file, ...start, endLine: end.line, endColumn: end.column };
|
package/src/codesys/st-files.ts
CHANGED
|
@@ -1,142 +1,149 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
EmitResult,
|
|
3
|
+
GeneratedObject,
|
|
4
|
+
SourceSpan,
|
|
5
|
+
} from "@tsplc/compiler/ir";
|
|
6
|
+
import { generatedDirectory } from "@tsplc/config/project-files";
|
|
2
7
|
import type { SourceMapping } from "./emit";
|
|
3
|
-
import
|
|
8
|
+
import { required } from "./required";
|
|
4
9
|
import { sourceFolders } from "./sources";
|
|
5
|
-
import {
|
|
10
|
+
import { moduleFile } from "./storage";
|
|
11
|
+
import type { EmissionContext } from "./symbols";
|
|
6
12
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
|
|
13
|
+
/**
|
|
14
|
+
* A line of an object's file that a source span produced, before `stFiles` places the object.
|
|
15
|
+
* `object` is the object's id.
|
|
16
|
+
*/
|
|
17
|
+
export interface ObjectMapping {
|
|
18
|
+
object: string;
|
|
19
|
+
file: "declaration" | "implementation";
|
|
20
|
+
line: number;
|
|
21
|
+
span: SourceSpan;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** An object with the files `stFiles` wrote it to. */
|
|
25
|
+
export type PlacedObject = GeneratedObject & {
|
|
26
|
+
/** Its only file, or `declaration.st` beside its implementation. */
|
|
27
|
+
sourcePath: string;
|
|
28
|
+
implementationPath?: string;
|
|
29
|
+
/** Its path below the Application in the CODESYS project, which keeps methods beside their block. */
|
|
30
|
+
nativePath: string;
|
|
31
|
+
};
|
|
10
32
|
|
|
11
33
|
/**
|
|
12
34
|
* Writes each object to its ST files, in its source module's folder: an object without an
|
|
13
35
|
* implementation is one `.st` file, any other a folder with `declaration.st` and
|
|
14
|
-
* `implementation.st`.
|
|
36
|
+
* `implementation.st`. Returns the objects with their files and each mapping at its file.
|
|
15
37
|
*/
|
|
16
38
|
export function stFiles(
|
|
17
|
-
|
|
18
|
-
symbols: CodesysSymbols,
|
|
39
|
+
context: EmissionContext,
|
|
19
40
|
objects: readonly GeneratedObject[],
|
|
20
|
-
mappings: readonly
|
|
21
|
-
):
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
41
|
+
mappings: readonly ObjectMapping[],
|
|
42
|
+
): {
|
|
43
|
+
artifacts: EmitResult["artifacts"];
|
|
44
|
+
objects: PlacedObject[];
|
|
45
|
+
mappings: SourceMapping[];
|
|
46
|
+
} {
|
|
47
|
+
const paths = objectPaths(context, objects);
|
|
48
|
+
const placed = objects.map((object): PlacedObject => {
|
|
49
|
+
const { path, nativePath } = paths(object);
|
|
50
|
+
return object.implementation === undefined
|
|
51
|
+
? { ...object, sourcePath: `${path}.st`, nativePath }
|
|
52
|
+
: {
|
|
53
|
+
...object,
|
|
54
|
+
sourcePath: `${path}/declaration.st`,
|
|
55
|
+
implementationPath: `${path}/implementation.st`,
|
|
56
|
+
nativePath,
|
|
57
|
+
};
|
|
27
58
|
});
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
59
|
+
const owners = new Map(placed.map((object) => [object.id, object]));
|
|
60
|
+
return {
|
|
61
|
+
artifacts: placed.flatMap((object) => [
|
|
62
|
+
{ path: object.sourcePath, content: object.declaration },
|
|
63
|
+
...(object.implementation === undefined
|
|
64
|
+
? []
|
|
65
|
+
: [
|
|
66
|
+
{
|
|
67
|
+
path: required(object.implementationPath),
|
|
68
|
+
content: object.implementation,
|
|
69
|
+
},
|
|
70
|
+
]),
|
|
71
|
+
]),
|
|
72
|
+
objects: placed,
|
|
73
|
+
mappings: mappings.map(({ object, file, line, span }) => {
|
|
74
|
+
const owner = required(owners.get(object));
|
|
75
|
+
return {
|
|
76
|
+
path:
|
|
77
|
+
file === "implementation"
|
|
78
|
+
? required(owner.implementationPath)
|
|
79
|
+
: owner.sourcePath,
|
|
80
|
+
line,
|
|
81
|
+
span,
|
|
82
|
+
};
|
|
83
|
+
}),
|
|
84
|
+
};
|
|
48
85
|
}
|
|
49
86
|
|
|
50
|
-
/** Each key's first item, and that item's position, so a lookup keeps a linear search's first-match order. */
|
|
51
|
-
const firstBy = <T>(items: readonly T[], key: (item: T) => string) => {
|
|
52
|
-
const index = new Map<string, { item: T; position: number }>();
|
|
53
|
-
items.forEach((item, position) => {
|
|
54
|
-
const name = key(item);
|
|
55
|
-
if (!index.has(name)) {
|
|
56
|
-
index.set(name, { item, position });
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
return index;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
87
|
/**
|
|
63
|
-
* The
|
|
64
|
-
*
|
|
88
|
+
* The folder of an object that no source module places. The IO list is generated too, and a
|
|
89
|
+
* folder of its own could share a name with a program's state list (`io` gives `Io`).
|
|
65
90
|
*/
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
path: string,
|
|
69
|
-
): T | undefined => {
|
|
70
|
-
const candidates = [
|
|
71
|
-
...(path.endsWith(".st") ? [path.slice(0, -".st".length)] : []),
|
|
72
|
-
...[...path.matchAll(/\//g)].map((match) => path.slice(0, match.index)),
|
|
73
|
-
];
|
|
74
|
-
const [first] = candidates
|
|
75
|
-
.flatMap((key) => {
|
|
76
|
-
const owner = owners.get(key);
|
|
77
|
-
return owner ? [owner] : [];
|
|
78
|
-
})
|
|
79
|
-
.sort((left, right) => left.position - right.position);
|
|
80
|
-
return first?.item;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
/** What an emitter names an object in its mappings' paths: a method by its id, as method names repeat across blocks. */
|
|
84
|
-
const mappingKey = (object: GeneratedObject) =>
|
|
85
|
-
object.kind === "method" ? object.id : object.name;
|
|
86
|
-
|
|
87
|
-
/** The folder of an object that no source module places. */
|
|
88
|
-
const generatedFolder = (object: GeneratedObject) => {
|
|
89
|
-
if (object.kind === "dataType") {
|
|
90
|
-
return "Types";
|
|
91
|
-
}
|
|
92
|
-
return object.id === "io" ? "IO" : "_Generated";
|
|
93
|
-
};
|
|
91
|
+
const generatedFolder = (object: GeneratedObject) =>
|
|
92
|
+
object.kind === "dataType" ? "Types" : "_Generated";
|
|
94
93
|
|
|
95
94
|
/** The module an object was emitted from, as the id of its symbol, program entry or variables names it. */
|
|
96
|
-
const sourceModules = ({
|
|
97
|
-
const groups =
|
|
98
|
-
const programs =
|
|
95
|
+
const sourceModules = ({ plan, symbols }: EmissionContext) => {
|
|
96
|
+
const groups = new Map(symbols.globals.map((group) => [group.id, group]));
|
|
97
|
+
const programs = new Map(
|
|
98
|
+
plan.core.programs.map((program) => [program.id, program]),
|
|
99
|
+
);
|
|
99
100
|
return (object: GeneratedObject): string => {
|
|
100
|
-
const group = groups.get(object.id)
|
|
101
|
+
const group = groups.get(object.id);
|
|
101
102
|
if (group) {
|
|
102
103
|
// Variable IDs are relative to tsconfig, unlike their diagnostic spans.
|
|
103
|
-
return group.variables[0]
|
|
104
|
+
return moduleFile(required(group.variables[0]).id);
|
|
104
105
|
}
|
|
105
|
-
|
|
106
|
-
return (program ? program.entry : object.id).split(":")[0] ?? "";
|
|
106
|
+
return moduleFile(programs.get(object.id)?.entry ?? object.id);
|
|
107
107
|
};
|
|
108
108
|
};
|
|
109
109
|
|
|
110
|
-
/**
|
|
110
|
+
/**
|
|
111
|
+
* Each object's path without extension, in its source module's folder or its block's for a
|
|
112
|
+
* method, and its path in the CODESYS project, where a method sits directly below its block.
|
|
113
|
+
*/
|
|
111
114
|
function objectPaths(
|
|
112
|
-
|
|
113
|
-
symbols: CodesysSymbols,
|
|
115
|
+
context: EmissionContext,
|
|
114
116
|
objects: readonly GeneratedObject[],
|
|
115
|
-
): (object: GeneratedObject) => string {
|
|
116
|
-
const byId =
|
|
117
|
-
const
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const parent = required(byId.get(object.parent)?.item);
|
|
122
|
-
return `${objectPath(parent)}/methods/${object.name}`;
|
|
123
|
-
}
|
|
117
|
+
): (object: GeneratedObject) => { path: string; nativePath: string } {
|
|
118
|
+
const byId = new Map(objects.map((item) => [item.id, item]));
|
|
119
|
+
const sourceModule = sourceModules(context);
|
|
120
|
+
const objectPath = (
|
|
121
|
+
object: GeneratedObject,
|
|
122
|
+
): { path: string; nativePath: string } => {
|
|
124
123
|
if (object.kind === "method") {
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return `${objectPath(owner)}/methods/${object.name}`;
|
|
124
|
+
const parent = objectPath(required(byId.get(required(object.parent))));
|
|
125
|
+
return {
|
|
126
|
+
path: `${parent.path}/methods/${object.name}`,
|
|
127
|
+
nativePath: `${parent.nativePath}/${object.name}`,
|
|
128
|
+
};
|
|
131
129
|
}
|
|
132
130
|
const folders = required(placed.get(object));
|
|
133
|
-
|
|
131
|
+
const path = [folderPath(folders), object.name].filter(Boolean).join("/");
|
|
132
|
+
return { path, nativePath: path };
|
|
134
133
|
};
|
|
135
134
|
const placedFolders = (object: GeneratedObject): string[] => {
|
|
136
|
-
const folders = sourceFolders(
|
|
135
|
+
const folders = sourceFolders(
|
|
136
|
+
context.plan.project.root,
|
|
137
|
+
sourceModule(object),
|
|
138
|
+
);
|
|
137
139
|
if (!folders) {
|
|
138
140
|
return [generatedFolder(object)];
|
|
139
141
|
}
|
|
142
|
+
// A plain program's state lists and types are generated beside its entry module, whose
|
|
143
|
+
// folder they belong in: CODESYS would show the hidden directory as a folder of its own.
|
|
144
|
+
if (folders.at(-1) === generatedDirectory) {
|
|
145
|
+
return folders.slice(0, -1);
|
|
146
|
+
}
|
|
140
147
|
// A DUT directory can also contain sibling input/output layouts. Keep
|
|
141
148
|
// that directory for every DUT, or its primary type would block the folder.
|
|
142
149
|
return object.kind !== "dataType" && folders.at(-1) === object.name
|