X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=calc.c;h=cadb186f4d1a68deab1d34471c188549a228f610;hb=743e93f0edf2c7779d0af4e16e17662994e0caaf;hp=920099b46a504c1fc88cd6d3ce733705ebd67584;hpb=c91672f948d371d3ba2b1a47db9a31fcabb763f5;p=calc.git diff --git a/calc.c b/calc.c index 920099b..cadb186 100644 --- 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 -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 #include @@ -43,7 +43,8 @@ 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, " -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); @@ -81,13 +82,30 @@ char *generator (const char *text, int state) 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[]) { char *buffer = NULL; char buffer_static[BUFFER_SIZE + 1] = {0}; - int i = 0, nb = 1; + int i = 0, nb = 1, in, out; int ret = 0; /* program name */ @@ -110,11 +128,26 @@ int main (int argc, char *argv[]) VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1)); return 1; } + char *pt = NULL; char c = arg[1]; switch (c) { - case 'n': - mode = 0; - buffer = buffer_static; + case 'b': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + VERBOSE (ERROR, fprintf (stderr, "%s: missing base definition\n", progname); usage (1)); + return 1; + } + in = out = strtol (arg, &pt, 10); + if ((*pt == ' ') || (*pt == ',') || (*pt == '-')) { + pt++; + if (*pt == '\0') { + VERBOSE (ERROR, fprintf (stderr, "%s: missing output base definition\n", progname); usage (1)); + return 1; + } else { + out = atoi (pt); + } + } + set_base (in, out); break; case 'i': arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; @@ -124,13 +157,17 @@ int main (int argc, char *argv[]) } iprompt = arg; break; + case 'n': + mode = 0; + buffer = buffer_static; + 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; + set_prompt (oprompt = arg); break; case 'p': arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; @@ -138,7 +175,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,12 +192,15 @@ int main (int argc, char *argv[]) } /* set format */ - set_format (oprompt, precision); + set_format (); /* completion list*/ completion_list = generate_completion_list (); rl_attempted_completion_function = completion; + /* startup hook */ + rl_startup_hook = edit_hook; + /* read from input stream */ while (1) { @@ -252,6 +292,10 @@ 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 -b 2>&1 | grep -q 'missing base' +// test: calc.exe -b 10, 2>&1 | grep -q 'missing output base' +// test: calc.exe -b 10- 2>&1 | grep -q 'missing output base' +// test: calc.exe -b '10 ' 2>&1 | grep -q 'missing output base' // 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' @@ -372,18 +416,20 @@ 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" // 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" -// test: echo -e 'sto (3, pi)\nclr\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0" +// 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)\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' @@ -392,6 +438,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 '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' @@ -399,8 +446,21 @@ int main (int argc, char *argv[]) // test: echo -e 'set (1, 2, -5, 6, -8, 9, -2, 23, 4)\nord\nshow' | calc.exe | grep -q 'stack: -8 -5 -2 1 2 4 6 9 23' // test: echo -e 'set (1, 2, -5, 6, -8, 9, -2, 23, 4)\nmin (5, -3)\nmax (-1)\nmean (1, 2)\nvar (1, 2)\nmin\nmean\nmed\nmax\nprod\nsum\nvar' | calc.exe | awk 'BEGIN { split("9 -3 -1 1.5 0.5 -8 3.33333 2 23 -794880 30 73.3333", v) } /=>/ { for (i in v) if ($2 == v[i]) n++ } END { exit n != 12 }' // 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' | calc.exe -n | grep -c error | xargs test 8 = +// 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 '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' | calc.exe | grep -q 'base (I/O): 10/10' + // 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' @@ -410,4 +470,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: */