0f8604cae8ca621e6549f118d90ff68b60adec25
[calc.git] / calc.c
1 /* depend: */
2 /* cflags: */
3 /* linker: atoi.o debug.o fdprintf.o parser.o -lm -lreadline */
4
5 #include <malloc.h>
6 #include <readline/readline.h>
7 #include <readline/history.h>
8 #include <stddef.h>
9 #include <stdio.h>
10 //#include <stdlib.h>
11 //#include <unistd.h>
12
13 #include "atoi.h"
14 #include "debug.h"
15 #include "fdprintf.h"
16 #include "parser.h"
17
18 /* constants */
19
20 /* macros */
21
22 #define CEIL(x, y) (((x) + (y) - 1) / (y))
23 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
24 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
25
26 /* gobal variables */
27
28 char *progname = NULL;
29 int precision = 6;
30
31 /* help function */
32
33 int usage (int ret)
34 {
35 int fd = ret ? stdfderr : stdfdout;
36 fdprintf (fd, "usage: %s\n", progname);
37 fdprintf (fd, " -h : help message\n");
38 fdprintf (fd, " -p : precision (%d)\n", precision);
39 fdprintf (fd, " -v : verbose level (%d)\n", verbose);
40
41 return ret;
42 }
43
44 /* main function */
45
46 int main (int argc, char *argv[])
47 {
48 char *buffer;
49 int i = 0;
50 int ret = 0;
51
52 /* program name */
53
54 progname = argv[0];
55 while (progname[i] != '\0') {
56 if ((progname[i] == '/') || (progname[i] == '\\')) {
57 progname += i + 1;
58 i = 0;
59 } else {
60 i++;
61 }
62 }
63
64 /* argument processing */
65
66 while (argc-- > 1) {
67 char *arg = *(++argv);
68 if (arg[0] != '-') {
69 PRINTERR ("%s: invalid option -- %s\n", progname, arg);
70 return usage (1);
71 }
72 char c = arg[1];
73 switch (c) {
74 case 'p':
75 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
76 if (arg == NULL) {
77 PRINTERR ("%s: missing precision\n", progname);
78 return usage (1);
79 }
80 precision = atoi (arg);
81 break;
82 case 'v':
83 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
84 if (arg == NULL) {
85 PRINTERR ("%s: missing verbose level\n", progname);
86 return usage (1);
87 }
88 verbose = atoi (arg);
89 break;
90 case 'h':
91 default:
92 return usage (c != 'h');
93 }
94 }
95
96 /* format */
97 char format[8] = "=> %.f\n";
98 format[4] = '0' + precision;
99
100 /* read from input stream */
101
102 while ((buffer = readline ("> ")) != NULL) {
103
104 /* check empty line */
105 if (strlen (buffer) == 0) {
106 free (buffer);
107 continue;
108 } else if (strcmp (buffer, ".") == 0) {
109 return 0;
110 }
111
112 /* add line into history */
113 add_history (buffer);
114 VERBOSE (INFO, PRINTOUT ("line (%d): %s\n", where_history (), buffer));
115 if (where_history () == 10) {
116 HIST_ENTRY *last = remove_history (0);
117 if (last) {
118 free_history_entry (last);
119 }
120 }
121
122 /* look for end of line */
123 element_t *element = parser (buffer, NULL, 0);
124 if (element == ERROR_OP) {
125 VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer));
126 ret = 1;
127 } else {
128 VERBOSE (INFO, print_element (element, 0));
129 PRINTOUT (format, evaluate_element (element, 0));
130 delelement (element);
131 ret = 0;
132 }
133
134 free (buffer);
135 }
136
137 return ret;
138 }
139
140 // test: calc.exe -h
141 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
142 // test: echo 1 | calc.exe -v3 | grep -q value
143 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
144 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
145 // test: calc.exe error 2>&1 | grep -q 'invalid option'
146 // test: calc.exe -p 2>&1 | grep -q 'missing precision'
147 // test: calc.exe -v 2>&1 | grep -q 'missing verbose'
148 // test: echo "1 + 2" | calc.exe | grep -q '=> 3'
149 // test: echo "1 - 2" | calc.exe | grep -q '=> -1'
150 // test: echo "2 * 3" | calc.exe | grep -q '=> 6'
151 // test: echo "1 / 2" | calc.exe | grep -q '=> 5e-1'
152 // test: echo "8 % 3" | calc.exe | grep -q '=> 2'
153 // test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2.8'
154 // test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
155 // test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2.3401'
156 // test: echo "sqrt (2)" | calc.exe | grep -q '=> 1.414213'
157 // test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
158 // test: echo "cos (2)" | calc.exe | grep -q '=> -4.161468e-1'
159 // test: echo "sin (2)" | calc.exe | grep -q '=> 9.092974e-1'
160 // test: echo "atan (2)" | calc.exe | grep -q '=> 1.107148'
161 // test: echo "exp (2)" | calc.exe | grep -q '=> 7.389056'
162 // test: echo "log (2)" | calc.exe | grep -q '=> 6.931471e-1'
163 // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
164 // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1.54030'
165 // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2.63274'
166 // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
167 // test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -2.5e-1'
168 // test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8.549879'
169 // test: echo "1 + -2" | calc.exe | grep -q '=> -1'
170 // test: echo "1 - +2" | calc.exe | grep -q '=> -1'
171 // test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
172 // test: echo "-1+2" | calc.exe | grep -q '=> 1'
173 // test: echo "1-2" | calc.exe | grep -q '=> -1'
174 // test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4.666666'
175 // test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 3.7e1'
176 // test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3.074e3'
177 // test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 3.267241e1'
178 // test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -5e-1'
179 // test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -5e-1'
180 // test: echo "95-6.3+15" | calc.exe | grep -q '=> 1.037e2'
181 // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> 0'
182 // test: echo "quit" | calc.exe | grep -q 'bye'
183 // test: echo "help" | calc.exe | grep -q 'miscellaneous'
184 // test: echo "1 + 2 *" | calc.exe | grep -q 'error'
185 // test: echo "* 1 - 2" | calc.exe | grep -q 'error'
186 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
187 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
188 // test: echo "pow (2)" | calc.exe | grep -q 'error'
189 // test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
190 // test: echo . | calc.exe
191 // 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 6.4e1
192 // 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)\nhelp\nquit' | calc.exe -v 3 | grep -q bye
193 // 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 =
194
195 /* vim: set ts=4 sw=4 et: */