extend a test
[calc.git] / parser.c
index e2c4673f4106d4c80074cc30b22e5f471cec3f45..38b7c2692fad24c3a4c9422caa92d3214ad70c3c 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -2,6 +2,7 @@
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "debug.h"
 
 
 double answer = 0;
 
-#define STORAGE_SIZE 10
-double storage[STORAGE_SIZE] = {0};
+#define DEFAULT_STORAGE_SIZE 10
+int storage_size = -1;
+
+double *storage = NULL;
+
+#define DEFAULT_FORMAT "=> %.6g\n"
+char *format = NULL;
+char *minform = NULL;
 
 /* compare codes */
 
@@ -43,7 +50,14 @@ 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;
+        exit (1);
+    }
+    if (nbops) {
+        new->ops = (element_t **) calloc (nbops, sizeof (element_t *));
+        if (new->ops == NULL) {
+            VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+            exit (1);
+        }
     }
     new->func = function;
     new->nbops = nbops;
@@ -56,44 +70,86 @@ element_t *newelement (func_t function, int nbops, int prio)
 
 void delelement (element_t *root)
 {
-    int i;
     if ((root != NULL) && (root != ERROR_OP)) {
+        int i;
         for (i = 0; i < root->nbops; i++) {
             if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
                 delelement (root->ops[i]);
             }
         }
+        if (root->nbops) {
+            free (root->ops);
+        }
         free (root);
     }
 }
 
-/* functions */
+/* duplicate element */
 
-#define NB_OPERATORS 6
+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);
+    tmp->value = root->value;
+    for (i = 0; i < root->nbops; i++) {
+        tmp->ops[i] = dupelement (root->ops[i]);
+    }
+    return tmp;
+}
+
+/* functions */
+
+#define NB_OPERATORS 14
 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}
+    { "^",   Pow, 2, 1, 4},
+    { "==",  Equal, 2, 2, -1},
+    { "!=",  Diff, 2, 2, -1},
+    { ">=",  Ge, 2, 2, -1},
+    { "<=",  Le, 2, 2, -1},
+    { ">",   Gt, 2, 1, -1},
+    { "<",   Lt, 2, 1, -1},
+    { "&",   And, 2, 1, -2},
+    { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 12
+#define NB_FUNCTIONS 26
 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},
     { "log",  Log, 1, 3, 5},
-    { "sto",  Store, 1, 3, 5},
+    { "exp",  Exp, 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},
-    { "disp",  Disp, 0, 4, 5},
-    { "quit", Quit, 0, 4, 5},
-    { "help", Help, 0, 4, 5}
+    { "inc",  Inc, 1, 3, 5},
+    { "dec",  Dec, 1, 3, 5},
+    { "disp", Disp, 0, 4, 9},
+    { "mem",  Mem, 1, 3, 9},
+    { "clr",  Clear, 0, 3, 9},
+    { "quit", Quit, 0, 4, 9},
+    { "help", Help, 0, 4, 9},
+    { "!",    Not, 1, 1, 6},
+    { "cond", Cond, 3, 4, 5},
+    { "while", While, 2, 5, 5},
+    { "print", Print, 1, 5, 5}
 };
 
 #define NB_CONSTANTS 3
@@ -103,26 +159,28 @@ 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))) {
+        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;
@@ -143,6 +201,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')) {
@@ -150,6 +217,48 @@ 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;
+            }
+            element_t **prog = NULL;
+            new = newelement (Prog, 0, 5);
+            root = new;
+            prog = &root;
+
+            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 *newprog = newelement (Prog, (*prog)->nbops + 1, 5);
+                for (i = 0; i < (*prog)->nbops; i++) {
+                    newprog->ops[i] = (*prog)->ops[i];
+                    (*prog)->ops[i] = NULL;
+                }
+                newprog->ops[(*prog)->nbops] = new;
+                delelement (*prog);
+                (*prog) = newprog;
+            } 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 == '(') {
@@ -158,7 +267,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;
                     }
