10 #include "adt/error.h"
12 struct obstack ast_obstack;
15 void print_const(FILE *out, const const_t *cnst)
17 fprintf(out, "%d", cnst->value);
21 void print_string_literal(FILE *out, const string_literal_t *string_literal)
23 /* TODO escape " and non-printable chars */
24 fprintf(out, "\"%s\"", string_literal->value);
28 void print_call_expression(FILE *out, const call_expression_t *call)
30 print_expression(out, call->method);
32 call_argument_t *argument = call->arguments;
34 while(argument != NULL) {
40 print_expression(out, argument->expression);
42 argument = argument->next;
48 void print_binary_expression(FILE *out, const binary_expression_t *binexpr)
51 print_expression(out, binexpr->left);
53 switch(binexpr->type) {
55 fprintf(out, "INVOP");
66 case BINEXPR_NOTEQUAL:
75 case BINEXPR_LESSEQUAL:
81 case BINEXPR_GREATEREQUAL:
85 /* TODO: add missing ops */
86 fprintf(out, "op%d", binexpr->type);
90 print_expression(out, binexpr->right);
94 void print_expression(FILE *out, const expression_t *expression)
96 switch(expression->type) {
98 fprintf(out, "*invalid expression*");
101 print_const(out, (const const_t*) expression);
103 case EXPR_STRING_LITERAL:
104 print_string_literal(out, (const string_literal_t*) expression);
107 print_call_expression(out, (const call_expression_t*) expression);
110 print_binary_expression(out, (const binary_expression_t*) expression);
115 case EXPR_ARRAY_ACCESS:
118 fprintf(out, "some expression of type %d", expression->type);
124 void print_compound_statement(FILE *out, int indent,
125 const compound_statement_t *block)
127 statement_t *statement = block->first_statement;
128 while(statement != NULL) {
129 print_statement(out, indent + 1, statement);
131 statement = statement->next;
136 void print_return_statement(FILE *out, const return_statement_t *statement)
138 fprintf(out, "return ");
139 if(statement->return_value != NULL)
140 print_expression(out, statement->return_value);
144 void print_expression_statement(FILE *out,
145 const expression_statement_t *statement)
147 print_expression(out, statement->expression);
151 void print_goto_statement(FILE *out, const goto_statement_t *statement)
153 fprintf(out, "goto ");
154 if(statement->label != NULL) {
155 fprintf(out, "%s", statement->label->symbol->string);
157 fprintf(out, "?%s", statement->label_symbol->string);
162 void print_label_statement(FILE *out, const label_statement_t *statement)
164 fprintf(out, ":%s", statement->symbol->string);
168 void print_if_statement(FILE *out, int indent, const if_statement_t *statement)
171 print_expression(out, statement->condition);
173 if(statement->true_statement != NULL) {
174 print_statement(out, indent, statement->true_statement);
177 if(statement->false_statement != NULL) {
178 fprintf(out, "else:\n");
179 print_statement(out, indent, statement->false_statement);
184 void print_declaration_statement(FILE *out,
185 const declaration_statement_t *statement)
188 fprintf(out, "*declaration statement*");
191 void print_statement(FILE *out, int indent, const statement_t *statement)
193 for(int i = 0; i < indent; ++i)
196 switch(statement->type) {
197 case STATEMENT_COMPOUND:
198 print_compound_statement(out, indent,
199 (const compound_statement_t*) statement);
201 case STATEMENT_RETURN:
202 print_return_statement(out, (const return_statement_t*) statement);
204 case STATEMENT_EXPRESSION:
205 print_expression_statement(out,
206 (const expression_statement_t*) statement);
208 case STATEMENT_LABEL:
209 print_label_statement(out, (const label_statement_t*) statement);
212 print_goto_statement(out, (const goto_statement_t*) statement);
215 print_if_statement(out, indent, (const if_statement_t*) statement);
217 case STATEMENT_DECLARATION:
218 print_declaration_statement(out,
219 (const declaration_statement_t*) statement);
221 case STATEMENT_INVALID:
223 fprintf(out, "*invalid statement*");
232 void print_method_parameters(FILE *out, const method_parameter_t *parameters,
233 const method_type_t *method_type)
238 const method_parameter_t *parameter = parameters;
239 const method_parameter_type_t *parameter_type
240 = method_type->parameter_types;
241 while(parameter != NULL && parameter_type != NULL) {
248 print_type(out, parameter_type->type);
249 fprintf(out, " %s", parameter->symbol->string);
251 parameter = parameter->next;
252 parameter_type = parameter_type->next;
254 assert(parameter == NULL && parameter_type == NULL);
261 void print_declaration(FILE *out, const declaration_t *declaration)
264 print_type(out, declaration->type);
265 fprintf(out, " %s", declaration->symbol->string);
269 void print_namespace_entry(FILE *out, const unit_entry_t *entry)
271 print_declaration(out, &entry->declaration);
274 void print_ast(FILE *out, const translation_unit_t *unit)
276 unit_entry_t *entry = unit->entries;
278 while(entry != NULL) {
279 print_namespace_entry(out, entry);
287 obstack_init(&ast_obstack);
292 obstack_free(&ast_obstack, NULL);
295 void* (allocate_ast) (size_t size)
297 return _allocate_ast(size);