Skip to content

Implement block scopes and control flow statements (if/else, while, and for loops)#5

Open
hand-burger wants to merge 6 commits into
AdamHameed:mainfrom
hand-burger:feature/control-flow
Open

Implement block scopes and control flow statements (if/else, while, and for loops)#5
hand-burger wants to merge 6 commits into
AdamHameed:mainfrom
hand-burger:feature/control-flow

Conversation

@hand-burger

Copy link
Copy Markdown

Summary

This PR adds support for block scopes and control flow statements in the RCC compiler. It introduces nested block statements ( { ... } ), if / else conditionals, while loops, and C-style for loops.

Changes

  1. Lexer & AST:

• Added tokenization support for the new keywords: if , else , while , and for .
• Extended the Statement AST enum with Block , If , While , and For variants.

  1. Parser:

• Implemented recursive parsing of block statements containing lists of nested statements.
• Added parser support for conditional constructs: if (cond) stmt else stmt .
• Added parser support for loops: while (cond) stmt and for (init; cond; post) stmt .

  1. LLVM Code Generation:

• Redesigned the variable mapping system from a flat table to a vector stack representing block-local scopes to correctly support block-level lexical scoping.
• Migrated code generation to a single-exit function design with a return_bb basic block, avoiding duplicate terminator instructions inside branches and loops.
• Implemented basic block routing and branch logic for if / else , while loops, and for loops.

  1. Tests & CI:

• Fixed a nested if warning in src/main.rs to satisfy Clippy CI checks.
• Adjusted codegen unit test assertions to match the single-exit block generation style.
• Added end-to-end integration tests verifying block scoping, if / else evaluation, while loop iteration, for loop step execution, and deeply nested loops and conditionals.

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