static void print_ifelse_stmt(int indent, struct stmt *s) { print_indent(indent); printf("IF\n"); print_expr_r(indent+1, s->ifelse.cond); print_block_stmt(indent+1, s->ifelse.block->block); if (s->ifelse.elseblock) { print_indent(indent); printf("ELSE\n"); print_block_stmt(indent+1, s->ifelse.elseblock->block); } }
static void print_stmt(int indent, struct stmt *s) { switch (s->type) { case STMT_EXPR: print_expr_r(indent, s->expr); break; case STMT_ASSIGN: print_assign_stmt(indent, s); break; case STMT_BLOCK: print_block_stmt(indent, s->block); break; case STMT_IFELSE: print_ifelse_stmt(indent, s); break; case STMT_FOR: print_for_stmt(indent, s); break; case STMT_FUNC: print_func_stmt(indent, s); break; case STMT_VAR: print_var_stmt(indent, s); break; case STMT_RETURN: print_return_stmt(indent, s); break; } }
static void print_for_stmt(int indent, struct stmt *s) { print_indent(indent); printf("FOR\n"); print_expr_r(indent+1, s->forloop.cond); print_block_stmt(indent+1, s->forloop.block->block); }
static void print_func_stmt(int indent, struct stmt *s) { print_indent(indent); if (s->func.block) printf("FUNC\n"); else printf("FOREIGN FUNC\n"); print_expr_r(indent+1, s->func.ident); if (s->func.args) print_call_args(indent+1, s->func.args); if (s->func.block) print_block_stmt(indent+1, s->func.block->block); }
void print_algo::operator()(std::ostream& os, const stmt& s) const { switch (s.get_kind()) { case block_stmt_kind: return print_block_stmt(os, cast<block_stmt>(s)); case decl_stmt_kind: return print_decl_stmt(os, cast<decl_stmt>(s)); case expr_stmt_kind: return print_expr_stmt(os, cast<expr_stmt>(s)); case ret_stmt_kind: return print_ret_stmt(os, cast<ret_stmt>(s)); default: break; } assert(false && "not a core statement"); }
void print_ast(struct stmts *top) { print_block_stmt(0, top); }