X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=parser.c;h=1d35b4c44785dc85eee79b7600b2c62eebb40910;hb=89cf095594de27bde3e3be007d255cad56ab0eca;hp=2190178ecba23fc5898a4cac2a2dc22db343f982;hpb=0b489a77ab06263674d1f4376c3bf6c2ead42cd6;p=calc.git diff --git a/parser.c b/parser.c index 2190178..1d35b4c 100644 --- a/parser.c +++ b/parser.c @@ -1,4 +1,5 @@ #include +#include #include #include "debug.h" @@ -13,10 +14,16 @@ int codecmp (char *ref, char *str) int sig; while (*ref != '\0') { - sig = *str++ - *ref++; + if (*ref == '\t') { + sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9')); + } else { + sig = *str - *ref; + } if (sig != 0) { return (sig > 0) ? 1 : -1; } + str++; + ref++; } return 0; @@ -24,7 +31,7 @@ int codecmp (char *ref, char *str) /* allocate new element */ -element_t *newelement (func_t function, int nbops) +element_t *newelement (func_t function, int nbops, int prio) { element_t *new = (element_t *) calloc (1, sizeof (element_t)); if (new == NULL) { @@ -33,47 +40,73 @@ element_t *newelement (func_t function, int nbops) } new->func = function; new->nbops = nbops; + new->prio = prio; return new; } /* functions */ -#define NB_OPERATORS 7 +#define NB_OPERATORS 6 keyword_t operators[NB_OPERATORS] = { - { "+ ", Add, 2, 1 }, - { "+\t", Add, 2, 1 }, - { "- ", Sub, 2, 1 }, - { "- ", Sub, 2, 1 }, - { "*", Mul, 2, 1 }, - { "/", Div, 2, 1 }, - { "^", Pow, 2, 1 } + { "+\t", Add, 2, 1, 1}, + { "-\t", Sub, 2, 1, 1}, + { "*", Mul, 2, 1, 2}, + { "/", Div, 2, 1, 2}, + { "%", Mod, 2, 1, 3}, + { "^", Pow, 2, 1, 4} }; -#define NB_FUNCTIONS 7 +#define NB_FUNCTIONS 9 keyword_t functions[NB_FUNCTIONS] = { - { "sqrt", Sqr, 1, 4 }, - { "pow", Pow, 2, 3 }, - { "cos", Cos, 1, 3 }, - { "sin", Sin, 1, 3 }, - { "atan", Atn, 1, 4 }, - { "exp", Exp, 1, 3 }, - { "log", Log, 1, 3 } + { "sqrt", Sqr, 1, 4, 5}, + { "pow", Pow, 2, 3, 5}, + { "cos", Cos, 1, 3, 5}, + { "sin", Sin, 1, 3, 5}, + { "atan", Atn, 1, 4, 5}, + { "exp", Exp, 1, 3, 5}, + { "log", Log, 1, 3, 5}, + { "quit", Qui, 0, 4, 5}, + { "help", Hel, 0, 4, 5} }; +/* subparser function */ + +element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio) +{ + element_t *new = newelement (func, nbops, prio); + if (new == NULL) { + return ERROR_OP; + } + new->ops[0] = *proot; + new->ops[1] = parser (*pstr, pstr, new->prio); + if (new->ops[1] == ERROR_OP) { + return ERROR_OP; + } + *proot = newelement (Val, 1, 5); + if (*proot == ERROR_OP) { + return ERROR_OP; + } + (*proot)->ops[0] = new; + + return *proot; +} + /* parser function */ -element_t *parser (char *str, char **next) +element_t *parser (char *str, char **next, int prio) { element_t *root = NULL; - int i, j; + int i; + + VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n")); /* main loop */ while (*str != '\0') { int found = 0; element_t *new = NULL; - VERBOSE (DEBUG, PRINTOUT ("Processing: %s\n", str)); + VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str)); /* skip spaces and tabs */ @@ -82,55 +115,51 @@ element_t *parser (char *str, char **next) continue; } - /* skip commas */ - - if (*str == ',') { - if (root == NULL) { - return ERROR_OP; - } else if (root->func != Set) { - new = newelement (Set, MAX_OPERANDS); - new->ops[0] = root; - root = new; - } - str++; - continue; - } - - /* check for parent */ + /* check for open bracket */ - if (*str == ')') { - if (next != NULL) { - *next = str + 1; - } - return root; - } if (*str == '(') { - new = parser (str + 1, &str); - if (new == (void *)(-1)) { - return new; - } + VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n")); if (root) { - for (i = 0, j = 0; i < root->nbops; i++) { - if (root->ops[i] == NULL) { - if (new->func == Set) { - root->ops[i] = new->ops[j++]; - if (new->ops[j] == NULL) { - found = 1; - break; - } - } else { + do { + found = 0; + new = parser (str + 1, &str, 0); + if (new == ERROR_OP) { + return ERROR_OP; + } + for (i = 0; i < root->nbops; i++) { + if (root->ops[i] == NULL) { root->ops[i] = new; found = 1; break; } } + if (!found) { + return ERROR_OP; + } + } while (*str == ','); + } else { + root = newelement (Val, 1, 5); + if (root == NULL) { + return ERROR_OP; } - if (!found) { + new = parser (str + 1, &str, 0); + if ((new == ERROR_OP) || (*str == ',')) { return ERROR_OP; } - } else { - return root; + root->ops[0] = new; } + str++; + VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n")); + continue; + } + + /* check for closing bracket or koma */ + + if ((*str == ')') || (*str == ',')) { + if (next != NULL) { + *next = str; + } + return root; } /* look for operators */ @@ -138,35 +167,45 @@ element_t *parser (char *str, char **next) for (i = 0; i < NB_OPERATORS; i++) { keyword_t *operator = operators + i; if (codecmp (operator->keyword, str) == 0) { - if ((root) && (root->func == Val)) { - VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", operator->func)); - new = newelement (operator->func, operator->nbops); + VERBOSE (DEBUG, PRINTOUT ("start processing operator\n")); + if (root) { + if ((prio) && (prio > operator->prio)) { + VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n")); + *next = str; + return root; + } + str += operator->offset; + VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func)); + if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) { + return ERROR_OP; + } + } else if (*str == '-') { + new = newelement (Sig, 1, 9); if (new == NULL) { return ERROR_OP; } - new->ops[0] = root; root = new; - } else { + } else { return ERROR_OP; } - str += operator->offset; found = 1; + VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n")); break; } } if (found) { - VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n")); continue; } - + /* look for functions */ for (i = 0; i < NB_FUNCTIONS; i++) { keyword_t *function = functions + i; if (codecmp (function->keyword, str) == 0) { + VERBOSE (DEBUG, PRINTOUT ("start processing function\n")); if (root == NULL) { - VERBOSE (DEBUG, PRINTOUT ("Func: %d\n", function->func)); - new = newelement (function->func, function->nbops); + VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func)); + new = newelement (function->func, function->nbops, function->prio); if (new == NULL) { return ERROR_OP; } @@ -176,66 +215,50 @@ element_t *parser (char *str, char **next) } str += function->offset; found = 1; + VERBOSE (DEBUG, PRINTOUT ("stop processing function\n")); break; } } if (found) { - VERBOSE (DEBUG, PRINTOUT ("stop processing function\n")); continue; } - /* last attend to detect addition and substraction */ - - if (((*str == '-') || (*str == '+')) && - ((*(str + 1) >= '0') && (*(str + 1) <= '9')) && - ((root) && (root->func == Val))) { - VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", Add)); - new = newelement (Add, 2); - if (new == NULL) { - return ERROR_OP; - } - new->ops[0] = root; - root = new; - } - /* look for number */ if (((*str >= '0') && (*str <= '9')) || - (*str == '.') || (*str == '-') ||(*str == '+')) { + (*str == '.') || (*str == '+') || (*str == '-')) { + VERBOSE (DEBUG, PRINTOUT ("start processing value\n")); char *pt; float value = strtof (str, &pt); - VERBOSE (DEBUG, PRINTOUT ("Value: %f\n", value)); + VERBOSE (INFO, PRINTOUT ("Value: %f\n", value)); if (str != pt) { - new = newelement (Val, 1); - new->value = value; - if (new == NULL) { - return ERROR_OP; - } if (root == NULL) { + new = newelement (Val, 1, 5); + if (new == NULL) { + return ERROR_OP; + } + new->value = value; root = new; - } else { - if (root->func == Val) { - element_t *set = newelement (Set, MAX_OPERANDS); - if (set == NULL) { - return ERROR_OP; + str = pt; + } else if (root->func == Val) { + if ((*str == '+') || (*str == '-')) { + if ((prio) && (prio > 1)) { + VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n")); + *next = str; + return root; } - set->ops[0] = root; - root = set; - } - for (i = 0; i < root->nbops; i++) { - if (root->ops[i] == NULL) { - root->ops[i] = new; - found = 1; - break; + if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) { + return ERROR_OP; } - } - if (!found) { + } else { return ERROR_OP; } + } else { + return ERROR_OP; } - str = pt; found = 1; } + VERBOSE (DEBUG, PRINTOUT ("stop processing value\n")); } /* error */ @@ -246,6 +269,9 @@ element_t *parser (char *str, char **next) } + if (next != NULL) { + *next = str; + } return root; } @@ -256,20 +282,22 @@ void print_element (element_t *root, int level) char *func = NULL; int i; - if (root == NULL) + if ((root == NULL) || (root == ERROR_OP)) { return; - + } + for (i = 0; i < level; i++) { PRINTOUT (" "); } switch (root->func) { case Val: func = "Value"; break; - case Set: func = "Set"; break; + case Sig: func = "Sign"; break; case Add: func = "Addition"; break; case Sub: func = "Subtraction"; break; case Mul: func = "Multiplication"; break; case Div: func = "Division"; break; + case Mod: func = "Modulo"; break; case Pow: func = "Power"; break; case Sqr: func = "Square Root"; break; case Cos: func = "Cosine"; break; @@ -277,11 +305,13 @@ void print_element (element_t *root, int level) case Atn: func = "Arc Tangent"; break; case Log: func = "Logarithm"; break; case Exp: func = "Exponantial"; break; + case Qui: func = "Quit"; break; + case Hel: func = "Help"; break; } PRINTOUT ("Function: %s\n", func); - if (root->func == Val) { + if ((root->func == Val) && (root->ops[0] == NULL)) { for (i = 0; i < level; i++) { PRINTOUT (" "); } @@ -293,4 +323,118 @@ void print_element (element_t *root, int level) } } +/* quit function */ + +void quit (void) +{ + PRINTOUT ("bye\n"); + exit (0); +} + +/* help message */ + +void help (void) +{ + PRINTOUT ("calc is a simple calculator\n\n"); + PRINTOUT ("supported operators:\n"); + PRINTOUT (" + - * / % ^\n\n"); + PRINTOUT ("supported functions:\n"); + PRINTOUT (" pow sqrt cos sin atan log exp\n\n"); + PRINTOUT ("miscellaneous functions:\n"); + PRINTOUT (" quit help\n"); +} + +/* evaluate element tree */ + +#define MASK_SUB 0x1 +#define MASK_DIV 0x2 + +double evaluate_element (element_t *root, char mask) +{ + double op0 = 0, op1 = 0; + char nextmask = mask; + + if ((root == NULL) || (root == ERROR_OP)) { + VERBOSE (WARNING, PRINTOUT ("error while evaluating\n")); + return 0; + } + + /* mask to manage sub operator sub and div */ + switch (root->func) { + case Add: + nextmask &= ~MASK_SUB; + nextmask &= ~MASK_DIV; + break; + case Sub: + nextmask |= MASK_SUB; + nextmask &= ~MASK_DIV; + break; + case Mul: + nextmask &= ~MASK_DIV; + break; + case Div: + nextmask |= MASK_DIV; + break; + default: + nextmask = mask; + } + + switch (root->func) { + case Val: + case Sig: + op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value; + break; + case Add: + case Sub: + case Mul: + case Div: + case Mod: + case Pow: + if (root->ops[1]) { + op1 = evaluate_element (root->ops[1], nextmask); + } else { + VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[1])\n")); + return 0; + } + /* fallthrough */ + case Sqr: + case Cos: + case Sin: + case Atn: + case Log: + case Exp: + if (root->ops[0]) { + op0 = evaluate_element (root->ops[0], 0); + } else { + VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n")); + return 0; + } + break; + case Qui: + case Hel: + break; + } + + switch (root->func) { + case Val: return op0; + case Sig: return -op0; + case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1; + case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1; + case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1; + case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1; + case Mod: return fmod (op0, op1); + case Pow: return pow (op0, op1); + case Sqr: return sqrt (op0); + case Cos: return cos (op0); + case Sin: return sin (op0); + case Atn: return atan (op0); + case Log: return log (op0); + case Exp: return exp (op0); + case Qui: quit (); break; + case Hel: help (); break; + } + + return 0; +} + /* vim: set ts=4 sw=4 et: */