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