-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticAnalysis.hpp
More file actions
81 lines (70 loc) · 2.73 KB
/
Copy pathSemanticAnalysis.hpp
File metadata and controls
81 lines (70 loc) · 2.73 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include "Core/Extras/ErrorManager/ErrorManager.hpp"
#include "Core/Frontend/Nodes.hpp"
#include "Core/Frontend/Token.hpp"
#include "Core/Frontend/Orchestrator/Orchestrator.hpp"
struct Program;
// Semantic Analysis is a part of Frontend in Compiler responsible for logical part of the code.
struct SemanticAnalysis {
// ErrorManager is used to report errors
ErrorManager* errorManager = nullptr;
// Main entry
void analyzeProgram(Program& program);
// Per-node analyzers
void analyzeModule(ModuleNode* module);
void analyzeBlock(BlockNode* block);
void analyzeDeclaration(DeclarationNode* node);
void analyzeAssignment(AssignmentNode* node);
void analyzeFunction(FunctionNode* node);
void analyzeClass(ClassNode* node);
void analyzeEnum(EnumNode* node);
void analyzeInterface(InterfaceNode* node);
void analyzeCallExpression(CallExpressionNode* node);
void analyzeDecorator(DecoratorNode* node);
void analyzeIf(IfNode* node);
void analyzeSwitch(SwitchNode* node);
void analyzeWhile(WhileLoopNode* node);
void analyzeFor(ForLoopNode* node);
void analyzeTryCatch(TryCatchNode* node);
void analyzeReturn(ReturnStatementNode* node);
void analyzeThrow(ThrowStatementNode* node);
void analyzeBreak(BreakStatementNode* node);
void analyzeContinue(ContinueStatementNode* node);
void analyzeLambda(LambdaNode* node);
void analyzeExpression(ASTNode* node); // dispatcher for expressions only
void analyzeStatement(ASTNode* node);
ResolvedType resolveType(RawTypeNode* type);
private:
struct Symbol {
enum class Kind { Variable, Function, Parameter, Class, Enum, Interface, Decorator };
Kind kind;
bool isConst = false;
std::string filePath;
int line = 0, column = 0;
};
std::vector<std::unordered_map<std::string, Symbol>> scopes;
int loopDepth = 0;
int functionDepth = 0;
// Scope helpers
void pushScope();
void popScope();
bool declareName(const std::string& name, Symbol symbol, ASTNode* node);
Symbol* findName(const std::string& name);
// just helpers
bool match(ASTNode* node, ASTNodeType type) {
if (node->type == type) return true;
return false;
}
// Recursively goes through MemberAccessNode until it finds the first ever parent VariableNode.
// if VariableNode is passed it returns it; used for analyzeAssignment().
ASTNode* getRootVariable(ASTNode* node) {
if (match(node, ASTNodeType::MemberAccess)) {
auto* ma = static_cast<MemberAccessNode*>(node);
return getRootVariable(ma->parent.get());
}
return node; // parent found
}
};