{ "|", Or, 2, 1, -2}
};
-#define NB_FUNCTIONS 50
+#define NB_FUNCTIONS 52
keyword_t functions[NB_FUNCTIONS] = {
{ "sqrt", Sqr, 1, 4, 5},
{ "pow", Pow, 2, 3, 5},
{ "inc", Inc, 1, 3, 5},
{ "dec", Dec, 1, 3, 5},
{ "disp", Disp, 0, 4, 9},
- { "mem", Mem, 1, 3, 5},
+ { "mem", Memory, 1, 3, 5},
{ "clr", Clear, 0, 3, 9},
{ "quit", Quit, 0, 4, 9},
{ "help", Help, 0, 4, 9},
{ "prod", Prod, 0, 4, 5},
{ "sum", Sum, 0, 3, 5},
{ "var", Variance, 2, 3, 5},
+ { "format", Precision, 1, 6, 9},
+ { "base", Base, 2, 4, 9}
};
#define NB_CONSTANTS 3
/* 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 */
case Inc: func = "Increase"; break;
case Dec: func = "Decrease"; break;
case Disp: func = "Display"; break;
- case Mem: func = "Memory"; break;
+ case Memory: func = "Memory"; break;
case Clear: func = "Clear"; break;
case Quit: func = "Quit"; break;
case Help: func = "Help"; break;
case Prod: func = "Product"; break;
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);
fprintf (stdout, "stack func.:");
fprintf (stdout, " max mean med min ord prod sum var\n");
fprintf (stdout, "control management:");
- fprintf (stdout, " 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
case Div:
case Mod:
case Pow:
- case Store:
case Equal:
case Diff:
case Ge:
case Or:
if (root->ops[1]) {
op1 = evaluate_element (root->ops[1], nextmask);
- } else if (root->func != Store) {
+ } else {
VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
return 0;
}
case Inc:
case Dec:
case Not:
- case Mem:
case Cond:
case Prog:
case Arg:
return 0;
}
break;
+ case Memory:
+ if (root->ops[0] == NULL) {
+ op0 = -1;
+ } else {
+ op0 = (int)evaluate_element (root->ops[0], 0);
+ if (op0 < 0) {
+ VERBOSE (WARNING, fprintf (stdout, "error incorrect memory size (%d)\n", (int)op0));
+ return 0;
+ }
+ }
+ break;
case Push:
case Print:
op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : answer;
break;
+ case Store:
case Put:
if (root->ops[0]) {
op0 = evaluate_element (root->ops[0], 0);
op0 = evaluate_element (root->ops[0], 0);
op1 = (root->ops[1]) ? evaluate_element (root->ops[1], 0) : answer;
}
+ break;
+ 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) {
case Abs: return fabs (op0);
case Ceil: return ceil (op0);
case Floor: return floor (op0);
- case Store: return store ((int)op0, (op1) ? op1 : answer);
+ case Store: return store ((int)op0, op1);
case Recall: return recall ((int)op0);
case Inc: return increase ((int)op0);
case Dec: return decrease ((int)op0);
case Disp: display (); break;
- case Mem: memory ((int)op0); break;
+ case Memory: return memory ((root->ops[0]) ? (int)op0 : -1);
case Clear: clear (); break;
case Quit: quit (); break;
case Help: help (); break;
break;
case Arg: return arg ((int)op0);
case Call:
- for (i = 1, nb =0; i < root->nbops; i++) {
+ for (i = 1, nb = 0; i < root->nbops; i++) {
if (root->ops[i]) {
nb++;
}
return op0 * op0 + op1 * op1;
}
return variance ();
+ case Precision:
+ return format ((int)op0);
+ case Base:
+ base ((int)op0, (int)op1);
+ break;
}
return 0;