-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexpr.cpp
410 lines (391 loc) · 12.7 KB
/
expr.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "expr.h"
#include "lit.h"
#include "lex.h"
#include "token.h"
#include "parse.h"
#include "util.h"
#include "var.h"
#include "func.h"
#include "ast.h"
#include "exprtype.h"
int Parser::is_string_tok() { return tok.get().type == TOK_STRING; }
int Parser::is_number_tok() { return tok.get().type == TOK_NUMBER; }
int Parser::is_ident_tok() { return tok.get().type == TOK_IDENT; }
int Parser::is_char_tok() { return tok.get().type == TOK_CHAR; }
AST *visit(AST *ast) {
auto mod_to_string = [](func_t f) -> std::string {
std::string out;
for(auto it = f.mod_name.begin(); it != f.mod_name.end(); ++it) {
out += *it + "::";
}
return out;
};
if(ast->get_type() == AST_BINARY) {
std::cout << "(" << ((BinaryAST *)ast)->op << " ";
visit(((BinaryAST *)ast)->left);
visit(((BinaryAST *)ast)->right);
std::cout << ")";
std::cout << std::endl;
} else if(ast->get_type() == AST_STRUCT) {
StructAST *sa = (StructAST *)ast;
std::cout << "(struct " << sa->name << "(" << std::endl;
for(int i = 0; i < sa->var_decls.size(); i++)
visit(sa->var_decls[i]);
std::cout << ")\n)" << std::endl;
} else if(ast->get_type() == AST_VARIABLE_ASGMT) {
std::cout << "(=";
visit(((VariableAsgmtAST *)ast)->var);
visit(((VariableAsgmtAST *)ast)->src);
std::cout << ")" << std::endl;;
} else if(ast->get_type() == AST_POSTFIX) {
std::cout << "(" << ((PostfixAST *)ast)->op << " ";
visit(((PostfixAST *)ast)->expr);
std::cout << ")";
} else if(ast->get_type() == AST_WHILE) {
WhileAST *wa = (WhileAST *)ast;
std::cout << "(while ("; visit(wa->cond);
std::cout << ")\n(\n";
for(int i = 0; i < wa->block.size(); i++)
visit(wa->block[i]);
std::cout << ")\n)";
std::cout << std::endl;
} else if(ast->get_type() == AST_FOR) {
ForAST *fa = (ForAST *)ast;
std::cout << "(for ("; visit(fa->asgmt);
if(fa->is_range_for) {
std::cout << ") ("; visit(fa->range);
std::cout << ")\n(" << std::endl;
} else {
std::cout << ") ("; visit(fa->cond);
std::cout << ") ("; visit(fa->step);
std::cout << ")\n(" << std::endl;
}
for(int i = 0; i < fa->block.size(); i++)
visit(fa->block[i]);
std::cout << ")\n)";
std::cout << std::endl;
} else if(ast->get_type() == AST_FUNCTION) {
FunctionAST *fa = ((FunctionAST *) ast);
std::cout << "(defunc " << mod_to_string(fa->info) << fa->info.name << " (";
for(int i = 0; i < fa->args.size(); i++)
visit(fa->args[i]);
std::cout << ")\n(\n";
for(int i = 0; i < fa->statement.size(); i++)
visit(fa->statement[i]);
std::cout << ")\n)";
std::cout << std::endl;
} else if(ast->get_type() == AST_IF) {
IfAST *ia = (IfAST *)ast;
std::cout << "(if ("; visit(ia->cond); std::cout << ")\n(";
for(int i = 0; i < ia->then_block.size(); i++)
visit(ia->then_block[i]);
std::cout << ")\n(\n";
for(int i = 0; i < ia->else_block.size(); i++)
visit(ia->else_block[i]);
std::cout << ")\n)";
std::cout << std::endl;
} else if(ast->get_type() == AST_VARIABLE_INDEX) {
std::cout << "([] ";
visit(((VariableIndexAST *)ast)->var);
visit(((VariableIndexAST *)ast)->idx);
std::cout << ")";
} else if(ast->get_type() == AST_VARIABLE_DECL) {
std::cout << "(vardecl "
// << ((VariableDeclAST *)ast)->info.mod_name << "::"
<< ((VariableDeclAST *)ast)->info.name << " "
<< ((VariableDeclAST *)ast)->info.type.get().type << ")";
} else if(ast->get_type() == AST_ARRAY) {
ArrayAST *ary = (ArrayAST *)ast;
std::cout << "(array ";
for(int i = 0; i < ary->elems.size(); i++)
visit(ary->elems[i]);
std::cout << ")";
} else if(ast->get_type() == AST_NUMBER) {
std::cout << " " << ((NumberAST *)ast)->number << " ";
} else if(ast->get_type() == AST_NUMBER_FLOAT) {
std::cout << " " << ((FloatNumberAST *)ast)->number << " ";
} else if(ast->get_type() == AST_CHAR) {
std::cout << " " << ((CharAST *)ast)->ch << " ";
} else if(ast->get_type() == AST_STRING) {
std::cout << " \"" << ((StringAST *)ast)->str << "\" ";
} else if(ast->get_type() == AST_VARIABLE) {
std::cout << "(var "
// << ((VariableAST *)ast)->info.mod_name << "::"
<< ((VariableAST *)ast)->info.name << ") ";
} else if(ast->get_type() == AST_FUNCTION_CALL) {
std::cout << "(call "
<< mod_to_string(((FunctionCallAST *)ast)->info)
<< ((FunctionCallAST *)ast)->info.name << " ";
for(int i = 0; i < ((FunctionCallAST *)ast)->args.size(); i++) {
visit(((FunctionCallAST *)ast)->args[i]);
}
std::cout << ")";
std::cout << std::endl;
} else if(ast->get_type() == AST_RETURN) {
std::cout << "(return ("; visit(((ReturnAST *)ast)->expr);
std::cout << "))" << std::endl;
} else if(ast->get_type() == AST_LIBRARY) {
LibraryAST *l = (LibraryAST *)ast;
std::cout << "(lib " << l->lib_name << "(" << std::endl;
for(int i = 0; i < l->proto.size(); i++)
visit(l->proto[i]);
std::cout << ")" << std::endl;
} else if(ast->get_type() == AST_PROTO) {
PrototypeAST *prt = (PrototypeAST *)ast;
std::cout << "(proto " << prt->proto.name << " " << prt->proto.params << ")" << std::endl;
} else if(ast->get_type() == AST_NEW) {
NewAllocAST *na = (NewAllocAST *)ast;
std::cout << "(new " << na->type << " ";
if(na->size) visit(na->size);
std::cout << ")" << std::endl;
} else if(ast->get_type() == AST_DOT) {
DotOpAST *da = (DotOpAST *)ast;
std::cout << "(dot ";
visit(da->var);
visit(da->member);
std::cout << ")";
} else if(ast->get_type() == AST_MODULE) {
ModuleAST *ma = (ModuleAST *)ast;
std::cout << "(module " << ma->name << " ";
for(auto it = ma->statement.begin(); it != ma->statement.end(); ++it) {
visit(*it);
}
std::cout << ")" << std::endl;
} else if(ast->get_type() == AST_CAST) {
CastAST *ca = (CastAST *)ast;
std::cout << "(cast " << ca->type << " ";
visit(ca->expr);
std::cout << ")" << std::endl;
} else if(ast->get_type() == AST_MINUS) {
UnaryMinusAST *ua = (UnaryMinusAST *)ast;
std::cout << "(minus ";
visit(ua->expr);
std::cout << ")" << std::endl;
}
return ast;
}
int Parser::get_op_prec(std::string op) {
return op_prec.count(op) != 0 ? op_prec[op] : -1;
}
AST *Parser::expr_rhs(int prec, AST *lhs) {
while(true) {
int tok_prec, next_prec;
if(tok.get().type == TOK_SYMBOL) {
tok_prec = get_op_prec(tok.get().val);
if(tok_prec < prec) return lhs;
} else return lhs;
std::string op = tok.next().val;
AST *rhs = expr_dot();
if(tok.get().type == TOK_SYMBOL) {
next_prec = get_op_prec(tok.get().val);
if(tok_prec < next_prec)
rhs = expr_rhs(tok_prec + 1, rhs);
}
if(op == "+=" ||
op == "-=" ||
op == "*=" ||
op == "/=" ||
op == "&=" ||
op == "|=" ||
op == "^=" ||
op == "=") {
bool add = op == "+=", sub = op == "-=", mul = op == "*=", div = op == "/=",
aand = op == "&=", aor = op == "|=", axor = op == "^=", normal = op == "=";
lhs = new VariableAsgmtAST(lhs, normal ? rhs :
new BinaryAST(
add ? "+" :
sub ? "-" :
mul ? "*" :
div ? "/" :
aand? "&" :
aor ? "|" :
axor? "^" : "ERROR", lhs, rhs));
} else
lhs = new BinaryAST(op, lhs, rhs);
}
}
AST *Parser::expr_entry() {
AST *lhs = expr_dot();
return expr_rhs(0, lhs);
}
AST *Parser::expr_dot() {
AST *l, *r;
l = expr_index();
while(tok.skip(".")) {
std::string name = tok.next().val;
if(tok.skip("(")) { // UFCS
func_t f = {
.name = name,
.mod_name = std::vector<std::string>(),
};
std::vector<AST *> args;
args.push_back(l);
while(!tok.skip(")") && !tok.is(";")) {
args.push_back(expr_entry());
tok.skip(",");
}
l = new FunctionCallAST(f, args);
} else { // member of struct
tok.prev();
r = expr_primary();
l = new DotOpAST(l, r);
while(tok.skip("[")) {
r = expr_entry();
l = new VariableIndexAST(l, r);
if(!tok.skip("]"))
error("error: %d: expected expression ']'", tok.get().nline);
}
}
}
return l;
}
AST *Parser::expr_index() {
AST *l, *r;
l = expr_unary();
if(tok.get().type == TOK_STRING) return l; // skip string tok such as "["
while(tok.skip("[")) {
r = expr_entry();
l = new VariableIndexAST(l, r);
if(!tok.skip("]"))
error("error: %d: expected expression ']'", tok.get().nline);
}
return l;
}
AST *Parser::expr_unary() { // TODO: implementation unary minus(-)!!
if(tok.skip("<")) { // cast
std::string type = tok.next().val;
while(tok.skip("[]")) type += "[]";
if(!tok.skip(">")) error("error: expected expression '>'");
AST *expr = expr_entry();
return new CastAST(type, expr);
} else if(tok.skip("-")) {
AST *expr = expr_entry();
return new UnaryMinusAST(expr);
}
return expr_primary();
}
AST *Parser::expr_primary() {
bool is_global_decl = false, is_ref = false;
std::string name;
std::vector<std::string> mod_name;
var_t *v = NULL;
if(tok.skip("$")) is_global_decl = true;
if(tok.skip("ref")) is_ref = true;
if(tok.get().val == "new") {
tok.skip();
std::string type = "int";
if(is_ident_tok()) {
type = tok.next().val;
while(tok.skip("[]"))
type += "[]";
}
AST *size = NULL;
if(tok.get().type != TOK_END)
size = expr_entry();
return new NewAllocAST(type, size);
} else if(is_number_tok()) {
if(strstr(tok.get().val.c_str(), ".") != NULL)
return new FloatNumberAST(atof(tok.next().val.c_str()));
else
return new NumberAST(tok.next().val);
} else if(is_char_tok()) {
return new CharAST(tok.next().val.c_str()[0]);
} else if(is_string_tok()) {
return new StringAST(tok.next().val);
} else if(tok.get().val == "true" || tok.get().val == "false" || tok.get().val == "nil") {
return new NumberAST(tok.next().val == "true" ? "1" :"0");
} else if(tok.is("if")) {
return make_if();
} else if(is_ident_tok()) {
name = tok.next().val;
int is_ary;
ExprType type;
bool is_vardecl = false;
std::string class_name;
if(tok.is("::")) { // module?
mod_name.push_back(name);
name = "";
while(tok.skip("::")) mod_name.push_back(tok.next().val);
name = mod_name.back();
mod_name.pop_back();
} else if(tok.skip(":")) { // variable declaration
is_ary = 0;
type.change(Type::str_to_type(tok.next().val));
if(tok.skip("[]")) {
ExprType *elem_ty = new ExprType(type);
type.change(T_ARRAY);
type.next = elem_ty;
}
is_vardecl = true;
} else {
type.change(T_INT);
}
{
bool has_pare = false;
std::vector<std::string> func_name = module;
func_name.push_back(name);
if(!is_func(func_name)) { func_name = mod_name; func_name.push_back(name); }
if(tok.skip("(")) { // function
func_t f = {
.name = name,
.mod_name = mod_name
};
std::vector<AST *> args;
while(!tok.skip(")") && !tok.is(";")) {
args.push_back(expr_entry());
tok.skip(",");
}
return new FunctionCallAST(f, args);
} else { // variable
var_t v = {
.name = name,
.mod_name = "",
.class_type = class_name,
.is_global = is_global_decl
};
v.type.change(new ExprType(type));
v.type.set_ref(is_ref);
append_local_var(name);
if(is_vardecl)
return new VariableDeclAST(v);
else
return new VariableAST(v);
}
}
} else if(tok.skip("(")) {
AST *e = expr_entry();
if(!tok.skip(")"))
error("error: %d: expected expression ')'", tok.get().nline);
return e;
} else {
AST *ary = expr_array();
if(ary == NULL)
error("error: %d: invalid expression", tok.get().nline);
else return ary;
}
return 0;
}
AST *Parser::expr_array() {
if(tok.skip("[")) {
ast_vector elems;
while(!tok.skip("]")) {
AST *elem = expr_entry();
elems.push_back(elem);
tok.skip(",");
}
return new ArrayAST(elems);
} else if(tok.skip("[]")) { // empty array
if(tok.skip(":")) {
std::string type_name = tok.next().val;
while(tok.skip("[]")) type_name += "[]";
return new ArrayAST(Type::str_to_type(type_name));
}
}
return NULL;
}
bool Parser::is_local_var(std::string name) {
return local_var_list.count(name) ? true : false;
}
void Parser::append_local_var(std::string name) {
local_var_list[name] = true;
}