memory feature
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index eee39fe0b54cc34dcd1fb13eab0f0d85d94ef08a..6950ad57522676c10055730e805f0baf89681d2e 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -28,7 +28,9 @@
 /* gobal variables */
 
 char *progname = NULL;
+char *iprompt = "<= ";
 int mode = 1;
+char *oprompt = "=> ";
 int precision = 6;
 char **completion_list = NULL;
 
@@ -39,7 +41,9 @@ int usage (int ret)
     FILE *fid = ret ? stderr : stdout;
     fprintf (fid, "usage: %s\n", progname);
     fprintf (fid, " -h : help message\n");
+    fprintf (fid, " -i : input prompt (%s)\n", iprompt);
     fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
+    fprintf (fid, " -o : output prompt (%s)\n", oprompt);
     fprintf (fid, " -p : precision (%d)\n", precision);
     fprintf (fid, " -v : verbose level (%d)\n", verbose);
 
@@ -110,6 +114,22 @@ int main (int argc, char *argv[])
             mode = 0;
             buffer = buffer_static;
             break;
+        case 'i':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing input prompt\n", progname); usage (1));
+                return 1;
+            }
+            iprompt = arg;
+            break;
+        case 'o':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing output prompt\n", progname); usage (1));
+                return 1;
+            }
+            oprompt = arg;
+            break;
         case 'p':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -132,9 +152,8 @@ int main (int argc, char *argv[])
         }
     }
 
-    /* format */
-    char format[9] = "=> %..g\n";
-    format[5] = '0' + precision;
+    /* set format */
+    set_format (oprompt, precision);
 
     /* completion list*/
     completion_list = generate_completion_list ();
@@ -146,7 +165,7 @@ int main (int argc, char *argv[])
         char *line[BUFFER_SIZE] = {0};
 
         if (mode) {
-            if ((buffer = readline ("<= ")) == NULL) {
+            if ((buffer = readline (iprompt)) == NULL) {
                 break;
             }
 
@@ -157,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);
@@ -171,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') {
@@ -193,13 +216,14 @@ int main (int argc, char *argv[])
             }
             element_t *element = parser (line[i], NULL, -9);
             if (element == ERROR_OP) {
-                VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]); fflush (stdout));
+                VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]));
                 ret = 1;
             } else if (element != NULL) {
                 VERBOSE (INFO, print_element (element, 0));
                 answer = evaluate_element (element, 0);
-                fprintf (stdout, format, answer);
-                fflush (stdout);
+                if (!element->hidden) {
+                    print (answer);
+                }
                 delelement (element);
                 ret = 0;
             }
@@ -214,6 +238,8 @@ int main (int argc, char *argv[])
 
     free_completion_list (completion_list);
 
+    free_format ();
+
     return ret;
 }
 
@@ -223,8 +249,12 @@ int main (int argc, char *argv[])
 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
 // test: calc.exe error 2>&1 | grep -q 'invalid option'
+// test: calc.exe -i 2>&1 | grep -q 'missing input prompt'
+// test: calc.exe -o 2>&1 | grep -q 'missing output prompt'
 // test: calc.exe -p 2>&1 | grep -q 'missing precision'
 // test: calc.exe -v 2>&1 | grep -q 'missing verbose'
+// test: echo "1 + 1" | calc.exe -i '# ' | grep -q '# 1 + 1'
+// test: echo "1 + 1" | calc.exe -o '# ' | grep -q '# 2'
 // test: echo "1 + 2" | calc.exe | grep -q '=> 3'
 // test: echo "1 - 2" | calc.exe | grep -q '=> -1'
 // test: echo "2 * 3" | calc.exe | grep -q '=> 6'
@@ -261,7 +291,7 @@ int main (int argc, char *argv[])
 // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0'
 // test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0'
 // test: echo "quit" | calc.exe | grep -q 'bye'
-// test: echo "help" | calc.exe | grep -q 'miscellaneous'
+// test: echo "help" | calc.exe | grep -q 'misc\.'
 // test: echo "1 + 2 *" | calc.exe | grep -q 'error'
 // test: echo "* 1 - 2" | calc.exe | grep -q 'error'
 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
@@ -334,9 +364,23 @@ int main (int argc, char *argv[])
 // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6'
 // test: echo -e '{\n{}\n{1, 2\n{sto (1, 1 + 1),\npow(2, {sto (1, 1 + 2), 2}, {rcl(2)})\n2 {sto (1, 1 + 1)}' | calc.exe | grep -c error | xargs test 6 =
 // 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
+// test: echo -e 'mem (3)\nsto (4, pi)' | calc.exe | grep -q "invalid index"
+// test: echo -e 'disp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
+// test: echo -e 'sto (3, 10)\ndisp' | calc.exe | grep -q "storage: 0 0 10 0 0 0 0 0 0 0"
+// test: echo -e 'rcl (3)\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
+// test: echo -e 'inc (2)\ndisp' | calc.exe | grep -q "storage: 0 1 0 0 0 0 0 0 0 0"
+// test: echo -e 'dec (2)\ndisp' | calc.exe | grep -q "storage: 0 -1 0 0 0 0 0 0 0 0"
+
+// 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'
 
 // 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)), sto (2, rcl (3))})}' | calc.exe | grep '=> 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'
 
 /* vim: set ts=4 sw=4 et: */