fix bracket evaluation
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index cadb186f4d1a68deab1d34471c188549a228f610..b6b62946275d4594d19a76e0e9df4b3d1dff834c 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,32 +1,26 @@
 /* depend: */
 /* cflags: */
-/* linker: alloc.o argument.o color.o debug.o element.o format.o parser.o program.o stack.o storage.o tabular.o workspace.o -lm -lreadline */
+/* linker: alloc.o argument.o color.o debug.o element.o format.o parser.o program.o readline.o stack.o storage.o tabular.o workspace.o -lm -lreadline */
 
 #include <malloc.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
-#include <readline/readline.h>
-#include <readline/history.h>
-
 #include "debug.h"
 #include "element.h"
 #include "format.h"
 #include "parser.h"
+#include "readline.h"
 
 /* constants */
 
 #define BUFFER_SIZE 4096
-#define HISTORY_LEN 10
 
 /* macros */
 
-#define CEIL(x, y) (((x) + (y) - 1) / (y))
-#define MIN(x, y) (((x) < (y)) ? (x) : (y))
-#define MAX(x, y) (((x) > (y)) ? (x) : (y))
-
 /* gobal variables */
 
 char *progname = NULL;
@@ -34,71 +28,39 @@ char *iprompt = "<= ";
 int mode = 1;
 char *oprompt = "=> ";
 int precision = 6;
-char **completion_list = NULL;
 
 /* help function */
 
+#define PRINT_OPTION(fid, option, message, format, ...) \
+    do { \
+               fprintf (fid, " %s: %s", option, message); \
+       if (format) { \
+                       fprintf (fid, " ("); \
+                       color_set (fid, FG_BLACK | BG_WHITE); \
+               fprintf (fid, format ? format : "xx", ## __VA_ARGS__); \
+               color_set (fid, COLOR_DEFAULT); \
+                       fprintf (fid, ")"); \
+       } \
+               fprintf (fid, "\n"); \
+       } while (0);
+
 int usage (int ret)
 {
     FILE *fid = ret ? stderr : stdout;
+    color_set (fid, UNDERLINE);
     fprintf (fid, "usage: %s\n", progname);
-    fprintf (fid, " -h : help message\n");
-    fprintf (fid, " -b : in/out-put base (%s)\n", show_base ());
-    fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
-    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);
+    color_set (fid, COLOR_DEFAULT);
+    PRINT_OPTION (fid, "-b", "in/out-put base", "%s", show_base ());
+    PRINT_OPTION (fid, "-h", "help message", NULL);
+    PRINT_OPTION (fid, "-i", "input prompt", "%s", oprompt);
+    PRINT_OPTION (fid, "-n", "no readline mode", "%s", mode ? "yes" : "no");
+    PRINT_OPTION (fid, "-o", "output prompt", "%s", oprompt);
+    PRINT_OPTION (fid, "-p", "precision", "%d", precision);
+    PRINT_OPTION (fid, "-v", "verbose level", "%d", verbose);
 
     return ret;
 }
 
-/* completion function */
-
-char *generator (const char *text, int state);
-
-char **completion (const char *text, __attribute__((unused)) int start, __attribute__((unused)) int end)
-{
-    rl_attempted_completion_over = 1;
-    return rl_completion_matches (text, generator);
-}
-
-char *generator (const char *text, int state)
-{
-    static int index, len;
-    char *name;
-
-    if (!state) {
-        index = 0;
-        len = strlen(text);
-    }
-
-    while ((name = completion_list[index++])) {
-        if (strncmp (name, text, len) == 0) {
-            return strdup (name);
-        }
-    }
-
-    return NULL;
-}
-
-/* edit line */
-char *edit_line = NULL;
-int edit_hook ()
-{
-    static int state = 0;
-    if (edit_line) {
-        if (state == 0) {
-            state = 1;
-        } else {
-            state = 0;
-            free (edit_line);
-            edit_line = NULL;
-        }
-    }
-    return rl_insert_text (edit_line);
-}
-
 /* main function */
 
 int main (int argc, char *argv[])
@@ -194,12 +156,10 @@ int main (int argc, char *argv[])
     /* set format */
     set_format ();
 
