@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.
Files changed (157) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.js +920 -943
  3. package/dist/cli.js.map +13 -10
  4. package/dist/codesys.js +3893 -3352
  5. package/dist/codesys.js.map +29 -20
  6. package/dist/elaborate-capture.js +33 -17
  7. package/dist/elaborate-capture.js.map +3 -3
  8. package/dist/index.js +8787 -6855
  9. package/dist/index.js.map +40 -20
  10. package/dist/ir.js +1575 -1072
  11. package/dist/ir.js.map +21 -17
  12. package/dist/math.js.map +2 -2
  13. package/dist/project.js +154 -60
  14. package/dist/project.js.map +5 -5
  15. package/dist/telemetry.js +27 -9
  16. package/dist/telemetry.js.map +5 -5
  17. package/package.json +112 -112
  18. package/src/codesys/config.ts +50 -0
  19. package/src/codesys/debug.ts +670 -0
  20. package/src/codesys/emit.ts +142 -0
  21. package/src/codesys/engineering.ts +109 -0
  22. package/src/codesys/expressions.ts +92 -0
  23. package/src/codesys/global-lists.ts +250 -0
  24. package/src/codesys/global-names.ts +68 -0
  25. package/src/codesys/http.ts +890 -0
  26. package/src/codesys/index.ts +30 -0
  27. package/src/codesys/instrumentation.ts +430 -0
  28. package/src/codesys/io-paths.ts +140 -0
  29. package/src/codesys/manifest.ts +643 -0
  30. package/src/codesys/names.ts +81 -0
  31. package/src/codesys/numeric.ts +409 -0
  32. package/src/codesys/operands.ts +62 -0
  33. package/src/codesys/plan.ts +605 -0
  34. package/src/codesys/programs.ts +53 -0
  35. package/src/codesys/publication.ts +171 -0
  36. package/src/codesys/routines.ts +603 -0
  37. package/src/codesys/sources.ts +71 -0
  38. package/src/codesys/st-files.ts +115 -0
  39. package/src/codesys/storage.ts +97 -0
  40. package/src/codesys/symbols.ts +220 -0
  41. package/src/codesys/task-access.ts +320 -0
  42. package/src/codesys/telemetry.ts +732 -0
  43. package/src/codesys/types.ts +250 -0
  44. package/src/codesys/vendor.ts +150 -0
  45. package/src/frontend/analyzer.ts +794 -0
  46. package/src/frontend/async.ts +317 -1137
  47. package/src/frontend/blocks.ts +537 -403
  48. package/src/frontend/configuration.ts +176 -144
  49. package/src/frontend/const-functions.ts +183 -148
  50. package/src/frontend/cooperative.ts +73 -43
  51. package/src/frontend/coroutines.ts +1032 -0
  52. package/src/frontend/elaborate/calls.ts +535 -0
  53. package/src/frontend/elaborate/capture.ts +157 -101
  54. package/src/frontend/elaborate/dialect.ts +257 -0
  55. package/src/frontend/elaborate/expressions.ts +992 -0
  56. package/src/frontend/elaborate/frame.ts +53 -0
  57. package/src/frontend/elaborate/graph.ts +116 -71
  58. package/src/frontend/elaborate/imports.ts +70 -0
  59. package/src/frontend/elaborate/index.ts +435 -331
  60. package/src/frontend/elaborate/initializers.ts +198 -0
  61. package/src/frontend/elaborate/layouts.ts +591 -384
  62. package/src/frontend/elaborate/model.ts +264 -169
  63. package/src/frontend/elaborate/specialize.ts +343 -2586
  64. package/src/frontend/elaborate/statements.ts +606 -0
  65. package/src/frontend/elaborate/storage.ts +546 -0
  66. package/src/frontend/elaborate/values.ts +58 -0
  67. package/src/frontend/elaborate/writes.ts +100 -0
  68. package/src/frontend/exposure.ts +172 -140
  69. package/src/frontend/expressions.ts +1383 -0
  70. package/src/frontend/intrinsics.ts +337 -0
  71. package/src/frontend/module-graph.ts +89 -59
  72. package/src/frontend/positions.ts +271 -200
  73. package/src/frontend/promise-chains.ts +897 -0
  74. package/src/frontend/promises.ts +390 -747
  75. package/src/frontend/routines.ts +181 -0
  76. package/src/frontend/sdk.ts +82 -0
  77. package/src/frontend/services.ts +506 -372
  78. package/src/frontend/statements.ts +855 -0
  79. package/src/frontend/tasks.ts +471 -0
  80. package/src/frontend/telemetry.ts +174 -0
  81. package/src/frontend/types.ts +845 -551
  82. package/src/frontend/typescript.ts +323 -2543
  83. package/src/index.ts +5 -5
  84. package/src/ir/aggregates.ts +275 -0
  85. package/src/ir/allocation.ts +84 -0
  86. package/src/ir/core-model.ts +146 -0
  87. package/src/ir/declarations.ts +295 -0
  88. package/src/ir/diagnostics.ts +61 -0
  89. package/src/ir/domains.ts +152 -0
  90. package/src/ir/fold.ts +342 -0
  91. package/src/ir/index.ts +90 -0
  92. package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
  93. package/src/ir/layouts.ts +181 -0
  94. package/src/ir/link.ts +354 -0
  95. package/src/ir/model.ts +386 -0
  96. package/{ir → src/ir}/normalization-error.ts +4 -4
  97. package/src/ir/normalize.ts +70 -0
  98. package/src/ir/numeric.ts +171 -0
  99. package/src/ir/ranges.ts +256 -0
  100. package/src/ir/routine.ts +1163 -0
  101. package/src/ir/scalars.ts +321 -0
  102. package/src/ir/types.ts +127 -0
  103. package/src/project/artifacts.ts +342 -0
  104. package/src/project/cli.ts +177 -0
  105. package/src/project/debugger.ts +82 -0
  106. package/src/project/index.ts +6 -0
  107. package/src/project/plant.ts +467 -0
  108. package/src/project/project-commands.ts +372 -0
  109. package/src/project/project.ts +457 -0
  110. package/src/project/release.ts +153 -0
  111. package/src/project/server.ts +150 -0
  112. package/src/project/terminal.ts +32 -0
  113. package/stdlib/math.ts +14 -10
  114. package/stdlib/telemetry.ts +71 -47
  115. package/types/async.d.ts +46 -40
  116. package/types/data-types.d.ts +26 -26
  117. package/types/globals.d.ts +41 -38
  118. package/types/index.d.ts +96 -51
  119. package/types/plc.d.ts +3 -4
  120. package/types/telemetry.d.ts +14 -14
  121. package/backends/codesys/config.ts +0 -54
  122. package/backends/codesys/debug.ts +0 -509
  123. package/backends/codesys/emit.ts +0 -1738
  124. package/backends/codesys/engineering.ts +0 -88
  125. package/backends/codesys/global-names.ts +0 -49
  126. package/backends/codesys/http.ts +0 -675
  127. package/backends/codesys/index.ts +0 -13
  128. package/backends/codesys/io-paths.ts +0 -105
  129. package/backends/codesys/manifest.ts +0 -312
  130. package/backends/codesys/names.ts +0 -76
  131. package/backends/codesys/numeric.ts +0 -356
  132. package/backends/codesys/plan.ts +0 -457
  133. package/backends/codesys/publication.ts +0 -59
  134. package/backends/codesys/storage.ts +0 -70
  135. package/backends/codesys/task-access.ts +0 -293
  136. package/backends/codesys/telemetry.ts +0 -593
  137. package/backends/codesys/types.ts +0 -179
  138. package/backends/codesys/vendor.ts +0 -108
  139. package/ir/aggregates.ts +0 -333
  140. package/ir/allocation.ts +0 -74
  141. package/ir/core-model.ts +0 -129
  142. package/ir/core.ts +0 -64
  143. package/ir/declarations.ts +0 -208
  144. package/ir/diagnostics.ts +0 -43
  145. package/ir/domains.ts +0 -96
  146. package/ir/index.ts +0 -8
  147. package/ir/link.ts +0 -185
  148. package/ir/model.ts +0 -346
  149. package/ir/routine.ts +0 -859
  150. package/ir/scalars.ts +0 -675
  151. package/ir/types.ts +0 -76
  152. package/project/artifacts.ts +0 -277
  153. package/project/cli.ts +0 -395
  154. package/project/index.ts +0 -1
  155. package/project/plant.ts +0 -509
  156. package/project/project.ts +0 -358
  157. package/project/server.ts +0 -113
