X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=parser.c;h=77013e389665d0968da61faa83f8575e14d3d59f;hb=0b69fdfbcd1174e83284c2bf984fd334b0ba679e;hp=b9823abaa25f10acd08cc4f40e2667a2e94ea902;hpb=e2a309f921968691af1566000dd694f191eca857;p=calc.git diff --git a/parser.c b/parser.c index b9823ab..77013e3 100644 --- a/parser.c +++ b/parser.c @@ -62,7 +62,7 @@ keyword_t operators[NB_OPERATORS] = { { "|", Or, 2, 1, -2} }; -#define NB_FUNCTIONS 51 +#define NB_FUNCTIONS 52 keyword_t functions[NB_FUNCTIONS] = { { "sqrt", Sqr, 1, 4, 5}, { "pow", Pow, 2, 3, 5}, @@ -114,7 +114,8 @@ keyword_t functions[NB_FUNCTIONS] = { { "prod", Prod, 0, 4, 5}, { "sum", Sum, 0, 3, 5}, { "var", Variance, 2, 3, 5}, - { "format", Precision, 1, 6, 9} + { "format", Precision, 1, 6, 9}, + { "base", Base, 2, 4, 9} }; #define NB_CONSTANTS 3 @@ -371,51 +372,48 @@ element_t *parser (char *str, char **next, int prio) /* look for number */ - if (((*str >= '0') && (*str <= '9')) || - (*str == '.') || (*str == '+') || (*str == '-')) { - VERBOSE (DEBUG, fprintf (stdout, "start processing value\n")); - char *pt; - double value = strtod (str, &pt); - VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value)); - if (str != pt) { - if ((root == NULL) || (root->prio == 6)) { - new = newelement (Val, 1, 5); - new->value = value; - if (root == NULL) { - root = new; - } else { - for (i = 0; i < root->nbops; i++) { - if (root->ops[i] == NULL) { - root->ops[i] = new; - found = 1; - break; - } - } - if (!found) { - delelement (new); - delelement (root); - return ERROR_OP; + VERBOSE (DEBUG, fprintf (stdout, "start processing value\n")); + char *pt; + double value = (get_ibase () == 10) ? strtod (str, &pt) : strtoul (str, &pt, get_ibase ()); + VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value)); + if (str != pt) { + if ((root == NULL) || (root->prio == 6)) { + new = newelement (Val, 1, 5); + new->value = value; + if (root == NULL) { + root = new; + } else { + for (i = 0; i < root->nbops; i++) { + if (root->ops[i] == NULL) { + root->ops[i] = new; + found = 1; + break; } } - str = pt; - } else if ((*str == '+') || (*str == '-')) { - if ((prio) && (prio > 1)) { - VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n")); - *next = str; - return root; - } - if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) { + if (!found) { + delelement (new); delelement (root); return ERROR_OP; } - } else { + } + str = pt; + } else if ((*str == '+') || (*str == '-')) { + if ((prio) && (prio > 1)) { + VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n")); + *next = str; + return root; + } + if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) { delelement (root); return ERROR_OP; } - found = 1; + } else { + delelement (root); + return ERROR_OP; } - VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n")); + found = 1; } + VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n")); /* error */ @@ -524,6 +522,7 @@ void print_element (element_t *root, int level) case Sum: func = "Sum"; break; case Variance: func = "Variance"; break; case Precision: func = "Precision"; break; + case Base: func = "Base"; break; } fprintf (stdout, "Function: %s\n", func); @@ -619,11 +618,43 @@ void help (void) fprintf (stdout, "stack func.:"); fprintf (stdout, " max mean med min ord prod sum var\n"); fprintf (stdout, "control management:"); - fprintf (stdout, " format help quit\n"); + fprintf (stdout, " base format help quit\n"); fprintf (stdout, "constants:"); fprintf (stdout, " ans e pi\n"); } +/* format function */ + +int format (int precision) +{ + if (precision > 0) { + set_precision (precision); + set_format (); + } else if (precision != -1) { + VERBOSE (WARNING, fprintf (stdout, "error incorrect precision (%d)\n", precision)); + return 0; + } + return get_precision (); +} + +/* base function */ + +void base (int in, int out) +{ + if ((in > 0) && (in < 37)) { + set_base (in, in); + if ((out > 0) && (out < 37)) { + set_base (in, out); + } else if (out != - 1) { + VERBOSE (WARNING, fprintf (stdout, "error incorrect output base (%d)\n", out)); + } + } else if (in != -1 ) { + VERBOSE (WARNING, fprintf (stdout, "error incorrect input base (%d)\n", in)); + } else { + printf ("base (I/O): %s\n", show_base ()); + } +} + /* evaluate element tree */ #define MASK_SUB 0x1 @@ -780,6 +811,10 @@ double evaluate_element (element_t *root, char mask) case Precision: op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : -1; break; + case Base: + op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : -1; + op1 = (root->ops[1]) ? evaluate_element (root->ops[1], 0) : -1; + break; } switch (root->func) { @@ -894,14 +929,10 @@ double evaluate_element (element_t *root, char mask) } return variance (); case Precision: - if (op0 > 0) { - set_precision ((int)op0); - set_format (); - } else if (op0 != -1) { - VERBOSE (WARNING, fprintf (stdout, "error incorrect precision (%g)\n", op0)); - return 0; - } - return get_precision(); + return format ((int)op0); + case Base: + base ((int)op0, (int)op1); + break; } return 0;