X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=write_fluffy.c;h=e9db530f8029657ba162c7c1c4a6847f75b7a8ea;hb=4eade33d45008f90c8952a65a92cdda17d9598f9;hp=62582c9121bc92c0776986e91a589e1b8f9ea75b;hpb=d1d999b6563a06706be2500a02217986c54ec13f;p=cparser diff --git a/write_fluffy.c b/write_fluffy.c index 62582c9..e9db530 100644 --- a/write_fluffy.c +++ b/write_fluffy.c @@ -5,6 +5,7 @@ #include "ast_t.h" #include "type_t.h" +#include "type.h" #include "adt/error.h" static const context_t *global_context; @@ -46,20 +47,26 @@ static void write_pointer_type(const pointer_type_t *type) fputc('*', out); } -static void write_compound_type(const compound_type_t *type) +static declaration_t *find_typedef(const type_t *type) { /* first: search for a matching typedef in the global type... */ declaration_t *declaration = global_context->declarations; while(declaration != NULL) { - if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) { + if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) { declaration = declaration->next; continue; } - if(declaration->type == (type_t*) type) + if(declaration->type == type) break; declaration = declaration->next; } + return declaration; +} + +static void write_compound_type(const compound_type_t *type) +{ + declaration_t *declaration = find_typedef((const type_t*) type); if(declaration != NULL) { fprintf(out, "%s", declaration->symbol->string); return; @@ -75,28 +82,49 @@ static void write_compound_type(const compound_type_t *type) fprintf(out, "/* TODO anonymous struct */byte"); } +static void write_enum_type(const enum_type_t *type) +{ + declaration_t *declaration = find_typedef((const type_t*) type); + if(declaration != NULL) { + fprintf(out, "%s", declaration->symbol->string); + return; + } + + /* does the enum have a name? */ + if(type->symbol != NULL) { + /* TODO: make sure we create an enum for it... */ + fprintf(out, "%s", type->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) { fprintf(out, "(func("); - method_parameter_type_t *parameter_type = type->parameter_types; - int first = 1; - while(parameter_type != NULL) { + method_parameter_t *parameter = type->parameters; + int first = 1; + while(parameter != NULL) { if(!first) { fprintf(out, ", "); } else { first = 0; } - if(parameter_type->symbol != NULL) { - fprintf(out, "%s : ", parameter_type->symbol->string); +#if 0 + if(parameter->symbol != NULL) { + fprintf(out, "%s : ", parameter->symbol->string); } else { /* TODO make up some unused names (or allow _ in fluffy?) */ fprintf(out, "_ : "); } - write_type(parameter_type->type); +#endif + fputs("_ : ", out); + write_type(parameter->type); - parameter_type = parameter_type->next; + parameter = parameter->next; } fprintf(out, ") : "); @@ -117,6 +145,9 @@ static void write_type(const type_t *type) case TYPE_COMPOUND_STRUCT: write_compound_type((const compound_type_t*) type); return; + case TYPE_ENUM: + write_enum_type((const enum_type_t*) type); + return; case TYPE_METHOD: write_method_type((const method_type_t*) type); return; @@ -186,7 +217,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); @@ -200,15 +235,16 @@ static void write_enum(const symbol_t *symbol, const enum_type_t *type) { fprintf(out, "enum %s:\n", symbol->string); - const enum_entry_t *entry = type->entries; - for ( ; entry != NULL; entry = entry->next) { + declaration_t *entry = type->entries_begin; + for ( ; entry != type->entries_end->next; entry = entry->next) { fprintf(out, "\t%s", entry->symbol->string); - if(entry->value != NULL) { + if(entry->initializer != NULL) { fprintf(out, " <- "); - write_expression(entry->value); + write_expression(entry->initializer); } fputc('\n', out); } + fprintf(out, "typealias %s <- int\n", symbol->string); fprintf(out, "\n"); } @@ -230,6 +266,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; + declaration_t *parameter = declaration->context.declarations; int first = 1; for( ; parameter != NULL; parameter = parameter->next) { @@ -245,9 +283,16 @@ static void write_function(const declaration_t *declaration) } write_type(parameter->type); } + if(method_type->variadic) { + if(!first) { + fprintf(out, ", "); + } else { + first = 0; + } + fputs("...", out); + } fprintf(out, ")"); - const method_type_t *method_type = (const method_type_t*) declaration->type; const type_t *result_type = method_type->result_type; if(result_type->type != TYPE_ATOMIC || ((const atomic_type_t*) result_type)->atype != ATOMIC_TYPE_VOID) { @@ -273,10 +318,9 @@ void write_fluffy_decls(const translation_unit_t *unit) /* write structs,unions + enums */ declaration_t *declaration = unit->context.declarations; - while(declaration != NULL) { + for( ; declaration != NULL; declaration = declaration->next) { //fprintf(out, "// Decl: %s\n", declaration->symbol->string); - if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) { - declaration = declaration->next; + if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) { continue; } type_t *type = declaration->type; @@ -287,14 +331,13 @@ void write_fluffy_decls(const translation_unit_t *unit) } else if(type->type == TYPE_ENUM) { write_enum(declaration->symbol, (enum_type_t*) type); } - - declaration = declaration->next; } /* write global variables */ declaration = unit->context.declarations; for( ; declaration != NULL; declaration = declaration->next) { - if(declaration->storage_class & STORAGE_CLASS_TYPEDEF) + if(declaration->storage_class == STORAGE_CLASS_TYPEDEF + || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY) continue; type_t *type = declaration->type; @@ -307,7 +350,8 @@ void write_fluffy_decls(const translation_unit_t *unit) /* write functions */ declaration = unit->context.declarations; for( ; declaration != NULL; declaration = declaration->next) { - if(declaration->storage_class & STORAGE_CLASS_TYPEDEF) + if(declaration->storage_class == STORAGE_CLASS_TYPEDEF + || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY) continue; type_t *type = declaration->type;