#include <malloc.h>
#include <math.h>
+#include <stdio.h>
#include <stdlib.h>
#include "debug.h"
-#include "fdprintf.h"
#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)
{
element_t *new = (element_t *) calloc (1, sizeof (element_t));
if (new == NULL) {
- VERBOSE (ERROR, fdprintf (stdfderr, "can't allocate memory\n"));
+ VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
return NULL;
}
new->func = function;
/* functions */
-#define NB_OPERATORS 6
+#define NB_OPERATORS 14
keyword_t operators[NB_OPERATORS] = {
{ "+\t", Add, 2, 1, 1},
{ "*", Mul, 2, 1, 2},
{ "/", Div, 2, 1, 2},
{ "%", Mod, 2, 1, 3},
- { "^", Pow, 2, 1, 4}
+ { "^", Pow, 2, 1, 4},
+ { "==", Equal, 2, 2, -1},
+ { "!=", Diff, 2, 2, -1},
+ { ">=", Ge, 2, 2, -1},
+ { "<=", Le, 2, 2, -1},
+ { ">", Gt, 2, 1, -1},
+ { "<", Lt, 2, 1, -1},
+ { "&", And, 2, 1, -2},
+ { "|", Or, 2, 1, -2}
};
-#define NB_FUNCTIONS 9
+#define NB_FUNCTIONS 13
keyword_t functions[NB_FUNCTIONS] = {
{ "sqrt", Sqr, 1, 4, 5},
{ "pow", Pow, 2, 3, 5},
{ "cos", Cos, 1, 3, 5},
{ "sin", Sin, 1, 3, 5},
- { "atan", Atn, 1, 4, 5},
+ { "atan", Atan, 1, 4, 5},
{ "exp", Exp, 1, 3, 5},
{ "log", Log, 1, 3, 5},
- { "quit", Qui, 0, 4, 5},
- { "help", Hel, 0, 4, 5}
+ { "sto", Store, 2, 3, 9},
+ { "rcl", Recall, 1, 3, 5},
+ { "disp", Disp, 0, 4, 9},
+ { "quit", Quit, 0, 4, 9},
+ { "help", Help, 0, 4, 9},
+ { "!", Not, 1, 1, 5}
+};
+
+#define NB_CONSTANTS 3
+keyword_t constants[NB_CONSTANTS] = {
+ { "ans", Ans, 0, 3, 5},
+ { "e", E, 0, 1, 5},
+ { "pi", Pi, 0, 2, 5}
};
/* subparser function */
}
new->ops[0] = *proot;
new->ops[1] = parser (*pstr, pstr, new->prio);
+ if ((new->ops[1] != NULL) && (new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9)) {
+ delelement (new->ops[1]);
+ new->ops[1] = ERROR_OP;
+ }
if (new->ops[1] == ERROR_OP) {
delelement (new);
*proot = NULL;
element_t *root = NULL;
int i;
- VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
/* main loop */
while (*str != '\0') {
int found = 0;
element_t *new = NULL;
- VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
+ VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
/* skip spaces and tabs */
/* check for open bracket */
if (*str == '(') {
- VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
if (root) {
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 == ERROR_OP) {
delelement (root);
return ERROR_OP;
return ERROR_OP;
}
new = parser (str + 1, &str, 0);
+ if ((new != NULL) && (new != ERROR_OP) && (new->prio == 9)) {
+ delelement (new);
+ new = ERROR_OP;
+ }
if ((new == ERROR_OP) || (*str == ',')) {
delelement (new);
delelement (root);
root->ops[0] = new;
}
str++;
- VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
continue;
}
for (i = 0; i < NB_OPERATORS; i++) {
keyword_t *operator = operators + i;
if (codecmp (operator->keyword, str) == 0) {
- VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
if (root) {
if ((prio) && (prio > operator->prio)) {
- VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
*next = str;
return root;
}
str += operator->offset;
- VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
+ VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
delelement (root);
return ERROR_OP;
return ERROR_OP;
}
found = 1;
- VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
break;
}
}
for (i = 0; i < NB_FUNCTIONS; i++) {
keyword_t *function = functions + i;
if (codecmp (function->keyword, str) == 0) {
- VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
if (root == NULL) {
- VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
+ VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
new = newelement (function->func, function->nbops, function->prio);
if (new == NULL) {
return ERROR_OP;
}
str += function->offset;
found = 1;
- VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
+ break;
+ }
+ }
+ if (found) {
+ continue;
+ }
+
+ /* look for constant */
+
+ for (i = 0; i < NB_CONSTANTS; i++) {
+ keyword_t *constant = constants + i;
+ if (codecmp (constant->keyword, str) == 0) {
+ VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
+ if (root == NULL) {
+ VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
+ new = newelement (constant->func, constant->nbops, constant->prio);
+ if (new == NULL) {
+ return ERROR_OP;
+ }
+ root = new;
+ } else {
+ delelement (root);
+ return ERROR_OP;
+ }
+ str += constant->offset;
+ found = 1;
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n"));
break;
}
}
if (((*str >= '0') && (*str <= '9')) ||
(*str == '.') || (*str == '+') || (*str == '-')) {
- VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
char *pt;
double value = strtod (str, &pt);
- VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
+ VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
if (str != pt) {
if (root == NULL) {
new = newelement (Val, 1, 5);
new->value = value;
root = new;
str = pt;
- } else if (root->func == Val) {
- if ((*str == '+') || (*str == '-')) {
- if ((prio) && (prio > 1)) {
- VERBOSE (DEBUG, PRINTOUT ("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;
}
}
found = 1;
}
- VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
}
/* error */
}
for (i = 0; i < level; i++) {
- PRINTOUT (" ");
+ fprintf (stdout, " ");
}
switch (root->func) {
case Sqr: func = "Square Root"; break;
case Cos: func = "Cosine"; break;
case Sin: func = "Sine"; break;
- case Atn: func = "Arc Tangent"; break;
+ case Atan: func = "Arc Tangent"; break;
case Log: func = "Logarithm"; break;
case Exp: func = "Exponantial"; break;
- case Qui: func = "Quit"; break;
- case Hel: func = "Help"; 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;
+ case Equal: func = "Equal"; break;
+ case Diff: func = "Different"; break;
+ case Ge: func = "Greater or equal"; break;
+ case Le: func = "Lesser or equal"; break;
+ case Gt: func = "Greater"; break;
+ case Lt: func = "Lesser"; break;
+ case And: func = "And"; break;
+ case Or: func = "Or"; break;
+ case Not: func = "Not"; break;
}
- PRINTOUT ("Function: %s\n", func);
+ fprintf (stdout, "Function: %s\n", func);
if ((root->func == Val) && (root->ops[0] == NULL)) {
for (i = 0; i < level; i++) {
- PRINTOUT (" ");
+ fprintf (stdout, " ");
}
- PRINTOUT ("value: %f\n", root->value);
+ fprintf (stdout, "value: %f\n", root->value);
} else {
for (i = 0; i < root->nbops; i++) {
print_element (root->ops[i], level + 1);
}
}
+/* 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)
{
- PRINTOUT ("bye\n");
+ fprintf (stdout, "bye\n");
exit (0);
}
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");
+ fprintf (stdout, "calc is a simple calculator\n\n");
+ fprintf (stdout, "supported operators:\n");
+ fprintf (stdout, " + - * / %% ^\n\n");
+ fprintf (stdout, "camparison operators:\n");
+ 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");
+ fprintf (stdout, " e pi\n");
}
/* evaluate element tree */
char nextmask = mask;
if ((root == NULL) || (root == ERROR_OP)) {
- VERBOSE (WARNING, PRINTOUT ("error while evaluating\n"));
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
return 0;
}
case Div:
case Mod:
case Pow:
+ case Store:
+ case Equal:
+ case Diff:
+ case Ge:
+ case Le:
+ case Gt:
+ case Lt:
+ case And:
+ case Or:
if (root->ops[1]) {
op1 = evaluate_element (root->ops[1], nextmask);
- } else {
- VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[1])\n"));
+ } else if (root->func != Store) {
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
return 0;
}
/* fallthrough */
case Sqr:
case Cos:
case Sin:
- case Atn:
+ case Atan:
case Log:
case Exp:
+ case Recall:
+ case Not:
if (root->ops[0]) {
op0 = evaluate_element (root->ops[0], 0);
} else {
- VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
return 0;
}
break;
- case Qui:
- case Hel:
+ case Disp:
+ case Quit:
+ case Help:
+ case Ans:
+ case Pi:
+ case E:
break;
}
case Sqr: return sqrt (op0);
case Cos: return cos (op0);
case Sin: return sin (op0);
- case Atn: return atan (op0);
+ case Atan: return atan (op0);
case Log: return log (op0);
case Exp: return exp (op0);
- case Qui: quit (); break;
- case Hel: help (); break;
+ 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;
+ case Equal: return op0 == op1;
+ case Diff: return op0 != op1;
+ case Ge: return op0 >= op1;
+ case Le: return op0 <= op1;
+ case Gt: return op0 > op1;
+ case Lt: return op0 < op1;
+ case And: return (op0 != 0) && (op1 != 0);
+ case Or: return (op0 != 0) || (op1 != 0);
+ case Not: return (op0 == 0);
}
return 0;