add hidden option
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 27 Jan 2023 15:48:19 +0000 (16:48 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 27 Jan 2023 15:58:21 +0000 (16:58 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index 873e3103d87dc60de6fcd39074716da3069ef512..1a8dd679200f7b2847e365b9df8bba525d2d6f4f 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -176,8 +176,6 @@ int main (int argc, char *argv[])
             } else if (strcmp (buffer, ".") == 0) {
                 break;
             }
-            line[0] = buffer;
-            nb = 1;
 
             /* add line into history */
             add_history (buffer);
@@ -190,21 +188,27 @@ int main (int argc, char *argv[])
                 }
             }
         } else {
+            printf ("%s", iprompt);
             if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) {
                 break;
             }
-            nb = 0;
-            char *pt = line[nb++] = buffer;
-            while (*pt != '\0') {
-                if (*pt == '\n') {
-                    *pt = '\0';
-                    line[nb++] = pt + 1;
-                }
-                pt++;
-            }
             VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
         }
 
+        /* pre-process buffer */
+        nb = 0;
+        char *pt = line[nb++] = buffer;
+        while (*pt != '\0') {
+            switch (*pt) {
+            case '\n':
+                *pt = '\0';
+                // fallthrough
+            case ';':
+                line[nb++] = pt + 1;
+            }
+            pt++;
+        }
+
         /* look for end of line */
         for (i = 0; i < nb; i++) {
             if (*line[i] == '\0') {
@@ -217,7 +221,9 @@ int main (int argc, char *argv[])
             } else if (element != NULL) {
                 VERBOSE (INFO, print_element (element, 0));
                 answer = evaluate_element (element, 0);
-                print (answer);
+                if (!element->hidden) {
+                    print (answer);
+                }
                 delelement (element);
                 ret = 0;
             }
@@ -360,14 +366,15 @@ int main (int argc, char *argv[])
 // test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 =
 // test: echo -e 'print (1)' | calc.exe -v 3 | grep -q Print
 // test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2
 
 // Gauss sequence
-// test: echo -e '{sto (1, 0), sto (10, 0), while (inc (10) < 100, {sto (1, rcl (1) + rcl (10)), print (rcl (1))})}' | calc.exe | grep -q '=> 5050'
+// test: echo -e '{sto (1, 0), sto (10, 0), while (inc (10) < 100, {sto (1, rcl (1) + rcl (10)), print (rcl (1))})};' | calc.exe | grep -q '=> 5050'
 
 // Fibonacci sequence
-// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)))})}' | calc.exe | grep -q '=> 144'
+// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)))})};' | calc.exe | grep -q '=> 144'
 
 // Gold number
-// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 15 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)) / rcl (1))})}' | calc.exe | grep -q '=> 1.61803'
+// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 15 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)) / rcl (1))})};' | calc.exe | grep -q '=> 1.61803'
 
 /* vim: set ts=4 sw=4 et: */
index 4da6b6b64b31497d702bffba03a42135cc206e95..5333a9c4a06ee01163a972a544777eb2cde1977f 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -195,6 +195,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')) {
@@ -670,7 +679,7 @@ void help (void)
     fprintf (stdout, "storage functions:");
     fprintf (stdout, " sto rcl inc dec\n");
     fprintf (stdout, "conditional functions:");
-    fprintf (stdout, " cond while print {}\n");
+    fprintf (stdout, " cond while print {} ;\n");
     fprintf (stdout, "miscellaneous functions:");
     fprintf (stdout, " quit help\n");
     fprintf (stdout, "supported constants:");
index 522adba9dec018d88493f1e6e977cadce30e835c..eed45f679d14585706f9e5eca3200143c16ac533 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -41,6 +41,7 @@ typedef struct _element_t {
     struct _element_t **ops;
     double value;
     int prio;
+    int hidden;
 } element_t;
 
 #define ERROR_OP ((element_t *)(-1))