all number supported
[calc.git] / parser.c
index e7c91888f15cec4380aabf2d6b65537a06ca200a..8ddd83ad62e14a80b1f152d00bb11ff7234c723f 100644 (file)
--- 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 },
@@ -67,7 +71,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 +88,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;
         }
@@ -224,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);
@@ -253,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;
@@ -326,7 +277,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 (" ");
         }