-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantic.hpp
More file actions
55 lines (45 loc) · 1.44 KB
/
semantic.hpp
File metadata and controls
55 lines (45 loc) · 1.44 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
#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include "ast.hpp"
struct SemanticResult {
bool ok;
std::vector<std::string> errors;
std::vector<std::string> warnings;
std::vector<std::string> optimizations;
SemanticResult() : ok(true) {}
void addError(const std::string& s) {
errors.push_back(s);
ok = false;
}
};
class SemanticAnalyzer {
public:
SemanticAnalyzer();
SemanticResult analyze(AST::Program* p);
private:
std::unordered_map<std::string, AST::ClassDecl*> classes;
AST::ClassDecl* curClass;
AST::MethodDecl* curMethod;
std::vector<std::unordered_map<std::string, std::string>> localsStack;
SemanticResult result;
std::vector<std::string> declaredLocals;
std::vector<std::string> usedLocals;
void indexClasses(AST::Program* p);
void analyzeClass(AST::ClassDecl* c);
void analyzeFields(AST::ClassDecl* c);
void analyzeCtor(AST::CtorDecl* c);
void analyzeMethod(AST::MethodDecl* m);
void analyzeBlock(AST::Block* b);
void analyzeStmt(AST::Stmt*& s);
void analyzeExpr(AST::Expr*& e);
std::string typeOfExpr(AST::Expr* e);
void pushScope();
void popScope();
void declareLocal(const std::string& name, const std::string& type);
void markUsed(const std::string& name);
bool foldConstantsInExpr(AST::Expr*& e);
void simplifyIf(AST::Stmt*& s);
void removeUnreachableInBlock(AST::Block* b);
};