jitters: print: add printer function
This commit is contained in:
parent
a8f4122146
commit
6ffcbacdba
8
src/ast/printer.h
Normal file
8
src/ast/printer.h
Normal file
|
@ -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 */
|
|
@ -16,3 +16,4 @@ jitters_LDADD =
|
|||
|
||||
include %D%/ast/local.am
|
||||
include %D%/parse/local.am
|
||||
include %D%/print/local.am
|
||||
|
|
4
src/print/local.am
Normal file
4
src/print/local.am
Normal file
|
@ -0,0 +1,4 @@
|
|||
jitters_SOURCES += \
|
||||
%D%/printer.c \
|
||||
%D%/printer.h \
|
||||
$(NULL)
|
66
src/print/printer.c
Normal file
66
src/print/printer.c
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include "printer.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
8
src/print/printer.h
Normal file
8
src/print/printer.h
Normal file
|
@ -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 */
|
Loading…
Reference in a new issue