X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ast.c;h=f2bc587276ce9cf94d27398a2826150593b4175c;hb=3e3bf65b33ece40f474c91dac37cf8ea716a5b03;hp=8f497ee8833edfbf30bed14fcdd64f3cfbbd8a30;hpb=e8b00fcb80ff3fdcfab757444e94663f1d56c743;p=cparser diff --git a/ast.c b/ast.c index 8f497ee..f2bc587 100644 --- a/ast.c +++ b/ast.c @@ -14,9 +14,14 @@ struct obstack ast_obstack; static FILE *out; static int indent; -static void print_expression(const expression_t *expression); 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) { @@ -97,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) { @@ -116,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: @@ -134,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); } @@ -148,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 @@ -165,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); } } @@ -198,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); @@ -229,7 +243,6 @@ void print_statement(const statement_t *statement) break; } - fprintf(out, "\n"); } #if 0 @@ -262,13 +275,30 @@ void print_method_parameters(const method_parameter_t *parameters, } #endif +static +void print_storage_class(storage_class_t storage_class) +{ + switch(storage_class) { + case STORAGE_CLASS_NONE: + break; + case STORAGE_CLASS_TYPEDEF: fputs("typedef ", out); break; + case STORAGE_CLASS_EXTERN: fputs("extern ", out); break; + case STORAGE_CLASS_STATIC: fputs("static ", out); break; + case STORAGE_CLASS_AUTO: fputs("auto ", out); break; + case STORAGE_CLASS_REGISTER: fputs("register ", out); break; + } +} + static void print_declaration(const declaration_t *declaration) { + print_storage_class(declaration->storage_class); print_type(declaration->type, declaration->symbol); - fprintf(out, "\n"); if(declaration->statement != NULL) { + fputs("\n", out); print_statement(declaration->statement); + } else { + fprintf(out, ";\n"); } }