-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.h
More file actions
268 lines (233 loc) · 7.6 KB
/
ast.h
File metadata and controls
268 lines (233 loc) · 7.6 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
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
#ifndef __AST_HPP__
#define __AST_HPP__
#ifndef __cplusplus
typedef struct _BlockAST BlockAST;
typedef struct _ExprAST ExprAST;
typedef struct _IdentifierExprAST IdentifierExprAST;
typedef struct _FunctionAST FunctionAST;
typedef struct _FunctionArgAST FunctionArgAST;
typedef struct _FunctionArgListAST FunctionArgListAST;
typedef struct _CallArgListAST CallArgListAST;
typedef struct _ComparisonAST ComparisonAST;
#else
#include <list>
#include <vector>
#include <map>
#include <memory>
#include <string>
#include <ostream>
namespace llvm {
class Value;
class Module;
};
namespace nanjit {
class TypeInfo;
}
class ScopeContext;
class SyntaxErrorException : public std::exception
{
std::string error_string;
public:
SyntaxErrorException (std::string e) : error_string(e.c_str()) {}
SyntaxErrorException (const char *e) : error_string(e) {}
~SyntaxErrorException() throw() {}
const char* what() const throw() { return error_string.c_str(); }
};
class ASTNode {
private:
int line_number;
int column_number;
public:
ASTNode();
void setLocation(int line, int col);
SyntaxErrorException genSyntaxError(std::string e);
};
class ExprAST : public ASTNode {
public:
virtual ~ExprAST() {}
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class DoubleExprAST : public ExprAST {
std::string str_value;
public:
DoubleExprAST(std::string val);
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class FloatExprAST : public ExprAST {
std::string str_value;
public:
FloatExprAST(std::string val);
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class IntExprAST : public ExprAST {
std::string str_value;
public:
IntExprAST(std::string val);
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class IdentifierExprAST : public ExprAST {
std::string Name;
public:
IdentifierExprAST(const std::string &name) : Name(name) {};
std::string getName() { return Name; };
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class BinaryExprAST : public ExprAST {
char Op;
std::auto_ptr<ExprAST> LHS, RHS;
public:
BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
: Op(op), LHS(lhs), RHS(rhs) {}
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class AssignmentExprAST : public ExprAST {
std::auto_ptr<IdentifierExprAST> LHSType;
std::auto_ptr<IdentifierExprAST> LHS;
std::auto_ptr<ExprAST> RHS;
public:
AssignmentExprAST(IdentifierExprAST *lhs, ExprAST *rhs)
: LHS(lhs), RHS(rhs) {}
AssignmentExprAST(IdentifierExprAST *lhstype, IdentifierExprAST *lhs, ExprAST *rhs)
: LHSType(lhstype), LHS(lhs), RHS(rhs) {}
virtual llvm::Value *codegen(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class ShuffleSelfAST : public ExprAST {
std::auto_ptr<IdentifierExprAST> Target;
std::auto_ptr<IdentifierExprAST> Access;
public:
ShuffleSelfAST(IdentifierExprAST *target, IdentifierExprAST *access)
: Target(target), Access(access) {}
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class CallArgListAST {
std::vector<ExprAST *> Args;
public:
CallArgListAST();
CallArgListAST(ExprAST *arg);
void prependArg(ExprAST *arg);
std::vector<ExprAST *> &getArgsList() { return Args; };
virtual std::ostream& print(std::ostream& os);
virtual ~CallArgListAST();
};
class VectorConstructorAST : public ExprAST {
std::auto_ptr<IdentifierExprAST> Type;
std::auto_ptr<CallArgListAST> ArgList;
public:
VectorConstructorAST(IdentifierExprAST *type, CallArgListAST *arg_list)
: Type(type), ArgList(arg_list) {}
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class CallAST : public ExprAST {
std::auto_ptr<IdentifierExprAST> Target;
std::auto_ptr<CallArgListAST> ArgList;
public:
CallAST(IdentifierExprAST *target, CallArgListAST* args)
: Target(target), ArgList(args) {}
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class BlockAST {
std::list<ExprAST *> Expressions;
public:
BlockAST(ExprAST *expr);
void PrependExpr(ExprAST *expr);
virtual llvm::Value *codegen(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
virtual ~BlockAST();
};
class ComparisonAST : public ExprAST {
public:
enum CompareOp {
EqualTo,
NotEqualTo,
GreaterThan,
GreaterThanOrEqual,
LessThan,
LessThanOrEqual
};
private:
CompareOp Op;
std::auto_ptr<ExprAST> LHS, RHS;
public:
ComparisonAST(CompareOp op, ExprAST *lhs, ExprAST *rhs)
: Op(op), LHS(lhs), RHS(rhs) {}
virtual llvm::Value *codegen(ScopeContext *scope);
virtual nanjit::TypeInfo getResultType(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class IfElseAST : public ExprAST /* FIXME: Not really an expr */ {
std::auto_ptr<ComparisonAST> Comparison;
std::auto_ptr<BlockAST> IfBlock;
std::auto_ptr<BlockAST> ElseBlock;
public:
IfElseAST(ComparisonAST *comp,
BlockAST *ifblock,
BlockAST *elseblock) : Comparison(comp), IfBlock(ifblock), ElseBlock(elseblock) {}
virtual llvm::Value *codegen(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class FunctionArgAST {
std::auto_ptr<IdentifierExprAST> Type;
std::auto_ptr<IdentifierExprAST> Name;
public:
FunctionArgAST(IdentifierExprAST *type, IdentifierExprAST *name) : Type(type), Name(name) {};
std::string getType();
std::string getName();
};
class FunctionArgListAST {
std::list<FunctionArgAST *> Args;
public:
FunctionArgListAST(FunctionArgAST *arg);
void prependArg(FunctionArgAST *arg);
std::list<FunctionArgAST *> &getArgsList();
virtual std::ostream& print(std::ostream& os);
virtual ~FunctionArgListAST();
};
class FunctionAST : public ASTNode {
std::auto_ptr<IdentifierExprAST> Name;
std::auto_ptr<IdentifierExprAST> ReturnType;
std::auto_ptr<FunctionArgListAST> Args;
std::auto_ptr<BlockAST> Block;
public:
FunctionAST(IdentifierExprAST *return_type, IdentifierExprAST *name, FunctionArgListAST *args, BlockAST *b) : ReturnType(return_type), Name(name), Args(args), Block(b) {}
std::string getName();
virtual llvm::Value *codegen(llvm::Module *module = NULL);
virtual std::ostream& print(std::ostream& os);
virtual ~FunctionAST();
};
class ReturnAST : public ExprAST /* FIXME: Not really an expr */ {
std::auto_ptr<ExprAST> Result;
public:
ReturnAST(ExprAST *expr = NULL) : Result(expr) {};
virtual llvm::Value *codegen(ScopeContext *scope);
virtual std::ostream& print(std::ostream& os);
};
class ModuleAST {
std::list<FunctionAST *> functions;
public:
ModuleAST();
void prependFunction(FunctionAST *func);
virtual llvm::Module *codegen(llvm::Module *module);
virtual std::ostream& print(std::ostream& os);
virtual ~ModuleAST();
};
#endif /* __cplusplus */
#endif /* __AST_HPP__ */