embellish usage display
[calc.git] / parser.c
index 844e9417f18b19d2ec4b32780e514dda6338c224..7880acfa156c049e13c778cc5c280faec0f3c412 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -1,18 +1,28 @@
-#include <malloc.h>
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
+#include "alloc.h"
+#include "argument.h"
 #include "debug.h"
+#include "element.h"
+#include "format.h"
+#include "program.h"
+#include "stack.h"
+#include "storage.h"
 
 #include "parser.h"
 
+/* external definition */
+
+extern void history ();
+
 /* global variables */
 
 double answer = 0;
 
-#define STORAGE_SIZE 10
-double storage[STORAGE_SIZE] = {0};
+double anglefactor = 1;
 
 /* compare codes */
 
@@ -36,99 +46,23 @@ int codecmp (char *ref, char *str)
     return 0;
 }
 
-/* allocate new element */
-
-element_t *newelement (func_t function, int nbops, int prio)
-{
-    element_t *new = (element_t *) calloc (1, sizeof (element_t));
-    if (new == NULL) {
-        VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
-        return NULL;
-    }
-    new->func = function;
-    new->nbops = nbops;
-    new->prio = 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 12
-
-keyword_t operators[NB_OPERATORS] = {
-    { "+\t", Add, 2, 1, 1},
-    { "-\t", Sub, 2, 1, 1},
-    { "*",   Mul, 2, 1, 2},
-    { "/",   Div, 2, 1, 2},
-    { "%",   Mod, 2, 1, 3},
-    { "^",   Pow, 2, 1, 4},
-    { "==",  Equal, 2, 2, 6},
-    { "!=",  Diff, 2, 2, 6},
-    { ">=",  Ge, 2, 2, 6},
-    { "<=",  Le, 2, 2, 6},
-    { ">",   Gt, 2, 1, 6},
-    { "<",   Lt, 2, 1, 6}
-};
-
-#define NB_FUNCTIONS 12
-keyword_t functions[NB_FUNCTIONS] = {
-    { "sqrt", Sqr, 1, 4, 5},
-    { "pow",  Pow, 2, 3, 5},
-    { "cos",  Cos, 1, 3, 5},
-    { "sin",  Sin, 1, 3, 5},
-    { "atan", Atan, 1, 4, 5},
-    { "exp",  Exp, 1, 3, 5},
-    { "log",  Log, 1, 3, 5},
-    { "sto",  Store, 2, 3, 5},
-    { "rcl",  Recall, 1, 3, 5},
-    { "disp", Disp, 0, 4, 5},
-    { "quit", Quit, 0, 4, 5},
-    { "help", Help, 0, 4, 5}
-};
-
-#define NB_CONSTANTS 3
-keyword_t constants[NB_CONSTANTS] = {
-    { "ans", Ans, 0, 3, 5},
-    { "e",   E, 0, 1, 5},
-    { "pi",  Pi, 0, 2, 5}
-};
-
 /* subparser function */
 
 element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
 {
     element_t *new = newelement (func, nbops, prio);
-    if (new == NULL) {
-        return ERROR_OP;
-    }
     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;
         return ERROR_OP;
     }
     *proot = newelement (Val, 1, 5);
-    if (*proot == NULL) {
-        delelement (new);
-        return ERROR_OP;
-    }
     (*proot)->ops[0] = new;
 
     return *proot;
@@ -139,6 +73,7 @@ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, in
 element_t *parser (char *str, char **next, int prio)
 {
     element_t *root = NULL;
+    char *string = str;
     int i;
 
     VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
@@ -149,6 +84,15 @@ element_t *parser (char *str, char **next, int prio)
         element_t *new = NULL;
         VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
 
+        /* end without printing */
+
+        if (*str == ';') {
+            if (root) {
+                root->hidden = 1;
+            }
+            break;
+        }
+
         /* skip spaces and tabs */
 
         if ((*str == ' ') || (*str == '\t')) {
@@ -156,6 +100,45 @@ element_t *parser (char *str, char **next, int prio)
             continue;
         }
 
+        /* check for open brace */
+
+        if (*str == '{') {
+            VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
+            if (root != NULL) {
+                delelement (root);
+                return ERROR_OP;
+            }
+            root = newelement (Code, 0, 5);
+
+            do {
+                new = parser (str + 1, &str, 0);
+                if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+                    delelement (new);
+                    new = ERROR_OP;
+                }
+                if (new == ERROR_OP) {
+                    delelement (root);
+                    return ERROR_OP;
+                }
+                element_t *newcode = newelement (Code, root->nbops + 1, 5);
+                for (i = 0; i < root->nbops; i++) {
+                    newcode->ops[i] = root->ops[i];
+                    root->ops[i] = NULL;
+                }
+                newcode->ops[root->nbops] = new;
+                delelement (root);
+                root = newcode;
+            } while (*str == ',');
+
+            if (*str != '}') {
+                delelement (root);
+                return ERROR_OP;
+            }
+            str++;
+            VERBOSE (DEBUG, fprintf (stdout, "stop processing brace\n"));
+            continue;
+        }
+
         /* check for open bracket */
 
         if (*str == '(') {
@@ -164,7 +147,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;
                     }
@@ -183,25 +170,34 @@ element_t *parser (char *str, char **next, int prio)
                 } while (*str == ',');
             } else {
                 root = newelement (Val, 1, 5);
-                if (root == NULL) {
-                    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;
         }
 
-        /* check for closing bracket or koma */
+        /* check for closing bracket, closing brace or koma */
 
-        if ((*str == ')') || (*str == ',')) {
+        if ((*str == ')') || (*str == '}') || (*str == ',')) {
+            if (prio == -9) {
+                delelement (root);
+                return ERROR_OP;
+            }
             if (next != NULL) {
                 *next = str;
             }
@@ -214,7 +210,11 @@ element_t *parser (char *str, char **next, int prio)
             keyword_t *operator = operators + i;
             if (codecmp (operator->keyword, str) == 0) {
                 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
-                if (root) {
+                if ((root) && (root->prio == 9)) {
+                    VERBOSE (DEBUG, fprintf (stdout, "terminal function (%d)\n", root->func));
+                    delelement (root);
+                    return ERROR_OP;
+                } else if (root) {
                     if ((prio) && (prio > operator->prio)) {
                         VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
                         *next = str;
@@ -227,11 +227,7 @@ element_t *parser (char *str, char **next, int prio)
                         return ERROR_OP;
                     }
                 } else if (*str == '-') {
-                    new = newelement (Sig, 1, 9);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
-                    root = new;
+                    root = newelement (Sig, 1, 6);
                 } else {
                     return ERROR_OP;
                 }
@@ -252,11 +248,7 @@ element_t *parser (char *str, char **next, int prio)
                 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
                 if (root == NULL) {
                     VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
-                    new = newelement (function->func, function->nbops, function->prio);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
-                    root = new;
+                    root = newelement (function->func, function->nbops, function->prio);
                 } else {
                     delelement (root);
                     return ERROR_OP;
@@ -279,11 +271,7 @@ element_t *parser (char *str, char **next, int prio)
                 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
                 if (root == NULL) {
                     VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
-                    new = newelement (constant->func, constant->nbops, constant->prio);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
-                    root = new;
+                    root = newelement (constant->func, constant->nbops, constant->prio);
                 } else {
                     delelement (root);
                     return ERROR_OP;
@@ -300,39 +288,48 @@ element_t *parser (char *str, char **next, int prio)
 
         /* look for number */
 
-        if (((*str >= '0') && (*str <= '9')) ||
-            (*str == '.') || (*str == '+') || (*str == '-')) {
-            VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
-            char *pt;
-            double value = strtod (str, &pt);
-            VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
-            if (str != pt) {
+        VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
+        char *pt;
+        double value = (get_ibase () == 10) ? strtod (str, &pt) : strtol (str, &pt, get_ibase ());
+        VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
+        if (str != pt) {
+            if ((root == NULL) || (root->prio == 6)) {
+                new = newelement (Val, 1, 5);
+                new->value = value;
                 if (root == NULL) {
-                    new = newelement (Val, 1, 5);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
-                    new->value = value;
                     root = new;
-                    str = pt;
-                } else if ((*str == '+') || (*str == '-')) {
-                    if ((prio) && (prio > 1)) {
-                        VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
-                        *next = str;
-                        return root;
+                } else {
+                    for (i = 0; i < root->nbops; i++) {
+                        if (root->ops[i] == NULL) {
+                            root->ops[i] = new;
+                            found = 1;
+                            break;
+                        }
                     }
-                    if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
+                    if (!found) {
+                        delelement (new);
                         delelement (root);
                         return ERROR_OP;
                     }
-                } else {
+                }
+                str = pt;
+            } else if ((*str == '+') || (*str == '-')) {
+                if ((prio) && (prio > 1)) {
+                    VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
+                    *next = str;
+                    return root;
+                }
+                if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
                     delelement (root);
                     return ERROR_OP;
                 }
-                found = 1;
+            } else {
+                delelement (root);
+                return ERROR_OP;
             }
-            VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
+            found = 1;
         }
+        VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
 
         /* error */
 
@@ -347,6 +344,11 @@ element_t *parser (char *str, char **next, int prio)
         *next = str;
     }
 
+    /* save string */
+    if (root != NULL) {
+        root->string = string;
+    }
+
     return root;
 }
 
@@ -362,7 +364,7 @@ void print_element (element_t *root, int level)
     }
 
     for (i = 0; i < level; i++) {
-        fprintf (stdout, " ");
+        printf (" ");
     }
 
     switch (root->func) {
@@ -377,14 +379,28 @@ void print_element (element_t *root, int level)
     case Sqr: func = "Square Root"; break;
     case Cos: func = "Cosine"; break;
     case Sin: func = "Sine"; break;
+    case Tan: func = "Tangent"; break;
+    case Acos: func = "Arc Cosine"; break;
+    case Asin: func = "Arc Sine"; break;
     case Atan: func = "Arc Tangent"; break;
-    case Log: func = "Logarithm"; break;
+    case Ln: func = "Logarithm (natural)"; break;
+    case Log: func = "Logarithm (10 base)"; break;
     case Exp: func = "Exponantial"; break;
+    case Erfc: func = "Complementary Error Function"; break;
+    case Erf: func = "Error Function"; break;
+    case Abs: func = "Absolute value"; break;
+    case Ceil: func = "Ceil value"; break;
+    case Floor: func = "Floor value"; break;
     case Store: func = "Store"; break;
     case Recall: func = "Recall"; break;
+    case Inc: func = "Increase"; break;
+    case Dec: func = "Decrease"; break;
     case Disp: func = "Display"; break;
+    case Memory: func = "Memory"; break;
+    case Clear: func = "Clear"; break;
     case Quit: func = "Quit"; break;
     case Help: func = "Help"; break;
+    case History: func = "History"; break;
     case Ans: func = "Ans"; break;
     case Pi: func = "Pi"; break;
     case E: func = "E"; break;
@@ -394,15 +410,48 @@ void print_element (element_t *root, int level)
     case Le: func = "Lesser or equal"; break;
     case Gt: func = "Greater"; break;
     case Lt: func = "Lesser"; break;
+    case And: func = "And"; break;
+    case Or: func = "Or"; break;
+    case Not: func = "Not"; break;
+    case Cond: func = "Condition"; break;
+    case While: func = "While"; break;
+    case Code: func = "Code"; break;
+    case Print: func = "Print"; break;
+    case Prog: func = "Program"; break;
+    case Arg: func = "Argument"; break;
+    case Call: func = "Call"; break;
+    case List: func = "List"; break;
+    case Edit: func = "Edit"; break;
+    case Del: func = "Del"; break;
+    case Get: func = "Get"; break;
+    case Length: func = "Length"; break;
+    case Pop: func = "Pop"; break;
+    case Push: func = "Push"; break;
+    case Put: func = "Put"; break;
+    case Set: func = "Set"; break;
+    case Show: func = "Show"; break;
+    case Max: func = "Maximum"; break;
+    case Mean: func = "Mean"; break;
+    case Median: func = "Median"; break;
+    case Min: func = "Minimum"; break;
+    case Order: func = "Order"; break;
+    case Prod: func = "Product"; break;
+    case Sum: func = "Sum"; break;
+    case Variance: func = "Variance"; break;
+    case Precision: func = "Precision"; break;
+    case Base: func = "Base"; break;
+    case Deg: func = "Degree"; break;
+    case Grad: func = "Gradian"; break;
+    case Rad: func = "Radian"; break;
     }
 
-    fprintf (stdout, "Function: %s\n", func);
+    printf ("Function: %s\n", func);
 
     if ((root->func == Val) && (root->ops[0] == NULL)) {
         for (i = 0; i < level; i++) {
-            fprintf (stdout, " ");
+            printf (" ");
         }
-        fprintf (stdout, "value: %f\n", root->value);
+        printf ("value: %f\n", root->value);
     } else {
         for (i = 0; i < root->nbops; i++) {
             print_element (root->ops[i], level + 1);
@@ -410,43 +459,52 @@ void print_element (element_t *root, int level)
     }
 }
 
-/* storage functions */
+/* While do function */
 
-double store (int index, double value)
+double while_do (element_t *cond, element_t *action)
 {
-    if ((index > 0) && (index <= STORAGE_SIZE)) {
-        storage[index - 1] = value;
-    } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+    double ret = 0;
+    element_t *temp = NULL;
+
+    VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
+    while (1) {
+        VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));
+
+        temp = dupelement (cond);
+        double test = evaluate_element (temp, 0);
+        delelement (temp);
+        if (!test) {
+            break;
+        }
+        if (action) {
+            temp = dupelement (action);
+            ret = evaluate_element (temp, 0);
+            delelement (temp);
+        }
     }
-    return value;
-}
 
-double recall (int index)
-{
-    if ((index > 0) && (index <= STORAGE_SIZE)) {
-        return storage[index - 1];
-    } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
-    }
-    return 0;
+    VERBOSE (DEBUG, fprintf (stdout, "ending while loop\n"));
+
+    return ret;
 }
 
-void display (void)
+/* program function */
+
+double execute_code (element_t **prog, int nbcalls)
 {
+    double ret = 0;
     int i;
-    fprintf (stdout, "storage:");
-    for (i = 0; i < STORAGE_SIZE; i++) {
-        fprintf (stdout, " %g", storage[i]);
+    for (i = 0; i < nbcalls; i++) {
+        ret = evaluate_element (prog[i], 0);
     }
-    fprintf (stdout, "\n");
+    return ret;
 }
 
 /* quit function */
 
 void quit (void)
 {
-    fprintf (stdout, "bye\n");
+    printf ("bye\n");
     exit (0);
 }
 
@@ -454,19 +512,69 @@ void quit (void)
 
 void help (void)
 {
-    fprintf (stdout, "calc is a simple calculator\n\n");
-    fprintf (stdout, "supported operators:\n");
-    fprintf (stdout, " + - * / %% ^\n\n");
-    fprintf (stdout, "camparison 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");
-    fprintf (stdout, " sto rcl\n\n");
-    fprintf (stdout, "miscellaneous functions:\n");
-    fprintf (stdout, " quit help\n\n");
-    fprintf (stdout, "supported constants:\n");
-    fprintf (stdout, " e pi\n");
+    printf ("calc is a simple calculator\n\n");
+    printf ("arithmetic op.:");
+    printf (" + - * / %% ^\n");
+    printf ("comparison op.:");
+    printf (" == != >= <= > <\n");
+    printf ("logical op.:");
+    printf (" & | !\n");
+    printf ("mathematic func.:");
+    printf (" exp ln log pow sqrt\n");
+    printf ("trigonometric func.:");
+    printf (" acos asin atan cos sin tan\n");
+    printf ("error functions:");
+    printf (" erf erfc\n");
+    printf ("miscellaneous func.:");
+    printf (" abs ceil floor\n");
+    printf ("storage func.:");
+    printf (" clear dec disp inc mem rcl sto\n");
+    printf ("control flow prim.:");
+    printf (" cond print while {} ;\n");
+    printf ("program management:");
+    printf (" arg call del edit ls prog\n");
+    printf ("stack management:");
+    printf (" get len pop push put set show\n");
+    printf ("stack func.:");
+    printf (" max mean med min ord prod sum var\n");
+    printf ("control management:");
+    printf (" base format help hist quit\n");
+    printf ("angle management:");
+    printf (" deg grad rad\n");
+    printf ("constants:");
+    printf (" ans e pi\n");
+}
+
+/* format function */
+
+int format (int precision)
+{
+    if (precision > 0) {
+        set_precision (precision);
+        set_format ();
+    } else if (precision != -1) {
+        VERBOSE (WARNING, fprintf (stdout, "error incorrect precision (%d)\n", precision));
+        return 0;
+    }
+    return get_precision ();
+}
+
+/* base function */
+
+void base (int in, int out)
+{
+    if ((in > 0) && (in < 37)) {
+        set_base (in, in);
+        if ((out > 0) && (out < 37)) {
+            set_base (in, out);
+        } else if (out != - 1) {
+            VERBOSE (WARNING, fprintf (stdout, "error incorrect output base (%d)\n", out));
+        }
+    } else if (in != -1 ) {
+        VERBOSE (WARNING, fprintf (stdout, "error incorrect input base (%d)\n", in));
+    } else {
+        printf ("base (I/O): %s\n", show_base ());
+    }
 }
 
 /* evaluate element tree */
@@ -478,6 +586,7 @@ double evaluate_element (element_t *root, char mask)
 {
     double op0 = 0, op1 = 0;
     char nextmask = mask;
+    int i, nb;
 
     if ((root == NULL) || (root == ERROR_OP)) {
         VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
@@ -515,16 +624,17 @@ double evaluate_element (element_t *root, char mask)
     case Div:
     case Mod:
     case Pow:
-    case Store:
     case Equal:
     case Diff:
     case Ge:
     case Le:
     case Gt:
     case Lt:
+    case And:
+    case Or:
         if (root->ops[1]) {
             op1 = evaluate_element (root->ops[1], nextmask);
-        } else if (root->func != Store) {
+        } else {
             VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
             return 0;
         }
@@ -532,10 +642,29 @@ double evaluate_element (element_t *root, char mask)
     case Sqr:
     case Cos:
     case Sin:
+    case Tan:
+    case Acos:
+    case Asin:
     case Atan:
+    case Ln:
     case Log:
     case Exp:
+    case Erfc:
+    case Erf:
+    case Abs:
+    case Ceil:
+    case Floor:
     case Recall:
+    case Inc:
+    case Dec:
+    case Not:
+    case Cond:
+    case Prog:
+    case Arg:
+    case Call:
+    case Edit:
+    case Del:
+    case Get:
         if (root->ops[0]) {
             op0 = evaluate_element (root->ops[0], 0);
         } else {
@@ -544,11 +673,73 @@ double evaluate_element (element_t *root, char mask)
         }
         break;
     case Disp:
+    case Clear:
     case Quit:
     case Help:
+    case History:
     case Ans:
     case Pi:
     case E:
+    case Code:
+    case List:
+    case Length:
+    case Pop:
+    case Set:
+    case Show:
+    case Median:
+    case Order:
+    case Prod:
+    case Sum:
+    case Deg:
+    case Grad:
+    case Rad:
+        break;
+    case While:
+        if (root->ops[0] == NULL) {
+            VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
+            return 0;
+        }
+        break;
+    case Memory:
+        if (root->ops[0] == NULL) {
+            op0 = -1;
+        } else {
+            op0 = (int)evaluate_element (root->ops[0], 0);
+            if (op0 < 0) {
+                VERBOSE (WARNING, fprintf (stdout, "error incorrect memory size (%d)\n", (int)op0));
+                return 0;
+            }
+        }
+        break;
+    case Push:
+    case Print:
+        op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : answer;
+        break;
+    case Store:
+    case Put:
+        if (root->ops[0]) {
+            op0 = evaluate_element (root->ops[0], 0);
+        } else {
+            VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
+            return 0;
+        }
+        op1 = (root->ops[1]) ? evaluate_element (root->ops[1], 0) : answer;
+        break;
+    case Max:
+    case Mean:
+    case Min:
+    case Variance:
+        if (root->ops[0]) {
+            op0 = evaluate_element (root->ops[0], 0);
+            op1 = (root->ops[1]) ? evaluate_element (root->ops[1], 0) : answer;
+        }
+        break;
+    case Precision:
+        op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : -1;
+        break;
+    case Base:
+        op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : -1;
+        op1 = (root->ops[1]) ? evaluate_element (root->ops[1], 0) : -1;
         break;
     }
 
@@ -562,16 +753,30 @@ double evaluate_element (element_t *root, char mask)
     case Mod: return fmod (op0, op1);
     case Pow: return pow (op0, op1);
     case Sqr: return sqrt (op0);
-    case Cos: return cos (op0);
-    case Sin: return sin (op0);
-    case Atan: return atan (op0);
-    case Log: return log (op0);
+    case Cos: return cos (op0 / anglefactor);
+    case Sin: return sin (op0 / anglefactor);
+    case Tan: return tan (op0 / anglefactor);
+    case Acos: return acos (op0) * anglefactor;
+    case Asin: return asin (op0) * anglefactor;
+    case Atan: return atan (op0) * anglefactor;
+    case Ln: return log (op0);
+    case Log: return log10 (op0);
     case Exp: return exp (op0);
-    case Store: return store ((int)op0, (op1) ? op1 : answer);
+    case Erfc: return erfc (op0);
+    case Erf: return erf (op0);
+    case Abs: return fabs (op0);
+    case Ceil: return ceil (op0);
+    case Floor: return floor (op0);
+    case Store: return store ((int)op0, op1);
     case Recall: return recall ((int)op0);
+    case Inc: return increase ((int)op0);
+    case Dec: return decrease ((int)op0);
     case Disp: display (); break;
+    case Memory: return memory ((root->ops[0]) ? (int)op0 : -1);
+    case Clear: clear (); break;
     case Quit: quit (); break;
     case Help: help (); break;
+    case History: history (); break;
     case Ans: return answer;
     case Pi: return M_PI;
     case E: return M_E;
@@ -581,6 +786,89 @@ double evaluate_element (element_t *root, char mask)
     case Le: return op0 <= op1;
     case Gt: return op0 > op1;
     case Lt: return op0 < op1;
+    case And: return (op0 != 0) && (op1 != 0);
+    case Or: return (op0 != 0) || (op1 != 0);
+    case Not: return (op0 == 0);
+    case Cond:
+        if ((op0) && (root->ops[1])) {
+            return evaluate_element (root->ops[1], 0);
+        } else if ((!op0) && (root->ops[2])) {
+            return evaluate_element (root->ops[2], 0);
+        } else {
+            return 0;
+        }
+    case While: return while_do (root->ops[0], root->ops[1]);
+    case Code: return execute_code (root->ops, root->nbops);
+    case Print: return print (op0);
+    case Prog:
+        prog ((int)op0, root->ops[1]);
+        savestring ((int)op0, root->string);
+        break;
+    case Arg: return arg ((int)op0);
+    case Call:
+        for (i = 1, nb = 0; i < root->nbops; i++) {
+            if (root->ops[i]) {
+                nb++;
+            }
+        }
+        return call ((int)op0, nb, root->ops + 1);
+    case List: list (); break;
+    case Edit: edit ((int)op0); break;
+    case Del: del ((int)op0); break;
+    case Get: return get ((int)op0);
+    case Length: return length ();
+    case Pop: return pop ();
+    case Push: return push (op0);
+    case Put: return put ((int)op0, op1);
+    case Set:
+        for (i = 0, nb =0; i < root->nbops; i++) {
+            if (root->ops[i]) {
+                nb++;
+            }
+        }
+        return set (nb, root->ops);
+    case Show: show (); break;
+    case Max:
+        if (root->ops[0]) {
+            return op0 > op1 ? op0 : op1;
+        }
+        return max ();
+    case Mean:
+        if (root->ops[0]) {
+            return (op0 + op1) / 2;
+        }
+        return mean ();
+    case Median: return median ();
+    case Min:
+        if (root->ops[0]) {
+            return op0 < op1 ? op0 : op1;
+        }
+        return min ();
+    case Order: order (); break;
+    case Prod: return prod ();
+    case Sum: return sum ();
+    case Variance:
+        if (root->ops[0]) {
+            double m = (op0 + op1) / 2;
+            op0 -= m;
+            op1 -= m;
+            return op0 * op0 + op1 * op1;
+        }
+        return variance ();
+    case Precision:
+        return format ((int)op0);
+    case Base:
+        base ((int)op0, (int)op1);
+        break;
+    case Deg:
+        anglefactor = 180 / M_PI;
+        break;
+    case Grad:
+        anglefactor = 200 / M_PI;
+        break;
+    case Rad:
+        anglefactor = 1;
+        break;
     }
 
     return 0;