10 #include "adt/error.h"
12 struct obstack ast_obstack;
17 static void print_statement(const statement_t *statement);
20 void print_const(const const_t *cnst)
22 fprintf(out, "%d", cnst->value);
26 void print_string_literal(const string_literal_t *string_literal)
28 /* TODO escape " and non-printable chars */
29 fprintf(out, "\"%s\"", string_literal->value);
33 void print_call_expression(const call_expression_t *call)
35 print_expression(call->method);
37 call_argument_t *argument = call->arguments;
39 while(argument != NULL) {
45 print_expression(argument->expression);
47 argument = argument->next;
53 void print_binary_expression(const binary_expression_t *binexpr)
56 print_expression(binexpr->left);
58 switch(binexpr->type) {
60 fprintf(out, "INVOP");
71 case BINEXPR_NOTEQUAL:
80 case BINEXPR_LESSEQUAL:
86 case BINEXPR_GREATEREQUAL:
90 /* TODO: add missing ops */
91 fprintf(out, "op%d", binexpr->type);
95 print_expression(binexpr->right);
99 void print_expression(const expression_t *expression)
101 switch(expression->type) {
103 fprintf(out, "*invalid expression*");
106 print_const((const const_t*) expression);
108 case EXPR_STRING_LITERAL:
109 print_string_literal((const string_literal_t*) expression);
112 print_call_expression((const call_expression_t*) expression);
115 print_binary_expression((const binary_expression_t*) expression);
120 case EXPR_ARRAY_ACCESS:
123 fprintf(out, "some expression of type %d", expression->type);
129 void print_compound_statement(const compound_statement_t *block)
134 statement_t *statement = block->statements;
135 while(statement != NULL) {
136 print_statement(statement);
138 statement = statement->next;
145 void print_return_statement(const return_statement_t *statement)
147 fprintf(out, "return ");
148 if(statement->return_value != NULL)
149 print_expression(statement->return_value);
153 void print_expression_statement(const expression_statement_t *statement)
155 print_expression(statement->expression);
159 void print_goto_statement(const goto_statement_t *statement)
161 fprintf(out, "goto ");
162 if(statement->label != NULL) {
163 fprintf(out, "%s", statement->label->symbol->string);
165 fprintf(out, "?%s", statement->label_symbol->string);
170 void print_label_statement(const label_statement_t *statement)
172 fprintf(out, ":%s", statement->symbol->string);
176 void print_if_statement(const if_statement_t *statement)
179 print_expression(statement->condition);
181 if(statement->true_statement != NULL) {
182 print_statement(statement->true_statement);
185 if(statement->false_statement != NULL) {
186 fprintf(out, "else:\n");
187 print_statement(statement->false_statement);
192 void print_declaration_statement(const declaration_statement_t *statement)
195 fprintf(out, "*declaration statement*");
198 void print_statement(const statement_t *statement)
200 for(int i = 0; i < indent; ++i)
203 switch(statement->type) {
204 case STATEMENT_COMPOUND:
205 print_compound_statement((const compound_statement_t*) statement);
207 case STATEMENT_RETURN:
208 print_return_statement((const return_statement_t*) statement);
210 case STATEMENT_EXPRESSION:
211 print_expression_statement((const expression_statement_t*) statement);
213 case STATEMENT_LABEL:
214 print_label_statement((const label_statement_t*) statement);
217 print_goto_statement((const goto_statement_t*) statement);
220 print_if_statement((const if_statement_t*) statement);
222 case STATEMENT_DECLARATION:
223 print_declaration_statement((const declaration_statement_t*) statement);
225 case STATEMENT_INVALID:
227 fprintf(out, "*invalid statement*");
236 void print_method_parameters(const method_parameter_t *parameters,
237 const method_type_t *method_type)
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) {
252 print_type(parameter_type->type);
253 fprintf(out, " %s", parameter->symbol->string);
255 parameter = parameter->next;
256 parameter_type = parameter_type->next;
258 assert(parameter == NULL && parameter_type == NULL);
265 void print_storage_class(storage_class_t storage_class)
267 switch(storage_class) {
268 case STORAGE_CLASS_NONE:
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;
279 void print_declaration(const declaration_t *declaration)
281 print_storage_class(declaration->storage_class);
282 print_type(declaration->type, declaration->symbol);
283 if(declaration->statement != NULL) {
285 print_statement(declaration->statement);
291 void print_ast(const translation_unit_t *unit)
293 declaration_t *declaration = unit->context.declarations;
294 while(declaration != NULL) {
295 print_declaration(declaration);
297 declaration = declaration->next;
303 obstack_init(&ast_obstack);
308 obstack_free(&ast_obstack, NULL);
311 void ast_set_output(FILE *stream)
314 type_set_output(stream);
317 void* (allocate_ast) (size_t size)
319 return _allocate_ast(size);