evalexpr: introduce single op_kind enum

This will allow making the precendence climbing cleaner
This commit is contained in:
Bruno BELANYI 2020-10-30 19:20:24 +01:00
parent 32d17134cd
commit f7c8413e3c
5 changed files with 32 additions and 23 deletions

View file

@ -15,8 +15,12 @@ struct ast_node *make_num(int val)
return ret;
}
struct ast_node *make_unop(enum unop_kind op, struct ast_node *tree)
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)
@ -29,9 +33,13 @@ struct ast_node *make_unop(enum unop_kind op, struct ast_node *tree)
return ret;
}
struct ast_node *make_binop(enum binop_kind op, struct ast_node *lhs,
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)

View file

@ -4,15 +4,14 @@
// Forward declaration
struct ast_node;
enum unop_kind
enum op_kind
{
// Prefix operators
UNOP_IDENTITY,
UNOP_NEGATE,
// Postfix operators
UNOP_FACT,
};
enum binop_kind
{
// Infix operators
BINOP_PLUS,
BINOP_MINUS,
BINOP_TIMES,
@ -22,13 +21,13 @@ enum binop_kind
struct unop_node
{
enum unop_kind op;
enum op_kind op;
struct ast_node *tree;
};
struct binop_node
{
enum binop_kind op;
enum op_kind op;
struct ast_node *lhs;
struct ast_node *rhs;
};
@ -51,9 +50,9 @@ struct ast_node
struct ast_node *make_num(int val);
struct ast_node *make_unop(enum unop_kind op, struct ast_node *tree);
struct ast_node *make_unop(enum op_kind op, struct ast_node *tree);
struct ast_node *make_binop(enum binop_kind op, struct ast_node *lhs,
struct ast_node *make_binop(enum op_kind op, struct ast_node *lhs,
struct ast_node *rhs);
void destroy_ast(struct ast_node *ast);