Skip to content

Project 2 - For Statement#6

Open
Mend25 wants to merge 4 commits into
rbonifacio:mainfrom
iianmelo:main
Open

Project 2 - For Statement#6
Mend25 wants to merge 4 commits into
rbonifacio:mainfrom
iianmelo:main

Conversation

@Mend25
Copy link
Copy Markdown

@Mend25 Mend25 commented Apr 21, 2026

Grupo

  • Victor Mendonça Aguiar
  • Ian Medeiros Melo
  • Guilherme Montenegro de Albuquerque
  • João Victor Fellows Rabêlo
  • Rafael Alves de Azevedo

Summary

Introduce full syntactic support for C-style for loops in the MiniC language, including AST integration, parser support for optional header clauses, keyword reservation, and supporting documentation/specification.

This PR implements the for construct at the parsing level, adding end-to-end syntactic support in MiniC through a new Statement::For AST variant and parser logic capable of handling optional initialization, condition, and update clauses. It also reserves for as a language keyword to prevent identifier conflicts.

Type checking and interpretation for for loops are intentionally left unimplemented at this stage, with explicit errors added to make that limitation clear until Milestone 2. In addition, the PR includes detailed design documentation, requirements, scenarios, and task breakdowns, while remaining fully backward compatible with the existing language.


Changes Made

AST (ast.rs)

  • Add the Statement::For { init, cond, update, body } variant
  • Represent the for loop with:
    • an optional initialization clause
    • an optional condition clause
    • an optional update clause
    • a mandatory block body

Parser (statements.rs)

  • Add parsing support for C-style for statements

  • Supported syntax:

    • for (init; cond; update) block
  • Header clause rules:

    • init is optional
    • cond is optional
    • update is optional
  • Add helper parsers for:

    • declarations without trailing semicolons
    • assignments without trailing semicolons
  • Update statement dispatch order so for_statement is parsed before declaration and assignment

  • Update grammar documentation and parser comments to reflect the new syntax


Language Rules

  • Reserve for as a keyword
  • Prevent for from being used as an identifier
  • Keep the change backward compatible with existing programs

Specification, Design & Planning

  • Add detailed openspec documentation covering:
    • design rationale
    • requirements
    • supported scenarios
    • parsing expectations
    • AST expectations
    • implementation planning and task breakdown

Tooling / Infra

  • Refactor flake.nix to simplify the dev shell definition
  • No functional changes to the build or development environment

iianmelo and others added 4 commits April 20, 2026 14:14
- Added a new `Statement::For` variant to the AST for three-part `for` loops.
- Implemented a parser for `for` statements, allowing declaration or assignment in the initialization clause and requiring a block body.
- Updated the statement dispatcher to recognize `for` before other statements.
- Reserved the `for` keyword to prevent its use as an identifier.
- Added integration tests covering various `for` statement scenarios, including valid and invalid forms.

This change lays the groundwork for future type checking and execution semantics in subsequent milestones.
- Added multiple tests to validate the parser's handling of `for` statements, including checks for empty clauses and complex conditions.
- Introduced a new fixture file `parser_for_loop.minic` to test a complete program with a `for` loop.
- Updated existing tests to ensure correct parsing and structure of `for` statements within various contexts, such as inside `while` loops and with return statements.

These enhancements improve the robustness of the parser and ensure compliance with the defined language specifications.
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.

3 participants