10 #include "adt/error.h"
12 struct obstack ast_obstack;
17 static void print_statement(const statement_t *statement);
19 static void print_indent(void)
21 for(int i = 0; i < indent; ++i)
26 void print_const(const const_t *cnst)
28 fprintf(out, "%d", cnst->value);
32 void print_string_literal(const string_literal_t *string_literal)
34 /* TODO escape " and non-printable chars */
35 fprintf(out, "\"%s\"", string_literal->value);
39 void print_call_expression(const call_expression_t *call)
41 print_expression(call->method);
43 call_argument_t *argument = call->arguments;
45 while(argument != NULL) {
51 print_expression(argument->expression);
53 argument = argument->next;
59 void print_binary_expression(const binary_expression_t *binexpr)
62 print_expression(binexpr->left);
64 switch(binexpr->type) {
66 fprintf(out, "INVOP");
77 case BINEXPR_NOTEQUAL:
86 case BINEXPR_LESSEQUAL:
92 case BINEXPR_GREATEREQUAL:
96 /* TODO: add missing ops */
97 fprintf(out, "op%d", binexpr->type);
101 print_expression(binexpr->right);
105 static void print_reference_expression(const reference_expression_t *ref)
107 fprintf(out, "%s", ref->declaration->symbol->string);
110 void print_expression(const expression_t *expression)
112 switch(expression->type) {
114 fprintf(out, "*invalid expression*");
117 print_const((const const_t*) expression);
119 case EXPR_STRING_LITERAL:
120 print_string_literal((const string_literal_t*) expression);
123 print_call_expression((const call_expression_t*) expression);
126 print_binary_expression((const binary_expression_t*) expression);
129 print_reference_expression((const reference_expression_t*) expression);
133 case EXPR_ARRAY_ACCESS:
136 fprintf(out, "some expression of type %d", expression->type);
142 void print_compound_statement(const compound_statement_t *block)
147 statement_t *statement = block->statements;
148 while(statement != NULL) {
150 print_statement(statement);
152 statement = statement->next;
160 void print_return_statement(const return_statement_t *statement)
162 fprintf(out, "return ");
163 if(statement->return_value != NULL)
164 print_expression(statement->return_value);
169 void print_expression_statement(const expression_statement_t *statement)
171 print_expression(statement->expression);
175 void print_goto_statement(const goto_statement_t *statement)
177 fprintf(out, "goto ");
178 if(statement->label != NULL) {
179 fprintf(out, "%s", statement->label->symbol->string);
181 fprintf(out, "?%s", statement->label_symbol->string);
187 void print_label_statement(const label_statement_t *statement)
189 fprintf(out, "%s:\n", statement->symbol->string);
193 void print_if_statement(const if_statement_t *statement)
196 print_expression(statement->condition);
198 if(statement->true_statement != NULL) {
199 print_statement(statement->true_statement);
202 if(statement->false_statement != NULL) {
204 fprintf(out, "else ");
205 print_statement(statement->false_statement);
210 void print_declaration_statement(const declaration_statement_t *statement)
213 fprintf(out, "*declaration statement*");
216 void print_statement(const statement_t *statement)
218 switch(statement->type) {
219 case STATEMENT_COMPOUND:
220 print_compound_statement((const compound_statement_t*) statement);
222 case STATEMENT_RETURN:
223 print_return_statement((const return_statement_t*) statement);
225 case STATEMENT_EXPRESSION:
226 print_expression_statement((const expression_statement_t*) statement);
228 case STATEMENT_LABEL:
229 print_label_statement((const label_statement_t*) statement);
232 print_goto_statement((const goto_statement_t*) statement);
235 print_if_statement((const if_statement_t*) statement);
237 case STATEMENT_DECLARATION:
238 print_declaration_statement((const declaration_statement_t*) statement);
240 case STATEMENT_INVALID:
242 fprintf(out, "*invalid statement*");
250 void print_method_parameters(const method_parameter_t *parameters,
251 const method_type_t *method_type)
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) {
266 print_type(parameter_type->type);
267 fprintf(out, " %s", parameter->symbol->string);
269 parameter = parameter->next;
270 parameter_type = parameter_type->next;
272 assert(parameter == NULL && parameter_type == NULL);
279 void print_storage_class(storage_class_t storage_class)
281 switch(storage_class) {
282 case STORAGE_CLASS_NONE:
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;
293 void print_declaration(const declaration_t *declaration)
295 print_storage_class(declaration->storage_class);
296 print_type(declaration->type, declaration->symbol);
297 if(declaration->statement != NULL) {
299 print_statement(declaration->statement);
305 void print_ast(const translation_unit_t *unit)
307 declaration_t *declaration = unit->context.declarations;
308 while(declaration != NULL) {
309 print_declaration(declaration);
311 declaration = declaration->next;
317 obstack_init(&ast_obstack);
322 obstack_free(&ast_obstack, NULL);
325 void ast_set_output(FILE *stream)
328 type_set_output(stream);
331 void* (allocate_ast) (size_t size)
333 return _allocate_ast(size);