-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.cpp
304 lines (279 loc) · 8.87 KB
/
parse.cpp
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "parse.h"
#include "lit.h"
#include "lex.h"
#include "expr.h"
#include "token.h"
#include "util.h"
#include "var.h"
#include "codegen.h"
#include "exprtype.h"
#include "func.h"
AST *Parser::make_break() {
if(tok.skip("break")) return new BreakAST();
return NULL;
}
AST *Parser::make_return() {
if(tok.skip("return")) {
AST *expr = expr_entry();
return new ReturnAST(expr);
}
return NULL;
}
AST *Parser::expression() {
if(tok.skip("def")) return make_func();
else if(tok.is("proto")) return make_proto();
else if(tok.is("struct")) return make_struct();
else if(tok.is("module")) return make_module();
else if(tok.is("lib")) return make_lib();
else if(tok.is("for")) return make_for();
else if(tok.is("while")) return make_while();
else if(tok.is("return")) return make_return();
else if(tok.is("if")) return make_if();
else if(tok.is("break")) return make_break();
else if(tok.is("else") || tok.is("elsif") || tok.is("end")) return NULL;
else return expr_entry();
return NULL;
}
ast_vector Parser::eval() {
ast_vector block;
while(tok.pos < tok.size) {
while(tok.skip(";"))
if(tok.pos >= tok.size) break;
AST *b = expression();
while(tok.skip(";"))
if(tok.pos >= tok.size) break;
if(b == NULL) break;
block.push_back(b);
}
return block;
}
llvm::Module *Parser::parser() {
tok.pos = 0;
blocksCount = 0;
op_prec[".."] = 50;
op_prec["..."] =50;
op_prec["="] = 100;
op_prec["+="] = 100;
op_prec["-="] = 100;
op_prec["*="] = 100;
op_prec["/="] = 100;
op_prec["%="] = 100;
op_prec["^="] = 100;
op_prec["|="] = 100;
op_prec["&="] = 100;
op_prec["=="] = 200;
op_prec["!="] = 200;
op_prec["<="] = 200;
op_prec[">="] = 200;
op_prec["<"] = 200;
op_prec[">"] = 200;
op_prec["&"] = 150;
op_prec["|"] = 150;
op_prec["^"] = 150;
op_prec["+"] = 300;
op_prec["-"] = 300;
op_prec["?"] = 300;
op_prec["*"] = 400;
op_prec["/"] = 400;
op_prec["%"] = 400;
// append standard functions to a list of declared functions
append_func(std::vector<std::string>{"Array" });
append_func(std::vector<std::string>{"GC" });
append_func(std::vector<std::string>{"printf" });
append_func(std::vector<std::string>{"gets" });
append_func(std::vector<std::string>{"getc" });
append_func(std::vector<std::string>{"strlen" });
append_func(std::vector<std::string>{"builtinlength" });
append_func(std::vector<std::string>{"puts" });
append_func(std::vector<std::string>{"print" }); // 'print' is almost the same as 'puts' but 'print' doesn't new line
append_func(std::vector<std::string>{"gets" });
append_func(std::vector<std::string>{"str_to_int" });
append_func(std::vector<std::string>{"str_to_float" });
append_func(std::vector<std::string>{"int_to_str" });
append_func(std::vector<std::string>{"int64_to_str" });
append_func(std::vector<std::string>{"float_to_str" });
ast_vector a = eval();
// std::cout << "\n---------- abstract syntax tree ----------" << std::endl;
// for(int i = 0; i < a.size(); i++)
// visit(a[i]), std::cout << std::endl;
llvm::Module *program_mod = Codegen::codegen(a); // start code generating
// std::cout << "\n---------- end of abstract syntax tree --" << std::endl;
return program_mod;
}
AST *Parser::make_lib() {
if(tok.skip("lib")) {
std::string name = tok.next().val;
ast_vector proto = eval();
if(!tok.skip("end")) error("error: %d: expected expression 'end'", tok.get().nline);
return new LibraryAST(name, proto);
}
return NULL;
}
AST *Parser::make_proto() {
if(tok.skip("proto")) {
std::string func_name = tok.next().val;
ast_vector args;
func_t function = { .name = func_name, .type = T_INT };
bool is_parentheses = false;
if((is_parentheses=tok.skip("(")) || is_ident_tok()) { // get params
do { args.push_back(expr_primary()); } while(tok.skip(",") || is_ident_tok());
if(is_parentheses) {
if(!tok.skip(")"))
error("error: %d: expected expression ')'", tok.get().nline);
}
}
function.params = args.size();
if(tok.skip(":")) {
ExprType *type = Type::str_to_type(tok.next().val);
if(tok.skip("[]")) {
type->change(T_ARRAY, type);
}
function.type = type;
}
std::string new_name;
if(tok.skip("|")) {
new_name = tok.next().val;
}
append_func(std::vector<std::string>(1, new_name.empty() ? func_name : new_name));
return new PrototypeAST(function, args, new_name);
}
return NULL;
}
AST *Parser::make_module() {
if(tok.skip("module")) {
std::string name = tok.next().val;
module.push_back(name);
ast_vector body = eval();
module.pop_back();
if(!tok.skip("end")) error("error: %d: expected expression 'end'", tok.get().nline);
return new ModuleAST(name, body);
}
return nullptr;
}
AST *Parser::make_struct() {
if(tok.skip("struct")) {
std::string name = tok.next().val;
ast_vector decl_vars = eval();
if(!tok.skip("end")) error("error: %d: expected expression 'end'", tok.get().nline);
return new StructAST(name, decl_vars);
}
return NULL;
}
AST *Parser::make_if() {
if(tok.skip("if")) {
AST *cond = expr_entry();
ast_vector then = eval(), else_block;
if(tok.skip("else")) {
else_block = eval();
} else if(tok.is("elsif")) {
else_block.push_back(make_elsif());
}
if(!tok.skip("end")) error("error: %d: expected expression 'end'", tok.get().nline);
AST *i = new IfAST(cond, then, else_block);
return i;
}
return NULL;
}
AST *Parser::make_elsif() {
if(tok.skip("elsif")) {
AST *cond = expr_entry();
ast_vector then = eval(), else_block;
if(tok.is("elsif")) {
else_block.push_back(make_elsif());
} else if(tok.skip("else")) {
else_block = eval();
}
return new IfAST(cond, then, else_block);
}
return NULL;
}
AST *Parser::make_for() {
if(tok.skip("for")) {
AST *asgmt = expr_entry();
if(tok.skip(",")) {
AST *cond = expr_entry();
if(!tok.skip(",")) error("error: %d: expect expression ','", tok.get().nline);
AST *step = expr_entry();
ast_vector block = eval();
if(!tok.skip("end")) error("error: %d: expected expression 'end'", tok.get().nline);
return new ForAST(asgmt, cond, step, block);
} else if(tok.skip("in")) {
AST *range = expr_entry(), *cond, *step;
if(range->get_type() == AST_BINARY) {
BinaryAST *cond_bin = (BinaryAST *)range;
AST *var = asgmt;
asgmt = new VariableAsgmtAST(asgmt, cond_bin->left);
cond = new BinaryAST(cond_bin->op == ".." ? "<=" : /* op=... */ "<", var, cond_bin->right);
step = new VariableAsgmtAST(var,
new BinaryAST("+", var, new NumberAST("1"))
);
} else error("error: %d: invalid expression", tok.get().nline);
ast_vector block = eval();
if(!tok.skip("end")) error("error: %d: expected expression 'end'", tok.get().nline);
return new ForAST(asgmt, cond, step, block);
} else {
error("error: %d: unknown syntax", tok.get().nline);
}
}
return NULL;
}
AST *Parser::make_while() {
if(tok.skip("while")) {
AST *cond = expr_entry();
ast_vector block = eval();
if(!tok.skip("end")) error("error: %d: expected expression 'end'", tok.get().nline);
return new WhileAST(cond, block);
}
return NULL;
}
AST *Parser::make_func() {
uint32_t params = 0;
ast_vector args, stmt;
std::string func_name = tok.next().val;
bool is_template = false;
if(func_name == "operator") {
func_name += tok.next().val; // user-defined operator
} else if(func_name == "template") { // template function
func_name = tok.next().val;
is_template = true;
}
func_t function = {
.name = func_name,
.is_template = is_template,
.type = T_INT
};
{
std::vector<std::string> realname = module;
realname.push_back(func_name);
append_func(realname);
}
bool is_parentheses = false;
if((is_parentheses=tok.skip("(")) || is_ident_tok()) { // get params
do { args.push_back(expr_primary()); } while(tok.skip(",") || is_ident_tok());
if(is_parentheses) {
if(!tok.skip(")"))
error("error: %d: expected expression ')'", tok.get().nline);
}
}
if(tok.skip(":")) {
int is_ary = 0;
ExprType type(T_INT);
type.change(Type::str_to_type(tok.next().val));
while(tok.skip("[]")) {
ExprType *elem_ty = &type;
type.next = new ExprType(elem_ty);
type.change(T_ARRAY);
}
function.type = type;
}
stmt = eval();
if(!tok.skip("end")) { error("error: source %d", __LINE__); }
local_var_list.clear();
return new FunctionAST(function, args, stmt);
}
bool Parser::is_func(std::vector<std::string> name) {
return function_list.count(name) != 0 ? true : false;
}
void Parser::append_func(std::vector<std::string> name) {
function_list[name] = true;
}