914f3a61c072d431f2132991f5bc2c65afb1ac94
[cparser] / parser.c
1 #include <config.h>
2
3 #include <assert.h>
4 #include <stdarg.h>
5
6 #include "parser.h"
7 #include "lexer.h"
8 #include "token_t.h"
9 #include "type_t.h"
10 #include "type_hash.h"
11 #include "ast_t.h"
12 #include "adt/bitfiddle.h"
13 #include "adt/error.h"
14 #include "adt/array.h"
15
16 #define PRINT_TOKENS
17 #define MAX_LOOKAHEAD 2
18
19 struct environment_entry_t {
20         symbol_t      *symbol;
21         declaration_t *old_declaration;
22         const void    *old_context;
23 };
24
25 static token_t               token;
26 static token_t               lookahead_buffer[MAX_LOOKAHEAD];
27 static int                   lookahead_bufpos;
28 static struct obstack        environment_obstack;
29 static environment_entry_t **environment_stack = NULL;
30 static translation_unit_t   *translation_unit  = NULL;
31 static block_statement_t    *context           = NULL;
32
33 static
34 statement_t *parse_compound_statement(void);
35 static
36 statement_t *parse_statement(void);
37
38 static
39 expression_t *parse_sub_expression(unsigned precedence);
40 static
41 expression_t *parse_expression(void);
42
43 static inline
44 void *allocate_ast_zero(size_t size)
45 {
46         void *res = allocate_ast(size);
47         memset(res, 0, size);
48         return res;
49 }
50
51 static inline
52 void *allocate_type_zero(size_t size)
53 {
54         void *res = obstack_alloc(type_obst, size);
55         memset(res, 0, size);
56         return res;
57 }
58
59 /**
60  * returns the top element of the environment stack
61  */
62 static inline
63 size_t environment_top()
64 {
65         return ARR_LEN(environment_stack);
66 }
67
68
69
70 static inline
71 void next_token(void)
72 {
73         token                              = lookahead_buffer[lookahead_bufpos];
74         lookahead_buffer[lookahead_bufpos] = lexer_token;
75         lexer_next_token();
76
77         lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
78
79 #ifdef PRINT_TOKENS
80         print_token(stderr, &token);
81         fprintf(stderr, "\n");
82 #endif
83 }
84
85 static inline
86 const token_t *la(int num)
87 {
88         assert(num > 0 && num <= MAX_LOOKAHEAD);
89         int pos = (num-1) % MAX_LOOKAHEAD;
90         return & lookahead_buffer[pos];
91 }
92
93 static inline
94 void eat(token_type_t type)
95 {
96         assert(token.type == type);
97         next_token();
98 }
99
100 void parser_print_error_prefix_pos(const source_position_t source_position)
101 {
102     fputs(source_position.input_name, stderr);
103     fputc(':', stderr);
104     fprintf(stderr, "%d", source_position.linenr);
105     fputs(": error: ", stderr);
106 }
107
108 void parser_print_error_prefix(void)
109 {
110         parser_print_error_prefix_pos(token.source_position);
111 }
112
113 static
114 void parse_error(const char *message)
115 {
116         parser_print_error_prefix();
117         fprintf(stderr, "parse error: %s\n", message);
118 }
119
120 static
121 void parse_error_expected(const char *message, ...)
122 {
123         va_list args;
124         int first = 1;
125
126         if(message != NULL) {
127                 parser_print_error_prefix();
128                 fprintf(stderr, "%s\n", message);
129         }
130         parser_print_error_prefix();
131         fputs("Parse error: got ", stderr);
132         print_token(stderr, &token);
133         fputs(", expected ", stderr);
134
135         va_start(args, message);
136         token_type_t token_type = va_arg(args, token_type_t);
137         while(token_type != 0) {
138                 if(first == 1) {
139                         first = 0;
140                 } else {
141                         fprintf(stderr, ", ");
142                 }
143                 print_token_type(stderr, token_type);
144                 token_type = va_arg(args, token_type_t);
145         }
146         va_end(args);
147         fprintf(stderr, "\n");
148 }
149
150 static
151 void eat_until(int token_type)
152 {
153         while(token.type != token_type) {
154                 if(token.type == T_EOF)
155                         return;
156                 next_token();
157         }
158         next_token();
159 }
160
161 #define expect(expected)                           \
162     if(UNLIKELY(token.type != (expected))) {       \
163         parse_error_expected(NULL, (expected), 0); \
164         eat_until(';');                            \
165         return NULL;                               \
166     }                                              \
167     next_token();
168
169 #define expect_void(expected)                      \
170     if(UNLIKELY(token.type != (expected))) {       \
171         parse_error_expected(NULL, (expected), 0); \
172         eat_until(';');                            \
173         return;                                    \
174     }                                              \
175     next_token();
176
177
178 /**
179  * pushs an environment_entry on the environment stack and links the
180  * corresponding symbol to the new entry
181  */
182 static inline
183 void environment_push(declaration_t *declaration, const void *context)
184 {
185         environment_entry_t *entry
186                 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
187         memset(entry, 0, sizeof(entry[0]));
188
189         int top = ARR_LEN(environment_stack);
190         ARR_RESIZE(environment_stack, top + 1);
191         environment_stack[top] = entry;
192
193         symbol_t *symbol = declaration->symbol;
194         assert(declaration != symbol->declaration);
195
196         if(symbol->context == context) {
197                 if(context != NULL) {
198                         assert(symbol->declaration != NULL);
199                         parser_print_error_prefix_pos(declaration->source_position);
200                         fprintf(stderr, "multiple definitions for symbol '%s'.\n",
201                                         symbol->string);
202                         parser_print_error_prefix_pos(symbol->declaration->source_position);
203                         fprintf(stderr, "this is the location of the previous declaration.\n");
204                 }
205         }
206
207         entry->old_declaration = symbol->declaration;
208         entry->old_context     = symbol->context;
209         entry->symbol          = symbol;
210         symbol->declaration    = declaration;
211         symbol->context        = context;
212 }
213
214 /**
215  * pops symbols from the environment stack until @p new_top is the top element
216  */
217 static inline
218 void environment_pop_to(size_t new_top)
219 {
220         environment_entry_t *entry = NULL;
221         size_t top = ARR_LEN(environment_stack);
222         size_t i;
223
224         if(new_top == top)
225                 return;
226
227         assert(new_top < top);
228         i = top;
229         do {
230                 entry = environment_stack[i - 1];
231
232                 symbol_t *symbol = entry->symbol;
233
234                 symbol->declaration = entry->old_declaration;
235                 symbol->context     = entry->old_context;
236
237                 --i;
238         } while(i != new_top);
239         obstack_free(&environment_obstack, entry);
240
241         ARR_SHRINKLEN(environment_stack, (int) new_top);
242 }
243
244
245
246 static expression_t *parse_constant_expression(void)
247 {
248         /* TODO: not correct yet */
249         return parse_expression();
250 }
251
252 static expression_t *parse_assignment_expression(void)
253 {
254         /* TODO: not correct yet */
255         return parse_expression();
256 }
257
258 static compound_entry_t *parse_compound_type_entries(void);
259 static void parse_declarator(declaration_t *declaration,
260                              storage_class_t storage_class, type_t *type);
261
262 typedef struct declaration_specifiers_t  declaration_specifiers_t;
263 struct declaration_specifiers_t {
264         storage_class_t  storage_class;
265         type_t          *type;
266 };
267
268 static type_t *parse_struct_specifier(void)
269 {
270         eat(T_struct);
271
272         compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
273         struct_type->type.type       = TYPE_COMPOUND_STRUCT;
274         struct_type->source_position = token.source_position;
275
276         if(token.type == T_IDENTIFIER) {
277                 /* TODO */
278                 next_token();
279                 if(token.type == '{') {
280                         parse_compound_type_entries();
281                 }
282         } else if(token.type == '{') {
283                 parse_compound_type_entries();
284         } else {
285                 parse_error_expected("problem while parsing struct type specifiers",
286                                      T_IDENTIFIER, '{', 0);
287                 return NULL;
288         }
289
290         return (type_t*) struct_type;
291 }
292
293 static type_t *parse_union_specifier(void)
294 {
295         eat(T_union);
296
297         compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
298         union_type->type.type       = TYPE_COMPOUND_UNION;
299         union_type->source_position = token.source_position;
300
301         if(token.type == T_IDENTIFIER) {
302                 union_type->symbol = token.v.symbol;
303                 next_token();
304                 if(token.type == '{') {
305                         parse_compound_type_entries();
306                 }
307         } else if(token.type == '{') {
308                 parse_compound_type_entries();
309         } else {
310                 parse_error_expected("problem while parsing union type specifiers",
311                                      T_IDENTIFIER, '{');
312         }
313
314         return (type_t*) union_type;
315 }
316
317 static void parse_enum_type_entries()
318 {
319         eat('{');
320
321         if(token.type == '}') {
322                 next_token();
323                 parse_error("empty enum not allowed");
324                 return;
325         }
326
327         do {
328                 if(token.type != T_IDENTIFIER) {
329                         parse_error_expected("problem while parsing enum entry",
330                                              T_IDENTIFIER, 0);
331                         eat_until('}');
332                         return;
333                 }
334                 next_token();
335
336                 if(token.type == '=') {
337                         parse_constant_expression();
338                 }
339
340                 if(token.type != ',')
341                         break;
342                 next_token();
343         } while(token.type != '}');
344
345         expect_void('}');
346 }
347
348 static type_t *parse_enum_specifier(void)
349 {
350         eat(T_enum);
351
352         enum_type_t *enum_type     = allocate_type_zero(sizeof(enum_type[0]));
353         enum_type->type.type       = TYPE_ENUM;
354         enum_type->source_position = token.source_position;
355
356         if(token.type == T_IDENTIFIER) {
357                 enum_type->symbol = token.v.symbol;
358                 next_token();
359                 if(token.type == '{') {
360                         parse_enum_type_entries();
361                 }
362         } else if(token.type == '{') {
363                 parse_enum_type_entries();
364         } else {
365                 parse_error_expected("problem while parsing enum type specifiers",
366                                      T_IDENTIFIER, '{');
367         }
368
369         return (type_t*) enum_type;
370 }
371
372 typedef enum {
373         SPECIFIER_SIGNED    = 1 << 0,
374         SPECIFIER_UNSIGNED  = 1 << 1,
375         SPECIFIER_LONG      = 1 << 2,
376         SPECIFIER_INT       = 1 << 3,
377         SPECIFIER_DOUBLE    = 1 << 4,
378         SPECIFIER_CHAR      = 1 << 5,
379         SPECIFIER_SHORT     = 1 << 6,
380         SPECIFIER_LONG_LONG = 1 << 7,
381         SPECIFIER_FLOAT     = 1 << 8,
382         SPECIFIER_BOOL      = 1 << 9,
383         SPECIFIER_VOID      = 1 << 10,
384 #ifdef PROVIDE_COMPLEX
385         SPECIFIER_COMPLEX   = 1 << 11,
386 #endif
387 #ifdef PROVIDE_IMAGINARY
388         SPECIFIER_IMAGINARY = 1 << 12,
389 #endif
390 } specifiers_t;
391
392 #define STORAGE_CLASSES     \
393         case T_typedef:         \
394         case T_extern:          \
395         case T_static:          \
396         case T_auto:            \
397         case T_register:
398
399 #define TYPE_QUALIFIERS     \
400         case T_const:           \
401         case T_restrict:        \
402         case T_volatile:        \
403         case T_inline:          \
404         case T___extension__:
405
406 #ifdef PROVIDE_COMPLEX
407 #define COMPLEX_SPECIFIERS  \
408         case T__Complex:
409 #else
410 #define COMPLEX_SPECIFIERS
411 #endif
412
413 #ifdef PROVIDE_IMAGINARY
414 #define IMAGINARY_SPECIFIERS \
415         case T__Imaginary:
416 #else
417 #define IMAGINARY_SPECIFIERS
418 #endif
419
420 #define TYPE_SPECIFIERS     \
421         case T_void:            \
422         case T_char:            \
423         case T_short:           \
424         case T_int:             \
425         case T_long:            \
426         case T_float:           \
427         case T_double:          \
428         case T_signed:          \
429         case T_unsigned:        \
430         case T__Bool:           \
431         case T_struct:          \
432         case T_union:           \
433         case T_enum:            \
434         COMPLEX_SPECIFIERS      \
435         IMAGINARY_SPECIFIERS
436 /* TODO: T_IDENTIFIER && typename */
437
438 #define DECLARATION_START   \
439         STORAGE_CLASSES         \
440         TYPE_QUALIFIERS         \
441         TYPE_SPECIFIERS
442
443 static
444 type_t *create_builtin_type(symbol_t *symbol)
445 {
446         builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
447         type->type.type      = TYPE_BUILTIN;
448         type->symbol         = symbol;
449
450         type_t *result = typehash_insert((type_t*) type);
451         if(result != (type_t*) type) {
452                 obstack_free(type_obst, type);
453         }
454
455         return result;
456 }
457
458 static
459 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
460 {
461         declaration_t *declaration;
462         type_t        *type            = NULL;
463         unsigned       type_qualifiers = 0;
464         unsigned       type_specifiers = 0;
465
466         while(1) {
467                 switch(token.type) {
468
469                 /* storage class */
470 #define MATCH_STORAGE_CLASS(token, class)                                \
471                 case token:                                                      \
472                         if(specifiers->storage_class != STORAGE_CLASS_NONE) {        \
473                                 parse_error("multiple storage classes in declaration "   \
474                                             "specifiers");                               \
475                         }                                                            \
476                         specifiers->storage_class = class;                           \
477                         next_token();                                                \
478                         break;
479
480                 MATCH_STORAGE_CLASS(T_typedef,  STORAGE_CLASS_TYPEDEF)
481                 MATCH_STORAGE_CLASS(T_extern,   STORAGE_CLASS_EXTERN)
482                 MATCH_STORAGE_CLASS(T_static,   STORAGE_CLASS_STATIC)
483                 MATCH_STORAGE_CLASS(T_auto,     STORAGE_CLASS_AUTO)
484                 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
485
486                 /* type qualifiers */
487 #define MATCH_TYPE_QUALIFIER(token, qualifier)                          \
488                 case token:                                                     \
489                         type_qualifiers |= qualifier;                               \
490                         next_token();                                               \
491                         break;
492
493                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
494                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
495                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
496                 MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
497
498                 case T___extension__:
499                         /* TODO */
500                         next_token();
501                         break;
502
503                 /* type specifiers */
504 #define MATCH_SPECIFIER(token, specifier, name)                         \
505                 case token:                                                     \
506                         next_token();                                               \
507                         if(type_specifiers & specifier) {                           \
508                                 parse_error("multiple " name " type specifiers given"); \
509                         } else {                                                    \
510                                 type_specifiers |= specifier;                           \
511                         }                                                           \
512                         break;
513
514                 MATCH_SPECIFIER(T_void,       SPECIFIER_VOID,      "void")
515                 MATCH_SPECIFIER(T_char,       SPECIFIER_CHAR,      "char")
516                 MATCH_SPECIFIER(T_short,      SPECIFIER_SHORT,     "short")
517                 MATCH_SPECIFIER(T_int,        SPECIFIER_INT,       "int")
518                 MATCH_SPECIFIER(T_float,      SPECIFIER_FLOAT,     "float")
519                 MATCH_SPECIFIER(T_double,     SPECIFIER_DOUBLE,    "double")
520                 MATCH_SPECIFIER(T_signed,     SPECIFIER_SIGNED,    "signed")
521                 MATCH_SPECIFIER(T_unsigned,   SPECIFIER_UNSIGNED,  "unsigned")
522                 MATCH_SPECIFIER(T__Bool,      SPECIFIER_BOOL,      "_Bool")
523 #ifdef PROVIDE_COMPLEX
524                 MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex")
525 #endif
526 #ifdef PROVIDE_IMAGINARY
527                 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
528 #endif
529                 case T_long:
530                         next_token();
531                         if(type_specifiers & SPECIFIER_LONG_LONG) {
532                                 parse_error("multiple type specifiers given");
533                         } else if(type_specifiers & SPECIFIER_LONG) {
534                                 type_specifiers |= SPECIFIER_LONG_LONG;
535                         } else {
536                                 type_specifiers |= SPECIFIER_LONG;
537                         }
538                         break;
539
540                 /* TODO: if type != NULL for the following rules issue an error */
541                 case T_struct:
542                         type = parse_struct_specifier();
543                         break;
544                 case T_union:
545                         type = parse_union_specifier();
546                         break;
547                 case T_enum:
548                         type = parse_enum_specifier();
549                         break;
550                 case T___builtin_va_list:
551                         type = create_builtin_type(token.v.symbol);
552                         next_token();
553                         break;
554
555                 case T_IDENTIFIER:
556                         declaration = token.v.symbol->declaration;
557                         if(declaration == NULL ||
558                                         declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
559                                 goto finish_specifiers;
560                         }
561
562                         type = declaration->type;
563                         assert(type != NULL);
564                         next_token();
565                         break;
566
567                 /* function specifier */
568                 default:
569                         goto finish_specifiers;
570                 }
571         }
572
573 finish_specifiers:
574
575         if(type == NULL) {
576                 atomic_type_type_t atomic_type;
577
578                 /* match valid basic types */
579                 switch(type_specifiers) {
580                 case SPECIFIER_VOID:
581                         atomic_type = ATOMIC_TYPE_VOID;
582                         break;
583                 case SPECIFIER_CHAR:
584                         atomic_type = ATOMIC_TYPE_CHAR;
585                         break;
586                 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
587                         atomic_type = ATOMIC_TYPE_SCHAR;
588                         break;
589                 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
590                         atomic_type = ATOMIC_TYPE_UCHAR;
591                         break;
592                 case SPECIFIER_SHORT:
593                 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
594                 case SPECIFIER_SHORT | SPECIFIER_INT:
595                 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
596                         atomic_type = ATOMIC_TYPE_SHORT;
597                         break;
598                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
599                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
600                         atomic_type = ATOMIC_TYPE_USHORT;
601                         break;
602                 case SPECIFIER_INT:
603                 case SPECIFIER_SIGNED:
604                 case SPECIFIER_SIGNED | SPECIFIER_INT:
605                         atomic_type = ATOMIC_TYPE_INT;
606                         break;
607                 case SPECIFIER_UNSIGNED:
608                 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
609                         atomic_type = ATOMIC_TYPE_UINT;
610                         break;
611                 case SPECIFIER_LONG:
612                 case SPECIFIER_SIGNED | SPECIFIER_LONG:
613                 case SPECIFIER_LONG | SPECIFIER_INT:
614                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
615                         atomic_type = ATOMIC_TYPE_LONG;
616                         break;
617                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
618                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
619                         atomic_type = ATOMIC_TYPE_ULONG;
620                         break;
621                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
622                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
623                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
624                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
625                         | SPECIFIER_INT:
626                         atomic_type = ATOMIC_TYPE_LONGLONG;
627                         break;
628                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
629                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
630                         | SPECIFIER_INT:
631                         atomic_type = ATOMIC_TYPE_ULONGLONG;
632                         break;
633                 case SPECIFIER_FLOAT:
634                         atomic_type = ATOMIC_TYPE_FLOAT;
635                         break;
636                 case SPECIFIER_DOUBLE:
637                         atomic_type = ATOMIC_TYPE_DOUBLE;
638                         break;
639                 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
640                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
641                         break;
642                 case SPECIFIER_BOOL:
643                         atomic_type = ATOMIC_TYPE_BOOL;
644                         break;
645 #ifdef PROVIDE_COMPLEX
646                 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
647                         atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
648                         break;
649                 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
650                         atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
651                         break;
652                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
653                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
654                         break;
655 #endif
656 #ifdef PROVIDE_IMAGINARY
657                 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
658                         atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
659                         break;
660                 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
661                         atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
662                         break;
663                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
664                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
665                         break;
666 #endif
667                 default:
668                         /* invalid specifier combination, give an error message */
669                         if(type_specifiers == 0) {
670                                 parse_error("no type specifiers given in declaration");
671                         } else if((type_specifiers & SPECIFIER_SIGNED) &&
672                                   (type_specifiers & SPECIFIER_UNSIGNED)) {
673                                 parse_error("signed and unsigned specifiers gives");
674                         } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
675                                 parse_error("only integer types can be signed or unsigned");
676                         } else {
677                                 parse_error("multiple datatypes in declaration");
678                         }
679                         atomic_type = ATOMIC_TYPE_INVALID;
680                 }
681
682                 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
683                 atype->type.type     = TYPE_ATOMIC;
684                 atype->atype         = atomic_type;
685
686                 type = (type_t*) atype;
687         } else {
688                 if(type_specifiers != 0) {
689                         parse_error("multiple datatypes in declaration");
690                 }
691         }
692
693         type->qualifiers = type_qualifiers;
694
695         type_t *result = typehash_insert(type);
696         if(result != (type_t*) type) {
697                 obstack_free(type_obst, type);
698         }
699
700         specifiers->type = result;
701
702         fprintf(stderr, "Specifiers type: ");
703         print_type(stderr, result);
704         fprintf(stderr, "\n");
705 }
706
707 static
708 unsigned parse_type_qualifiers()
709 {
710         unsigned type_qualifiers = 0;
711
712         while(1) {
713                 switch(token.type) {
714                 /* type qualifiers */
715                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
716                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
717                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
718                 MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
719
720                 default:
721                         return type_qualifiers;
722                 }
723         }
724 }
725
726 static
727 type_t *parse_pointer(type_t *type)
728 {
729         while(token.type == '*') {
730                 /* pointer */
731                 next_token();
732
733                 pointer_type_t *pointer_type
734                         = allocate_type_zero(sizeof(pointer_type[0]));
735                 pointer_type->type.type = TYPE_POINTER;
736                 pointer_type->points_to = type;
737
738                 pointer_type->type.qualifiers = parse_type_qualifiers();
739
740                 type_t *result = typehash_insert((type_t*) pointer_type);
741                 if(result != (type_t*) pointer_type) {
742                         obstack_free(type_obst, pointer_type);
743                 }
744
745                 type = result;
746         }
747
748         return type;
749 }
750
751 static
752 void parse_identifier_list()
753 {
754         while(1) {
755                 if(token.type != T_IDENTIFIER) {
756                         parse_error_expected("problem while parsing parameter identifier "
757                                              "list", T_IDENTIFIER, 0);
758                         return;
759                 }
760                 next_token();
761                 if(token.type != ',')
762                         break;
763                 next_token();
764         }
765 }
766
767 static
768 void parse_parameter()
769 {
770         if(token.type == T_DOTDOTDOT) {
771                 next_token();
772                 return;
773         }
774
775         declaration_specifiers_t specifiers;
776         memset(&specifiers, 0, sizeof(specifiers));
777
778         parse_declaration_specifiers(&specifiers);
779         specifiers.type = parse_pointer(specifiers.type);
780
781         if(token.type == '(' || token.type == T_IDENTIFIER) {
782                 declaration_t declaration;
783                 memset(&declaration, 0, sizeof(declaration));
784                 parse_declarator(&declaration, specifiers.storage_class,
785                                  specifiers.type);
786         }
787 }
788
789 static
790 void parse_parameters()
791 {
792         if(token.type == T_IDENTIFIER) {
793                 parse_identifier_list();
794                 return;
795         }
796
797         while(1) {
798                 switch(token.type) {
799                 case T_DOTDOTDOT:
800                 DECLARATION_START
801                         parse_parameter();
802                         break;
803                 default:
804                         return;
805                 }
806                 if(token.type != ',')
807                         return;
808                 next_token();
809         }
810 }
811
812 static
813 void parse_attributes(void)
814 {
815         while(token.type == T___attribute__) {
816                 next_token();
817                 fprintf(stderr, "TODO: __attribute__ not handled yet\n");
818
819                 expect_void('(');
820                 int depth = 1;
821                 while(depth > 0) {
822                         switch(token.type) {
823                         case T_EOF:
824                                 parse_error("EOF while parsing attribute");
825                                 break;
826                         case '(':
827                                 next_token();
828                                 depth++;
829                                 break;
830                         case ')':
831                                 next_token();
832                                 depth--;
833                                 break;
834                         default:
835                                 next_token();
836                         }
837                 }
838         }
839 }
840
841 static
842 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
843                       type_t *type)
844 {
845         type = parse_pointer(type);
846         declaration->storage_class = storage_class;
847         declaration->type          = type;
848
849         switch(token.type) {
850         case T_IDENTIFIER:
851                 declaration->symbol = token.v.symbol;
852                 next_token();
853                 break;
854         case '(':
855                 next_token();
856                 parse_declarator(declaration, storage_class, type);
857                 expect_void(')');
858                 break;
859         default:
860                 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
861                                      '(', 0);
862         }
863
864         while(1) {
865                 switch(token.type) {
866                 case '(':
867                         next_token();
868
869                         parse_parameters();
870
871                         expect_void(')');
872                         break;
873                 case '[':
874                         next_token();
875
876                         if(token.type == T_static) {
877                                 next_token();
878                         }
879
880                         unsigned type_qualifiers = parse_type_qualifiers();
881                         if(type_qualifiers != 0) {
882                                 if(token.type == T_static) {
883                                         next_token();
884                                 }
885                         }
886
887                         if(token.type == '*' /* TODO: && lookahead == ']' */) {
888                                 next_token();
889                         } else if(token.type != ']') {
890                                 parse_assignment_expression();
891                         }
892
893                         expect_void(']');
894                         break;
895                 default:
896                         goto declarator_finished;
897                 }
898         }
899
900 declarator_finished:
901         parse_attributes();
902
903         fprintf(stderr, "Declarator type: ");
904         print_type(stderr, type);
905         fprintf(stderr, "\n");
906
907         symbol_t *symbol = declaration->symbol;
908
909         if(symbol != NULL) {
910                 environment_push(declaration, context);
911         }
912 }
913
914 static
915 void parse_init_declarators(const declaration_specifiers_t *specifiers)
916 {
917         while(1) {
918                 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
919
920                 parse_declarator(declaration, specifiers->storage_class,
921                                  specifiers->type);
922                 if(token.type == '=') {
923                         next_token();
924                         if(token.type == '{') {
925                                 // TODO
926                                 expect_void('}');
927                         } else {
928                                 parse_assignment_expression();
929                         }
930                 } else if(token.type == '{') {
931                         parse_compound_statement();
932                         return;
933                 }
934
935                 if(token.type != ',')
936                         break;
937                 next_token();
938         }
939         expect_void(';');
940 }
941
942 static
943 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
944 {
945         while(1) {
946                 declaration_t declaration;
947                 compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
948
949                 if(token.type == ':') {
950                         next_token();
951                         parse_constant_expression();
952                         /* TODO */
953                 } else {
954                         parse_declarator(&declaration, specifiers->storage_class,
955                                          specifiers->type);
956
957                         if(token.type == ':') {
958                                 next_token();
959                                 parse_constant_expression();
960                                 /* TODO */
961                         }
962                 }
963
964                 if(token.type != ',')
965                         break;
966                 next_token();
967         }
968         expect_void(';');
969 }
970
971 static compound_entry_t *parse_compound_type_entries(void)
972 {
973         eat('{');
974
975         compound_entry_t *entries = NULL;
976
977         while(token.type != '}' && token.type != T_EOF) {
978                 declaration_specifiers_t specifiers;
979                 memset(&specifiers, 0, sizeof(specifiers));
980                 /* TODO not correct as this allows storage class stuff... but only
981                  * specifiers and qualifiers sould be allowed here */
982                 parse_declaration_specifiers(&specifiers);
983
984                 parse_struct_declarators(&specifiers);
985         }
986         next_token();
987
988         return entries;
989 }
990
991 void parse_declaration(void)
992 {
993         declaration_specifiers_t specifiers;
994         memset(&specifiers, 0, sizeof(specifiers));
995         parse_declaration_specifiers(&specifiers);
996
997         if(token.type == ';') {
998                 next_token();
999                 return;
1000         }
1001         parse_init_declarators(&specifiers);
1002 }
1003
1004 type_t *parse_typename(void)
1005 {
1006         declaration_specifiers_t specifiers;
1007         memset(&specifiers, 0, sizeof(specifiers));
1008         /* TODO not correct storage class elements are not allowed here */
1009         parse_declaration_specifiers(&specifiers);
1010
1011         specifiers.type = parse_pointer(specifiers.type);
1012
1013         return specifiers.type;
1014 }
1015
1016
1017
1018
1019 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1020 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1021                                                           expression_t *left);
1022
1023 typedef struct expression_parser_function_t expression_parser_function_t;
1024 struct expression_parser_function_t {
1025         unsigned                         precedence;
1026         parse_expression_function        parser;
1027         unsigned                         infix_precedence;
1028         parse_expression_infix_function  infix_parser;
1029 };
1030
1031 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1032
1033 static
1034 expression_t *expected_expression_error(void)
1035 {
1036         parser_print_error_prefix();
1037         fprintf(stderr, "expected expression, got token ");
1038         print_token(stderr, & token);
1039         fprintf(stderr, "\n");
1040
1041         expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1042         expression->type = EXPR_INVALID;
1043         next_token();
1044
1045         return expression;
1046 }
1047
1048 static
1049 expression_t *parse_string_const(void)
1050 {
1051         string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1052
1053         cnst->expression.type = EXPR_STRING_LITERAL;
1054         cnst->value           = token.v.string;
1055
1056         next_token();
1057
1058         return (expression_t*) cnst;
1059 }
1060
1061 static
1062 expression_t *parse_int_const(void)
1063 {
1064         const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1065
1066         cnst->expression.type = EXPR_CONST;
1067         cnst->value           = token.v.intvalue;
1068
1069         next_token();
1070
1071         return (expression_t*) cnst;
1072 }
1073
1074 static
1075 expression_t *parse_reference(void)
1076 {
1077         reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1078
1079         ref->expression.type            = EXPR_REFERENCE;
1080         ref->symbol                     = token.v.symbol;
1081
1082         next_token();
1083
1084         return (expression_t*) ref;
1085 }
1086
1087 static
1088 expression_t *parse_brace_expression(void)
1089 {
1090         eat('(');
1091
1092         expression_t *result = parse_expression();
1093         expect(')');
1094
1095         return result;
1096 }
1097
1098 static
1099 expression_t *parse_primary_expression(void)
1100 {
1101         switch(token.type) {
1102         case T_INTEGER:
1103                 return parse_int_const();
1104         case T_STRING_LITERAL:
1105                 return parse_string_const();
1106         case T_IDENTIFIER:
1107                 return parse_reference();
1108         case '(':
1109                 return parse_brace_expression();
1110         }
1111
1112         /* TODO: error message */
1113         return NULL;
1114 }
1115
1116 static
1117 expression_t *parse_array_expression(unsigned precedence,
1118                                      expression_t *array_ref)
1119 {
1120         (void) precedence;
1121
1122         eat('[');
1123
1124         array_access_expression_t *array_access
1125                 = allocate_ast_zero(sizeof(array_access[0]));
1126
1127         array_access->expression.type = EXPR_ARRAY_ACCESS;
1128         array_access->array_ref       = array_ref;
1129         array_access->index           = parse_expression();
1130
1131         if(token.type != ']') {
1132                 parse_error_expected("Problem while parsing array access", ']', 0);
1133                 return NULL;
1134         }
1135         next_token();
1136
1137         return (expression_t*) array_access;
1138 }
1139
1140 static
1141 type_t *get_expression_type(const expression_t *expression)
1142 {
1143         (void) expression;
1144         /* TODO */
1145         return NULL;
1146 }
1147
1148 static
1149 expression_t *parse_sizeof(unsigned precedence)
1150 {
1151         eat(T_sizeof);
1152
1153         sizeof_expression_t *sizeof_expression
1154                 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1155         sizeof_expression->expression.type = EXPR_SIZEOF;
1156
1157         if(token.type == '(' /* && LA1 is type_specifier */) {
1158                 next_token();
1159                 sizeof_expression->type = parse_typename();
1160                 expect(')');
1161         } else {
1162                 expression_t *expression = parse_sub_expression(precedence);
1163                 sizeof_expression->type  = get_expression_type(expression);
1164         }
1165
1166         return (expression_t*) sizeof_expression;
1167 }
1168
1169 static
1170 expression_t *parse_select_expression(unsigned precedence,
1171                                       expression_t *compound)
1172 {
1173         (void) precedence;
1174
1175         assert(token.type == '.' || token.type == T_SELECT);
1176         next_token();
1177
1178         select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1179
1180         select->expression.type = EXPR_SELECT;
1181         select->compound        = compound;
1182
1183         if(token.type != T_IDENTIFIER) {
1184                 parse_error_expected("Problem while parsing compound select",
1185                                      T_IDENTIFIER, 0);
1186                 return NULL;
1187         }
1188         select->symbol = token.v.symbol;
1189         next_token();
1190
1191         return (expression_t*) select;
1192 }
1193
1194 static
1195 expression_t *parse_call_expression(unsigned precedence,
1196                                     expression_t *expression)
1197 {
1198         (void) precedence;
1199         call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1200
1201         call->expression.type            = EXPR_CALL;
1202         call->method                     = expression;
1203
1204         /* parse arguments */
1205         eat('(');
1206
1207         if(token.type != ')') {
1208                 call_argument_t *last_argument = NULL;
1209
1210                 while(1) {
1211                         call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1212
1213                         argument->expression = parse_expression();
1214                         if(last_argument == NULL) {
1215                                 call->arguments = argument;
1216                         } else {
1217                                 last_argument->next = argument;
1218                         }
1219                         last_argument = argument;
1220
1221                         if(token.type != ',')
1222                                 break;
1223                         next_token();
1224                 }
1225         }
1226         expect(')');
1227
1228         return (expression_t*) call;
1229 }
1230
1231 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type)     \
1232 static                                                                    \
1233 expression_t *parse_##unexpression_type(unsigned precedence)              \
1234 {                                                                         \
1235         eat(token_type);                                                      \
1236                                                                           \
1237         unary_expression_t *unary_expression                                  \
1238                 = allocate_ast_zero(sizeof(unary_expression[0]));                 \
1239         unary_expression->expression.type = EXPR_UNARY;                       \
1240         unary_expression->type            = unexpression_type;                \
1241         unary_expression->value           = parse_sub_expression(precedence); \
1242                                                                           \
1243         return (expression_t*) unary_expression;                              \
1244 }
1245
1246 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1247 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1248 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1249 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1250 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1251 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1252 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_PREFIX_INCREMENT)
1253 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1254
1255 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1256 static                                                                        \
1257 expression_t *parse_##unexpression_type(unsigned precedence,                  \
1258                                         expression_t *left)                   \
1259 {                                                                             \
1260         (void) precedence;                                                        \
1261         eat(token_type);                                                          \
1262                                                                               \
1263         unary_expression_t *unary_expression                                      \
1264                 = allocate_ast_zero(sizeof(unary_expression[0]));                     \
1265         unary_expression->expression.type = EXPR_UNARY;                           \
1266         unary_expression->type            = unexpression_type;                    \
1267         unary_expression->value           = left;                                 \
1268                                                                               \
1269         return (expression_t*) unary_expression;                                  \
1270 }
1271
1272 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT)
1273 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1274
1275 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type)    \
1276 static                                                           \
1277 expression_t *parse_##binexpression_type(unsigned precedence,    \
1278                                          expression_t *left)     \
1279 {                                                                \
1280         eat(token_type);                                             \
1281                                                                  \
1282         expression_t *right = parse_sub_expression(precedence);      \
1283                                                                  \
1284         binary_expression_t *binexpr                                 \
1285                 = allocate_ast_zero(sizeof(binexpr[0]));                 \
1286         binexpr->expression.type            = EXPR_BINARY;           \
1287         binexpr->type                       = binexpression_type;    \
1288         binexpr->left                       = left;                  \
1289         binexpr->right                      = right;                 \
1290                                                                  \
1291         return (expression_t*) binexpr;                              \
1292 }
1293
1294 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1295 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1296 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1297 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1298 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1299 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1300 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1301 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1302 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1303 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1304 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1305 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1306 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1307 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1308 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1309 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1310
1311 static
1312 expression_t *parse_sub_expression(unsigned precedence)
1313 {
1314         if(token.type < 0) {
1315                 return expected_expression_error();
1316         }
1317
1318         expression_parser_function_t *parser
1319                 = &expression_parsers[token.type];
1320         source_position_t             source_position = token.source_position;
1321         expression_t                 *left;
1322
1323         if(parser->parser != NULL) {
1324                 left = parser->parser(parser->precedence);
1325         } else {
1326                 left = parse_primary_expression();
1327         }
1328         if(left != NULL)
1329                 left->source_position = source_position;
1330
1331         while(1) {
1332                 if(token.type < 0) {
1333                         return expected_expression_error();
1334                 }
1335
1336                 parser = &expression_parsers[token.type];
1337                 if(parser->infix_parser == NULL)
1338                         break;
1339                 if(parser->infix_precedence < precedence)
1340                         break;
1341
1342                 left = parser->infix_parser(parser->infix_precedence, left);
1343                 if(left != NULL)
1344                         left->source_position = source_position;
1345         }
1346
1347         return left;
1348 }
1349
1350 static
1351 expression_t *parse_expression(void)
1352 {
1353         return parse_sub_expression(1);
1354 }
1355
1356
1357
1358 void register_expression_parser(parse_expression_function parser,
1359                                 int token_type, unsigned precedence)
1360 {
1361         expression_parser_function_t *entry = &expression_parsers[token_type];
1362
1363         if(entry->parser != NULL) {
1364                 fprintf(stderr, "for token ");
1365                 print_token_type(stderr, token_type);
1366                 fprintf(stderr, "\n");
1367                 panic("trying to register multiple expression parsers for a token");
1368         }
1369         entry->parser     = parser;
1370         entry->precedence = precedence;
1371 }
1372
1373 void register_expression_infix_parser(parse_expression_infix_function parser,
1374                                       int token_type, unsigned precedence)
1375 {
1376         expression_parser_function_t *entry = &expression_parsers[token_type];
1377
1378         if(entry->infix_parser != NULL) {
1379                 fprintf(stderr, "for token ");
1380                 print_token_type(stderr, token_type);
1381                 fprintf(stderr, "\n");
1382                 panic("trying to register multiple infix expression parsers for a "
1383                       "token");
1384         }
1385         entry->infix_parser     = parser;
1386         entry->infix_precedence = precedence;
1387 }
1388
1389 static
1390 void init_expression_parsers(void)
1391 {
1392         memset(&expression_parsers, 0, sizeof(expression_parsers));
1393
1394         register_expression_infix_parser(parse_BINEXPR_MUL,       '*', 16);
1395         register_expression_infix_parser(parse_BINEXPR_DIV,       '/', 16);
1396         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1397                                    T_LESSLESS, 16);
1398         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1399                                    T_GREATERGREATER, 16);
1400         register_expression_infix_parser(parse_BINEXPR_ADD,       '+', 15);
1401         register_expression_infix_parser(parse_BINEXPR_SUB,       '-', 15);
1402         register_expression_infix_parser(parse_BINEXPR_LESS,      '<', 14);
1403         register_expression_infix_parser(parse_BINEXPR_GREATER,   '>', 14);
1404         register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1405         register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1406                                    T_GREATEREQUAL, 14);
1407         register_expression_infix_parser(parse_BINEXPR_EQUAL,     T_EQUALEQUAL, 13);
1408         register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1409                                          T_EXCLAMATIONMARKEQUAL, 13);
1410         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&',        12);
1411         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^',        11);
1412         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR,  '|',        10);
1413         register_expression_infix_parser(parse_BINEXPR_ASSIGN,      T_EQUAL,     2);
1414
1415         register_expression_infix_parser(parse_array_expression,        '[',    30);
1416         register_expression_infix_parser(parse_call_expression,         '(',    30);
1417         register_expression_infix_parser(parse_select_expression,       '.',    30);
1418         register_expression_infix_parser(parse_select_expression,  T_SELECT,    30);
1419         register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1420                                          T_PLUSPLUS, 30);
1421         register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1422                                          T_MINUSMINUS, 30);
1423
1424         register_expression_parser(parse_UNEXPR_NEGATE,           '-',          25);
1425         register_expression_parser(parse_UNEXPR_PLUS,             '+',          25);
1426         register_expression_parser(parse_UNEXPR_NOT,              '!',          25);
1427         register_expression_parser(parse_UNEXPR_BITWISE_NEGATE,   '~',          25);
1428         register_expression_parser(parse_UNEXPR_DEREFERENCE,      '*',          25);
1429         register_expression_parser(parse_UNEXPR_TAKE_ADDRESS,     '&',          25);
1430         register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS,   25);
1431         register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1432         register_expression_parser(parse_sizeof,                  T_sizeof,     25);
1433 }
1434
1435
1436 static
1437 statement_t *parse_case_statement(void)
1438 {
1439         eat(T_case);
1440         parse_expression();
1441         expect(':');
1442         parse_statement();
1443
1444         return NULL;
1445 }
1446
1447 static
1448 statement_t *parse_default_statement(void)
1449 {
1450         eat(T_default);
1451         expect(':');
1452         parse_statement();
1453
1454         return NULL;
1455 }
1456
1457 static
1458 statement_t *parse_label_statement(void)
1459 {
1460         eat(T_IDENTIFIER);
1461         expect(':');
1462         parse_statement();
1463
1464         return NULL;
1465 }
1466
1467 static
1468 statement_t *parse_if(void)
1469 {
1470         eat(T_if);
1471         expect('(');
1472         parse_expression();
1473         expect(')');
1474
1475         parse_statement();
1476         if(token.type == T_else) {
1477                 next_token();
1478                 parse_statement();
1479         }
1480
1481         return NULL;
1482 }
1483
1484 static
1485 statement_t *parse_switch(void)
1486 {
1487         eat(T_switch);
1488         expect('(');
1489         parse_expression();
1490         expect(')');
1491         parse_statement();
1492
1493         return NULL;
1494 }
1495
1496 static
1497 statement_t *parse_while(void)
1498 {
1499         eat(T_while);
1500         expect('(');
1501         parse_expression();
1502         expect(')');
1503         parse_statement();
1504
1505         return NULL;
1506 }
1507
1508 static
1509 statement_t *parse_do(void)
1510 {
1511         eat(T_do);
1512         parse_statement();
1513         expect(T_while);
1514         expect('(');
1515         parse_expression();
1516         expect(')');
1517
1518         return NULL;
1519 }
1520
1521 static
1522 statement_t *parse_for(void)
1523 {
1524         eat(T_for);
1525         expect('(');
1526         if(token.type != ';') {
1527                 /* TODO not correct... this could also be a declaration */
1528                 parse_expression();
1529         }
1530         expect(';');
1531         if(token.type != ';') {
1532                 parse_expression();
1533         }
1534         expect(';');
1535         if(token.type != ')') {
1536                 parse_expression();
1537         }
1538         expect(')');
1539         parse_statement();
1540
1541         return NULL;
1542 }
1543
1544 static
1545 statement_t *parse_goto(void)
1546 {
1547         eat(T_goto);
1548         expect(T_IDENTIFIER);
1549         expect(';');
1550
1551         return NULL;
1552 }
1553
1554 static
1555 statement_t *parse_continue(void)
1556 {
1557         eat(T_continue);
1558         expect(';');
1559
1560         return NULL;
1561 }
1562
1563 static
1564 statement_t *parse_break(void)
1565 {
1566         eat(T_break);
1567         expect(';');
1568
1569         return NULL;
1570 }
1571
1572 static
1573 statement_t *parse_return(void)
1574 {
1575         eat(T_return);
1576         parse_expression();
1577         expect(';');
1578
1579         return NULL;
1580 }
1581
1582 static
1583 statement_t *parse_declaration_statement(void)
1584 {
1585         parse_declaration();
1586         return NULL;
1587 }
1588
1589 static
1590 statement_t *parse_expression_statement(void)
1591 {
1592         parse_expression();
1593         return NULL;
1594 }
1595
1596 static
1597 statement_t *parse_statement(void)
1598 {
1599         declaration_t *declaration;
1600         statement_t   *statement = NULL;
1601
1602         /* declaration or statement */
1603         switch(token.type) {
1604         case T_case:
1605                 statement = parse_case_statement();
1606                 break;
1607
1608         case T_default:
1609                 statement = parse_default_statement();
1610                 break;
1611
1612         case '{':
1613                 statement = parse_compound_statement();
1614                 break;
1615
1616         case T_if:
1617                 statement = parse_if();
1618                 break;
1619
1620         case T_switch:
1621                 statement = parse_switch();
1622                 break;
1623
1624         case T_while:
1625                 statement = parse_while();
1626                 break;
1627
1628         case T_do:
1629                 statement = parse_do();
1630                 break;
1631
1632         case T_for:
1633                 statement = parse_for();
1634                 break;
1635
1636         case T_goto:
1637                 statement = parse_goto();
1638                 break;
1639
1640         case T_continue:
1641                 statement = parse_continue();
1642                 break;
1643
1644         case T_break:
1645                 statement = parse_break();
1646                 break;
1647
1648         case T_return:
1649                 statement = parse_return();
1650                 break;
1651
1652         case ';':
1653                 statement = NULL;
1654                 break;
1655
1656         case T_IDENTIFIER:
1657                 if(la(1)->type == ':') {
1658                         statement = parse_label_statement();
1659                         break;
1660                 }
1661
1662                 declaration = token.v.symbol->declaration;
1663                 if(declaration != NULL &&
1664                                 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1665                         statement = parse_declaration_statement();
1666                         break;
1667                 }
1668
1669                 statement = parse_expression_statement();
1670                 break;
1671
1672         DECLARATION_START
1673                 statement = parse_declaration_statement();
1674                 break;
1675         }
1676
1677         return statement;
1678 }
1679
1680 static
1681 statement_t *parse_compound_statement(void)
1682 {
1683         eat('{');
1684
1685         int top = environment_top();
1686
1687         while(token.type != '}') {
1688                 parse_statement();
1689         }
1690
1691         environment_pop_to(top);
1692
1693         next_token();
1694
1695         return NULL;
1696 }
1697
1698 static
1699 translation_unit_t *parse_translation_unit(void)
1700 {
1701         translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1702
1703         assert(translation_unit == NULL);
1704         assert(context == NULL);
1705         translation_unit = unit;
1706
1707         while(token.type != T_EOF) {
1708                 parse_declaration();
1709         }
1710
1711         translation_unit = NULL;
1712         return unit;
1713 }
1714
1715 translation_unit_t *parse(void)
1716 {
1717         obstack_init(&environment_obstack);
1718         environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1719
1720         lookahead_bufpos = 0;
1721         for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
1722                 next_token();
1723         }
1724         translation_unit_t *unit = parse_translation_unit();
1725
1726         DEL_ARR_F(environment_stack);
1727         obstack_free(&environment_obstack, NULL);
1728
1729         return unit;
1730 }
1731
1732 void init_parser(void)
1733 {
1734         init_expression_parsers();
1735 }
1736
1737 void exit_parser(void)
1738 {
1739 }