From d2ff8478555f878bdeb7f77b00fb5c77dfe8d05b Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Thu, 29 Dec 2022 21:21:50 +0100 Subject: [PATCH] remove duplicated code --- parser.c | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/parser.c b/parser.c index 75cc294..fdabef2 100644 --- a/parser.c +++ b/parser.c @@ -69,6 +69,28 @@ keyword_t functions[NB_FUNCTIONS] = { { "log", Log, 1, 3, 5} }; +/* subparser function */ + +element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio) +{ + element_t *new = newelement (func, nbops, prio); + if (new == NULL) { + return ERROR_OP; + } + new->ops[0] = *proot; + new->ops[1] = parser (*pstr, pstr, new->prio); + if (new->ops[1] == ERROR_OP) { + return ERROR_OP; + } + *proot = newelement (Val, 1, 4); + if (*proot == ERROR_OP) { + return ERROR_OP; + } + (*proot)->ops[0] = new; + + return *proot; +} + /* parser function */ element_t *parser (char *str, char **next, int prio) @@ -152,20 +174,9 @@ element_t *parser (char *str, char **next, int prio) } str += operator->offset; VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func)); - new = newelement (operator->func, operator->nbops, operator->prio); - if (new == NULL) { - return ERROR_OP; - } - new->ops[0] = root; - new->ops[1] = parser (str, &str, new->prio); - if (new->ops[1] == ERROR_OP) { - return ERROR_OP; - } - root = newelement (Val, 1, 4); - if (root == ERROR_OP) { + if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) { return ERROR_OP; } - root->ops[0] = new; } else { return ERROR_OP; } @@ -228,20 +239,9 @@ element_t *parser (char *str, char **next, int prio) *next = str; return root; } - new = newelement (Add, 2, 1); - if (new == NULL) { - return ERROR_OP; - } - new->ops[0] = root; - new->ops[1] = parser (str, &str, new->prio); - if (new->ops[1] == ERROR_OP) { - return ERROR_OP; - } - root = newelement (Val, 1, 4); - if (root == ERROR_OP) { + if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) { return ERROR_OP; } - root->ops[0] = new; } else { return ERROR_OP; } -- 2.30.2