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 method_parameter_t *parameter = type->parameters;
108 while(parameter != NULL) {
116 if(parameter->symbol != NULL) {
117 fprintf(out, "%s : ", parameter->symbol->string);
119 /* TODO make up some unused names (or allow _ in fluffy?) */
120 fprintf(out, "_ : ");
124 write_type(parameter->type);
126 parameter = parameter->next;
129 fprintf(out, ") : ");
130 write_type(type->result_type);
134 static void write_type(const type_t *type)
138 write_atomic_type((const atomic_type_t*) type);
141 write_pointer_type((const pointer_type_t*) type);
143 case TYPE_COMPOUND_UNION:
144 case TYPE_COMPOUND_STRUCT:
145 write_compound_type((const compound_type_t*) type);
148 write_enum_type((const enum_type_t*) type);
151 write_method_type((const method_type_t*) type);
154 panic("invalid type found");
157 fprintf(out, "/* TODO type */");
162 static void write_struct_entry(const declaration_t *declaration)
164 fprintf(out, "\t%s : ", declaration->symbol->string);
165 write_type(declaration->type);
169 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
171 fprintf(out, "struct %s:\n", symbol->string);
173 const declaration_t *declaration = type->context.declarations;
174 while(declaration != NULL) {
175 write_struct_entry(declaration);
176 declaration = declaration->next;
182 static void write_union(const symbol_t *symbol, const compound_type_t *type)
184 fprintf(out, "union %s:\n", symbol->string);
186 const declaration_t *declaration = type->context.declarations;
187 while(declaration != NULL) {
188 write_struct_entry(declaration);
189 declaration = declaration->next;
195 static void write_expression(const expression_t *expression);
197 static void write_unary_expression(const unary_expression_t *expression)
199 switch(expression->type) {
207 panic("unimeplemented unary expression found");
209 write_expression(expression->value);
212 static void write_expression(const expression_t *expression)
214 const const_t *constant;
216 switch(expression->type) {
218 constant = (const const_t*) expression;
219 fprintf(out, "%d", constant->value);
222 write_unary_expression((const unary_expression_t*) expression);
225 panic("not implemented expression");
229 static void write_enum(const symbol_t *symbol, const enum_type_t *type)
231 fprintf(out, "enum %s:\n", symbol->string);
233 const enum_entry_t *entry = type->entries;
234 for ( ; entry != NULL; entry = entry->next) {
235 fprintf(out, "\t%s", entry->symbol->string);
236 if(entry->value != NULL) {
237 fprintf(out, " <- ");
238 write_expression(entry->value);
242 fprintf(out, "typealias %s <- int\n", symbol->string);
246 static void write_variable(const declaration_t *declaration)
248 fprintf(out, "var %s : ", declaration->symbol->string);
249 write_type(declaration->type);
250 /* TODO: initializers */
254 static void write_function(const declaration_t *declaration)
256 if(declaration->statement != NULL) {
257 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
258 declaration->symbol->string);
261 fprintf(out, "func extern %s(",
262 declaration->symbol->string);
264 const method_type_t *method_type = (const method_type_t*) declaration->type;
266 declaration_t *parameter = declaration->context.declarations;
268 for( ; parameter != NULL; parameter = parameter->next) {
274 if(parameter->symbol != NULL) {
275 fprintf(out, "%s : ", parameter->symbol->string);
279 write_type(parameter->type);
281 if(method_type->variadic) {
291 const type_t *result_type = method_type->result_type;
292 if(result_type->type != TYPE_ATOMIC ||
293 ((const atomic_type_t*) result_type)->atype != ATOMIC_TYPE_VOID) {
295 write_type(result_type);
300 void write_fluffy_decls(const translation_unit_t *unit)
303 out = fopen("out.fluffy", "w");
305 fprintf(stderr, "Couldn't open out.fluffy: %s\n", strerror(errno));
310 global_context = &unit->context;
312 fprintf(out, "/* WARNING: Automatically generated file */\n");
314 /* write structs,unions + enums */
315 declaration_t *declaration = unit->context.declarations;
316 for( ; declaration != NULL; declaration = declaration->next) {
317 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
318 if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
321 type_t *type = declaration->type;
322 if(type->type == TYPE_COMPOUND_STRUCT) {
323 write_struct(declaration->symbol, (compound_type_t*) type);
324 } else if(type->type == TYPE_COMPOUND_UNION) {
325 write_union(declaration->symbol, (compound_type_t*) type);
326 } else if(type->type == TYPE_ENUM) {
327 write_enum(declaration->symbol, (enum_type_t*) type);
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);