new function: format
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index 2cf73f09a3740aded055150ae8dcdfe5eb3dc37c..bd816b982b0a7a2d2c58ea659e858628f54360b4 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: alloc.o argument.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 stack.o storage.o tabular.o workspace.o -lm -lreadline */
 
 #include <malloc.h>
 #include <stddef.h>
@@ -130,7 +130,7 @@ int main (int argc, char *argv[])
                 VERBOSE (ERROR, fprintf (stderr, "%s: missing output prompt\n", progname); usage (1));
                 return 1;
             }
-            oprompt = arg;
+            set_prompt (oprompt = arg);
             break;
         case 'p':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
@@ -138,7 +138,7 @@ int main (int argc, char *argv[])
                 VERBOSE (ERROR, fprintf (stderr, "%s: missing precision\n", progname); usage (1));
                 return 1;
             }
-            precision = atoi (arg);
+            set_precision (precision = atoi (arg));
             break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
@@ -155,7 +155,7 @@ int main (int argc, char *argv[])
     }
 
     /* set format */
-    set_format (oprompt, precision);
+    set_format ();
 
     /* completion list*/
     completion_list = generate_completion_list ();
@@ -372,7 +372,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 (3)\nsto (4, pi)' | calc.exe | grep -q error
+// 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$'
 // 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"
@@ -383,7 +384,8 @@ int main (int argc, char *argv[])
 // 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)\n\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))\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 (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 =
 // test: echo -e 'set (1, 2, 3, 3.11, pi, 4)\nget (4)' | calc.exe | grep -q '=> 3.11'
@@ -402,6 +404,12 @@ 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'
+
 // 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'
 
@@ -411,4 +419,7 @@ int main (int argc, char *argv[])
 // 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'
 
+// 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'
+
 /* vim: set ts=4 sw=4 et: */