Open a construct: a bracketed loop, or a fork with the arm this site is in.
(into: WireBlock, scope: Scope)
| 241 | |
| 242 | /** Open a construct: a bracketed loop, or a fork with the arm this site is in. */ |
| 243 | function openScope(into: WireBlock, scope: Scope): Open { |
| 244 | if (scope.kind === 'loop') { |
| 245 | const block: WireItem = { kind: 'block', block: 'loop', by: scope.loop.text, loop: scope.loop.kind, body: [] }; |
| 246 | into.push(block); |
| 247 | return { branch: scope.branch, body: (block as Extract<WireItem, { kind: 'block' }>).body }; |
| 248 | } |
| 249 | const g = scope.guard; |
| 250 | // The decision as a reader says it, not as the guard stores it: a |
| 251 | // disjunction keeps the parentheses that stop `a || b` from reading as two |
| 252 | // ways of arriving (`guardLabel` is the one place that decides). |
| 253 | const fork: Extract<WireItem, { kind: 'fork' }> = { kind: 'fork', on: guardLabel([{ ...g, negated: false }]), form: formOf(g), arms: [] }; |
| 254 | // An early exit is a fork whose OTHER arm left before this site could run: |
| 255 | // `if (!product) throw` — the throw is written first, so it is the first arm, |
| 256 | // and it is empty because nothing in the picture happens there. |
| 257 | if (g.form === 'guard' && g.negated) { |
| 258 | fork.arms.push({ when: guardLabel([{ ...g, negated: false }]), ends: g.exit ?? 'return', body: [] }); |
| 259 | } |
| 260 | into.push(fork); |
| 261 | return { branch: scope.branch, fork, armKey: armKey(g), body: armFor(fork, g).body }; |
| 262 | } |
| 263 | |
| 264 | /** What one recorded site draws as. */ |
| 265 | function itemFor(input: ProgramInput, site: ProgramSite, path: readonly string[], state: Reading): WireItem | null { |