@tsplc/tmp-debug 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.
Files changed (78) hide show
  1. package/README.md +38 -2
  2. package/dist/api.js +744 -0
  3. package/dist/api.js.map +12 -0
  4. package/dist/constant.js +33 -0
  5. package/dist/constant.js.map +10 -0
  6. package/dist/explain-file.js +468 -0
  7. package/dist/explain-file.js.map +11 -0
  8. package/dist/explain.js +1310 -0
  9. package/dist/explain.js.map +19 -0
  10. package/dist/format.js +214 -0
  11. package/dist/format.js.map +12 -0
  12. package/dist/index.js +4066 -0
  13. package/dist/index.js.map +44 -0
  14. package/dist/manifest.js +240 -0
  15. package/dist/manifest.js.map +10 -0
  16. package/dist/paths.js +242 -0
  17. package/dist/paths.js.map +10 -0
  18. package/package.json +72 -4
  19. package/src/adapter.ts +77 -0
  20. package/src/api.ts +446 -0
  21. package/src/app-server.ts +151 -0
  22. package/src/build-files.ts +46 -0
  23. package/src/constant.ts +41 -0
  24. package/src/debugger.ts +237 -0
  25. package/src/errors.ts +13 -0
  26. package/src/explain-expressions.ts +484 -0
  27. package/src/explain-file.ts +325 -0
  28. package/src/explain-frames.ts +155 -0
  29. package/src/explain-loops.ts +151 -0
  30. package/src/explain-operations.ts +232 -0
  31. package/src/explain-tables.ts +345 -0
  32. package/src/explain-value.ts +203 -0
  33. package/src/explain-verdicts.ts +548 -0
  34. package/src/explain.ts +82 -0
  35. package/src/hardware.ts +159 -0
  36. package/src/http.ts +218 -0
  37. package/src/index.ts +44 -0
  38. package/src/jobs.ts +139 -0
  39. package/src/login-value.ts +165 -0
  40. package/src/login.ts +315 -0
  41. package/src/manifest.ts +328 -0
  42. package/src/opcua.ts +80 -0
  43. package/src/paths.ts +363 -0
  44. package/src/requests.ts +99 -0
  45. package/src/server-cursor.ts +22 -0
  46. package/src/shown.ts +229 -0
  47. package/src/telemetry.ts +125 -0
  48. package/src/timeline.ts +148 -0
  49. package/src/trace.ts +62 -0
  50. package/src/ui/bits.ts +48 -0
  51. package/src/ui/client.ts +87 -0
  52. package/src/ui/dom.ts +61 -0
  53. package/src/ui/explain-read.ts +105 -0
  54. package/src/ui/format.ts +261 -0
  55. package/src/ui/hardware.ts +425 -0
  56. package/src/ui/header.ts +207 -0
  57. package/src/ui/health.ts +64 -0
  58. package/src/ui/index.ts +27 -0
  59. package/src/ui/jobs.ts +263 -0
  60. package/src/ui/links.ts +59 -0
  61. package/src/ui/live-value.ts +85 -0
  62. package/src/ui/main.ts +76 -0
  63. package/src/ui/overview.ts +178 -0
  64. package/src/ui/page.ts +286 -0
  65. package/src/ui/pages.ts +64 -0
  66. package/src/ui/recent-window.ts +49 -0
  67. package/src/ui/recent.ts +355 -0
  68. package/src/ui/request-trace.ts +38 -0
  69. package/src/ui/requests.ts +80 -0
  70. package/src/ui/routes.ts +98 -0
  71. package/src/ui/store.ts +295 -0
  72. package/src/ui/success-cache.ts +11 -0
  73. package/src/ui/tree.ts +162 -0
  74. package/src/ui/variables.ts +597 -0
  75. package/src/ui/why-watch.ts +11 -0
  76. package/src/ui/why.ts +694 -0
  77. package/src/values.ts +170 -0
  78. package/src/watch.ts +32 -0
