From 3b51111ff5e8a1b580804a4cbcaf3a69ee9066dc Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 30 Oct 2020 19:44:49 +0100 Subject: [PATCH] evalexpr: parse: clean-up character conversion Differentiate the postfix and prefix cases to allow overlap in operators used for each kind of operation --- src/parse/climbing_parse.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/parse/climbing_parse.c b/src/parse/climbing_parse.c index 0a8486e..09af8be 100644 --- a/src/parse/climbing_parse.c +++ b/src/parse/climbing_parse.c @@ -46,11 +46,19 @@ static enum op_kind char_to_binop(char c) UNREACHABLE(); } -// Only works because no overlap between prefix and postfix operators -static enum op_kind char_to_unop(char c) +static enum op_kind char_to_prefix(char c) { 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_PREFIX && c == ops[i].op[0]) + return ops[i].kind; + + UNREACHABLE(); +} + +static enum op_kind char_to_postfix(char c) +{ + for (size_t i = 0; i < ARR_SIZE(ops); ++i) + if (ops[i].fix == OP_POSTFIX && c == ops[i].op[0]) return ops[i].kind; UNREACHABLE(); @@ -159,7 +167,7 @@ static bool update_op(enum op_kind *op, const char **input) } if (is_postfix(c)) { - *op = char_to_unop(c); + *op = char_to_postfix(c); return true; } @@ -231,8 +239,8 @@ static struct ast_node *parse_operand(const char **input) int val = 0; if (is_prefix(*input[0])) { - enum op_kind op = char_to_unop(*input[0]); - // Remove the parenthesis + enum op_kind op = char_to_prefix(*input[0]); + // Remove the operator eat_char(input); ast = climbing_parse_internal(input, next_prec(op));