more bugfixes, started working on a fluffy declaration exporter
[cparser] / write_fluffy.c
1 #include <config.h>
2
3 #include <errno.h>
4 #include <string.h>
5
6 #include "ast_t.h"
7 #include "type_t.h"
8 #include "adt/error.h"
9
10 static FILE *out;
11
12 static void write_type(const type_t *type);
13
14 static const char *get_atomic_type_string(const atomic_type_type_t type)
15 {
16         switch(type) {
17         case ATOMIC_TYPE_VOID:      return "void";
18         case ATOMIC_TYPE_CHAR:      return "byte";
19         case ATOMIC_TYPE_SCHAR:     return "byte";
20         case ATOMIC_TYPE_UCHAR:     return "unsigned byte";
21         case ATOMIC_TYPE_SHORT:     return "short";
22         case ATOMIC_TYPE_USHORT:    return "unsigned short";
23         case ATOMIC_TYPE_INT:       return "int";
24         case ATOMIC_TYPE_UINT:      return "unsigned int";
25         case ATOMIC_TYPE_LONG:      return "int";
26         case ATOMIC_TYPE_ULONG:     return "unsigned int";
27         case ATOMIC_TYPE_LONGLONG:  return "long";
28         case ATOMIC_TYPE_ULONGLONG: return "unsigned long";
29         case ATOMIC_TYPE_FLOAT:     return "float";
30         case ATOMIC_TYPE_DOUBLE:    return "double";
31         case ATOMIC_TYPE_BOOL:      return "bool";
32         default:
33                                                                 panic("unsupported atomic type");
34         }
35 }
36
37 static void write_atomic_type(const atomic_type_t *type)
38 {
39         fprintf(out, "%s", get_atomic_type_string(type->atype));
40 }
41
42 static void write_pointer_type(const pointer_type_t *type)
43 {
44         write_type(type->points_to);
45         fputc('*', out);
46 }
47
48 static void write_type(const type_t *type)
49 {
50         switch(type->type) {
51         case TYPE_ATOMIC:
52                 write_atomic_type((const atomic_type_t*) type);
53                 return;
54         case TYPE_POINTER:
55                 write_pointer_type((const pointer_type_t*) type);
56                 return;
57         case TYPE_INVALID:
58                 panic("invalid type found");
59                 break;
60         default:
61                 fprintf(out, "/* TODO type */");
62                 break;
63         }
64 }
65
66 static void write_struct_entry(const declaration_t *declaration)
67 {
68         fprintf(out, "\t%s : ", declaration->symbol->string);
69         write_type(declaration->type);
70         fprintf(out, "\n");
71 }
72
73 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
74 {
75         (void) type;
76         fprintf(out, "struct %s:\n", symbol->string);
77
78         const declaration_t *declaration = type->context.declarations;
79         while(declaration != NULL) {
80                 write_struct_entry(declaration);
81                 declaration = declaration->next;
82         }
83
84         fprintf(out, "\n");
85 }
86
87 static void write_variable(const declaration_t *declaration)
88 {
89         fprintf(out, "var %s : ", declaration->symbol->string);
90         write_type(declaration->type);
91         /* TODO: initializers */
92         fprintf(out, "\n");
93 }
94
95 void write_fluffy_decls(const translation_unit_t *unit)
96 {
97 #if 0
98         out = fopen("out.fluffy", "w");
99         if(out == NULL) {
100                 fprintf(stderr, "Couldn't open out.fluffy: %s\n", strerror(errno));
101                 exit(1);
102         }
103 #endif
104         out = stdout;
105
106         fprintf(out, "/* WARNING: Automatically generated file */\n");
107
108         /* write structs + enums */
109         declaration_t *declaration = unit->context.declarations;
110         while(declaration != NULL) {
111                 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
112                 if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
113                         declaration = declaration->next;
114                         continue;
115                 }
116                 type_t *type = declaration->type;
117                 if(type->type == TYPE_COMPOUND_STRUCT) {
118                         write_struct(declaration->symbol, (compound_type_t*) type);
119                 } else if(type->type == TYPE_COMPOUND_UNION) {
120                         /* TODO */
121                 }
122
123                 declaration = declaration->next;
124         }
125
126         /* write global variables */
127         declaration = unit->context.declarations;
128         while(declaration != NULL) {
129                 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF) {
130                         declaration = declaration->next;
131                         continue;
132                 }
133
134                 type_t *type = declaration->type;
135                 if(type->type != TYPE_METHOD) {
136                         write_variable(declaration);
137                 }
138                 declaration = declaration->next;
139         }
140
141         //fclose(out);
142 }