add Fibonacci sequence
[calc.git] / parser.c
index 20f507d014446d92f63cd2bf73dba65f4fda2930..9796048af19e5f8d552a682fef0c1afc564fd799 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -43,7 +43,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,13 +63,16 @@ 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);
     }
 }
@@ -78,16 +88,9 @@ element_t *dupelement (element_t *root)
         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;
 }
@@ -145,9 +148,6 @@ keyword_t constants[NB_CONSTANTS] = {
 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 +160,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;
@@ -191,6 +187,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 == '(') {
@@ -222,9 +260,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 +281,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;
             }
@@ -274,11 +313,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;
                 }
@@ -299,11 +334,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 +357,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 +383,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;
@@ -463,6 +487,7 @@ void print_element (element_t *root, int level)
     case Not: func = "Not"; break;
     case Cond: func = "Condition"; break;
     case While: func = "While"; break;
+    case Prog: func = "Program"; break;
     }
 
     fprintf (stdout, "Function: %s\n", func);
@@ -539,9 +564,6 @@ 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"));
 
@@ -560,6 +582,19 @@ double while_do (element_t *cond, element_t *action)
     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;
+}
+
 /* quit function */
 
 void quit (void)
@@ -677,6 +712,7 @@ double evaluate_element (element_t *root, char mask)
     case Ans:
     case Pi:
     case E:
+    case Prog:
         break;
     case While:
         if (root->ops[0] == NULL) {
@@ -729,6 +765,7 @@ double evaluate_element (element_t *root, char mask)
             return 0;
         }
     case While: return while_do (root->ops[0], root->ops[1]);
+    case Prog: return program_do (root->ops, root->nbops);
     }
 
     return 0;