bracket supported
[calc.git] / parser.c
index 2190178ecba23fc5898a4cac2a2dc22db343f982..2ed8d9d845dd4f6749b021f582703fe9a92a168c 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -45,7 +45,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "+ ",  Add, 2, 1 },
     { "+\t", Add, 2, 1 },
     { "- ",  Sub, 2, 1 },
-    { "- ",  Sub, 2, 1 },
+    { "-\t", Sub, 2, 1 },
     { "*",   Mul, 2, 1 },
     { "/",   Div, 2, 1 },
     { "^",   Pow, 2, 1 }
@@ -67,13 +67,15 @@ 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"));
 
     /* main loop */
     while (*str != '\0') {
         int found = 0;
         element_t *new = NULL;
-        VERBOSE (DEBUG, PRINTOUT ("Processing: %s\n", str));
+        VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
 
         /* skip spaces and tabs */
 
@@ -82,55 +84,51 @@ element_t *parser (char *str, char **next)
             continue;
         }
 
-        /* skip commas */
-
-        if (*str == ',') {
-            if (root == NULL) {
-                return ERROR_OP;
-            } else if (root->func != Set) {
-                new = newelement (Set, MAX_OPERANDS);
-                new->ops[0] = root;
-                root = new;
-            }
-            str++;
-            continue;
-        }
-
-        /* check for parent */
+        /* check for open bracket */
 
-        if (*str == ')') {
-            if (next != NULL) {
-                *next = str + 1;
-            }
-            return root;
-        }
         if (*str == '(') {
-            new = parser (str + 1, &str);
-            if (new == (void *)(-1)) {
-                return new;
-            }
+            VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
             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) {
+                        return ERROR_OP;
+                    }
+                } while (*str == ',');
+            } else {
+                root = newelement (Val, 1);
+                if (root == NULL) {
+                    return ERROR_OP;
                 }
-                if (!found) {
+                new = parser (str + 1, &str);
+                if ((new == ERROR_OP) || (*str == ',')) {
                     return ERROR_OP;
                 }
-            } else {
-                return root;
+                root->ops[0] = new;
+            }
+            str++;
+            VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
+            continue;
+        }
+
+        /* check for closing bracket or koma */
+
+        if ((*str == ')') || (*str == ',')) {
+            if (next != NULL) {
+                *next = str;
             }
+            return root;
         }
 
         /* look for operators */
@@ -138,34 +136,41 @@ element_t *parser (char *str, char **next)
         for (i = 0; i < NB_OPERATORS; i++) {
             keyword_t *operator = operators + i;
             if (codecmp (operator->keyword, str) == 0) {
-                if ((root) && (root->func == Val)) {
-                    VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", operator->func));
+                VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
+                str += operator->offset;
+                if ((root) && (root->func != Set)) {
+                    VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
                     new = newelement (operator->func, operator->nbops);
                     if (new == NULL) {
                         return ERROR_OP;
                     }
                     new->ops[0] = root;
                     root = new;
-                 } else {
+                    new = parser (str, &str);
+                    if (new == ERROR_OP) {
+                        return ERROR_OP;
+                    }
+                    root->ops[1] = new;
+                } else {
                     return ERROR_OP;
                 }
-                str += operator->offset;
                 found = 1;
+                VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
                 break;
             }
         }
         if (found) {
-            VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
             continue;
         }
+
         /* look for functions */
 
         for (i = 0; i < NB_FUNCTIONS; i++) {
             keyword_t *function = functions + i;
             if (codecmp (function->keyword, str) == 0) {
+                VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
                 if (root == NULL) {
-                    VERBOSE (DEBUG, PRINTOUT ("Func: %d\n", function->func));
+                    VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
                     new = newelement (function->func, function->nbops);
                     if (new == NULL) {
                         return ERROR_OP;
@@ -176,11 +181,11 @@ element_t *parser (char *str, char **next)
                 }
                 str += function->offset;
                 found = 1;
+                VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
                 break;
             }
         }
         if (found) {
-            VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
             continue;
         }
 
@@ -189,7 +194,7 @@ element_t *parser (char *str, char **next)
         if (((*str == '-') || (*str == '+')) &&
             ((*(str + 1) >= '0') && (*(str + 1) <= '9')) &&
             ((root) && (root->func == Val))) {
-            VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", Add));
+            VERBOSE (INFO, PRINTOUT ("Oper: %d\n", Add));
             new = newelement (Add, 2);
             if (new == NULL) {
                 return ERROR_OP;
@@ -200,17 +205,17 @@ element_t *parser (char *str, char **next)
 
         /* look for number */
 
-        if (((*str >= '0') && (*str <= '9')) ||
-            (*str == '.') || (*str == '-') ||(*str == '+')) {
+        if (((*str >= '0') && (*str <= '9')) || (*str == '.')) {
+            VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
             char *pt;
             float value = strtof (str, &pt);
-            VERBOSE (DEBUG, PRINTOUT ("Value: %f\n", value));
+            VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
             if (str != pt) {
                 new = newelement (Val, 1);
-                new->value = value;
                 if (new == NULL) {
                     return ERROR_OP;
                 }
+                new->value = value;
                 if (root == NULL) {
                     root = new;
                 } else {
@@ -236,6 +241,7 @@ element_t *parser (char *str, char **next)
                 str = pt;
                 found = 1;
             }
+            VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
         }
 
         /* error */
@@ -246,6 +252,9 @@ element_t *parser (char *str, char **next)
 
     }
 
+    if (next != NULL) {
+        *next = str;
+    }
     return root;
 }
 
@@ -256,9 +265,10 @@ void print_element (element_t *root, int level)
     char *func = NULL;
     int i;
 
-    if (root == NULL)
+    if ((root == NULL) || (root == ERROR_OP)) {
         return;
-    
+    }
+
     for (i = 0; i < level; i++) {
         PRINTOUT (" ");
     }
@@ -281,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 (" ");
         }