diff --git a/src/print/printer.c b/src/print/printer.c index 0b74ff6..4d5a632 100644 --- a/src/print/printer.c +++ b/src/print/printer.c @@ -2,65 +2,73 @@ #include -static void printer_binop(const struct binop_node *bin_op) +static void printer_ast(const struct ast_node *ast, FILE *file); + +static void printer_binop(const struct binop_node *bin_op, FILE *file) { - putchar('('); - printer_ast(bin_op->lhs); + fputc('(', file); + printer_ast(bin_op->lhs, file); switch (bin_op->op) { case PLUS: - printf(" + "); + fputs(" + ", file); break; case MINUS: - printf(" - "); + fputs(" - ", file); break; case TIMES: - printf(" * "); + fputs(" * ", file); break; case DIVIDE: - printf(" / "); + fputs(" / ", file); break; default: break; } - printer_ast(bin_op->rhs); - putchar(')'); + printer_ast(bin_op->rhs, file); + fputc(')', file); } -static void printer_unop(const struct unop_node *un_op) +static void printer_unop(const struct unop_node *un_op, FILE *file) { - putchar('('); + fputc('(', file); switch (un_op->op) { case NEGATE: - printf("- "); + fputs("- ", file); case IDENTITY: default: break; } - printer_ast(un_op->rhs); - putchar(')'); + printer_ast(un_op->rhs, file); + fputc(')', file); } -static void printer_num(int num) +static void printer_num(int num, FILE *file) { - printf("%d", num); + fprintf(file, "%d", num); } -void printer_ast(const struct ast_node *ast) +static void printer_ast(const struct ast_node *ast, FILE *file) { switch (ast->kind) { case BINOP: - printer_binop(&ast->val.bin_op); + printer_binop(&ast->val.bin_op, file); break; case UNOP: - printer_unop(&ast->val.un_op); + printer_unop(&ast->val.un_op, file); break; case NUM: - printer_num(ast->val.num); + printer_num(ast->val.num, file); break; default: break; } } + +void print_ast(const struct ast_node *ast, FILE *file) +{ + printer_ast(ast, file); + fputc('\n', file); +} diff --git a/src/print/printer.h b/src/print/printer.h index 6f087b5..ca9e551 100644 --- a/src/print/printer.h +++ b/src/print/printer.h @@ -1,8 +1,10 @@ #ifndef PRINTER_H #define PRINTER_H +#include + #include "ast/ast.h" -void printer_ast(const struct ast_node *ast); +void print_ast(const struct ast_node *ast, FILE *file); #endif /* !PRINTER_H */