check parenthesis
[calc.git] / parser.c
index 20f507d014446d92f63cd2bf73dba65f4fda2930..7bdcda5a1ec7c06b06c4400dcfd6dd39ef631ab9 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -1,9 +1,16 @@
-#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"
 
@@ -11,9 +18,6 @@
 
 double answer = 0;
 
-#define STORAGE_SIZE 10
-double storage[STORAGE_SIZE] = {0};
-
 /* compare codes */
 
 int codecmp (char *ref, char *str)
@@ -36,64 +40,10 @@ 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);
-    }
-}
-
-/* duplicate element */
-
-element_t *dupelement (element_t *root)
-{
-    element_t *tmp = NULL;
-    int i;
-
-    if ((root == NULL) || (root == ERROR_OP)) {
-        return root;
-    }
-    tmp = newelement (root->func, root->nbops, root->prio);
-    if (tmp == NULL) {
-        return ERROR_OP;
-    }
-    tmp->value = root->value;
-    for (i = 0; i < root->nbops; i++) {
-        tmp->ops[i] = dupelement (root->ops[i]);
-        if (tmp->ops[i] == ERROR_OP) {
-            delelement (tmp);
-            return ERROR_OP;
-        }
-    }
-    return tmp;
-}
-
 /* functions */
 
+#define MAX_ARGS 100
+
 #define NB_OPERATORS 14
 keyword_t operators[NB_OPERATORS] = {
     { "+\t", Add, 2, 1, 1},
@@ -112,25 +62,58 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 17
+#define NB_FUNCTIONS 50
 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},
+    { "tan",  Tan, 1, 3, 5},
+    { "acos", Acos, 1, 4, 5},
+    { "asin", Asin, 1, 4, 5},
     { "atan", Atan, 1, 4, 5},
-    { "exp",  Exp, 1, 3, 5},
+    { "ln",   Ln, 1, 2, 5},
     { "log",  Log, 1, 3, 5},
+    { "exp",  Exp, 1, 3, 5},
+    { "erfc", Erfc, 1, 4, 5},
+    { "erf",  Erf, 1, 3, 5},
+    { "abs",  Abs, 1, 3, 5},
+    { "floor", Floor, 1, 5, 5},
+    { "ceil", Ceil, 1, 4, 5},
     { "sto",  Store, 2, 3, 5},
     { "rcl",  Recall, 1, 3, 5},
     { "inc",  Inc, 1, 3, 5},
     { "dec",  Dec, 1, 3, 5},
     { "disp", Disp, 0, 4, 9},
+    { "mem",  Mem, 1, 3, 5},
+    { "clr",  Clear, 0, 3, 9},
     { "quit", Quit, 0, 4, 9},
     { "help", Help, 0, 4, 9},
     { "!",    Not, 1, 1, 6},
     { "cond", Cond, 3, 4, 5},
-    { "whl",  While, 2, 3, 5}
+    { "while", While, 2, 5, 5},
+    { "print", Print, 1, 5, 5},
+    { "prog", Prog, 2, 4, 9},
+    { "arg",  Arg, 1, 3, 5},
+    { "call", Call, MAX_ARGS, 4, 5},
+    { "ls",   List, 0, 2, 9},
+    { "edit", Edit, 1, 4, 9},
+    { "del",  Del, 1, 3, 9},
+    { "get",  Get, 1, 3, 5},
+    { "len",  Length, 0, 3, 5},
+    { "pop",  Pop, 0, 3, 5},
+    { "push", Push, 1, 4, 5},
+    { "put",  Put, 2, 3, 5},
+    { "set",  Set, MAX_ARGS, 3, 5},
+    { "show", Show, 0, 4, 5},
+    { "max",  Max, 2, 3, 5},
+    { "mean", Mean, 2, 4, 5},
+    { "med",  Median, 0, 3, 5},
+    { "min",  Min, 2, 3, 5},
+    { "ord",  Order, 0, 3, 5},
+    { "prod", Prod, 0, 4, 5},
+    { "sum",  Sum, 0, 3, 5},
+    { "var",  Variance, 2, 3, 5},
 };
 
 #define NB_CONSTANTS 3
@@ -140,14 +123,16 @@ keyword_t constants[NB_CONSTANTS] = {
     { "pi",  Pi, 0, 2, 5}
 };
 
