| ︙ | | | ︙ | |
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
|
int ch;
char* buf;
int buf_len;
struct token* tokens;
int token_len;
int isidentch(int c) {
return isalnum(c) || c == '_';
}
void next_ch() {
ch = getchar();
}
void accept_ch() {
buf = realloc(buf, ++buf_len);
buf[buf_len-1] = (char)ch;
}
void skip_ws() {
while(ch == ' ' || ch == '\t') {
next_ch();
}
}
void finalise_buf() {
buf = realloc(buf, ++buf_len);
buf[buf_len-1] = 0;
}
void reset_buf() { /* warning, memory leaking by design */
buf = 0;
buf_len = 0;
}
void add_token(enum token_type type, char* str) {
tokens = realloc(tokens, sizeof(struct token) * (++token_len));
tokens[token_len - 1].type = type;
tokens[token_len - 1].str = str;
}
void tokenise() {
enum token_type mode = TOK_NONE;
reset_buf();
tokens = 0;
token_len = 0;
next_ch();
do {
switch(mode) {
|
>
|
>
|
>
|
>
|
>
|
>
|
>
|
>
|
|
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
|
int ch;
char* buf;
int buf_len;
struct token* tokens;
int token_len;
int
isidentch(int c) {
return isalnum(c) || c == '_';
}
void
next_ch() {
ch = getchar();
}
void
accept_ch() {
buf = realloc(buf, ++buf_len);
buf[buf_len-1] = (char)ch;
}
void
skip_ws() {
while(ch == ' ' || ch == '\t') {
next_ch();
}
}
void
finalise_buf() {
buf = realloc(buf, ++buf_len);
buf[buf_len-1] = 0;
}
void
reset_buf() { /* warning, memory leaking by design */
buf = 0;
buf_len = 0;
}
void
add_token(enum token_type type, char* str) {
tokens = realloc(tokens, sizeof(struct token) * (++token_len));
tokens[token_len - 1].type = type;
tokens[token_len - 1].str = str;
}
void
tokenise() {
enum token_type mode = TOK_NONE;
reset_buf();
tokens = 0;
token_len = 0;
next_ch();
do {
switch(mode) {
|
| ︙ | | | ︙ | |
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
|
enum node_type type;
char* val;
char* data_type_str;
struct data_type* data_type;
struct function* fun;
};
struct node* node_new(enum node_type type) {
struct node* node = malloc(sizeof(struct node));
node->childs = 0;
node->child_count = 0;
node->name = 0;
node->type = type;
node->val = 0;
node->data_type_str = 0;
node->data_type = 0;
node->fun = 0;
return node;
}
void node_add_child(struct node* parent, struct node* child) {
int new_size = sizeof(struct node*) * (++parent->child_count);
parent->childs = realloc(parent->childs, new_size);
parent->childs[parent->child_count-1] = child;
}
struct function_parameter {
char* data_type_str;
struct data_type* data_type;
char* name;
};
struct function {
char* name;
char* return_type_str;
struct data_type* return_type;
struct function_parameter* params;
int param_count;
struct node* body;
};
struct function* function_new() {
struct function* fun = malloc(sizeof(struct function));
fun->name = 0;
fun->return_type_str = 0;
fun->return_type = 0;
fun->params = 0;
fun->param_count = 0;
fun->body = node_new(NODE_FUNBODY);
}
void function_add_param(struct function* fun, char* name, char* type) {
int new_size = sizeof(struct function_parameter) * (++fun->param_count);
fun->params = realloc(fun->params, new_size);
fun->params[fun->param_count-1].name = name;
fun->params[fun->param_count-1].data_type_str = type;
}
struct token* expect(enum token_type type) {
if(type == tokens[parse_pos].type) {
++parse_pos;
return &tokens[parse_pos-1];
}
else {
printf("expected (%d), got (%d), at (%d)\n",
type, tokens[parse_pos].type, parse_pos);
exit(-1);
}
}
int is_next_tok(enum token_type type) {
return tokens[parse_pos].type == type;
}
struct node* p_expr();
struct node* p_func_call() {
struct node* node = node_new(NODE_FUNCALL);
node->name = expect(TOK_IDENT)->str;
expect(TOK_PAREN_OPEN);
if(tokens[parse_pos].type != TOK_PAREN_CLOSE) {
node_add_child(node, p_expr());
while(tokens[parse_pos].type == TOK_COMMA) {
expect(TOK_COMMA);
node_add_child(node, p_expr());
}
}
expect(TOK_PAREN_CLOSE);
return node;
}
struct node* p_var_use() {
struct node* node = node_new(NODE_VAR_USE);
node->name = expect(TOK_IDENT)->str;
return node;
}
struct node* p_expr() {
switch(tokens[parse_pos].type) {
case TOK_IDENT:
if(tokens[parse_pos+1].type == TOK_PAREN_OPEN)
return p_func_call();
else
return p_var_use();
case TOK_LITERAL_INT:
{
struct node* node = node_new(NODE_LITERAL_INT);
node->val = expect(TOK_LITERAL_INT)->str;
return node;
}
default:
printf("can't parse expression, (%d)\n", tokens[parse_pos].type);
exit(-1);
}
}
struct node* p_statement() {
struct node* node;
switch(tokens[parse_pos].type) {
default:
node = p_expr();
expect(TOK_NEWLINE);
}
return node;
}
struct function* p_fun() {
struct function* fun = function_new();
expect(TOK_FUN);
fun->name = expect(TOK_IDENT)->str;
expect(TOK_PAREN_OPEN);
if(tokens[parse_pos].type != TOK_PAREN_CLOSE) {
for(;;) {
char* name;
|
>
|
>
|
|
>
>
|
>
|
>
|
|
>
|
>
|
>
|
>
|
>
|
>
|
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
|
enum node_type type;
char* val;
char* data_type_str;
struct data_type* data_type;
struct function* fun;
};
struct node*
node_new(enum node_type type) {
struct node* node = malloc(sizeof(struct node));
node->childs = 0;
node->child_count = 0;
node->name = 0;
node->type = type;
node->val = 0;
node->data_type_str = 0;
node->data_type = 0;
node->fun = 0;
return node;
}
void
node_add_child(struct node* parent, struct node* child) {
int new_size = sizeof(struct node*) * (++parent->child_count);
parent->childs = realloc(parent->childs, new_size);
parent->childs[parent->child_count-1] = child;
}
struct function_parameter {
char* data_type_str;
struct data_type* data_type;
char* name;
};
struct function {
char* name;
char* return_type_str;
struct data_type* return_type;
struct function_parameter* params;
int param_count;
struct node* body;
};
struct function*
function_new() {
struct function* fun = malloc(sizeof(struct function));
fun->name = 0;
fun->return_type_str = 0;
fun->return_type = 0;
fun->params = 0;
fun->param_count = 0;
fun->body = node_new(NODE_FUNBODY);
}
void
function_add_param(struct function* fun, char* name, char* type) {
int new_size = sizeof(struct function_parameter) * (++fun->param_count);
fun->params = realloc(fun->params, new_size);
fun->params[fun->param_count-1].name = name;
fun->params[fun->param_count-1].data_type_str = type;
}
struct token*
expect(enum token_type type) {
if(type == tokens[parse_pos].type) {
++parse_pos;
return &tokens[parse_pos-1];
}
else {
printf("expected (%d), got (%d), at (%d)\n",
type, tokens[parse_pos].type, parse_pos);
exit(-1);
}
}
int
is_next_tok(enum token_type type) {
return tokens[parse_pos].type == type;
}
struct node*
p_expr();
struct node*
p_func_call() {
struct node* node = node_new(NODE_FUNCALL);
node->name = expect(TOK_IDENT)->str;
expect(TOK_PAREN_OPEN);
if(tokens[parse_pos].type != TOK_PAREN_CLOSE) {
node_add_child(node, p_expr());
while(tokens[parse_pos].type == TOK_COMMA) {
expect(TOK_COMMA);
node_add_child(node, p_expr());
}
}
expect(TOK_PAREN_CLOSE);
return node;
}
struct node*
p_var_use() {
struct node* node = node_new(NODE_VAR_USE);
node->name = expect(TOK_IDENT)->str;
return node;
}
struct node*
p_expr() {
switch(tokens[parse_pos].type) {
case TOK_IDENT:
if(tokens[parse_pos+1].type == TOK_PAREN_OPEN)
return p_func_call();
else
return p_var_use();
case TOK_LITERAL_INT:
{
struct node* node = node_new(NODE_LITERAL_INT);
node->val = expect(TOK_LITERAL_INT)->str;
return node;
}
default:
printf("can't parse expression, (%d)\n", tokens[parse_pos].type);
exit(-1);
}
}
struct node*
p_statement() {
struct node* node;
switch(tokens[parse_pos].type) {
default:
node = p_expr();
expect(TOK_NEWLINE);
}
return node;
}
struct function*
p_fun() {
struct function* fun = function_new();
expect(TOK_FUN);
fun->name = expect(TOK_IDENT)->str;
expect(TOK_PAREN_OPEN);
if(tokens[parse_pos].type != TOK_PAREN_CLOSE) {
for(;;) {
char* name;
|
| ︙ | | | ︙ | |
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
expect(TOK_NEWLINE);
return fun;
}
struct function** functions;
int function_count;
void add_function(struct function* function) {
functions = realloc(functions, sizeof(struct function*)*(++function_count));
functions[function_count-1] = function;
}
void parse() {
functions = 0;
function_count = 0;
parse_pos = 0;
while(tokens[parse_pos].type != TOK_EOF) {
switch(tokens[parse_pos].type) {
case TOK_NEWLINE:
break;
|
>
|
>
|
|
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
expect(TOK_NEWLINE);
return fun;
}
struct function** functions;
int function_count;
void
add_function(struct function* function) {
functions = realloc(functions, sizeof(struct function*)*(++function_count));
functions[function_count-1] = function;
}
void
parse() {
functions = 0;
function_count = 0;
parse_pos = 0;
while(tokens[parse_pos].type != TOK_EOF) {
switch(tokens[parse_pos].type) {
case TOK_NEWLINE:
break;
|
| ︙ | | | ︙ | |
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
|
/* ANNOTATE */
/* */
/******************************************************************************/
struct data_type* data_types;
int data_type_count;
void add_data_type(char* name) {
int new_size = sizeof(struct data_type) * (++data_type_count);
data_types = realloc(data_types, new_size);
data_types[data_type_count-1].name = name;
}
void init_data_types() { /* internal data types */
data_types = 0;
data_type_count = 0;
add_data_type("void");
add_data_type("i8");
add_data_type("i16");
add_data_type("i32");
add_data_type("i64");
add_data_type("u8");
add_data_type("u16");
add_data_type("u32");
add_data_type("u64");
add_data_type("f32");
add_data_type("f64");
}
struct data_type* get_data_type(char* name) {
for(int i = 0; i < data_type_count; ++i) {
if(strcmp(name, data_types[i].name) == 0)
return &data_types[i];
}
return 0;
}
struct function* get_function(char* name, struct data_type** params, int param_count) {
for(int i = 0; i < function_count; ++i) {
struct function* fun = functions[i];
if(strcmp(fun->name, name) == 0 && fun->param_count == param_count) {
for(int j = 0; j < param_count; ++j) {
if(fun->params[j].data_type != params[j])
goto tryagain;
}
return fun;
}
tryagain: ;
}
return 0;
}
void annotate_data_type(struct data_type** data_type, char* name) {
if(name == 0) { /* nullpointer == void */
*data_type = &data_types[0];
return;
}
for(int i = 0; i < data_type_count; ++i) {
if(strcmp(data_types[i].name, name) == 0) {
*data_type = &data_types[i];
return;
}
}
printf("unknown datatype '%s'\n", name);
exit(-1);
}
void annotate_function_data_types(struct function* fun) {
annotate_data_type(&fun->return_type, fun->return_type_str);
for(int i = 0; i < fun->param_count; ++i) {
annotate_data_type(&fun->params[i].data_type, fun->params[i].data_type_str);
}
}
void annotate_node_data_types(struct node* node) {
for(int i = 0; i < node->child_count; ++i) {
annotate_node_data_types(node->childs[i]);
}
switch(node->type) {
case NODE_LITERAL_INT:
node->data_type = get_data_type("i32"); /* temporary */
break;
case NODE_FUNCALL: {
int size = sizeof(struct data_type*) * node->child_count;
struct data_type** params = malloc(size);
for(int i = 0; i < node->child_count; ++i) {
params[i] = node->childs[i]->data_type;
}
struct function* fun = get_function(node->name, params, node->child_count);
if(!fun) {
printf("error, function %s(", node->name);
for(int i = 0; i < node->child_count; ++i) {
printf("%s", params[i]->name);
if(i < node->child_count-1)
printf(", ");
}
|
>
|
>
|
|
>
>
|
>
|
>
|
|
>
>
|
>
|
|
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
|
/* ANNOTATE */
/* */
/******************************************************************************/
struct data_type* data_types;
int data_type_count;
void
add_data_type(char* name) {
int new_size = sizeof(struct data_type) * (++data_type_count);
data_types = realloc(data_types, new_size);
data_types[data_type_count-1].name = name;
}
void
init_data_types() { /* internal data types */
data_types = 0;
data_type_count = 0;
add_data_type("void");
add_data_type("i8");
add_data_type("i16");
add_data_type("i32");
add_data_type("i64");
add_data_type("u8");
add_data_type("u16");
add_data_type("u32");
add_data_type("u64");
add_data_type("f32");
add_data_type("f64");
}
struct data_type*
get_data_type(char* name) {
for(int i = 0; i < data_type_count; ++i) {
if(strcmp(name, data_types[i].name) == 0)
return &data_types[i];
}
return 0;
}
struct function*
get_function(char* name, struct data_type** params, int param_count) {
for(int i = 0; i < function_count; ++i) {
struct function* fun = functions[i];
if(strcmp(fun->name, name) == 0 && fun->param_count == param_count) {
for(int j = 0; j < param_count; ++j) {
if(fun->params[j].data_type != params[j])
goto tryagain;
}
return fun;
}
tryagain: ;
}
return 0;
}
void
annotate_data_type(struct data_type** data_type, char* name) {
if(name == 0) { /* nullpointer == void */
*data_type = &data_types[0];
return;
}
for(int i = 0; i < data_type_count; ++i) {
if(strcmp(data_types[i].name, name) == 0) {
*data_type = &data_types[i];
return;
}
}
printf("unknown datatype '%s'\n", name);
exit(-1);
}
void
annotate_function_data_types(struct function* fun) {
annotate_data_type(&fun->return_type, fun->return_type_str);
for(int i = 0; i < fun->param_count; ++i) {
annotate_data_type(&fun->params[i].data_type,
fun->params[i].data_type_str);
}
}
void
annotate_node_data_types(struct node* node) {
for(int i = 0; i < node->child_count; ++i) {
annotate_node_data_types(node->childs[i]);
}
switch(node->type) {
case NODE_LITERAL_INT:
node->data_type = get_data_type("i32"); /* temporary */
break;
case NODE_FUNCALL: {
int size = sizeof(struct data_type*) * node->child_count;
struct data_type** params = malloc(size);
for(int i = 0; i < node->child_count; ++i) {
params[i] = node->childs[i]->data_type;
}
struct function* fun;
fun = get_function(node->name, params, node->child_count);
if(!fun) {
printf("error, function %s(", node->name);
for(int i = 0; i < node->child_count; ++i) {
printf("%s", params[i]->name);
if(i < node->child_count-1)
printf(", ");
}
|
| ︙ | | | ︙ | |
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
|
break;
default:
printf("error, not yet annotating %i\n", node->type);
exit(-1);
}
}
void annotate_data_types() {
for(int i = 0; i < function_count; ++i) {
annotate_function_data_types(functions[i]);
}
/* done in seperate loop as the second step depends on all functions
having gone through the first step first */
for(int i = 0; i < function_count; ++i) {
annotate_node_data_types(functions[i]->body);
}
}
/******************************************************************************/
/* */
/* MAIN */
/* */
/******************************************************************************/
void print_indent(int num) {
for(int i = 0; i < num; ++i) printf(" ");
}
void print_data_type(struct data_type* data_type) {
printf("%s", data_type->name);
}
void print_mangled_function_name(struct function* fun) {
printf("%s__%s", fun->name, fun->return_type->name);
for(int i = 0; i < fun->param_count; ++i)
printf("__%s", fun->params[i].data_type->name);
}
void print_tree(struct node* node, int indent) {
switch(node->type) {
case NODE_ROOT:
for(int j = 0; j < node->child_count; ++j) {
print_tree(node->childs[j], 0);
printf("\n");
}
break;
|
>
|
>
|
>
|
>
|
>
|
|
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
|
break;
default:
printf("error, not yet annotating %i\n", node->type);
exit(-1);
}
}
void
annotate_data_types() {
for(int i = 0; i < function_count; ++i) {
annotate_function_data_types(functions[i]);
}
/* done in seperate loop as the second step depends on all functions
having gone through the first step first */
for(int i = 0; i < function_count; ++i) {
annotate_node_data_types(functions[i]->body);
}
}
/******************************************************************************/
/* */
/* MAIN */
/* */
/******************************************************************************/
void
print_indent(int num) {
for(int i = 0; i < num; ++i) printf(" ");
}
void
print_data_type(struct data_type* data_type) {
printf("%s", data_type->name);
}
void
print_mangled_function_name(struct function* fun) {
printf("%s__%s", fun->name, fun->return_type->name);
for(int i = 0; i < fun->param_count; ++i)
printf("__%s", fun->params[i].data_type->name);
}
void
print_tree(struct node* node, int indent) {
switch(node->type) {
case NODE_ROOT:
for(int j = 0; j < node->child_count; ++j) {
print_tree(node->childs[j], 0);
printf("\n");
}
break;
|
| ︙ | | | ︙ | |
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
printf("%s", node->val);
break;
default:
printf("(UNKNOWN TOKEN)");
}
}
void print_functions() {
for(int i = 0; i < function_count; ++i) {
struct function* fun = functions[i];
print_data_type(fun->return_type);
printf(" ");
print_mangled_function_name(fun);
printf("(");
for(int i = 0; i < fun->param_count; ++i) {
|
>
|
|
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
printf("%s", node->val);
break;
default:
printf("(UNKNOWN TOKEN)");
}
}
void
print_functions() {
for(int i = 0; i < function_count; ++i) {
struct function* fun = functions[i];
print_data_type(fun->return_type);
printf(" ");
print_mangled_function_name(fun);
printf("(");
for(int i = 0; i < fun->param_count; ++i) {
|
| ︙ | | | ︙ | |
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
print_tree(fun->body->childs[j], 1);
printf(";\n");
}
printf("}\n\n");
}
}
int main() {
tokenise();
parse();
init_data_types();
annotate_data_types();
print_functions();
return 0;
}
|
>
|
|
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
|
print_tree(fun->body->childs[j], 1);
printf(";\n");
}
printf("}\n\n");
}
}
int
main() {
tokenise();
parse();
init_data_types();
annotate_data_types();
print_functions();
return 0;
}
|
| ︙ | | | ︙ | |