-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expr.hpp
148 lines (124 loc) · 3.75 KB
/
Expr.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef expr_hpp
#define expr_hpp
#include "Token.hpp"
#include "llvm/IR/BasicBlock.h"
#include <map>
#include <string>
#include <vector>
class Expr {
public:
virtual ~Expr() = default;
virtual llvm::Value *codegen() = 0;
};
class intExpr : public Expr {
int mVal;
public:
intExpr(const int &Val) : mVal(Val){};
llvm::Value *codegen() override;
};
class doubleExpr : public Expr {
double mVal;
public:
doubleExpr(const double &Val) : mVal(Val){};
llvm::Value *codegen() override;
};
class boolExpr : public Expr {
bool mVal;
public:
boolExpr(const bool &Val) : mVal(Val){};
llvm::Value *codegen() override;
};
// When calling a variable by name
class VariableExpr : public Expr {
std::string Name;
public:
VariableExpr(const std::string &Name) : Name(Name) {}
const std::string &getName() { return Name; }
llvm::Value *codegen() override;
};
/// VarExprAST - Expression class for var x = ""
class VarExprAST : public Expr {
// Var Names and their initializer expression
std::vector<std::pair<std::string, std::unique_ptr<Expr>>> VarNames;
public:
VarExprAST(
std::vector<std::pair<std::string, std::unique_ptr<Expr>>> VarNames)
: VarNames(std::move(VarNames)) {}
llvm::Value *codegen() override;
};
class GroupingExpr : public Expr {
std::unique_ptr<Expr> LHS;
public:
GroupingExpr(std::unique_ptr<Expr> LHS) : LHS(std::move(LHS)){};
llvm::Value *codegen() { return LHS->codegen(); };
};
class BinaryExpr : public Expr {
int op;
std::unique_ptr<Expr> LHS, RHS;
public:
BinaryExpr(int Op, std::unique_ptr<Expr> LHS, std::unique_ptr<Expr> RHS)
: op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)){};
llvm::Value *codegen() override;
};
// represents something like !true or -a
class UnaryExpr : public Expr {
Token op;
std::unique_ptr<Expr> LHS;
public:
UnaryExpr(Token op, std::unique_ptr<Expr> LHS)
: op(op), LHS(std::move(LHS)){};
llvm::Value *codegen() override;
};
class Block : public Expr {
std::vector<std::unique_ptr<Expr>> expressions;
public:
explicit Block(std::vector<std::unique_ptr<Expr>> exprs)
: expressions(std::move(exprs)){};
llvm::Value *codegen() override;
};
class IfExpr : public Expr {
std::unique_ptr<Expr> Cond, Then, Else;
public:
IfExpr(std::unique_ptr<Expr> Cond, std::unique_ptr<Expr> Then,
std::unique_ptr<Expr> Else)
: Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)){};
llvm::Value *codegen() override;
};
class CallExpr : public Expr {
std::string Callee;
std::vector<std::unique_ptr<Expr>> Args;
public:
CallExpr(const std::string &Callee, std::vector<std::unique_ptr<Expr>> Args)
: Callee(Callee), Args(std::move(Args)){};
llvm::Value *codegen() override;
};
class ForExpr : public Expr {
std::string VarName;
std::unique_ptr<Expr> Start, End, Step, Body;
public:
ForExpr(const std::string &VarName, std::unique_ptr<Expr> Start,
std::unique_ptr<Expr> End, std::unique_ptr<Expr> Step,
std::unique_ptr<Expr> Body)
: VarName(VarName), Start(std::move(Start)), End(std::move(End)),
Step(std::move(Step)), Body(std::move(Body)) {}
llvm::Value *codegen() override;
};
class PrototypeAST {
std::string Name;
std::vector<std::string> Args;
public:
PrototypeAST(const std::string &Name, std::vector<std::string> Args)
: Name(Name), Args(std::move(Args)) {}
const std::string &getName() const { return Name; }
llvm::Function *codegen();
};
/// FunctionAST - This class represents a function definition itself.
class FunctionAST {
std::unique_ptr<PrototypeAST> Proto;
std::unique_ptr<Expr> Body;
public:
FunctionAST(std::unique_ptr<PrototypeAST> Proto, std::unique_ptr<Expr> Body)
: Proto(std::move(Proto)), Body(std::move(Body)) {}
llvm::Function *codegen();
};
#endif