| 1 | #include <malloc.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include "debug.h" |
| 5 | #include "fdprintf.h" |
| 6 | |
| 7 | #include "parser.h" |
| 8 | |
| 9 | /* compare codes */ |
| 10 | |
| 11 | int codecmp (char *ref, char *str) |
| 12 | { |
| 13 | int sig; |
| 14 | |
| 15 | while (*ref != '\0') { |
| 16 | if (*ref == '\t') { |
| 17 | sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9')); |
| 18 | } else { |
| 19 | sig = *str - *ref; |
| 20 | } |
| 21 | if (sig != 0) { |
| 22 | return (sig > 0) ? 1 : -1; |
| 23 | } |
| 24 | str++; |
| 25 | ref++; |
| 26 | } |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | /* allocate new element */ |
| 32 | |
| 33 | element_t *newelement (func_t function, int nbops, int prio) |
| 34 | { |
| 35 | element_t *new = (element_t *) calloc (1, sizeof (element_t)); |
| 36 | if (new == NULL) { |
| 37 | VERBOSE (ERROR, fdprintf (stdfderr, "can't allocate memory\n")); |
| 38 | return NULL; |
| 39 | } |
| 40 | new->func = function; |
| 41 | new->nbops = nbops; |
| 42 | new->prio = prio; |
| 43 | |
| 44 | return new; |
| 45 | } |
| 46 | |
| 47 | /* functions */ |
| 48 | |
| 49 | #define NB_OPERATORS 5 |
| 50 | |
| 51 | keyword_t operators[NB_OPERATORS] = { |
| 52 | { "+\t", Add, 2, 1, 1}, |
| 53 | { "-\t", Sub, 2, 1, 1}, |
| 54 | { "*", Mul, 2, 1, 2}, |
| 55 | { "/", Div, 2, 1, 2}, |
| 56 | { "^", Pow, 2, 1, 3} |
| 57 | }; |
| 58 | |
| 59 | #define NB_FUNCTIONS 7 |
| 60 | keyword_t functions[NB_FUNCTIONS] = { |
| 61 | { "sqrt", Sqr, 1, 4, 4}, |
| 62 | { "pow", Pow, 2, 3, 4}, |
| 63 | { "cos", Cos, 1, 3, 4}, |
| 64 | { "sin", Sin, 1, 3, 4}, |
| 65 | { "atan", Atn, 1, 4, 4}, |
| 66 | { "exp", Exp, 1, 3, 4}, |
| 67 | { "log", Log, 1, 3, 4} |
| 68 | }; |
| 69 | |
| 70 | /* parser function */ |
| 71 | |
| 72 | element_t *parser (char *str, char **next, int prio) |
| 73 | { |
| 74 | element_t *root = NULL; |
| 75 | int i; |
| 76 | |
| 77 | VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n")); |
| 78 | |
| 79 | /* main loop */ |
| 80 | while (*str != '\0') { |
| 81 | int found = 0; |
| 82 | element_t *new = NULL; |
| 83 | VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str)); |
| 84 | |
| 85 | /* skip spaces and tabs */ |
| 86 | |
| 87 | if ((*str == ' ') || (*str == '\t')) { |
| 88 | str++; |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | /* check for open bracket */ |
| 93 | |
| 94 | if (*str == '(') { |
| 95 | VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n")); |
| 96 | if (root) { |
| 97 | do { |
| 98 | found = 0; |
| 99 | new = parser (str + 1, &str, 0); |
| 100 | if (new == ERROR_OP) { |
| 101 | return ERROR_OP; |
| 102 | } |
| 103 | for (i = 0; i < root->nbops; i++) { |
| 104 | if (root->ops[i] == NULL) { |
| 105 | root->ops[i] = new; |
| 106 | found = 1; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | if (!found) { |
| 111 | return ERROR_OP; |
| 112 | } |
| 113 | } while (*str == ','); |
| 114 | } else { |
| 115 | root = newelement (Val, 1, 4); |
| 116 | if (root == NULL) { |
| 117 | return ERROR_OP; |
| 118 | } |
| 119 | new = parser (str + 1, &str, 0); |
| 120 | if ((new == ERROR_OP) || (*str == ',')) { |
| 121 | return ERROR_OP; |
| 122 | } |
| 123 | root->ops[0] = new; |
| 124 | } |
| 125 | str++; |
| 126 | VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n")); |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | /* check for closing bracket or koma */ |
| 131 | |
| 132 | if ((*str == ')') || (*str == ',')) { |
| 133 | if (next != NULL) { |
| 134 | *next = str; |
| 135 | } |
| 136 | return root; |
| 137 | } |
| 138 | |
| 139 | /* look for operators */ |
| 140 | |
| 141 | for (i = 0; i < NB_OPERATORS; i++) { |
| 142 | keyword_t *operator = operators + i; |
| 143 | if (codecmp (operator->keyword, str) == 0) { |
| 144 | VERBOSE (DEBUG, PRINTOUT ("start processing operator\n")); |
| 145 | if (root) { |
| 146 | if ((prio) && (prio > operator->prio)) { |
| 147 | VERBOSE (DEBUG, PRINTOUT ("stop processing operator because operator priority\n")); |
| 148 | *next = str; |
| 149 | return root; |
| 150 | } |
| 151 | str += operator->offset; |
| 152 | VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func)); |
| 153 | new = newelement (operator->func, operator->nbops, operator->prio); |
| 154 | if (new == NULL) { |
| 155 | return ERROR_OP; |
| 156 | } |
| 157 | new->ops[0] = root; |
| 158 | new->ops[1] = parser (str, &str, new->prio); |
| 159 | if (new->ops[1] == ERROR_OP) { |
| 160 | return ERROR_OP; |
| 161 | } |
| 162 | root = newelement (Val, 1, 4); |
| 163 | if (root == ERROR_OP) { |
| 164 | return ERROR_OP; |
| 165 | } |
| 166 | root->ops[0] = new; |
| 167 | } else { |
| 168 | return ERROR_OP; |
| 169 | } |
| 170 | found = 1; |
| 171 | VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n")); |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | if (found) { |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | /* look for functions */ |
| 180 | |
| 181 | for (i = 0; i < NB_FUNCTIONS; i++) { |
| 182 | keyword_t *function = functions + i; |
| 183 | if (codecmp (function->keyword, str) == 0) { |
| 184 | VERBOSE (DEBUG, PRINTOUT ("start processing function\n")); |
| 185 | if (root == NULL) { |
| 186 | VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func)); |
| 187 | new = newelement (function->func, function->nbops, function->prio); |
| 188 | if (new == NULL) { |
| 189 | return ERROR_OP; |
| 190 | } |
| 191 | root = new; |
| 192 | } else { |
| 193 | return ERROR_OP; |
| 194 | } |
| 195 | str += function->offset; |
| 196 | found = 1; |
| 197 | VERBOSE (DEBUG, PRINTOUT ("stop processing function\n")); |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | if (found) { |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | /* look for number */ |
| 206 | |
| 207 | if (((*str >= '0') && (*str <= '9')) || |
| 208 | (*str == '.') || (*str == '+') || (*str == '-')) { |
| 209 | VERBOSE (DEBUG, PRINTOUT ("start processing value\n")); |
| 210 | char *pt; |
| 211 | float value = strtof (str, &pt); |
| 212 | VERBOSE (INFO, PRINTOUT ("Value: %f\n", value)); |
| 213 | if (str != pt) { |
| 214 | new = newelement (Val, 1, 4); |
| 215 | if (new == NULL) { |
| 216 | return ERROR_OP; |
| 217 | } |
| 218 | new->value = value; |
| 219 | if (root == NULL) { |
| 220 | root = new; |
| 221 | } else if (root->func == Val) { |
| 222 | if ((*str == '+') || (*str == '-')) { |
| 223 | element_t *add = newelement (Add, 2, 1); |
| 224 | if (add == NULL) { |
| 225 | return ERROR_OP; |
| 226 | } |
| 227 | add->ops[0] = root; |
| 228 | add->ops[1] = new; |
| 229 | root = add; |
| 230 | } else { |
| 231 | return ERROR_OP; |
| 232 | } |
| 233 | } else { |
| 234 | return ERROR_OP; |
| 235 | } |
| 236 | str = pt; |
| 237 | found = 1; |
| 238 | } |
| 239 | VERBOSE (DEBUG, PRINTOUT ("stop processing value\n")); |
| 240 | } |
| 241 | |
| 242 | /* error */ |
| 243 | |
| 244 | if (!found) { |
| 245 | return ERROR_OP; |
| 246 | } |
| 247 | |
| 248 | } |
| 249 | |
| 250 | if (next != NULL) { |
| 251 | *next = str; |
| 252 | } |
| 253 | return root; |
| 254 | } |
| 255 | |
| 256 | /* print element tree */ |
| 257 | |
| 258 | void print_element (element_t *root, int level) |
| 259 | { |
| 260 | char *func = NULL; |
| 261 | int i; |
| 262 | |
| 263 | if ((root == NULL) || (root == ERROR_OP)) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | for (i = 0; i < level; i++) { |
| 268 | PRINTOUT (" "); |
| 269 | } |
| 270 | |
| 271 | switch (root->func) { |
| 272 | case Val: func = "Value"; break; |
| 273 | case Set: func = "Set"; 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 | /* vim: set ts=4 sw=4 et: */ |