-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathast.h
273 lines (242 loc) · 6.71 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#ifndef _AST_LIT_
#define _AST_LIT_
#include "common.h"
#include "var.h"
#include "func.h"
#include "exprtype.h"
enum {
AST_NUMBER,
AST_NUMBER_FLOAT,
AST_STRING,
AST_CHAR,
AST_POSTFIX,
AST_BINARY,
AST_VARIABLE,
AST_VARIABLE_DECL,
AST_VARIABLE_ASGMT,
AST_VARIABLE_INDEX,
AST_FUNCTION_CALL,
AST_FUNCTION,
AST_IF,
AST_WHILE,
AST_FOR,
AST_BREAK,
AST_RETURN,
AST_ARRAY,
AST_LIBRARY,
AST_PROTO,
AST_NEW,
AST_STRUCT,
AST_DOT,
AST_MODULE,
AST_CAST,
AST_MINUS,
};
class AST {
public:
virtual int get_type() const = 0;
virtual ~AST() {};
};
class FloatNumberAST : public AST {
public:
double number;
FloatNumberAST(double);
virtual int get_type() const { return AST_NUMBER_FLOAT; };
llvm::Value *codegen(Function &, ExprType *);
};
class NumberAST : public AST {
public:
std::string number;
NumberAST(std::string);
virtual int get_type() const { return AST_NUMBER; };
llvm::Value *codegen(Function &, ExprType *);
};
class CharAST : public AST {
public:
int ch;
CharAST(int);
virtual int get_type() const { return AST_CHAR; }
llvm::Value *codegen(Function &, ExprType *);
};
class StringAST : public AST {
public:
std::string str;
StringAST(std::string);
virtual int get_type() const { return AST_STRING; };
llvm::Value *codegen(Function &, ExprType *);
};
class PostfixAST : public AST {
public:
std::string op;
AST *expr;
PostfixAST(std::string, AST *);
~PostfixAST() { delete expr; }
virtual int get_type() const { return AST_POSTFIX; }
};
class BinaryAST : public AST {
public:
std::string op;
AST *left, *right;
BinaryAST(std::string o, AST *le, AST *re);
virtual int get_type() const { return AST_BINARY; }
llvm::Value *codegen(Function &, Program &, ExprType *); // ret is type of expr.
};
class UnaryMinusAST : public AST {
public:
AST *expr;
UnaryMinusAST(AST *_expr);
virtual int get_type() const { return AST_MINUS; }
llvm::Value *codegen(Function &, Program &, ExprType *);
};
class CastAST : public AST {
public:
std::string type;
AST *expr;
CastAST(std::string, AST *);
virtual int get_type() const { return AST_CAST; }
llvm::Value *codegen(Function &, Program &, ExprType *);
};
class NewAllocAST : public AST {
public:
std::string type;
AST * size;
NewAllocAST(std::string, AST *size);
virtual int get_type() const { return AST_NEW; }
llvm::Value *codegen(Function &, Program &, ExprType *);
};
class VariableAST : public AST {
public:
var_t info;
VariableAST(var_t v);
virtual int get_type() const { return AST_VARIABLE; }
llvm::Value *codegen(Function &, Program &, ExprType *);
var_t *get(Function &, Program &);
var_t *append(Function &, Program &);
};
class VariableDeclAST : public AST {
public:
var_t info;
VariableDeclAST(var_t);
virtual int get_type() const { return AST_VARIABLE_DECL; }
var_t *get(Function &, Program &);
var_t *append(Function &, Program &);
};
class VariableAsgmtAST : public AST {
public:
AST *var, *src;
VariableAsgmtAST(AST *, AST *);
virtual int get_type() const { return AST_VARIABLE_ASGMT; }
llvm::Value * codegen(Function &, Program &, ExprType *);
};
class VariableIndexAST : public AST {
public:
AST *var, *idx;
VariableIndexAST(AST *, AST *);
virtual int get_type() const { return AST_VARIABLE_INDEX; }
llvm::Value * codegen(Function &, Program &, ExprType *);
llvm::Value *get_elem(Function &, Program &, ExprType *);
};
class LibraryAST : public AST {
public:
std::string lib_name;
std::vector<AST *> proto;
LibraryAST(std::string, std::vector<AST *>);
virtual int get_type() const { return AST_LIBRARY; }
llvm::Value * codegen(Program &);
};
class PrototypeAST : public AST {
public:
func_t proto;
std::string name;
std::vector<AST *> args_type;
PrototypeAST(func_t, std::vector<AST *>, std::string = "");
virtual int get_type() const { return AST_PROTO; }
void append(llvm::Module *, Program &);
};
class FunctionCallAST : public AST {
public:
func_t info;
std::vector<AST *> args;
FunctionCallAST(func_t f, std::vector<AST *> a);
virtual int get_type() const { return AST_FUNCTION_CALL; }
llvm::Value * codegen(Function &, Program &, ExprType *);
};
class FunctionAST : public AST {
public:
func_t info;
std::vector<AST *> args, statement;
FunctionAST(func_t f, std::vector<AST *> a, std::vector<AST *>);
virtual int get_type() const { return AST_FUNCTION; }
Function codegen(Program &);
};
class ModuleAST : public AST {
public:
std::string name;
std::vector<AST *> statement;
ModuleAST(std::string name, std::vector<AST *>);
virtual int get_type() const { return AST_MODULE; }
void codegen(Program &);
};
class StructAST : public AST {
public:
std::string name;
std::vector<AST *> var_decls;
StructAST(std::string, std::vector<AST *>);
virtual int get_type() const { return AST_STRUCT; }
llvm::Value *codegen(Program &);
};
class DotOpAST : public AST {
public:
AST *var, *member;
DotOpAST(AST *, AST *);
virtual int get_type() const { return AST_DOT; }
llvm::Value *codegen(Function &, Program &, ExprType *);
};
class ArrayAST : public AST {
public:
std::vector<AST *> elems;
ExprType *type = NULL;
ArrayAST(std::vector<AST *>);
ArrayAST(ExprType *);
virtual int get_type() const { return AST_ARRAY; }
llvm::Value * codegen(Function &, Program &, ExprType *);
};
class IfAST : public AST {
public:
AST *cond;
std::vector<AST *> then_block, else_block;
IfAST(AST *, std::vector<AST*>, std::vector<AST *>);
virtual int get_type() const { return AST_IF; }
llvm::Value * codegen(Function &, Program &, ExprType *);
};
class WhileAST : public AST {
public:
AST *cond;
std::vector<AST *> block;
WhileAST(AST *, std::vector<AST *>);
virtual int get_type() const { return AST_WHILE; }
llvm::Value * codegen(Function &, Program &);
};
class ForAST : public AST {
public:
bool is_range_for;
AST *asgmt, *cond, *step, *range;
std::vector<AST *> block;
ForAST(AST *, AST *, AST *, std::vector<AST *>); // assignment, condition, step, body
virtual int get_type() const { return AST_FOR; }
llvm::Value * codegen(Function &, Program &);
};
class BreakAST : public AST {
public:
virtual int get_type() const { return AST_BREAK; }
llvm::Value * codegen(Function &, Program &);
};
class ReturnAST : public AST {
public:
AST *expr;
ReturnAST(AST *);
virtual int get_type() const { return AST_RETURN; }
llvm::Value * codegen(Function &, Program &);
};
typedef std::vector<AST *> ast_vector;
#endif