| 1 | #include <malloc.h> |
| 2 | #include <math.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | |
| 6 | #include "debug.h" |
| 7 | |
| 8 | #include "parser.h" |
| 9 | |
| 10 | /* global variables */ |
| 11 | |
| 12 | double answer = 0; |
| 13 | |
| 14 | /* compare codes */ |
| 15 | |
| 16 | int codecmp (char *ref, char *str) |
| 17 | { |
| 18 | int sig; |
| 19 | |
| 20 | while (*ref != '\0') { |
| 21 | if (*ref == '\t') { |
| 22 | sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9')); |
| 23 | } else { |
| 24 | sig = *str - *ref; |
| 25 | } |
| 26 | if (sig != 0) { |
| 27 | return (sig > 0) ? 1 : -1; |
| 28 | } |
| 29 | str++; |
| 30 | ref++; |
| 31 | } |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | /* allocate new element */ |
| 37 | |
| 38 | element_t *newelement (func_t function, int nbops, int prio) |
| 39 | { |
| 40 | element_t *new = (element_t *) calloc (1, sizeof (element_t)); |
| 41 | if (new == NULL) { |
| 42 | VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n")); |
| 43 | return NULL; |
| 44 | } |
| 45 | new->func = function; |
| 46 | new->nbops = nbops; |
| 47 | new->prio = prio; |
| 48 | |
| 49 | return new; |
| 50 | } |
| 51 | |
| 52 | /* desallocate element */ |
| 53 | |
| 54 | void delelement (element_t *root) |
| 55 | { |
| 56 | int i; |
| 57 | if ((root != NULL) && (root != ERROR_OP)) { |
| 58 | for (i = 0; i < root->nbops; i++) { |
| 59 | if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) { |
| 60 | delelement (root->ops[i]); |
| 61 | } |
| 62 | } |
| 63 | free (root); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* functions */ |
| 68 | |
| 69 | #define NB_OPERATORS 6 |
| 70 | |
| 71 | keyword_t operators[NB_OPERATORS] = { |
| 72 | { "+\t", Add, 2, 1, 1}, |
| 73 | { "-\t", Sub, 2, 1, 1}, |
| 74 | { "*", Mul, 2, 1, 2}, |
| 75 | { "/", Div, 2, 1, 2}, |
| 76 | { "%", Mod, 2, 1, 3}, |
| 77 | { "^", Pow, 2, 1, 4} |
| 78 | }; |
| 79 | |
| 80 | #define NB_FUNCTIONS 9 |
| 81 | keyword_t functions[NB_FUNCTIONS] = { |
| 82 | { "sqrt", Sqr, 1, 4, 5}, |
| 83 | { "pow", Pow, 2, 3, 5}, |
| 84 | { "cos", Cos, 1, 3, 5}, |
| 85 | { "sin", Sin, 1, 3, 5}, |
| 86 | { "atan", Atan, 1, 4, 5}, |
| 87 | { "exp", Exp, 1, 3, 5}, |
| 88 | { "log", Log, 1, 3, 5}, |
| 89 | { "quit", Quit, 0, 4, 5}, |
| 90 | { "help", Help, 0, 4, 5} |
| 91 | }; |
| 92 | |
| 93 | #define NB_CONSTANTS 3 |
| 94 | keyword_t constants[NB_CONSTANTS] = { |
| 95 | { "ans", Ans, 0, 3, 5}, |
| 96 | { "e", E, 0, 1, 5}, |
| 97 | { "pi", Pi, 0, 2, 5} |
| 98 | }; |
| 99 | |
| 100 | /* subparser function */ |
| 101 | |
| 102 | element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio) |
| 103 | { |
| 104 | element_t *new = newelement (func, nbops, prio); |
| 105 | if (new == NULL) { |
| 106 | return ERROR_OP; |
| 107 | } |
| 108 | new->ops[0] = *proot; |
| 109 | new->ops[1] = parser (*pstr, pstr, new->prio); |
| 110 | if (new->ops[1] == ERROR_OP) { |
| 111 | delelement (new); |
| 112 | *proot = NULL; |
| 113 | return ERROR_OP; |
| 114 | } |
| 115 | *proot = newelement (Val, 1, 5); |
| 116 | if (*proot == NULL) { |
| 117 | delelement (new); |
| 118 | return ERROR_OP; |
| 119 | } |
| 120 | (*proot)->ops[0] = new; |
| 121 | |
| 122 | return *proot; |
| 123 | } |
| 124 | |
| 125 | /* parser function */ |
| 126 | |
| 127 | element_t *parser (char *str, char **next, int prio) |
| 128 | { |
| 129 | element_t *root = NULL; |
| 130 | int i; |
| 131 | |
| 132 | VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n")); |
| 133 | |
| 134 | /* main loop */ |
| 135 | while (*str != '\0') { |
| 136 | int found = 0; |
| 137 | element_t *new = NULL; |
| 138 | VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str)); |
| 139 | |
| 140 | /* skip spaces and tabs */ |
| 141 | |
| 142 | if ((*str == ' ') || (*str == '\t')) { |
| 143 | str++; |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | /* check for open bracket */ |
| 148 | |
| 149 | if (*str == '(') { |
| 150 | VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n")); |
| 151 | if (root) { |
| 152 | do { |
| 153 | found = 0; |
| 154 | new = parser (str + 1, &str, 0); |
| 155 | if (new == ERROR_OP) { |
| 156 | delelement (root); |
| 157 | return ERROR_OP; |
| 158 | } |
| 159 | for (i = 0; i < root->nbops; i++) { |
| 160 | if (root->ops[i] == NULL) { |
| 161 | root->ops[i] = new; |
| 162 | found = 1; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | if (!found) { |
| 167 | delelement (new); |
| 168 | delelement (root); |
| 169 | return ERROR_OP; |
| 170 | } |
| 171 | } while (*str == ','); |
| 172 | } else { |
| 173 | root = newelement (Val, 1, 5); |
| 174 | if (root == NULL) { |
| 175 | return ERROR_OP; |
| 176 | } |
| 177 | new = parser (str + 1, &str, 0); |
| 178 | if ((new == ERROR_OP) || (*str == ',')) { |
| 179 | delelement (new); |
| 180 | delelement (root); |
| 181 | return ERROR_OP; |
| 182 | } |
| 183 | root->ops[0] = new; |
| 184 | } |
| 185 | str++; |
| 186 | VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n")); |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | /* check for closing bracket or koma */ |
| 191 | |
| 192 | if ((*str == ')') || (*str == ',')) { |
| 193 | if (next != NULL) { |
| 194 | *next = str; |
| 195 | } |
| 196 | return root; |
| 197 | } |
| 198 | |
| 199 | /* look for operators */ |
| 200 | |
| 201 | for (i = 0; i < NB_OPERATORS; i++) { |
| 202 | keyword_t *operator = operators + i; |
| 203 | if (codecmp (operator->keyword, str) == 0) { |
| 204 | VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n")); |
| 205 | if (root) { |
| 206 | if ((prio) && (prio > operator->prio)) { |
| 207 | VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n")); |
| 208 | *next = str; |
| 209 | return root; |
| 210 | } |
| 211 | str += operator->offset; |
| 212 | VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func)); |
| 213 | if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) { |
| 214 | delelement (root); |
| 215 | return ERROR_OP; |
| 216 | } |
| 217 | } else if (*str == '-') { |
| 218 | new = newelement (Sig, 1, 9); |
| 219 | if (new == NULL) { |
| 220 | return ERROR_OP; |
| 221 | } |
| 222 | root = new; |
| 223 | } else { |
| 224 | return ERROR_OP; |
| 225 | } |
| 226 | found = 1; |
| 227 | VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n")); |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | if (found) { |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | /* look for functions */ |
| 236 | |
| 237 | for (i = 0; i < NB_FUNCTIONS; i++) { |
| 238 | keyword_t *function = functions + i; |
| 239 | if (codecmp (function->keyword, str) == 0) { |
| 240 | VERBOSE (DEBUG, fprintf (stdout, "start processing function\n")); |
| 241 | if (root == NULL) { |
| 242 | VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func)); |
| 243 | new = newelement (function->func, function->nbops, function->prio); |
| 244 | if (new == NULL) { |
| 245 | return ERROR_OP; |
| 246 | } |
| 247 | root = new; |
| 248 | } else { |
| 249 | delelement (root); |
| 250 | return ERROR_OP; |
| 251 | } |
| 252 | str += function->offset; |
| 253 | found = 1; |
| 254 | VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n")); |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | if (found) { |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | /* look for constant */ |
| 263 | |
| 264 | for (i = 0; i < NB_CONSTANTS; i++) { |
| 265 | keyword_t *constant = constants + i; |
| 266 | if (codecmp (constant->keyword, str) == 0) { |
| 267 | VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n")); |
| 268 | if (root == NULL) { |
| 269 | VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func)); |
| 270 | new = newelement (constant->func, constant->nbops, constant->prio); |
| 271 | if (new == NULL) { |
| 272 | return ERROR_OP; |
| 273 | } |
| 274 | root = new; |
| 275 | } else { |
| 276 | delelement (root); |
| 277 | return ERROR_OP; |
| 278 | } |
| 279 | str += constant->offset; |
| 280 | found = 1; |
| 281 | VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n")); |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | if (found) { |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | /* look for number */ |
| 290 | |
| 291 | if (((*str >= '0') && (*str <= '9')) || |
| 292 | (*str == '.') || (*str == '+') || (*str == '-')) { |
| 293 | VERBOSE (DEBUG, fprintf (stdout, "start processing value\n")); |
| 294 | char *pt; |
| 295 | double value = strtod (str, &pt); |
| 296 | VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value)); |
| 297 | if (str != pt) { |
| 298 | if (root == NULL) { |
| 299 | new = newelement (Val, 1, 5); |
| 300 | if (new == NULL) { |
| 301 | return ERROR_OP; |
| 302 | } |
| 303 | new->value = value; |
| 304 | root = new; |
| 305 | str = pt; |
| 306 | } else if ((*str == '+') || (*str == '-')) { |
| 307 | if ((prio) && (prio > 1)) { |
| 308 | VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n")); |
| 309 | *next = str; |
| 310 | return root; |
| 311 | } |
| 312 | if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) { |
| 313 | delelement (root); |
| 314 | return ERROR_OP; |
| 315 | } |
| 316 | } else { |
| 317 | delelement (root); |
| 318 | return ERROR_OP; |
| 319 | } |
| 320 | found = 1; |
| 321 | } |
| 322 | VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n")); |
| 323 | } |
| 324 | |
| 325 | /* error */ |
| 326 | |
| 327 | if (!found) { |
| 328 | delelement (root); |
| 329 | return ERROR_OP; |
| 330 | } |
| 331 | |
| 332 | } |
| 333 | |
| 334 | if (next != NULL) { |
| 335 | *next = str; |
| 336 | } |
| 337 | |
| 338 | return root; |
| 339 | } |
| 340 | |
| 341 | /* print element tree */ |
| 342 | |
| 343 | void print_element (element_t *root, int level) |
| 344 | { |
| 345 | char *func = NULL; |
| 346 | int i; |
| 347 | |
| 348 | if ((root == NULL) || (root == ERROR_OP)) { |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | for (i = 0; i < level; i++) { |
| 353 | fprintf (stdout, " "); |
| 354 | } |
| 355 | |
| 356 | switch (root->func) { |
| 357 | case Val: func = "Value"; break; |
| 358 | case Sig: func = "Sign"; break; |
| 359 | case Add: func = "Addition"; break; |
| 360 | case Sub: func = "Subtraction"; break; |
| 361 | case Mul: func = "Multiplication"; break; |
| 362 | case Div: func = "Division"; break; |
| 363 | case Mod: func = "Modulo"; break; |
| 364 | case Pow: func = "Power"; break; |
| 365 | case Sqr: func = "Square Root"; break; |
| 366 | case Cos: func = "Cosine"; break; |
| 367 | case Sin: func = "Sine"; break; |
| 368 | case Atan: func = "Arc Tangent"; break; |
| 369 | case Log: func = "Logarithm"; break; |
| 370 | case Exp: func = "Exponantial"; break; |
| 371 | case Quit: func = "Quit"; break; |
| 372 | case Help: func = "Help"; break; |
| 373 | case Ans: func = "Ans"; break; |
| 374 | case Pi: func = "Pi"; break; |
| 375 | case E: func = "E"; break; |
| 376 | } |
| 377 | |
| 378 | fprintf (stdout, "Function: %s\n", func); |
| 379 | |
| 380 | if ((root->func == Val) && (root->ops[0] == NULL)) { |
| 381 | for (i = 0; i < level; i++) { |
| 382 | fprintf (stdout, " "); |
| 383 | } |
| 384 | fprintf (stdout, "value: %f\n", root->value); |
| 385 | } else { |
| 386 | for (i = 0; i < root->nbops; i++) { |
| 387 | print_element (root->ops[i], level + 1); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /* quit function */ |
| 393 | |
| 394 | void quit (void) |
| 395 | { |
| 396 | fprintf (stdout, "bye\n"); |
| 397 | exit (0); |
| 398 | } |
| 399 | |
| 400 | /* help message */ |
| 401 | |
| 402 | void help (void) |
| 403 | { |
| 404 | fprintf (stdout, "calc is a simple calculator\n\n"); |
| 405 | fprintf (stdout, "supported operators:\n"); |
| 406 | fprintf (stdout, " + - * / %% ^\n\n"); |
| 407 | fprintf (stdout, "supported functions:\n"); |
| 408 | fprintf (stdout, " pow sqrt cos sin atan log exp\n\n"); |
| 409 | fprintf (stdout, "miscellaneous functions:\n"); |
| 410 | fprintf (stdout, " quit help\n\n"); |
| 411 | fprintf (stdout, "supported constants:\n"); |
| 412 | fprintf (stdout, " e pi\n"); |
| 413 | } |
| 414 | |
| 415 | /* evaluate element tree */ |
| 416 | |
| 417 | #define MASK_SUB 0x1 |
| 418 | #define MASK_DIV 0x2 |
| 419 | |
| 420 | double evaluate_element (element_t *root, char mask) |
| 421 | { |
| 422 | double op0 = 0, op1 = 0; |
| 423 | char nextmask = mask; |
| 424 | |
| 425 | if ((root == NULL) || (root == ERROR_OP)) { |
| 426 | VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n")); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* mask to manage sub operator sub and div */ |
| 431 | switch (root->func) { |
| 432 | case Add: |
| 433 | nextmask &= ~MASK_SUB; |
| 434 | nextmask &= ~MASK_DIV; |
| 435 | break; |
| 436 | case Sub: |
| 437 | nextmask |= MASK_SUB; |
| 438 | nextmask &= ~MASK_DIV; |
| 439 | break; |
| 440 | case Mul: |
| 441 | nextmask &= ~MASK_DIV; |
| 442 | break; |
| 443 | case Div: |
| 444 | nextmask |= MASK_DIV; |
| 445 | break; |
| 446 | default: |
| 447 | nextmask = mask; |
| 448 | } |
| 449 | |
| 450 | switch (root->func) { |
| 451 | case Val: |
| 452 | case Sig: |
| 453 | op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value; |
| 454 | break; |
| 455 | case Add: |
| 456 | case Sub: |
| 457 | case Mul: |
| 458 | case Div: |
| 459 | case Mod: |
| 460 | case Pow: |
| 461 | if (root->ops[1]) { |
| 462 | op1 = evaluate_element (root->ops[1], nextmask); |
| 463 | } else { |
| 464 | VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n")); |
| 465 | return 0; |
| 466 | } |
| 467 | /* fallthrough */ |
| 468 | case Sqr: |
| 469 | case Cos: |
| 470 | case Sin: |
| 471 | case Atan: |
| 472 | case Log: |
| 473 | case Exp: |
| 474 | if (root->ops[0]) { |
| 475 | op0 = evaluate_element (root->ops[0], 0); |
| 476 | } else { |
| 477 | VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n")); |
| 478 | return 0; |
| 479 | } |
| 480 | break; |
| 481 | case Quit: |
| 482 | case Help: |
| 483 | case Ans: |
| 484 | case Pi: |
| 485 | case E: |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | switch (root->func) { |
| 490 | case Val: return op0; |
| 491 | case Sig: return -op0; |
| 492 | case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1; |
| 493 | case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1; |
| 494 | case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1; |
| 495 | case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1; |
| 496 | case Mod: return fmod (op0, op1); |
| 497 | case Pow: return pow (op0, op1); |
| 498 | case Sqr: return sqrt (op0); |
| 499 | case Cos: return cos (op0); |
| 500 | case Sin: return sin (op0); |
| 501 | case Atan: return atan (op0); |
| 502 | case Log: return log (op0); |
| 503 | case Exp: return exp (op0); |
| 504 | case Quit: quit (); break; |
| 505 | case Help: help (); break; |
| 506 | case Ans: return answer; |
| 507 | case Pi: return M_PI; |
| 508 | case E: return M_E; |
| 509 | } |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | /* vim: set ts=4 sw=4 et: */ |