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 declaration_t *find_typedef(const 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)
60 declaration = declaration->next;
66 static void write_compound_type(const compound_type_t *type)
68 declaration_t *declaration = find_typedef((const type_t*) type);
69 if(declaration != NULL) {
70 fprintf(out, "%s", declaration->symbol->string);
74 /* does the struct have a name? */
75 if(type->symbol != NULL) {
76 /* TODO: make sure we create a struct for it... */
77 fprintf(out, "%s", type->symbol->string);
80 /* TODO: create a struct and use its name here... */
81 fprintf(out, "/* TODO anonymous struct */byte");
84 static void write_enum_type(const enum_type_t *type)
86 declaration_t *declaration = find_typedef((const type_t*) type);
87 if(declaration != NULL) {
88 fprintf(out, "%s", declaration->symbol->string);
92 /* does the enum have a name? */
93 if(type->symbol != NULL) {
94 /* TODO: make sure we create an enum for it... */
95 fprintf(out, "%s", type->symbol->string);
98 /* TODO: create a struct and use its name here... */
99 fprintf(out, "/* TODO anonymous enum */byte");
102 static void write_method_type(const method_type_t *type)
104 fprintf(out, "(func(");
106 declaration_t *parameter = type->parameters;
108 while(parameter != NULL) {
115 if(parameter->symbol != NULL) {
116 fprintf(out, "%s : ", parameter->symbol->string);
118 /* TODO make up some unused names (or allow _ in fluffy?) */
119 fprintf(out, "_ : ");
121 write_type(parameter->type);
123 parameter = parameter->next;
126 fprintf(out, ") : ");
127 write_type(type->result_type);
131 static void write_type(const type_t *type)
135 write_atomic_type((const atomic_type_t*) type);
138 write_pointer_type((const pointer_type_t*) type);
140 case TYPE_COMPOUND_UNION:
141 case TYPE_COMPOUND_STRUCT:
142 write_compound_type((const compound_type_t*) type);
145 write_enum_type((const enum_type_t*) type);
148 write_method_type((const method_type_t*) type);
151 panic("invalid type found");
154 fprintf(out, "/* TODO type */");
159 static void write_struct_entry(const declaration_t *declaration)
161 fprintf(out, "\t%s : ", declaration->symbol->string);
162 write_type(declaration->type);
166 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
168 fprintf(out, "struct %s:\n", symbol->string);
170 const declaration_t *declaration = type->context.declarations;
171 while(declaration != NULL) {
172 write_struct_entry(declaration);
173 declaration = declaration->next;
179 static void write_union(const symbol_t *symbol, const compound_type_t *type)
181 fprintf(out, "union %s:\n", symbol->string);
183 const declaration_t *declaration = type->context.declarations;
184 while(declaration != NULL) {
185 write_struct_entry(declaration);
186 declaration = declaration->next;
192 static void write_expression(const expression_t *expression);
194 static void write_unary_expression(const unary_expression_t *expression)
196 switch(expression->type) {
204 panic("unimeplemented unary expression found");
206 write_expression(expression->value);
209 static void write_expression(const expression_t *expression)
211 const const_t *constant;
213 switch(expression->type) {
215 constant = (const const_t*) expression;
216 fprintf(out, "%d", constant->value);
219 write_unary_expression((const unary_expression_t*) expression);
222 panic("not implemented expression");
226 static void write_enum(const symbol_t *symbol, const enum_type_t *type)
228 fprintf(out, "enum %s:\n", symbol->string);
230 const enum_entry_t *entry = type->entries;
231 for ( ; entry != NULL; entry = entry->next) {
232 fprintf(out, "\t%s", entry->symbol->string);
233 if(entry->value != NULL) {
234 fprintf(out, " <- ");
235 write_expression(entry->value);
239 fprintf(out, "typealias %s <- int\n", symbol->string);
243 static void write_variable(const declaration_t *declaration)
245 fprintf(out, "var %s : ", declaration->symbol->string);
246 write_type(declaration->type);
247 /* TODO: initializers */
251 static void write_function(const declaration_t *declaration)
253 if(declaration->statement != NULL) {
254 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
255 declaration->symbol->string);
258 fprintf(out, "func extern %s(",
259 declaration->symbol->string);
261 const method_type_t *method_type = (const method_type_t*) declaration->type;
263 declaration_t *parameter = method_type->parameters;
265 for( ; parameter != NULL; parameter = parameter->next) {
271 if(parameter->symbol != NULL) {
272 fprintf(out, "%s : ", parameter->symbol->string);
276 write_type(parameter->type);
278 if(method_type->variadic) {
288 const type_t *result_type = method_type->result_type;
289 if(result_type->type != TYPE_ATOMIC ||
290 ((const atomic_type_t*) result_type)->atype != ATOMIC_TYPE_VOID) {
292 write_type(result_type);
297 void write_fluffy_decls(const translation_unit_t *unit)
300 out = fopen("out.fluffy", "w");
302 fprintf(stderr, "Couldn't open out.fluffy: %s\n", strerror(errno));
307 global_context = &unit->context;
309 fprintf(out, "/* WARNING: Automatically generated file */\n");
311 /* write structs,unions + enums */
312 declaration_t *declaration = unit->context.declarations;
313 while(declaration != NULL) {
314 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
315 if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
316 declaration = declaration->next;
319 type_t *type = declaration->type;
320 if(type->type == TYPE_COMPOUND_STRUCT) {
321 write_struct(declaration->symbol, (compound_type_t*) type);
322 } else if(type->type == TYPE_COMPOUND_UNION) {
323 write_union(declaration->symbol, (compound_type_t*) type);
324 } else if(type->type == TYPE_ENUM) {
325 write_enum(declaration->symbol, (enum_type_t*) type);
328 declaration = declaration->next;
331 /* write global variables */
332 declaration = unit->context.declarations;
333 for( ; declaration != NULL; declaration = declaration->next) {
334 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF)
337 type_t *type = declaration->type;
338 if(type->type == TYPE_METHOD)
341 write_variable(declaration);
344 /* write functions */
345 declaration = unit->context.declarations;
346 for( ; declaration != NULL; declaration = declaration->next) {
347 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF)
350 type_t *type = declaration->type;
351 if(type->type != TYPE_METHOD)
354 write_function(declaration);