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)
141 fprintf(out, "struct %s:\n", symbol->string);
143 const declaration_t *declaration = type->context.declarations;
144 while(declaration != NULL) {
145 write_struct_entry(declaration);
146 declaration = declaration->next;
152 static void write_union(const symbol_t *symbol, const compound_type_t *type)
154 fprintf(out, "union %s:\n", symbol->string);
156 const declaration_t *declaration = type->context.declarations;
157 while(declaration != NULL) {
158 write_struct_entry(declaration);
159 declaration = declaration->next;
165 static void write_expression(const expression_t *expression);
167 static void write_unary_expression(const unary_expression_t *expression)
169 switch(expression->type) {
177 panic("unimeplemented unary expression found");
179 write_expression(expression->value);
182 static void write_expression(const expression_t *expression)
184 const const_t *constant;
186 switch(expression->type) {
188 constant = (const const_t*) expression;
189 fprintf(out, "%d", constant->value);
192 write_unary_expression((const unary_expression_t*) expression);
195 panic("not implemented expression");
199 static void write_enum(const symbol_t *symbol, const enum_type_t *type)
201 fprintf(out, "enum %s:\n", symbol->string);
203 const enum_entry_t *entry = type->entries;
204 for ( ; entry != NULL; entry = entry->next) {
205 fprintf(out, "\t%s", entry->symbol->string);
206 if(entry->value != NULL) {
207 fprintf(out, " <- ");
208 write_expression(entry->value);
215 static void write_variable(const declaration_t *declaration)
217 fprintf(out, "var %s : ", declaration->symbol->string);
218 write_type(declaration->type);
219 /* TODO: initializers */
223 static void write_function(const declaration_t *declaration)
225 if(declaration->statement != NULL) {
226 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
227 declaration->symbol->string);
230 fprintf(out, "func extern %s(",
231 declaration->symbol->string);
233 declaration_t *parameter = declaration->context.declarations;
235 for( ; parameter != NULL; parameter = parameter->next) {
241 if(parameter->symbol != NULL) {
242 fprintf(out, "%s : ", parameter->symbol->string);
246 write_type(parameter->type);
250 const method_type_t *method_type = (const method_type_t*) declaration->type;
251 const type_t *result_type = method_type->result_type;
252 if(result_type->type != TYPE_ATOMIC ||
253 ((const atomic_type_t*) result_type)->atype != ATOMIC_TYPE_VOID) {
255 write_type(result_type);
260 void write_fluffy_decls(const translation_unit_t *unit)
263 out = fopen("out.fluffy", "w");
265 fprintf(stderr, "Couldn't open out.fluffy: %s\n", strerror(errno));
270 global_context = &unit->context;
272 fprintf(out, "/* WARNING: Automatically generated file */\n");
274 /* write structs,unions + enums */
275 declaration_t *declaration = unit->context.declarations;
276 while(declaration != NULL) {
277 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
278 if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
279 declaration = declaration->next;
282 type_t *type = declaration->type;
283 if(type->type == TYPE_COMPOUND_STRUCT) {
284 write_struct(declaration->symbol, (compound_type_t*) type);
285 } else if(type->type == TYPE_COMPOUND_UNION) {
286 write_union(declaration->symbol, (compound_type_t*) type);
287 } else if(type->type == TYPE_ENUM) {
288 write_enum(declaration->symbol, (enum_type_t*) type);
291 declaration = declaration->next;
294 /* write global variables */
295 declaration = unit->context.declarations;
296 for( ; declaration != NULL; declaration = declaration->next) {
297 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF)
300 type_t *type = declaration->type;
301 if(type->type == TYPE_METHOD)
304 write_variable(declaration);
307 /* write functions */
308 declaration = unit->context.declarations;
309 for( ; declaration != NULL; declaration = declaration->next) {
310 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF)
313 type_t *type = declaration->type;
314 if(type->type != TYPE_METHOD)
317 write_function(declaration);