3 /* linker: debug.o parser.o -lm -lreadline */
11 #include <readline/readline.h>
12 #include <readline/history.h>
19 #define BUFFER_SIZE 4096
20 #define HISTORY_LEN 10
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))
30 char *progname
= NULL
;
38 FILE *fid
= ret
? stderr
: stdout
;
39 fprintf (fid
, "usage: %s\n", progname
);
40 fprintf (fid
, " -h : help message\n");
41 fprintf (fid
, " -n : no readline mode (%s)\n", mode
? "yes" : "no");
42 fprintf (fid
, " -p : precision (%d)\n", precision
);
43 fprintf (fid
, " -v : verbose level (%d)\n", verbose
);
50 int main (int argc
, char *argv
[])
53 char buffer_static
[BUFFER_SIZE
+ 1] = {0};
60 while (progname
[i
] != '\0') {
61 if ((progname
[i
] == '/') || (progname
[i
] == '\\')) {
69 /* argument processing */
72 char *arg
= *(++argv
);
74 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid option -- '%s'\n", progname
, arg
); usage (1));
81 buffer
= buffer_static
;
84 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
86 VERBOSE (ERROR
, fprintf (stderr
, "%s: missing precision\n", progname
); usage (1));
89 precision
= atoi (arg
);
92 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
94 VERBOSE (ERROR
, fprintf (stderr
, "%s: missing verbose level\n", progname
); usage (1));
101 return usage (c
!= 'h');
106 char format
[9] = "=> %..g\n";
107 format
[5] = '0' + precision
;
109 /* read from input stream */
112 char *line
[BUFFER_SIZE
] = {0};
115 if ((buffer
= readline ("<= ")) == NULL
) {
119 /* check empty line */
120 if (strlen (buffer
) == 0) {
123 } else if (strcmp (buffer
, ".") == 0) {
129 /* add line into history */
130 add_history (buffer
);
131 VERBOSE (INFO
, fprintf (stdout
, "line (%d/%d): '%s'\n",
132 where_history (), history_length
, buffer
));
133 if (history_length
> HISTORY_LEN
) {
134 HIST_ENTRY
*last
= remove_history (0);
136 free_history_entry (last
);
140 if (read (STDIN_FILENO
, buffer
, BUFFER_SIZE
) == 0) {
144 char *pt
= line
[nb
++] = buffer
;
145 while (*pt
!= '\0') {
152 VERBOSE (INFO
, fprintf (stdout
, "line: '%s'\n", buffer
));
155 /* look for end of line */
156 for (i
= 0; i
< nb
; i
++) {
157 if (*line
[i
] == '\0') {
160 element_t
*element
= parser (line
[i
], NULL
, -9);
161 if (element
== ERROR_OP
) {
162 VERBOSE (WARNING
, fprintf (stdout
, "error while parsing: '%s'\n", line
[i
]); fflush (stdout
));
164 } else if (element
!= NULL
) {
165 VERBOSE (INFO
, print_element (element
, 0));
166 answer
= evaluate_element (element
, 0);
167 fprintf (stdout
, format
, answer
);
169 delelement (element
);
177 memset (buffer
, 0, BUFFER_SIZE
);
185 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
186 // test: echo 1 | calc.exe -v3 | grep -q value
187 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
188 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
189 // test: calc.exe error 2>&1 | grep -q 'invalid option'
190 // test: calc.exe -p 2>&1 | grep -q 'missing precision'
191 // test: calc.exe -v 2>&1 | grep -q 'missing verbose'
192 // test: echo "1 + 2" | calc.exe | grep -q '=> 3'
193 // test: echo "1 - 2" | calc.exe | grep -q '=> -1'
194 // test: echo "2 * 3" | calc.exe | grep -q '=> 6'
195 // test: echo "1 / 2" | calc.exe | grep -q '=> 0\.5'
196 // test: echo "8 % 3" | calc.exe | grep -q '=> 2'
197 // test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2\.8'
198 // test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
199 // test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2340\.1'
200 // test: echo "sqrt (2)" | calc.exe | grep -q '=> 1\.41421'
201 // test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
202 // test: echo "cos (2)" | calc.exe | grep -q '=> -0\.416147'
203 // test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297'
204 // test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715'
205 // test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906'
206 // test: echo "log (2)" | calc.exe | grep -q '=> 0\.693147'
207 // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
208 // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403'
209 // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275'
210 // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
211 // test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25'
212 // test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988'
213 // test: echo "1 + -2" | calc.exe | grep -q '=> -1'
214 // test: echo "1 - +2" | calc.exe | grep -q '=> -1'
215 // test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
216 // test: echo "-1+2" | calc.exe | grep -q '=> 1'
217 // test: echo "1-2" | calc.exe | grep -q '=> -1'
218 // test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4\.66667'
219 // test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 37'
220 // test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3074'
221 // test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 32\.6724'
222 // test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -0.5'
223 // test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5'
224 // test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7'
225 // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0'
226 // test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0'
227 // test: echo "quit" | calc.exe | grep -q 'bye'
228 // test: echo "help" | calc.exe | grep -q 'miscellaneous'
229 // test: echo "1 + 2 *" | calc.exe | grep -q 'error'
230 // test: echo "* 1 - 2" | calc.exe | grep -q 'error'
231 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
232 // test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error'
233 // test: echo "2 + (foo)" | calc.exe | grep -q 'error'
234 // test: echo "2 + cos (pi +" | calc.exe | grep -q 'error'
235 // test: echo "2 + cos (pi" | calc.exe | grep -q 'error'
236 // test: echo "(2 + " | calc.exe | grep -q 'error'
237 // test: echo "cos (1, 2)" | calc.exe | grep -q 'error'
238 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
239 // test: echo "pow (2)" | calc.exe | grep -q 'error'
240 // test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
241 // test: echo . | calc.exe
242 // 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
243 // 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
244 // 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
245 // 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
246 // 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
247 // 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 =
248 // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
249 // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
250 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
251 // test: echo -e '\n\n\n' | calc.exe | grep -qv 'error'
252 // test: echo -e '\n\n\n' | calc.exe -n
253 // test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9
254 // test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 =
255 // 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'
256 // test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1'
257 // test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1'
258 // test: echo -e '1 == 1 + 1 == 1' | calc.exe | grep -q '=> 0'
259 // test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 1'
260 // test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0'
261 // test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1'
262 // test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1'
263 // test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1'
264 // test: echo -e '1 > 2' | calc.exe | grep -q '=> 0'
265 // test: echo -e '2 > 2' | calc.exe | grep -q '=> 0'
266 // test: echo -e '1 < 2' | calc.exe | grep -q '=> 1'
267 // test: echo -e '2 < 2' | calc.exe | grep -q '=> 0'
268 // test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye
269 // test: echo -e '(3 == 3) & (2 > 1)' | calc.exe | grep -q '=> 1'
270 // test: echo -e '(3 == 4) & (2 > 1)' | calc.exe | grep -q '=> 0'
271 // test: echo -e '(3 == 3) & (2 > 2)' | calc.exe | grep -q '=> 0'
272 // test: echo -e '(3 == 4) & (2 > 2)' | calc.exe | grep -q '=> 0'
273 // test: echo -e '(3 == 3) | (2 > 1)' | calc.exe | grep -q '=> 1'
274 // test: echo -e '(3 == 4) | (2 > 1)' | calc.exe | grep -q '=> 1'
275 // test: echo -e '(3 == 3) | (2 > 2)' | calc.exe | grep -q '=> 1'
276 // test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0'
277 // test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1'
278 // test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0'
279 // test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -v 3 | grep -qv error
280 // test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1'
281 // test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1'
282 // test: echo -e '1 + quit' | calc.exe | grep -q error
283 // test: echo -e 'cos (quit)' | calc.exe | grep -q error
284 // test: echo -e '(quit)' | calc.exe | grep -q error
285 // test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe | grep -c error | xargs test 3 =
286 // test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 4\.15888'
287 // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 1808\.04'
288 // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64))' | calc.exe | grep -q '=> 0'
289 // test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond
290 // test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 =
291 // test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1'
292 // test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe | grep -c error | xargs test 4 =
293 // test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c invalid | xargs test 2 =
294 // test: echo -e 'whl (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 5050'
295 // test: echo -e 'whl\nwhl (inc (1) < 3,\nwhl (inc (1) < 100, sto (2, rcl (1) + rcl (2))' | calc.exe | grep -c error | xargs test 3 =
296 // test: echo -e 'whl (0, 1)' | calc.exe -v 3 | grep -q While
297 // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe -v 3 | grep -q 'Program'
298 // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6'
299 // 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 =
300 // test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 =
302 // Fibonacci sequence
303 // test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), whl (inc (10) < 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), sto (2, rcl (3))})}' | calc.exe | grep '=> 144'
305 /* vim: set ts=4 sw=4 et: */