improved support for enums
[cparser] / parser.c
index 35c734b..278c524 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -13,7 +13,8 @@
 #include "adt/error.h"
 #include "adt/array.h"
 
-#define PRINT_TOKENS
+//#define PRINT_TOKENS
+//#define ABORT_ON_ERROR
 #define MAX_LOOKAHEAD 2
 
 struct environment_entry_t {
@@ -27,8 +28,9 @@ static token_t               lookahead_buffer[MAX_LOOKAHEAD];
 static int                   lookahead_bufpos;
 static struct obstack        environment_obstack;
 static environment_entry_t **environment_stack = NULL;
-static translation_unit_t   *translation_unit  = NULL;
-static const void           *context           = NULL;
+static context_t            *context           = NULL;
+static declaration_t        *last_declaration  = NULL;
+static struct obstack        temp_obst;
 
 static
 statement_t *parse_compound_statement(void);
@@ -60,7 +62,7 @@ void *allocate_type_zero(size_t size)
  * returns the top element of the environment stack
  */
 static inline
-size_t environment_top()
+size_t environment_top(void)
 {
        return ARR_LEN(environment_stack);
 }
@@ -86,7 +88,7 @@ static inline
 const token_t *la(int num)
 {
        assert(num > 0 && num <= MAX_LOOKAHEAD);
-       int pos = (num-1) % MAX_LOOKAHEAD;
+       int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
        return & lookahead_buffer[pos];
 }
 
@@ -103,6 +105,9 @@ void parser_print_error_prefix_pos(const source_position_t source_position)
     fputc(':', stderr);
     fprintf(stderr, "%d", source_position.linenr);
     fputs(": error: ", stderr);
+#ifdef ABORT_ON_ERROR
+       abort();
+#endif
 }
 
 void parser_print_error_prefix(void)
@@ -174,6 +179,21 @@ void eat_until(int token_type)
     }                                              \
     next_token();
 
