partial program feature
authorLaurent Mazet <mazet@softndesign.org>
Wed, 25 Jan 2023 07:11:08 +0000 (08:11 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Wed, 25 Jan 2023 07:11:08 +0000 (08:11 +0100)
parser.c
parser.h

index 7a9f9885318585f31c4cf4a16fd1da186a0c32fb..798abb8545e30b443da0983ee2541f9fa4aa746e 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -200,6 +200,60 @@ element_t *parser (char *str, char **next, int prio)
             continue;
         }
 
+        /* check for open brace */
+
+        if (*str == '{') {
+            VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
+            element_t *prog = newelement (Prog, 0, 5);
+            if (prog == NULL) {
+                delelement (root);
+                return ERROR_OP;
+            }
+            if (root == NULL) {
+                root = prog;
+            } else {
+                for (i = 0; i < root->nbops; i++) {
+                    if (root->ops[i] == NULL) {
+                        root->ops[i] = prog;
+                        found = 1;
+                    }
+                }
+                if (!found) {
+                    delelement (prog);
+                    delelement (root);
+                    return ERROR_OP;
+                }
+            }
+
+            do {
+                found = 0;
+                new = parser (str + 1, &str, 0);
+                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;
+                }
+                element_t *prognew = newelement (Prog, prog->nbops + 1, 5);
+                for (i = 0; i < prog->nbops; i++) {
+                    prognew->ops[i] = prog->ops[i];
+                }
+                prog->ops[prog->nbops] = new;
+                delelement (prog);
+                prog = prognew;
+            } 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 == '(') {
@@ -255,9 +309,9 @@ 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 (next != NULL) {
                 *next = str;
             }
@@ -472,6 +526,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);
@@ -569,6 +624,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)
@@ -686,6 +754,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) {
@@ -738,6 +807,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;
index 67dd9ab2a7f9ef1f64f6c5eea95c665294c35af1..734d818f173f4bc1a1ca39ac01de405c804df77a 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -19,7 +19,7 @@ typedef enum {
     Ans, E, Pi,
     Equal, Diff, Ge, Le, Gt, Lt,
     And, Or, Not,
-    Cond, While
+    Cond, While, Prog
 } func_t;
 
 /* keyword type */