+#define NB_SYMBOLS 4
+char *symbols[NB_SYMBOLS] = {
+    "(", ")", "{", "}"
+};
+
 /* 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))) {
@@ -160,10 +145,6 @@ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, in
         return ERROR_OP;
     }
     *proot = newelement (Val, 1, 5);
-    if (*proot == NULL) {
-        delelement (new);
-        return ERROR_OP;
-    }
     (*proot)->ops[0] = new;
 
     return *proot;
@@ -174,6 +155,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"));
@@ -184,6 +166,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')) {
@@ -191,6 +182,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 == '(') {
@@ -222,9 +252,6 @@ 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 == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
                     delelement (new);
@@ -246,9 +273,13 @@ element_t *parser (char *str, char **next, int prio)
             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;
             }
@@ -261,7 +292,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;
@@ -274,11 +309,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;
                 }
@@ -299,11 +330,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;
@@ -326,11 +353,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;
@@ -356,9 +379,6 @@ element_t *parser (char *str, char **next, int prio)
             if (str != pt) {
                 if ((root == NULL) || (root->prio == 6)) {
                     new = newelement (Val, 1, 5);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
                     new->value = value;
                     if (root == NULL) {
                         root = new;
@@ -409,6 +429,11 @@ element_t *parser (char *str, char **next, int prio)
         *next = str;
     }
 
+    /* save string */
+    if (root != NULL) {
+        root->string = string;
+    }
+
     return root;
 }
 
@@ -439,14 +464,25 @@ 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 Mem: func = "Memory"; break;
+    case Clear: func = "Clear"; break;
     case Quit: func = "Quit"; break;
     case Help: func = "Help"; break;
     case Ans: func = "Ans"; break;
@@ -463,6 +499,29 @@ void print_element (element_t *root, int level)
     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;
     }
 
     fprintf (stdout, "Function: %s\n", func);
@@ -479,58 +538,6 @@ void print_element (element_t *root, int level)
     }
 }
 
-/* storage functions */
-
-double store (int index, double value)
-{
-    if ((index > 0) && (index <= STORAGE_SIZE)) {
-        storage[index - 1] = value;
-    } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
-    }
-    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;
-}
-
-double increase (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;
-}
-
-double decrease (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;
-}
-
-void display (void)
-{
-    int i;
-    fprintf (stdout, "storage:");
-    for (i = 0; i < STORAGE_SIZE; i++) {
-        fprintf (stdout, " %g", storage[i]);
-    }
-    fprintf (stdout, "\n");
-}
-
 /* While do function */
 
 double while_do (element_t *cond, element_t *action)
@@ -539,19 +546,19 @@ double while_do (element_t *cond, element_t *action)
     element_t *temp = NULL;
 
     VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
-    if (cond == NULL) {
-        return ret;
-    }
     while (1) {
         VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));
 
         temp = dupelement (cond);
-        if (!evaluate_element (temp, 0)) {
+        double test = evaluate_element (temp, 0);
+        delelement (temp);
+        if (!test) {
             break;
         }
         if (action) {
             temp = dupelement (action);
             ret = evaluate_element (temp, 0);
+            delelement (temp);
         }
     }
 
@@ -560,6 +567,18 @@ double while_do (element_t *cond, element_t *action)
     return ret;
 }
 
+/* program function */
+
+double execute_code (element_t **prog, int nbcalls)
+{
+    double ret = 0;
+    int i;
+    for (i = 0; i < nbcalls; i++) {
+        ret = evaluate_element (prog[i], 0);
+    }
+    return ret;
+}
+
 /* quit function */
 
 void quit (void)
