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; 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)); struct ast_node *ret = malloc(sizeof(*ret));
if (ret == NULL) if (ret == NULL)
@ -29,9 +33,13 @@ struct ast_node *make_unop(enum unop_kind op, struct ast_node *tree)
return ret; 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) struct ast_node *rhs)
{ {
// Defensive programming
if (op < BINOP_PLUS)
return NULL;
struct ast_node *ret = malloc(sizeof(*ret)); struct ast_node *ret = malloc(sizeof(*ret));
if (ret == NULL) if (ret == NULL)

View file

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

View file

@ -30,8 +30,9 @@ static int eval_unop(const struct unop_node *un_op)
return -eval_ast(un_op->tree); return -eval_ast(un_op->tree);
case UNOP_FACT: case UNOP_FACT:
return my_fact(eval_ast(un_op->tree)); return my_fact(eval_ast(un_op->tree));
default:
UNREACHABLE();
} }
UNREACHABLE();
} }
static int eval_binop(const struct binop_node *bin_op) static int eval_binop(const struct binop_node *bin_op)
@ -49,9 +50,10 @@ static int eval_binop(const struct binop_node *bin_op)
return EVAL_OP(/, bin_op); return EVAL_OP(/, bin_op);
case BINOP_POW: case BINOP_POW:
return my_pow(eval_ast(bin_op->lhs), eval_ast(bin_op->rhs)); return my_pow(eval_ast(bin_op->lhs), eval_ast(bin_op->rhs));
default:
UNREACHABLE();
} }
#undef EVAL_OP #undef EVAL_OP
UNREACHABLE();
} }
int eval_ast(const struct ast_node *ast) int eval_ast(const struct ast_node *ast)

View file

@ -37,7 +37,7 @@ static void skip_whitespace(const char **input)
eat_char(input); eat_char(input);
} }
static enum binop_kind char_to_binop(char c) static enum op_kind char_to_binop(char c)
{ {
for (size_t i = 0; i < ARR_SIZE(ops); ++i) for (size_t i = 0; i < ARR_SIZE(ops); ++i)
if (ops[i].fix == OP_INFIX && c == ops[i].op[0]) if (ops[i].fix == OP_INFIX && c == ops[i].op[0])
@ -46,7 +46,7 @@ static enum binop_kind char_to_binop(char c)
UNREACHABLE(); UNREACHABLE();
} }
static enum unop_kind char_to_unop(char c) static enum op_kind char_to_unop(char c)
{ {
for (size_t i = 0; i < ARR_SIZE(ops); ++i) for (size_t i = 0; i < ARR_SIZE(ops); ++i)
if (ops[i].fix != OP_INFIX && c == ops[i].op[0]) if (ops[i].fix != OP_INFIX && c == ops[i].op[0])
@ -164,7 +164,7 @@ static struct ast_node *climbing_parse_internal(const char **input, int prec)
eat_char(input); eat_char(input);
if (is_binop(c)) if (is_binop(c))
{ {
enum binop_kind op = char_to_binop(c); enum op_kind op = char_to_binop(c);
struct ast_node *rhs = climbing_parse_internal(input, right_prec(c)); struct ast_node *rhs = climbing_parse_internal(input, right_prec(c));
if (!rhs) if (!rhs)
{ {
@ -227,7 +227,7 @@ static struct ast_node *parse_operand(const char **input)
int val = 0; int val = 0;
if (is_prefix(*input[0])) if (is_prefix(*input[0]))
{ {
enum unop_kind op = char_to_unop(*input[0]); enum op_kind op = char_to_unop(*input[0]);
// Remove the parenthesis // Remove the parenthesis
eat_char(input); eat_char(input);

View file

@ -61,7 +61,7 @@ struct ast_node *recursive_parse(const char *input)
return ast; return ast;
} }
static enum binop_kind char_to_binop(char c) static enum op_kind char_to_binop(char c)
{ {
switch (c) switch (c)
{ {
@ -94,7 +94,7 @@ static struct ast_node *parse_expression(const char **input)
if (*input[0] == '+' || *input[0] == '-') if (*input[0] == '+' || *input[0] == '-')
{ {
const enum binop_kind op = char_to_binop(*input[0]); const enum op_kind op = char_to_binop(*input[0]);
eat_char(input); eat_char(input);
@ -130,7 +130,7 @@ static struct ast_node *parse_term(const char **input)
if (*input[0] == '*' || *input[0] == '/') if (*input[0] == '*' || *input[0] == '/')
{ {
const enum binop_kind op = char_to_binop(*input[0]); const enum op_kind op = char_to_binop(*input[0]);
eat_char(input); eat_char(input);
@ -151,7 +151,7 @@ static struct ast_node *parse_term(const char **input)
return lhs; return lhs;
} }
static enum unop_kind char_to_unop(char c) static enum op_kind char_to_unop(char c)
{ {
switch (c) switch (c)
{ {
@ -170,7 +170,7 @@ static struct ast_node *parse_factor(const char **input)
skip_whitespace(input); // Whitespace is not significant skip_whitespace(input); // Whitespace is not significant
while (*input[0] == '+' || *input[0] == '-') while (*input[0] == '+' || *input[0] == '-')
{ {
const enum unop_kind op = char_to_unop(*input[0]); const enum op_kind op = char_to_unop(*input[0]);
eat_char(input); eat_char(input);
@ -197,7 +197,7 @@ static struct ast_node *parse_power(const char **input)
if (*input[0] == '^') if (*input[0] == '^')
{ {
const enum binop_kind op = char_to_binop(*input[0]); const enum op_kind op = char_to_binop(*input[0]);
eat_char(input); eat_char(input);