From f7c8413e3c5f50d3931f7a834d197dab41fb9abb Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 30 Oct 2020 19:20:24 +0100 Subject: [PATCH] evalexpr: introduce single op_kind enum This will allow making the precendence climbing cleaner --- src/ast/ast.c | 12 ++++++++++-- src/ast/ast.h | 17 ++++++++--------- src/eval/eval.c | 6 ++++-- src/parse/climbing_parse.c | 8 ++++---- src/parse/recursive_parse.c | 12 ++++++------ 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/ast/ast.c b/src/ast/ast.c index 4db6bb8..6795f5f 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -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) diff --git a/src/ast/ast.h b/src/ast/ast.h index d992e62..54caf84 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -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); diff --git a/src/eval/eval.c b/src/eval/eval.c index facb31a..937296e 100644 --- a/src/eval/eval.c +++ b/src/eval/eval.c @@ -30,8 +30,9 @@ static int eval_unop(const struct unop_node *un_op) return -eval_ast(un_op->tree); case UNOP_FACT: return my_fact(eval_ast(un_op->tree)); + default: + UNREACHABLE(); } - UNREACHABLE(); } 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); case BINOP_POW: return my_pow(eval_ast(bin_op->lhs), eval_ast(bin_op->rhs)); + default: + UNREACHABLE(); } #undef EVAL_OP - UNREACHABLE(); } int eval_ast(const struct ast_node *ast) diff --git a/src/parse/climbing_parse.c b/src/parse/climbing_parse.c index 351d2eb..6852634 100644 --- a/src/parse/climbing_parse.c +++ b/src/parse/climbing_parse.c @@ -37,7 +37,7 @@ static void skip_whitespace(const 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) 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(); } -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) 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); 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)); if (!rhs) { @@ -227,7 +227,7 @@ static struct ast_node *parse_operand(const char **input) int val = 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 eat_char(input); diff --git a/src/parse/recursive_parse.c b/src/parse/recursive_parse.c index c24e905..e033cee 100644 --- a/src/parse/recursive_parse.c +++ b/src/parse/recursive_parse.c @@ -61,7 +61,7 @@ struct ast_node *recursive_parse(const char *input) return ast; } -static enum binop_kind char_to_binop(char c) +static enum op_kind char_to_binop(char c) { switch (c) { @@ -94,7 +94,7 @@ static struct ast_node *parse_expression(const char **input) 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); @@ -130,7 +130,7 @@ static struct ast_node *parse_term(const char **input) 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); @@ -151,7 +151,7 @@ static struct ast_node *parse_term(const char **input) return lhs; } -static enum unop_kind char_to_unop(char c) +static enum op_kind char_to_unop(char c) { switch (c) { @@ -170,7 +170,7 @@ static struct ast_node *parse_factor(const char **input) skip_whitespace(input); // Whitespace is not significant 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); @@ -197,7 +197,7 @@ static struct ast_node *parse_power(const char **input) 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);