-    /* completion list*/
-    completion_list = generate_completion_list ();
-    rl_attempted_completion_function = completion;
-
-    /* startup hook */
-    rl_startup_hook = edit_hook;
+    /* init readline */
+    if (mode) {
+        init_read_line ();
+    }
 
     /* read from input stream */
 
@@ -207,35 +167,27 @@ int main (int argc, char *argv[])
         char *line[BUFFER_SIZE] = {0};
 
         if (mode) {
-            if ((buffer = readline (iprompt)) == NULL) {
+            if (read_line (&buffer, iprompt)) {
                 break;
             }
 
             /* check empty line */
-            if (strlen (buffer) == 0) {
-                free (buffer);
+            if (buffer == NULL) {
                 continue;
-            } else if (strcmp (buffer, ".") == 0) {
-                free (buffer);
-                break;
             }
 
             /* add line into history */
-            add_history (buffer);
-            VERBOSE (INFO, fprintf (stdout, "line (%d/%d): '%s'\n",
-                                    where_history (), history_length, buffer));
-            if (history_length > HISTORY_LEN) {
-                HIST_ENTRY *last = remove_history (0);
-                if (last) {
-                    free_history_entry (last);
-                }
-            }
+            manage_history (buffer);
+
         } else {
             printf ("%s", iprompt);
             if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) {
                 break;
             }
-            VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
+        }
+
+        if (strcmp (buffer, ".") == 0) {
+            break;
         }
 
         /* pre-process buffer */
@@ -274,12 +226,16 @@ int main (int argc, char *argv[])
 
         if (mode) {
             free (buffer);
+            buffer = NULL;
         } else {
             memset (buffer, 0, BUFFER_SIZE);
         }
+        fflush (stdout);
     }
 
-    free_completion_list (completion_list);
+    if (mode) {
+        clean_read_line (buffer);
+    }
 
     free_format ();
 
@@ -300,8 +256,11 @@ int main (int argc, char *argv[])
 // 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 +" | calc.exe; test $? -eq 1
 // test: echo "1 + 1" | calc.exe -i '# ' | grep -q '# 1 + 1'
+// test: echo "1 + 1" | calc.exe -i '# ' -i 'x ' | grep -q 'x 1 + 1'
 // test: echo "1 + 1" | calc.exe -o '# ' | grep -q '# 2'
+// test: echo "1 + 1" | calc.exe -o '# ' -o 'x ' | grep -q 'x 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'
@@ -322,6 +281,8 @@ int main (int argc, char *argv[])
 // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403'
 // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275'
 // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
+// test: echo "10 - (5 + 5)" | calc.exe | grep -q '=> 0'
+// test: echo "10 + (5 + 5) * 2" | calc.exe | grep -q '=> 30'
 // test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25'
 // test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988'
 // test: echo "1 + -2" | calc.exe | grep -q '=> -1'
@@ -340,6 +301,7 @@ int main (int argc, char *argv[])
 // 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 -e '1 + 1\nhist' | calc.exe | grep -q '2: 1 + 1'
 // 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'
@@ -348,6 +310,7 @@ int main (int argc, char *argv[])
 // test: echo "2 + cos (pi +" | calc.exe | grep -q 'error'
 // test: echo "2 + cos (pi" | calc.exe | grep -q 'error'
 // test: echo "(2 + " | calc.exe | grep -q 'error'
+// test: echo "1 (2 + 3)" | calc.exe | grep -q 'error'
 // test: echo "cos (1, 2)" | calc.exe | grep -q 'error'
 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
 // test: echo "pow (2)" | calc.exe | grep -q 'error'
@@ -357,7 +320,7 @@ int main (int argc, char *argv[])
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe -n | grep -q 2
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 64
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 2
-// test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\ntan (0)\nacos (0)\nasin (0)\natan (0)\nln (1)\nlog (1)\nexp (1)\nabs (-1)\nceil (1.2)\nfloor (-1.2)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -n -v 3 | grep -q bye
+// test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\ntan (0)\nacos (0)\nasin (0)\natan (0)\nln (1)\nlog (1)\nexp (1)\nabs (-1)\nceil (1.2)\nfloor (-1.2)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nhist\nquit' | calc.exe -n -v 3 | grep -q bye
 // test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\ntan ()\nacos ()\nasin ()\natan ()\nln ()\nlog ()\nexp ()\nabs ()\nceil ()\nfloor ()\n1 + (\n1+2(\n1+2cos\n1+2pi' | calc.exe | grep -c error | xargs test 22 =
 // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
 // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
