clean help message
[calc.git] / parser.c
index ad246cdcde676807a7769893dcb82b304af347e8..53d6e6c78efdea14be71bc4ddc216ddbcc005687 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -70,7 +70,6 @@ void delelement (element_t *root)
 /* functions */
 
 #define NB_OPERATORS 14
-
 keyword_t operators[NB_OPERATORS] = {
     { "+\t", Add, 2, 1, 1},
     { "-\t", Sub, 2, 1, 1},
@@ -102,7 +101,7 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "disp", Disp, 0, 4, 9},
     { "quit", Quit, 0, 4, 9},
     { "help", Help, 0, 4, 9},
-    { "!",    Not, 1, 1, 5}
+    { "!",    Not, 1, 1, 6}
 };
 
 #define NB_CONSTANTS 3
@@ -122,6 +121,10 @@ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, in
     }
     new->ops[0] = *proot;
     new->ops[1] = parser (*pstr, pstr, new->prio);
+    if ((new->ops[1] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
+        delelement (new->ops[1]);
+        new->ops[1] = ERROR_OP;
+    }
     if (new->ops[1] == ERROR_OP) {
         delelement (new);
         *proot = NULL;
@@ -167,7 +170,11 @@ element_t *parser (char *str, char **next, int prio)
                 do {
                     found = 0;
                     new = parser (str + 1, &str, 0);
-                    if (new == ERROR_OP) {
+                    if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+                        delelement (new);
+                        new = ERROR_OP;
+                    }
+                    if ((new == NULL) || (new == ERROR_OP)) {
                         delelement (root);
                         return ERROR_OP;
                     }
@@ -190,13 +197,21 @@ element_t *parser (char *str, char **next, int prio)
                     return ERROR_OP;
                 }
                 new = parser (str + 1, &str, 0);
-                if ((new == ERROR_OP) || (*str == ',')) {
+                if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+                    delelement (new);
+                    new = ERROR_OP;
+                }
+                if ((new == NULL) || (new == ERROR_OP) || (*str == ',')) {
                     delelement (new);
                     delelement (root);
                     return ERROR_OP;
                 }
                 root->ops[0] = new;
             }
+            if (*str != ')') {
+                delelement (root);
+                return ERROR_OP;
+            }
             str++;
             VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
             continue;
@@ -310,13 +325,28 @@ element_t *parser (char *str, char **next, int prio)
             double value = strtod (str, &pt);
             VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
             if (str != pt) {
-                if (root == NULL) {
+                if ((root == NULL) || (root->prio == 6)) {
                     new = newelement (Val, 1, 5);
                     if (new == NULL) {
                         return ERROR_OP;
                     }
                     new->value = value;
-                    root = new;
+                    if (root == NULL) {
+                        root = new;
+                    } else {
+                        for (i = 0; i < root->nbops; i++) {
+                            if (root->ops[i] == NULL) {
+                                root->ops[i] = new;
+                                found = 1;
+                                break;
+                            }
+                        }
+                        if (!found) {
+                            delelement (new);
+                            delelement (root);
+                            return ERROR_OP;
+                        }
+                    }
                     str = pt;
                 } else if ((*str == '+') || (*str == '-')) {
                     if ((prio) && (prio > 1)) {
@@ -465,6 +495,8 @@ void help (void)
     fprintf (stdout, " + - * / %% ^\n\n");
     fprintf (stdout, "camparison operators:\n");
     fprintf (stdout, " == != >= <= > <\n\n");
+    fprintf (stdout, "logical operators:\n");
+    fprintf (stdout, " & | !\n\n");
     fprintf (stdout, "supported functions:\n");
     fprintf (stdout, " pow sqrt cos sin atan log exp\n\n");
     fprintf (stdout, "storage functions:\n");