aboutsummaryrefslogtreecommitdiff
path: root/include/semantic/semantic_analyzer.hpp
blob: 0d565996be705398e031ac9fc6b96622fc0f6702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once

class SemanticAnalyzer {
public:
    explicit SemanticAnalyzer();
    bool analyze(ASTNode* ast);
    std::vector<Error> getErrors();
    std::vector<Error> getWarnings();
    bool hasErrors();

    // Main visitor method
    std::string visit(ASTNode* ast);

    // Visitor methods for program structure
    std::string visitProgram(ProgramNode* node);
    std::string visitFunctionDecl(FunctionDeclNode* node);
    std::string visitParameter(ParameterNode*);

    // Visitor methods for statements
    std::string visitVarDeclaration(VarDeclNode* node);
    std::string visitAssignment(AssignmentNode* node);
    std::string visitIfStatement(IfStatementNode* node);
    std::string visitWhileStatement(WhileStatementNode* node);
    std::string visitForStatement(ForStatementNode* node);
    std::string visitReturnStatement(ReturnStatementNode* node);
    std::String visitExpressionStatement(ExpressionStatementNode* node);
    
    // Visitor methods for expressions
    std::string visitBinaryExpression(BinaryExprNode* node);
    std::string visitUnaryExpression(UnaryExprNode* node);
    std::string visitFunctionCall(FunctionCallNode* node);
    std::string visitIdentifier(IdentifierNode* node);
    std::string visitLiteral(LiteralNode* node);
    
    // Type checking helper methods
    bool checkTypeCompatibility(std::string target, std::string source);
    std::string inferBinaryOpType(std::string op, std::string left, std::string right);

private:
    SymbolTable symbol_table;
    std::vector<Error> errors;
    std::vector<Error> warnings;
    FunctionDeclNode* current_function;
    std::string current_function_return_type;
    bool has_main_function;
};