Skip to content

feat: add J/K loop index words and nested control flow tests#19

Merged
tetsuo-cpp merged 2 commits intocanonfrom
feat/nested-control-flow-j-k
Feb 16, 2026
Merged

feat: add J/K loop index words and nested control flow tests#19
tetsuo-cpp merged 2 commits intocanonfrom
feat/nested-control-flow-j-k

Conversation

@tetsuo-cpp
Copy link
Owner

Summary

  • Add depth attribute to Forth_LoopIndexOp to support J (depth=1) and K (depth=2) for accessing outer loop indices in nested DO/LOOP
  • Parser validates nesting depth; conversion walks up N enclosing scf.for ops to find the target induction variable
  • Add comprehensive nested control flow tests (translation, conversion, pipeline)

Related to #16

Test plan

  • All 41 tests pass (38 existing + 3 new)
  • Manual smoke test: 3 0 DO 4 0 DO J I + LOOP LOOP through translate and convert

@tetsuo-cpp tetsuo-cpp changed the title Add J/K loop index words and nested control flow tests feat: add J/K loop index words and nested control flow tests Feb 16, 2026
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 #19 Review: Add J/K Loop Index Words and Nested Control Flow Tests

Overview

Clean, focused PR that extends forth.loop_index to support accessing outer loop induction variables via J (depth=1) and K (depth=2), matching standard Forth semantics. Adds a depth attribute to the op, validates nesting depth at parse time, and walks up enclosing scf.for ops during conversion. Comprehensive tests at all three levels (translation, conversion, pipeline).

Code Quality

Well done:

  • Minimal diff — changes are focused and avoid unnecessary refactoring
  • doLoopDepth tracking in the parser correctly validates nesting before emitting the op, with good error messages
  • The conversion loop walking up scf.for ops is correct and straightforward
  • Test coverage is thorough: translation tests cover nested IF, IF-in-DO, nested DO with J, triple-nested DO with K, and mixed control flow; conversion tests verify the lowered IR; pipeline tests verify end-to-end through GPU
  • DefaultValuedAttr<I64Attr, "0"> means existing depth=0 (I) cases don't need the attribute printed, preserving backward compatibility in IR

Suggestions

  1. Op class change from Forth_StackOpBase to Forth_Op (ForthOps.td:532): This is because Forth_StackOpBase doesn't support extra attributes. Fine for now — if more ops eventually need optional attributes, a parameterized variant of the base class could help. No action needed.

  2. Consider a negative test for J/K outside sufficient nesting: The translation tests exercise the happy path. A not + FileCheck test verifying the error diagnostic would be a nice addition:

    \ RUN: not %warpforth-translate --forth-to-mlir %s 2>&1 | FileCheck %s
    \ CHECK: 'J' requires 2 nested DO/LOOP(s)
    0 DO J LOOP
    
  3. Pipeline test specificity (test/Pipeline/nested-control-flow.forth): The MID checks are fairly loose (cf.br, cf.cond_br, gpu.return) — they mostly verify the intermediate doesn't crash rather than checking structure. Pragmatic for a pipeline test, just flagging it.

Potential Issues

None found. The getParentOfType walk is the standard MLIR pattern. Parser validation ensures depth is always valid by the time we reach conversion. DefaultValuedAttr makes this fully backward-compatible.

Verdict

Looks good to merge. The one suggestion worth acting on is adding a negative test for J/K at insufficient nesting depth.

@tetsuo-cpp tetsuo-cpp merged commit 4ce6559 into canon Feb 16, 2026
1 check passed
@tetsuo-cpp tetsuo-cpp deleted the feat/nested-control-flow-j-k branch February 16, 2026 05:33
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.

1 participant