-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.c
1720 lines (1455 loc) · 56.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
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 <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "lice.h"
#include "lexer.h"
static ast_t *parse_expression(void);
static ast_t *parse_expression_unary(void);
static ast_t *parse_expression_intermediate(int);
static ast_t *parse_statement_compound(void);
static void parse_statement_declaration(list_t *);
static ast_t *parse_statement(void);
static data_type_t *parse_declaration_specification(storage_t *);
static list_t *parse_initializer_declaration(data_type_t *type);
static data_type_t *parse_declarator(char **, data_type_t *, list_t *, cdecl_t);
static void parse_declaration(list_t *, ast_t *(*)(data_type_t *, char *));
static void parse_function_parameter(data_type_t **, char **, bool);
static data_type_t *parse_function_parameters(list_t *, data_type_t *);
table_t *parse_typedefs = &SENTINEL_TABLE;
static bool parse_type_check(lexer_token_t *token);
static void parse_semantic_lvalue(ast_t *ast) {
switch (ast->type) {
case AST_TYPE_VAR_LOCAL:
case AST_TYPE_VAR_GLOBAL:
case AST_TYPE_DEREFERENCE:
case AST_TYPE_STRUCT:
return;
}
compile_error("expected lvalue, `%s' isn't a valid lvalue", ast_string(ast));
}
static void parse_semantic_notvoid(data_type_t *type) {
if (type->type == TYPE_VOID)
compile_error("void not allowed in expression");
}
static void parse_semantic_integer(ast_t *node) {
if (!ast_type_integer(node->ctype))
compile_error("expected integer type, `%s' isn't a valid integer type", ast_string(node));
}
static bool parse_semantic_rightassoc(lexer_token_t *token) {
return (token->punct == '=');
}
static void parse_expect(char punct) {
lexer_token_t *token = lexer_next();
if (!lexer_ispunct(token, punct))
compile_error("expected `%c`, got %s instead", punct, lexer_tokenstr(token));
}
static bool parse_identifer_check(lexer_token_t *token, const char *identifier) {
return token->type == LEXER_TOKEN_IDENTIFIER && !strcmp(token->string, identifier);
}
int parse_evaluate(ast_t *ast) {
switch (ast->type) {
case AST_TYPE_LITERAL:
if (ast_type_integer(ast->ctype))
return ast->integer;
compile_error("not a valid integer constant expression `%s'", ast_string(ast));
break;
case '+': return parse_evaluate(ast->left) + parse_evaluate(ast->right);
case '-': return parse_evaluate(ast->left) - parse_evaluate(ast->right);
case '*': return parse_evaluate(ast->left) * parse_evaluate(ast->right);
case '/': return parse_evaluate(ast->left) / parse_evaluate(ast->right);
case '<': return parse_evaluate(ast->left) < parse_evaluate(ast->right);
case '>': return parse_evaluate(ast->left) > parse_evaluate(ast->right);
case '^': return parse_evaluate(ast->left) ^ parse_evaluate(ast->right);
case '%': return parse_evaluate(ast->left) % parse_evaluate(ast->right);
case LEXER_TOKEN_AND: return parse_evaluate(ast->left) && parse_evaluate(ast->right);
case LEXER_TOKEN_OR: return parse_evaluate(ast->left) || parse_evaluate(ast->right);
case LEXER_TOKEN_EQUAL: return parse_evaluate(ast->left) == parse_evaluate(ast->right);
case LEXER_TOKEN_LEQUAL: return parse_evaluate(ast->left) <= parse_evaluate(ast->right);
case LEXER_TOKEN_GEQUAL: return parse_evaluate(ast->left) >= parse_evaluate(ast->right);
case LEXER_TOKEN_NEQUAL: return parse_evaluate(ast->left) != parse_evaluate(ast->right);
case LEXER_TOKEN_LSHIFT: return parse_evaluate(ast->left) << parse_evaluate(ast->right);
case LEXER_TOKEN_RSHIFT: return parse_evaluate(ast->left) >> parse_evaluate(ast->right);
/* Deal with unary operations differently */
case '!': return !parse_evaluate(ast->unary.operand);
case '~': return ~parse_evaluate(ast->unary.operand);
case AST_TYPE_EXPRESSION_CAST: return parse_evaluate(ast->unary.operand);
default:
compile_error("not a valid integer constant expression `%s'", ast_string(ast));
}
return -1;
}
static int parse_operator_priority(lexer_token_t *token) {
switch (token->punct) {
case '[':
case '.':
case LEXER_TOKEN_ARROW:
return 1;
case LEXER_TOKEN_INCREMENT:
case LEXER_TOKEN_DECREMENT:
return 2;
case '*':
case '/':
case '%':
return 3;
case '+':
case '-':
return 4;
case LEXER_TOKEN_LSHIFT:
case LEXER_TOKEN_RSHIFT:
return 5;
case '<':
case '>':
return 6;
case LEXER_TOKEN_EQUAL:
case LEXER_TOKEN_GEQUAL:
case LEXER_TOKEN_LEQUAL:
case LEXER_TOKEN_NEQUAL:
return 7;
case '&':
return 8;
case '^':
return 9;
case '|':
return 10;
case LEXER_TOKEN_AND:
return 11;
case LEXER_TOKEN_OR:
return 12;
case '?':
return 13;
case '=':
case LEXER_TOKEN_COMPOUND_ADD:
case LEXER_TOKEN_COMPOUND_AND:
case LEXER_TOKEN_COMPOUND_DIV:
case LEXER_TOKEN_COMPOUND_LSHIFT:
case LEXER_TOKEN_COMPOUND_MOD:
case LEXER_TOKEN_COMPOUND_MUL:
case LEXER_TOKEN_COMPOUND_OR:
case LEXER_TOKEN_COMPOUND_RSHIFT:
case LEXER_TOKEN_COMPOUND_SUB:
case LEXER_TOKEN_COMPOUND_XOR:
return 14;
}
return -1;
}
static list_t *parse_parameter_types(list_t *parameters) {
list_t *list = list_create();
for (list_iterator_t *it = list_iterator(parameters); !list_iterator_end(it); )
list_push(list, ((ast_t*)list_iterator_next(it))->ctype);
return list;
}
static void parse_function_typecheck(const char *name, list_t *parameters, list_t *arguments) {
if (list_length(arguments) < list_length(parameters))
compile_error("too few arguments for function `%s'", name);
for (list_iterator_t *it = list_iterator(parameters),
*jt = list_iterator(arguments); !list_iterator_end(jt); )
{
data_type_t *parameter = list_iterator_next(it);
data_type_t *argument = list_iterator_next(jt);
if (parameter)
ast_result_type('=', parameter, argument);
else
ast_result_type('=', argument, ast_data_table[AST_DATA_INT]);
}
}
static ast_t *parse_function_call(char *name) {
list_t *list = list_create();
for (;;) {
lexer_token_t *token = lexer_next();
if (lexer_ispunct(token, ')'))
break;
lexer_unget(token);
list_push(list, parse_expression());
token = lexer_next();
if (lexer_ispunct(token, ')'))
break;
if (!lexer_ispunct(token, ','))
compile_error("unexpected token `%s'", lexer_tokenstr(token));
}
ast_t *func = table_find(ast_localenv, name);
if (func) {
data_type_t *declaration = func->ctype;
if (declaration->type != TYPE_FUNCTION)
compile_error("expected a function name, `%s' isn't a function", name);
parse_function_typecheck(name, declaration->parameters, parse_parameter_types(list));
return ast_call(declaration->returntype, name, list, declaration->parameters);
}
/* TODO: warn about implicit int return */
return ast_call(ast_data_table[AST_DATA_INT], name, list, list_create());
}
static ast_t *parse_generic(char *name) {
ast_t *var = NULL;
lexer_token_t *token = lexer_next();
if (lexer_ispunct(token, '('))
return parse_function_call(name);
lexer_unget(token);
if (!(var = table_find(ast_localenv, name)))
compile_error("undefined variable `%s'", name);
return var;
}
static ast_t *parse_number_integer(const char *string) {
const char *p = string;
int base = 10;
if (!strncasecmp(string, "0x", 2)) {
base = 16;
p++;
p++;
} else if (string[0] == '0' && string[1] != '\0') {
base = 8;
p++;
}
while (isxdigit(*p)) {
if (base == 10 && isalpha(*p))
compile_error("invalid character in decimal literal");
if (base == 8 && !('0' <= *p && *p <= '7'))
compile_error("invalid character in octal literal");
p++;
}
if (!strcasecmp(p, "l"))
return ast_new_integer(ast_data_table[AST_DATA_LONG], strtol(string, NULL, base));
if (!strcasecmp(p, "ul") || !strcasecmp(p, "lu"))
return ast_new_integer(ast_data_table[AST_DATA_ULONG], strtoul(string, NULL, base));
if (!strcasecmp(p, "ll"))
return ast_new_integer(ast_data_table[AST_DATA_LLONG], strtoll(string, NULL, base));
if (!strcasecmp(p, "ull") || !strcasecmp(p, "llu"))
return ast_new_integer(ast_data_table[AST_DATA_ULLONG], strtoull(string, NULL, base));
if (*p != '\0')
compile_error("invalid suffix for literal");
long value = strtol(string, NULL, base);
return (value & ~(long)UINT_MAX)
? ast_new_integer(ast_data_table[AST_DATA_LONG], value)
: ast_new_integer(ast_data_table[AST_DATA_INT], value);
}
static ast_t *parse_number_floating(const char *string) {
const char *p = string;
char *end;
while (p[1])
p++;
ast_t *ast;
if (*p == 'l' || *p == 'L')
ast = ast_new_floating(ast_data_table[AST_DATA_LDOUBLE], strtold(string, &end));
else if (*p == 'f' || *p == 'F')
ast = ast_new_floating(ast_data_table[AST_DATA_FLOAT], strtof(string, &end));
else {
ast = ast_new_floating(ast_data_table[AST_DATA_DOUBLE], strtod(string, &end));
p++;
}
if (end != p)
compile_error("malformatted float literal");
return ast;
}
static ast_t *parse_number(const char *string) {
return strpbrk(string, ".pe")
? parse_number_floating(string)
: parse_number_integer(string);
}
static ast_t *parse_expression_primary(void) {
lexer_token_t *token;
ast_t *ast;
if (!(token = lexer_next()))
return NULL;
switch (token->type) {
case LEXER_TOKEN_IDENTIFIER:
return parse_generic(token->string);
case LEXER_TOKEN_NUMBER:
return parse_number(token->string);
case LEXER_TOKEN_CHAR:
return ast_new_integer(ast_data_table[AST_DATA_CHAR], token->character);
case LEXER_TOKEN_STRING:
ast = ast_new_string(token->string);
list_push(ast_strings, ast);
return ast;
case LEXER_TOKEN_PUNCT:
lexer_unget(token);
return NULL;
default:
break;
}
compile_error("Internal error: parse_expression_primary");
return NULL;
}
static ast_t *parse_expression_subscript(ast_t *ast) {
ast_t *subscript = parse_expression();
parse_expect(']');
ast_t *node = ast_new_binary('+', ast, subscript);
return ast_new_unary(AST_TYPE_DEREFERENCE, node->ctype->pointer, node);
}
static data_type_t *parse_sizeof_type(bool typename) {
lexer_token_t *token = lexer_next();
if (typename && parse_type_check(token)) {
lexer_unget(token);
data_type_t *type;
parse_function_parameter(&type, NULL, true);
return type;
}
if (lexer_ispunct(token, '(')) {
data_type_t *next = parse_sizeof_type(true);
parse_expect(')');
token = lexer_next();
if (lexer_ispunct(token, '{')) {
parse_initializer_declaration(next);
parse_expect('}');
} else {
lexer_unget(token);
}
return next;
}
lexer_unget(token);
ast_t *expression = parse_expression_unary();
if (expression->ctype->size == 0)
compile_error("sizeof(void) illegal");
return expression->ctype;
}
static ast_t *parse_expression_compound_literal(data_type_t *type) {
char *name = ast_label();
list_t *list = parse_initializer_declaration(type);
parse_expect('}');
ast_t *node = ast_variable_local(type, name);
node->variable.init = list;
return node;
}
static ast_t *parse_expression_unary_cast(void) {
data_type_t *basetype = parse_declaration_specification(NULL);
data_type_t *casttype = parse_declarator(NULL, basetype, NULL, CDECL_CAST);
parse_expect(')');
lexer_token_t *token = lexer_next();
if (lexer_ispunct(token, '{'))
return parse_expression_compound_literal(casttype);
lexer_unget(token);
ast_t *expression = parse_expression_unary();
return ast_new_unary(AST_TYPE_EXPRESSION_CAST, casttype, expression);
}
static ast_t *parse_expression_unary(void) {
lexer_token_t *token = lexer_next();
if (!token)
compile_error("unexpected end of input");
if (parse_identifer_check(token, "sizeof")) {
return ast_new_integer(ast_data_table[AST_DATA_LONG], parse_sizeof_type(false)->size);
}
if (lexer_ispunct(token, '(')) {
if (parse_type_check(lexer_peek()))
return parse_expression_unary_cast();
ast_t *next = parse_expression();
parse_expect(')');
return next;
}
if (lexer_ispunct(token, '&')) {
ast_t *operand = parse_expression_intermediate(3);
parse_semantic_lvalue(operand);
return ast_new_unary(AST_TYPE_ADDRESS, ast_pointer(operand->ctype), operand);
}
if (lexer_ispunct(token, '!')) {
ast_t *operand = parse_expression_intermediate(3);
return ast_new_unary('!', ast_data_table[AST_DATA_INT], operand);
}
if (lexer_ispunct(token, '-')) {
ast_t *ast = parse_expression_intermediate(3);
return ast_new_binary('-', ast_new_integer(ast_data_table[AST_DATA_INT], 0), ast);
}
if (lexer_ispunct(token, '~')) {
ast_t *ast = parse_expression_intermediate(3);
if (!ast_type_integer(ast->ctype))
compile_error("invalid expression `%s'", ast_string(ast));
return ast_new_unary('~', ast->ctype, ast);
}
if (lexer_ispunct(token, '*')) {
ast_t *operand = parse_expression_intermediate(3);
data_type_t *type = ast_array_convert(operand->ctype);
if (type->type != TYPE_POINTER)
compile_error("expected pointer type, `%s' isn't pointer type", ast_string(operand));
return ast_new_unary(AST_TYPE_DEREFERENCE, operand->ctype->pointer, operand);
}
if (lexer_ispunct(token, LEXER_TOKEN_INCREMENT)
|| lexer_ispunct(token, LEXER_TOKEN_DECREMENT))
{
ast_t *next = parse_expression_intermediate(3);
parse_semantic_lvalue(next);
int operand = lexer_ispunct(token, LEXER_TOKEN_INCREMENT)
? AST_TYPE_PRE_INCREMENT
: AST_TYPE_PRE_DECREMENT;
return ast_new_unary(operand, next->ctype, next);
}
lexer_unget(token);
return parse_expression_primary();
}
static ast_t *parse_expression_condition(ast_t *condition) {
ast_t *then = parse_expression();
parse_expect(':');
ast_t *last = parse_expression();
return ast_ternary(then->ctype, condition, then, last);
}
static ast_t *parse_structure_field(ast_t *structure) {
if (structure->ctype->type != TYPE_STRUCTURE)
compile_error("expected structure type, `%s' isn't structure type", ast_string(structure));
lexer_token_t *name = lexer_next();
if (name->type != LEXER_TOKEN_IDENTIFIER)
compile_error("expected field name, got `%s' instead", lexer_tokenstr(name));
data_type_t *field = table_find(structure->ctype->fields, name->string);
if (!field)
compile_error("structure has no such field `%s'", lexer_tokenstr(name));
return ast_structure_reference(field, structure, name->string);
}
static int parse_operation_compound_operator(lexer_token_t *token) {
if (token->type != LEXER_TOKEN_PUNCT)
return 0;
switch (token->punct) {
case LEXER_TOKEN_COMPOUND_RSHIFT: return LEXER_TOKEN_RSHIFT;
case LEXER_TOKEN_COMPOUND_LSHIFT: return LEXER_TOKEN_LSHIFT;
case LEXER_TOKEN_COMPOUND_ADD: return '+';
case LEXER_TOKEN_COMPOUND_AND: return '&';
case LEXER_TOKEN_COMPOUND_DIV: return '/';
case LEXER_TOKEN_COMPOUND_MOD: return '%';
case LEXER_TOKEN_COMPOUND_MUL: return '*';
case LEXER_TOKEN_COMPOUND_OR: return '|';
case LEXER_TOKEN_COMPOUND_SUB: return '-';
case LEXER_TOKEN_COMPOUND_XOR: return '^';
default:
return 0;
}
return -1;
}
static int parse_operation_reclassify(int punct) {
switch (punct) {
case LEXER_TOKEN_LSHIFT: return AST_TYPE_LSHIFT;
case LEXER_TOKEN_RSHIFT: return AST_TYPE_RSHIFT;
case LEXER_TOKEN_EQUAL: return AST_TYPE_EQUAL;
case LEXER_TOKEN_GEQUAL: return AST_TYPE_GEQUAL;
case LEXER_TOKEN_LEQUAL: return AST_TYPE_LEQUAL;
case LEXER_TOKEN_NEQUAL: return AST_TYPE_NEQUAL;
case LEXER_TOKEN_AND: return AST_TYPE_AND;
case LEXER_TOKEN_OR: return AST_TYPE_OR;
default:
break;
}
return punct;
}
static bool parse_operation_integer_check(int operation) {
return operation == '^'
|| operation == '%'
|| operation == LEXER_TOKEN_LSHIFT
|| operation == LEXER_TOKEN_RSHIFT;
}
static ast_t *parse_expression_intermediate(int precision) {
ast_t *ast;
ast_t *next;
if (!(ast = parse_expression_unary()))
return NULL;
for (;;) {
lexer_token_t *token = lexer_next();
if (token->type != LEXER_TOKEN_PUNCT) {
lexer_unget(token);
return ast;
}
int pri = parse_operator_priority(token);
if (pri < 0 || precision <= pri) {
lexer_unget(token);
return ast;
}
if (lexer_ispunct(token, '?')) {
ast = parse_expression_condition(ast);
continue;
}
if (lexer_ispunct(token, '.')) {
ast = parse_structure_field(ast);
continue;
}
if (lexer_ispunct(token, LEXER_TOKEN_ARROW)) {
if (ast->ctype->type != TYPE_POINTER)
compile_error("Not a valid pointer type: %s", ast_string(ast));
ast = ast_new_unary(AST_TYPE_DEREFERENCE, ast->ctype->pointer, ast);
ast = parse_structure_field(ast);
continue;
}
if (lexer_ispunct(token, '[')) {
ast = parse_expression_subscript(ast);
continue;
}
if (lexer_ispunct(token, LEXER_TOKEN_INCREMENT) ||
lexer_ispunct(token, LEXER_TOKEN_DECREMENT)) {
parse_semantic_lvalue(ast);
int operand = lexer_ispunct(token, LEXER_TOKEN_INCREMENT)
? AST_TYPE_POST_INCREMENT
: AST_TYPE_POST_DECREMENT;
ast = ast_new_unary(operand, ast->ctype, ast);
continue;
}
int compound = parse_operation_compound_operator(token);
if (lexer_ispunct(token, '=') || compound)
parse_semantic_lvalue(ast);
next = parse_expression_intermediate(pri + !!parse_semantic_rightassoc(token));
if (!next)
compile_error("Internal error: parse_expression_intermediate (next)");
int operation = compound ? compound : token->punct;
int op = parse_operation_reclassify(operation);
if (parse_operation_integer_check(op)) {
parse_semantic_integer(ast);
parse_semantic_integer(next);
}
if (compound)
ast = ast_new_binary('=', ast, ast_new_binary(op, ast, next));
else
ast = ast_new_binary(op, ast, next);
}
return NULL;
}
static ast_t *parse_expression(void) {
return parse_expression_intermediate(16);
}
static bool parse_type_check(lexer_token_t *token) {
if (token->type != LEXER_TOKEN_IDENTIFIER)
return false;
static const char *keywords[] = {
"char", "short", "int", "long", "float", "double",
"struct", "union", "signed", "unsigned", "enum", "void",
"typedef", "extern", "static", "auto", "register", "const",
"volatile", "inline", "restrict"
};
for (int i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++)
if (!strcmp(keywords[i], token->string))
return true;
if (table_find(parse_typedefs, token->string))
return true;
return false;
}
/* struct / union */
static char *parse_memory_tag(void) {
lexer_token_t *token = lexer_next();
if (token->type == LEXER_TOKEN_IDENTIFIER)
return token->string;
lexer_unget(token);
return NULL;
}
static int parse_memory_fields_padding(int offset, int size) {
size = MIN(size, ARCH_ALIGNMENT);
return (offset % size == 0) ? 0 : size - offset % size;
}
static void parse_memory_fields_squash(table_t *table, data_type_t *unnamed, int offset) {
for (list_iterator_t *it = list_iterator(table_keys(unnamed->fields)); !list_iterator_end(it); ) {
char *name = list_iterator_next(it);
data_type_t *type = ast_type_copy(table_find(unnamed->fields, name));
type->offset += offset;
table_insert(table, name, type);
}
}
static table_t *parse_memory_fields(int *rsize, bool isstruct) {
lexer_token_t *token = lexer_next();
if (!lexer_ispunct(token, '{')) {
lexer_unget(token);
return NULL;
}
int offset = 0;
int maxsize = 0;
table_t *table = table_create(NULL);
for (;;) {
if (!parse_type_check(lexer_peek()))
break;
data_type_t *basetype = parse_declaration_specification(NULL);
if (basetype->type == TYPE_STRUCTURE && lexer_ispunct(lexer_peek(), ';')) {
lexer_next(); /* Skip */
parse_memory_fields_squash(table, basetype, offset);
if (isstruct)
offset += basetype->size;
else
maxsize = MAX(maxsize, basetype->size);
continue;
}
for (;;) {
char *name;
data_type_t *fieldtype = parse_declarator(&name, basetype, NULL, CDECL_PARAMETER);
parse_semantic_notvoid(fieldtype);
if (isstruct) {
offset += parse_memory_fields_padding(offset, fieldtype->size);
fieldtype = ast_structure_field(fieldtype, offset);
offset += fieldtype->size;
} else {
maxsize = MAX(maxsize, fieldtype->size);
fieldtype = ast_structure_field(fieldtype, 0);
}
table_insert(table, name, fieldtype);
token = lexer_next();
if (lexer_ispunct(token, ','))
continue;
lexer_unget(token);
parse_expect(';');
break;
}
}
parse_expect('}');
*rsize = isstruct ? offset : maxsize;
return table;
}
static data_type_t *parse_tag_definition(table_t *table, bool isstruct) {
char *tag = parse_memory_tag();
int size = 0;
table_t *fields = parse_memory_fields(&size, isstruct);
data_type_t *r;
if (tag) {
if (!(r = table_find(table, tag))) {
r = ast_structure_new(NULL, 0, isstruct);
table_insert(table, tag, r);
}
} else {
r = ast_structure_new(NULL, 0, isstruct);
if (tag)
table_insert(table, tag, r);
}
if (r && !fields)
return r;
if (r && fields) {
r->fields = fields;
r->size = size;
return r;
}
return r;
}
/* enum */
static data_type_t *parse_enumeration(void) {
lexer_token_t *token = lexer_next();
if (token->type == LEXER_TOKEN_IDENTIFIER)
token = lexer_next();
if (!lexer_ispunct(token, '{')) {
lexer_unget(token);
return ast_data_table[AST_DATA_INT];
}
int accumulate = 0;
for (;;) {
token = lexer_next();
if (lexer_ispunct(token, '}'))
break;
if (token->type != LEXER_TOKEN_IDENTIFIER)
compile_error("NOPE");
char *name = token->string;
token = lexer_next();
if (lexer_ispunct(token, '='))
accumulate = parse_evaluate(parse_expression());
else
lexer_unget(token);
ast_t *constval = ast_new_integer(ast_data_table[AST_DATA_INT], accumulate++);
table_insert(ast_localenv ? ast_localenv : ast_globalenv, name, constval);
token = lexer_next();
if (lexer_ispunct(token, ','))
continue;
if (lexer_ispunct(token, '}'))
break;
compile_error("NOPE!");
}
return ast_data_table[AST_DATA_INT];
}
/* initializer */
static void parse_assign_string(list_t *init, data_type_t *type, char *p, int offset) {
if (type->length == -1)
type->length = type->size = strlen(p) + 1;
int i = 0;
for (; i < type->length && *p; i++) {
list_push(
init,
ast_initializer(
ast_new_integer(ast_data_table[AST_DATA_CHAR], *p++),
ast_data_table[AST_DATA_CHAR],
offset + i
)
);
}
for (; i < type->length; i++) {
list_push(
init,
ast_initializer(
ast_new_integer(ast_data_table[AST_DATA_CHAR], 0),
ast_data_table[AST_DATA_CHAR],
offset + i
)
);
}
}
static bool parse_brace_maybe(void) {
lexer_token_t *token = lexer_next();
if (lexer_ispunct(token, '{'))
return true;
lexer_unget(token);
return false;
}
static void parse_commaskip_maybe(void) {
lexer_token_t *token = lexer_next();
if (!lexer_ispunct(token, ','))
lexer_unget(token);
}
static void parse_brace_skipto(void) {
for (;;) {
lexer_token_t *token = lexer_next();
if (lexer_ispunct(token, '}'))
return;
if (lexer_ispunct(token, '.')) {
lexer_next();
parse_expect('=');
} else {
lexer_unget(token);
}
ast_t *ignore = parse_expression_intermediate(3);
if (!ignore)
return;
/* TODO aggregate warning */
token = lexer_next();
if (!lexer_ispunct(token, ','))
lexer_unget(token);
}
}
static ast_t *parse_zero(data_type_t *type) {
return ast_type_floating(type)
? ast_new_floating(ast_data_table[AST_DATA_DOUBLE], 0.0)
: ast_new_integer (ast_data_table[AST_DATA_INT], 0);
}
static void parse_initializer_list(list_t *init, data_type_t *type, int offset);
static void parse_initializer_element(list_t *init, data_type_t *type, int offset) {
if (type->type == TYPE_ARRAY || type->type == TYPE_STRUCTURE)
parse_initializer_list(init, type, offset);
else {
ast_t *expression = parse_expression_intermediate(3);
ast_result_type('=', type, expression->ctype);
list_push(init, ast_initializer(expression, type, offset));
}
}
static void parse_initializer_zero(list_t *init, data_type_t *type, int offset) {
if (type->type == TYPE_STRUCTURE) {
list_iterator_t *it = list_iterator(table_keys(type->fields));
while (!list_iterator_end(it)) {
char *fieldname = list_iterator_next(it);
data_type_t *fieldtype = table_find(type->fields, fieldname);
parse_initializer_zero(init, fieldtype, offset + fieldtype->offset);
if (!type->isstruct)
return;
}
return;
}
if (type->type == TYPE_ARRAY) {
for (int i = 0; i < type->length; i++)
parse_initializer_zero(init, type->pointer, offset + i * type->pointer->size);
return;
}
list_push(init, ast_initializer(parse_zero(type), type, offset));
}
static void parse_initializer_structure(list_t *init, data_type_t *type, int offset) {
bool brace = parse_brace_maybe();
list_iterator_t *it = list_iterator(table_keys(type->fields));
table_t *wrote = table_create(NULL);
for (;;) {
lexer_token_t *token = lexer_next();
if (lexer_ispunct(token, '}')) {
if (!brace)
lexer_unget(token);
goto complete;
}
char *fieldname;
data_type_t *fieldtype;
if (lexer_ispunct(token, '.')) {
if (!(token = lexer_next()) || token->type != LEXER_TOKEN_IDENTIFIER)
compile_error("invalid designated initializer");
fieldname = token->string;
if (!(fieldtype = table_find(type->fields, fieldname)))
compile_error("field doesn't exist in designated initializer");
parse_expect('=');
it = list_iterator(table_keys(type->fields));
while (!list_iterator_end(it))
if (!strcmp(fieldname, list_iterator_next(it)))
break;
} else {
lexer_unget(token);
if (list_iterator_end(it))
break;
fieldname = list_iterator_next(it);
fieldtype = table_find(type->fields, fieldname);
}
if (table_find(wrote, fieldname))
compile_error("field initialized twice in designated initializer");
table_insert(wrote, fieldname, (void*)1);
parse_initializer_element(init, fieldtype, offset + fieldtype->offset);
parse_commaskip_maybe();
if (!type->isstruct)
break;
}
if (brace)
parse_brace_skipto();
complete:
it = list_iterator(table_keys(type->fields));
while (!list_iterator_end(it)) {
char *fieldname = list_iterator_next(it);
if (table_find(wrote, fieldname))
continue;
data_type_t *fieldtype = table_find(type->fields, fieldname);
parse_initializer_zero(init, fieldtype, offset + fieldtype->offset);
}
}
static void parse_initializer_array(list_t *init, data_type_t *type, int offset) {
bool brace = parse_brace_maybe();
int size = type->pointer->size;
int i;
for (i = 0; type->length == -1 || i < type->length; i++) {
lexer_token_t *token = lexer_next();
if (lexer_ispunct(token, '}')) {
if (!brace)
lexer_unget(token);
goto complete;
}
lexer_unget(token);
parse_initializer_element(init, type->pointer, offset + size * i);
parse_commaskip_maybe();
}
if (brace)
parse_brace_skipto();
complete:
if (type->length == -1) {
type->length = i;
type->size = size * i;
}
for (; i < type->length; i++)
parse_initializer_zero(init, type->pointer, offset + size * i);
}
static void parse_initializer_list(list_t *init, data_type_t *type, int offset) {
lexer_token_t *token = lexer_next();
if (type->type == TYPE_ARRAY && type->pointer->type == TYPE_CHAR) {
if (token->type == LEXER_TOKEN_STRING) {
parse_assign_string(init, type, token->string, offset);
return;
}
if (lexer_ispunct(token, '{') && lexer_peek()->type == LEXER_TOKEN_STRING) {
parse_assign_string(init, type, token->string, offset);
parse_expect('}');
return;
}
}
lexer_unget(token);
if (type->type == TYPE_ARRAY)
parse_initializer_array(init, type, offset);
else if (type->type == TYPE_STRUCTURE)
parse_initializer_structure(init, type, offset);
else
compile_error("ICE");
}
static list_t *parse_initializer_declaration(data_type_t *type) {
list_t *list = list_create();
if (type->type == TYPE_ARRAY || type->type == TYPE_STRUCTURE)
parse_initializer_list(list, type, 0);
else
list_push(list, ast_initializer(parse_expression(), type, 0));
return list;
}
/* declarator */
static data_type_t *parse_declaration_specification(storage_t *rstorage) {
storage_t storage = 0;
lexer_token_t *token = lexer_peek();
if (!token || token->type != LEXER_TOKEN_IDENTIFIER)
compile_error("internal error in declaration specification parsing");
/*
* large declaration specification state machine:
* There is six pieces of state to the following state machine
* for dealing with all the permutations of declaration
* specification.
*
* 1: The type, most common of course, this is the "base type"
* of the declaration.