full program feature
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Wed, 25 Jan 2023 17:17:31 +0000 (18:17 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Wed, 25 Jan 2023 17:17:31 +0000 (18:17 +0100)
calc.c
parser.c

diff --git a/calc.c b/calc.c
index 7013673b8600a88e713c0696cddd121dfbbb8a3a..b964ad6e409b7ce74ed6fe276b716c6ebb19d830 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -294,5 +294,8 @@ int main (int argc, char *argv[])
 // test: echo -e 'whl (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 5050'
 // test: echo -e 'whl\nwhl (inc (1) < 3,\nwhl (inc (1) < 100, sto (2, rcl (1) + rcl (2))' 2>&1 | calc.exe | grep -c error | xargs test 3 =
 // test: echo -e 'whl (0, 1)' | calc.exe -v 3 | grep -q While
+// test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe -v 3 | grep -q 'Program'
+// test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6'
+// test: echo -e '{\n{}\n{sto (1, 1 + 1),\npow(2, {sto (1, 1 + 2), 2}, {rcl(2)})\n2 {sto (1, 1 + 1)}' 2>&1 | calc.exe | grep -c error | xargs test 5 =
 
 /* vim: set ts=4 sw=4 et: */
index 798abb8545e30b443da0983ee2541f9fa4aa746e..ebb56a9c678a35aa2fae9404394598a4375406a4 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -45,11 +45,13 @@ element_t *newelement (func_t function, int nbops, int prio)
         VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
         return NULL;
     }
-    new->ops = (element_t **) calloc (1, sizeof (element_t *));
-    if (new->ops == NULL) {
-        free (new);
-        VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
-        return NULL;
+    if (nbops) {
+        new->ops = (element_t **) calloc (nbops, sizeof (element_t *));
+        if (new->ops == NULL) {
+            free (new);
+            VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+            return NULL;
+        }
     }
     new->func = function;
     new->nbops = nbops;
@@ -204,29 +206,20 @@ element_t *parser (char *str, char **next, int prio)
 
         if (*str == '{') {
             VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
-            element_t *prog = newelement (Prog, 0, 5);
-            if (prog == NULL) {
+            if (root != 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;
-                }
+            element_t **prog = NULL;
+            new = newelement (Prog, 0, 5);
+            if (new == NULL) {
+                delelement (root);
+                return ERROR_OP;
             }
+            root = new;
+            prog = &root;
 
             do {
-                found = 0;
                 new = parser (str + 1, &str, 0);
                 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
                     delelement (new);
@@ -234,15 +227,16 @@ element_t *parser (char *str, char **next, int prio)
                 }
                 if ((new == NULL) || (new == ERROR_OP)) {
                     delelement (root);
-                return ERROR_OP;
+                    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];
+                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;
                 }
-                prog->ops[prog->nbops] = new;
-                delelement (prog);
-                prog = prognew;
+                newprog->ops[(*prog)->nbops] = new;
+                delelement (*prog);
+                (*prog) = newprog;
             } while (*str == ',');
 
             if (*str != '}') {