eat() the ':' token of a label statement instead of expect()ing it, because the looka...
[cparser] / write_fluffy.c
index 1213383..3c870da 100644 (file)
@@ -37,14 +37,14 @@ static const char *get_atomic_type_string(const atomic_type_type_t type)
        }
 }
 
-static void write_atomic_type(const type_t *type)
+static void write_atomic_type(const atomic_type_t *type)
 {
-       fprintf(out, "%s", get_atomic_type_string(type->v.atomic_type.atype));
+       fprintf(out, "%s", get_atomic_type_string(type->atype));
 }
 
-static void write_pointer_type(const type_t *type)
+static void write_pointer_type(const pointer_type_t *type)
 {
-       write_type(type->v.pointer_type.points_to);
+       write_type(type->points_to);
        fputc('*', out);
 }
 
@@ -65,16 +65,16 @@ static declaration_t *find_typedef(const type_t *type)
        return declaration;
 }
 
-static void write_compound_type(const type_t *type)
+static void write_compound_type(const compound_type_t *type)
 {
-       declaration_t *declaration = find_typedef(type);
+       declaration_t *declaration = find_typedef((const type_t*) type);
        if(declaration != NULL) {
                fprintf(out, "%s", declaration->symbol->string);
                return;
        }
 
        /* does the struct have a name? */
-       symbol_t *symbol = type->v.compound_type.declaration->symbol;
+       symbol_t *symbol = type->declaration->symbol;
        if(symbol != NULL) {
                /* TODO: make sure we create a struct for it... */
                fprintf(out, "%s", symbol->string);
@@ -84,16 +84,16 @@ static void write_compound_type(const type_t *type)
        fprintf(out, "/* TODO anonymous struct */byte");
 }
 
-static void write_enum_type(const type_t *type)
+static void write_enum_type(const enum_type_t *type)
 {
-       declaration_t *declaration = find_typedef(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? */
-       symbol_t *symbol = type->v.enum_type.declaration->symbol;
+       symbol_t *symbol = type->declaration->symbol;
        if(symbol != NULL) {
                /* TODO: make sure we create an enum for it... */
                fprintf(out, "%s", symbol->string);
@@ -103,11 +103,11 @@ static void write_enum_type(const type_t *type)
        fprintf(out, "/* TODO anonymous enum */byte");
 }
 
-static void write_function_type(const type_t *type)
+static void write_function_type(const function_type_t *type)
 {
        fprintf(out, "(func(");
 
-       function_parameter_t *parameter = type->v.function_type.parameters;
+       function_parameter_t *parameter = type->parameters;
        int                   first     = 1;
        while(parameter != NULL) {
                if(!first) {
@@ -131,28 +131,28 @@ static void write_function_type(const type_t *type)
        }
 
        fprintf(out, ") : ");
-       write_type(type->v.function_type.result_type);
+       write_type(type->return_type);
        fprintf(out, ")");
 }
 
 static void write_type(const type_t *type)
 {
-       switch(type->type) {
+       switch(type->kind) {
        case TYPE_ATOMIC:
-               write_atomic_type(type);
+               write_atomic_type(&type->atomic);
                return;
        case TYPE_POINTER:
-               write_pointer_type(type);
+               write_pointer_type(&type->pointer);
                return;
        case TYPE_COMPOUND_UNION:
        case TYPE_COMPOUND_STRUCT:
-               write_compound_type(type);
+               write_compound_type(&type->compound);
                return;
        case TYPE_ENUM:
-               write_enum_type(type);
+               write_enum_type(&type->enumt);
                return;
        case TYPE_FUNCTION:
-               write_function_type(type);
+               write_function_type(&type->function);
                return;
        case TYPE_INVALID:
                panic("invalid type found");
@@ -170,11 +170,11 @@ static void write_struct_entry(const declaration_t *declaration)
        fprintf(out, "\n");
 }
 
-static void write_struct(const symbol_t *symbol, const type_t *type)
+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->v.compound_type.declaration->context.declarations;
+       const declaration_t *declaration = type->declaration->context.declarations;
        while(declaration != NULL) {
                write_struct_entry(declaration);
                declaration = declaration->next;
@@ -183,11 +183,11 @@ static void write_struct(const symbol_t *symbol, const type_t *type)
        fprintf(out, "\n");
 }
 
-static void write_union(const symbol_t *symbol, const type_t *type)
+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->v.compound_type.declaration->context.declarations;
+       const declaration_t *declaration = type->declaration->context.declarations;
        while(declaration != NULL) {
                write_struct_entry(declaration);
                declaration = declaration->next;
@@ -200,33 +200,33 @@ static void write_expression(const expression_t *expression);
 
 static void write_unary_expression(const unary_expression_t *expression)
 {
-       switch(expression->type) {
-       case UNEXPR_NEGATE:
+       switch(expression->expression.kind) {
+       case EXPR_UNARY_NEGATE:
                fputc('-', out);
                break;
-       case UNEXPR_NOT:
+       case EXPR_UNARY_NOT:
                fputc('!', out);
                break;
        default:
-               panic("unimplemented unary expression found");
+               panic("unimeplemented unary expression found");
        }
        write_expression(expression->value);
 }
 
 static void write_expression(const expression_t *expression)
 {
-       const const_t *constant;
+       const const_expression_t *constant;
        /* TODO */
-       switch(expression->type) {
+       switch(expression->kind) {
        case EXPR_CONST:
-               constant = (const const_t*) expression;
-               if(is_type_integer(expression->datatype)) {
+               constant = &expression->conste;
+               if(is_type_integer(expression->base.datatype)) {
                        fprintf(out, "%lld", constant->v.int_value);
                } else {
                        fprintf(out, "%Lf", constant->v.float_value);
                }
                break;
-       case EXPR_UNARY:
+       EXPR_UNARY_CASES
                write_unary_expression((const unary_expression_t*) expression);
                break;
        default:
@@ -234,11 +234,11 @@ static void write_expression(const expression_t *expression)
        }
 }
 
-static void write_enum(const symbol_t *symbol, const type_t *type)
+static void write_enum(const symbol_t *symbol, const enum_type_t *type)
 {
        fprintf(out, "enum %s:\n", symbol->string);
 
-       declaration_t *entry = type->v.enum_type.declaration->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);
@@ -270,7 +270,8 @@ static void write_function(const declaration_t *declaration)
        fprintf(out, "func extern %s(",
                declaration->symbol->string);
 
-       const type_t *function_type = declaration->type;
+       const function_type_t *function_type
+               = (const function_type_t*) declaration->type;
 
        declaration_t *parameter = declaration->context.declarations;
        int            first     = 1;
@@ -287,7 +288,7 @@ static void write_function(const declaration_t *declaration)
                }
                write_type(parameter->type);
        }
-       if(function_type->v.function_type.variadic) {
+       if(function_type->variadic) {
                if(!first) {
                        fprintf(out, ", ");
                } else {
@@ -297,30 +298,23 @@ static void write_function(const declaration_t *declaration)
        }
        fprintf(out, ")");
 
-       const type_t *result_type = function_type->v.function_type.result_type;
-       if(result_type->type != TYPE_ATOMIC ||
-          result_type->v.atomic_type.atype != ATOMIC_TYPE_VOID) {
+       const type_t *return_type = function_type->return_type;
+       if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
                fprintf(out, " : ");
-               write_type(result_type);
+               write_type(return_type);
        }
        fputc('\n', out);
 }
 
-void write_fluffy_decls(const translation_unit_t *unit)
+void write_fluffy_decls(FILE *output, const translation_unit_t *unit)
 {
-#if 0
-       out = fopen("out.fluffy", "w");
-       if(out == NULL) {
-               fprintf(stderr, "Couldn't open out.fluffy: %s\n", strerror(errno));
-               exit(1);
-       }
-#endif
-       out            = stdout;
+       out            = output;
        global_context = &unit->context;
 
+       ast_set_output(out);
        fprintf(out, "/* WARNING: Automatically generated file */\n");
 
-       /* write structs, unions + enums */
+       /* write structs,unions + enums */
        declaration_t *declaration = unit->context.declarations;
        for( ; declaration != NULL; declaration = declaration->next) {
                //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
@@ -328,12 +322,12 @@ void write_fluffy_decls(const translation_unit_t *unit)
                        continue;
                }
                type_t *type = declaration->type;
-               if(type->type == TYPE_COMPOUND_STRUCT) {
-                       write_struct(declaration->symbol, type);
-               } else if(type->type == TYPE_COMPOUND_UNION) {
-                       write_union(declaration->symbol, type);
-               } else if(type->type == TYPE_ENUM) {
-                       write_enum(declaration->symbol, type);
+               if(type->kind == TYPE_COMPOUND_STRUCT) {
+                       write_struct(declaration->symbol, &type->compound);
+               } else if(type->kind == TYPE_COMPOUND_UNION) {
+                       write_union(declaration->symbol, &type->compound);
+               } else if(type->kind == TYPE_ENUM) {
+                       write_enum(declaration->symbol, &type->enumt);
                }
        }
 
@@ -347,7 +341,7 @@ void write_fluffy_decls(const translation_unit_t *unit)
                        continue;
 
                type_t *type = declaration->type;
-               if(type->type == TYPE_FUNCTION)
+               if(type->kind == TYPE_FUNCTION)
                        continue;
 
                write_variable(declaration);
@@ -363,11 +357,9 @@ void write_fluffy_decls(const translation_unit_t *unit)
                        continue;
 
                type_t *type = declaration->type;
-               if(type->type != TYPE_FUNCTION)
+               if(type->kind != TYPE_FUNCTION)
                        continue;
 
                write_function(declaration);
        }
-
-       //fclose(out);
 }