Skip to content

feat(translate): implement EXIT word and fix UNLOOP for early loop exit#27

Merged
tetsuo-cpp merged 1 commit intocanonfrom
exit-word
Feb 19, 2026
Merged

feat(translate): implement EXIT word and fix UNLOOP for early loop exit#27
tetsuo-cpp merged 1 commit intocanonfrom
exit-word

Conversation

@tetsuo-cpp
Copy link
Owner

Summary

  • Implement EXIT word for early return from user-defined words (colon definitions), emitting func.return via a dummy cond_br pattern to keep dead blocks structurally reachable for dialect conversion
  • Fix UNLOOP to not pop the loop stack at parse time, enabling the idiomatic UNLOOP EXIT pattern for early loop exit
  • Error on EXIT outside a word definition

Test plan

  • Translation test: EXIT inside IF/THEN emits return (test/Translation/Forth/exit.forth)
  • Negative test: EXIT outside word definition (test/Translation/Forth/exit-outside-word-error.forth)
  • Translation test: UNLOOP EXIT inside DO LOOP (test/Translation/Forth/unloop-exit.forth)
  • Pipeline test: EXIT compiles to gpu.binary (test/Pipeline/exit.forth)
  • Pipeline test: UNLOOP EXIT compiles to gpu.binary (test/Pipeline/unloop-exit.forth)
  • All 54 tests pass

Closes #24

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.

Looks good! Clean implementation that follows the established LEAVE pattern well.

A few observations:

  • UNLOOP fix is correctpop_back_val() was desyncing the loopStack, this no-op approach is right since loop control is CFG blocks + memref counter.
  • EXIT implementation mirrors LEAVE's cond_br %true → return/dead block pattern nicely. Good use of InsertionGuard.
  • Test coverage is solid — translation, negative, pipeline, and the realistic FIND-FIVE idiom.

One thing to consider as a follow-up: EXIT inside a loop without UNLOOP (e.g. DO ... EXIT ... LOOP) silently leaks the loop counter alloca. Matches standard Forth semantics where UNLOOP is the programmer's responsibility, but a diagnostic warning could be helpful down the line.

@tetsuo-cpp tetsuo-cpp merged commit e1db4aa into canon Feb 19, 2026
1 check passed
@tetsuo-cpp tetsuo-cpp deleted the exit-word branch February 19, 2026 08:13
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.

feat: implement EXIT word

1 participant

Comments