-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAST.h
111 lines (90 loc) · 2.74 KB
/
AST.h
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
#include "llvm/IR/Value.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <memory>
#include <vector>
using namespace std;
using namespace llvm;
void initialModulesAndPassManager();
void initializeNativeTargets();
class ExpressionAST
{
public:
virtual ~ExpressionAST() {}
virtual Value *codegen() = 0;
};
class NumberExpAST : public ExpressionAST
{
double value;
public:
NumberExpAST(double val) : value(val) {}
Value *codegen() override;
};
class VariableExpAST : public ExpressionAST
{
string name;
public:
VariableExpAST(string name) : name(name) {}
Value *codegen() override;
};
class BinaryExpAST : public ExpressionAST
{
char op;
unique_ptr<ExpressionAST> lhs, rhs;
public:
BinaryExpAST(char op, unique_ptr<ExpressionAST> lhs,
unique_ptr<ExpressionAST> rhs) : op(op), lhs(move(lhs)), rhs(move(rhs)) {}
Value *codegen() override;
};
class CallExpressionAST : public ExpressionAST
{
string funcName;
vector<unique_ptr<ExpressionAST>> args;
public:
CallExpressionAST(string funcName, vector<unique_ptr<ExpressionAST>> args) : funcName(funcName),
args(move(args)) {}
Value *codegen() override;
};
class IfExpressionAST : public ExpressionAST
{
unique_ptr<ExpressionAST> cond, thenStmt, elseStmt;
public:
IfExpressionAST(unique_ptr<ExpressionAST> cond, unique_ptr<ExpressionAST> thenStmt,
unique_ptr<ExpressionAST> elseStmt)
: cond(move(cond)), thenStmt(move(thenStmt)), elseStmt(move(elseStmt)) {}
Value *codegen() override;
};
class ForExpressionAST : public ExpressionAST
{
string varName;
unique_ptr<ExpressionAST> start, end, step, body;
public:
ForExpressionAST(string &varName, unique_ptr<ExpressionAST> start, unique_ptr<ExpressionAST> end,
unique_ptr<ExpressionAST> step, unique_ptr<ExpressionAST> body)
: varName(varName), start(move(start)), end(move(end)), step(move(step)), body(move(body)) {}
Value *codegen() override;
};
class PrototypeAST
{
string name;
vector<string> args;
public:
PrototypeAST(string funcName, vector<string> args) : name(funcName),
args(move(args)) {}
Function *codegen();
string getName() { return this->name; }
};
class FunctionExpressionAST
{
unique_ptr<PrototypeAST> prototype;
unique_ptr<ExpressionAST> body;
public:
FunctionExpressionAST(unique_ptr<PrototypeAST> prototype,
unique_ptr<ExpressionAST> body) : prototype(move(prototype)), body(move(body)) {}
Function *codegen();
};