blob: 630a2672402f46571e292d289980a95f38f675f5 (
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
47
48
49
50
51
52
53
54
|
#pragma once
#include "ast_node.hpp"
#include <string>
#include <vector>
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;
};
SemanticAnalyzer::SemanticAnalyzer() : symbol_table(), errors(), warnings(), current_function(nullptr), current_function_return_type(), has_main_functions(false) { }
|