| 1 | #include <malloc.h> |
| 2 | #include <math.h> |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | #include "debug.h" |
| 6 | #include "fdprintf.h" |
| 7 | |
| 8 | #include "parser.h" |
| 9 | |
| 10 | /* compare codes */ |
| 11 | |
| 12 | int codecmp (char *ref, char *str) |
| 13 | { |
| 14 | int sig; |
| 15 | |
| 16 | while (*ref != '\0') { |
| 17 | if (*ref == '\t') { |
| 18 | sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9')); |
| 19 | } else { |
| 20 | sig = *str - *ref; |
| 21 | } |
| 22 | if (sig != 0) { |
| 23 | return (sig > 0) ? 1 : -1; |
| 24 | } |
| 25 | str++; |
| 26 | ref++; |
| 27 | } |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | /* allocate new element */ |
| 33 | |
| 34 | element_t *newelement (func_t function, int nbops, int prio) |
| 35 | { |
| 36 | element_t *new = (element_t *) calloc (1, sizeof (element_t)); |
| 37 | if (new == NULL) { |
| 38 | VERBOSE (ERROR, fdprintf (stdfderr, "can't allocate memory\n")); |
| 39 | return NULL; |
| 40 | } |
| 41 | new->func = function; |
| 42 | new->nbops = nbops; |
| 43 | new->prio = prio; |
| 44 | |
| 45 | return new; |
| 46 | } |
| 47 | |
| 48 | /* functions */ |
| 49 | |
| 50 | #define NB_OPERATORS 5 |
| 51 | |
| 52 | keyword_t operators[NB_OPERATORS] = { |
| 53 | { "+\t", Add, 2, 1, 1}, |
| 54 | { "-\t", Sub, 2, 1, 1}, |
| 55 | { "*", Mul, 2, 1, 2}, |
| 56 | { "/", Div, 2, 1, 2}, |
| 57 | { "^", Pow, 2, 1, 3} |
| 58 | }; |
| 59 | |
| 60 | #define NB_FUNCTIONS 7 |
| 61 | keyword_t functions[NB_FUNCTIONS] = { |
| 62 | { "sqrt", Sqr, 1, 4, 4}, |
| 63 | { "pow", Pow, 2, 3, 4}, |
| 64 | { "cos", Cos, 1, 3, 4}, |
| 65 | { "sin", Sin, 1, 3, 4}, |
| 66 | { "atan", Atn, 1, 4, 4}, |
| 67 | { "exp", Exp, 1, 3, 4}, |
| 68 | { "log", Log, 1, 3, 4} |
| 69 | }; |
| 70 | |
| 71 | /* parser function */ |
| 72 | |
| 73 | element_t *parser (char *str, char **next, int prio) |
| 74 | { |
| 75 | element_t *root = NULL; |
| 76 | int i; |
| 77 | |
| 78 | VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n")); |
| 79 | |
| 80 | /* main loop */ |
| 81 | while (*str != '\0') { |
| 82 | int found = 0; |
| 83 | element_t *new = NULL; |
| 84 | VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str)); |
| 85 | |
| 86 | /* skip spaces and tabs */ |
| 87 | |
| 88 | if ((*str == ' ') || (*str == '\t')) { |
| 89 | str++; |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | /* check for open bracket */ |
| 94 | |
| 95 | if (*str == '(') { |
| 96 | VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n")); |
| 97 | if (root) { |
| 98 | do { |
| 99 | found = 0; |
| 100 | new = parser (str + 1, &str, 0); |
| 101 | if (new == ERROR_OP) { |
| 102 | return ERROR_OP; |
| 103 | } |
| 104 | for (i = 0; i < root->nbops; i++) { |
| 105 | if (root->ops[i] == NULL) { |
| 106 | root->ops[i] = new; |
| 107 | found = 1; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | if (!found) { |
| 112 | return ERROR_OP; |
| 113 | } |
| 114 | } while (*str == ','); |
| 115 | } else { |
| 116 | root = newelement (Val, 1, 4); |
| 117 | if (root == NULL) { |
| 118 | return ERROR_OP; |
| 119 | } |
| 120 | new = parser (str + 1, &str, 0); |
| 121 | if ((new == ERROR_OP) || (*str == ',')) { |
| 122 | return ERROR_OP; |
| 123 | } |
| 124 | root->ops[0] = new; |
| 125 | } |
| 126 | str++; |
| 127 | VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n")); |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | /* check for closing bracket or koma */ |
| 132 | |
| 133 | if ((*str == ')') || (*str == ',')) { |
| 134 | if (next != NULL) { |
| 135 | *next = str; |
| 136 | } |
| 137 | return root; |
| 138 | } |
| 139 | |
| 140 | /* look for operators */ |
| 141 | |
| 142 | for (i = 0; i < NB_OPERATORS; i++) { |
| 143 | keyword_t *operator = operators + i; |
| 144 | if (codecmp (operator->keyword, str) == 0) { |
| 145 | VERBOSE (DEBUG, PRINTOUT ("start processing operator\n")); |
| 146 | if (root) { |
| 147 | if ((prio) && (prio > operator->prio)) { |
| 148 | VERBOSE (DEBUG, PRINTOUT ("stop processing operator because operator priority\n")); |
| 149 | *next = str; |
| 150 | return root; |
| 151 | } |
| 152 | str += operator->offset; |
| 153 | VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func)); |
| 154 | new = newelement (operator->func, operator->nbops, operator->prio); |
| 155 | if (new == NULL) { |
| 156 | return ERROR_OP; |
| 157 | } |
| 158 | new->ops[0] = root; |
| 159 | new->ops[1] = parser (str, &str, new->prio); |
| 160 | if (new->ops[1] == ERROR_OP) { |
| 161 | return ERROR_OP; |
| 162 | } |
| 163 | root = newelement (Val, 1, 4); |
| 164 | if (root == ERROR_OP) { |
| 165 | return ERROR_OP; |
| 166 | } |
| 167 | root->ops[0] = new; |
| 168 | } else { |
| 169 | return ERROR_OP; |
| 170 | } |
| 171 | found = 1; |
| 172 | VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n")); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | if (found) { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | /* look for functions */ |
| 181 | |
| 182 | for (i = 0; i < NB_FUNCTIONS; i++) { |
| 183 | keyword_t *function = functions + i; |
| 184 | if (codecmp (function->keyword, str) == 0) { |
| 185 | VERBOSE (DEBUG, PRINTOUT ("start processing function\n")); |
| 186 | if (root == NULL) { |
| 187 | VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func)); |
| 188 | new = newelement (function->func, function->nbops, function->prio); |
| 189 | if (new == NULL) { |
| 190 | return ERROR_OP; |
| 191 | } |
| 192 | root = new; |
| 193 | } else { |
| 194 | return ERROR_OP; |
| 195 | } |
| 196 | str += function->offset; |
| 197 | found = 1; |
| 198 | VERBOSE (DEBUG, PRINTOUT ("stop processing function\n")); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | if (found) { |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | /* look for number */ |
| 207 | |
| 208 | if (((*str >= '0') && (*str <= '9')) || |
| 209 | (*str == '.') || (*str == '+') || (*str == '-')) { |
| 210 | VERBOSE (DEBUG, PRINTOUT ("start processing value\n")); |
| 211 | char *pt; |
| 212 | float value = strtof (str, &pt); |
| 213 | VERBOSE (INFO, PRINTOUT ("Value: %f\n", value)); |
| 214 | if (str != pt) { |
| 215 | new = newelement (Val, 1, 4); |
| 216 | if (new == NULL) { |
| 217 | return ERROR_OP; |
| 218 | } |
| 219 | new->value = value; |
| 220 | if (root == NULL) { |
| 221 | root = new; |
| 222 | } else if (root->func == Val) { |
| 223 | if ((*str == '+') || (*str == '-')) { |
| 224 | element_t *add = newelement (Add, 2, 1); |
| 225 | if (add == NULL) { |
| 226 | return ERROR_OP; |
| 227 | } |
| 228 | add->ops[0] = root; |
| 229 | add->ops[1] = new; |
| 230 | root = add; |
| 231 | } else { |
| 232 | return ERROR_OP; |
| 233 | } |
| 234 | } else { |
| 235 | return ERROR_OP; |
| 236 | } |
| 237 | str = pt; |
| 238 | found = 1; |
| 239 | } |
| 240 | VERBOSE (DEBUG, PRINTOUT ("stop processing value\n")); |
| 241 | } |
| 242 | |
| 243 | /* error */ |
| 244 | |
| 245 | if (!found) { |
| 246 | return ERROR_OP; |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | if (next != NULL) { |
| 252 | *next = str; |
| 253 | } |
| 254 | return root; |
| 255 | } |
| 256 | |
| 257 | /* print element tree */ |
| 258 | |
| 259 | void print_element (element_t *root, int level) |
| 260 | { |
| 261 | char *func = NULL; |
| 262 | int i; |
| 263 | |
| 264 | if ((root == NULL) || (root == ERROR_OP)) { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | for (i = 0; i < level; i++) { |
| 269 | PRINTOUT (" "); |
| 270 | } |
| 271 | |
| 272 | switch (root->func) { |
| 273 | case Val: func = "Value"; break; |
| 274 | case Add: func = "Addition"; break; |
| 275 | case Sub: func = "Subtraction"; break; |
| 276 | case Mul: func = "Multiplication"; break; |
| 277 | case Div: func = "Division"; break; |
| 278 | case Pow: func = "Power"; break; |
| 279 | case Sqr: func = "Square Root"; break; |
| 280 | case Cos: func = "Cosine"; break; |
| 281 | case Sin: func = "Sine"; break; |
| 282 | case Atn: func = "Arc Tangent"; break; |
| 283 | case Log: func = "Logarithm"; break; |
| 284 | case Exp: func = "Exponantial"; break; |
| 285 | } |
| 286 | |
| 287 | PRINTOUT ("Function: %s\n", func); |
| 288 | |
| 289 | if ((root->func == Val) && (root->ops[0] == NULL)) { |
| 290 | for (i = 0; i < level; i++) { |
| 291 | PRINTOUT (" "); |
| 292 | } |
| 293 | PRINTOUT ("value: %f\n", root->value); |
| 294 | } else { |
| 295 | for (i = 0; i < root->nbops; i++) { |
| 296 | print_element (root->ops[i], level + 1); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /* evaluate element tree */ |
| 302 | |
| 303 | double evaluate_element (element_t *root) |
| 304 | { |
| 305 | double op0 = 0, op1 = 0; |
| 306 | |
| 307 | if ((root == NULL) || (root == ERROR_OP)) { |
| 308 | VERBOSE (WARNING, PRINTOUT ("error while evaluating\n")); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | switch (root->func) { |
| 313 | case Val: |
| 314 | if (root->ops[0]) { |
| 315 | return evaluate_element (root->ops[0]); |
| 316 | } else { |
| 317 | return root->value; |
| 318 | } |
| 319 | break; |
| 320 | case Add: |
| 321 | case Sub: |
| 322 | case Mul: |
| 323 | case Div: |
| 324 | case Pow: |
| 325 | if (root->ops[1]) { |
| 326 | op1 = evaluate_element (root->ops[1]); |
| 327 | } else { |
| 328 | VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[1])\n")); |
| 329 | return 0; |
| 330 | } |
| 331 | /* fallthrough */ |
| 332 | case Sqr: |
| 333 | case Cos: |
| 334 | case Sin: |
| 335 | case Atn: |
| 336 | case Log: |
| 337 | case Exp: |
| 338 | if (root->ops[0]) { |
| 339 | op0 = evaluate_element (root->ops[0]); |
| 340 | } else { |
| 341 | VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n")); |
| 342 | return 0; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | switch (root->func) { |
| 347 | case Add: return op0 + op1; |
| 348 | case Sub: return op0 - op1; |
| 349 | case Mul: return op0 * op1; |
| 350 | case Div: return op0 / op1; |
| 351 | case Pow: return pow (op0, op1); |
| 352 | case Sqr: return sqrt (op0); |
| 353 | case Cos: return cos (op0); |
| 354 | case Sin: return sin (op0); |
| 355 | case Atn: return atan (op0); |
| 356 | case Log: return log (op0); |
| 357 | case Exp: return exp (op0); |
| 358 | default: break; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /* vim: set ts=4 sw=4 et: */ |