evalexpr/src/ast/ast.c

76 lines
1.3 KiB
C

#include "ast.h"
#include <stdlib.h>
struct ast_node *make_num(int val)
{
struct ast_node *ret = malloc(sizeof(*ret));
if (ret == NULL)
return ret;
ret->kind = NODE_NUM;
ret->val.num = val;
return ret;
}
struct ast_node *make_unop(enum op_kind op, struct ast_node *tree)
{
// Defensive programming
if (op >= BINOP_PLUS)
return NULL;
struct ast_node *ret = malloc(sizeof(*ret));
if (ret == NULL)
return ret;
ret->kind = NODE_UNOP;
ret->val.un_op.op = op;
ret->val.un_op.tree = tree;
return ret;
}
struct ast_node *make_binop(enum op_kind op, struct ast_node *lhs,
struct ast_node *rhs)
{
// Defensive programming
if (op < BINOP_PLUS)
return NULL;
struct ast_node *ret = malloc(sizeof(*ret));
if (ret == NULL)
return ret;
ret->kind = NODE_BINOP;
ret->val.bin_op.op = op;
ret->val.bin_op.lhs = lhs;
ret->val.bin_op.rhs = rhs;
return ret;
}
void destroy_ast(struct ast_node *ast)
{
if (!ast)
return;
switch (ast->kind)
{
case NODE_BINOP:
destroy_ast(ast->val.bin_op.lhs);
destroy_ast(ast->val.bin_op.rhs);
break;
case NODE_UNOP:
destroy_ast(ast->val.un_op.tree);
/* fallthrough */
case NODE_NUM:
break;
}
free(ast);
}