Merge branch 'master' of https://secure.softndesign.org/git/calc
[calc.git] / parser.c
index 13efac35bdac40ad675babcf184720e71a833323..b7b74e230834129a77d98175ce20769930148c10 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -45,6 +45,21 @@ element_t *newelement (func_t function, int nbops, int prio)
     return new;
 }
 
+/* desallocate element */
+
+void delelement (element_t *root)
+{
+    int i;
+    if ((root != NULL) && (root != ERROR_OP)) {
+        for (i = 0; i < root->nbops; i++) {
+            if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
+                delelement (root->ops[i]);
+            }
+        }
+        free (root);
+    }
+}
+
 /* functions */
 
 #define NB_OPERATORS 6
@@ -58,7 +73,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "^",   Pow, 2, 1, 4}
 };
 
-#define NB_FUNCTIONS 7
+#define NB_FUNCTIONS 9
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -66,7 +81,9 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "sin",  Sin, 1, 3, 5},
     { "atan", Atn, 1, 4, 5},
     { "exp",  Exp, 1, 3, 5},
-    { "log",  Log, 1, 3, 5}
+    { "log",  Log, 1, 3, 5},
+    { "quit", Qui, 0, 4, 5},
+    { "help", Hel, 0, 4, 5}
 };
 
 /* subparser function */
@@ -80,10 +97,13 @@ 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] == ERROR_OP) {
+        delelement (new);
+        *proot = NULL;
         return ERROR_OP;
     }
     *proot = newelement (Val, 1, 5);
-    if (*proot == ERROR_OP) {
+    if (*proot == NULL) {
+        delelement (new);
         return ERROR_OP;
     }
     (*proot)->ops[0] = new;
@@ -122,6 +142,7 @@ element_t *parser (char *str, char **next, int prio)
                     found = 0;
                     new = parser (str + 1, &str, 0);
                     if (new == ERROR_OP) {
+                        delelement (root);
                         return ERROR_OP;
                     }
                     for (i = 0; i < root->nbops; i++) {
@@ -132,6 +153,8 @@ element_t *parser (char *str, char **next, int prio)
                         }
                     }
                     if (!found) {
+                        delelement (new);
+                        delelement (root);
                         return ERROR_OP;
                     }
                 } while (*str == ',');
@@ -142,6 +165,8 @@ element_t *parser (char *str, char **next, int prio)
                 }
                 new = parser (str + 1, &str, 0);
                 if ((new == ERROR_OP) || (*str == ',')) {
+                    delelement (new);
+                    delelement (root);
                     return ERROR_OP;
                 }
                 root->ops[0] = new;
@@ -175,6 +200,7 @@ element_t *parser (char *str, char **next, int prio)
                     str += operator->offset;
                     VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
                     if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
+                        delelement (root);
                         return ERROR_OP;
                     }
                 } else if (*str == '-') {
@@ -208,7 +234,8 @@ element_t *parser (char *str, char **next, int prio)
                         return ERROR_OP;
                     }
                     root = new;
-                 } else {
+                } else {
+                    delelement (root);
                     return ERROR_OP;
                 }
                 str += function->offset;
@@ -227,7 +254,7 @@ element_t *parser (char *str, char **next, int prio)
             (*str == '.') || (*str == '+') || (*str == '-')) {
             VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
             char *pt;
-            float value = strtof (str, &pt);
+            double value = strtod (str, &pt);
             VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
             if (str != pt) {
                 if (root == NULL) {
@@ -246,12 +273,15 @@ element_t *parser (char *str, char **next, int prio)
                             return root;
                         }
                         if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
+                            delelement (root);
                             return ERROR_OP;
                         }
                     } else {
+                        delelement (root);
                         return ERROR_OP;
                     }
                 } else {
+                    delelement (root);
                     return ERROR_OP;
                 }
                 found = 1;
@@ -262,6 +292,7 @@ element_t *parser (char *str, char **next, int prio)
         /* error */
 
         if (!found) {
+            delelement (root);
             return ERROR_OP;
         }
 
@@ -270,6 +301,7 @@ element_t *parser (char *str, char **next, int prio)
     if (next != NULL) {
         *next = str;
     }
+
     return root;
 }
 
@@ -290,6 +322,7 @@ void print_element (element_t *root, int level)
 
     switch (root->func) {
     case Val: func = "Value"; break;
+    case Sig: func = "Sign"; break;
     case Add: func = "Addition"; break;
     case Sub: func = "Subtraction"; break;
     case Mul: func = "Multiplication"; break;
@@ -302,7 +335,8 @@ void print_element (element_t *root, int level)
     case Atn: func = "Arc Tangent"; break;
     case Log: func = "Logarithm"; break;
     case Exp: func = "Exponantial"; break;
-    case Sig: func = "Sign"; break;
+    case Qui: func = "Quit"; break;
+    case Hel: func = "Help"; break;
     }
 
     PRINTOUT ("Function: %s\n", func);
@@ -319,11 +353,32 @@ void print_element (element_t *root, int level)
     }
 }
 
-#define MASK_SUB 0x1
-#define MASK_DIV 0x2
+/* quit function */
+
+void quit (void)
+{
+    PRINTOUT ("bye\n");
+    exit (0);
+}
+
+/* help message */
+
+void help (void)
+{
+    PRINTOUT ("calc is a simple calculator\n\n");
+    PRINTOUT ("supported operators:\n");
+    PRINTOUT (" + - * / % ^\n\n");
+    PRINTOUT ("supported functions:\n");
+    PRINTOUT (" pow sqrt cos sin atan log exp\n\n");
+    PRINTOUT ("miscellaneous functions:\n");
+    PRINTOUT (" quit help\n");
+}
 
 /* evaluate element tree */
 
+#define MASK_SUB 0x1
+#define MASK_DIV 0x2
+
 double evaluate_element (element_t *root, char mask)
 {
     double op0 = 0, op1 = 0;
@@ -384,6 +439,10 @@ double evaluate_element (element_t *root, char mask)
             VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
             return 0;
         }
+        break;
+    case Qui:
+    case Hel:
+        break;
     }
 
     switch (root->func) {
@@ -401,6 +460,8 @@ double evaluate_element (element_t *root, char mask)
     case Atn: return atan (op0);
     case Log: return log (op0);
     case Exp: return exp (op0);
+    case Qui: quit (); break;
+    case Hel: help (); break;
     }
 
     return 0;