jitters: print: print on FILE instead of stdout
This commit is contained in:
parent
6aff66a4b6
commit
fe05cff2c8
|
@ -2,65 +2,73 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#ifndef PRINTER_H
|
||||
#define PRINTER_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#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 */
|
||||
|
|
Loading…
Reference in a new issue