bl-compiler

compiler for the bl programming language.
Log | Files | Refs | README

semantic_analyzer.hpp (1692B)


      1 #pragma once
      2 
      3 class SemanticAnalyzer {
      4 public:
      5     explicit SemanticAnalyzer();
      6     bool analyze(ASTNode* ast);
      7     std::vector<Error> getErrors();
      8     std::vector<Error> getWarnings();
      9     bool hasErrors();
     10 
     11     // Main visitor method
     12     std::string visit(ASTNode* ast);
     13 
     14     // Visitor methods for program structure
     15     std::string visitProgram(ProgramNode* node);
     16     std::string visitFunctionDecl(FunctionDeclNode* node);
     17     std::string visitParameter(ParameterNode*);
     18 
     19     // Visitor methods for statements
     20     std::string visitVarDeclaration(VarDeclNode* node);
     21     std::string visitAssignment(AssignmentNode* node);
     22     std::string visitIfStatement(IfStatementNode* node);
     23     std::string visitWhileStatement(WhileStatementNode* node);
     24     std::string visitForStatement(ForStatementNode* node);
     25     std::string visitReturnStatement(ReturnStatementNode* node);
     26     std::String visitExpressionStatement(ExpressionStatementNode* node);
     27     
     28     // Visitor methods for expressions
     29     std::string visitBinaryExpression(BinaryExprNode* node);
     30     std::string visitUnaryExpression(UnaryExprNode* node);
     31     std::string visitFunctionCall(FunctionCallNode* node);
     32     std::string visitIdentifier(IdentifierNode* node);
     33     std::string visitLiteral(LiteralNode* node);
     34     
     35     // Type checking helper methods
     36     bool checkTypeCompatibility(std::string target, std::string source);
     37     std::string inferBinaryOpType(std::string op, std::string left, std::string right);
     38 
     39 private:
     40     SymbolTable symbol_table;
     41     std::vector<Error> errors;
     42     std::vector<Error> warnings;
     43     FunctionDeclNode* current_function;
     44     std::string current_function_return_type;
     45     bool has_main_function;
     46 };