#include <string.h>
#include "alloc.h"
+#include "argument.h"
#include "debug.h"
#include "element.h"
#include "format.h"
#include "parser.h"
+/* external definition */
+
+extern void history ();
+
/* global variables */
double answer = 0;
+double anglefactor = 1;
+
/* compare codes */
int codecmp (char *ref, char *str)
return 0;
}
-/* functions */
-
-#define MAX_ARGS 100
-
-#define NB_OPERATORS 14
-keyword_t operators[NB_OPERATORS] = {
- { "+\t", Add, 2, 1, 1},
- { "-\t", Sub, 2, 1, 1},
- { "*", Mul, 2, 1, 2},
- { "/", Div, 2, 1, 2},
- { "%", Mod, 2, 1, 3},
- { "^", 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 50
-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},
- { "tan", Tan, 1, 3, 5},
- { "acos", Acos, 1, 4, 5},
- { "asin", Asin, 1, 4, 5},
- { "atan", Atan, 1, 4, 5},
- { "ln", Ln, 1, 2, 5},
- { "log", Log, 1, 3, 5},
- { "exp", Exp, 1, 3, 5},
- { "erfc", Erfc, 1, 4, 5},
- { "erf", Erf, 1, 3, 5},
- { "abs", Abs, 1, 3, 5},
- { "floor", Floor, 1, 5, 5},
- { "ceil", Ceil, 1, 4, 5},
- { "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},
- { "mem", Mem, 1, 3, 5},
- { "clr", Clear, 0, 3, 9},
- { "quit", Quit, 0, 4, 9},
- { "help", Help, 0, 4, 9},
- { "!", Not, 1, 1, 6},
- { "cond", Cond, 3, 4, 5},
- { "while", While, 2, 5, 5},
- { "print", Print, 1, 5, 5},
- { "prog", Prog, 2, 4, 9},
- { "arg", Arg, 1, 3, 5},
- { "call", Call, MAX_ARGS, 4, 5},
- { "ls", List, 0, 2, 9},
- { "edit", Edit, 1, 4, 9},
- { "del", Del, 1, 3, 9},
- { "get", Get, 1, 3, 5},
- { "len", Length, 0, 3, 5},
- { "pop", Pop, 0, 3, 5},
- { "push", Push, 1, 4, 5},
- { "put", Put, 2, 3, 5},
- { "set", Set, MAX_ARGS, 3, 5},
- { "show", Show, 0, 4, 5},
- { "max", Max, 2, 3, 5},
- { "mean", Mean, 2, 4, 5},
- { "med", Median, 0, 3, 5},
- { "min", Min, 2, 3, 5},
- { "ord", Order, 0, 3, 5},
- { "prod", Prod, 0, 4, 5},
- { "sum", Sum, 0, 3, 5},
- { "var", Variance, 2, 3, 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}
-};
-
-#define NB_SYMBOLS 4
-char *symbols[NB_SYMBOLS] = {
- "(", ")", "{", "}"
-};
-
/* subparser function */
element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
/* 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 */
}
for (i = 0; i < level; i++) {
- fprintf (stdout, " ");
+ printf (" ");
}
switch (root->func) {
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 History: func = "History"; break;
case Ans: func = "Ans"; break;
case Pi: func = "Pi"; break;
case E: func = "E"; 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;
+ case Deg: func = "Degree"; break;
+ case Grad: func = "Gradian"; break;
+ case Rad: func = "Radian"; break;
}
- fprintf (stdout, "Function: %s\n", func);
+ printf ("Function: %s\n", func);
if ((root->func == Val) && (root->ops[0] == NULL)) {
for (i = 0; i < level; i++) {
- fprintf (stdout, " ");
+ printf (" ");
}
- fprintf (stdout, "value: %f\n", root->value);
+ printf ("value: %f\n", root->value);
} else {
for (i = 0; i < root->nbops; i++) {
print_element (root->ops[i], level + 1);
void quit (void)
{
- fprintf (stdout, "bye\n");
+ printf ("bye\n");
exit (0);
}
void help (void)
{
- fprintf (stdout, "calc is a simple calculator\n\n");
- fprintf (stdout, "arithmetic op.:");
- fprintf (stdout, " + - * / %% ^\n");
- fprintf (stdout, "comparison op.:");
- fprintf (stdout, " == != >= <= > <\n");
- fprintf (stdout, "logical op.:");
- fprintf (stdout, " & | !\n");
- fprintf (stdout, "mathematic func.:");
- fprintf (stdout, " exp ln log pow sqrt\n");
- fprintf (stdout, "trigonometric func.:");
- fprintf (stdout, " acos asin atan cos sin tan\n");
- fprintf (stdout, "error functions:");
- fprintf (stdout, " erf erfc\n");
- fprintf (stdout, "miscellaneous func.:");
- fprintf (stdout, " abs ceil floor\n");
- fprintf (stdout, "storage func.:");
- fprintf (stdout, " clear dec disp inc mem rcl sto\n");
- fprintf (stdout, "control flow prim.:");
- fprintf (stdout, " cond print while {} ;\n");
- fprintf (stdout, "program management:");
- fprintf (stdout, " arg call del edit ls prog\n");
- fprintf (stdout, "stack management:");
- fprintf (stdout, " get len pop push put set show\n");
- 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, "constants:");
- fprintf (stdout, " ans e pi\n");
+ printf ("calc is a simple calculator\n\n");
+ printf ("arithmetic op.:");
+ printf (" + - * / %% ^\n");
+ printf ("comparison op.:");
+ printf (" == != >= <= > <\n");
+ printf ("logical op.:");
+ printf (" & | !\n");
+ printf ("mathematic func.:");
+ printf (" exp ln log pow sqrt\n");
+ printf ("trigonometric func.:");
+ printf (" acos asin atan cos sin tan\n");
+ printf ("error functions:");
+ printf (" erf erfc\n");
+ printf ("miscellaneous func.:");
+ printf (" abs ceil floor\n");
+ printf ("storage func.:");
+ printf (" clear dec disp inc mem rcl sto\n");
+ printf ("control flow prim.:");
+ printf (" cond print while {} ;\n");
+ printf ("program management:");
+ printf (" arg call del edit ls prog\n");
+ printf ("stack management:");
+ printf (" get len pop push put set show\n");
+ printf ("stack func.:");
+ printf (" max mean med min ord prod sum var\n");
+ printf ("control management:");
+ printf (" base format help hist quit\n");
+ printf ("angle management:");
+ printf (" deg grad rad\n");
+ printf ("constants:");
+ printf (" 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 */
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:
case Clear:
case Quit:
case Help:
+ case History:
case Ans:
case Pi:
case E:
case Order:
case Prod:
case Sum:
+ case Deg:
+ case Grad:
+ case Rad:
break;
case While:
if (root->ops[0] == NULL) {
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 Mod: return fmod (op0, op1);
case Pow: return pow (op0, op1);
case Sqr: return sqrt (op0);
- case Cos: return cos (op0);
- case Sin: return sin (op0);
- case Tan: return tan (op0);
- case Acos: return acos (op0);
- case Asin: return asin (op0);
- case Atan: return atan (op0);
+ case Cos: return cos (op0 / anglefactor);
+ case Sin: return sin (op0 / anglefactor);
+ case Tan: return tan (op0 / anglefactor);
+ case Acos: return acos (op0) * anglefactor;
+ case Asin: return asin (op0) * anglefactor;
+ case Atan: return atan (op0) * anglefactor;
case Ln: return log (op0);
case Log: return log10 (op0);
case Exp: return exp (op0);
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;
+ case History: history (); break;
case Ans: return answer;
case Pi: return M_PI;
case E: return M_E;
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;
+ case Deg:
+ anglefactor = 180 / M_PI;
+ break;
+ case Grad:
+ anglefactor = 200 / M_PI;
+ break;
+ case Rad:
+ anglefactor = 1;
+ break;
}
return 0;
}
-char **generate_completion_list ()
-{
- int i, j, l = 0;
- char **list = (char **) callocordie (NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1, sizeof (char *));
-
- for (i = 0; i < NB_OPERATORS; i++) {
- list[l] = strdup ((operators + i)->keyword);
- for (j = 0; j < (int)strlen (list[l]); j++) {
- if (list[i][j] == '\t') {
- list[i][j] = '\0';
- }
- }
- if (list[l] != NULL) {
- l++;
- }
- }
-
- for (i = 0; i < NB_FUNCTIONS; i++) {
- list[l] = strdup ((functions + i)->keyword);
- if (list[l] != NULL) {
- l++;
- }
- }
-
- for (i = 0; i < NB_CONSTANTS; i++) {
- list[l] = strdup ((constants + i)->keyword);
- if (list[l] != NULL) {
- l++;
- }
- }
-
- for (i = 0; i < NB_SYMBOLS; i++) {
- list[l] = strdup (symbols[i]);
- if (list[l] != NULL) {
- l++;
- }
- }
-
- return (list);
-}
-
-void free_completion_list (char **list)
-{
- int i;
-
- if (list) {
- for (i = 0; i < NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1; i++) {
- if (list[i] != NULL) {
- free (list[i]);
- }
- }
- free (list);
- }
-}
-
/* vim: set ts=4 sw=4 et: */