Skip to content

Super Fat refactoring No Cap!#145

Merged
SIGMazer merged 5 commits intomainfrom
refactor/ast
Nov 11, 2025
Merged

Super Fat refactoring No Cap!#145
SIGMazer merged 5 commits intomainfrom
refactor/ast

Conversation

@SIGMazer
Copy link
Member

@SIGMazer SIGMazer commented Nov 5, 2025

Description

This pull request introduces a major refactor to the Brainrot language implementation by adding a visitor-based architecture for semantic analysis and interpretation, as well as support for built-in functions. The changes lay the foundation for improved error reporting, modularity, and extensibility. Additionally, new semantic error test cases are included to validate the enhanced semantic analysis.

Visitor-based architecture and semantic analysis:

  • Added semantic_analyzer.h and corresponding visitor implementation, enabling semantic analysis with error reporting, type checking, and scope validation. This includes new error types, error structures, and utility/type-checking functions.
  • Updated lang.y to run semantic analysis after parsing and before execution; execution now only proceeds if semantic analysis passes. Also improved resource cleanup and file handling. [1] [2] [3]

Interpreter and execution:

  • Added interpreter.h and interpreter.c, implementing a visitor-based interpreter for AST execution, with modular visitor methods for expressions and statements, and support for scope management and return/break flags. [1] [2]
  • Extended the ASTNode struct to include a line_number field for precise error reporting during semantic analysis and interpretation.

Built-in functions support:

  • Added stdrot.h to define and manage built-in Brainrot functions, including function name checks and execution routing for built-in calls.
  • Updated the build system (Makefile) to include new source files for visitor, semantic analyzer, interpreter, and built-in functions.

Testing and validation:

  • Added new semantic error test cases to test_cases/semantic_error_const.brainrot, test_cases/semantic_error_function_redef.brainrot, test_cases/semantic_error_scope.brainrot, and test_cases/semantic_error_type_mismatch.brainrot to validate the semantic analysis phase. [1] [2] [3] [4]

Related Issue

Fixes #18, #139

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Performance improvement
  • Refactor

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have documented my changes in the code or documentation
  • I have added tests that prove my changes work (if applicable)
  • I have run the unit tests locally
  • I have run the valgrind memory tests locally
  • All new and existing tests pass

@SIGMazer SIGMazer requested a review from leo-aa88 November 5, 2025 09:20
Introduce a visitor pattern for semantic analysis and interpretation.
Refactor built-in function handling into stdrot.c/h. Add semantic error
reporting and test cases for const assignment, function redefinition,
scope, and type mismatch errors. Update Makefile and main to support
new semantic and interpreter phases. Update expected test results.

Signed-off-by: SIGMazer <mazinasd7@gmail.com>
@leo-aa88
Copy link
Member

leo-aa88 commented Nov 5, 2025

image 💀

@leo-aa88
Copy link
Member

leo-aa88 commented Nov 5, 2025

this is what I call a CHUNGUS refactor
image

@SIGMazer SIGMazer linked an issue Nov 6, 2025 that may be closed by this pull request
Refactor semantic analyzer to use a unified scope-aware symbol table for
variable and function declarations. Implement two-phase analysis: first
collect declarations, then perform semantic checks with scope tracking.
Improve error reporting for undefined variables, out-of-scope access,
const assignment, and function redefinition. Update tests to match new
error messages and line numbers.

Signed-off-by: SIGMazer <mazinasd7@gmail.com>
Remove outdated and redundant comments from semantic analyzer and related
code. Simplify phase descriptions and clarify scope handling. This improves
readability and maintains semantic analysis logic without functional changes.
```

Signed-off-by: SIGMazer <mazinasd7@gmail.com>
Refactor interpreter to use the visitor pattern for AST execution, replacing
direct statement execution in function calls and control flow. Fix double
evaluation bugs for increment/decrement and assignment nodes by skipping
auto-visits in visitor.c. Improve array access robustness and type handling
in ast.c and interpreter.c. Update expected test results for type mismatch
and scope errors.

Signed-off-by: SIGMazer <mazinasd7@gmail.com>
Refactored interpreter visitor functions to no longer allocate memory for
literal and identifier values, as these are only needed for side effects
and not expression evaluation. Updated function call and sizeof handling
to match this approach. Introduced a global interpreter pointer for
cleanup in main and ensured proper freeing in the cleanup routine.
Fixed memory leak for string variables in hash map cleanup.

Signed-off-by: SIGMazer <mazinasd7@gmail.com>

yapping("%d", y); 🚽 This should fail - y is out of scope
bussin 0;
} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline

@SIGMazer SIGMazer merged commit 9f9db06 into main Nov 11, 2025
2 checks passed
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.

Type checker Enhanced error handling

2 participants