Skip to content

feat: add comparison operators <> <= >= and BEGIN/WHILE/REPEAT loop#15

Merged
tetsuo-cpp merged 3 commits intocanonfrom
feat/comparison-while-repeat
Feb 16, 2026
Merged

feat: add comparison operators <> <= >= and BEGIN/WHILE/REPEAT loop#15
tetsuo-cpp merged 3 commits intocanonfrom
feat/comparison-while-repeat

Conversation

@tetsuo-cpp
Copy link
Owner

Summary

  • Add missing comparison operators <> (not-equal), <= (less-or-equal), >= (greater-or-equal)
  • Implement BEGIN/WHILE/REPEAT pre-test loop as a two-region op that lowers to scf.while
  • Update CLAUDE.md supported words list

Test plan

  • Extended comparison-ops.forth translation test with <>, <=, >=
  • Extended comparison.mlir conversion test with ne, le, ge predicates
  • New begin-while-repeat.forth translation test
  • New begin-while-repeat.mlir conversion test
  • New begin-while-repeat.forth full pipeline test
  • All 38 tests pass

Closes #9

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.

PR #15 Review: feat: add comparison operators <> <= >= and BEGIN/WHILE/REPEAT loop

Overview

This PR adds two features:

  1. Comparison operators <> (ne), <= (le), >= (ge) — straightforward additions following existing patterns
  2. BEGIN/WHILE/REPEAT pre-test loop — a new two-region op that lowers to scf.while

Code Quality & Correctness

Comparison operators — Clean and correct. Uses the existing BinaryCmpOpConversion template with the right arith::CmpIPredicate variants (ne, sle, sge). Signed predicates are consistent with the existing slt/sgt usage.

BEGIN/WHILE/REPEAT lowering — Well-structured. The two-region approach (condition + body) maps naturally to scf.while's before/after regions. The conversion pattern correctly follows the established convertRegionTypes + inlineRegionBefore + mergeBlocks pattern from BeginUntilOpConversion.

YieldOp while_cond attribute — Clever reuse. Instead of creating a separate yield op, the UnitAttr flag distinguishes WHILE (continue on non-zero) from UNTIL (exit on non-zero) semantics in a single conversion pattern.

Issues

  1. isWhileLoop doesn't track IF/THEN nesting (ForthToMLIR.cpp:547). It only increments depth on BEGIN/DO and decrements on UNTIL/LOOP/REPEAT. A WHILE word used inside an IF/ELSE/THEN block within a BEGIN/UNTIL loop would be incorrectly treated as the loop's own WHILE. In practice this is unlikely since standard Forth doesn't nest WHILE inside IF within BEGIN, so this is probably fine for real-world code — just worth noting.

  2. YieldOpConversion comment is now stale (ForthToMemRef.cpp:626-627). The doc comment still says "Context-aware: inside scf.while's before region (from BeginUntilOp)" — it should mention BeginWhileRepeatOp as well.

Suggestions

  • Minor: The getSuccessorRegions implementation in ForthDialect.cpp looks correct for the three cases (parent → condition, condition → body/exit, body → condition). Well done matching the scf.while semantics.

  • Consider: Adding a nested loop test case, e.g. BEGIN ... BEGIN ... UNTIL ... WHILE ... REPEAT to verify the lookahead handles nesting correctly.

Test Coverage

Good coverage across all three test tiers:

  • Translation tests verify parsing (begin-while-repeat.forth, comparison-ops.forth)
  • Conversion tests verify MemRef lowering (begin-while-repeat.mlir, comparison.mlir)
  • Pipeline test verifies full compilation to GPU binary (begin-while-repeat.forth)

Summary

Solid PR. The comparison operators are trivially correct. The BEGIN/WHILE/REPEAT implementation is well-designed — reusing YieldOp with a while_cond attribute is a clean approach that avoids duplicating the yield conversion logic. The two substantive notes are the stale comment and the IF/THEN-nesting edge case in the lookahead (low risk in practice). Looks good to merge after addressing the comment.

@tetsuo-cpp tetsuo-cpp force-pushed the feat/comparison-while-repeat branch from 8558561 to 5fc00cc Compare February 16, 2026 00:45
@tetsuo-cpp tetsuo-cpp merged commit 02e9abf into canon Feb 16, 2026
1 check passed
@tetsuo-cpp tetsuo-cpp deleted the feat/comparison-while-repeat branch February 16, 2026 01:22
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.

Additional comparison operators and WHILE/REPEAT loop

1 participant