return new;
}
+/* desallocate element */
+
+void delelement (element_t *root)
+{
+ int i;
+ if ((root != NULL) && (root != ERROR_OP)) {
+ for (i = 0; i < root->nbops; i++) {
+ if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
+ delelement (root->ops[i]);
+ }
+ }
+ free (root);
+ }
+}
+
/* functions */
#define NB_OPERATORS 6
{ "^", Pow, 2, 1, 4}
};
-#define NB_FUNCTIONS 7
+#define NB_FUNCTIONS 9
keyword_t functions[NB_FUNCTIONS] = {
{ "sqrt", Sqr, 1, 4, 5},
{ "pow", Pow, 2, 3, 5},
{ "sin", Sin, 1, 3, 5},
{ "atan", Atn, 1, 4, 5},
{ "exp", Exp, 1, 3, 5},
- { "log", Log, 1, 3, 5}
+ { "log", Log, 1, 3, 5},
+ { "quit", Qui, 0, 4, 5},
+ { "help", Hel, 0, 4, 5}
};
/* subparser function */
new->ops[0] = *proot;
new->ops[1] = parser (*pstr, pstr, new->prio);
if (new->ops[1] == ERROR_OP) {
+ delelement (new);
+ *proot = NULL;
return ERROR_OP;
}
*proot = newelement (Val, 1, 5);
- if (*proot == ERROR_OP) {
+ if (*proot == NULL) {
+ delelement (new);
return ERROR_OP;
}
(*proot)->ops[0] = new;
found = 0;
new = parser (str + 1, &str, 0);
if (new == ERROR_OP) {
+ delelement (root);
return ERROR_OP;
}
for (i = 0; i < root->nbops; i++) {
}
}
if (!found) {
+ delelement (new);
+ delelement (root);
return ERROR_OP;
}
} while (*str == ',');
}
new = parser (str + 1, &str, 0);
if ((new == ERROR_OP) || (*str == ',')) {
+ delelement (new);
+ delelement (root);
return ERROR_OP;
}
root->ops[0] = new;
str += operator->offset;
VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
+ delelement (root);
return ERROR_OP;
}
} else if (*str == '-') {
return ERROR_OP;
}
root = new;
- } else {
+ } else {
+ delelement (root);
return ERROR_OP;
}
str += function->offset;
(*str == '.') || (*str == '+') || (*str == '-')) {
VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
char *pt;
- float value = strtof (str, &pt);
+ double value = strtod (str, &pt);
VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
if (str != pt) {
if (root == NULL) {
return root;
}
if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
+ delelement (root);
return ERROR_OP;
}
} else {
+ delelement (root);
return ERROR_OP;
}
} else {
+ delelement (root);
return ERROR_OP;
}
found = 1;
/* error */
if (!found) {
+ delelement (root);
return ERROR_OP;
}
if (next != NULL) {
*next = str;
}
+
return root;
}
switch (root->func) {
case Val: func = "Value"; break;
+ case Sig: func = "Sign"; break;
case Add: func = "Addition"; break;
case Sub: func = "Subtraction"; break;
case Mul: func = "Multiplication"; break;
case Atn: func = "Arc Tangent"; break;
case Log: func = "Logarithm"; break;
case Exp: func = "Exponantial"; break;
- case Sig: func = "Sign"; break;
+ case Qui: func = "Quit"; break;
+ case Hel: func = "Help"; break;
}
PRINTOUT ("Function: %s\n", func);
}
}
-#define MASK_SUB 0x1
-#define MASK_DIV 0x2
+/* 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;
VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
return 0;
}
+ break;
+ case Qui:
+ case Hel:
+ break;
}
switch (root->func) {
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;