@@ -416,6 +379,8 @@ int main (int argc, char *argv[])
 // test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1'
 // test: echo -e '\t\t' | calc.exe | grep -q 'print'
 // test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2
+// test: echo -e 'mem\nsto (4, pi)\ndisp' | calc.exe | grep -q "storage: 0 0 0 3.14159 0 0 0 0 0 0"
+// test: echo -e 'mem (0)\nsto (2, pi)\ndisp' | calc.exe | grep -q "storage: 0 3.14159 0 0 0 0 0 0 0 0"
 // test: echo -e 'mem\nmem (3)\nsto (4, pi)' | calc.exe | grep -q "error out of bound"
 // test: echo -e 'mem (-1)' | calc.exe | grep -q "error"
 // test: echo -e 'sto (2, 3)\nmem (2)\ndisp' | calc.exe | grep -q 'storage: 0 3$'
@@ -427,8 +392,8 @@ int main (int argc, char *argv[])
 // test: echo -e 'clr\nsto (3, pi)\nclr\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
 // test: echo -e 'mem (3)\nclr\nquit' | calc.exe -v 3 | grep -q Clear
 // test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (1, {cos (arg (1)^2)})\ncall (1, pi/6)\nprog (2, {arg (1) * 3})\ncall (2, 1, 2)\nls' | calc.exe | grep -q 'programs: 2 1'
-// test: echo -e 'prog (1, {arg (2) - arg (1)})\ncall (1, 2, 3)\nls\nedit (1)\nprog (1, {arg (2) + arg (1)})\nedit (1)\ndel (1)\nquit' | calc.exe -v 3 | grep -q bye
-// test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (3, cos(arg (1) * pi / 3))\ncall (1, 2, 3)\ncall (2, 1)\nls\nedit (1)\ndel (1)\ndel (3)\ndel (2)\ncall (2, 1, 4)' | calc.exe | grep -c error | xargs test 5 =
+// test: echo -e 'prog (1, {arg (2) - arg (1)})\ncall (1, 2, 3)\nls\nedit (1)\n\nprog (1, {arg (2) + arg (1)})\nedit (1)\n\ndel (1)\nquit' | calc.exe -v 3 | grep -q bye
+// test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (3, cos(arg (1) * pi / 3))\ncall (1, 2, 3)\ncall (2, 1)\nls\nedit (1)\n\ndel (1)\ndel (3)\ndel (2)\ncall (2, 1, 4)' | calc.exe | grep -c error | xargs test 5 =
 // test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (3, cos(arg (1) * pi / 3))\ndel (2)\ndel (3)\nls' | calc.exe | grep -q '^programs:$'
 // test: echo -e 'erf (1)\nerfc (1)\nquit' | calc.exe -v 3 | grep -q bye
 // test: echo -e 'erf ()\nerfc ()' | calc.exe | grep -c error | xargs test 2 =
@@ -438,7 +403,7 @@ int main (int argc, char *argv[])
 // test: echo -e 'set (0, -1)\nset (1, 2, 3, 3.11, pi, 4)\nlen' | calc.exe | grep -q '=> 6'
 // test: echo -e 'set (1, 2)\npop\npush (3)\nput (5, -1)\nlen\nshow\nget (3)\nquit' | calc.exe -n -v 3 | grep -q bye
 // test: echo -e 'put\nget\nget (1)\npop\nput (0)' | calc.exe | grep -c 'error' | xargs test 5 =
