Repository navigation
Expand file tree
/
Copy pathparser.c
More file actions
506 lines (442 loc) · 11.7 KB
/
Copy pathparser.c
File metadata and controls
506 lines (442 loc) · 11.7 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include "parser.h"
#include "tokenizer.h"
#include "type.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int label_count = 0;
var_list_t *locals;
var_list_t *globals;
void init_node(node_t *init_node, node_kind_t kind, node_t *next, typ_t *ty,
token_t *tok, node_t *lhs, node_t *rhs, node_t *cond,
node_t *then, node_t *els, node_t *init, node_t *inc,
node_t *body, char *func_name, node_t *args, va_t *v, int val) {
init_node->kind = kind;
init_node->next = next;
init_node->ty = ty;
init_node->tok = tok;
init_node->lhs = lhs;
init_node->rhs = rhs;
init_node->cond = cond;
init_node->then = then;
init_node->els = els;
init_node->init = init;
init_node->inc = inc;
init_node->body = body;
init_node->func_name = func_name;
init_node->args = args;
init_node->v = v;
init_node->val = val;
}
void init_va(va_t *v, char *name, typ_t *ty, bool is_local, char *contents,
int content_len, int offset) {
v->name = name;
v->ty = ty;
v->is_local = is_local;
v->contents = contents;
v->content_length = content_len;
v->offset = offset;
}
void init_type(typ_t *ty, type_kind_t kind, typ_t *base, int array_size) {
ty->kind = kind;
ty->base = base;
ty->array_size = array_size;
}
void init_var_list(var_list_t *vl, var_list_t *next, va_t *v) {
vl->next = next;
vl->v = v;
}
void init_function(fun_t *fn, fun_t *next, char *name, var_list_t *params,
node_t *nde, var_list_t *locals, int stack_size) {
fn->next = next;
fn->name = name;
fn->params = params;
fn->node = nde;
fn->locals = locals;
fn->stack_size = stack_size;
}
va_t *find_var(token_t *tok) {
var_list_t *vl = locals;
va_t *v;
while (vl != NULL) {
v = vl->v;
if (strlen(v->name) == tok->len && strncmp(tok->str, v->name, tok->len)) {
return v;
}
vl = vl->next;
}
vl = globals;
while (vl != NULL) {
v = vl->v;
if (strlen(v->name) == tok->len && strncmp(tok->str, v->name, tok->len)) {
return v;
}
vl = vl->next;
}
return NULL;
}
node_t *new_unary(node_kind_t k, node_t *n, token_t *tok) {
node_t *nd = (node_t *)malloc(sizeof(node_t));
init_node(nd, k, NULL, NULL, tok, n, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, 0);
return nd;
}
node_t *new_binary(node_kind_t k, node_t *lhs, node_t *rhs, token_t *tok) {
node_t *n = (node_t *)malloc(sizeof(node_t));
init_node(n, k, NULL, NULL, tok, lhs, rhs, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, 0);
return n;
}
node_t *new_number(int v, token_t *tok) {
node_t *n = (node_t *)malloc(sizeof(node_t));
init_node(n, nd_num, NULL, NULL, tok, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, v);
return n;
}
node_t *new_var(va_t *v, token_t *tok) {
node_t *n = (node_t *)malloc(sizeof(node_t));
init_node(n, nd_var, NULL, NULL, tok, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, v, 0);
return n;
}
char *new_label() {
char *name = (char *)malloc(sizeof(char) * 20);
sprintf(name, ".L.data.%d", label_count);
return name;
}
va_t *push_var(char *name, typ_t *ty, bool is_local) {
va_t *v = (va_t *)malloc(sizeof(va_t));
init_va(v, name, ty, is_local, NULL, 0, 0);
var_list_t vl = {.v = v};
if (is_local) {
vl.next = locals;
locals = &vl;
} else {
vl.next = globals;
globals = &vl;
}
return v;
}
node_t *primary() {
printf("%s\n", t->str);
if (consume("(") != NULL) {
node_t *n = expr();
expect(")");
return n;
}
token_t *tok = consume("sizeof");
if (tok != NULL) {
return new_unary(nd_sizeof, unary(), tok);
}
tok = consume_ident();
if (tok != NULL) {
if (consume("(") == NULL) {
node_t *n = (node_t *)malloc(sizeof(node_t));
init_node(n, nd_func_call, NULL, NULL, tok, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, tok->str, func_args(), NULL, 0);
return n;
}
va_t *v = find_var(tok);
if (v == NULL) {
error_tok(tok, "undefined variable");
}
return new_var(v, tok);
}
tok = t;
if (tok->kind == tk_str) {
t = t->next;
typ_t *cty = char_type();
typ_t *ty = array_of(cty, tok->content_length);
va_t *v = push_var(new_label(), ty, false);
v->contents = tok->contents;
v->content_length = tok->content_length;
return new_var(v, tok);
}
/* if (tok->kind == tk_ident) { */
/* t = t->next; */
/* return relational(); */
/* } */
/* } else if (tok->kind == tk_reserved) { */
/* t = t->next; */
/* return relational(); */
/* } */
return new_number(expect_number(), tok);
}
node_t *func_args() {
token_t *tok;
if ((tok = consume(")")) != NULL)
return NULL;
node_t *h = assign();
node_t *cur = h;
tok = consume(",");
while (tok != NULL) {
cur->next = assign();
cur = cur->next;
}
expect(")");
return h;
}
node_t *post_fix() {
node_t *n = primary();
for (token_t *tok = consume("["); tok != NULL; tok = consume("[")) {
node_t *exp = new_binary(nd_add, n, expr(), tok);
expect("]");
n = new_unary(nd_deref, exp, tok);
}
return n;
}
node_t *unary() {
if (consume("+") != NULL)
return unary();
token_t *tok;
if ((tok = consume("-")) != NULL)
return new_binary(nd_sub, new_number(0, tok), unary(), tok);
if ((tok = consume("&")) != NULL)
return new_unary(nd_addr, unary(), tok);
if ((tok = consume("*")) != NULL)
return new_unary(nd_deref, unary(), tok);
return post_fix();
}
node_t *mul() {
node_t *n = unary();
token_t *tok;
while (true) {
if ((tok = consume("*")) != NULL)
n = new_binary(nd_mul, n, unary(), tok);
else if ((tok = consume("/")) != NULL)
n = new_binary(nd_div, n, unary(), tok);
else
return n;
}
}
node_t *add() {
node_t *n = mul();
token_t *tok;
while (true) {
if ((tok = consume("+")) != NULL)
n = new_binary(nd_add, n, mul(), tok);
else if ((tok = consume("-")) != NULL)
n = new_binary(nd_sub, n, mul(), tok);
else
return n;
}
}
node_t *relational() {
node_t *n = add();
token_t *tok;
while (true) {
if ((tok = consume("<")) != NULL)
n = new_binary(nd_lt, n, add(), tok);
else if ((tok = consume("<=")) != NULL)
n = new_binary(nd_le, n, add(), tok);
else if ((tok = consume(">")) != NULL)
n = new_binary(nd_lt, n, add(), tok); // gt?
else if ((tok = consume(">=")) != NULL)
n = new_binary(nd_le, n, add(), tok); // ge?
else
return n;
}
}
node_t *equality() {
node_t *n = relational();
token_t *tok;
while (true) {
if ((tok = consume("==")) != NULL)
n = new_binary(nd_eq, n, relational(), tok);
else if ((tok = consume("!=")) != NULL)
n = new_binary(nd_ne, n, relational(), tok);
else
return n;
}
}
node_t *assign() {
node_t *n = equality();
token_t *tok = consume("=");
if (tok != NULL)
n = new_binary(nd_assign, n, assign(), tok);
return n;
}
node_t *expr() { return assign(); }
node_t *stmt() {
token_t *tok;
if ((tok = consume("return")) != NULL) {
node_t *n = new_unary(nd_ret, expr(), tok);
expect(";");
return n;
}
if ((tok = consume("if")) != NULL) {
node_t *n = (node_t *)malloc(sizeof(node_t));
init_node(n, nd_if, NULL, NULL, tok, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, 0);
expect("(");
n->cond = expr();
expect(")");
n->then = stmt();
if (consume("else") != NULL)
n->els = stmt();
return n;
}
if ((tok = consume("while")) != NULL) {
node_t *n = (node_t *)malloc(sizeof(node_t));
init_node(n, nd_while, NULL, NULL, tok, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, 0);
expect("(");
n->cond = expr();
expect(")");
n->then = stmt();
return n;
}
if ((tok = consume("for")) != NULL) {
node_t *n = (node_t *)malloc(sizeof(node_t));
init_node(n, nd_for, NULL, NULL, tok, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, 0);
expect("(");
if (consume(";") == NULL) {
n->init = read_expr_stmt();
expect(";");
}
if (consume(";") == NULL) {
n->cond = read_expr_stmt();
expect(";");
}
if (consume(")") == NULL) {
n->inc = read_expr_stmt();
expect(")");
}
n->then = stmt();
return n;
}
if ((tok = consume("{")) != NULL) {
node_t h;
node_t *current = &h;
while (consume("}") == NULL) {
current->next = stmt();
current = current->next;
}
node_t *n = (node_t *)malloc(sizeof(node_t));
init_node(n, nd_block, NULL, NULL, tok, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, h.next, NULL, NULL, NULL, 0);
return n;
}
if (is_type_name())
return declaration();
node_t *n = read_expr_stmt();
expect(";");
return n;
}
bool is_type_name() { return peek("char") || peek("int"); }
node_t *read_expr_stmt() {
token_t *tt = t;
return new_unary(nd_expr_stmt, expr(), tt);
}
node_t *declaration() {
token_t *tok = t;
typ_t *ty = base_type();
char *name = expect_ident();
ty = read_type_suffix(ty);
va_t *v = push_var(name, ty, true);
if (consume(";") != NULL) {
node_t *nd = (node_t *)malloc(sizeof(node_t));
init_node(nd, nd_null, NULL, NULL, tok, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, 0);
return nd;
}
expect("=");
node_t *lhs = (node_t *)malloc(sizeof(node_t));
init_node(lhs, nd_var, NULL, NULL, tok, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, v, 0);
node_t *rhs = expr();
expect(";");
node_t *n = new_binary(nd_assign, lhs, rhs, tok);
return new_unary(nd_expr_stmt, n, tok);
}
void global_var() {
typ_t *ty = base_type();
char *name = expect_ident();
ty = read_type_suffix(ty);
expect(";");
push_var(name, ty, false);
}
fun_t *function() {
locals = NULL;
base_type();
fun_t *fn = (fun_t *)malloc(sizeof(fun_t));
init_function(fn, NULL, expect_ident(), NULL, NULL, NULL, 0);
expect("(");
fn->params = read_func_params();
expect("{");
node_t h;
node_t *current = &h;
while (consume("}") == NULL) {
current->next = stmt();
current = current->next;
}
fn->node = h.next;
fn->locals = locals;
return fn;
}
typ_t *base_type() {
typ_t *ty = (typ_t *)malloc(sizeof(typ_t));
if (consume("char") != NULL)
ty = char_type();
else {
expect("int");
ty = int_type();
}
while (consume("*") != NULL)
ty = pointer_to(ty);
return ty;
}
typ_t *read_type_suffix(typ_t *b) {
if (consume("[") == NULL)
return b;
int arr_size = expect_number();
expect("]");
b = read_type_suffix(b);
return array_of(b, arr_size);
}
var_list_t *read_func_param() {
typ_t *ty = base_type();
char *name = expect_ident();
ty = read_type_suffix(ty);
var_list_t *res = (var_list_t *)malloc(sizeof(var_list_t));
init_var_list(res, NULL, push_var(name, ty, true));
return res;
}
var_list_t *read_func_params() {
if (consume(")") != NULL)
return NULL;
var_list_t *h = read_func_param();
var_list_t *current = h;
while (consume(")") == NULL) {
expect(",");
current->next = read_func_param();
current = current->next;
}
return h;
}
bool is_function() {
token_t *tok = t;
base_type();
bool is_ident = consume_ident() != NULL;
bool is_func_open = consume("(") != NULL;
t = tok;
return is_ident && is_func_open;
}
program_t *prog() {
fun_t h;
fun_t *current = &h;
globals = NULL;
while (!at_eof()) {
if (is_function()) {
current->next = function();
current = current->next;
} else
global_var();
}
program_t *pg = (program_t *)malloc(sizeof(program_t));
pg->globals = globals;
pg->fns = h.next;
return pg;
}