+static void set_context(context_t *new_context)
+{
+       context = new_context;
+
+       declaration_t *declaration = new_context->declarations;
+       if(declaration != NULL) {
+               while(1) {
+                       if(declaration->next == NULL)
+                               break;
+                       declaration = declaration->next;
+               }
+       }
+
+       last_declaration = declaration;
+}
 
 /**
  * pushs an environment_entry on the environment stack and links the
@@ -206,8 +226,6 @@ void environment_push(declaration_t *declaration, const void *context)
                }
        }
 
-       fprintf(stderr, "Set '%s' to %p\n", symbol->string, (void*) declaration);
-
        entry->old_declaration = symbol->declaration;
        entry->old_context     = symbol->context;
        entry->symbol          = symbol;
@@ -259,10 +277,12 @@ static expression_t *parse_assignment_expression(void)
        return parse_expression();
 }
 
-static compound_entry_t *parse_compound_type_entries(void);
+static void parse_compound_type_entries(void);
 static void parse_declarator(declaration_t *declaration,
-                             storage_class_t storage_class, type_t *type);
+                             storage_class_t storage_class, type_t *type,
+                             int may_be_abstract);
 static void maybe_push_declaration(declaration_t *declaration);
+static void record_declaration(declaration_t *declaration);
 
 typedef struct declaration_specifiers_t  declaration_specifiers_t;
 struct declaration_specifiers_t {
@@ -278,29 +298,25 @@ static type_t *parse_struct_specifier(void)
        struct_type->type.type       = TYPE_COMPOUND_STRUCT;
        struct_type->source_position = token.source_position;
 
-       fprintf(stderr, "New struct %p\n", (void*) struct_type);
-
        int         top          = environment_top();
-       const void *last_context = context;
-       context                  = struct_type;
+       context_t  *last_context = context;
+       set_context(&struct_type->context);
 
        if(token.type == T_IDENTIFIER) {
                next_token();
                if(token.type == '{') {
                        parse_compound_type_entries();
                }
-               fprintf(stderr, "Finished struct %p\n",(void*)  struct_type);
        } else if(token.type == '{') {
                parse_compound_type_entries();
-               fprintf(stderr, "Finished struct %p\n", (void*) struct_type);
        } else {
                parse_error_expected("problem while parsing struct type specifiers",
                                     T_IDENTIFIER, '{', 0);
                struct_type = NULL;
        }
 
-       assert(context == struct_type);
-       context = last_context;
+       assert(context == &struct_type->context);
+       set_context(last_context);
        environment_pop_to(top);
 
        return (type_t*) struct_type;
@@ -315,8 +331,8 @@ static type_t *parse_union_specifier(void)
        union_type->source_position = token.source_position;
 
        int         top          = environment_top();
-       const void *last_context = context;
-       context                  = union_type;
+       context_t  *last_context = context;
+       set_context(&union_type->context);
 
        if(token.type == T_IDENTIFIER) {
                union_type->symbol = token.v.symbol;
@@ -332,42 +348,55 @@ static type_t *parse_union_specifier(void)
                union_type = NULL;
        }
 
-       assert(context == union_type);
-       context = last_context;
+       assert(context == &union_type->context);
+       set_context(last_context);
        environment_pop_to(top);
 
        return (type_t*) union_type;
 }
 
-static void parse_enum_type_entries()
+static enum_entry_t *parse_enum_type_entries(void)
 {
        eat('{');
 
        if(token.type == '}') {
                next_token();
                parse_error("empty enum not allowed");
-               return;
+               return NULL;
        }
 
+       enum_entry_t *result     = NULL;
+       enum_entry_t *last_entry = NULL;
        do {
+               enum_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
                if(token.type != T_IDENTIFIER) {
                        parse_error_expected("problem while parsing enum entry",
                                             T_IDENTIFIER, 0);
                        eat_until('}');
-                       return;
+                       return result;
                }
+               entry->symbol = token.v.symbol;
                next_token();
 
                if(token.type == '=') {
-                       parse_constant_expression();
+                       next_token();
+                       entry->value = parse_constant_expression();
                }
 
+               if(last_entry != NULL) {
+                       last_entry->next = entry;
+               } else {
+                       result = entry;
+               }
+               last_entry = entry;
+
                if(token.type != ',')
                        break;
                next_token();
        } while(token.type != '}');
 
-       expect_void('}');
+       expect('}');
+       return result;
 }
 
 static type_t *parse_enum_specifier(void)
@@ -382,10 +411,10 @@ static type_t *parse_enum_specifier(void)
                enum_type->symbol = token.v.symbol;
                next_token();
                if(token.type == '{') {
-                       parse_enum_type_entries();
+                       enum_type->entries = parse_enum_type_entries();
                }
        } else if(token.type == '{') {
-               parse_enum_type_entries();
+               enum_type->entries = parse_enum_type_entries();
        } else {
                parse_error_expected("problem while parsing enum type specifiers",
                                     T_IDENTIFIER, '{');
@@ -394,6 +423,72 @@ static type_t *parse_enum_specifier(void)
        return (type_t*) enum_type;
 }
 
+static
+const char *parse_string_literals(void)
+{
+       assert(token.type == T_STRING_LITERAL);
+       const char *result = token.v.string;
+
+       next_token();
+
+       while(token.type == T_STRING_LITERAL) {
+               result = concat_strings(result, token.v.string);
+               next_token();
+       }
+
+       return result;
+}
+
+static
+void parse_attributes(void)
+{
+       while(1) {
+               switch(token.type) {
+               case T___attribute__:
+                       next_token();
+
+                       expect_void('(');
+                       int depth = 1;
+                       while(depth > 0) {
+                               switch(token.type) {
+                               case T_EOF:
+                                       parse_error("EOF while parsing attribute");
+                                       break;
+                               case '(':
+                                       next_token();
+                                       depth++;
+                                       break;
+                               case ')':
+                                       next_token();
+                                       depth--;
+                                       break;
+                               default:
+                                       next_token();
+                               }
+                       }
+                       break;
+               case T_asm:
+                       next_token();
+                       expect_void('(');
+                       if(token.type != T_STRING_LITERAL) {
+                               parse_error_expected("while parsing assembler attribute",
+                                                    T_STRING_LITERAL);
+                               eat_until(')');
+                               break;
+                       } else {
+                               parse_string_literals();
+                       }
+                       expect_void(')');
+                       break;
+               default:
+                       goto attributes_finished;
+               }
+       }
+
+attributes_finished:
+       ;
+}
+
 typedef enum {
        SPECIFIER_SIGNED    = 1 << 0,
        SPECIFIER_UNSIGNED  = 1 << 1,
@@ -577,6 +672,11 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                        next_token();
                        break;
 
+               case T___attribute__:
+                       /* TODO */
+                       parse_attributes();
+                       break;
+
                case T_IDENTIFIER:
                        declaration = token.v.symbol->declaration;
                        if(declaration == NULL ||
@@ -724,14 +824,10 @@ finish_specifiers:
        }
 
        specifiers->type = result;