-// test: echo -e 'push (2)' | calc.exe | grep -q '=> 2'
+// test: echo -e 'push (1)\npush (2)' | calc.exe | grep -q '=> 2'
 // test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show})\ncall (1, 10)\nprog (1, {mem (1), sto (1, cos (arg (1)))})\ncall (1, pi / 2)\ndel (1)' | calc.exe -n | grep -q 'stack: 1 2 10'
 // test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show});\ncall (1, 10);\nshow\ndel (1)' | calc.exe -n | grep -q 'stack:$'
 // test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show});\ncall (1, 10);\nlen' | calc.exe -n | grep -q '=> 0'
@@ -448,18 +413,21 @@ int main (int argc, char *argv[])
 // test: echo -e 'set (1, 2, -5, 6, -8, 9, -2, 23, 4)\nmin (5, -3)\nmax (-1)\nmin\nmean\nmed\nmax\nord\nprod\nsum\nvar\nquit' | calc.exe -n -v 3 | grep -q bye
 // test: echo -e 'min\nmean\nmed\nmax\nprod\nsum\nvar\nord\nset (1)\nord' | calc.exe -n | grep -c error | xargs test 9 =
 // test: echo -e 'prog (1, cos(pi * arg (1))) / 4' | calc.exe | grep -c error | xargs test 1 = 
-// test: echo -e 'format\n.12345678901' | calc.exe | grep -n '=> 6'
-// test: echo -e 'format (8)\n.12345678901' | calc.exe | grep -n '=> 0.12345679'
-// test: echo -e 'format (12)\n.12345678901' | calc.exe | grep -n '=> 0.12345678901'
-// test: echo -e 'format (4)\n.12345678901\format' | calc.exe | grep -n '=> 4'
-// test: echo -e 'format (0)' | calc.exe | grep -n 'error'
+// test: echo -e 'format\n.12345678901' | calc.exe | grep -q '=> 6'
+// test: echo -e 'format (8)\n.12345678901' | calc.exe | grep -q '=> 0.12345679'
+// test: echo -e 'format (12)\n.12345678901' | calc.exe | grep -q '=> 0.12345678901'
+// test: echo -e 'format (4)\n.12345678901\format' | calc.exe | grep -q '=> 4'
+// test: echo -e 'format (0)' | calc.exe | grep -q 'error'
 // test: echo -e 'ff + ff' | calc.exe -b 16 | grep -q '=> 1fe'
 // test: echo -e '60 / 4' | calc.exe -b 8 | grep -q '=> 14'
 // test: echo -e 'z00-z0+1-2*z+20x' | calc.exe -b 36 | grep -q '=> 1000'
 // test: echo -e '255' | calc.exe -b 10,16 | grep -q '=> ff'
 // test: echo -e 'base (-2)\nbase (16, 0)' | calc.exe | grep -c error | xargs test 2 =
 // test: echo -e 'base (10, 16)\n255' | calc.exe | grep -q '=> ff'
+// test: echo -e 'base (10, 16)\nsto (2, 255)\ndisp' | calc.exe | grep -q 'storage: 0 ff 0 0 0 0 0 0 0 0'
 // test: echo -e 'base' | calc.exe | grep -q 'base (I/O): 10/10'
+// test: echo -e 'deg\nacos (-1)\ngrad\nacos (-1)\nrad\nacos (-1)' | calc.exe | awk 'BEGIN { split("180 200 3.14159", v) } /=>/ { for (i in v) if ($2 == v[i]) n++ } END { exit n != 3 }'
+// test: echo -e 'format\nbase\ndeg\ngrad\nrad\nquit' | calc.exe -v 3 | grep -q bye
 
 // 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'
@@ -473,4 +441,7 @@ int main (int argc, char *argv[])
 // Factorial sequence
 // test: echo -e 'prog (1, cond (arg (1) > 1, arg (1) * call (1, arg (1) - 1), 1))\ncall (1, 10)' | calc.exe | grep -q '=> 3.6288e+06'
 
+// Birthday problem
+// test: echo -e '{sto (1, 365), sto (2, 0), sto (10, 1), while (inc (2) < 50, {sto (10, rcl (10) * (rcl (1) - rcl (2)) / rcl (1)), print (rcl (2) + 1), print ((1 - rcl (10)) * 100)})};' | calc.exe | grep -q '=> 97.0374'
+
 /* vim: set ts=4 sw=4 et: */