From 85b4a72c38f1d5f2efe5d21d7df1dd5fc962b976 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 27 Dec 2022 22:50:38 +0100 Subject: [PATCH] bracket supported --- parser.c | 89 +++++++++++++++++--------------------------------------- 1 file changed, 27 insertions(+), 62 deletions(-) diff --git a/parser.c b/parser.c index e7c9188..2ed8d9d 100644 --- a/parser.c +++ b/parser.c @@ -67,7 +67,7 @@ keyword_t functions[NB_FUNCTIONS] = { element_t *parser (char *str, char **next) { element_t *root = NULL; - int i, j; + int i; VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n")); @@ -84,84 +84,49 @@ element_t *parser (char *str, char **next) continue; } - /* skip commas */ - - if (*str == ',') { - VERBOSE (DEBUG, PRINTOUT ("start processing coma\n")); - str++; - if (root == NULL) { - return parser (str, &str); - } else if (root->func != Set) { - new = newelement (Set, MAX_OPERANDS); - if (new == NULL) { - return ERROR_OP; - } - new->ops[0] = root; - root = new; - VERBOSE (DEBUG, PRINTOUT ("end processing first coma\n")); - } else /* if (root->func == Set) */ { - new = parser (str, &str); - if (!found){ - return ERROR_OP; - } - for (i = 0; i < root->nbops; i++) { - if (root->ops[i] == NULL) { - root->ops[i] = new; - found = 1; - } - } - if (!found){ - return ERROR_OP; - } - VERBOSE (DEBUG, PRINTOUT ("end processing other coma\n")); - } - continue; - } - - /* check for parent */ + /* check for open bracket */ if (*str == '(') { VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n")); - new = parser (str + 1, &str); - if (new == ERROR_OP) { - return ERROR_OP; - } if (root) { - for (i = 0, j = 0; i < root->nbops; i++) { - if (root->ops[i] == NULL) { - if (new->func == Set) { - root->ops[i] = new->ops[j++]; - if (new->ops[j] == NULL) { - found = 1; - break; - } - } else { + do { + found = 0; + new = parser (str + 1, &str); + if (new == ERROR_OP) { + return ERROR_OP; + } + for (i = 0; i < root->nbops; i++) { + if (root->ops[i] == NULL) { root->ops[i] = new; found = 1; break; } } - } - if (!found) { + if (!found) { + return ERROR_OP; + } + } while (*str == ','); + } else { + root = newelement (Val, 1); + if (root == NULL) { return ERROR_OP; } - } else { - if (new->func != Set) { - root = new; - } else { + new = parser (str + 1, &str); + if ((new == ERROR_OP) || (*str == ',')) { return ERROR_OP; } + root->ops[0] = new; } + str++; VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n")); - //if (next != NULL) { - // *next = str; - //} - //return root; continue; } - if (*str == ')') { + + /* check for closing bracket or koma */ + + if ((*str == ')') || (*str == ',')) { if (next != NULL) { - *next = str + 1; + *next = str; } return root; } @@ -326,7 +291,7 @@ void print_element (element_t *root, int level) PRINTOUT ("Function: %s\n", func); - if (root->func == Val) { + if ((root->func == Val) && (root->ops[0] == NULL)) { for (i = 0; i < level; i++) { PRINTOUT (" "); } -- 2.30.2