improvements in statement parsing, improvements in ast printing
authorMatthias Braun <matze@braunis.de>
Fri, 14 Sep 2007 21:44:52 +0000 (21:44 +0000)
committerMatthias Braun <matze@braunis.de>
Fri, 14 Sep 2007 21:44:52 +0000 (21:44 +0000)
[r18352]

ast.c
parser.c

diff --git a/ast.c b/ast.c
index 81cd3ca..f2bc587 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -16,6 +16,12 @@ static int   indent;
 
 static void print_statement(const statement_t *statement);
 
+static void print_indent(void)
+{
+       for(int i = 0; i < indent; ++i)
+               fprintf(out, "\t");
+}
+
 static
 void print_const(const const_t *cnst)
 {
@@ -96,6 +102,11 @@ void print_binary_expression(const binary_expression_t *binexpr)
        fprintf(out, ")");
 }
 
+static void print_reference_expression(const reference_expression_t *ref)
+{
+       fprintf(out, "%s", ref->declaration->symbol->string);
+}
+
 void print_expression(const expression_t *expression)
 {
        switch(expression->type) {
@@ -115,6 +126,8 @@ void print_expression(const expression_t *expression)
                print_binary_expression((const binary_expression_t*) expression);
                break;
        case EXPR_REFERENCE:
+               print_reference_expression((const reference_expression_t*) expression);
+               break;
        case EXPR_UNARY:
        case EXPR_SELECT:
        case EXPR_ARRAY_ACCESS:
@@ -133,11 +146,13 @@ void print_compound_statement(const compound_statement_t *block)
 
        statement_t *statement = block->statements;
        while(statement != NULL) {
+               print_indent();
                print_statement(statement);
 
                statement = statement->next;
        }
        indent--;
+       print_indent();
        fputs("}\n", out);
 }
 
@@ -147,6 +162,7 @@ void print_return_statement(const return_statement_t *statement)
        fprintf(out, "return ");
        if(statement->return_value != NULL)
                print_expression(statement->return_value);
+       fputs(";\n", out);
 }
 
 static
@@ -164,26 +180,28 @@ void print_goto_statement(const goto_statement_t *statement)
        } else {
                fprintf(out, "?%s", statement->label_symbol->string);
        }
+       fputs(";\n", out);
 }
 
 static
 void print_label_statement(const label_statement_t *statement)
 {
-       fprintf(out, ":%s", statement->symbol->string);
+       fprintf(out, "%s:\n", statement->symbol->string);
 }
 
 static
 void print_if_statement(const if_statement_t *statement)
 {
-       fprintf(out, "if ");
+       fprintf(out, "if(");
        print_expression(statement->condition);
-       fprintf(out, ":\n");
+       fprintf(out, "");
        if(statement->true_statement != NULL) {
                print_statement(statement->true_statement);
        }
 
        if(statement->false_statement != NULL) {
-               fprintf(out, "else:\n");
+               print_indent();
+               fprintf(out, "else ");
                print_statement(statement->false_statement);
        }
 }
@@ -197,9 +215,6 @@ void print_declaration_statement(const declaration_statement_t *statement)
 
 void print_statement(const statement_t *statement)
 {
-       for(int i = 0; i < indent; ++i)
-               fprintf(out, "\t");
-
        switch(statement->type) {
        case STATEMENT_COMPOUND:
                print_compound_statement((const compound_statement_t*) statement);
@@ -228,7 +243,6 @@ void print_statement(const statement_t *statement)
                break;
 
        }
-       fprintf(out, "\n");
 }
 
 #if 0
@@ -281,7 +295,7 @@ void print_declaration(const declaration_t *declaration)
        print_storage_class(declaration->storage_class);
        print_type(declaration->type, declaration->symbol);
        if(declaration->statement != NULL) {
-               fprintf(out, "\n");
+               fputs("\n", out);
                print_statement(declaration->statement);
        } else {
                fprintf(out, ";\n");
index 19679a2..31d692b 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -85,7 +85,7 @@ void next_token(void)
 }
 
 static inline
-const token_t *la(int num)
+const token_t *look_ahead(int num)
 {
        assert(num > 0 && num <= MAX_LOOKAHEAD);
        int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
@@ -152,21 +152,60 @@ void parse_error_expected(const char *message, ...)
        fprintf(stderr, "\n");
 }
 