@@ -177,25 +290,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;
             }
@@ -221,11 +343,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, 9);
                 } else {
                     return ERROR_OP;
                 }
@@ -246,11 +364,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;
@@ -273,11 +387,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;
@@ -301,13 +411,25 @@ 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)) {
@@ -371,17 +493,40 @@ 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 Exp: func = "Exponantial"; 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;
     case Pi: func = "Pi"; break;
     case E: func = "E"; break;
+    case Equal: func = "Equal"; break;
+    case Diff: func = "Different"; break;
+    case Ge: func = "Greater or equal"; break;
+    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 Prog: func = "Program"; break;
+    case Print: func = "Print"; break;
     }
 
     fprintf (stdout, "Function: %s\n", func);
@@ -400,22 +545,76 @@ void print_element (element_t *root, int level)
 
 /* storage functions */
 
-double store (int index)
+void memory (int nb)
 {
-    if ((index > 0) && (index <= STORAGE_SIZE)) {
-        storage[index - 1] = answer;
+    int i, l;
+    double *tmp = NULL;
+    if (nb != storage_size) {
+        l = (nb < storage_size) ? nb : storage_size;
+        tmp = (double *) calloc (nb, sizeof (double));
+        if (tmp == NULL) {
+            VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+            exit (1);
+        }
+        for (i = 0; i < l; i++) {
+            tmp[i] = storage[i];
+        }
+        if (storage != NULL) {
+            free (storage);
+        }
+        storage = tmp;
+        storage_size = nb;
+    }
+}
+
+double store (int index, double value)
+{
+    if (storage_size == -1) {
+        memory (DEFAULT_STORAGE_SIZE);
+    }
+    if ((index > 0) && (index <= storage_size)) {
+        storage[index - 1] = value;
     } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
-    return answer;
+    return value;
 }
 
 double recall (int index)
 {
-    if ((index > 0) && (index <= STORAGE_SIZE)) {
+    if (storage_size == -1) {
+        memory (DEFAULT_STORAGE_SIZE);
+    }
+    if ((index > 0) && (index <= storage_size)) {
         return storage[index - 1];
     } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+    }
+    return 0;
+}
+
+double increase (int index)
+{
+    if (storage_size == -1) {
+        memory (DEFAULT_STORAGE_SIZE);
+    }
+    if ((index > 0) && (index <= storage_size)) {
+        return storage[index - 1]++;
+    } else {
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+    }
+    return 0;
+}
+
+double decrease (int index)
+{
+    if (storage_size == -1) {
+        memory (DEFAULT_STORAGE_SIZE);
+    }
+    if ((index > 0) && (index <= storage_size)) {
+        return storage[index - 1]--;
+    } else {
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
     return 0;
 }
@@ -423,13 +622,95 @@ double recall (int index)
 void display (void)
 {
     int i;
+    if (storage_size == -1) {
+        memory (DEFAULT_STORAGE_SIZE);
+    }
     fprintf (stdout, "storage:");
-    for (i = 0; i < STORAGE_SIZE; i++) {
-        fprintf (stdout, " %g", storage[i]);
+    for (i = 0; i < storage_size; i++) {
+        fprintf (stdout, " ");
+        fprintf (stdout, minform, storage[i]);
     }
     fprintf (stdout, "\n");
 }
 
+void clear ()
+{
+    int i;
+    for (i = 0; i < storage_size; i++) {
+        storage[i] = 0;
+    }
+}
+
+/* While do function */
+
+double while_do (element_t *cond, element_t *action)
+{
+    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);
+        if (!evaluate_element (temp, 0)) {
+            break;
+        }
+        if (action) {
+            temp = dupelement (action);
+            ret = evaluate_element (temp, 0);
+        }
+    }
+
+    VERBOSE (DEBUG, fprintf (stdout, "ending while loop\n"));
+
+    return ret;
+}
+
+/* program function */
+
+double program_do (element_t **prog, int nbcalls)
+{
+    double ret = 0;
+    int i;
+    for (i = 0; i < nbcalls; i++) {
+        ret = evaluate_element (prog[i], 0);
+        prog[i] = NULL;
+    }
+    return ret;
+}
+
+/* print function */
+
+void set_format (char *prompt, int precision)
+{
+    char buffer[128] = {0};
+    free_format ();
+    sprintf (buffer, "%s%%.%dg\n", prompt, precision);
+    format = strdup (buffer);
+    sprintf (buffer, "%%.%dg", precision);
+    minform = strdup (buffer);
+}
+
+void free_format ()
+{
+    if (format) {
+        free (format);
+        format = NULL;
+    }
+    if (minform) {
+        free (minform);
+        minform = NULL;
+    }
+}
+
+double print (double value)
+{
+    fprintf (stdout, format ? format : DEFAULT_FORMAT, value);
+    fflush (stdout);
+    return value;
+}
+
 /* quit function */
 
 void quit (void)
@@ -443,15 +724,25 @@ 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, "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, "supported operators:");
+    fprintf (stdout, " + - * / %% ^\n");
+    fprintf (stdout, "camparison operators:");
+    fprintf (stdout, " == != >= <= > <\n");
+    fprintf (stdout, "logical operators:");
+    fprintf (stdout, " & | !\n");
+    fprintf (stdout, "mathematic functions:");
+    fprintf (stdout, " pow sqrt log exp\n");
+    fprintf (stdout, "trig. func.:");
+    fprintf (stdout, " cos sin tan acos asin atan\n");
+    fprintf (stdout, "supported functions:");
+    fprintf (stdout, " abs ceil floor\n");
+    fprintf (stdout, "storage functions:");
+    fprintf (stdout, " mem sto rcl inc dec disp\n");
+    fprintf (stdout, "prog. functions:");
+    fprintf (stdout, " cond while print {} ;\n");
+    fprintf (stdout, "misc. functions:");
+    fprintf (stdout, " quit help\n");
+    fprintf (stdout, "supported constants:");
     fprintf (stdout, " e pi\n");
 }
 