@@ -0,0 +1,232 @@
1
+ import { elementaryType, isIntegerValued } from "@tsplc/iec";
2
+ import {
3
+ applyIntegerPolicy,
4
+ compare,
5
+ floatArithmetic,
6
+ floatUnary,
7
+ integerArithmetic,
8
+ integerUnary,
9
+ isElementary,
10
+ roundFloat,
11
+ saturateFloatToInteger,
12
+ } from "@tsplc/iec/arithmetic";
13
+ import type { ExplainExpression } from "./explain-file";
14
+ import { known, type Outcome, type PlcValue, unknown } from "./explain-value";
15
+
16
+ type Node<Kind extends ExplainExpression["kind"]> = Extract<
17
+ ExplainExpression,
18
+ { kind: Kind }
19
+ >;
20
+
21
+ const integerLayout = (type: string) => {
22
+ const scalar = elementaryType(type);
23
+ return isIntegerValued(scalar)
24
+ ? { bits: scalar.bits, signed: scalar.signed }
25
+ : undefined;
26
+ };
27
+ const floatLayout = (type: string) => {
28
+ const scalar = elementaryType(type);
29
+ return scalar?.family === "real" ? { bits: scalar.bits } : undefined;
30
+ };
31
+
32
+ /** Wraps an integer result to its type, as the emitted `TO_<type>` does. */
33
+ const typed = (
34
+ value: bigint,
35
+ type: string,
36
+ policy: "wrap" | "saturate" = "wrap",
37
+ ): PlcValue => {
38
+ const layout = integerLayout(type);
39
+ return {
40
+ kind: "integer",
41
+ value: layout ? applyIntegerPolicy(value, layout, policy) : value,
42
+ };
43
+ };
44
+
45
+ export const binary = (
46
+ node: Node<"binary">,
47
+ left: PlcValue,
48
+ right: PlcValue,
49
+ ): Outcome => {
50
+ if (left.kind === "interval" || right.kind === "interval") {
51
+ return intervalBinary(node, left, right);
52
+ }
53
+ // A reference compares by the id it holds, as the PLC compares it.
54
+ if (left.kind === "instance" || right.kind === "instance") {
55
+ const id = (value: PlcValue): PlcValue =>
56
+ value.kind === "instance" ? { kind: "integer", value: value.id } : value;
57
+ return binary(node, id(left), id(right));
58
+ }
59
+ if (left.kind === "bool" && right.kind === "bool") {
60
+ const compared = compare(
61
+ node.operator,
62
+ Number(left.value),
63
+ Number(right.value),
64
+ );
65
+ return compared === undefined
66
+ ? unknown("an operation this debugger can't evaluate")
67
+ : known({ kind: "bool", value: compared });
68
+ }
69
+ if (left.kind === "integer" && right.kind === "integer") {
70
+ const compared = compare(node.operator, left.value, right.value);
71
+ if (compared !== undefined) {
72
+ return known({ kind: "bool", value: compared });
73
+ }
74
+ const result = integerArithmetic(node.operator, left.value, right.value);
75
+ if (result === undefined) {
76
+ return unknown(
77
+ right.value === 0n
78
+ ? "a division by zero"
79
+ : "an operation this debugger can't evaluate",
80
+ );
81
+ }
82
+ return known(typed(result, node.type, node.policy));
83
+ }
84
+ const leftNumber =
85
+ left.kind === "float" || left.kind === "integer"
86
+ ? Number(left.value)
87
+ : undefined;
88
+ const rightNumber =
89
+ right.kind === "float" || right.kind === "integer"
90
+ ? Number(right.value)
91
+ : undefined;
92
+ if (leftNumber === undefined || rightNumber === undefined) {
93
+ if (left.kind === "text" && right.kind === "text") {
94
+ const compared = compare(node.operator, left.value, right.value);
95
+ return compared === undefined
96
+ ? unknown("an operation on text")
97
+ : known({ kind: "bool", value: compared });
98
+ }
99
+ return unknown("an operation this debugger can't evaluate");
100
+ }
101
+ const compared = compare(node.operator, leftNumber, rightNumber);
102
+ if (compared !== undefined) {
103
+ return known({ kind: "bool", value: compared }, [
104
+ "a comparison of REAL values",
105
+ ]);
106
+ }
107
+ const result = floatArithmetic(node.operator, leftNumber, rightNumber);
108
+ const layout = floatLayout(node.type);
109
+ return result === undefined
110
+ ? unknown("an operation this debugger can't evaluate")
111
+ : known({
112
+ kind: "float",
113
+ value: layout ? roundFloat(result, layout) : result,
114
+ });
115
+ };
116
+
117
+ /** An operation on the clock interval: decided only when both ends agree. */
118
+ const intervalBinary = (
119
+ node: Node<"binary">,
120
+ left: PlcValue,
121
+ right: PlcValue,
122
+ ): Outcome => {
123
+ const ends = (value: PlcValue): [bigint, bigint] | undefined => {
124
+ if (value.kind === "interval") {
125
+ return [value.low, value.high];
126
+ }
127
+ return value.kind === "integer" ? [value.value, value.value] : undefined;
128
+ };
129
+ const leftEnds = ends(left);
130
+ const rightEnds = ends(right);
131
+ if (!leftEnds || !rightEnds) {
132
+ return unknown("an operation on the clock");
133
+ }
134
+ const low = binary(
135
+ node,
136
+ { kind: "integer", value: leftEnds[0] },
137
+ { kind: "integer", value: rightEnds[0] },
138
+ );
139
+ const high = binary(
140
+ node,
141
+ { kind: "integer", value: leftEnds[1] },
142
+ { kind: "integer", value: rightEnds[1] },
143
+ );
144
+ if (!low.known || !high.known) {
145
+ return unknown("an operation on the clock");
146
+ }
147
+ if (low.value.kind === "bool" && high.value.kind === "bool") {
148
+ return low.value.value === high.value.value
149
+ ? known(low.value)
150
+ : unknown("within one scan of the limit");
151
+ }
152
+ if (low.value.kind === "integer" && high.value.kind === "integer") {
153
+ // Only addition and subtraction keep the interval's order, and a wrap inside it splits it.
154
+ const monotonic = node.operator === "add" || node.operator === "subtract";
155
+ const [from, to] =
156
+ node.operator === "subtract" && right.kind === "interval"
157
+ ? [high.value.value, low.value.value]
158
+ : [low.value.value, high.value.value];
159
+ return monotonic && from <= to
160
+ ? known({ kind: "interval", low: from, high: to })
161
+ : unknown("within one scan of the clock");
162
+ }
163
+ return unknown("an operation on the clock");
164
+ };
165
+
166
+ export const unary = (node: Node<"unary">, operand: PlcValue): Outcome => {
167
+ if (node.operator === "not" && operand.kind === "bool") {
168
+ return known({ kind: "bool", value: !operand.value });
169
+ }
170
+ if (operand.kind === "integer") {
171
+ const result = integerUnary(node.operator, operand.value);
172
+ if (result !== undefined) {
173
+ return known(typed(result, node.type));
174
+ }
175
+ }
176
+ if (operand.kind === "float") {
177
+ const result = floatUnary(node.operator, operand.value);
178
+ if (result === undefined) {
179
+ return unknown("an operation this debugger can't evaluate");
180
+ }
181
+ if (node.operator === "truncate") {
182
+ return known(typed(BigInt(result), node.type));
183
+ }
184
+ const layout = floatLayout(node.type);
185
+ return known({
186
+ kind: "float",
187
+ value:
188
+ layout && isElementary(node.operator)
189
+ ? roundFloat(result, layout)
190
+ : result,
191
+ });
192
+ }
193
+ return unknown("an operation this debugger can't evaluate");
194
+ };
195
+
196
+ export const convert = (node: Node<"convert">, operand: PlcValue): Outcome => {
197
+ const integer = integerLayout(node.type);
198
+ const float = floatLayout(node.type);
199
+ if (operand.kind === "integer" && integer) {
200
+ return known({
201
+ kind: "integer",
202
+ value:
203
+ node.policy === "exact"
204
+ ? operand.value
205
+ : applyIntegerPolicy(operand.value, integer, node.policy),
206
+ });
207
+ }
208
+ if (operand.kind === "integer" && float) {
209
+ return known({
210
+ kind: "float",
211
+ value: roundFloat(Number(operand.value), float),
212
+ });
213
+ }
214
+ if (operand.kind === "float" && integer && node.policy === "saturate") {
215
+ return known({
216
+ kind: "integer",
217
+ value: saturateFloatToInteger(operand.value, integer),
218
+ });
219
+ }
220
+ if (operand.kind === "float" && float) {
221
+ return known({
222
+ kind: "float",
223
+ value: roundFloat(operand.value, float),
224
+ });
225
+ }
226
+ if (operand.kind === "interval" && integer) {
227
+ return known(operand);
228
+ }
229
+ return node.policy === "exact"
230
+ ? known(operand)
231
+ : unknown("a conversion this debugger can't evaluate");
232
+ };
@@ -0,0 +1,345 @@
1
+ import { arrayIndex, joinPath } from "@tsplc/opcua/contract";
2
+ import type {
3
+ ExplainAssignment,
4
+ ExplainCall,
5
+ ExplainCondition,
6
+ ExplainFile,
7
+ ExplainFlags,
8
+ } from "./explain-file";
9
+ import { merge } from "./explain-value";
10
+ import type { DebugIo, DebugManifest } from "./manifest";
11
+ import { anyIndex, pathSegments } from "./paths";
12
+
13
+ /** The most chains followed from the task to each writer. */
14
+ const maxChains = 16;
15
+
16
+ /** A path through calls from a task's entry to a writer's routine instance. */
17
+ export interface Chain {
18
+ readonly task: string;
19
+ /** The entry routine row. */
20
+ readonly entry: number;
21
+ /** Call indexes, outermost first. */
22
+ readonly calls: readonly number[];
23
+ }
24
+
25
+ export interface Writer {
26
+ readonly assignment: number;
27
+ /** The indexes of the explained path the writer's `[*]`s must take. */
28
+ readonly indexes: readonly bigint[];
29
+ readonly chains: readonly Chain[];
30
+ }
31
+
32
+ export interface Explanation {
33
+ readonly path: string;
34
+ readonly writers: readonly Writer[];
35
+ readonly flags: Omit<ExplainFlags, "path">;
36
+ readonly io: DebugIo | null;
37
+ }
38
+
39
+ /**
40
+ * Whether a concrete path starts with a match of a pattern's segments, the index each `[*]` takes,
41
+ * and whether the path ends where the pattern does.
42
+ */
43
+ const matchPattern = (pattern: readonly string[], path: string) => {
44
+ const segments = pathSegments(path);
45
+ const indexes: bigint[] = [];
46
+ for (const [position, part] of pattern.entries()) {
47
+ const segment = segments[position];
48
+ if (segment === undefined) {
49
+ return undefined;
50
+ }
51
+ if (part === anyIndex) {
52
+ const index = arrayIndex(segment);
53
+ if (index === undefined) {
54
+ return undefined;
55
+ }
56
+ indexes.push(BigInt(index));
57
+ } else if (part !== segment) {
58
+ return undefined;
59
+ }
60
+ }
61
+ return { indexes, exact: segments.length === pattern.length };
62
+ };
63
+
64
+ /**
65
+ * What `explain.json` says, by what an evaluation looks up: the writers of a path and the chains
66
+ * of calls that reach them, the flags of a path, and what each routine row's slots name.
67
+ */
68
+ export const createExplainTables = (
69
+ manifest: DebugManifest,
70
+ file: ExplainFile,
71
+ ) => {
72
+ const text = (index: number | null): string | null =>
73
+ index === null ? null : (file.texts[index] ?? null);
74
+ /** A statement's source text and where it is, for reasons. */
75
+ const siteText = (site: number | null): string => {
76
+ const found = site === null ? undefined : file.sites[site];
77
+ const source = found ? text(found.text) : null;
78
+ const sourceFile = found ? manifest.sources[found.range.file] : undefined;
79
+ const where =
80
+ found && sourceFile
81
+ ? `${sourceFile.split("/").at(-1)}:${found.range.line}`
82
+ : "";
83
+ return [source, where && `at ${where}`].filter(Boolean).join(" ");
84
+ };
85
+
86
+ const byPath = new Map<string, number[]>();
87
+ const patterns: {
88
+ assignment: number;
89
+ segments: string[];
90
+ whole: boolean;
91
+ }[] = [];
92
+ file.assignments.forEach((assignment, index) => {
93
+ const written = file.paths[assignment.writes.path] ?? "";
94
+ if (assignment.writes.kind === "pattern") {
95
+ patterns.push({
96
+ assignment: index,
97
+ segments: pathSegments(written),
98
+ whole: assignment.whole,
99
+ });
100
+ return;
101
+ }
102
+ byPath.set(written, [...(byPath.get(written) ?? []), index]);
103
+ });
104
+ const flagsByPath = new Map(
105
+ file.flags.map((item) => [file.paths[item.path] ?? "", item]),
106
+ );
107
+ const rowsByKey = new Map<string, number[]>();
108
+ file.routines.forEach((row, index) => {
109
+ rowsByKey.set(row.key, [...(rowsByKey.get(row.key) ?? []), index]);
110
+ });
111
+ // Expressions and conditions name paths, locals, calls and loops by slot in their routine row.
112
+ const slots = (row: number) => file.routines[row];
113
+ const pathAt = (row: number, slot: number): string =>
114
+ file.paths[slots(row)?.paths[slot] ?? -1] ?? "";
115
+ const localAt = (row: number, slot: number): number =>
116
+ slots(row)?.locals[slot] ?? -1;
117
+ const callAt = (row: number, slot: number): number =>
118
+ slots(row)?.calls[slot] ?? -1;
119
+ const loopAt = (row: number, slot: number): number =>
120
+ slots(row)?.loops[slot] ?? -1;
121
+ const loopSlot = (row: number, loop: number): number =>
122
+ slots(row)?.loops.indexOf(loop) ?? -1;
123
+ const instanceOf = (row: number): string | null => {
124
+ const instance = file.routines[row]?.instance;
125
+ return instance === null || instance === undefined
126
+ ? null
127
+ : (file.paths[instance] ?? null);
128
+ };
129
+
130
+ /** The routine rows a call can run, and the receiver indexes each needs. */
131
+ const calleesOf = (
132
+ call: ExplainCall,
133
+ ): { row: number; indexes: readonly bigint[] }[] => {
134
+ const rows = rowsByKey.get(call.callee) ?? [];
135
+ const { receiver } = call;
136
+ if (receiver.kind === "none") {
137
+ return rows
138
+ .filter((row) => instanceOf(row) === null)
139
+ .map((row) => ({ row, indexes: [] }));
140
+ }
141
+ const target = file.paths[receiver.path] ?? "";
142
+ if (receiver.kind === "path") {
143
+ return rows
144
+ .filter((row) => instanceOf(row) === target)
145
+ .map((row) => ({ row, indexes: [] }));
146
+ }
147
+ const pattern = pathSegments(target);
148
+ return rows.flatMap((row) => {
149
+ const match = matchPattern(pattern, instanceOf(row) ?? "");
150
+ return match?.exact ? [{ row, indexes: match.indexes }] : [];
151
+ });
152
+ };
153
+ const callers = new Map<number, number[]>();
154
+ file.calls.forEach((call, index) => {
155
+ for (const { row } of calleesOf(call)) {
156
+ callers.set(row, [...(callers.get(row) ?? []), index]);
157
+ }
158
+ });
159
+ const entries = new Map(file.entries.map((entry) => [entry.routine, entry]));
160
+
161
+ /** Chains from task entries to a row, outermost call first. */
162
+ const chainsTo = (row: number): Chain[] => {
163
+ const found: Chain[] = [];
164
+ const walk = (
165
+ current: number,
166
+ below: readonly number[],
167
+ seen: ReadonlySet<number>,
168
+ ) => {
169
+ if (found.length >= maxChains) {
170
+ return;
171
+ }
172
+ const entry = entries.get(current);
173
+ if (entry) {
174
+ found.push({ task: entry.task, entry: current, calls: below });
175
+ }
176
+ for (const call of callers.get(current) ?? []) {
177
+ const caller = file.calls[call]?.routine;
178
+ if (caller !== undefined && !seen.has(caller)) {
179
+ walk(caller, [call, ...below], new Set([...seen, caller]));
180
+ }
181
+ }
182
+ };
183
+ walk(row, [], new Set([row]));
184
+ return found;
185
+ };
186
+ const chainCache = new Map<number, Chain[]>();
187
+ const chains = (row: number): Chain[] => {
188
+ const cached = chainCache.get(row);
189
+ if (cached) {
190
+ return cached;
191
+ }
192
+ const computed = chainsTo(row);
193
+ chainCache.set(row, computed);
194
+ return computed;
195
+ };
196
+
197
+ /**
198
+ * The chains that reach a writer. A write through a by-reference parameter reaches the path only
199
+ * through the calls that pass it, so its chains run to the outermost of them and end with them.
200
+ */
201
+ const writerChains = (assignment: ExplainAssignment): Chain[] => {
202
+ const outermost = assignment.via.at(-1);
203
+ if (outermost === undefined) {
204
+ return chains(assignment.routine);
205
+ }
206
+ const passing = [...assignment.via].reverse();
207
+ return chains(file.calls[outermost]?.routine ?? -1).map((chain) => ({
208
+ ...chain,
209
+ calls: [...chain.calls, ...passing],
210
+ }));
211
+ };
212
+
213
+ /** The flags of a path: those stored at it and at every prefix. */
214
+ const flagsOf = (path: string): Omit<ExplainFlags, "path"> => {
215
+ const merged: Omit<ExplainFlags, "path"> = {
216
+ input: null,
217
+ clientWritable: false,
218
+ hmiControl: null,
219
+ retained: false,
220
+ constantTable: false,
221
+ writerTasks: [],
222
+ service: null,
223
+ initial: null,
224
+ reference: false,
225
+ };
226
+ let prefix = "";
227
+ for (const segment of pathSegments(path)) {
228
+ prefix = joinPath(prefix, segment);
229
+ const item = flagsByPath.get(prefix);
230
+ if (!item) {
231
+ continue;
232
+ }
233
+ merged.input ??= item.input;
234
+ merged.clientWritable ||= item.clientWritable;
235
+ merged.hmiControl ??= item.hmiControl;
236
+ merged.retained ||= item.retained;
237
+ merged.constantTable ||= item.constantTable;
238
+ merged.writerTasks = merge(merged.writerTasks, item.writerTasks);
239
+ merged.service ??= item.service;
240
+ merged.reference ||= item.reference;
241
+ if (prefix === path) {
242
+ merged.initial = item.initial;
243
+ }
244
+ }
245
+ return merged;
246
+ };
247
+
248
+ /** Writers of `path` itself, of a record or array above it written whole, or of a pattern matching it. */
249
+ const writersOf = (path: string): Writer[] => {
250
+ const found: { assignment: number; indexes: bigint[] }[] = [];
251
+ const segments = pathSegments(path);
252
+ let prefix = "";
253
+ for (const [position, segment] of segments.entries()) {
254
+ prefix = joinPath(prefix, segment);
255
+ const last = position === segments.length - 1;
256
+ for (const assignment of byPath.get(prefix) ?? []) {
257
+ if (last || file.assignments[assignment]?.whole) {
258
+ found.push({ assignment, indexes: [] });
259
+ }
260
+ }
261
+ }
262
+ for (const pattern of patterns) {
263
+ const match = matchPattern(pattern.segments, path);
264
+ if (match && (match.exact || pattern.whole)) {
265
+ found.push({ assignment: pattern.assignment, indexes: match.indexes });
266
+ }
267
+ }
268
+ return found.map((item) => {
269
+ const assignment = file.assignments[item.assignment];
270
+ return {
271
+ ...item,
272
+ chains: assignment ? writerChains(assignment) : [],
273
+ };
274
+ });
275
+ };
276
+
277
+ /** The scan order of a write through `chain`: the entry's order, then each call's and its own position. */
278
+ const writeOrder = (chain: Chain, position: number): number[] => [
279
+ entries.get(chain.entry)?.order ?? 0,
280
+ ...chain.calls.map((call) => file.calls[call]?.position ?? 0),
281
+ position,
282
+ ];
283
+
284
+ /** The conditions of a tree entry and its parents, outermost first. */
285
+ const conditionList = (entry: number | null): ExplainCondition[] => {
286
+ const list: ExplainCondition[] = [];
287
+ for (
288
+ let current = entry;
289
+ current !== null;
290
+ current = file.conditions[current]?.parent ?? null
291
+ ) {
292
+ const item = file.conditions[current];
293
+ if (item) {
294
+ list.unshift(item.condition);
295
+ }
296
+ }
297
+ return list;
298
+ };
299
+
300
+ /** A loop's counter: the local it is, or the stored path. */
301
+ const counterNode = (loop: number) => {
302
+ const record = file.loops[loop];
303
+ const node = record ? file.expressions[record.counter] : undefined;
304
+ return record && node && { routine: record.routine, node };
305
+ };
306
+ const loopCounterLocal = (loop: number): number | undefined => {
307
+ const counter = counterNode(loop);
308
+ return counter?.node.kind === "local"
309
+ ? localAt(counter.routine, counter.node.local)
310
+ : undefined;
311
+ };
312
+ const loopCounterPath = (loop: number): string | undefined => {
313
+ const counter = counterNode(loop);
314
+ return counter?.node.kind === "read"
315
+ ? pathAt(counter.routine, counter.node.path)
316
+ : undefined;
317
+ };
318
+
319
+ return {
320
+ text,
321
+ siteText,
322
+ slots,
323
+ pathAt,
324
+ localAt,
325
+ callAt,
326
+ loopAt,
327
+ loopSlot,
328
+ calleesOf,
329
+ entries,
330
+ flagsOf,
331
+ writersOf,
332
+ writeOrder,
333
+ conditionList,
334
+ loopCounterLocal,
335
+ loopCounterPath,
336
+ explain: (path: string): Explanation => ({
337
+ path,
338
+ writers: writersOf(path),
339
+ flags: flagsOf(path),
340
+ io: manifest.io.find((item) => item.path === path) ?? null,
341
+ }),
342
+ };
343
+ };
344
+
345
+ export type ExplainTables = ReturnType<typeof createExplainTables>;