Project 3 - TAC code generation for Switch Statement - #16
Open
PauloV1 wants to merge 17 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.
Implement type checker and interpreter for Switch statement
- Implement semantic check to reject duplicate cases in a switch block - fix: make literal pattern matching exhaustive by returning a TypeError for unsupported types
- Implement runtime/evaluation check for duplicate labels in switch cases - fix: import from AST module to resolve scope compilation error
tac code generation
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.
Descrição Geral
Este PR implementa o suporte à geração de código intermediário de três endereços (TAC) para o Switch Statement na linguagem MiniC. A implementação integra o Switch com a pipeline de geração de código e adiciona testes de integração correspondentes.
Detalhes das Implementações
1. Geração de Código TAC (
src/codegen/tac_code_gen.rs)Tradução da estrutura condicional
switchpara código intermediário TAC:target): A expressãotargeté avaliada exatamente uma vez e seu endereço temporário (target_addr) é armazenado, prevenindo múltiplos efeitos colaterais caso a expressão possua chamadas de função ou operações complexas.ConditionalJMPRelationalde igualdade (Operator::EQ) que desviam o fluxo para o rótulo do caso correspondente caso o valor avaliado do target coincida com o literal constante do caso.JMPincondicional no final da cadeia de verificações direcionada ao rótulo do blocodefault.casetermina com um desvio incondicionalJMPpara o rótulo de encerramento (label_end), assegurando que a execução de um caso não passe para o seguinte.Cobertura de Testes
Os testes de integração cobrem a corretude das instruções TAC e rótulos de fluxo gerados.
Testes de Geração de Código TAC (
tests/tac_gen.rs)test_if_else_with_relational_condition: Correção de teste antigo para se adequar ao comportamento atual do gerador de condicionais relacionais da pipeline.test_switch_int_tac: Validação da geração de código TAC para literais inteiras.test_switch_bool_tac: Validação de geração de código para literais booleanas.test_switch_complex_target_tac: Validação de geração com expressão de alvo complexa (soma aritméticax + y), assegurando a avaliação correta de expressões antes do desvio do switch.