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