Skip to content

Project 3 - Switch Statement#4

Open
PauloV1 wants to merge 7 commits into
rbonifacio:mainfrom
PauloV1:main
Open

Project 3 - Switch Statement#4
PauloV1 wants to merge 7 commits into
rbonifacio:mainfrom
PauloV1:main

Conversation

@PauloV1
Copy link
Copy Markdown

@PauloV1 PauloV1 commented Apr 21, 2026

Grupo

  • Joana D'arc
  • Juliana Silva
  • Leandro Luiz
  • Paulo Vitor
  • Thiago Henrique

Summary

Introduce full syntactic support for switch statements, including AST integration, parser validation rules, and comprehensive test coverage.

This PR implements the switch construct end-to-end at the parsing level, enforcing structural constraints such as mandatory default, at least one case, and valid literal conditions. It also includes extensive tests covering valid scenarios, nesting, and failure cases to ensure parser robustness.

Additionally, it introduces minor developer tooling improvements to simplify local execution of the project.


Changes Made

AST (ast.rs)

  • Add the Statement::Switch variant
  • Represent the switch with:
    • a target expression
    • a list of cases (Literal, Vec<Statement>)
    • a mandatory default block

Parser (statements.rs)

  • Add parsing support for switch statements
  • Supported syntax:
    • switch_stmt = "switch" expr "{" case+ default "}" ;
    • case = "case" literal ":" stmt+ ;
    • default = "default" ":" stmt+ ;
  • Constraints enforced:
    • At least one case
    • Exactly one mandatory default
    • Each case/default block contains ≥1 statement
  • Support integer (Int) and boolean (Bool) literals in case conditions

Type Checker & Interpreter

  • Add todo!() stubs for Statement::Switch to satisfy exhaustive matching
  • Keep the project compiling while semantic analysis and evaluation are implemented later

Tooling & Environment

  • Add scripts/minic wrapper for easier execution
  • Add .envrc for automatic environment setup via direnv
  • Enable local usage of the minic command as documented in the README

Tests (parser.rs)

The following tests validate parsing correctness, structure, nesting, and error handling:

  • Valid parsing scenarios:

    • test_switch_statement
    • test_switch_multiple_cases
    • test_switch_multiple_statements
    • test_switch_multiple_cases_and_statements
    • test_switch_boolean_cases
    • test_switch_whitespace
  • Structural integration:

    • test_switch_in_function
    • test_switch_in_if
    • test_switch_in_while
  • Error handling:

    • test_switch_multiple_defaults_err
    • test_switch_no_default_err
    • test_switch_no_cases_err
    • test_switch_non_expression_err
  • Invalid case definitions:

    • test_switch_invalid_case_literal
  • Invalid syntax detection:

    • test_switch_invalid_syntax

Next Steps (out of scope for this PR)

  • Add type checking support for the switch statement
  • Add interpreter support for the switch statement

PauloV1 added 7 commits April 16, 2026 23:37
- Add scripts/minic wrapper script for convenient access to mini_c binary
- Add .envrc configuration for automatic direnv activation
- Allows using 'minic' command as documented in README
Introduce the  variant in  to represent switch-case control flow, including a target expression, literal cases with their bodies, and a default branch.
Parse switch statements in statements.rs, including the target expression, one or more case branches (integer and boolean literals), and a mandatory default block. Ensure each case and default contains at least one statement.
Add todo!() stubs for Statement::Switch in both type checking and evaluation phases to satisfy exhaustive pattern matching and keep the project compiling.
Replaced the mutable closures case_parse and default_parse with declarative nom combinators (tuple and map) inside the switch parser. This removes the need for mutable state in sub-parsers and makes the parsing logic more idiomatic and cohesive.
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