Project 3 - Switch Statement#4
Open
PauloV1 wants to merge 7 commits into
Open
Conversation
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Grupo
Summary
Introduce full syntactic support for
switchstatements, including AST integration, parser validation rules, and comprehensive test coverage.This PR implements the
switchconstruct end-to-end at the parsing level, enforcing structural constraints such as mandatorydefault, at least onecase, 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)Statement::Switchvariant(Literal, Vec<Statement>)Parser (
statements.rs)casedefaultcase/defaultblock contains ≥1 statementInt) and boolean (Bool) literals in case conditionsType Checker & Interpreter
todo!()stubs forStatement::Switchto satisfy exhaustive matchingTooling & Environment
scripts/minicwrapper for easier execution.envrcfor automatic environment setup viadirenvminiccommand as documented in the READMETests (
parser.rs)The following tests validate parsing correctness, structure, nesting, and error handling:
Valid parsing scenarios:
test_switch_statementtest_switch_multiple_casestest_switch_multiple_statementstest_switch_multiple_cases_and_statementstest_switch_boolean_casestest_switch_whitespaceStructural integration:
test_switch_in_functiontest_switch_in_iftest_switch_in_whileError handling:
test_switch_multiple_defaults_errtest_switch_no_default_errtest_switch_no_cases_errtest_switch_non_expression_errInvalid case definitions:
test_switch_invalid_case_literalInvalid syntax detection:
test_switch_invalid_syntaxNext Steps (out of scope for this PR)