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)
{
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) {
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:
statement_t *statement = block->statements;
while(statement != NULL) {
+ print_indent();
print_statement(statement);
statement = statement->next;
}
indent--;
+ print_indent();
fputs("}\n", out);
}
fprintf(out, "return ");
if(statement->return_value != NULL)
print_expression(statement->return_value);
+ fputs(";\n", out);
}
static
} 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);
}
}
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);
break;
}
- fprintf(out, "\n");
}
#if 0
}
#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");
}
}