10 static const context_t *global_context;
13 static void write_type(const type_t *type);
15 static const char *get_atomic_type_string(const atomic_type_type_t type)
18 case ATOMIC_TYPE_VOID: return "void";
19 case ATOMIC_TYPE_CHAR: return "byte";
20 case ATOMIC_TYPE_SCHAR: return "byte";
21 case ATOMIC_TYPE_UCHAR: return "unsigned byte";
22 case ATOMIC_TYPE_SHORT: return "short";
23 case ATOMIC_TYPE_USHORT: return "unsigned short";
24 case ATOMIC_TYPE_INT: return "int";
25 case ATOMIC_TYPE_UINT: return "unsigned int";
26 case ATOMIC_TYPE_LONG: return "int";
27 case ATOMIC_TYPE_ULONG: return "unsigned int";
28 case ATOMIC_TYPE_LONGLONG: return "long";
29 case ATOMIC_TYPE_ULONGLONG: return "unsigned long";
30 case ATOMIC_TYPE_FLOAT: return "float";
31 case ATOMIC_TYPE_DOUBLE: return "double";
32 case ATOMIC_TYPE_LONG_DOUBLE: return "double";
33 case ATOMIC_TYPE_BOOL: return "bool";
34 default: panic("unsupported atomic type");
38 static void write_atomic_type(const atomic_type_t *type)
40 fprintf(out, "%s", get_atomic_type_string(type->atype));
43 static void write_pointer_type(const pointer_type_t *type)
45 write_type(type->points_to);
49 static void write_compound_type(const compound_type_t *type)
51 /* first: search for a matching typedef in the global type... */
52 declaration_t *declaration = global_context->declarations;
53 while(declaration != NULL) {
54 if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
55 declaration = declaration->next;
58 if(declaration->type == (type_t*) type)
60 declaration = declaration->next;
63 if(declaration != NULL) {
64 fprintf(out, "%s", declaration->symbol->string);
68 /* does the struct have a name? */
69 if(type->symbol != NULL) {
70 /* TODO: make sure we create a struct for it... */
71 fprintf(out, "%s", type->symbol->string);
74 /* TODO: create a struct and use its name here... */
75 fprintf(out, "/* TODO anonymous struct */byte");
78 static void write_method_type(const method_type_t *type)
80 fprintf(out, "(func(");
82 method_parameter_type_t *parameter_type = type->parameter_types;
84 while(parameter_type != NULL) {
91 if(parameter_type->symbol != NULL) {
92 fprintf(out, "%s : ", parameter_type->symbol->string);
94 /* TODO make up some unused names (or allow _ in fluffy?) */
97 write_type(parameter_type->type);
99 parameter_type = parameter_type->next;
102 fprintf(out, ") : ");
103 write_type(type->result_type);
107 static void write_type(const type_t *type)
111 write_atomic_type((const atomic_type_t*) type);
114 write_pointer_type((const pointer_type_t*) type);
116 case TYPE_COMPOUND_UNION:
117 case TYPE_COMPOUND_STRUCT:
118 write_compound_type((const compound_type_t*) type);
121 write_method_type((const method_type_t*) type);
124 panic("invalid type found");
127 fprintf(out, "/* TODO type */");
132 static void write_struct_entry(const declaration_t *declaration)
134 fprintf(out, "\t%s : ", declaration->symbol->string);
135 write_type(declaration->type);
139 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
142 fprintf(out, "struct %s:\n", symbol->string);
144 const declaration_t *declaration = type->context.declarations;
145 while(declaration != NULL) {
146 write_struct_entry(declaration);
147 declaration = declaration->next;
153 static void write_variable(const declaration_t *declaration)
155 fprintf(out, "var %s : ", declaration->symbol->string);
156 write_type(declaration->type);
157 /* TODO: initializers */
161 static void write_function(const declaration_t *declaration)
163 if(declaration->statement != NULL) {
164 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
165 declaration->symbol->string);
168 fprintf(out, "func extern %s(",
169 declaration->symbol->string);
171 declaration_t *parameter = declaration->context.declarations;
173 for( ; parameter != NULL; parameter = parameter->next) {
179 if(parameter->symbol != NULL) {
180 fprintf(out, "%s : ", parameter->symbol->string);
184 write_type(parameter->type);
188 const method_type_t *method_type = (const method_type_t*) declaration->type;
189 const type_t *result_type = method_type->result_type;
190 if(result_type->type != TYPE_ATOMIC ||
191 ((const atomic_type_t*) result_type)->atype != ATOMIC_TYPE_VOID) {
193 write_type(result_type);
198 void write_fluffy_decls(const translation_unit_t *unit)
201 out = fopen("out.fluffy", "w");
203 fprintf(stderr, "Couldn't open out.fluffy: %s\n", strerror(errno));
208 global_context = &unit->context;
210 fprintf(out, "/* WARNING: Automatically generated file */\n");
212 /* write structs + enums */
213 declaration_t *declaration = unit->context.declarations;
214 while(declaration != NULL) {
215 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
216 if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
217 declaration = declaration->next;
220 type_t *type = declaration->type;
221 if(type->type == TYPE_COMPOUND_STRUCT) {
222 write_struct(declaration->symbol, (compound_type_t*) type);
223 } else if(type->type == TYPE_COMPOUND_UNION) {
227 declaration = declaration->next;
230 /* write global variables */
231 declaration = unit->context.declarations;
232 for( ; declaration != NULL; declaration = declaration->next) {
233 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF)
236 type_t *type = declaration->type;
237 if(type->type == TYPE_METHOD)
240 write_variable(declaration);
243 /* write functions */
244 declaration = unit->context.declarations;
245 for( ; declaration != NULL; declaration = declaration->next) {
246 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF)
249 type_t *type = declaration->type;
250 if(type->type != TYPE_METHOD)
253 write_function(declaration);