Repository navigation
Expand file tree
/
Copy pathparse.c
More file actions
1387 lines (1252 loc) · 43.9 KB
/
Copy pathparse.c
File metadata and controls
1387 lines (1252 loc) · 43.9 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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "parse.h"
#include "token.h"
#include "util.h"
#include "diag.h"
#include "type.h"
#include "tu.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
typedef int node_pos;
#define TOKEN(context) (&context->tokens[context->position])
#define PEEK(context) (&context->tokens[context->position + 1])
#define PEEKN(context, n) (&context->tokens[context->position + (n)])
struct context {
struct tu *tu;
struct token *tokens;
int position;
const char *source;
int errors;
};
// static functions
static bool is_typename(struct context *, struct token *first, size_t count);
static bool more_data(struct context *);
static struct node *new(struct context *, enum node_type);
static void report_error(struct context *, const char *message, ...);
static struct node *report_error_node(struct context *, const char *message, ...);
static void pass(struct context *);
static void eat(struct context *, int token_type, const char *function_name);
static struct node *parse_assignment_expression(struct context *);
static struct node *parse_expression(struct context *);
static struct node *parse_declaration(struct context *);
static struct node *parse_statement(struct context *);
static struct node *parse_external_definition(struct context *);
int parse(struct tu *tu) {
struct context *context = &(struct context){
.tu = tu,
.tokens = tu->tokens,
.source = tu->source,
};
struct node *root = new(context, NODE_ROOT);
root->token = tu->tokens;
root->type = NODE_ROOT;
int n = 0;
while (more_data(context) && context->errors == 0) {
list_push(&root->root.children, parse_external_definition(context));
}
tu->ast_root = root;
return context->errors;
}
static bool more_data(struct context *context) {
return TOKEN(context)->type != TOKEN_EOF;
}
// TODO: make this a pool or bump allocator of some kind so I can free these without
// recursing through the entire tree.
static struct node *new(struct context *context, enum node_type type) {
struct node *node = calloc(1, sizeof(struct node));
node->type = type;
node->token = TOKEN(context);
return node;
}
static void report_error(struct context *context, const char *message, ...) {
va_list args;
va_start(args, message);
vprint_error_token(context->tu, TOKEN(context), message, args);
context->errors += 1;
va_end(args);
}
static struct node *report_error_node(struct context *context, const char *message, ...) {
va_list args;
va_start(args, message);
struct node *node = new(context, NODE_ERROR);
vprint_error_node(context->tu, node, message, args);
context->errors += 1;
pass(context);
va_end(args);
return node;
}
static void pass(struct context *context) {
context->position += 1;
}
// This isn't very useful right now since it just advances no matter what.
// It should probably add an error node or something, but I'm not sure that
// can be genericized.
static void eat(struct context *context, int token_type, const char *function_name) {
if (TOKEN(context)->type != token_type) {
print_error_token(context->tu, TOKEN(context),
"expected '%s', found '%s' in %s",
token_type_string(token_type),
token_type_string(TOKEN(context)->type),
function_name);
}
pass(context);
}
static void fast_forward(struct context *context, int token_type) {
}
#define eat(ctx, typ) eat(ctx, typ, __func__)
static void print_space(int level) {
for (int i = 0; i < level; i++) fputs(" ", stderr);
}
const char *node_type_strings[NODE_TYPE_COUNT] = {
[NODE_NULL] = "NODE_NULL",
[NODE_ROOT] = "NODE_ROOT",
[NODE_BINARY_OP] = "NODE_BINARY_OP",
[NODE_UNARY_OP] = "NODE_UNARY_OP",
[NODE_POSTFIX_OP] = "NODE_POSTFIX_OP",
[NODE_IDENT] = "NODE_IDENT",
[NODE_INT_LITERAL] = "NODE_INT_LITERAL",
[NODE_FLOAT_LITERAL] = "NODE_FLOAT_LITERAL",
[NODE_STRING_LITERAL] = "NODE_STRING_LITERAL",
[NODE_ERROR] = "NODE_ERROR",
[NODE_MEMBER] = "NODE_MEMBER",
[NODE_SUBSCRIPT] = "NODE_SUBSCRIPT",
[NODE_TERNARY] = "NODE_TERNARY",
[NODE_FUNCTION_CALL] = "NODE_FUNCTION_CALL",
[NODE_DECLARATION] = "NODE_DECLARATION",
[NODE_TYPE_SPECIFIER] = "NODE_TYPE_SPECIFIER",
[NODE_DECLARATOR] = "NODE_DECLARATOR",
[NODE_ARRAY_DECLARATOR] = "NODE_ARRAY_DECLARATOR",
[NODE_FUNCTION_DECLARATOR] = "NODE_FUNCTION_DECLARATOR",
[NODE_FUNCTION_DEFINITION] = "NODE_FUNCTION_DEFINITION",
[NODE_STATIC_ASSERT] = "NODE_STATIC_ASSERT",
[NODE_BLOCK] = "NODE_BLOCK",
[NODE_LABEL] = "NODE_LABEL",
[NODE_RETURN] = "NODE_RETURN",
[NODE_IF] = "NODE_IF",
[NODE_WHILE] = "NODE_WHILE",
[NODE_DO] = "NODE_DO",
[NODE_FOR] = "NODE_FOR",
[NODE_GOTO] = "NODE_GOTO",
[NODE_SWITCH] = "NODE_SWITCH",
[NODE_CASE] = "NODE_CASE",
[NODE_BREAK] = "NODE_BREAK",
[NODE_CONTINUE] = "NODE_CONTINUE",
[NODE_DEFAULT] = "NODE_DEFAULT",
[NODE_STRUCT] = "NODE_STRUCT",
[NODE_ENUM] = "NODE_ENUM",
[NODE_UNION] = "NODE_UNION",
};
#define TOKEN_STR(token) (&(tu)->source[(token)->index])
#define PRINT_TOKEN(token) fprintf(stderr, "%.*s", (token)->len, TOKEN_STR(token))
static void print_dcl_flat(struct tu *tu, struct node *node) {
if (node->type != NODE_DECLARATOR) {
print_internal_error(tu, "attempting to print non-d with print_dcl_flat");
return;
}
if (node->d.inner) {
print_dcl_flat(tu, node->d.inner);
fprintf(stderr, " -> ");
}
if (node->d.name) {
PRINT_TOKEN(node->d.name);
} else if (node->d.nameless) {
fprintf(stderr, "(nameless)");
}
}
static void print_dcl_list(struct tu *tu, node_list_t *nodes) {
fprintf(stderr, "print_dcl_list: ");
for_each (nodes) {
struct node *d = list_first(&(*it)->decl.declarators);
if (d) {
print_dcl_flat(tu, d);
}
fprintf(stderr, " ");
print_type(tu, (*it)->decl.decl_spec_c_type);
if (!list_islast(nodes, *it)) {
fprintf(stderr, ", ");
}
}
}
#define RECUR(node) print_ast_recursive(nullptr, tu, (node), level + 1)
#define RECUR_INFO(info, node) print_ast_recursive((info), tu, (node), level + 1)
static void print_ast_recursive(const char *info, struct tu *tu, struct node *node, int level) {
print_space(level);
if (info) fprintf(stderr, "%s ", info);
if (!node) {
print_internal_error(tu, "ast node is nullptr");
return;
}
if (level > 50) {
print_internal_error(tu, "ast more than 50 levels deep, loop?");
exit(1);
}
if (node == tu->ast_root && level > 0) {
print_internal_error(tu, "found root node in non-root position");
exit(1);
}
struct token *token = node->token;
const char *source = tu->source;
switch (node->type) {
case NODE_ROOT: {
fprintf(stderr, "root:\n");
for_each (&node->root.children) {
RECUR(*it);
}
break;
}
case NODE_BLOCK: {
fprintf(stderr, "block:\n");
for_each (&node->block.children) {
RECUR(*it);
}
break;
}
case NODE_INT_LITERAL: {
fprintf(stderr, "int: %.*s (%llu)\n", token->len, &source[token->index], token->int_.value);
break;
}
case NODE_FLOAT_LITERAL: {
fprintf(stderr, "float: %.*s (%f)\n", token->len, &source[token->index], token->float_.value);
break;
}
case NODE_STRING_LITERAL:
fprintf(stderr, "string: %.*s\n", token->len, &source[token->index]);
break;
case NODE_IDENT: {
fprintf(stderr, "ident: %.*s\n", token->len, &source[token->index]);
break;
}
case NODE_BINARY_OP: {
fprintf(stderr, "binop: %.*s\n", token->len, &source[token->index]);
RECUR(node->binop.lhs);
RECUR(node->binop.rhs);
break;
}
case NODE_UNARY_OP: {
fprintf(stderr, "unop: %.*s\n", token->len, &source[token->index]);
RECUR(node->unary_op.inner);
break;
}
case NODE_POSTFIX_OP: {
fprintf(stderr, "postfix: %.*s\n", token->len, &source[token->index]);
RECUR(node->unary_op.inner);
break;
}
case NODE_SUBSCRIPT: {
fprintf(stderr, "subscript:\n");
RECUR_INFO("arr:", node->subscript.inner);
RECUR_INFO("sub:", node->subscript.subscript);
break;
}
case NODE_TERNARY: {
fprintf(stderr, "ternary:\n");
RECUR_INFO("cnd:", node->ternary.condition);
RECUR_INFO("tru:", node->ternary.branch_true);
RECUR_INFO("fls:", node->ternary.branch_false);
break;
}
case NODE_FUNCTION_CALL: {
fprintf(stderr, "funcall:\n");
RECUR_INFO("fun:", node->funcall.inner);
for_each (&node->funcall.args) {
RECUR_INFO("arg", *it);
}
break;
}
case NODE_DECLARATION: {
fprintf(stderr, "decl:\n");
print_space(level + 1);
fprintf(stderr, "typ: ");
print_type(tu, node->decl.decl_spec_c_type);
fprintf(stderr, "\n");
for_each (&node->decl.declarators) {
RECUR_INFO("dcl:", *it);
}
break;
}
case NODE_TYPE_SPECIFIER: {
fprintf(stderr, "decl_spec: %.*s\n", token->len, &source[token->index]);
break;
}
case NODE_DECLARATOR:
case NODE_FUNCTION_DECLARATOR:
case NODE_ARRAY_DECLARATOR: {
fprintf(stderr, "d: ");
struct node *n = node;
while (true) {
token = n->token;
if (n->type == NODE_DECLARATOR) {
fprintf(stderr, "%.*s", token->len, &source[token->index]);
} else if (n->type == NODE_FUNCTION_DECLARATOR) {
fprintf(stderr, "()");
} else if (n->type == NODE_ARRAY_DECLARATOR) {
fprintf(stderr, "[]");
}
if (!n->d.inner) {
fprintf(stderr, "\n");
break;
}
fprintf(stderr, " -> ");
n = n->d.inner;
}
if (node->d.initializer)
RECUR_INFO("ini:", node->d.initializer);
break;
}
case NODE_STATIC_ASSERT: {
fprintf(stderr, "static assert:\n");
RECUR_INFO("tst:", node->st_assert.expr);
if (node->st_assert.message)
RECUR_INFO("msg:", node->st_assert.message);
break;
}
case NODE_FUNCTION_DEFINITION: {
fprintf(stderr, "function:\n");
RECUR_INFO("typ:", node->fun.decl);
RECUR_INFO("bdy:", node->fun.body);
break;
}
case NODE_RETURN:
fprintf(stderr, "return:\n");
RECUR(node->ret.expr);
break;
case NODE_IF:
fprintf(stderr, "if:\n");
RECUR_INFO("cnd:", node->if_.cond);
RECUR_INFO("yes:", node->if_.block_true);
if (node->if_.block_false)
RECUR_INFO("no: ", node->if_.block_false);
break;
case NODE_WHILE:
fprintf(stderr, "while:\n");
RECUR_INFO("cnd:", node->while_.cond);
RECUR_INFO("blk:", node->while_.block);
break;
case NODE_NULL:
fprintf(stderr, "null:\n");
break;
case NODE_ERROR:
fprintf(stderr, "error: %.*s\n", token->len, &source[token->index]);
break;
case NODE_MEMBER:
fprintf(stderr, "member:\n");
RECUR_INFO("val:", node->member.inner);
RECUR_INFO("nam:", node->member.ident);
break;
case NODE_LABEL:
fprintf(stderr, "label:\n");
RECUR(node->label.name);
break;
case NODE_DO:
fprintf(stderr, "do:\n");
RECUR_INFO("blk:", node->do_.block);
RECUR_INFO("cnd:", node->do_.cond);
break;
case NODE_FOR:
fprintf(stderr, "for:\n");
RECUR_INFO("ini:", node->for_.init);
RECUR_INFO("cnd:", node->for_.cond);
RECUR_INFO("nxt:", node->for_.next);
RECUR_INFO("blk:", node->for_.block);
break;
case NODE_GOTO:
fprintf(stderr, "goto:\n");
RECUR(node->goto_.label);
break;
case NODE_SWITCH:
fprintf(stderr, "switch:\n");
RECUR_INFO("exp:", node->switch_.expr);
RECUR_INFO("blk:", node->switch_.block);
break;
case NODE_CASE:
fprintf(stderr, "case:\n");
RECUR(node->case_.value);
break;
case NODE_CONTINUE:
fprintf(stderr, "continue:\n");
break;
case NODE_BREAK:
fprintf(stderr, "break:\n");
break;
case NODE_DEFAULT:
fprintf(stderr, "default:\n");
break;
case NODE_STRUCT:
fprintf(stderr, "struct:\n");
for_each (&node->struct_.decls) {
RECUR(*it);
}
break;
case NODE_UNION:
fprintf(stderr, "union:\n");
for_each (&node->struct_.decls) {
RECUR(*it);
}
break;
default:
fprintf(stderr, "UNKNOWN:\n");
break;
}
}
#undef RECUR
#undef RECUR_INFO
void print_ast(struct tu *tu) {
print_ast_recursive(nullptr, tu, tu->ast_root, 0);
}
struct token *node_begin(struct node *node) {
switch (node->type) {
case NODE_BINARY_OP:
return node_begin(node->binop.lhs);
case NODE_POSTFIX_OP:
return node_begin(node->unary_op.inner);
case NODE_FUNCTION_DECLARATOR:
case NODE_ARRAY_DECLARATOR:
return node_begin(node->d.inner);
case NODE_TERNARY:
return node_begin(node->ternary.condition);
default:
return node->token;
}
}
struct token *node_end(struct node *node) {
if (node->token_end) return node->token_end;
switch (node->type) {
case NODE_FUNCTION_DEFINITION:
return node_end(node->fun.body);
case NODE_IF:
if (node->if_.block_false)
return node_end(node->if_.block_false);
else
return node_end(node->if_.block_true);
case NODE_WHILE:
return node_end(node->while_.block);
case NODE_UNARY_OP:
return node_end(node->unary_op.inner);
case NODE_BINARY_OP:
return node_end(node->binop.rhs);
case NODE_DECLARATOR:
if (node->d.inner)
return node_end(node->d.inner);
else
return node->token;
case NODE_TERNARY:
return node_end(node->ternary.branch_false);
default:
return node->token;
}
}
static struct node *parse_ident(struct context *context) {
if (TOKEN(context)->type != TOKEN_IDENT)
return report_error_node(context, "expected an ident, but didn't find it");
struct node *node = new(context, NODE_IDENT);
eat(context, TOKEN_IDENT);
return node;
}
static struct node *parse_primary_expression(struct context *context) {
switch (TOKEN(context)->type) {
case TOKEN_INT_LITERAL: {
struct node *node = new(context, NODE_INT_LITERAL);
pass(context);
return node;
}
case TOKEN_FLOAT_LITERAL: {
struct node *node = new(context, NODE_FLOAT_LITERAL);
pass(context);
return node;
}
case TOKEN_IDENT: {
struct node *node = new(context, NODE_IDENT);
pass(context);
return node;
}
case TOKEN_STRING_LITERAL: {
struct node *node = new(context, NODE_STRING_LITERAL);
pass(context);
return node;
}
case '(': {
pass(context);
struct node *expr = parse_expression(context);
expr->token_end = TOKEN(context);
eat(context, ')');
return expr;
}
default:
return report_error_node(context, "expected primary expression");
}
}
static struct node *parse_postfix_expression(struct context *context) {
struct node *inner = parse_primary_expression(context);
bool cont = true;
while (cont) {
switch (TOKEN(context)->type) {
case TOKEN_PLUS_PLUS:
case TOKEN_MINUS_MINUS: {
struct node *node = new(context, NODE_POSTFIX_OP);
pass(context);
node->unary_op.inner = inner;
inner = node;
break;
}
case '.':
case TOKEN_ARROW: {
if (PEEK(context)->type != TOKEN_IDENT) {
return report_error_node(context, "need ident after member reference");
}
struct node *node = new(context, NODE_MEMBER);
pass(context);
node->member.inner = inner;
node->member.ident = parse_ident(context);
node->token_end = TOKEN(context);
pass(context);
inner = node;
break;
}
case '(': {
struct node *node = new(context, NODE_FUNCTION_CALL);
pass(context);
node->funcall.inner = inner;
while (TOKEN(context)->type != ')') {
list_push(&node->funcall.args, parse_assignment_expression(context));
if (TOKEN(context)->type != ')') eat(context, ',');
}
eat(context, ')');
inner = node;
break;
}
case '[': {
struct node *node = new(context, NODE_SUBSCRIPT);
pass(context);
node->subscript.inner = inner;
node->subscript.subscript = parse_expression(context);
eat(context, ']');
inner = node;
break;
}
default:
cont = false;
}
}
return inner;
}
static struct node *parse_prefix_expression(struct context *context) {
struct token *token = TOKEN(context);
if (token->type == TOKEN_PLUS_PLUS || token->type == TOKEN_MINUS_EQUAL ||
token->type == '+' || token->type == '-' || token->type == '*' ||
token->type == '&' || token->type == '~' || token->type == '!' ||
token->type == TOKEN_SIZEOF || token->type == TOKEN_ALIGNOF) {
struct node *node = new(context, NODE_UNARY_OP);
pass(context);
node->unary_op.inner = parse_prefix_expression(context);
return node;
} else {
return parse_postfix_expression(context);
}
}
static struct node *parse_cast_expression(struct context *context) {
return parse_prefix_expression(context);
}
#define PARSE_BINOP(name, upstream, tt_expr) \
static struct node *name(struct context *context) { \
struct node *result = upstream(context); \
struct token *token = TOKEN(context); \
while (tt_expr) { \
struct node *node = new(context, NODE_BINARY_OP); \
pass(context); \
node->binop.lhs = result; \
node->binop.rhs = upstream(context); \
result = node; \
token = TOKEN(context); \
} \
return result; \
}
PARSE_BINOP(parse_mul, parse_cast_expression, token->type == '*' || token->type == '/' || token->type == '%')
PARSE_BINOP(parse_add, parse_mul, token->type == '+' || token->type == '-')
PARSE_BINOP(parse_shift, parse_add, token->type == TOKEN_SHIFT_LEFT || token->type == TOKEN_SHIFT_RIGHT)
PARSE_BINOP(parse_rel, parse_shift, token->type == '<' || token->type == '>' || token->type == TOKEN_GREATER_EQUAL || token->type == TOKEN_LESS_EQUAL)
PARSE_BINOP(parse_eq, parse_rel, token->type == TOKEN_EQUAL_EQUAL || token->type == TOKEN_NOT_EQUAL)
PARSE_BINOP(parse_bitand, parse_eq, token->type == '&')
PARSE_BINOP(parse_bitxor, parse_bitand, token->type == '^')
PARSE_BINOP(parse_bitor, parse_bitxor, token->type == '|')
PARSE_BINOP(parse_and, parse_bitor, token->type == TOKEN_AND_AND)
PARSE_BINOP(parse_or, parse_and, token->type == TOKEN_OR_OR)
static struct node *parse_ternary_expression(struct context *context) {
struct node *condition = parse_or(context);
if (TOKEN(context)->type != '?') {
return condition;
}
struct node *node = new(context, NODE_TERNARY);
pass(context);
struct node *branch_true = parse_expression(context);
eat(context, ':');
struct node *branch_false = parse_ternary_expression(context);
node->ternary.condition = condition;
node->ternary.branch_true = branch_true;
node->ternary.branch_false = branch_false;
return node;
}
static struct node *parse_assignment_expression(struct context *context) {
struct context saved = *context;
struct node *expr = parse_prefix_expression(context);
struct token *token = TOKEN(context);
if (token->type == '=' || token->type == TOKEN_STAR_EQUAL || token->type == TOKEN_DIVIDE_EQUAL ||
token->type == TOKEN_MOD_EQUAL || token->type == TOKEN_PLUS_EQUAL || token->type == TOKEN_MINUS_EQUAL ||
token->type == TOKEN_SHIFT_LEFT_EQUAL || token->type == TOKEN_SHIFT_RIGHT_EQUAL ||
token->type == TOKEN_BITAND_EQUAL || token->type == TOKEN_BITXOR_EQUAL || token->type == TOKEN_BITOR_EQUAL) {
struct node *node = new(context, NODE_BINARY_OP);
pass(context);
node->binop.lhs = expr;
node->binop.rhs = parse_assignment_expression(context);
return node;
} else {
*context = saved;
return parse_ternary_expression(context);
}
}
PARSE_BINOP(parse_expression, parse_assignment_expression, token->type == ',')
// end expressions
static bool is_type_qualifier(struct token *token) {
// C23(N3096) 6.7.3.1
return token->type == TOKEN_CONST ||
token->type == TOKEN_RESTRICT ||
token->type == TOKEN_VOLATILE ||
token->type == TOKEN__ATOMIC;
}
static bool is_storage_class(struct token *token) {
// C23(N3096) 6.7.1.1
return token->type == TOKEN_AUTO ||
token->type == TOKEN_CONSTEXPR ||
token->type == TOKEN_EXTERN ||
token->type == TOKEN_REGISTER ||
token->type == TOKEN_STATIC ||
token->type == TOKEN_THREAD_LOCAL ||
token->type == TOKEN_TYPEDEF;
}
static bool is_bare_type_specifier(struct token *token) {
// C23(N3096) 6.7.2.1
return token->type == TOKEN_VOID ||
token->type == TOKEN_CHAR ||
token->type == TOKEN_SHORT ||
token->type == TOKEN_INT ||
token->type == TOKEN_LONG ||
token->type == TOKEN_FLOAT_LITERAL ||
token->type == TOKEN_DOUBLE ||
token->type == TOKEN_SIGNED ||
token->type == TOKEN_BOOL ||
token->type == TOKEN__DECIMAL32 ||
token->type == TOKEN__DECIMAL64 ||
token->type == TOKEN__DECIMAL128;
}
static bool is_function_specifier(struct token *token) {
// C23(N3096) 6.7.4.1
return token->type == TOKEN_INLINE ||
token->type == TOKEN__NORETURN;
}
static bool is_typedef(struct context *context, struct token *token) {
return false;
}
static bool is_declaration_specifier(struct context *context, struct token *token) {
return is_type_qualifier(token) ||
is_type_qualifier(token) ||
is_bare_type_specifier(token) ||
is_storage_class(token) ||
is_function_specifier(token); // || is_typedef(context, token);
}
static bool begins_type_name(struct context *context, struct token *token) {
// return is_type_qualifier(token) ||
// is_type_qualifier(token) ||
// is_bare_type_specifier(token) ||
// is_function_specifier(token) ||
// is_typedef(context, token);
return is_bare_type_specifier(token) || token->type == TOKEN_STRUCT || token->type == TOKEN_UNION ||
is_declaration_specifier(context, token);
}
static struct node *parse_struct(struct context *context) {
struct node *node;
if (TOKEN(context)->type == TOKEN_STRUCT)
node = new(context, NODE_STRUCT);
else if (TOKEN(context)->type == TOKEN_UNION)
node = new(context, NODE_UNION);
else return report_error_node(context, "not a struct");
pass(context);
if (TOKEN(context)->type == TOKEN_IDENT) {
struct node *name = parse_ident(context);
node->struct_.name = name;
}
if (TOKEN(context)->type == ';') {
eat(context, ';');
return node;
}
eat(context, '{');
while (TOKEN(context)->type != '}') {
struct node *n = parse_declaration(context);
list_push(&node->struct_.decls, n);
}
node->token_end = TOKEN(context);
eat(context, '}');
return node;
}
static struct node *parse_type_specifier(struct context *context) {
struct token *token = TOKEN(context);
if (is_bare_type_specifier(token)) {
struct node *node = new(context, NODE_TYPE_SPECIFIER);
pass(context);
return node;
} else if (TOKEN(context)->type == TOKEN_STRUCT || TOKEN(context)->type == TOKEN_UNION) {
return parse_struct(context);
} else {
return report_error_node(context, "non-basic decl_spec specifiers are not yet supported");
}
}
static struct node *parse_direct_declarator(struct context *);
static struct node *parse_single_declaration(struct context *);
static struct node *parse_declarator(struct context *context) {
struct token *token = TOKEN(context);
if (token->type == '*') {
struct node *node = new(context, NODE_DECLARATOR);
pass(context);
node->d.inner = parse_declarator(context);
node->d.name = node->d.inner->d.name;
return node;
} else {
return parse_direct_declarator(context);
}
}
static struct node *parse_direct_declarator(struct context *context) {
struct node *node;
switch (TOKEN(context)->type) {
case TOKEN_IDENT: {
struct node *inner = new(context, NODE_DECLARATOR);
inner->d.name = TOKEN(context);
pass(context);
node = inner;
break;
}
case '(': {
pass(context);
node = parse_declarator(context);
node->token_end = TOKEN(context);
eat(context, ')');
break;
}
case ',':
case ';':
case ')': {
print_info_token(context->tu, TOKEN(context), "interpreting this as a nameless declarator");
struct node *inner = new(context, NODE_DECLARATOR);
inner->d.name = nullptr;
inner->d.nameless = true;
node = inner;
break;
}
default:
return report_error_node(context, "unable to parse d");
}
struct node *inner = nullptr;
bool cont = true;
while (cont) {
switch (TOKEN(context)->type) {
case '[': {
inner = new(context, NODE_ARRAY_DECLARATOR);
pass(context);
inner->d.inner = node;
inner->d.name = inner->d.inner->d.name;
if (TOKEN(context)->type != ']')
inner->d.arr.subscript = parse_assignment_expression(context);
node->token_end = TOKEN(context);
eat(context, ']');
node = inner;
break;
}
case '(': {
inner = new(context, NODE_FUNCTION_DECLARATOR);
eat(context, '(');
inner->d.inner = node;
inner->d.name = inner->d.inner->d.name;
// TODO: args
while (TOKEN(context)->type != ')') {
list_push(&inner->d.fun.args, parse_single_declaration(context));
if (TOKEN(context)->type != ')') eat(context, ',');
}
node->token_end = TOKEN(context);
eat(context, ')');
node = inner;
break;
}
default:
cont = false;
}
}
return node;
}
static struct node *parse_full_declarator(struct context *context) {
struct node *inner = parse_declarator(context);
struct node *expr = nullptr;
if (TOKEN(context)->type == '=') {
pass(context);
expr = parse_assignment_expression(context);
}
struct node *node = inner;
node->d.initializer = expr;
node->d.full = true;
return inner;
}
static struct node *parse_static_assert_declaration(struct context *context) {
struct node* node = new(context, NODE_STATIC_ASSERT);
pass(context);
eat(context, '(');
node->st_assert.expr = parse_assignment_expression(context);
if (TOKEN(context)->type == ',') {
eat(context, ',');
if (TOKEN(context)->type == TOKEN_STRING_LITERAL) {
node->st_assert.message = report_error_node(context, "static assert string literals not supported");
} else {
node->st_assert.message = report_error_node(context, "static assert message must be string literal");
}
}
eat(context, ')');
node->token_end = TOKEN(context);
eat(context, ';');
return node;
}
enum parse_state {
SEEN_TOKEN_CHAR = (1 << 0),
SEEN_TOKEN_SHORT = (1 << 1),
SEEN_TOKEN_LONG = (1 << 2),
SEEN_TOKEN_LONG_TWICE = (1 << 3),
SEEN_TOKEN_INT = (1 << 4),
SEEN_TOKEN_SIGNED = (1 << 5),
SEEN_TOKEN_UNSIGNED = (6 << 6),
SEEN_TOKEN_FLOAT = (1 << 7),
SEEN_TOKEN_DOUBLE = (1 << 8),
SEEN_TOKEN_COMPLEX = (1 << 9),
SEEN_FLOAT = SEEN_TOKEN_FLOAT | SEEN_TOKEN_DOUBLE | SEEN_TOKEN_COMPLEX,
};
static bool incompatible_type_token(enum parse_state state, struct token *token) {
switch (token->type) {
case TOKEN_CHAR:
return (state & (SEEN_TOKEN_CHAR | SEEN_TOKEN_SHORT | SEEN_TOKEN_LONG | SEEN_FLOAT)) != 0;
case TOKEN_SHORT:
return (state & (SEEN_TOKEN_CHAR | SEEN_TOKEN_LONG | SEEN_FLOAT)) != 0;
case TOKEN_LONG:
return (state & (SEEN_TOKEN_CHAR | SEEN_TOKEN_LONG_TWICE | SEEN_TOKEN_FLOAT)) != 0;
case TOKEN_INT:
return (state & (SEEN_TOKEN_CHAR | SEEN_TOKEN_INT | SEEN_FLOAT)) != 0;
case TOKEN_SIGNED:
return (state & (SEEN_TOKEN_UNSIGNED | SEEN_FLOAT)) != 0;
case TOKEN_UNSIGNED:
return (state & (SEEN_TOKEN_SIGNED | SEEN_FLOAT)) != 0;
case TOKEN_FLOAT:
return (state & (SEEN_TOKEN_CHAR | SEEN_TOKEN_SHORT | SEEN_TOKEN_LONG | SEEN_TOKEN_INT |
SEEN_TOKEN_SIGNED | SEEN_TOKEN_UNSIGNED)) != 0;
case TOKEN_DOUBLE:
return (state & (SEEN_TOKEN_CHAR | SEEN_TOKEN_SHORT | SEEN_TOKEN_INT | SEEN_TOKEN_SIGNED |
SEEN_TOKEN_UNSIGNED)) != 0;
default:
return false;
}
}
static struct node *parse_declaration_specifier_list(struct context *context, struct node *node) {
enum layer_type base_type = 0;
enum type_flags type_flags = 0;
enum storage_class sc = 0;
enum parse_state state = 0;
while (is_declaration_specifier(context, TOKEN(context))) {
if (incompatible_type_token(state, TOKEN(context))) {
return report_error_node(context, "invalid combination of declaration specifiers");
}
switch (TOKEN(context)->type) {
case TOKEN_STRUCT:
case TOKEN_UNION: {
base_type = TYPE_STRUCT;
struct node *struct_ = parse_struct(context);
case TOKEN_ENUM:
return report_error_node(context, "constructing struct and enum types is not yet supported");
}
case TOKEN_CONST:
type_flags |= TF_CONST;
break;
case TOKEN_VOLATILE:
type_flags |= TF_VOLATILE;
break;
case TOKEN__ATOMIC:
type_flags |= TF_ATOMIC;
break;
case TOKEN_RESTRICT:
type_flags |= TF_RESTRICT;
break;
case TOKEN_INLINE:
type_flags |= TF_INLINE;
break;
case TOKEN__NORETURN:
type_flags |= TF_NORETURN;
break;
case TOKEN_AUTO:
case TOKEN_CONSTEXPR:
if (sc != 0) goto error;
sc = ST_CONSTEXPR;
break;
case TOKEN_EXTERN:
if (sc != 0) goto error;
sc = ST_EXTERNAL;
break;
case TOKEN_REGISTER:
if (sc != 0) goto error;
sc = ST_REGISTER;
break;
case TOKEN_STATIC:
if (sc != 0) goto error;
sc = ST_STATIC;
break;
case TOKEN_THREAD_LOCAL: