10 #include "adt/error.h"
12 struct obstack ast_obstack;
17 static void print_expression(const expression_t *expression);
18 static void print_statement(const statement_t *statement);
21 void print_const(const const_t *cnst)
23 fprintf(out, "%d", cnst->value);
27 void print_string_literal(const string_literal_t *string_literal)
29 /* TODO escape " and non-printable chars */
30 fprintf(out, "\"%s\"", string_literal->value);
34 void print_call_expression(const call_expression_t *call)
36 print_expression(call->method);
38 call_argument_t *argument = call->arguments;
40 while(argument != NULL) {
46 print_expression(argument->expression);
48 argument = argument->next;
54 void print_binary_expression(const binary_expression_t *binexpr)
57 print_expression(binexpr->left);
59 switch(binexpr->type) {
61 fprintf(out, "INVOP");
72 case BINEXPR_NOTEQUAL:
81 case BINEXPR_LESSEQUAL:
87 case BINEXPR_GREATEREQUAL:
91 /* TODO: add missing ops */
92 fprintf(out, "op%d", binexpr->type);
96 print_expression(binexpr->right);
100 void print_expression(const expression_t *expression)
102 switch(expression->type) {
104 fprintf(out, "*invalid expression*");
107 print_const((const const_t*) expression);
109 case EXPR_STRING_LITERAL:
110 print_string_literal((const string_literal_t*) expression);
113 print_call_expression((const call_expression_t*) expression);
116 print_binary_expression((const binary_expression_t*) expression);
121 case EXPR_ARRAY_ACCESS:
124 fprintf(out, "some expression of type %d", expression->type);
130 void print_compound_statement(const compound_statement_t *block)
135 statement_t *statement = block->statements;
136 while(statement != NULL) {
137 print_statement(statement);
139 statement = statement->next;
146 void print_return_statement(const return_statement_t *statement)
148 fprintf(out, "return ");
149 if(statement->return_value != NULL)
150 print_expression(statement->return_value);
154 void print_expression_statement(const expression_statement_t *statement)
156 print_expression(statement->expression);
160 void print_goto_statement(const goto_statement_t *statement)
162 fprintf(out, "goto ");
163 if(statement->label != NULL) {
164 fprintf(out, "%s", statement->label->symbol->string);
166 fprintf(out, "?%s", statement->label_symbol->string);
171 void print_label_statement(const label_statement_t *statement)
173 fprintf(out, ":%s", statement->symbol->string);
177 void print_if_statement(const if_statement_t *statement)
180 print_expression(statement->condition);
182 if(statement->true_statement != NULL) {
183 print_statement(statement->true_statement);
186 if(statement->false_statement != NULL) {
187 fprintf(out, "else:\n");
188 print_statement(statement->false_statement);
193 void print_declaration_statement(const declaration_statement_t *statement)
196 fprintf(out, "*declaration statement*");
199 void print_statement(const statement_t *statement)
201 for(int i = 0; i < indent; ++i)
204 switch(statement->type) {
205 case STATEMENT_COMPOUND:
206 print_compound_statement((const compound_statement_t*) statement);
208 case STATEMENT_RETURN:
209 print_return_statement((const return_statement_t*) statement);
211 case STATEMENT_EXPRESSION:
212 print_expression_statement((const expression_statement_t*) statement);
214 case STATEMENT_LABEL:
215 print_label_statement((const label_statement_t*) statement);
218 print_goto_statement((const goto_statement_t*) statement);
221 print_if_statement((const if_statement_t*) statement);
223 case STATEMENT_DECLARATION:
224 print_declaration_statement((const declaration_statement_t*) statement);
226 case STATEMENT_INVALID:
228 fprintf(out, "*invalid statement*");
237 void print_method_parameters(const method_parameter_t *parameters,
238 const method_type_t *method_type)
243 const method_parameter_t *parameter = parameters;
244 const method_parameter_type_t *parameter_type
245 = method_type->parameter_types;
246 while(parameter != NULL && parameter_type != NULL) {
253 print_type(parameter_type->type);
254 fprintf(out, " %s", parameter->symbol->string);
256 parameter = parameter->next;
257 parameter_type = parameter_type->next;
259 assert(parameter == NULL && parameter_type == NULL);
266 void print_declaration(const declaration_t *declaration)
268 print_type(declaration->type, declaration->symbol);
270 if(declaration->statement != NULL) {
271 print_statement(declaration->statement);
275 void print_ast(const translation_unit_t *unit)
277 declaration_t *declaration = unit->context.declarations;
278 while(declaration != NULL) {
279 print_declaration(declaration);
281 declaration = declaration->next;
287 obstack_init(&ast_obstack);
292 obstack_free(&ast_obstack, NULL);
295 void ast_set_output(FILE *stream)
298 type_set_output(stream);
301 void* (allocate_ast) (size_t size)
303 return _allocate_ast(size);