8081c804963290490c166fef5eb5cf1d1ef2425c
[calc.git] / calc.c
1 /* depend: */
2 /* cflags: */
3 /* linker: debug.o parser.o -lm -lreadline */
4
5 #include <malloc.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10
11 #include <readline/readline.h>
12 #include <readline/history.h>
13
14 #include "debug.h"
15 #include "parser.h"
16
17 /* constants */
18
19 #define BUFFER_SIZE 4096
20 #define HISTORY_LEN 10
21
22 /* macros */
23
24 #define CEIL(x, y) (((x) + (y) - 1) / (y))
25 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
26 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
27
28 /* gobal variables */
29
30 char *progname = NULL;
31 int mode = 1;
32 int precision = 6;
33 char **completion_list = NULL;
34
35 /* help function */
36
37 int usage (int ret)
38 {
39 FILE *fid = ret ? stderr : stdout;
40 fprintf (fid, "usage: %s\n", progname);
41 fprintf (fid, " -h : help message\n");
42 fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
43 fprintf (fid, " -p : precision (%d)\n", precision);
44 fprintf (fid, " -v : verbose level (%d)\n", verbose);
45
46 return ret;
47 }
48
49 /* completion function */
50
51 char *generator (const char *text, int state);
52
53 char **completion (const char *text, __attribute__((unused)) int start, __attribute__((unused)) int end)
54 {
55 rl_attempted_completion_over = 1;
56 return rl_completion_matches (text, generator);
57 }
58
59 char *generator (const char *text, int state)
60 {
61 static int index, len;
62 char *name;
63
64 if (!state) {
65 index = 0;
66 len = strlen(text);
67 }
68
69 while ((name = completion_list[index++])) {
70 if (strncmp (name, text, len) == 0) {
71 return strdup (name);
72 }
73 }
74
75 return NULL;
76 }
77
78 /* main function */
79
80 int main (int argc, char *argv[])
81 {
82 char *buffer = NULL;
83 char buffer_static[BUFFER_SIZE + 1] = {0};
84 int i = 0, nb = 1;
85 int ret = 0;
86
87 /* program name */
88
89 progname = argv[0];
90 while (progname[i] != '\0') {
91 if ((progname[i] == '/') || (progname[i] == '\\')) {
92 progname += i + 1;
93 i = 0;
94 } else {
95 i++;
96 }
97 }
98
99 /* argument processing */
100
101 while (argc-- > 1) {
102 char *arg = *(++argv);
103 if (arg[0] != '-') {
104 VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1));
105 return 1;
106 }
107 char c = arg[1];
108 switch (c) {
109 case 'n':
110 mode = 0;
111 buffer = buffer_static;
112 break;
113 case 'p':
114 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
115 if (arg == NULL) {
116 VERBOSE (ERROR, fprintf (stderr, "%s: missing precision\n", progname); usage (1));
117 return 1;
118 }
119 precision = atoi (arg);
120 break;
121 case 'v':
122 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
123 if (arg == NULL) {
124 VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1));
125 return 1;
126 }
127 verbose = atoi (arg);
128 break;
129 case 'h':
130 default:
131 return usage (c != 'h');
132 }
133 }
134
135 /* format */
136 format[5] = '0' + precision;
137
138 /* completion list*/
139 completion_list = generate_completion_list ();
140 rl_attempted_completion_function = completion;
141
142 /* read from input stream */
143
144 while (1) {
145 char *line[BUFFER_SIZE] = {0};
146
147 if (mode) {
148 if ((buffer = readline ("<= ")) == NULL) {
149 break;
150 }
151
152 /* check empty line */
153 if (strlen (buffer) == 0) {
154 free (buffer);
155 continue;
156 } else if (strcmp (buffer, ".") == 0) {
157 break;
158 }
159 line[0] = buffer;
160 nb = 1;
161
162 /* add line into history */
163 add_history (buffer);
164 VERBOSE (INFO, fprintf (stdout, "line (%d/%d): '%s'\n",
165 where_history (), history_length, buffer));
166 if (history_length > HISTORY_LEN) {
167 HIST_ENTRY *last = remove_history (0);
168 if (last) {
169 free_history_entry (last);
170 }
171 }
172 } else {
173 if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) {
174 break;
175 }
176 nb = 0;
177 char *pt = line[nb++] = buffer;
178 while (*pt != '\0') {
179 if (*pt == '\n') {
180 *pt = '\0';
181 line[nb++] = pt + 1;
182 }
183 pt++;
184 }
185 VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
186 }
187
188 /* look for end of line */
189 for (i = 0; i < nb; i++) {
190 if (*line[i] == '\0') {
191 continue;
192 }
193 element_t *element = parser (line[i], NULL, -9);
194 if (element == ERROR_OP) {
195 VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]); fflush (stdout));
196 ret = 1;
197 } else if (element != NULL) {
198 VERBOSE (INFO, print_element (element, 0));
199 answer = evaluate_element (element, 0);
200 fprintf (stdout, format, answer);
201 fflush (stdout);
202 delelement (element);
203 ret = 0;
204 }
205 }
206
207 if (mode) {
208 free (buffer);
209 } else {
210 memset (buffer, 0, BUFFER_SIZE);
211 }
212 }
213
214 free_completion_list (completion_list);
215
216 return ret;
217 }
218
219 // test: calc.exe -h
220 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
221 // test: echo 1 | calc.exe -v3 | grep -q value
222 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
223 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
224 // test: calc.exe error 2>&1 | grep -q 'invalid option'
225 // test: calc.exe -p 2>&1 | grep -q 'missing precision'
226 // test: calc.exe -v 2>&1 | grep -q 'missing verbose'
227 // test: echo "1 + 2" | calc.exe | grep -q '=> 3'
228 // test: echo "1 - 2" | calc.exe | grep -q '=> -1'
229 // test: echo "2 * 3" | calc.exe | grep -q '=> 6'
230 // test: echo "1 / 2" | calc.exe | grep -q '=> 0\.5'
231 // test: echo "8 % 3" | calc.exe | grep -q '=> 2'
232 // test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2\.8'
233 // test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
234 // test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2340\.1'
235 // test: echo "sqrt (2)" | calc.exe | grep -q '=> 1\.41421'
236 // test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
237 // test: echo "cos (2)" | calc.exe | grep -q '=> -0\.416147'
238 // test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297'
239 // test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715'
240 // test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906'
241 // test: echo "log (2)" | calc.exe | grep -q '=> 0\.693147'
242 // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
243 // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403'
244 // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275'
245 // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
246 // test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25'
247 // test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988'
248 // test: echo "1 + -2" | calc.exe | grep -q '=> -1'
249 // test: echo "1 - +2" | calc.exe | grep -q '=> -1'
250 // test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
251 // test: echo "-1+2" | calc.exe | grep -q '=> 1'
252 // test: echo "1-2" | calc.exe | grep -q '=> -1'
253 // test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4\.66667'
254 // test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 37'
255 // test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3074'
256 // test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 32\.6724'
257 // test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -0.5'
258 // test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5'
259 // test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7'
260 // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0'
261 // test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0'
262 // test: echo "quit" | calc.exe | grep -q 'bye'
263 // test: echo "help" | calc.exe | grep -q 'miscellaneous'
264 // test: echo "1 + 2 *" | calc.exe | grep -q 'error'
265 // test: echo "* 1 - 2" | calc.exe | grep -q 'error'
266 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
267 // test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error'
268 // test: echo "2 + (foo)" | calc.exe | grep -q 'error'
269 // test: echo "2 + cos (pi +" | calc.exe | grep -q 'error'
270 // test: echo "2 + cos (pi" | calc.exe | grep -q 'error'
271 // test: echo "(2 + " | calc.exe | grep -q 'error'
272 // test: echo "cos (1, 2)" | calc.exe | grep -q 'error'
273 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
274 // test: echo "pow (2)" | calc.exe | grep -q 'error'
275 // test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
276 // test: echo . | calc.exe
277 // 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 -n | grep -q 64
278 // 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 -n | grep -q 2
279 // 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
280 // 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
281 // test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\ntan (0)\nacos (0)\nasin (0)\natan (0)\nlog (1)\nexp (1)\nabs (-1)\nceil (1.2)\nfloor (-1.2)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -v 3 | grep -q bye
282 // test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\ntan ()\nacos ()\nasin ()\natan ()\nlog ()\nexp ()\nabs ()\nceil ()\nfloor ()\n1 + (\n1+2(\n1+2cos\n1+2pi' | calc.exe | grep -c error | xargs test 21 =
283 // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
284 // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
285 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
286 // test: echo -e '\n\n\n' | calc.exe | grep -qv 'error'
287 // test: echo -e '\n\n\n' | calc.exe -n
288 // test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9
289 // test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 =
290 // test: echo -e '1\nsto (2)\n3\nsto (5, 7)\nsto(9)\ndisp' | calc.exe | grep -q '0 1 0 0 7 0 0 0 7 0'
291 // test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1'
292 // test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1'
293 // test: echo -e '1 == 1 + 1 == 1' | calc.exe | grep -q '=> 0'
294 // test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 1'
295 // test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0'
296 // test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1'
297 // test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1'
298 // test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1'
299 // test: echo -e '1 > 2' | calc.exe | grep -q '=> 0'
300 // test: echo -e '2 > 2' | calc.exe | grep -q '=> 0'
301 // test: echo -e '1 < 2' | calc.exe | grep -q '=> 1'
302 // test: echo -e '2 < 2' | calc.exe | grep -q '=> 0'
303 // test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye
304 // test: echo -e '(3 == 3) & (2 > 1)' | calc.exe | grep -q '=> 1'
305 // test: echo -e '(3 == 4) & (2 > 1)' | calc.exe | grep -q '=> 0'
306 // test: echo -e '(3 == 3) & (2 > 2)' | calc.exe | grep -q '=> 0'
307 // test: echo -e '(3 == 4) & (2 > 2)' | calc.exe | grep -q '=> 0'
308 // test: echo -e '(3 == 3) | (2 > 1)' | calc.exe | grep -q '=> 1'
309 // test: echo -e '(3 == 4) | (2 > 1)' | calc.exe | grep -q '=> 1'
310 // test: echo -e '(3 == 3) | (2 > 2)' | calc.exe | grep -q '=> 1'
311 // test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0'
312 // test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1'
313 // test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0'
314 // test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -v 3 | grep -qv error
315 // test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1'
316 // test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1'
317 // test: echo -e '1 + quit' | calc.exe | grep -q error
318 // test: echo -e 'cos (quit)' | calc.exe | grep -q error
319 // test: echo -e '(quit)' | calc.exe | grep -q error
320 // test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe | grep -c error | xargs test 3 =
321 // test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 4\.15888'
322 // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 1808\.04'
323 // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64))' | calc.exe | grep -q '=> 0'
324 // test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond
325 // test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 =
326 // test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1'
327 // test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe | grep -c error | xargs test 4 =
328 // test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c invalid | xargs test 2 =
329 // test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 5050'
330 // test: echo -e 'while\nwhile (inc (1) < 3,\nwhile (inc (1) < 100, sto (2, rcl (1) + rcl (2))' | calc.exe | grep -c error | xargs test 3 =
331 // test: echo -e 'while (0, 1)' | calc.exe -v 3 | grep -q While
332 // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe -v 3 | grep -q 'Program'
333 // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6'
334 // test: echo -e '{\n{}\n{1, 2\n{sto (1, 1 + 1),\npow(2, {sto (1, 1 + 2), 2}, {rcl(2)})\n2 {sto (1, 1 + 1)}' | calc.exe | grep -c error | xargs test 6 =
335 // test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 =
336 // test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1'
337
338 // Gauss sequence
339 // 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'
340
341 // Fibonacci sequence
342 // test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)))})}' | calc.exe | grep '=> 144'
343
344 /* vim: set ts=4 sw=4 et: */