-
-       fprintf(stderr, "Specifiers type: ");
-       print_type(stderr, result);
-       fprintf(stderr, "\n");
 }
 
 static
-unsigned parse_type_qualifiers()
+unsigned parse_type_qualifiers(void)
 {
        unsigned type_qualifiers = 0;
 
@@ -749,19 +845,44 @@ unsigned parse_type_qualifiers()
        }
 }
 
+typedef struct parsed_pointer_t parsed_pointer_t;
+struct parsed_pointer_t {
+       unsigned          type_qualifiers;
+       parsed_pointer_t *next;
+};
+
 static
-type_t *parse_pointer(type_t *type)
+parsed_pointer_t *parse_pointers(void)
 {
+       parsed_pointer_t *result       = NULL;
+       parsed_pointer_t *last_pointer = NULL;
+
        while(token.type == '*') {
-               /* pointer */
                next_token();
+               parsed_pointer_t *pointer
+                       = obstack_alloc(&temp_obst, sizeof(pointer[0]));
+               pointer->type_qualifiers = parse_type_qualifiers();
 
+               if(last_pointer != NULL) {
+                       last_pointer->next = pointer;
+               } else {
+                       result = pointer;
+               }
+               last_pointer = pointer;
+       }
+
+       return result;
+}
+
+static
+type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
+{
+       for( ; pointer != NULL; pointer = pointer->next) {
                pointer_type_t *pointer_type
                        = allocate_type_zero(sizeof(pointer_type[0]));
-               pointer_type->type.type = TYPE_POINTER;
-               pointer_type->points_to = type;
-
-               pointer_type->type.qualifiers = parse_type_qualifiers();
+               pointer_type->type.type       = TYPE_POINTER;
+               pointer_type->points_to       = type;
+               pointer_type->type.qualifiers = pointer->type_qualifiers;
 
                type_t *result = typehash_insert((type_t*) pointer_type);
                if(result != (type_t*) pointer_type) {
@@ -775,7 +896,7 @@ type_t *parse_pointer(type_t *type)
 }
 
 static
-void parse_identifier_list()
+void parse_identifier_list(void)
 {
        while(1) {
                if(token.type != T_IDENTIFIER) {
@@ -791,29 +912,22 @@ void parse_identifier_list()
 }
 
 static
-void parse_parameter()
+declaration_t *parse_parameter(void)
 {
-       if(token.type == T_DOTDOTDOT) {
-               next_token();
-               return;
-       }
-
        declaration_specifiers_t specifiers;
        memset(&specifiers, 0, sizeof(specifiers));
 
        parse_declaration_specifiers(&specifiers);
-       specifiers.type = parse_pointer(specifiers.type);
 
-       if(token.type == '(' || token.type == T_IDENTIFIER) {
-               declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
-               parse_declarator(declaration, specifiers.storage_class,
-                                specifiers.type);
-               maybe_push_declaration(declaration);
-       }
+       declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
+       parse_declarator(declaration, specifiers.storage_class,
+                        specifiers.type, 1);
+
+       return declaration;
 }
 
 static
-void parse_parameters()
+void parse_parameters(method_type_t *type)
 {
        if(token.type == T_IDENTIFIER) {
                symbol_t      *symbol      = token.v.symbol;
@@ -825,13 +939,42 @@ void parse_parameters()
                }
        }
 
+       if(token.type == ')') {
+               type->unspecified_parameters = 1;
+               return;
+       }
+       if(token.type == T_void && la(1)->type == ')') {
+               next_token();
+               return;
+       }
+
+       declaration_t           *declaration;
+       method_parameter_type_t *parameter_type;
+       method_parameter_type_t *last_parameter_type = NULL;
+
        while(1) {
                switch(token.type) {
                case T_DOTDOTDOT:
+                       next_token();
+                       type->variadic = 1;
+                       return;
+
                case T_IDENTIFIER:
                DECLARATION_START
-                       parse_parameter();
+                       declaration = parse_parameter();
+
+                       parameter_type = allocate_type_zero(sizeof(parameter_type[0]));
+                       parameter_type->type   = declaration->type;
+                       parameter_type->symbol = declaration->symbol;
+
+                       if(last_parameter_type != NULL) {
+                               last_parameter_type->next = parameter_type;
+                       } else {
+                               type->parameter_types = parameter_type;
+                       }
+                       last_parameter_type = parameter_type;
                        break;
+
                default:
                        return;
                }
@@ -841,55 +984,44 @@ void parse_parameters()
        }
 }
 
-static
-void parse_attributes(void)
-{
-       while(token.type == T___attribute__) {
-               next_token();
-               fprintf(stderr, "TODO: __attribute__ not handled yet\n");
-
-               expect_void('(');
-               int depth = 1;
-               while(depth > 0) {
-                       switch(token.type) {
-                       case T_EOF:
-                               parse_error("EOF while parsing attribute");
-                               break;
-                       case '(':
-                               next_token();
-                               depth++;
-                               break;
-                       case ')':
-                               next_token();
-                               depth--;
-                               break;
-                       default:
-                               next_token();
-                       }
-               }
-       }
-}
+typedef struct declarator_part declarator_part;
+struct declarator_part {
+       parsed_pointer_t *pointers;
+       method_type_t    *method_type;
+       declarator_part  *inner;
+};
+
 
 static
-void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
-                      type_t *type)
+declarator_part *parse_inner_declarator(declaration_t *declaration,
+                                        int may_be_abstract)
 {
-       type = parse_pointer(type);
-       declaration->storage_class = storage_class;
-       declaration->type          = type;
+       declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
+       memset(part, 0, sizeof(part[0]));
+
+       part->pointers = parse_pointers();
+
+       /* TODO: find out if this is correct */
+       parse_attributes();
 
        switch(token.type) {
        case T_IDENTIFIER:
-               declaration->symbol          = token.v.symbol;
-               declaration->source_position = token.source_position;
+               if(declaration == NULL) {
+                       parse_error("no identifier expected in typename");
+               } else {
+                       declaration->symbol          = token.v.symbol;
+                       declaration->source_position = token.source_position;
+               }
                next_token();
                break;
        case '(':
                next_token();
-               parse_declarator(declaration, storage_class, type);
-               expect_void(')');
+               part->inner = parse_inner_declarator(declaration, may_be_abstract);
+               expect(')');
                break;
        default:
+               if(may_be_abstract)
+                       break;
                parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
                                     '(', 0);
        }
@@ -899,17 +1031,15 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
                case '(':
                        next_token();
 
-                       int         top          = environment_top();
-                       const void *last_context = context;
-                       context                  = NULL;
+                       method_type_t *method_type
+                               = allocate_type_zero(sizeof(method_type[0]));
+                       method_type->type.type   = TYPE_METHOD;
 
-                       parse_parameters();
+                       parse_parameters(method_type);
 
-                       assert(context == NULL);
-                       context = last_context;
-                       environment_pop_to(top);
+                       part->method_type = method_type;
 
-                       expect_void(')');
+                       expect(')');
                        break;
                case '[':
                        next_token();
@@ -925,13 +1055,15 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
                                }
                        }
 
