diff --git a/src/ast/printer.h b/src/ast/printer.h new file mode 100644 index 0000000..6f087b5 --- /dev/null +++ b/src/ast/printer.h @@ -0,0 +1,8 @@ +#ifndef PRINTER_H +#define PRINTER_H + +#include "ast/ast.h" + +void printer_ast(const struct ast_node *ast); + +#endif /* !PRINTER_H */ diff --git a/src/local.am b/src/local.am index 387ec3c..a548005 100644 --- a/src/local.am +++ b/src/local.am @@ -16,3 +16,4 @@ jitters_LDADD = include %D%/ast/local.am include %D%/parse/local.am +include %D%/print/local.am diff --git a/src/print/local.am b/src/print/local.am new file mode 100644 index 0000000..e6db924 --- /dev/null +++ b/src/print/local.am @@ -0,0 +1,4 @@ +jitters_SOURCES += \ + %D%/printer.c \ + %D%/printer.h \ + $(NULL) diff --git a/src/print/printer.c b/src/print/printer.c new file mode 100644 index 0000000..0b74ff6 --- /dev/null +++ b/src/print/printer.c @@ -0,0 +1,66 @@ +#include "printer.h" + +#include + +static void printer_binop(const struct binop_node *bin_op) +{ + putchar('('); + printer_ast(bin_op->lhs); + switch (bin_op->op) + { + case PLUS: + printf(" + "); + break; + case MINUS: + printf(" - "); + break; + case TIMES: + printf(" * "); + break; + case DIVIDE: + printf(" / "); + break; + default: + break; + } + printer_ast(bin_op->rhs); + putchar(')'); +} + +static void printer_unop(const struct unop_node *un_op) +{ + putchar('('); + switch (un_op->op) + { + case NEGATE: + printf("- "); + case IDENTITY: + default: + break; + } + printer_ast(un_op->rhs); + putchar(')'); +} + +static void printer_num(int num) +{ + printf("%d", num); +} + +void printer_ast(const struct ast_node *ast) +{ + switch (ast->kind) + { + case BINOP: + printer_binop(&ast->val.bin_op); + break; + case UNOP: + printer_unop(&ast->val.un_op); + break; + case NUM: + printer_num(ast->val.num); + break; + default: + break; + } +} diff --git a/src/print/printer.h b/src/print/printer.h new file mode 100644 index 0000000..6f087b5 --- /dev/null +++ b/src/print/printer.h @@ -0,0 +1,8 @@ +#ifndef PRINTER_H +#define PRINTER_H + +#include "ast/ast.h" + +void printer_ast(const struct ast_node *ast); + +#endif /* !PRINTER_H */