fix bracket evaluation
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index 869c00e43cda6e9e0926330ea8319dad17045004..b6b62946275d4594d19a76e0e9df4b3d1dff834c 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -31,18 +31,32 @@ int precision = 6;
 
 /* 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, " -i : input prompt (%s)\n", oprompt);
-    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;
 }
@@ -242,6 +256,7 @@ 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'
@@ -266,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'
@@ -293,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'
@@ -361,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$'