+                       /* TODO */
+
                        if(token.type == '*' && la(1)->type == ']') {
                                next_token();
                        } else if(token.type != ']') {
                                parse_assignment_expression();
                        }
 
-                       expect_void(']');
+                       expect(']');
                        break;
                default:
                        goto declarator_finished;
@@ -940,16 +1072,68 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
 
 declarator_finished:
        parse_attributes();
+
+       return part;
 }
 
 static
-void maybe_push_declaration(declaration_t *declaration)
+type_t *construct_declarator_type(declarator_part *part, type_t *type)
 {
-       fprintf(stderr, "Declarator '%s' type: ",
-                       declaration->symbol ? declaration->symbol->string : "");
-       print_type(stderr, declaration->type);
-       fprintf(stderr, "\n");
+       do {
+               type = make_pointers(type, part->pointers);
+
+               method_type_t *method_type = part->method_type;
+               if(method_type != NULL) {
+                       method_type->result_type = type;
+
+                       type = (type_t*) method_type;
+               }
+
+               part = part->inner;
+       } while(part != NULL);
 
+       return type;
+}
+
+static
+void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
+                      type_t *type, int may_be_abstract)
+{
+       declarator_part *part
+               = parse_inner_declarator(declaration, may_be_abstract);
+
+       if(part != NULL) {
+               declaration->type          = construct_declarator_type(part, type);
+               declaration->storage_class = storage_class;
+               obstack_free(&temp_obst, part);
+       }
+}
+
+static
+type_t *parse_abstract_declarator(type_t *base_type)
+{
+       declarator_part *part = parse_inner_declarator(NULL, 1);
+
+       type_t *result = construct_declarator_type(part, base_type);
+       obstack_free(&temp_obst, part);
+
+       return result;
+}
+
+static void record_declaration(declaration_t *declaration)
+{
+       if(last_declaration != NULL) {
+               last_declaration->next = declaration;
+       } else {
+               if(context != NULL)
+                       context->declarations = declaration;
+       }
+       last_declaration = declaration;
+}
+
+static
+void maybe_push_declaration(declaration_t *declaration)
+{
        symbol_t *symbol = declaration->symbol;
 
        if(symbol != NULL) {
@@ -964,8 +1148,9 @@ void parse_init_declarators(const declaration_specifiers_t *specifiers)
                declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
 
                parse_declarator(declaration, specifiers->storage_class,
-                                specifiers->type);
+                                specifiers->type, 0);
                maybe_push_declaration(declaration);
+               record_declaration(declaration);
                if(token.type == '=') {
                        next_token();
                        if(token.type == '{') {
@@ -975,7 +1160,8 @@ void parse_init_declarators(const declaration_specifiers_t *specifiers)
                                parse_assignment_expression();
                        }
                } else if(token.type == '{') {
-                       parse_compound_statement();
+                       statement_t *statement = parse_compound_statement();
+                       declaration->statement = statement;
                        return;
                }
 
@@ -990,24 +1176,22 @@ static
 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
 {
        while(1) {
-               declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
-
-               compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
-               entry->declaration = declaration;
-
                if(token.type == ':') {
                        next_token();
                        parse_constant_expression();
-                       /* TODO */
+                       /* TODO (bitfields) */
                } else {
+                       declaration_t *declaration
+                               = allocate_ast_zero(sizeof(declaration[0]));
                        parse_declarator(declaration, specifiers->storage_class,
-                                        specifiers->type);
+                                        specifiers->type, 0);
                        maybe_push_declaration(declaration);
+                       record_declaration(declaration);
 
                        if(token.type == ':') {
                                next_token();
                                parse_constant_expression();
-                               /* TODO */
+                               /* TODO (bitfields) */
                        }
                }
 
@@ -1018,12 +1202,10 @@ void parse_struct_declarators(const declaration_specifiers_t *specifiers)
        expect_void(';');
 }
 
-static compound_entry_t *parse_compound_type_entries(void)
+static void parse_compound_type_entries(void)
 {
        eat('{');
 
-       compound_entry_t *entries = NULL;
-
        while(token.type != '}' && token.type != T_EOF) {
                declaration_specifiers_t specifiers;
                memset(&specifiers, 0, sizeof(specifiers));
@@ -1033,9 +1215,10 @@ static compound_entry_t *parse_compound_type_entries(void)
 
                parse_struct_declarators(&specifiers);
        }
+       if(token.type == T_EOF) {
+               parse_error("unexpected error while parsing struct");
+       }
        next_token();
-
-       return entries;
 }
 
 void parse_declaration(void)
@@ -1055,12 +1238,17 @@ type_t *parse_typename(void)
 {
        declaration_specifiers_t specifiers;
        memset(&specifiers, 0, sizeof(specifiers));
-       /* TODO not correct storage class elements are not allowed here */
        parse_declaration_specifiers(&specifiers);
+       if(specifiers.storage_class != STORAGE_CLASS_NONE) {
+               /* TODO: improve error message, user does probably not know what a
+                * storage class is...
+                */
+               parse_error("typename may not have a storage class");
+       }
 
-       specifiers.type = parse_pointer(specifiers.type);
+       type_t *result = parse_abstract_declarator(specifiers.type);
 
-       return specifiers.type;
+       return result;
 }
 
 
@@ -1101,9 +1289,7 @@ expression_t *parse_string_const(void)
        string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
 
        cnst->expression.type = EXPR_STRING_LITERAL;
-       cnst->value           = token.v.string;
-
-       next_token();
+       cnst->value           = parse_string_literals();
 
        return (expression_t*) cnst;
 }
