-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
640 lines (597 loc) · 16.5 KB
/
parse.c
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#include "m99cc.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//TODO: shouldn't this be GET_TOKEN(T, I) ((Token *)(T)->data[(I)]) ?
#define GET_TOKEN(T, I) ((Token *)(T)->data[(I)])
static Vector *tokens;
static int pos;
enum {
SC_GLOBAL, // Global scope.
SC_LOCAL // Local scope.
};
// Error reporting function.
void error(char *s, char *message, char *file, int line) {
fprintf(stderr, s, message);
fprintf(stderr, " in %s, at %d.", file, line);
fprintf(stderr, "\n");
exit(1);
}
char *create_string_in_heap(char *str, int len) {
len = (len < IDENT_LEN - 1) ? len : IDENT_LEN - 1;
char *str_mem = malloc(sizeof(char) * (len + 1));
strncpy(str_mem, str, len);
str_mem[len] = '\0';
return str_mem;
}
Node *new_node(int op) {
Node *node = malloc(sizeof(Node));
node->ty = op;
node->lhs = NULL;
node->rhs = NULL;
node->val = 0;
node->name = NULL;
node->block = NULL;
return node;
}
Node *new_node_str(char *str, int len) {
Node *node = new_node(ND_STR);
node->name = create_string_in_heap(str, len);
return node;
}
Node *new_2term_node(int op, Node *lhs, Node *rhs) {
Node *node = new_node(op);
node->lhs = lhs;
node->rhs = rhs;
return node;
}
Node *new_node_num(int val) {
Node *node = new_node(ND_NUM);
node->val = val;
return node;
}
int get_array_size() {
if (GET_TOKEN(tokens, pos)->ty != '[') {
return 0;
}
++pos;
int num = 0;
if (GET_TOKEN(tokens, pos)->ty == TK_NUM) {
num = GET_TOKEN(tokens, pos++)->val;
}
if (GET_TOKEN(tokens, pos++)->ty != ']') {
fprintf(stderr, "Closing bracket ']' is missing (get_array_size): \"%s\"\n",
GET_TOKEN(tokens, pos - 1)->input);
exit(1);
}
return num;
}
Node *new_node_ident(char *name, int len) {
Node *node = malloc(sizeof(Node));
node->ty = ND_IDENT;
node->lhs = NULL;
node->rhs = NULL;
node->val = 0;
node->name = create_string_in_heap(name, len);
node->block = NULL;
return node;
}
#define HIGH_PRIORITY (9)
#define STRUCT_PRIOIRTY (HIGH_PRIORITY - 1)
#define MULTIPLE_PRIORITY (HIGH_PRIORITY - 2)
#define ADD_PRIORITY (HIGH_PRIORITY - 3)
#define COMPARE_PRIORITY (HIGH_PRIORITY - 4)
#define EQUAL_PRIORITY (HIGH_PRIORITY - 5)
#define AND_PRIORITY (HIGH_PRIORITY - 6)
#define OR_PRIORITY (HIGH_PRIORITY - 7)
#define ASSIGN_PRIORITY (HIGH_PRIORITY - 8)
#define LOW_PRIORITY (0)
int operation_priority(int token_type) {
switch (token_type) {
case '.':
case TK_ARROW:
return STRUCT_PRIOIRTY;
case '*':
case '/':
case '%':
return MULTIPLE_PRIORITY;
case '+':
case '-':
return ADD_PRIORITY;
case '<':
case '>':
case TK_GE:
case TK_LE:
return COMPARE_PRIORITY;
case TK_EQ:
case TK_NE:
return EQUAL_PRIORITY;
case TK_AND:
return AND_PRIORITY;
case TK_OR:
return OR_PRIORITY;
case '=':
return ASSIGN_PRIORITY;
default:
return LOW_PRIORITY;
}
}
int get_node_type_from_token(int token_type) {
switch (token_type) {
case TK_NUM:
return ND_NUM;
case TK_IDENT:
return ND_IDENT;
case TK_EQ:
return ND_EQ;
case TK_NE:
return ND_NE;
case TK_LE:
return ND_LE;
case TK_GE:
return ND_GE;
case TK_AND:
return ND_AND;
case TK_OR:
return ND_OR;
case TK_STR:
return ND_STR;
case TK_PE:
return ND_PE;
case TK_ME:
return ND_ME;
default:
// For other operations (*/+- others, token_type -> node_type)
return token_type;
}
}
int get_node_type_from_token_single(int token_type) {
if (token_type == '*') {
return ND_DEREF;
}
return get_node_type_from_token(token_type);
}
int get_dtype_from_token(int token_type) {
switch (token_type) {
case TK_VOID:
return DT_VOID;
case TK_CHAR:
return DT_CHAR;
case TK_INT:
return DT_INT;
case TK_STRUCT:
return DT_STRUCT;
default:
return DT_INVALID;
}
}
// TODO: Re-write this with global pos.
Node *get_data_type_node_at_pos() {
Token *tk = GET_TOKEN(tokens, pos++);
int dtype = get_dtype_from_token(tk->ty);
Node *node = new_node(ND_DATATYPE);
if (dtype == DT_VOID) {
node->lhs = new_node(ND_VOID);
} else if (dtype == DT_INT) {
node->lhs = new_node(ND_INT);
} else if (dtype == DT_CHAR) {
node->lhs = new_node(ND_CHAR);
} else {
error("Invalid data type token %s", tk->input, __FILE__, __LINE__);
}
while (GET_TOKEN(tokens, pos)->ty == '*') {
pos++;
node = new_2term_node(ND_DATATYPE, new_node(ND_PNT), node);
}
return node;
}
Node *term();
Node *argument();
Node *expression(int priority) {
if (priority == HIGH_PRIORITY) {
return term();
}
Node *lhs = expression(priority + 1);
if (GET_TOKEN(tokens, pos)->ty == ';') {
return lhs;
}
int token_type = GET_TOKEN(tokens, pos)->ty;
if (operation_priority(token_type) == priority) {
pos++;
int node_type = get_node_type_from_token(token_type);
Node *rhs = expression(priority);
return new_2term_node(node_type, lhs, rhs);
}
return lhs;
}
// term : number | identifier | ( expression )
Node *term() {
// Simple number
if (GET_TOKEN(tokens, pos)->ty == TK_NUM) {
return new_node_num(GET_TOKEN(tokens, pos++)->val);
}
Token *tk = GET_TOKEN(tokens, pos);
if (tk->ty == TK_STR) {
char *str = tk->input;
int len = tk->len;
pos++;
return new_node_str(str, len);
}
// Variable or Function
tk = GET_TOKEN(tokens, pos);
if (tk->ty == TK_IDENT) {
Node *id = new_node_ident(tk->input,
tk->len);
pos++;
// if followed by (, it's a function call.
if (GET_TOKEN(tokens, pos)->ty == '(') {
++pos;
Node *arg = NULL;
// Argument exists.
if (GET_TOKEN(tokens, pos)->ty != ')') {
arg = expression(ASSIGN_PRIORITY);
}
if (GET_TOKEN(tokens, pos)->ty != ')') {
error("No right parenthesis corresponding to left parenthesis"
" (term, function): \"%s\"",
GET_TOKEN(tokens, pos)->input, __FILE__, __LINE__);
}
pos++;
Node *node = new_2term_node(ND_FUNCCALL, id, arg);
return node;
}
// If not followed by (, it's a variable.
// is it array?
Node *node = id;
if (GET_TOKEN(tokens, pos)->ty == '[') {
pos++;
Node *index = expression(ASSIGN_PRIORITY);
if (GET_TOKEN(tokens, pos++)->ty != ']') {
fprintf(stderr, "Closing bracket ']' missing. \"%s\"\n",
GET_TOKEN(tokens, pos - 1)->input);
exit(1);
}
node = new_2term_node(ND_DEREF, new_2term_node('+', id, index), NULL);
}
return node;
}
// "( expression )"
if (GET_TOKEN(tokens, pos)->ty == '(') {
pos++;
Node *node = expression(ASSIGN_PRIORITY);
if (GET_TOKEN(tokens, pos)->ty != ')') {
error("No right parenthesis corresponding to left parenthesis "
"(term, parenthesis): \"%s\"",
GET_TOKEN(tokens, pos)->input, __FILE__, __LINE__);
}
pos++;
return node;
}
// Single term operators
tk = GET_TOKEN(tokens, pos);
if (tk->ty == '+' || tk->ty == '-' ||
tk->ty == '*' || tk->ty == '&') {
int type = tk->ty;
pos++;
Node *lhs = term();
return new_2term_node(get_node_type_from_token_single(type), lhs, NULL);
}
if (tk->ty == TK_INC || tk->ty == TK_DEC) {
// convert ++x to (x += 1).
int operation = (tk->ty == TK_INC) ? ND_PE : ND_ME;
pos++;
Node *lhs = term();
int step = 1;
Node *rhs = new_node_num(step);
return new_2term_node(operation, lhs, rhs);
}
// Code should not reach here.
error("Unexpected token (parse.c term): \"%s\"",
GET_TOKEN(tokens, pos)->input, __FILE__, __LINE__);
return NULL;
}
Node *argument() {
// TODO: make argument a list.
Node *node_dt = get_data_type_node_at_pos();
Token *tk = GET_TOKEN(tokens, pos);
if (tk->ty != TK_IDENT) {
error("Invalid (not IDENT) token in argument declaration position \"%s\"",
tk->input, __FILE__, __LINE__);
}
// argument name?
tk = GET_TOKEN(tokens, pos);
char *name = create_string_in_heap(tk->input,
tk->len);
int array_size = get_array_size();
if (array_size > 0) {
// Make the node_dt pointer node
node_dt = new_2term_node(ND_DATATYPE, new_node(ND_PNT), node_dt);
}
tk = GET_TOKEN(tokens, pos);
Node *id =
new_node_ident(tk->input,
tk->len);
id->name = name;
Node *arg_node = new_2term_node(ND_DECLARE, node_dt, id);
pos++;
return arg_node;
}
Node *assign_dash() {
Node *lhs = expression(ASSIGN_PRIORITY);
int token_type = GET_TOKEN(tokens, pos)->ty;
if (token_type == '=' || token_type == TK_PE || token_type == TK_ME) {
pos++;
return new_2term_node(get_node_type_from_token(token_type), lhs, assign_dash());
}
return lhs;
}
Node *assign() {
Node *lhs = assign_dash();
if (GET_TOKEN(tokens, pos)->ty == '(') {
while (GET_TOKEN(tokens, pos)->ty != ')') {
++pos;
}
return lhs;
}
while (GET_TOKEN(tokens, pos)->ty == ';') {
pos++;
}
return lhs;
}
void add_code(Vector *code, int i) {
if (code->len > i)
return;
vec_push(code, malloc(sizeof(Node)));
}
void code_block(Vector *code);
Node *if_node() {
pos++;
if (GET_TOKEN(tokens, pos++)->ty != '(') {
error("Left paraenthesis '(' missing (if): \"%s\"\n",
GET_TOKEN(tokens, pos - 1)->input, __FILE__, __LINE__);
}
Node *cond = expression(ASSIGN_PRIORITY);
if (GET_TOKEN(tokens, pos++)->ty != ')') {
error("Right paraenthesis ')' missing (if): \"%s\"\n",
GET_TOKEN(tokens, pos - 1)->input, __FILE__, __LINE__);
}
Node *ifnd = new_2term_node(ND_IF, cond, NULL);
ifnd->block = new_vector();
code_block(ifnd->block);
if (GET_TOKEN(tokens, pos)->ty == TK_ELSE) {
pos++;
if (GET_TOKEN(tokens, pos)->ty == TK_IF) {
ifnd->rhs = if_node();
} else {
ifnd->rhs = new_node(ND_BLOCK);
ifnd->rhs->block = new_vector();
code_block(ifnd->rhs->block);
vec_push(ifnd->rhs->block, NULL);
}
}
vec_push(ifnd->block, NULL);
// TODO: Add else
return ifnd;
}
Node *while_node() {
pos++;
Token *tk = GET_TOKEN(tokens, pos++);
if (tk->ty != '(') {
error("Left paraenthesis '(' missing (while): \"%s\"\n",
tk->input, __FILE__, __LINE__);
}
Node *cond = expression(ASSIGN_PRIORITY);
tk = GET_TOKEN(tokens, pos++);
if (tk->ty != ')') {
error("Right paraenthesis ')' missing (while): \"%s\"\n",
tk->input, __FILE__, __LINE__);
}
Node *while_nd = new_2term_node(ND_WHILE, cond, NULL);
while_nd->block = new_vector();
code_block(while_nd->block);
vec_push(while_nd->block, NULL);
return while_nd;
}
void for_node(Vector *code) {
// TODO: allow multiple statement using ','.
pos++;
Token *tk = GET_TOKEN(tokens, pos++);
if (tk->ty != '(') {
error("Left paraenthesis '(' missing (for): \"%s\"\n",
tk->input, __FILE__, __LINE__);
}
Node *init = expression(ASSIGN_PRIORITY);
vec_push(code, init);
tk = GET_TOKEN(tokens, pos++);
if (tk->ty != ';') {
error("1st Semicolon ';' missing (for): \"%s\"\n",
tk->input, __FILE__, __LINE__);
}
Node *cond = expression(ASSIGN_PRIORITY);
tk = GET_TOKEN(tokens, pos++);
if (tk->ty != ';') {
error("2nd Semicolon ';' missing (for): \"%s\"\n",
tk->input, __FILE__, __LINE__);
}
Node *increment = expression(ASSIGN_PRIORITY);
tk = GET_TOKEN(tokens, pos++);
if (tk->ty != ')') {
error("Right paraenthesis '(' missing (for): \"%s\"\n",
tk->input, __FILE__, __LINE__);
}
Node *for_nd = new_2term_node(ND_WHILE, cond, NULL);
vec_push(code, for_nd);
// Add the increment code and {} block.
for_nd->block = new_vector();
code_block(for_nd->block);
vec_push(for_nd->block, increment);
vec_push(for_nd->block, NULL);
}
Node *return_node() {
pos++;
Node *nd = expression(ASSIGN_PRIORITY);
Node *return_nd = new_2term_node(ND_RETURN, nd, NULL);
while (GET_TOKEN(tokens, pos)->ty == ';') {
pos++;
}
return return_nd;
}
void declaration_node(Vector *code);
void code_block(Vector *code) {
Token *tk = GET_TOKEN(tokens, pos++);
if (tk->ty != '{') {
error("Left brace '{' missing (code_block): \"%s\"",
tk->input, __FILE__, __LINE__);
}
int ty;
while ((ty = GET_TOKEN(tokens, pos)->ty) != '}') {
if (ty == TK_IF) {
vec_push(code, if_node());
} else if (ty == TK_WHILE) {
vec_push(code, while_node());
} else if (ty == TK_FOR) {
for_node(code);
} else if (ty == TK_RETURN) {
vec_push(code, return_node());
} else {
int dtype = get_dtype_from_token(ty);
if (dtype != DT_INVALID) {
// TODO: declaration_node inside function can not generate function.
// Check.
declaration_node(code);
} else {
vec_push(code, assign());
}
}
}
if (GET_TOKEN(tokens, pos++)->ty != '}') {
error("Right brace '}' missing (code_block): \"%s\"",
GET_TOKEN(tokens, pos - 1)->input, __FILE__, __LINE__);
}
}
Node *identifier_node() {
Token *tk = GET_TOKEN(tokens, pos);
if (tk->ty != TK_IDENT) {
error("Unexpected token: \"%s\"", tk->input, __FILE__, __LINE__);
}
Node *id =
new_node_ident(tk->input,
tk->len);
pos++;
// global or local variable.
if (GET_TOKEN(tokens, pos)->ty != '(') {
id->val = get_array_size();
// TODO: add initialization.
return id;
}
// TODO: implement function declaration.
// function definition..
Node *arg = NULL;
pos++;
if (GET_TOKEN(tokens, pos)->ty != ')') {
arg = argument();
}
if (GET_TOKEN(tokens, pos++)->ty != ')') {
// TODO: add support of nmultiple arguments.
error("Right parenthesis ')' missing (function): \"%s\"",
GET_TOKEN(tokens, pos - 1)->input, __FILE__, __LINE__);
}
Node *f = new_2term_node(ND_FUNCDEF, id, arg);
f->block = new_vector();
code_block(f->block);
vec_push(f->block, NULL);
return f;
}
Node *identifier_sequence() {
Node *id = identifier_node();
if (GET_TOKEN(tokens, pos)->ty == ',') {
pos++;
Node *ids = identifier_sequence();
return new_2term_node(ND_IDENTSEQ, id, ids);
}
if (GET_TOKEN(tokens, pos)->ty == ';') {
pos++;
return id;
}
return id;
}
Node *struct_identifier_node() {
Node *data_type_node = get_data_type_node_at_pos();
Node *id = identifier_node();
return new_2term_node(ND_DECLARE, data_type_node, id);
}
Node *struct_identifier_sequence() {
Node *node = struct_identifier_node();
if (GET_TOKEN(tokens, pos++)->ty != ';') {
error("Struct token not ending with ';' (initialization not supported yet) %s",
GET_TOKEN(tokens, pos - 1)->input, __FILE__, __LINE__);
}
Node *next_node = NULL;
if (GET_TOKEN(tokens, pos)->ty != '}') {
next_node = struct_identifier_sequence();
}
return new_2term_node(ND_IDENTSEQ, node, next_node);
}
Node *get_data_type_node_from_struct_node(Node *struct_node) {
Node *node = new_2term_node(ND_DATATYPE, struct_node, NULL);
while (GET_TOKEN(tokens, pos)->ty == '*') {
pos++;
node = new_2term_node(ND_DATATYPE, new_node(ND_PNT), node);
}
return node;
}
Node *struct_variable_declaration(Node *struct_node) {
Node *data_node = get_data_type_node_from_struct_node(struct_node);
Node *ids = identifier_sequence();
Node *node = new_2term_node(ND_DECLARE, data_node, ids);
return node;
}
Node *struct_declaration() {
Node *struct_node = new_node(ND_STRUCT);
++pos;
Token *tk = GET_TOKEN(tokens, pos);
struct_node->name = create_string_in_heap(tk->input,
tk->len);
pos++;
tk = GET_TOKEN(tokens, pos);
if (tk->ty != '{') {
// if '{' doesn't follow, it's a variable;
return struct_variable_declaration(struct_node);
}
pos++;
struct_node->lhs = struct_identifier_sequence();
tk = GET_TOKEN(tokens, pos++);
if (tk->ty != '}') {
error("Struct declaration missing '}'. (%s)", tk->input, __FILE__, __LINE__);
}
tk = GET_TOKEN(tokens, pos++);
if (tk->ty != ';') {
error("Struct declaration missing ';'. (%s)", tk->input, __FILE__, __LINE__);
}
return struct_node;
}
void declaration_node(Vector *code) {
// Struct declaration.
if (GET_TOKEN(tokens, pos)->ty == TK_STRUCT) {
vec_push(code, struct_declaration());
return;
}
// Regular declaration of variable and function.
Node *data_node = get_data_type_node_at_pos();
Node *node_ids = identifier_sequence();
Node *declaration = new_2term_node(ND_DECLARE, data_node, node_ids);
vec_push(code, declaration);
return;
}
Vector *parse(Vector *tokens_input) {
tokens = tokens_input;
pos = 0;
Vector *code = new_vector();
while (GET_TOKEN(tokens, pos)->ty != TK_EOF) {
declaration_node(code);
}
vec_push(code, NULL);
return code;
}