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 fprintf (stderr
, "%s: invalid option -- %s\n", progname
, arg
);
81 buffer
= buffer_static
;
84 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
86 fprintf (stderr
, "%s: missing precision\n", progname
);
89 precision
= atoi (arg
);
92 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
94 fprintf (stderr
, "%s: missing verbose level\n", progname
);
101 return usage (c
!= 'h');
106 char format
[5] = "%..g";
107 format
[2] = '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') {
151 VERBOSE (INFO
, fprintf (stdout
, "line: '%s'\n", buffer
));
154 /* look for end of line */
155 for (i
= 0; i
< nb
; i
++) {
156 if (*line
[i
] == '\0') {
159 element_t
*element
= parser (line
[i
], NULL
, 0);
160 if (element
== ERROR_OP
) {
161 VERBOSE (WARNING
, fprintf (stdout
, "error while parsing: %s\n", line
[i
]));
164 VERBOSE (INFO
, print_element (element
, 0));
165 double value
= evaluate_element (element
, 0);
166 char number
[256] = {0};
167 sprintf (number
, format
, value
);
168 fprintf (stdout
, "=> %s\n", number
);
170 delelement (element
);
178 memset (buffer
, 0, BUFFER_SIZE
);
186 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
187 // test: echo 1 | calc.exe -v3 | grep -q value
188 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
189 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
190 // test: calc.exe error 2>&1 | grep -q 'invalid option'
191 // test: calc.exe -p 2>&1 | grep -q 'missing precision'
192 // test: calc.exe -v 2>&1 | grep -q 'missing verbose'
193 // test: echo "1 + 2" | calc.exe | grep -q '=> 3'
194 // test: echo "1 - 2" | calc.exe | grep -q '=> -1'
195 // test: echo "2 * 3" | calc.exe | grep -q '=> 6'
196 // test: echo "1 / 2" | calc.exe | grep -q '=> 0\.5'
197 // test: echo "8 % 3" | calc.exe | grep -q '=> 2'
198 // test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2\.8'
199 // test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
200 // test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2340\.1'
201 // test: echo "sqrt (2)" | calc.exe | grep -q '=> 1\.41421'
202 // test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
203 // test: echo "cos (2)" | calc.exe | grep -q '=> -0\.416147'
204 // test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297'
205 // test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715'
206 // test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906'
207 // test: echo "log (2)" | calc.exe | grep -q '=> 0\.693147'
208 // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
209 // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403'
210 // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275'
211 // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
212 // test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25'
213 // test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988'
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" | calc.exe | grep -q '=> -1'
219 // test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4\.66667'
220 // test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 37'
221 // test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3074'
222 // test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 32\.6724'
223 // test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -0.5'
224 // test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5'
225 // test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7'
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 "sqrt 2" | calc.exe | grep -q 'error'
233 // test: echo "pow (2)" | calc.exe | grep -q 'error'
234 // test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
235 // test: echo . | calc.exe
236 // 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
237 // 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
238 // 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
239 // 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
240 // 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
241 // 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 =
243 /* vim: set ts=4 sw=4 et: */