@@ -1134,11 +1320,44 @@ expression_t *parse_reference(void)
        return (expression_t*) ref;
 }
 
+static
+expression_t *parse_cast(void)
+{
+       unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
+
+       cast->expression.type            = EXPR_UNARY;
+       cast->type                       = UNEXPR_CAST;
+       cast->expression.source_position = token.source_position;
+
+       type_t *type  = parse_typename();
+
+       expect(')');
+       expression_t *value = parse_sub_expression(20);
+
+       cast->expression.datatype = type;
+       cast->value               = value;
+
+       return (expression_t*) cast;
+}
+
 static
 expression_t *parse_brace_expression(void)
 {
        eat('(');
 
+       declaration_t *declaration;
+       switch(token.type) {
+       TYPE_QUALIFIERS
+       TYPE_SPECIFIERS
+               return parse_cast();
+       case T_IDENTIFIER:
+               declaration = token.v.symbol->declaration;
+               if(declaration != NULL &&
+                               (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
+                       return parse_cast();
+               }
+       }
+
        expression_t *result = parse_expression();
        expect(')');
 
@@ -1278,6 +1497,23 @@ expression_t *parse_call_expression(unsigned precedence,
        return (expression_t*) call;
 }
 
+static
+expression_t *parse_conditional_expression(unsigned precedence,
+                                           expression_t *expression)
+{
+       eat('?');
+
+       conditional_expression_t *conditional
+               = allocate_ast_zero(sizeof(conditional[0]));
+       conditional->condition = expression;
+
+       conditional->true_expression = parse_expression();
+       expect(':');
+       conditional->false_expression = parse_sub_expression(precedence);
+
+       return (expression_t*) conditional;
+}
+
 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type)     \
 static                                                                    \
 expression_t *parse_##unexpression_type(unsigned precedence)              \
@@ -1355,6 +1591,8 @@ CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
+CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
+CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
 
@@ -1460,6 +1698,9 @@ void init_expression_parsers(void)
        register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&',        12);
        register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^',        11);
        register_expression_infix_parser(parse_BINEXPR_BITWISE_OR,  '|',        10);
+       register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND,    9);
+       register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR,  T_PIPEPIPE,  8);
+       register_expression_infix_parser(parse_conditional_expression, '?',      7);
        register_expression_infix_parser(parse_BINEXPR_ASSIGN,      T_EQUAL,     2);
 
        register_expression_infix_parser(parse_array_expression,        '[',    30);
