VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
return NULL;
}
+ new->ops = (element_t **) calloc (1, sizeof (element_t *));
+ if (new->ops == NULL) {
+ free (new);
+ VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+ return NULL;
+ }
new->func = function;
new->nbops = nbops;
new->prio = prio;
void delelement (element_t *root)
{
- int i;
if ((root != NULL) && (root != ERROR_OP)) {
+ int i;
for (i = 0; i < root->nbops; i++) {
if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
delelement (root->ops[i]);
}
}
+ if (root->nbops) {
+ free (root->ops);
+ }
free (root);
}
}
+/* duplicate element */
+
+element_t *dupelement (element_t *root)
+{
+ element_t *tmp = NULL;
+ int i;
+
+ if ((root == NULL) || (root == ERROR_OP)) {
+ return root;
+ }
+ tmp = newelement (root->func, root->nbops, root->prio);
+ if (tmp == NULL) {
+ return ERROR_OP;
+ }
+ tmp->value = root->value;
+ for (i = 0; i < root->nbops; i++) {
+ tmp->ops[i] = dupelement (root->ops[i]);
+ if (tmp->ops[i] == ERROR_OP) {
+ delelement (tmp);
+ return ERROR_OP;
+ }
+ }
+ return tmp;
+}
+
/* functions */
#define NB_OPERATORS 14
{ "|", Or, 2, 1, -2}
};
-#define NB_FUNCTIONS 14
+#define NB_FUNCTIONS 17
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, 9},
+ { "sto", Store, 2, 3, 5},
{ "rcl", Recall, 1, 3, 5},
+ { "inc", Inc, 1, 3, 5},
+ { "dec", Dec, 1, 3, 5},
{ "disp", Disp, 0, 4, 9},
{ "quit", Quit, 0, 4, 9},
{ "help", Help, 0, 4, 9},
{ "!", Not, 1, 1, 6},
- { "cond", Cond, 3, 4, 5}
+ { "cond", Cond, 3, 4, 5},
+ { "whl", While, 2, 3, 5}
};
#define NB_CONSTANTS 3
continue;
}
+ /* check for open brace */
+
+ if (*str == '{') {
+ VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
+ element_t *prog = newelement (Prog, 0, 5);
+ if (prog == NULL) {
+ delelement (root);
+ return ERROR_OP;
+ }
+ if (root == NULL) {
+ root = prog;
+ } else {
+ for (i = 0; i < root->nbops; i++) {
+ if (root->ops[i] == NULL) {
+ root->ops[i] = prog;
+ found = 1;
+ }
+ }
+ if (!found) {
+ delelement (prog);
+ delelement (root);
+ return ERROR_OP;
+ }
+ }
+
+ do {
+ found = 0;
+ new = parser (str + 1, &str, 0);
+ if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+ delelement (new);
+ new = ERROR_OP;
+ }
+ if ((new == NULL) || (new == ERROR_OP)) {
+ delelement (root);
+ return ERROR_OP;
+ }
+ element_t *prognew = newelement (Prog, prog->nbops + 1, 5);
+ for (i = 0; i < prog->nbops; i++) {
+ prognew->ops[i] = prog->ops[i];
+ }
+ prog->ops[prog->nbops] = new;
+ delelement (prog);
+ prog = prognew;
+ } while (*str == ',');
+
+ if (*str != '}') {
+ delelement (root);
+ return ERROR_OP;
+ }
+ str++;
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing brace\n"));
+ continue;
+ }
+
/* check for open bracket */
if (*str == '(') {
continue;
}
- /* check for closing bracket or koma */
+ /* check for closing bracket, closing brace or koma */
- if ((*str == ')') || (*str == ',')) {
+ if ((*str == ')') || (*str == '}') || (*str == ',')) {
if (next != NULL) {
*next = str;
}
case Exp: func = "Exponantial"; break;
case Store: func = "Store"; break;
case Recall: func = "Recall"; break;
+ case Inc: func = "Increase"; break;
+ case Dec: func = "Decrease"; break;
case Disp: func = "Display"; break;
case Quit: func = "Quit"; break;
case Help: func = "Help"; break;
case Or: func = "Or"; break;
case Not: func = "Not"; break;
case Cond: func = "Condition"; break;
+ case While: func = "While"; break;
+ case Prog: func = "Program"; break;
}
fprintf (stdout, "Function: %s\n", func);
return 0;
}
+double increase (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;
+}
+
+double decrease (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, "\n");
}
+/* While do function */
+
+double while_do (element_t *cond, element_t *action)
+{
+ double ret = 0;
+ element_t *temp = NULL;
+
+ VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
+ if (cond == NULL) {
+ return ret;
+ }
+ while (1) {
+ VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));
+
+ temp = dupelement (cond);
+ if (!evaluate_element (temp, 0)) {
+ break;
+ }
+ if (action) {
+ temp = dupelement (action);
+ ret = evaluate_element (temp, 0);
+ }
+ }
+
+ VERBOSE (DEBUG, fprintf (stdout, "ending while loop\n"));
+
+ return ret;
+}
+
+/* program function */
+
+double program_do (element_t **prog, int nbcalls)
+{
+ double ret = 0;
+ int i;
+ for (i = 0; i < nbcalls; i++) {
+ ret = evaluate_element (prog[i], 0);
+ prog[i] = NULL;
+ }
+ return ret;
+}
+
/* quit function */
void quit (void)
fprintf (stdout, "supported functions:");
fprintf (stdout, " pow sqrt cos sin atan log exp\n");
fprintf (stdout, "storage functions:");
- fprintf (stdout, " sto rcl\n");
+ fprintf (stdout, " sto rcl inc dec\n");
fprintf (stdout, "conditional functions:");
fprintf (stdout, " cond\n");
fprintf (stdout, "miscellaneous functions:");
case Log:
case Exp:
case Recall:
+ case Inc:
+ case Dec:
case Not:
case Cond:
if (root->ops[0]) {
case Ans:
case Pi:
case E:
+ case Prog:
+ break;
+ case While:
+ if (root->ops[0] == NULL) {
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
+ return 0;
+ }
break;
}
case Exp: return exp (op0);
case Store: return store ((int)op0, (op1) ? op1 : answer);
case Recall: return recall ((int)op0);
+ case Inc: return increase ((int)op0);
+ case Dec: return decrease ((int)op0);
case Disp: display (); break;
case Quit: quit (); break;
case Help: help (); break;
} else {
return 0;
}
+ case While: return while_do (root->ops[0], root->ops[1]);
+ case Prog: return program_do (root->ops, root->nbops);
}
return 0;