Skip to content

Implement function parameters, logical operators (&&, ||), and loops (while, for)#9

Merged
vmillet-dev merged 10 commits intomainfrom
devin/1753476757-function-params-logical-loops
Jul 27, 2025
Merged

Implement function parameters, logical operators (&&, ||), and loops (while, for)#9
vmillet-dev merged 10 commits intomainfrom
devin/1753476757-function-params-logical-loops

Conversation

@devin-ai-integration
Copy link
Copy Markdown
Contributor

Implement function parameters, logical operators (&&, ||), and loops (while, for)

Summary

This PR implements three major language features for the Mini-C compiler's IR compilation pipeline:

  1. Function Parameters: Functions can now accept typed parameters (e.g., int add(int a, int b))
  2. Logical Operators: Added && and || with proper short-circuit evaluation
  3. Loop Constructs: Implemented while and for loops with break and continue support

The implementation spans the entire compilation pipeline from lexer tokens to IR generation, focusing exclusively on the IR compilation mode (cargo run -- --ir) as requested.

Review & Testing Checklist for Human

  • Test logical operator short-circuit behavior - Verify false && expensive_call() doesn't execute the right side, and true || expensive_call() doesn't execute the right side
  • Test complex nested loops with break/continue - Ensure break/continue target the correct loop level and don't cause infinite loops or incorrect jumps
  • Test function parameters with different types and counts - Verify functions work with 0, 1, and multiple parameters of different types (int, float, etc.)
  • Test edge cases - Try break/continue outside loops (should error), deeply nested structures, and complex logical expressions like (a && b) || (c && d)
  • End-to-end compilation testing - Compile and run actual Mini-C programs using the new features to ensure the generated assembly executes correctly

Diagram

%%{ init : { "theme" : "default" }}%%
graph TD
    subgraph Legend
        L1["Major Edit"]:::major-edit
        L2["Minor Edit"]:::minor-edit  
        L3["Context/No Edit"]:::context
    end

    TokenTypes["src/lexer/token.rs<br/>TokenType definitions"]:::context
    AST["src/parser/ast.rs<br/>AST node definitions"]:::major-edit
    Parser["src/parser/parser.rs<br/>Parsing logic + tests"]:::major-edit
    Semantic["src/semantic/lifetime_simple.rs<br/>Semantic analysis"]:::minor-edit
    IRGen["src/ir/generator.rs<br/>IR generation"]:::major-edit
    IRCodegen["src/codegen/ir_codegen.rs<br/>IR to assembly"]:::context
    IntegrationTests["tests/integration_tests.rs<br/>Integration tests"]:::major-edit

    TokenTypes --> Parser
    AST --> Parser
    Parser --> Semantic
    Parser --> IRGen
    IRGen --> IRCodegen
    IRGen --> IntegrationTests

    classDef major-edit fill:#90EE90
    classDef minor-edit fill:#87CEEB  
    classDef context fill:#FFFFFF
Loading

Notes

  • IR-Only Implementation: Changes focus exclusively on IR compilation pipeline as requested - direct assembly generation path unchanged
  • Comprehensive Testing: Added 58 unit tests + 26 integration tests, all passing
  • Short-Circuit Evaluation: Logical operators use proper branching to avoid evaluating unnecessary expressions
  • Loop Stack Management: Break/continue statements use a stack-based approach to target the correct loop level
  • Session Info: Implemented by Devin AI for @vmillet-dev - Link to Devin run

⚠️ Complexity Warning: This is a substantial change involving complex control flow logic. The short-circuit evaluation and loop management code should be carefully reviewed for edge cases and correctness.

…(while, for)

- Add While, For, Break, Continue AST nodes to support loop constructs
- Implement function parameter parsing in parser with Parameter struct
- Add logical operator precedence handling for && and || operators
- Implement loop statement parsing for while, for, break, continue
- Add IR generation for loop control flow with proper label management
- Implement short-circuit evaluation for logical operators in IR
- Add comprehensive unit and integration tests for all new features
- Support nested loops and complex logical expressions
- All tests pass (58 unit + 26 integration tests)

Co-Authored-By: Valentin  Millet <valentin.millet39@gmail.com>
@devin-ai-integration
Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@vmillet-dev vmillet-dev force-pushed the devin/1753476757-function-params-logical-loops branch from 472e74e to fbdea63 Compare July 27, 2025 18:51
@vmillet-dev vmillet-dev merged commit 269a311 into main Jul 27, 2025
8 of 9 checks passed
@vmillet-dev vmillet-dev deleted the devin/1753476757-function-params-logical-loops branch July 27, 2025 18:55
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