@@ -1607,7 +1848,11 @@ statement_t *parse_continue(void)
        eat(T_continue);
        expect(';');
 
-       return NULL;
+       statement_t *statement     = allocate_ast_zero(sizeof(statement[0]));
+       statement->source_position = token.source_position;
+       statement->type            = STATEMENT_CONTINUE;
+
+       return statement;
 }
 
 static
@@ -1623,10 +1868,15 @@ static
 statement_t *parse_return(void)
 {
        eat(T_return);
-       parse_expression();
+
+       return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
+       statement->statement.type = STATEMENT_RETURN;
+       if(token.type != ';') {
+               statement->return_value = parse_expression();
+       }
        expect(';');
 
-       return NULL;
+       return (statement_t*) statement;
 }
 
 static
@@ -1736,21 +1986,30 @@ statement_t *parse_compound_statement(void)
                = allocate_ast_zero(sizeof(compound_statement[0]));
        compound_statement->statement.type = STATEMENT_COMPOUND;
 
-       int         top          = environment_top();
-       const void *last_context = context;
-       context                  = compound_statement;
+       int        top          = environment_top();
+       context_t *last_context = context;
+       set_context(&compound_statement->context);
 
-       while(token.type != '}') {
-               parse_statement();
+       statement_t *last_statement = NULL;
+
+       while(token.type != '}' && token.type != T_EOF) {
+               statement_t *statement = parse_statement();
+
+               if(last_statement != NULL) {
+                       last_statement->next = statement;
+               } else {
+                       compound_statement->statements = statement;
+               }
+               last_statement = statement;
        }
 
-       assert(context == compound_statement);
-       context = last_context;
+       assert(context == &compound_statement->context);
+       set_context(last_context);
        environment_pop_to(top);
 
        next_token();
 
-       return NULL;
+       return (statement_t*) compound_statement;
 }
 
 static
@@ -1758,15 +2017,17 @@ translation_unit_t *parse_translation_unit(void)
 {
        translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
 
-       assert(translation_unit == NULL);
        assert(context == NULL);
-       translation_unit = unit;
+       set_context(&unit->context);
 
        while(token.type != T_EOF) {
                parse_declaration();
        }
 
-       translation_unit = NULL;
+       assert(context == &unit->context);
+       context          = NULL;
+       last_declaration = NULL;
+
        return unit;
 }
 
@@ -1790,8 +2051,10 @@ translation_unit_t *parse(void)
 void init_parser(void)
 {
        init_expression_parsers();
+       obstack_init(&temp_obst);
 }
 
 void exit_parser(void)
 {
+       obstack_free(&temp_obst, NULL);
 }