-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.hpp
128 lines (89 loc) · 2.82 KB
/
ast.hpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "lexer.hpp"
enum AstId { NumberExpr, VariableExpr, CallExpr, BinaryExpr };
class ExprAST {
private:
AstId id;
public:
ExprAST(AstId id) : id(id){};
~ExprAST(){};
AstId getValueId() const { return id; }
};
class NumberAST : public ExprAST {
private:
int value;
public:
NumberAST(double value) : ExprAST(NumberExpr), value(value){};
~NumberAST(){};
int getValue() { return value; }
static inline bool classof(ExprAST const* expr) { return expr->getValueId() == NumberExpr; }
};
class VariableAST : public ExprAST {
private:
std::string name;
public:
VariableAST(std::string name) : ExprAST(VariableExpr), name(name){};
~VariableAST(){};
std::string getName() { return name; }
static inline bool classof(ExprAST const* expr) { return expr->getValueId() == VariableExpr; }
};
class BinaryExprAST : public ExprAST {
private:
OpType op;
std::unique_ptr<ExprAST> LHS, RHS;
public:
BinaryExprAST(OpType op, std::unique_ptr<ExprAST> LHS, std::unique_ptr<ExprAST> RHS)
: ExprAST(BinaryExpr), op(op), LHS(std::move(LHS)), RHS(std::move(RHS)){};
~BinaryExprAST(){};
ExprAST* getLHS() { return LHS.get(); }
ExprAST* getRHS() { return RHS.get(); }
OpType getOp() { return op; }
static inline bool classof(ExprAST const* expr) { return expr->getValueId() == BinaryExpr; }
};
class CallExprAST : public ExprAST {
private:
std::string callee;
std::vector<std::unique_ptr<ExprAST>> args;
public:
CallExprAST(std::string callee, std::vector<std::unique_ptr<ExprAST>> args)
: ExprAST(CallExpr), callee(callee), args(std::move(args)){};
~CallExprAST(){};
std::string getCalleeName() { return callee; }
int getArgsSize() { return args.size(); }
ExprAST* getArgExpr(int idx);
static inline bool classof(ExprAST const* expr) { return expr->getValueId() == CallExpr; }
};
class PrototypeAST {
private:
std::string name;
std::vector<std::string> args;
public:
PrototypeAST(std::string name, std::vector<std::string> args) : name(name), args(args){};
~PrototypeAST(){};
std::string getName() { return name; }
int getArgsSize() { return args.size(); }
std::string getArg(int idx);
};
class FunctionAST {
private:
std::unique_ptr<PrototypeAST> proto;
std::unique_ptr<ExprAST> body;
public:
FunctionAST(std::unique_ptr<PrototypeAST> proto, std::unique_ptr<ExprAST> body)
: proto(std::move(proto)), body(std::move(body)){};
~FunctionAST(){};
PrototypeAST* getProto() { return proto.get(); }
ExprAST* getBody() { return body.get(); }
};
class TranslationUnitAST {
private:
std::vector<std::unique_ptr<FunctionAST>> functions;
public:
TranslationUnitAST(){};
~TranslationUnitAST(){};
void addFunction(std::unique_ptr<FunctionAST> func);
FunctionAST* getFunction(int idx);
};