Skip to content

fix(translate): validate control flow stack before popping#40

Merged
tetsuo-cpp merged 2 commits intocanonfrom
worktree-fix/control-flow-checks
Feb 21, 2026
Merged

fix(translate): validate control flow stack before popping#40
tetsuo-cpp merged 2 commits intocanonfrom
worktree-fix/control-flow-checks

Conversation

@tetsuo-cpp
Copy link
Owner

Summary

  • Add empty-stack guards and tag validation before each cfStack.pop_back_val() in the Forth translator
  • Mismatched control flow words (e.g. THEN without IF, REPEAT without WHILE) now produce clear error messages instead of crashing

Test plan

  • Added 7 negative test cases covering empty-stack and tag-mismatch scenarios
  • All 91 tests pass

Closes #21

Copy link
Owner Author

@tetsuo-cpp tetsuo-cpp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

Looks good. Clean, well-scoped defensive fix with solid test coverage. All 6 cfStack.pop_back_val() sites are now guarded, the tag checks are correct against each push site, and the error messages are clear.

Minor suggestions

  1. Missing test for REPEAT tag mismatch: All REPEAT tests trigger the empty-stack path. There's no test exercising the destTag != CFTag::Dest or origTag != CFTag::Orig branches inside REPEAT (e.g. IF REPEAT would hit the first). Not a blocker — the logic is identical to the other sites which are tested for mismatch — but would give full branch coverage.

  2. Future follow-up: No end-of-body check for unclosed control flow (e.g. IF without THEN leaves the stack non-empty at end of parsing). Out of scope here but could be a follow-up issue.

@tetsuo-cpp tetsuo-cpp force-pushed the worktree-fix/control-flow-checks branch from 0bb07fd to f2ec8d6 Compare February 21, 2026 06:12
@tetsuo-cpp tetsuo-cpp changed the title fix(translator): validate control flow stack before popping fix(translate): validate control flow stack before popping Feb 21, 2026
@tetsuo-cpp tetsuo-cpp merged commit ae4070a into canon Feb 21, 2026
1 check passed
@tetsuo-cpp tetsuo-cpp deleted the worktree-fix/control-flow-checks branch February 21, 2026 06:16
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.

Add validation for mismatched control flow words

1 participant