@@ -501,9 +792,18 @@ 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 {
+        } else if (root->func != Store) {
             VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
             return 0;
         }
@@ -511,11 +811,21 @@ double evaluate_element (element_t *root, char mask)
     case Sqr:
     case Cos:
     case Sin:
+    case Tan:
+    case Acos:
+    case Asin:
     case Atan:
     case Log:
     case Exp:
-    case Store:
+    case Abs:
+    case Ceil:
+    case Floor:
     case Recall:
+    case Inc:
+    case Dec:
+    case Not:
+    case Mem:
+    case Cond:
         if (root->ops[0]) {
             op0 = evaluate_element (root->ops[0], 0);
         } else {
@@ -524,11 +834,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 Prog:
+        break;
+    case While:
+        if (root->ops[0] == NULL) {
+            VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
+            return 0;
+        }
+        break;
+    case Print:
+        op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : answer;
         break;
     }
 
@@ -544,20 +865,110 @@ 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 Exp: return exp (op0);
-    case Store: return store ((int)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;
     case Pi: return M_PI;
     case E: return M_E;
+    case Equal: return op0 == op1;
+    case Diff: return op0 != op1;
+    case Ge: return op0 >= op1;
+    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 Prog: return program_do (root->ops, root->nbops);
+    case Print: return print (op0);
     }
 
     return 0;
 }
 
+char **generate_completion_list ()
+{
+    int i, j, l = 0;
+    char **list = (char **) calloc (NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1, sizeof (char *));
+    if (list == NULL) {
+        VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+        exit (1);
+    }
+
+    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_FUNCTIONS + NB_CONSTANTS; i++) {
+            if (list[i] != NULL) {
+                free (list[i]);
+            }
+        }
+        free (list);
+    }
+}
+
+
 /* vim: set ts=4 sw=4 et: */