import {BlockId, HIRFunction, PrunedScopeTerminal} from '../HIR';
import {assertExhaustive, retainWhere} from '../Utils/utils';
export function flattenReactiveLoopsHIR(fn: HIRFunction): void {
const activeLoops = Array<BlockId>();
for (const [, block] of fn.body.blocks) {
retainWhere(activeLoops, id => id !== block.id);
const {terminal} = block;
switch (terminal.kind) {
case 'do-while':
case 'for':
case 'for-in':
case 'for-of':
case 'while': {
activeLoops.push(terminal.fallthrough);
break;
}
case 'scope': {
if (activeLoops.length !== 0) {
block.terminal = {
kind: 'pruned-scope',
block: terminal.block,
fallthrough: terminal.fallthrough,
id: terminal.id,
loc: terminal.loc,
scope: terminal.scope,
} as PrunedScopeTerminal;
}
break;
}
case 'branch':
case 'goto':
case 'if':
case 'label':
case 'logical':
case 'maybe-throw':
case 'optional':
case 'pruned-scope':
case 'return':
case 'sequence':
case 'switch':
case 'ternary':
case 'throw':
case 'try':
case 'unreachable':
case 'unsupported': {
break;
}
default: {
assertExhaustive(
terminal,
`Unexpected terminal kind \`${(terminal as any).kind}\``,
);
}
}
}
}