11 static const context_t *global_context;
14 static void write_type(const type_t *type);
16 static const char *get_atomic_type_string(const atomic_type_type_t type)
19 case ATOMIC_TYPE_VOID: return "void";
20 case ATOMIC_TYPE_CHAR: return "byte";
21 case ATOMIC_TYPE_SCHAR: return "byte";
22 case ATOMIC_TYPE_UCHAR: return "unsigned byte";
23 case ATOMIC_TYPE_SHORT: return "short";
24 case ATOMIC_TYPE_USHORT: return "unsigned short";
25 case ATOMIC_TYPE_INT: return "int";
26 case ATOMIC_TYPE_UINT: return "unsigned int";
27 case ATOMIC_TYPE_LONG: return "int";
28 case ATOMIC_TYPE_ULONG: return "unsigned int";
29 case ATOMIC_TYPE_LONGLONG: return "long";
30 case ATOMIC_TYPE_ULONGLONG: return "unsigned long";
31 case ATOMIC_TYPE_FLOAT: return "float";
32 case ATOMIC_TYPE_DOUBLE: return "double";
33 case ATOMIC_TYPE_LONG_DOUBLE: return "double";
34 case ATOMIC_TYPE_BOOL: return "bool";
35 default: panic("unsupported atomic type");
39 static void write_atomic_type(const atomic_type_t *type)
41 fprintf(out, "%s", get_atomic_type_string(type->atype));
44 static void write_pointer_type(const pointer_type_t *type)
46 write_type(type->points_to);
50 static declaration_t *find_typedef(const type_t *type)
52 /* first: search for a matching typedef in the global type... */
53 declaration_t *declaration = global_context->declarations;
54 while(declaration != NULL) {
55 if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
56 declaration = declaration->next;
59 if(declaration->type == type)
61 declaration = declaration->next;
67 static void write_compound_type(const compound_type_t *type)
69 declaration_t *declaration = find_typedef((const type_t*) type);
70 if(declaration != NULL) {
71 fprintf(out, "%s", declaration->symbol->string);
75 /* does the struct have a name? */
76 if(type->symbol != NULL) {
77 /* TODO: make sure we create a struct for it... */
78 fprintf(out, "%s", type->symbol->string);
81 /* TODO: create a struct and use its name here... */
82 fprintf(out, "/* TODO anonymous struct */byte");
85 static void write_enum_type(const enum_type_t *type)
87 declaration_t *declaration = find_typedef((const type_t*) type);
88 if(declaration != NULL) {
89 fprintf(out, "%s", declaration->symbol->string);
93 /* does the enum have a name? */
94 if(type->symbol != NULL) {
95 /* TODO: make sure we create an enum for it... */
96 fprintf(out, "%s", type->symbol->string);
99 /* TODO: create a struct and use its name here... */
100 fprintf(out, "/* TODO anonymous enum */byte");
103 static void write_method_type(const method_type_t *type)
105 fprintf(out, "(func(");
107 method_parameter_t *parameter = type->parameters;
109 while(parameter != NULL) {
117 if(parameter->symbol != NULL) {
118 fprintf(out, "%s : ", parameter->symbol->string);
120 /* TODO make up some unused names (or allow _ in fluffy?) */
121 fprintf(out, "_ : ");
125 write_type(parameter->type);
127 parameter = parameter->next;
130 fprintf(out, ") : ");
131 write_type(type->result_type);
135 static void write_type(const type_t *type)
139 write_atomic_type((const atomic_type_t*) type);
142 write_pointer_type((const pointer_type_t*) type);
144 case TYPE_COMPOUND_UNION:
145 case TYPE_COMPOUND_STRUCT:
146 write_compound_type((const compound_type_t*) type);
149 write_enum_type((const enum_type_t*) type);
152 write_method_type((const method_type_t*) type);
155 panic("invalid type found");
158 fprintf(out, "/* TODO type */");
163 static void write_struct_entry(const declaration_t *declaration)
165 fprintf(out, "\t%s : ", declaration->symbol->string);
166 write_type(declaration->type);
170 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
172 fprintf(out, "struct %s:\n", symbol->string);
174 const declaration_t *declaration = type->context.declarations;
175 while(declaration != NULL) {
176 write_struct_entry(declaration);
177 declaration = declaration->next;
183 static void write_union(const symbol_t *symbol, const compound_type_t *type)
185 fprintf(out, "union %s:\n", symbol->string);
187 const declaration_t *declaration = type->context.declarations;
188 while(declaration != NULL) {
189 write_struct_entry(declaration);
190 declaration = declaration->next;
196 static void write_expression(const expression_t *expression);
198 static void write_unary_expression(const unary_expression_t *expression)
200 switch(expression->type) {
208 panic("unimeplemented unary expression found");
210 write_expression(expression->value);
213 static void write_expression(const expression_t *expression)
215 const const_t *constant;
217 switch(expression->type) {
219 constant = (const const_t*) expression;
220 if(is_type_integer(expression->datatype)) {
221 fprintf(out, "%d", constant->v.int_value);
223 fprintf(out, "%Lf", constant->v.float_value);
227 write_unary_expression((const unary_expression_t*) expression);
230 panic("not implemented expression");
234 static void write_enum(const symbol_t *symbol, const enum_type_t *type)
236 fprintf(out, "enum %s:\n", symbol->string);
238 declaration_t *entry = type->entries_begin;
239 for ( ; entry != type->entries_end->next; entry = entry->next) {
240 fprintf(out, "\t%s", entry->symbol->string);
241 if(entry->initializer != NULL) {
242 fprintf(out, " <- ");
243 write_expression(entry->initializer);
247 fprintf(out, "typealias %s <- int\n", symbol->string);
251 static void write_variable(const declaration_t *declaration)
253 fprintf(out, "var %s : ", declaration->symbol->string);
254 write_type(declaration->type);
255 /* TODO: initializers */
259 static void write_function(const declaration_t *declaration)
261 if(declaration->statement != NULL) {
262 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
263 declaration->symbol->string);
266 fprintf(out, "func extern %s(",
267 declaration->symbol->string);
269 const method_type_t *method_type = (const method_type_t*) declaration->type;
271 declaration_t *parameter = declaration->context.declarations;
273 for( ; parameter != NULL; parameter = parameter->next) {
279 if(parameter->symbol != NULL) {
280 fprintf(out, "%s : ", parameter->symbol->string);
284 write_type(parameter->type);
286 if(method_type->variadic) {
296 const type_t *result_type = method_type->result_type;
297 if(result_type->type != TYPE_ATOMIC ||
298 ((const atomic_type_t*) result_type)->atype != ATOMIC_TYPE_VOID) {
300 write_type(result_type);
305 void write_fluffy_decls(const translation_unit_t *unit)
308 out = fopen("out.fluffy", "w");
310 fprintf(stderr, "Couldn't open out.fluffy: %s\n", strerror(errno));
315 global_context = &unit->context;
317 fprintf(out, "/* WARNING: Automatically generated file */\n");
319 /* write structs,unions + enums */
320 declaration_t *declaration = unit->context.declarations;
321 for( ; declaration != NULL; declaration = declaration->next) {
322 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
323 if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
326 type_t *type = declaration->type;
327 if(type->type == TYPE_COMPOUND_STRUCT) {
328 write_struct(declaration->symbol, (compound_type_t*) type);
329 } else if(type->type == TYPE_COMPOUND_UNION) {
330 write_union(declaration->symbol, (compound_type_t*) type);
331 } else if(type->type == TYPE_ENUM) {
332 write_enum(declaration->symbol, (enum_type_t*) type);
336 /* write global variables */
337 declaration = unit->context.declarations;
338 for( ; declaration != NULL; declaration = declaration->next) {
339 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
340 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
343 type_t *type = declaration->type;
344 if(type->type == TYPE_METHOD)
347 write_variable(declaration);
350 /* write functions */
351 declaration = unit->context.declarations;
352 for( ; declaration != NULL; declaration = declaration->next) {
353 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
354 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
357 type_t *type = declaration->type;
358 if(type->type != TYPE_METHOD)
361 write_function(declaration);