Project 1 — Struct Types - #17
Open
chances190 wants to merge 25 commits into
Open
Conversation
- Rename "tagged types" -> "aggregate types", kind -> specifier, tag -> identifier, for semantic clarity - Move types hashmap to evironment for reuse in type-checker and interpreter
# Conflicts: # src/semantic/type_checker.rs
… minic existing code
Entrega 3
add more TAC tests
… by making enums exhaustive, make variable declarations consistent without placeholders
- Rename aggregate types to user-defined types throughout - Add remove() method to Environment - Fix struct init values being zeroed at runtime - Fix match binding leaking to outer scope - Add cast runtime coercion (int<->float) - Add value equality for Struct and Enum types - Fix cast target type validation in enum Decl - Fix duplicate struct init field rejection - Fix unary parser to skip whitespace
…function parameter) to allow usage without cast
…nstead of all in one file
Closed
There was a problem hiding this comment.
Pull request overview
This PR extends MiniC with user-defined aggregate types (structs and enums) across the full pipeline (AST → parser → type checker → interpreter), adds match over enums, and introduces/expands TAC generation + CLI support alongside a large test suite refactor/split.
Changes:
- Added AST and parser support for
struct/enumdeclarations, initializers ({ .field = ... }), member access (a.b), casts, andmatch. - Updated the semantic/type-checking and runtime environment to track UDT declarations and enforce/execute struct/enum rules (including match payload binding).
- Added TAC generation coverage for UDT-related constructs and introduced
--tacin the CLI, plus many new unit/integration/CLI tests and new documentation.
Reviewed changes
Copilot reviewed 71 out of 71 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/type_checker/stdlib.rs | New type-checker tests for stdlib signature/arity rules |
| tests/type_checker/mod.rs | New type-checker test module layout |
| tests/type_checker/helpers.rs | Shared helper for parsing + type-checking snippets |
| tests/type_checker/data_types.rs | New type-checker tests for struct/enum/match/member rules |
| tests/type_checker/core.rs | Core type-checker tests split into dedicated module |
| tests/type_checker.rs | Refactors type-checker tests to module-based layout |
| tests/tac/mod.rs | New TAC test module layout |
| tests/tac/helpers.rs | Fixture-based helper for TAC tests |
| tests/tac/functions.rs | TAC tests for function calls involving UDT-expanded addresses |
| tests/tac/data_types.rs | TAC tests for struct/enum init + match + nesting |
| tests/tac.rs | New TAC test entrypoint module |
| tests/tac_gen.rs | Removes old TAC unit tests in favor of fixture-based TAC tests |
| tests/stdlib.rs | Test naming/formatting cleanup for stdlib runtime tests |
| tests/program.rs | Removes older fixture-parsing tests (replaced by new parser modules) |
| tests/parser/types.rs | New parser tests for UDT type syntax + UDT declarations |
| tests/parser/struct_init.rs | New parser tests for { .field = ... } initializer syntax |
| tests/parser/programs.rs | New parser tests for program-level parsing (incl. type decls) |
| tests/parser/mod.rs | New parser test module layout |
| tests/parser/member_access.rs | New parser tests for member access + member lvalues |
| tests/parser/match_st.rs | New parser tests for match statement syntax |
| tests/parser/literals.rs | New parser tests split out for literals |
| tests/parser/identifiers.rs | New parser tests split out for identifiers/reserved words |
| tests/parser/helpers.rs | Fixture parsing helper for parser tests |
| tests/parser/functions.rs | New parser tests split out for functions/calls |
| tests/parser/expressions.rs | New parser tests split out for expression precedence |
| tests/parser/declarations.rs | New parser tests split out for assignments/declarations |
| tests/parser/control_flow.rs | New parser tests split out for if/while |
| tests/parser/cast.rs | New parser tests for cast syntax (incl. UDT casts) |
| tests/parser/blocks.rs | New parser tests for blocks |
| tests/parser/arrays.rs | New parser tests for arrays/indexing |
| tests/parser.rs | Refactors parser tests to module-based layout |
| tests/interpreter/strings.rs | New interpreter tests for string equality/defaults |
| tests/interpreter/stdlib.rs | New interpreter tests for stdlib behavior |
| tests/interpreter/mod.rs | New interpreter test module layout |
| tests/interpreter/helpers.rs | Shared helper: parse → type-check → interpret |
| tests/interpreter/data_types.rs | New interpreter tests for struct/enum init + match + equality |
| tests/interpreter/core.rs | Core interpreter tests split into dedicated module |
| tests/interpreter/cast.rs | New interpreter tests for numeric cast behavior |
| tests/interpreter/arrays.rs | New interpreter tests for arrays and bounds errors |
| tests/interpreter.rs | Refactors interpreter tests to module-based layout |
| tests/fixtures/tac_struct.minic | Fixture: struct init + member access in TAC |
| tests/fixtures/tac_nested.minic | Fixture: nested struct/enum init in TAC |
| tests/fixtures/tac_enum.minic | Fixture: enum init + match in TAC |
| tests/fixtures/data_types.minic | Fixture: combined UDT behavior for CLI --run |
| tests/cli/tac.test | CLI shelltest for --tac output over fixtures |
| tests/cli/run.test | Extends CLI run tests to include UDT fixture execution |
| src/stdlib/mod.rs | Refactors stdlib registry initialization formatting |
| src/semantic/type_checker.rs | Adds UDT-aware type checking, match, member access, init checking |
| src/parser/types.rs | New shared type + UDT declaration parser module |
| src/parser/statements.rs | Adds match, member lvalues, and uses shared type_definition |
| src/parser/program.rs | Parses mixed top-level items (UDT decls + functions) |
| src/parser/mod.rs | Exposes new parser module(s) and re-exports UDT parser |
| src/parser/identifiers.rs | Expands reserved words + adds identifier_decl using type_definition |
| src/parser/functions.rs | Switches params/type parsing to shared type-definition/identifier_decl |
| src/parser/expressions.rs | Adds init/cast/member parsing support |
| src/main.rs | Adds --tac mode and routes into TAC codegen |
| src/lib.rs | Reorders module exports (exposes codegen) |
| src/ir/tac.rs | Adds Display impls for TAC printing |
| src/ir/ast.rs | Extends AST with UDT kinds/decls/members, member access, init, cast, match |
| src/interpreter/value.rs | Adds runtime Value variants for Struct and Enum |
| src/interpreter/mod.rs | Initializes interpreter environment with UDT declarations |
| src/interpreter/exec_stmt.rs | Executes match, supports member assignment, UDT runtime helpers |
| src/interpreter/eval_expr.rs | Evaluates member access, struct init, enum variant values, casts |
| src/environment/mod.rs | Re-exports type-decl map helpers/types |
| src/environment/env.rs | Stores shared UDT decl table in environment + lookup helpers |
| src/codegen/tac_code_gen.rs | TAC generation updates for UDT init/member/match and printing |
| docs/tac_rules.tex | Updates TAC documentation formatting/layout |
| docs/09-projects01.md | Updates project/group allocation table and milestone date |
| docs/00-guide.md | New guide for Rust + Nom + MiniC project structure |
| .vscode/launch.json | Adds debug configuration for --tac runs |
| .github/copilot-instructions.md | Adds repository coding guidance for educational clarity |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+181
to
+199
| match ty { | ||
| Type::Struct(identifier) => { | ||
| if !env.has_type_decl(&UDTKind::Struct, identifier) { | ||
| return Err(TypeError::new(format!( | ||
| "unknown struct type: {}", | ||
| identifier | ||
| ))); | ||
| } | ||
| } | ||
| Type::Enum(identifier) => { | ||
| if !env.has_type_decl(&UDTKind::Enum, identifier) { | ||
| return Err(TypeError::new(format!( | ||
| "unknown enum type: {}", | ||
| identifier | ||
| ))); | ||
| } | ||
| } | ||
| _ => {} | ||
| } |
Comment on lines
+298
to
+332
| fn assign_member( | ||
| base: &CheckedExpr, | ||
| member: &str, | ||
| val: Value, | ||
| env: &mut Environment<Value>, | ||
| ) -> Result<(), RuntimeError> { | ||
| match &base.exp { | ||
| Expr::Ident(name) => { | ||
| let current = env | ||
| .get(name) | ||
| .cloned() | ||
| .ok_or_else(|| RuntimeError::new(format!("undefined variable '{}'", name)))?; | ||
| let updated = match current { | ||
| Value::Struct { | ||
| identifier, | ||
| mut fields, | ||
| } => { | ||
| fields.insert(member.to_string(), val); | ||
| Value::Struct { identifier, fields } | ||
| } | ||
| other => { | ||
| return Err(RuntimeError::new(format!( | ||
| "cannot assign member on non-struct value: {}", | ||
| other | ||
| ))) | ||
| } | ||
| }; | ||
| env.set(name, updated); | ||
| Ok(()) | ||
| } | ||
| _ => Err(RuntimeError::new( | ||
| "member assignment currently requires a simple variable base".to_string(), | ||
| )), | ||
| } | ||
| } |
Comment on lines
+295
to
+298
| let tag_var = match &target_addr { | ||
| Address::Variable(name, _) => name.clone(), | ||
| _ => unreachable!("match target must be a variable"), | ||
| }; |
…for match statements - Introduced a new `lower_copy_to_prefix` function to streamline copying values for structs and enums in TAC generation. - Updated the handling of enum variants in match statements to use temporary bindings for payloads, preventing variable name clashes. - Added comprehensive type validation for program type references, ensuring all types used in structs, enums, and function signatures are declared. - Implemented checks for non-exhaustive match cases and duplicate match arms in the type checker. - Enhanced tests for struct and enum handling in TAC generation, including copy and assignment operations.
Refactor TAC generation for structs and enums; enhance type checking for match statements
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 7
Implementados Struct e Enum com as correções sugeridas na apresentação. Substitui #1