partial while feature
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index 8aa124a959db68faadbe7cff587e2faa4490db64..41429cb93771d0871255224aaae06095746a9125 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -71,8 +71,8 @@ int main (int argc, char *argv[])
      while (argc-- > 1) {
         char *arg = *(++argv);
         if (arg[0] != '-') {
-            fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg);
-            return usage (1);
+            VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1));
+            return 1;
         }
         char c = arg[1];
         switch (c) {
@@ -83,16 +83,16 @@ int main (int argc, char *argv[])
         case 'p':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
-                fprintf (stderr, "%s: missing precision\n", progname);
-                return usage (1);
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing precision\n", progname); usage (1));
+                return 1;
             }
             precision = atoi (arg);
             break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
-                fprintf (stderr, "%s: missing verbose level\n", progname);
-                return usage (1);
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1));
+                return 1;
             }
             verbose = atoi (arg);
             break;
@@ -231,7 +231,9 @@ int main (int argc, char *argv[])
 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
 // test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error'
 // test: echo "2 + (foo)" | calc.exe | grep -q 'error'
+// 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 "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'
@@ -242,7 +244,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 | 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)\natan (0)\nlog (1)\nexp (1)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -v 3 | grep -q bye
-// test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\natan ()\nlog ()\nexp ()\n1 + (' | calc.exe | grep -c error | xargs test 11 =
+// test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\natan ()\nlog ()\nexp ()\n1 + (' | calc.exe | grep -c error | xargs test 12 =
 // 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
 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
@@ -274,8 +276,21 @@ int main (int argc, char *argv[])
 // test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0'
 // test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1'
 // test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0'
-// test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -v 3 | grep -q bye
+// test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -v 3 | grep -qv error
 // test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1'
 // test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 + quit' | calc.exe 2>&1 | grep -q error
+// test: echo -e 'cos (quit)' | calc.exe 2>&1 | grep -q error
+// test: echo -e '(quit)' | calc.exe 2>&1 | grep -q error
+// test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe 2>&1 | grep -c error | xargs test 3 =
+// test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe  | grep -q '=> 4\.15888'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe  | grep -q '=> 1808\.04'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64))' | calc.exe  | grep -q '=> 0'
+// test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond
+// test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe 2>&1 | grep -c error | xargs test 3 =
+// test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1'
+// test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe 2>&1 | grep -c error | xargs test 4 =
+// test: echo -e 'inc (11)\ndec (0)' | calc.exe 2>&1 | grep -c invalid | xargs test 2 =
+// test: echo -e 'whl (inc (1) < 3, sto (rcl (2) + 3))' | calc.exe | xargs test 16 =
 
 /* vim: set ts=4 sw=4 et: */