#include "parser.h"
+/* global variables */
+
+double answer = 0;
+
+#define STORAGE_SIZE 10
+double storage[STORAGE_SIZE] = {0};
+
/* compare codes */
int codecmp (char *ref, char *str)
{ "^", Pow, 2, 1, 4}
};
-#define NB_FUNCTIONS 9
+#define NB_FUNCTIONS 12
keyword_t functions[NB_FUNCTIONS] = {
{ "sqrt", Sqr, 1, 4, 5},
{ "pow", Pow, 2, 3, 5},
{ "atan", Atan, 1, 4, 5},
{ "exp", Exp, 1, 3, 5},
{ "log", Log, 1, 3, 5},
+ { "sto", Store, 2, 3, 5},
+ { "rcl", Recall, 1, 3, 5},
+ { "disp", Disp, 0, 4, 5},
{ "quit", Quit, 0, 4, 5},
{ "help", Help, 0, 4, 5}
};
-#define NB_CONSTANTS 2
+#define NB_CONSTANTS 3
keyword_t constants[NB_CONSTANTS] = {
- { "e", E, 0, 1, 5},
- { "pi", Pi, 0, 2, 5}
+ { "ans", Ans, 0, 3, 5},
+ { "e", E, 0, 1, 5},
+ { "pi", Pi, 0, 2, 5}
};
/* subparser function */
if (codecmp (constant->keyword, str) == 0) {
VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
if (root == NULL) {
- VERBOSE (INFO, fprintf (stdout, "Func: %d\n", constant->func));
+ VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
new = newelement (constant->func, constant->nbops, constant->prio);
if (new == NULL) {
return ERROR_OP;
new->value = value;
root = new;
str = pt;
- } else if (root->func == Val) {
- 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;
- }
- } else {
+ } 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;
}
case Atan: func = "Arc Tangent"; break;
case Log: func = "Logarithm"; break;
case Exp: func = "Exponantial"; break;
+ case Store: func = "Store"; break;
+ case Recall: func = "Recall"; break;
+ case Disp: func = "Display"; break;
case Quit: func = "Quit"; break;
case Help: func = "Help"; break;
+ case Ans: func = "Ans"; break;
case Pi: func = "Pi"; break;
case E: func = "E"; break;
}
}
}
+/* storage functions */
+
+double store (int index, double value)
+{
+ if ((index > 0) && (index <= STORAGE_SIZE)) {
+ storage[index - 1] = value;
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+ }
+ return value;
+}
+
+double recall (int index)
+{
+ if ((index > 0) && (index <= STORAGE_SIZE)) {
+ return storage[index - 1];
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+ }
+ return 0;
+}
+
+void display (void)
+{
+ int i;
+ fprintf (stdout, "storage:");
+ for (i = 0; i < STORAGE_SIZE; i++) {
+ fprintf (stdout, " %g", storage[i]);
+ }
+ fprintf (stdout, "\n");
+}
+
/* quit function */
void quit (void)
fprintf (stdout, " + - * / %% ^\n\n");
fprintf (stdout, "supported functions:\n");
fprintf (stdout, " pow sqrt cos sin atan log exp\n\n");
+ fprintf (stdout, "storage functions:\n");
+ fprintf (stdout, " sto rcl\n\n");
fprintf (stdout, "miscellaneous functions:\n");
fprintf (stdout, " quit help\n\n");
fprintf (stdout, "supported constants:\n");
case Div:
case Mod:
case Pow:
+ case Store:
if (root->ops[1]) {
op1 = evaluate_element (root->ops[1], nextmask);
- } else {
+ } else if (root->func != Store) {
VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
return 0;
}
case Atan:
case Log:
case Exp:
+ case Recall:
if (root->ops[0]) {
op0 = evaluate_element (root->ops[0], 0);
} else {
return 0;
}
break;
+ case Disp:
case Quit:
case Help:
+ case Ans:
case Pi:
case E:
break;
case Atan: return atan (op0);
case Log: return log (op0);
case Exp: return exp (op0);
+ case Store: return store ((int)op0, (op1) ? op1 : answer);
+ case Recall: return recall ((int)op0);
+ case Disp: display (); break;
case Quit: quit (); break;
case Help: help (); break;
+ case Ans: return answer;
case Pi: return M_PI;
case E: return M_E;
}