Skip to content

Implement switch statement type checking, interpretation, and tests#2

Open
Ypisds wants to merge 3 commits into
PauloV1:mainfrom
Ypisds:main
Open

Implement switch statement type checking, interpretation, and tests#2
Ypisds wants to merge 3 commits into
PauloV1:mainfrom
Ypisds:main

Conversation

@Ypisds

@Ypisds Ypisds commented Jun 14, 2026

Copy link
Copy Markdown

This PR completes the Switch Statement work for Project 3, covering the type checker fix, interpreter implementation, and interpreter-level tests.

Changes

1. Fix type checker switch case handling

  • Updated the switch case literal handling in the type checker.
  • Added support for rejecting unsupported case labels such as float and str.
  • Prevents Rust from failing compilation due to a non-exhaustive match.
  • Keeps the MiniC rule explicit: switch case labels must be int or bool.

2. Implement switch execution in the interpreter

  • Evaluates the switch target expression.
  • Executes the first matching case.
  • Executes the default branch when no case matches.
  • Preserves assignments to variables declared outside the switch.
  • Cleans up variables declared inside the switch scope.
  • Propagates return correctly from inside switch branches.
  • Detects duplicate case labels and reports a runtime error.

3. Add interpreter tests for switch

Added tests covering:

  • Integer switch case execution.
  • Default branch execution.
  • Boolean switch case execution.
  • Assignment to outer variables inside switch branches.
  • Runtime error for duplicate case labels.

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