Skip to content

Fix infinite loops in parser and support nested IF#25

Merged
associate-1 merged 1 commit intomainfrom
fix/nested-if-infinite-loop
Feb 19, 2026
Merged

Fix infinite loops in parser and support nested IF#25
associate-1 merged 1 commit intomainfrom
fix/nested-if-infinite-loop

Conversation

@associate-1
Copy link
Member

Summary

  • Fix OOM-inducing infinite loops in the parser that occurred when transpiling real-world occam code (e.g., KRoC course module). Two root causes: (1) nested/replicated IF within IF blocks caused parseExpression to fail without advancing the cursor, and (2) unknown type declarations in variant ? CASE blocks caused the same stall pattern.
  • Support nested IF constructs — both plain nested IF (choices inlined into parent chain) and replicated IF i = 0 FOR n (emitted as a loop with _ifmatched flag). This is a common occam idiom for searching arrays.
  • Add progress guards to all parser loops (IF, CASE, ALT, variant receive) as a safety net against future infinite loop regressions.

Test plan

  • All existing tests pass (go test ./...)
  • 4 new e2e tests: replicated IF with default, no-match fallthrough, preceding normal choice, non-replicated nested IF
  • All KRoC course module .occ files terminate without hanging (previously 4 of 9 caused OOM)

🤖 Generated with Claude Code

The parser would enter infinite loops (consuming memory until OOM) when
encountering: (1) nested/replicated IF within an IF block, and (2)
unknown type declarations inside variant receive CASE blocks. Both
caused by parsing loops that failed to advance the token position on
unrecognized constructs.

Fix nested IF by detecting IF tokens as choices in parseIfStatement and
recursively parsing them, storing in a new IfChoice.NestedIf field.
Codegen flattens non-replicated nested IFs and emits replicated ones
as loops with a _ifmatched flag. Add progress guards to all parser
loops (IF, CASE, ALT, variant receive) as a safety net.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@associate-1 associate-1 merged commit ef96b78 into main Feb 19, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments