12 static void write_type(const type_t *type);
14 static const char *get_atomic_type_string(const atomic_type_type_t type)
17 case ATOMIC_TYPE_VOID: return "void";
18 case ATOMIC_TYPE_CHAR: return "byte";
19 case ATOMIC_TYPE_SCHAR: return "byte";
20 case ATOMIC_TYPE_UCHAR: return "unsigned byte";
21 case ATOMIC_TYPE_SHORT: return "short";
22 case ATOMIC_TYPE_USHORT: return "unsigned short";
23 case ATOMIC_TYPE_INT: return "int";
24 case ATOMIC_TYPE_UINT: return "unsigned int";
25 case ATOMIC_TYPE_LONG: return "int";
26 case ATOMIC_TYPE_ULONG: return "unsigned int";
27 case ATOMIC_TYPE_LONGLONG: return "long";
28 case ATOMIC_TYPE_ULONGLONG: return "unsigned long";
29 case ATOMIC_TYPE_FLOAT: return "float";
30 case ATOMIC_TYPE_DOUBLE: return "double";
31 case ATOMIC_TYPE_BOOL: return "bool";
33 panic("unsupported atomic type");
37 static void write_atomic_type(const atomic_type_t *type)
39 fprintf(out, "%s", get_atomic_type_string(type->atype));
42 static void write_pointer_type(const pointer_type_t *type)
44 write_type(type->points_to);
48 static void write_type(const type_t *type)
52 write_atomic_type((const atomic_type_t*) type);
55 write_pointer_type((const pointer_type_t*) type);
58 panic("invalid type found");
61 fprintf(out, "/* TODO type */");
66 static void write_struct_entry(const declaration_t *declaration)
68 fprintf(out, "\t%s : ", declaration->symbol->string);
69 write_type(declaration->type);
73 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
76 fprintf(out, "struct %s:\n", symbol->string);
78 const declaration_t *declaration = type->context.declarations;
79 while(declaration != NULL) {
80 write_struct_entry(declaration);
81 declaration = declaration->next;
87 static void write_variable(const declaration_t *declaration)
89 fprintf(out, "var %s : ", declaration->symbol->string);
90 write_type(declaration->type);
91 /* TODO: initializers */
95 void write_fluffy_decls(const translation_unit_t *unit)
98 out = fopen("out.fluffy", "w");
100 fprintf(stderr, "Couldn't open out.fluffy: %s\n", strerror(errno));
106 fprintf(out, "/* WARNING: Automatically generated file */\n");
108 /* write structs + enums */
109 declaration_t *declaration = unit->context.declarations;
110 while(declaration != NULL) {
111 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
112 if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
113 declaration = declaration->next;
116 type_t *type = declaration->type;
117 if(type->type == TYPE_COMPOUND_STRUCT) {
118 write_struct(declaration->symbol, (compound_type_t*) type);
119 } else if(type->type == TYPE_COMPOUND_UNION) {
123 declaration = declaration->next;
126 /* write global variables */
127 declaration = unit->context.declarations;
128 while(declaration != NULL) {
129 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF) {
130 declaration = declaration->next;
134 type_t *type = declaration->type;
135 if(type->type != TYPE_METHOD) {
136 write_variable(declaration);
138 declaration = declaration->next;