From 0b9cc9b0959ca362b29cd3bf25907567d6a5feaf Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 27 Dec 2022 23:33:39 +0100 Subject: [PATCH] all number supported --- parser.c | 56 +++++++++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/parser.c b/parser.c index 2ed8d9d..8ddd83a 100644 --- a/parser.c +++ b/parser.c @@ -13,10 +13,16 @@ int codecmp (char *ref, char *str) int sig; while (*ref != '\0') { - sig = *str++ - *ref++; + if (*ref == '\t') { + sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9')); + } else { + sig = *str - *ref; + } if (sig != 0) { return (sig > 0) ? 1 : -1; } + str++; + ref++; } return 0; @@ -39,12 +45,10 @@ element_t *newelement (func_t function, int nbops) /* functions */ -#define NB_OPERATORS 7 +#define NB_OPERATORS 5 keyword_t operators[NB_OPERATORS] = { - { "+ ", Add, 2, 1 }, - { "+\t", Add, 2, 1 }, - { "- ", Sub, 2, 1 }, + { "+\t", Add, 2, 1 }, { "-\t", Sub, 2, 1 }, { "*", Mul, 2, 1 }, { "/", Div, 2, 1 }, @@ -189,23 +193,10 @@ element_t *parser (char *str, char **next) continue; } - /* last attend to detect addition and substraction */ - - if (((*str == '-') || (*str == '+')) && - ((*(str + 1) >= '0') && (*(str + 1) <= '9')) && - ((root) && (root->func == Val))) { - VERBOSE (INFO, PRINTOUT ("Oper: %d\n", Add)); - new = newelement (Add, 2); - if (new == NULL) { - return ERROR_OP; - } - new->ops[0] = root; - root = new; - } - /* look for number */ - if (((*str >= '0') && (*str <= '9')) || (*str == '.')) { + if (((*str >= '0') && (*str <= '9')) || + (*str == '.') || (*str == '+') || (*str == '-')) { VERBOSE (DEBUG, PRINTOUT ("start processing value\n")); char *pt; float value = strtof (str, &pt); @@ -218,25 +209,20 @@ element_t *parser (char *str, char **next) new->value = value; if (root == NULL) { root = new; - } else { - if (root->func == Val) { - element_t *set = newelement (Set, MAX_OPERANDS); - if (set == NULL) { + } else if (root->func == Val) { + if ((*str == '+') || (*str == '-')) { + element_t *add = newelement (Add, 2); + if (add == NULL) { return ERROR_OP; } - set->ops[0] = root; - root = set; - } - for (i = 0; i < root->nbops; i++) { - if (root->ops[i] == NULL) { - root->ops[i] = new; - found = 1; - break; - } - } - if (!found) { + add->ops[0] = root; + add->ops[1] = new; + root = add; + } else { return ERROR_OP; } + } else { + return ERROR_OP; } str = pt; found = 1; -- 2.30.2