| 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 | char *iprompt = "<= "; |
| 32 | int mode = 1; |
| 33 | char *oprompt = "=> "; |
| 34 | int precision = 6; |
| 35 | char **completion_list = NULL; |
| 36 | |
| 37 | /* help function */ |
| 38 | |
| 39 | int usage (int ret) |
| 40 | { |
| 41 | FILE *fid = ret ? stderr : stdout; |
| 42 | fprintf (fid, "usage: %s\n", progname); |
| 43 | fprintf (fid, " -h : help message\n"); |
| 44 | fprintf (fid, " -i : input prompt (%s)\n", iprompt); |
| 45 | fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no"); |
| 46 | fprintf (fid, " -o : output prompt (%s)\n", oprompt); |
| 47 | fprintf (fid, " -p : precision (%d)\n", precision); |
| 48 | fprintf (fid, " -v : verbose level (%d)\n", verbose); |
| 49 | |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | /* completion function */ |
| 54 | |
| 55 | char *generator (const char *text, int state); |
| 56 | |
| 57 | char **completion (const char *text, __attribute__((unused)) int start, __attribute__((unused)) int end) |
| 58 | { |
| 59 | rl_attempted_completion_over = 1; |
| 60 | return rl_completion_matches (text, generator); |
| 61 | } |
| 62 | |
| 63 | char *generator (const char *text, int state) |
| 64 | { |
| 65 | static int index, len; |
| 66 | char *name; |
| 67 | |
| 68 | if (!state) { |
| 69 | index = 0; |
| 70 | len = strlen(text); |
| 71 | } |
| 72 | |
| 73 | while ((name = completion_list[index++])) { |
| 74 | if (strncmp (name, text, len) == 0) { |
| 75 | return strdup (name); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | /* main function */ |
| 83 | |
| 84 | int main (int argc, char *argv[]) |
| 85 | { |
| 86 | char *buffer = NULL; |
| 87 | char buffer_static[BUFFER_SIZE + 1] = {0}; |
| 88 | int i = 0, nb = 1; |
| 89 | int ret = 0; |
| 90 | |
| 91 | /* program name */ |
| 92 | |
| 93 | progname = argv[0]; |
| 94 | while (progname[i] != '\0') { |
| 95 | if ((progname[i] == '/') || (progname[i] == '\\')) { |
| 96 | progname += i + 1; |
| 97 | i = 0; |
| 98 | } else { |
| 99 | i++; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* argument processing */ |
| 104 | |
| 105 | while (argc-- > 1) { |
| 106 | char *arg = *(++argv); |
| 107 | if (arg[0] != '-') { |
| 108 | VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1)); |
| 109 | return 1; |
| 110 | } |
| 111 | char c = arg[1]; |
| 112 | switch (c) { |
| 113 | case 'n': |
| 114 | mode = 0; |
| 115 | buffer = buffer_static; |
| 116 | break; |
| 117 | case 'i': |
| 118 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 119 | if (arg == NULL) { |
| 120 | VERBOSE (ERROR, fprintf (stderr, "%s: missing input prompt\n", progname); usage (1)); |
| 121 | return 1; |
| 122 | } |
| 123 | iprompt = arg; |
| 124 | break; |
| 125 | case 'o': |
| 126 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 127 | if (arg == NULL) { |
| 128 | VERBOSE (ERROR, fprintf (stderr, "%s: missing output prompt\n", progname); usage (1)); |
| 129 | return 1; |
| 130 | } |
| 131 | oprompt = arg; |
| 132 | break; |
| 133 | case 'p': |
| 134 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 135 | if (arg == NULL) { |
| 136 | VERBOSE (ERROR, fprintf (stderr, "%s: missing precision\n", progname); usage (1)); |
| 137 | return 1; |
| 138 | } |
| 139 | precision = atoi (arg); |
| 140 | break; |
| 141 | case 'v': |
| 142 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 143 | if (arg == NULL) { |
| 144 | VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1)); |
| 145 | return 1; |
| 146 | } |
| 147 | verbose = atoi (arg); |
| 148 | break; |
| 149 | case 'h': |
| 150 | default: |
| 151 | return usage (c != 'h'); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* set format */ |
| 156 | set_format (oprompt, precision); |
| 157 | |
| 158 | /* completion list*/ |
| 159 | completion_list = generate_completion_list (); |
| 160 | rl_attempted_completion_function = completion; |
| 161 | |
| 162 | /* read from input stream */ |
| 163 | |
| 164 | while (1) { |
| 165 | char *line[BUFFER_SIZE] = {0}; |
| 166 | |
| 167 | if (mode) { |
| 168 | if ((buffer = readline (iprompt)) == NULL) { |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | /* check empty line */ |
| 173 | if (strlen (buffer) == 0) { |
| 174 | free (buffer); |
| 175 | continue; |
| 176 | } else if (strcmp (buffer, ".") == 0) { |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | /* add line into history */ |
| 181 | add_history (buffer); |
| 182 | VERBOSE (INFO, fprintf (stdout, "line (%d/%d): '%s'\n", |
| 183 | where_history (), history_length, buffer)); |
| 184 | if (history_length > HISTORY_LEN) { |
| 185 | HIST_ENTRY *last = remove_history (0); |
| 186 | if (last) { |
| 187 | free_history_entry (last); |
| 188 | } |
| 189 | } |
| 190 | } else { |
| 191 | printf ("%s", iprompt); |
| 192 | if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) { |
| 193 | break; |
| 194 | } |
| 195 | VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer)); |
| 196 | } |
| 197 | |
| 198 | /* pre-process buffer */ |
| 199 | nb = 0; |
| 200 | char *pt = line[nb++] = buffer; |
| 201 | while (*pt != '\0') { |
| 202 | switch (*pt) { |
| 203 | case '\n': |
| 204 | *pt = '\0'; |
| 205 | // fallthrough |
| 206 | case ';': |
| 207 | line[nb++] = pt + 1; |
| 208 | } |
| 209 | pt++; |
| 210 | } |
| 211 | |
| 212 | /* look for end of line */ |
| 213 | for (i = 0; i < nb; i++) { |
| 214 | if (*line[i] == '\0') { |
| 215 | continue; |
| 216 | } |
| 217 | element_t *element = parser (line[i], NULL, -9); |
| 218 | if (element == ERROR_OP) { |
| 219 | VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i])); |
| 220 | ret = 1; |
| 221 | } else if (element != NULL) { |
| 222 | VERBOSE (INFO, print_element (element, 0)); |
| 223 | answer = evaluate_element (element, 0); |
| 224 | if (!element->hidden) { |
| 225 | print (answer); |
| 226 | } |
| 227 | delelement (element); |
| 228 | ret = 0; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (mode) { |
| 233 | free (buffer); |
| 234 | } else { |
| 235 | memset (buffer, 0, BUFFER_SIZE); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | free_completion_list (completion_list); |
| 240 | |
| 241 | free_format (); |
| 242 | |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | // test: calc.exe -h |
| 247 | // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' |
| 248 | // test: echo 1 | calc.exe -v3 | grep -q value |
| 249 | // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }' |
| 250 | // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' |
| 251 | // test: calc.exe error 2>&1 | grep -q 'invalid option' |
| 252 | // test: calc.exe -i 2>&1 | grep -q 'missing input prompt' |
| 253 | // test: calc.exe -o 2>&1 | grep -q 'missing output prompt' |
| 254 | // test: calc.exe -p 2>&1 | grep -q 'missing precision' |
| 255 | // test: calc.exe -v 2>&1 | grep -q 'missing verbose' |
| 256 | // test: echo "1 + 1" | calc.exe -i '# ' | grep -q '# 1 + 1' |
| 257 | // test: echo "1 + 1" | calc.exe -o '# ' | grep -q '# 2' |
| 258 | // test: echo "1 + 2" | calc.exe | grep -q '=> 3' |
| 259 | // test: echo "1 - 2" | calc.exe | grep -q '=> -1' |
| 260 | // test: echo "2 * 3" | calc.exe | grep -q '=> 6' |
| 261 | // test: echo "1 / 2" | calc.exe | grep -q '=> 0\.5' |
| 262 | // test: echo "8 % 3" | calc.exe | grep -q '=> 2' |
| 263 | // test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2\.8' |
| 264 | // test: echo "2 ^ 3" | calc.exe | grep -q '=> 8' |
| 265 | // test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2340\.1' |
| 266 | // test: echo "sqrt (2)" | calc.exe | grep -q '=> 1\.41421' |
| 267 | // test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8' |
| 268 | // test: echo "cos (2)" | calc.exe | grep -q '=> -0\.416147' |
| 269 | // test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297' |
| 270 | // test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715' |
| 271 | // test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906' |
| 272 | // test: echo "log (2)" | calc.exe | grep -q '=> 0\.693147' |
| 273 | // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1' |
| 274 | // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403' |
| 275 | // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275' |
| 276 | // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3' |
| 277 | // test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25' |
| 278 | // test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988' |
| 279 | // test: echo "1 + -2" | calc.exe | grep -q '=> -1' |
| 280 | // test: echo "1 - +2" | calc.exe | grep -q '=> -1' |
| 281 | // test: echo "-1 + +2" | calc.exe | grep -q '=> 1' |
| 282 | // test: echo "-1+2" | calc.exe | grep -q '=> 1' |
| 283 | // test: echo "1-2" | calc.exe | grep -q '=> -1' |
| 284 | // test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4\.66667' |
| 285 | // test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 37' |
| 286 | // test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3074' |
| 287 | // test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 32\.6724' |
| 288 | // test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -0.5' |
| 289 | // test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5' |
| 290 | // test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7' |
| 291 | // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0' |
| 292 | // test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0' |
| 293 | // test: echo "quit" | calc.exe | grep -q 'bye' |
| 294 | // test: echo "help" | calc.exe | grep -q 'misc\.' |
| 295 | // test: echo "1 + 2 *" | calc.exe | grep -q 'error' |
| 296 | // test: echo "* 1 - 2" | calc.exe | grep -q 'error' |
| 297 | // test: echo "2 + * 3" | calc.exe | grep -q 'error' |
| 298 | // test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error' |
| 299 | // test: echo "2 + (foo)" | calc.exe | grep -q 'error' |
| 300 | // test: echo "2 + cos (pi +" | calc.exe | grep -q 'error' |
| 301 | // test: echo "2 + cos (pi" | calc.exe | grep -q 'error' |
| 302 | // test: echo "(2 + " | calc.exe | grep -q 'error' |
| 303 | // test: echo "cos (1, 2)" | calc.exe | grep -q 'error' |
| 304 | // test: echo "sqrt 2" | calc.exe | grep -q 'error' |
| 305 | // test: echo "pow (2)" | calc.exe | grep -q 'error' |
| 306 | // test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234' |
| 307 | // test: echo . | calc.exe |
| 308 | // 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 |
| 309 | // 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 |
| 310 | // 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 |
| 311 | // 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 |
| 312 | // 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 |
| 313 | // 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 = |
| 314 | // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 = |
| 315 | // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1 |
| 316 | // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906' |
| 317 | // test: echo -e '\n\n\n' | calc.exe | grep -qv 'error' |
| 318 | // test: echo -e '\n\n\n' | calc.exe -n |
| 319 | // test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9 |
| 320 | // test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 = |
| 321 | // 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' |
| 322 | // test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1' |
| 323 | // test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1' |
| 324 | // test: echo -e '1 == 1 + 1 == 1' | calc.exe | grep -q '=> 0' |
| 325 | // test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 1' |
| 326 | // test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0' |
| 327 | // test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1' |
| 328 | // test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1' |
| 329 | // test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1' |
| 330 | // test: echo -e '1 > 2' | calc.exe | grep -q '=> 0' |
| 331 | // test: echo -e '2 > 2' | calc.exe | grep -q '=> 0' |
| 332 | // test: echo -e '1 < 2' | calc.exe | grep -q '=> 1' |
| 333 | // test: echo -e '2 < 2' | calc.exe | grep -q '=> 0' |
| 334 | // test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye |
| 335 | // test: echo -e '(3 == 3) & (2 > 1)' | calc.exe | grep -q '=> 1' |
| 336 | // test: echo -e '(3 == 4) & (2 > 1)' | calc.exe | grep -q '=> 0' |
| 337 | // test: echo -e '(3 == 3) & (2 > 2)' | calc.exe | grep -q '=> 0' |
| 338 | // test: echo -e '(3 == 4) & (2 > 2)' | calc.exe | grep -q '=> 0' |
| 339 | // test: echo -e '(3 == 3) | (2 > 1)' | calc.exe | grep -q '=> 1' |
| 340 | // test: echo -e '(3 == 4) | (2 > 1)' | calc.exe | grep -q '=> 1' |
| 341 | // test: echo -e '(3 == 3) | (2 > 2)' | calc.exe | grep -q '=> 1' |
| 342 | // test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0' |
| 343 | // test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1' |
| 344 | // test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0' |
| 345 | // test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -v 3 | grep -qv error |
| 346 | // test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1' |
| 347 | // test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1' |
| 348 | // test: echo -e '1 + quit' | calc.exe | grep -q error |
| 349 | // test: echo -e 'cos (quit)' | calc.exe | grep -q error |
| 350 | // test: echo -e '(quit)' | calc.exe | grep -q error |
| 351 | // test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe | grep -c error | xargs test 3 = |
| 352 | // test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 4\.15888' |
| 353 | // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 1808\.04' |
| 354 | // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64))' | calc.exe | grep -q '=> 0' |
| 355 | // test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond |
| 356 | // test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 = |
| 357 | // test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1' |
| 358 | // test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe | grep -c error | xargs test 4 = |
| 359 | // test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c invalid | xargs test 2 = |
| 360 | // test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 5050' |
| 361 | // 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 = |
| 362 | // test: echo -e 'while (0, 1)' | calc.exe -v 3 | grep -q While |
| 363 | // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe -v 3 | grep -q 'Code' |
| 364 | // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6' |
| 365 | // 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 = |
| 366 | // test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 = |
| 367 | // test: echo -e 'print (1)' | calc.exe -v 3 | grep -q Print |
| 368 | // test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1' |
| 369 | // test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2 |
| 370 | // test: echo -e 'mem (3)\nsto (4, pi)' | calc.exe | grep -q "invalid index" |
| 371 | // test: echo -e 'sto (2, 3)\nmem (2)\ndisp' | calc.exe | grep -q 'storage: 0 3$' |
| 372 | // test: echo -e 'disp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0" |
| 373 | // test: echo -e 'sto (3, 10)\ndisp' | calc.exe | grep -q "storage: 0 0 10 0 0 0 0 0 0 0" |
| 374 | // test: echo -e 'rcl (3)\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0" |
| 375 | // test: echo -e 'inc (2)\ndisp' | calc.exe | grep -q "storage: 0 1 0 0 0 0 0 0 0 0" |
| 376 | // test: echo -e 'dec (2)\ndisp' | calc.exe | grep -q "storage: 0 -1 0 0 0 0 0 0 0 0" |
| 377 | // test: echo -e 'sto (3, pi)\nclr\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0" |
| 378 | // test: echo -e 'mem (3)\nclr' | calc.exe -v 3 | grep -q Clear |
| 379 | // test: echo -e 'prog (2, 2, {rcl (2) - rcl (1)})\nprog (1, 1, {cos (rcl (1)^2)})\ncall (1, pi/6)\nprog (2, 1, {rcl (1) * 3})\ncall (2, 1, 2)\nls' | calc.exe |
| 380 | // test: echo -e 'prog (1, 2, {rcl (2) - rcl (1)})\ncall (1, 2, 3)\nls\nedit (1)\ndel (1)' | calc.exe -v 3 |grep -q Program |
| 381 | // test: echo -e 'prog (2, 2, {rcl (2) - rcl (1)})\nprog (3, 1, cos(rcl (1) * pi / 3))\ncall (1, 2, 3)\nls\nedit (1)\ndel (1)\n\ndel (2)\ncall (2, 1, 4)' | calc.exe | grep -c error | xargs test 4 = |
| 382 | |
| 383 | // Gauss sequence |
| 384 | // 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' |
| 385 | |
| 386 | // Fibonacci sequence |
| 387 | // 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 -q '=> 144' |
| 388 | |
| 389 | // Gold number |
| 390 | // 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' |
| 391 | |
| 392 | /* vim: set ts=4 sw=4 et: */ |