Skip to content

feat: add local variables support#41

Merged
tetsuo-cpp merged 2 commits intocanonfrom
worktree-feat/local-variables
Feb 21, 2026
Merged

feat: add local variables support#41
tetsuo-cpp merged 2 commits intocanonfrom
worktree-feat/local-variables

Conversation

@tetsuo-cpp
Copy link
Owner

Summary

  • Add { a b c -- } syntax for read-only local variables in word definitions
  • Reuses existing forth.pop and forth.push_value ops (pure SSA approach, no new dialect operations)
  • Locals work across control flow (IF/ELSE/THEN, loops) since SSA values from the entry block dominate all blocks

Test plan

  • Basic local variable binding and reference test
  • Locals across IF/ELSE/THEN control flow test
  • Duplicate name error test
  • Full pipeline to gpu.binary test
  • All 88 tests pass

Closes #8

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

Clean implementation — reusing forth.pop/forth.push_value as pure SSA values is the right design. Locals map to registers on GPU for free, and entry-block SSA dominance gives you control flow support without any extra work.

Suggestions

Missing error-path tests: The code checks for param and shared memory name conflicts, but neither path has a test. Consider adding:

\ param conflict
\! kernel main
\! param DATA i64[256]
: BAD { data -- } data ;
BAD
\ shared conflict
\! kernel main
\! shared BUF i64[64]
: BAD { buf -- } buf ;
BAD

{ outside a word definition: Currently falls through to "unknown word: {" which is a confusing error. A targeted diagnostic like "local variables can only be declared inside word definitions" would be friendlier. Low priority.

Minor: Output names after -- are silently skipped without validation — { a b -- 42 } is accepted. Might be worth a code comment noting this is intentional (documentation-only, per ANS Forth).

Everything else looks good. The reverse-order pop loop, localVars.clear() cleanup, and conflict checks are all correct.

@tetsuo-cpp tetsuo-cpp merged commit 6aa7be5 into canon Feb 21, 2026
1 check passed
@tetsuo-cpp tetsuo-cpp deleted the worktree-feat/local-variables 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.

Local variables support

1 participant