81cd3ca09bcb7abb76d5c015b0e368b0b0d6ab4a
[cparser] / ast.c
1 #include <config.h>
2
3 #include "ast_t.h"
4 #include "type_t.h"
5
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include "adt/error.h"
11
12 struct obstack ast_obstack;
13
14 static FILE *out;
15 static int   indent;
16
17 static void print_statement(const statement_t *statement);
18
19 static
20 void print_const(const const_t *cnst)
21 {
22         fprintf(out, "%d", cnst->value);
23 }
24
25 static
26 void print_string_literal(const string_literal_t *string_literal)
27 {
28         /* TODO escape " and non-printable chars */
29         fprintf(out, "\"%s\"", string_literal->value);
30 }
31
32 static
33 void print_call_expression(const call_expression_t *call)
34 {
35         print_expression(call->method);
36         fprintf(out, "(");
37         call_argument_t *argument = call->arguments;
38         int              first    = 1;
39         while(argument != NULL) {
40                 if(!first) {
41                         fprintf(out, ", ");
42                 } else {
43                         first = 0;
44                 }
45                 print_expression(argument->expression);
46
47                 argument = argument->next;
48         }
49         fprintf(out, ")");
50 }
51
52 static
53 void print_binary_expression(const binary_expression_t *binexpr)
54 {
55         fprintf(out, "(");
56         print_expression(binexpr->left);
57         fprintf(out, " ");
58         switch(binexpr->type) {
59         case BINEXPR_INVALID:
60                 fprintf(out, "INVOP");
61                 break;
62         case BINEXPR_ASSIGN:
63                 fprintf(out, "<-");
64                 break;
65         case BINEXPR_ADD:
66                 fprintf(out, "+");
67                 break;
68         case BINEXPR_SUB:
69                 fprintf(out, "-");
70                 break;
71         case BINEXPR_NOTEQUAL:
72                 fprintf(out, "/=");
73                 break;
74         case BINEXPR_EQUAL:
75                 fprintf(out, "=");
76                 break;
77         case BINEXPR_LESS:
78                 fprintf(out, "<");
79                 break;
80         case BINEXPR_LESSEQUAL:
81                 fprintf(out, "<=");
82                 break;
83         case BINEXPR_GREATER:
84                 fprintf(out, ">");
85                 break;
86         case BINEXPR_GREATEREQUAL:
87                 fprintf(out, ">=");
88                 break;
89         default:
90                 /* TODO: add missing ops */
91                 fprintf(out, "op%d", binexpr->type);
92                 break;
93         }
94         fprintf(out, " ");
95         print_expression(binexpr->right);
96         fprintf(out, ")");
97 }
98
99 void print_expression(const expression_t *expression)
100 {
101         switch(expression->type) {
102         case EXPR_INVALID:
103                 fprintf(out, "*invalid expression*");
104                 break;
105         case EXPR_CONST:
106                 print_const((const const_t*) expression);
107                 break;
108         case EXPR_STRING_LITERAL:
109                 print_string_literal((const string_literal_t*) expression);
110                 break;
111         case EXPR_CALL:
112                 print_call_expression((const call_expression_t*) expression);
113                 break;
114         case EXPR_BINARY:
115                 print_binary_expression((const binary_expression_t*) expression);
116                 break;
117         case EXPR_REFERENCE:
118         case EXPR_UNARY:
119         case EXPR_SELECT:
120         case EXPR_ARRAY_ACCESS:
121         case EXPR_SIZEOF:
122                 /* TODO */
123                 fprintf(out, "some expression of type %d", expression->type);
124                 break;
125         }
126 }
127
128 static
129 void print_compound_statement(const compound_statement_t *block)
130 {
131         fputs("{\n", out);
132         indent++;
133
134         statement_t *statement = block->statements;
135         while(statement != NULL) {
136                 print_statement(statement);
137
138                 statement = statement->next;
139         }
140         indent--;
141         fputs("}\n", out);
142 }
143
144 static
145 void print_return_statement(const return_statement_t *statement)
146 {
147         fprintf(out, "return ");
148         if(statement->return_value != NULL)
149                 print_expression(statement->return_value);
150 }
151
152 static
153 void print_expression_statement(const expression_statement_t *statement)
154 {
155         print_expression(statement->expression);
156 }
157
158 static
159 void print_goto_statement(const goto_statement_t *statement)
160 {
161         fprintf(out, "goto ");
162         if(statement->label != NULL) {
163                 fprintf(out, "%s", statement->label->symbol->string);
164         } else {
165                 fprintf(out, "?%s", statement->label_symbol->string);
166         }
167 }
168
169 static
170 void print_label_statement(const label_statement_t *statement)
171 {
172         fprintf(out, ":%s", statement->symbol->string);
173 }
174
175 static
176 void print_if_statement(const if_statement_t *statement)
177 {
178         fprintf(out, "if ");
179         print_expression(statement->condition);
180         fprintf(out, ":\n");
181         if(statement->true_statement != NULL) {
182                 print_statement(statement->true_statement);
183         }
184
185         if(statement->false_statement != NULL) {
186                 fprintf(out, "else:\n");
187                 print_statement(statement->false_statement);
188         }
189 }
190
191 static
192 void print_declaration_statement(const declaration_statement_t *statement)
193 {
194         (void) statement;
195         fprintf(out, "*declaration statement*");
196 }
197
198 void print_statement(const statement_t *statement)
199 {
200         for(int i = 0; i < indent; ++i)
201                 fprintf(out, "\t");
202
203         switch(statement->type) {
204         case STATEMENT_COMPOUND:
205                 print_compound_statement((const compound_statement_t*) statement);
206                 break;
207         case STATEMENT_RETURN:
208                 print_return_statement((const return_statement_t*) statement);
209                 break;
210         case STATEMENT_EXPRESSION:
211                 print_expression_statement((const expression_statement_t*) statement);
212                 break;
213         case STATEMENT_LABEL:
214                 print_label_statement((const label_statement_t*) statement);
215                 break;
216         case STATEMENT_GOTO:
217                 print_goto_statement((const goto_statement_t*) statement);
218                 break;
219         case STATEMENT_IF:
220                 print_if_statement((const if_statement_t*) statement);
221                 break;
222         case STATEMENT_DECLARATION:
223                 print_declaration_statement((const declaration_statement_t*) statement);
224                 break;
225         case STATEMENT_INVALID:
226         default:
227                 fprintf(out, "*invalid statement*");
228                 break;
229
230         }
231         fprintf(out, "\n");
232 }
233
234 #if 0
235 static
236 void print_method_parameters(const method_parameter_t *parameters,
237                              const method_type_t *method_type)
238 {
239         fprintf(out, "(");
240
241         int                            first          = 1;
242         const method_parameter_t      *parameter      = parameters;
243         const method_parameter_type_t *parameter_type
244                 = method_type->parameter_types;
245         while(parameter != NULL && parameter_type != NULL) {
246                 if(!first) {
247                         fprintf(out, ", ");
248                 } else {
249                         first = 0;
250                 }
251
252                 print_type(parameter_type->type);
253                 fprintf(out, " %s", parameter->symbol->string);
254
255                 parameter      = parameter->next;
256                 parameter_type = parameter_type->next;
257         }
258         assert(parameter == NULL && parameter_type == NULL);
259
260         fprintf(out, ")");
261 }
262 #endif
263
264 static
265 void print_storage_class(storage_class_t storage_class)
266 {
267         switch(storage_class) {
268         case STORAGE_CLASS_NONE:
269                 break;
270         case STORAGE_CLASS_TYPEDEF:  fputs("typedef ", out); break;
271         case STORAGE_CLASS_EXTERN:   fputs("extern ", out); break;
272         case STORAGE_CLASS_STATIC:   fputs("static ", out); break;
273         case STORAGE_CLASS_AUTO:     fputs("auto ", out); break;
274         case STORAGE_CLASS_REGISTER: fputs("register ", out); break;
275         }
276 }
277
278 static
279 void print_declaration(const declaration_t *declaration)
280 {
281         print_storage_class(declaration->storage_class);
282         print_type(declaration->type, declaration->symbol);
283         if(declaration->statement != NULL) {
284                 fprintf(out, "\n");
285                 print_statement(declaration->statement);
286         } else {
287                 fprintf(out, ";\n");
288         }
289 }
290
291 void print_ast(const translation_unit_t *unit)
292 {
293         declaration_t *declaration = unit->context.declarations;
294         while(declaration != NULL) {
295                 print_declaration(declaration);
296
297                 declaration = declaration->next;
298         }
299 }
300
301 void init_ast(void)
302 {
303         obstack_init(&ast_obstack);
304 }
305
306 void exit_ast(void)
307 {
308         obstack_free(&ast_obstack, NULL);
309 }
310
311 void ast_set_output(FILE *stream)
312 {
313         out = stream;
314         type_set_output(stream);
315 }
316
317 void* (allocate_ast) (size_t size)
318 {
319         return _allocate_ast(size);
320 }