@@ -573,22 +592,34 @@ void quit (void)
 void help (void)
 {
     fprintf (stdout, "calc is a simple calculator\n\n");
-    fprintf (stdout, "supported operators:");
+    fprintf (stdout, "arithmetic op.:");
     fprintf (stdout, " + - * / %% ^\n");
-    fprintf (stdout, "camparison operators:");
+    fprintf (stdout, "comparison op.:");
     fprintf (stdout, " == != >= <= > <\n");
-    fprintf (stdout, "logical operators:");
+    fprintf (stdout, "logical op.:");
     fprintf (stdout, " & | !\n");
-    fprintf (stdout, "supported functions:");
-    fprintf (stdout, " pow sqrt cos sin atan log exp\n");
-    fprintf (stdout, "storage functions:");
-    fprintf (stdout, " sto rcl inc dec\n");
-    fprintf (stdout, "conditional functions:");
-    fprintf (stdout, " cond\n");
-    fprintf (stdout, "miscellaneous functions:");
-    fprintf (stdout, " quit help\n");
-    fprintf (stdout, "supported constants:");
-    fprintf (stdout, " e pi\n");
+    fprintf (stdout, "mathematic func.:");
+    fprintf (stdout, " exp ln log pow sqrt\n");
+    fprintf (stdout, "trigonometric func.:");
+    fprintf (stdout, " acos asin atan cos sin tan\n");
+    fprintf (stdout, "error functions:");
+    fprintf (stdout, " erf erfc\n");
+    fprintf (stdout, "miscellaneous func.:");
+    fprintf (stdout, " abs ceil floor\n");
+    fprintf (stdout, "storage func.:");
+    fprintf (stdout, " clear dec disp inc mem rcl sto\n");
+    fprintf (stdout, "control flow prim.:");
+    fprintf (stdout, " cond print while {} ;\n");
+    fprintf (stdout, "program management:");
+    fprintf (stdout, " arg call del edit ls prog\n");
+    fprintf (stdout, "stack management:");
+    fprintf (stdout, " get len pop push put set show\n");
+    fprintf (stdout, "stack func.:");
+    fprintf (stdout, " max mean med min ord prod sum var\n");
+    fprintf (stdout, "control management:");
+    fprintf (stdout, " help quit\n");
+    fprintf (stdout, "constants:");
+    fprintf (stdout, " ans e pi\n");
 }
 
 /* evaluate element tree */
@@ -600,6 +631,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"));
@@ -656,14 +688,30 @@ 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 Mem:
     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 {
@@ -672,11 +720,22 @@ double evaluate_element (element_t *root, char mask)
         }
         break;
     case Disp:
+    case Clear:
     case Quit:
     case Help:
     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:
         break;
     case While:
         if (root->ops[0] == NULL) {
@@ -684,6 +743,27 @@ double evaluate_element (element_t *root, char mask)
             return 0;
         }
         break;
+    case Push:
+    case Print:
+        op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : answer;
+        break;
+    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;
+        }
     }
 
     switch (root->func) {
@@ -698,14 +778,25 @@ double evaluate_element (element_t *root, char mask)
     case Sqr: return sqrt (op0);
     case Cos: return cos (op0);
     case Sin: return sin (op0);
+    case Tan: return tan (op0);
+    case Acos: return acos (op0);
+    case Asin: return asin (op0);
     case Atan: return atan (op0);
-    case Log: return log (op0);
+    case Ln: return log (op0);
+    case Log: return log10 (op0);
     case Exp: return exp (op0);
+    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) ? op1 : answer);
     case Recall: return recall ((int)op0);
     case Inc: return increase ((int)op0);
     case Dec: return decrease ((int)op0);
     case Disp: display (); break;
+    case Mem: memory ((int)op0); break;
+    case Clear: clear (); break;
     case Quit: quit (); break;
     case Help: help (); break;
     case Ans: return answer;
@@ -729,9 +820,121 @@ double evaluate_element (element_t *root, char mask)
             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 ();
     }
 
     return 0;
 }
 
+char **generate_completion_list ()
+{
+    int i, j, l = 0;
+    char **list = (char **) callocordie (NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1, sizeof (char *));
+
+    for (i = 0; i < NB_OPERATORS; i++) {
+        list[l] = strdup ((operators + i)->keyword);
+        for (j = 0; j < (int)strlen (list[l]); j++) {
+            if (list[i][j] == '\t') {
+                list[i][j] = '\0';
+            }
+        }
+        if (list[l] != NULL) {
+            l++;
+        }
+    }
+
+    for (i = 0; i < NB_FUNCTIONS; i++) {
+        list[l] = strdup ((functions + i)->keyword);
+        if (list[l] != NULL) {
+            l++;
+        }
+    }
+
+    for (i = 0; i < NB_CONSTANTS; i++) {
+        list[l] = strdup ((constants + i)->keyword);
+        if (list[l] != NULL) {
+            l++;
+        }
+    }
+
+    for (i = 0; i < NB_SYMBOLS; i++) {
+        list[l] = strdup (symbols[i]);
+        if (list[l] != NULL) {
+            l++;
+        }
+    }
+
+    return (list);
+}
+
+void free_completion_list (char **list)
+{
+    int i;
+
+    if (list) {
+        for (i = 0; i < NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1; i++) {
+            if (list[i] != NULL) {
+                free (list[i]);
+            }
+        }
+        free (list);
+    }
+}
+
 /* vim: set ts=4 sw=4 et: */