-static
-void eat_until(int token_type)
+static void eat_block(void)
 {
-       while(token.type != token_type) {
+       if(token.type == '{')
+               next_token();
+
+       while(token.type != '}') {
                if(token.type == T_EOF)
                        return;
+               if(token.type == '{') {
+                       eat_block();
+                       continue;
+               }
                next_token();
        }
-       next_token();
+       eat('}');
+}
+
+static void eat_statement(void)
+{
+       while(token.type != ';') {
+               if(token.type == T_EOF)
+                       return;
+               if(token.type == '}')
+                       return;
+               if(token.type == '{') {
+                       eat_block();
+                       continue;
+               }
+               next_token();
+       }
+       eat(';');
+}
+
+static void eat_brace(void)
+{
+       if(token.type == '(')
+               next_token();
+
+       while(token.type != ')') {
+               if(token.type == T_EOF)
+                       return;
+               if(token.type == '{') {
+                       eat_block();
+                       continue;
+               }
+               next_token();
+       }
+       eat(')');
 }
 
 #define expect(expected)                           \
     if(UNLIKELY(token.type != (expected))) {       \
         parse_error_expected(NULL, (expected), 0); \
-        eat_until(';');                            \
+        eat_statement();                           \
         return NULL;                               \
     }                                              \
     next_token();
@@ -174,7 +213,7 @@ void eat_until(int token_type)
 #define expect_void(expected)                      \
     if(UNLIKELY(token.type != (expected))) {       \
         parse_error_expected(NULL, (expected), 0); \
-        eat_until(';');                            \
+        eat_statement();                           \
         return;                                    \
     }                                              \
     next_token();
@@ -409,7 +448,7 @@ static enum_entry_t *parse_enum_type_entries(void)
                if(token.type != T_IDENTIFIER) {
                        parse_error_expected("problem while parsing enum entry",
                                             T_IDENTIFIER, 0);
-                       eat_until('}');
+                       eat_block();
                        return result;
                }
                entry->symbol = token.v.symbol;
@@ -514,7 +553,7 @@ void parse_attributes(void)
                        if(token.type != T_STRING_LITERAL) {
                                parse_error_expected("while parsing assembler attribute",
                                                     T_STRING_LITERAL);
-                               eat_until(')');
+                               eat_brace();
                                break;
                        } else {
                                parse_string_literals();
@@ -985,7 +1024,7 @@ declaration_t *parse_parameters(method_type_t *type)
                type->unspecified_parameters = 1;
                return NULL;
        }
-       if(token.type == T_void && la(1)->type == ')') {
+       if(token.type == T_void && look_ahead(1)->type == ')') {
                next_token();
                return NULL;
        }
@@ -1081,7 +1120,10 @@ declarator_part *parse_inner_declarator(declaration_t *declaration,
                                = allocate_type_zero(sizeof(method_type[0]));
                        method_type->type.type   = TYPE_METHOD;
 
-                       declaration->context.declarations = parse_parameters(method_type);
+                       declaration_t *parameters = parse_parameters(method_type);
+                       if(declaration != NULL) {
+                               declaration->context.declarations = parameters;
+                       }
 
                        part->method_type = method_type;
 
@@ -1103,7 +1145,7 @@ declarator_part *parse_inner_declarator(declaration_t *declaration,
 
                        /* TODO */
 
-                       if(token.type == '*' && la(1)->type == ']') {
+                       if(token.type == '*' && look_ahead(1)->type == ']') {
                                next_token();
                        } else if(token.type != ']') {
                                parse_assignment_expression();
@@ -1164,6 +1206,9 @@ type_t *parse_abstract_declarator(type_t *base_type)
 {
        declarator_part *part = parse_inner_declarator(NULL, 1);
 
+       if(part == NULL)
+               return NULL;
+
        type_t *result = construct_declarator_type(part, base_type);
        obstack_free(&temp_obst, part);
 
@@ -1241,7 +1286,22 @@ void parse_init_declarators(const declaration_specifiers_t *specifiers)
                                parser_error_multiple_definition(declaration, ndeclaration);
                        }
 
+                       int         top          = environment_top();
+                       context_t  *last_context = context;
+                       set_context(&declaration->context);
+
+                       /* push function parameters */
+                       declaration_t *parameter = declaration->context.declarations;
+                       for( ; parameter != NULL; parameter = parameter->next) {
+                               environment_push(parameter, context);
+                       }
+
                        statement_t *statement = parse_compound_statement();
+
+                       assert(context == &declaration->context);
+                       set_context(last_context);
+                       environment_pop_to(top);
+
                        declaration->statement = statement;
                        return;
                }
@@ -1265,7 +1325,7 @@ void parse_struct_declarators(const declaration_specifiers_t *specifiers)
                        declaration_t *declaration
                                = allocate_ast_zero(sizeof(declaration[0]));
                        parse_declarator(declaration, specifiers->storage_class,
-                                        specifiers->type, 0);
+                                        specifiers->type, 1);
 
                        /* TODO: check for doubled fields */
                        record_declaration(declaration);
@@ -1394,8 +1454,14 @@ expression_t *parse_reference(void)
 {
        reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
 
-       ref->expression.type            = EXPR_REFERENCE;
-       ref->symbol                     = token.v.symbol;
+       ref->expression.type = EXPR_REFERENCE;
+       ref->symbol          = token.v.symbol;
+
+       if(ref->symbol->declaration == NULL) {
+               parser_print_error_prefix();
+               fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
+       }
+       ref->declaration     = ref->symbol->declaration;
 
        next_token();
 
@@ -1460,7 +1526,11 @@ expression_t *parse_primary_expression(void)
                return parse_brace_expression();
        }
 
-       /* TODO: error message */
+       parser_print_error_prefix();
+       fprintf(stderr, "unexpected token ");
+       print_token(stderr, &token);
+       fprintf(stderr, "\n");
+       eat_statement();
        return NULL;
 }
 
@@ -1496,6 +1566,26 @@ type_t *get_expression_type(const expression_t *expression)
        return NULL;
 }
 
+static
+int is_type_specifier(const token_t *token)
+{
+       declaration_t *declaration;
+
+       switch(token->type) {
+               TYPE_SPECIFIERS
+                       return 1;
+               case T_IDENTIFIER:
+                       declaration = token->v.symbol->declaration;
+                       if(declaration == NULL)
+                               return 0;
+                       if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
+                               return 0;
+                       return 1;
+               default:
+                       return 0;
+       }
+}
+
 static
 expression_t *parse_sizeof(unsigned precedence)
 {
@@ -1505,7 +1595,7 @@ expression_t *parse_sizeof(unsigned precedence)
                = allocate_ast_zero(sizeof(sizeof_expression[0]));
        sizeof_expression->expression.type = EXPR_SIZEOF;
 
-       if(token.type == '(' /* && LA1 is type_specifier */) {
+       if(token.type == '(' && is_type_specifier(look_ahead(1))) {
                next_token();
                sizeof_expression->type = parse_typename();
                expect(')');
@@ -1667,7 +1757,7 @@ CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
-CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
+CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
@@ -1841,17 +1931,21 @@ static
 statement_t *parse_if(void)
 {
        eat(T_if);
+
+       if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
+       statement->statement.type = STATEMENT_IF;
+
        expect('(');
-       parse_expression();
+       statement->condition = parse_expression();
        expect(')');
 
-       parse_statement();
+       statement->true_statement = parse_statement();
        if(token.type == T_else) {
                next_token();
-               parse_statement();
+               statement->false_statement = parse_statement();
        }
 
-       return NULL;
+       return (statement_t*) statement;
 }
 
 static
@@ -2032,11 +2126,12 @@ statement_t *parse_statement(void)
                break;
 
        case ';':
+               next_token();
                statement = NULL;
                break;
 
        case T_IDENTIFIER:
-               if(la(1)->type == ':') {
+               if(look_ahead(1)->type == ':') {
                        statement = parse_label_statement();
                        break;
                }
@@ -2054,6 +2149,10 @@ statement_t *parse_statement(void)
        DECLARATION_START
                statement = parse_declaration_statement();
                break;
+
+       default:
+               statement = parse_expression_statement();
+               break;
        }
 
        return statement;