@@ -0,0 +1,115 @@
1
+ import type { EmitResult, GeneratedObject } from "@tsplc/compiler/ir";
2
+ import type { SourceMapping } from "./emit.ts";
3
+ import type { CodesysPlan } from "./plan.ts";
4
+ import { type CodesysSymbols, required } from "./symbols.ts";
5
+
6
+ /** The file that holds an object's declaration, under its path: its only file, or `declaration.st` beside its implementation. */
7
+ export const declarationFile = (path: string, object: GeneratedObject) =>
8
+ object.implementation === undefined ? `${path}.st` : `${path}/declaration.st`;
9
+
10
+ /**
11
+ * Writes each object to its ST files, in its source module's folder: an object without an
12
+ * implementation is one `.st` file, any other a folder with `declaration.st` and
13
+ * `implementation.st`. Each object's `sourcePath` and each mapping's `path` become those files.
14
+ */
15
+ export function stFiles(
16
+ plan: CodesysPlan,
17
+ symbols: CodesysSymbols,
18
+ objects: readonly GeneratedObject[],
19
+ mappings: readonly SourceMapping[],
20
+ ): EmitResult["artifacts"] {
21
+ const placed = objects.map((object) => {
22
+ const path = objectPath(plan, symbols, objects, object);
23
+ object.sourcePath = declarationFile(path, object);
24
+ return { object, path };
25
+ });
26
+ const artifacts = placed.flatMap(({ object, path }) => [
27
+ { path: declarationFile(path, object), content: object.declaration },
28
+ ...(object.implementation === undefined
29
+ ? []
30
+ : [
31
+ {
32
+ path: `${path}/implementation.st`,
33
+ content: object.implementation,
34
+ },
35
+ ]),
36
+ ]);
37
+ for (const mapping of mappings) {
38
+ const owner = placed.find(({ object }) => {
39
+ const key = mappingKey(object);
40
+ return mapping.path === `${key}.st` || mapping.path.startsWith(`${key}/`);
41
+ });
42
+ if (owner) {
43
+ mapping.path =
44
+ owner.path + mapping.path.slice(mappingKey(owner.object).length);
45
+ }
46
+ }
47
+ return artifacts;
48
+ }
49
+
50
+ /** What an emitter names an object in its mappings' paths: a method by its id, as method names repeat across blocks. */
51
+ const mappingKey = (object: GeneratedObject) =>
52
+ object.kind === "method" ? object.id : object.name;
53
+
54
+ /** The folder of an object that no source module places. */
55
+ const generatedFolder = (object: GeneratedObject) => {
56
+ if (object.kind === "dataType") {
57
+ return "Types";
58
+ }
59
+ return object.id === "io" ? "IO" : "_Generated";
60
+ };
61
+
62
+ /** The module an object was emitted from, as the id of its symbol, program entry or variables names it. */
63
+ const sourceModule = (
64
+ { core }: CodesysPlan,
65
+ symbols: CodesysSymbols,
66
+ object: GeneratedObject,
67
+ ): string => {
68
+ const group = symbols.globals.find((item) => item.id === object.id);
69
+ if (group) {
70
+ // Variable IDs are relative to tsconfig, unlike their diagnostic spans.
71
+ return group.variables[0]?.id.split(":")[0] ?? "";
72
+ }
73
+ const program = core.programs.find((item) => item.id === object.id);
74
+ return (program ? program.entry : object.id).split(":")[0] ?? "";
75
+ };
76
+
77
+ function objectPath(
78
+ plan: CodesysPlan,
79
+ symbols: CodesysSymbols,
80
+ objects: readonly GeneratedObject[],
81
+ object: GeneratedObject,
82
+ ): string {
83
+ const { core } = plan;
84
+ if (object.kind === "method" && object.parent) {
85
+ const parent = required(
86
+ objects.find((candidate) => candidate.id === object.parent),
87
+ );
88
+ return `${objectPath(plan, symbols, objects, parent)}/methods/${object.name}`;
89
+ }
90
+ if (object.kind === "method") {
91
+ const method = required(core.functions.find((fn) => fn.id === object.id));
92
+ const owner = required(
93
+ objects.find((item) => item.id === method.owner?.block),
94
+ );
95
+ return `${objectPath(plan, symbols, objects, owner)}/methods/${object.name}`;
96
+ }
97
+ const source = sourceModule(plan, symbols, object);
98
+ if (
99
+ /^[\w./ -]+\.tsx?$/.test(source) &&
100
+ !source.split(/[\\/]/).includes("..") &&
101
+ !source.startsWith("/")
102
+ ) {
103
+ const segments = source.replaceAll("\\", "/").split("/").slice(0, -1);
104
+ if (segments[0] === "src") {
105
+ segments.shift();
106
+ }
107
+ // A DUT directory can also contain sibling input/output layouts. Keep
108
+ // that directory for every DUT, or its primary type would block the folder.
109
+ if (object.kind !== "dataType" && segments.at(-1) === object.name) {
110
+ segments.pop();
111
+ }
112
+ return [...segments, object.name].join("/");
113
+ }
114
+ return `${generatedFolder(object)}/${object.name}`;
115
+ }
@@ -0,0 +1,97 @@
1
+ import { basename } from "node:path";
2
+ import {
3
+ instancedBlock,
4
+ type Variable,
5
+ type VerifiedCoreModule,
6
+ } from "@tsplc/compiler/ir";
7
+
8
+ /** Which global list of its module a variable goes in. */
9
+ export type GlobalStorage = "constants" | "retained" | "state";
10
+
11
+ interface GlobalList {
12
+ id: string;
13
+ module: string;
14
+ storage: GlobalStorage;
15
+ variables: Variable[];
16
+ }
17
+
18
+ /**
19
+ * The module that declares a module-level variable, from its id. Spans point at what the user
20
+ * wrote, which for elaborated storage is not the module the elaborator made for it.
21
+ */
22
+ export const moduleFile = (variable: Variable): string =>
23
+ variable.id.slice(0, variable.id.indexOf(":"));
24
+
25
+ /** The module's name: its file name without extension. */
26
+ export const moduleName = (variable: Variable): string =>
27
+ basename(moduleFile(variable)).replace(/\.[^.]+$/, "");
28
+
29
+ const globalStorage = (
30
+ variable: Variable,
31
+ retainedVariables: ReadonlySet<string>,
32
+ ): GlobalStorage => {
33
+ if (variable.storage === "constant") {
34
+ return "constants";
35
+ }
36
+ return retainedVariables.has(variable.id) ? "retained" : "state";
37
+ };
38
+
39
+ /** CODESYS promotes an enclosing instance when private child state is retained. */
40
+ export function storageLayout(core: VerifiedCoreModule) {
41
+ const types = new Map(core.types.map((type) => [type.id, type]));
42
+ const blocks = new Map(core.blocks.map((block) => [block.id, block]));
43
+ const retention = new Map<string, boolean>();
44
+ const blockRetained = (id: string): boolean => {
45
+ const cached = retention.get(id);
46
+ if (cached !== undefined) {
47
+ return cached;
48
+ }
49
+ const block = blocks.get(id);
50
+ if (!block) {
51
+ throw new Error("Missing verified block during retention planning.");
52
+ }
53
+ const retained = block.state.some((field) => {
54
+ const type = instancedBlock(field.type, types);
55
+ return field.retain || (type !== undefined && blockRetained(type.block));
56
+ });
57
+ retention.set(id, retained);
58
+ return retained;
59
+ };
60
+
61
+ const retainedBlocks = new Set(
62
+ core.blocks
63
+ .filter((block) => blockRetained(block.id))
64
+ .map((block) => block.id),
65
+ );
66
+ const retainedVariables = new Set(
67
+ [...core.variables, ...core.blocks.flatMap((block) => block.state)]
68
+ .filter((variable) => {
69
+ const type = instancedBlock(variable.type, types);
70
+ return (
71
+ variable.retain ||
72
+ (type !== undefined && retainedBlocks.has(type.block))
73
+ );
74
+ })
75
+ .map((variable) => variable.id),
76
+ );
77
+
78
+ const groups = new Map<string, GlobalList>();
79
+ for (const variable of core.variables) {
80
+ if (variable.storage === "input" || variable.storage === "output") {
81
+ continue;
82
+ }
83
+ const storage = globalStorage(variable, retainedVariables);
84
+ const id = `${moduleFile(variable)}:${storage}`;
85
+ let group = groups.get(id);
86
+ if (!group) {
87
+ group = { id, module: moduleName(variable), storage, variables: [] };
88
+ groups.set(id, group);
89
+ }
90
+ group.variables.push(variable);
91
+ }
92
+ return {
93
+ retainedBlocks,
94
+ retainedVariables,
95
+ globalLists: [...groups.values()],
96
+ };
97
+ }
@@ -0,0 +1,220 @@
1
+ import {
2
+ type CoreFunction,
3
+ type CoreFunctionBlock,
4
+ type GeneratedObject,
5
+ standardBlocks,
6
+ } from "@tsplc/compiler/ir";
7
+ import { planGlobalNames } from "./global-names.ts";
8
+ import type { Instrumentation } from "./instrumentation.ts";
9
+ import { Names } from "./names.ts";
10
+ import { NumericLowering } from "./numeric.ts";
11
+ import type { CodesysPlan } from "./plan.ts";
12
+ import { accessedStorage } from "./task-access.ts";
13
+
14
+ // All symbol relationships were checked by normalization and planning. Failure
15
+ // here is an emitter invariant violation, rather than a recoverable source error.
16
+ export function required<T>(value: T | undefined): T {
17
+ if (value === undefined) {
18
+ throw new Error("Missing verified CODESYS symbol.");
19
+ }
20
+ return value;
21
+ }
22
+
23
+ /** The names a routine's ST gives its parameters, locals and the block state it reaches. */
24
+ interface RoutineScope {
25
+ names: Names;
26
+ references: Map<string, string>;
27
+ }
28
+
29
+ /**
30
+ * The target names of a program: its objects, storage, routines and block fields. They are
31
+ * allocated in emission order, each scope reserving the objects emitted before it, and are
32
+ * complete before any routine body is written.
33
+ */
34
+ export type CodesysSymbols = ReturnType<typeof createSymbols>;
35
+
36
+ /** A plan with the target names and instrumentation allocated for it: what each emitter reads. */
37
+ export interface EmissionContext {
38
+ plan: CodesysPlan;
39
+ symbols: CodesysSymbols;
40
+ instrumentation: Instrumentation;
41
+ }
42
+
43
+ export function createSymbols({ core, options }: CodesysPlan) {
44
+ const persistent = new Set(
45
+ core.variables
46
+ .filter((item) => item.storage !== "constant")
47
+ .map((item) => item.id),
48
+ );
49
+ const programRoutines = new Set<string>();
50
+ for (const fn of core.functions) {
51
+ if (
52
+ !fn.owner &&
53
+ [...accessedStorage(fn, core)].some((id) => persistent.has(id))
54
+ ) {
55
+ programRoutines.add(fn.id);
56
+ }
57
+ }
58
+ const globalNames = planGlobalNames(core, options.programNames);
59
+ return {
60
+ ...globalNames,
61
+ blocks: new Map(core.blocks.map((block) => [block.id, block])),
62
+ numeric: new NumericLowering(globalNames.typeMap, (label) =>
63
+ globalNames.names.allocate(label),
64
+ ),
65
+ programRoutines,
66
+ /** The global symbol of each module variable and logical IO. */
67
+ references: new Map<string, string>(),
68
+ functionNames: new Map<string, string>(),
69
+ programResults: new Map<string, string>(),
70
+ mailboxLocks: new Map<string, string>(),
71
+ blockFields: new Map<string, Map<string, string>>(),
72
+ routineScopes: new Map<string, RoutineScope>(),
73
+ };
74
+ }
75
+
76
+ export function allocateFunctionNames(
77
+ symbols: CodesysSymbols,
78
+ { core }: CodesysPlan,
79
+ ): void {
80
+ const entries = new Set(core.programs.map((program) => program.entry));
81
+ for (const fn of core.functions.filter((fn) => !fn.owner)) {
82
+ const program = symbols.programRoutines.has(fn.id);
83
+ const label = program && entries.has(fn.id) ? `${fn.name}_body` : fn.name;
84
+ symbols.functionNames.set(
85
+ fn.id,
86
+ symbols.names.allocateObject(program ? "PRG" : "F", label),
87
+ );
88
+ }
89
+ }
90
+
91
+ /** The type of each instrumentation field allocateBlockFields gives a block; the manifest describes them with it. */
92
+ export const instrumentationFieldTypes = {
93
+ busy: "BOOL",
94
+ step: "UINT",
95
+ startedAt: "UDINT",
96
+ job: "UDINT",
97
+ sequence: "UDINT",
98
+ } as const;
99
+
100
+ export function allocateBlockFields(
101
+ symbols: CodesysSymbols,
102
+ instrumentation: Instrumentation,
103
+ plan: CodesysPlan,
104
+ objects: readonly GeneratedObject[],
105
+ ): void {
106
+ const { core } = plan;
107
+ const { functionNames, blockNames, blockFields } = symbols;
108
+ for (const block of core.blocks) {
109
+ const fields = new Names();
110
+ const mapped = new Map<string, string>();
111
+ for (const object of objects) {
112
+ fields.reserve(object.name);
113
+ }
114
+ fields.reserve(instrumentation.global);
115
+ for (const objectName of [
116
+ ...core.functions
117
+ .filter((fn) => !fn.owner)
118
+ .map((fn) => required(functionNames.get(fn.id))),
119
+ ...blockNames.values(),
120
+ ]) {
121
+ fields.reserve(objectName);
122
+ }
123
+ for (const method of core.functions.filter(
124
+ (fn) => fn.owner?.block === block.id,
125
+ )) {
126
+ functionNames.set(method.id, fields.allocate(method.name));
127
+ }
128
+ const native = block.standard && standardBlocks[block.standard];
129
+ for (const item of [...block.inputs, ...block.outputs, ...block.state]) {
130
+ mapped.set(
131
+ item.id,
132
+ native
133
+ ? required(
134
+ [...native.inputs, ...native.outputs].find(
135
+ ([name]) => name === item.name,
136
+ ),
137
+ )[1]
138
+ : fields.allocate(item.name),
139
+ );
140
+ }
141
+ if (block.runtime?.kind === "mailbox") {
142
+ symbols.mailboxLocks.set(block.id, fields.allocate("plc_mailbox_lock"));
143
+ }
144
+ blockFields.set(block.id, mapped);
145
+
146
+ const settings = plan.project.debug;
147
+ // Trace sites and an unhandled failure need the step a job finished at.
148
+ const stepped =
149
+ instrumentation.analysis.jobSites.has(block.id) ||
150
+ plan.telemetry?.failures.has(block.id);
151
+ const tracked = Boolean(block.job && (settings?.jobs || stepped));
152
+ const carries =
153
+ settings?.jobs && instrumentation.analysis.carriers.has(block.id);
154
+ if (tracked || carries) {
155
+ instrumentation.fields.set(block.id, {
156
+ ...(tracked && {
157
+ busy: fields.allocate("tsplcBusy"),
158
+ track: fields.allocate("TsplcTrack"),
159
+ }),
160
+ ...(tracked &&
161
+ settings?.jobs && { startedAt: fields.allocate("startedAt") }),
162
+ ...(stepped && { step: fields.allocate("tsplcStep") }),
163
+ ...(carries && { job: fields.allocate("requestJob") }),
164
+ ...(carries &&
165
+ block.runtime?.kind === "http" && {
166
+ sequence: fields.allocate("sequence"),
167
+ }),
168
+ });
169
+ }
170
+ }
171
+ }
172
+
173
+ export function allocateRoutineScope(
174
+ symbols: CodesysSymbols,
175
+ instrumentation: Instrumentation,
176
+ objects: readonly GeneratedObject[],
177
+ fn: CoreFunction | CoreFunctionBlock,
178
+ ): void {
179
+ const block = "inputs" in fn ? fn : undefined;
180
+ const owner =
181
+ block ??
182
+ ("owner" in fn && fn.owner
183
+ ? symbols.blocks.get(fn.owner.block)
184
+ : undefined);
185
+ const names = new Names();
186
+ const references = new Map<string, string>();
187
+ for (const object of objects) {
188
+ names.reserve(object.name);
189
+ }
190
+ names.reserve(instrumentation.global);
191
+ for (const name of [
192
+ ...symbols.functionNames.values(),
193
+ ...symbols.blockNames.values(),
194
+ ]) {
195
+ names.reserve(name);
196
+ }
197
+ if (symbols.programRoutines.has(fn.id) && "parameters" in fn && fn.result) {
198
+ symbols.programResults.set(fn.id, names.allocate("plc_result"));
199
+ }
200
+ if (owner) {
201
+ for (const item of [...owner.outputs, ...owner.state]) {
202
+ const name = required(
203
+ required(symbols.blockFields.get(owner.id)).get(item.id),
204
+ );
205
+ names.reserve(name);
206
+ references.set(item.id, name);
207
+ }
208
+ }
209
+ for (const item of "parameters" in fn ? fn.parameters : fn.inputs) {
210
+ const name = block
211
+ ? required(required(symbols.blockFields.get(block.id)).get(item.id))
212
+ : names.allocate(item.name);
213
+ names.reserve(name);
214
+ references.set(item.id, name);
215
+ }
216
+ for (const item of fn.locals) {
217
+ references.set(item.id, names.allocate(item.name));
218
+ }
219
+ symbols.routineScopes.set(fn.id, { names, references });
220
+ }