X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=write_fluffy.c;h=e8ccbd78b4760e48f9bec2593fa526ff9e500236;hb=603715f1486af6096c5e4194b4c60ed2786c52f3;hp=651bcffee8f73ae5a606fe4b953e40852073bada;hpb=b25e6207470812cdca4e99650505b1b5de9e3046;p=cparser diff --git a/write_fluffy.c b/write_fluffy.c index 651bcff..e8ccbd7 100644 --- a/write_fluffy.c +++ b/write_fluffy.c @@ -3,8 +3,10 @@ #include #include +#include "write_fluffy.h" #include "ast_t.h" #include "type_t.h" +#include "type.h" #include "adt/error.h" static const context_t *global_context; @@ -72,9 +74,10 @@ static void write_compound_type(const compound_type_t *type) } /* does the struct have a name? */ - if(type->symbol != NULL) { + symbol_t *symbol = type->declaration->symbol; + if(symbol != NULL) { /* TODO: make sure we create a struct for it... */ - fprintf(out, "%s", type->symbol->string); + fprintf(out, "%s", symbol->string); return; } /* TODO: create a struct and use its name here... */ @@ -90,21 +93,22 @@ static void write_enum_type(const enum_type_t *type) } /* does the enum have a name? */ - if(type->symbol != NULL) { + symbol_t *symbol = type->declaration->symbol; + if(symbol != NULL) { /* TODO: make sure we create an enum for it... */ - fprintf(out, "%s", type->symbol->string); + fprintf(out, "%s", symbol->string); return; } /* TODO: create a struct and use its name here... */ fprintf(out, "/* TODO anonymous enum */byte"); } -static void write_method_type(const method_type_t *type) +static void write_function_type(const function_type_t *type) { fprintf(out, "(func("); - method_parameter_t *parameter = type->parameters; - int first = 1; + function_parameter_t *parameter = type->parameters; + int first = 1; while(parameter != NULL) { if(!first) { fprintf(out, ", "); @@ -147,8 +151,8 @@ static void write_type(const type_t *type) case TYPE_ENUM: write_enum_type((const enum_type_t*) type); return; - case TYPE_METHOD: - write_method_type((const method_type_t*) type); + case TYPE_FUNCTION: + write_function_type((const function_type_t*) type); return; case TYPE_INVALID: panic("invalid type found"); @@ -170,7 +174,7 @@ static void write_struct(const symbol_t *symbol, const compound_type_t *type) { fprintf(out, "struct %s:\n", symbol->string); - const declaration_t *declaration = type->context.declarations; + const declaration_t *declaration = type->declaration->context.declarations; while(declaration != NULL) { write_struct_entry(declaration); declaration = declaration->next; @@ -183,7 +187,7 @@ static void write_union(const symbol_t *symbol, const compound_type_t *type) { fprintf(out, "union %s:\n", symbol->string); - const declaration_t *declaration = type->context.declarations; + const declaration_t *declaration = type->declaration->context.declarations; while(declaration != NULL) { write_struct_entry(declaration); declaration = declaration->next; @@ -216,7 +220,11 @@ static void write_expression(const expression_t *expression) switch(expression->type) { case EXPR_CONST: constant = (const const_t*) expression; - fprintf(out, "%d", constant->value); + if(is_type_integer(expression->datatype)) { + fprintf(out, "%d", constant->v.int_value); + } else { + fprintf(out, "%Lf", constant->v.float_value); + } break; case EXPR_UNARY: write_unary_expression((const unary_expression_t*) expression); @@ -226,16 +234,26 @@ static void write_expression(const expression_t *expression) } } +static void write_initializer(const initializer_t *initializer) +{ + if(initializer->type != INITIALIZER_VALUE) { + panic("list initializers not supported yet"); + } + + write_expression(initializer->v.value); +} + static void write_enum(const symbol_t *symbol, const enum_type_t *type) { fprintf(out, "enum %s:\n", symbol->string); - declaration_t *entry = type->entries_begin; - for ( ; entry != type->entries_end->next; entry = entry->next) { + declaration_t *entry = type->declaration->next; + for ( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY; + entry = entry->next) { fprintf(out, "\t%s", entry->symbol->string); - if(entry->initializer != NULL) { + if(entry->init.initializer != NULL) { fprintf(out, " <- "); - write_expression(entry->initializer); + write_initializer(entry->init.initializer); } fputc('\n', out); } @@ -253,7 +271,7 @@ static void write_variable(const declaration_t *declaration) static void write_function(const declaration_t *declaration) { - if(declaration->statement != NULL) { + if(declaration->init.statement != NULL) { fprintf(stderr, "Warning: can't convert function bodies (at %s)\n", declaration->symbol->string); } @@ -261,7 +279,8 @@ static void write_function(const declaration_t *declaration) fprintf(out, "func extern %s(", declaration->symbol->string); - const method_type_t *method_type = (const method_type_t*) declaration->type; + const function_type_t *function_type + = (const function_type_t*) declaration->type; declaration_t *parameter = declaration->context.declarations; int first = 1; @@ -278,7 +297,7 @@ static void write_function(const declaration_t *declaration) } write_type(parameter->type); } - if(method_type->variadic) { + if(function_type->variadic) { if(!first) { fprintf(out, ", "); } else { @@ -288,7 +307,7 @@ static void write_function(const declaration_t *declaration) } fprintf(out, ")"); - const type_t *result_type = method_type->result_type; + const type_t *result_type = function_type->result_type; if(result_type->type != TYPE_ATOMIC || ((const atomic_type_t*) result_type)->atype != ATOMIC_TYPE_VOID) { fprintf(out, " : "); @@ -336,7 +355,7 @@ void write_fluffy_decls(const translation_unit_t *unit) continue; type_t *type = declaration->type; - if(type->type == TYPE_METHOD) + if(type->type == TYPE_FUNCTION) continue; write_variable(declaration); @@ -350,7 +369,7 @@ void write_fluffy_decls(const translation_unit_t *unit) continue; type_t *type = declaration->type; - if(type->type != TYPE_METHOD) + if(type->type != TYPE_FUNCTION) continue; write_function(declaration);