+/* storage functions */
+
+void memory (int nb)
+{
+ int i, l;
+ double *tmp = NULL;
+ if (nb != storage_size) {
+ l = (nb < storage_size) ? nb : storage_size;
+ tmp = (double *) calloc (nb, sizeof (double));
+ if (tmp == NULL) {
+ VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+ exit (1);
+ }
+ for (i = 0; i < l; i++) {
+ tmp[i] = storage[i];
+ }
+ if (storage != NULL) {
+ free (storage);
+ }
+ storage = tmp;
+ storage_size = nb;
+ }
+}
+
+double store (int index, double value)
+{
+ if (storage_size == -1) {
+ memory (DEFAULT_STORAGE_SIZE);
+ }
+ if ((index > 0) && (index <= storage_size)) {
+ storage[index - 1] = value;
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+ }
+ return value;
+}
+
+double recall (int index)
+{
+ if (storage_size == -1) {
+ memory (DEFAULT_STORAGE_SIZE);
+ }
+ if ((index > 0) && (index <= storage_size)) {
+ return storage[index - 1];
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+ }
+ return 0;
+}
+
+double increase (int index)
+{
+ if (storage_size == -1) {
+ memory (DEFAULT_STORAGE_SIZE);
+ }
+ if ((index > 0) && (index <= storage_size)) {
+ return storage[index - 1]++;
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+ }
+ return 0;
+}
+
+double decrease (int index)
+{
+ if (storage_size == -1) {
+ memory (DEFAULT_STORAGE_SIZE);
+ }
+ if ((index > 0) && (index <= storage_size)) {
+ return storage[index - 1]--;
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+ }
+ return 0;
+}
+
+void display (void)
+{
+ int i;
+ if (storage_size == -1) {
+ memory (DEFAULT_STORAGE_SIZE);
+ }
+ fprintf (stdout, "storage:");
+ for (i = 0; i < storage_size; i++) {
+ fprintf (stdout, " ");
+ fprintf (stdout, minform, storage[i]);
+ }
+ fprintf (stdout, "\n");
+}
+
+void clear ()
+{
+ int i;
+ for (i = 0; i < storage_size; i++) {
+ storage[i] = 0;
+ }
+}
+
+/* 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"));
+ 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;
+}
+
+/* print function */
+
+void set_format (char *prompt, int precision)
+{
+ char buffer[128] = {0};
+ free_format ();
+ sprintf (buffer, "%s%%.%dg\n", prompt, precision);
+ format = strdup (buffer);
+ sprintf (buffer, "%%.%dg", precision);
+ minform = strdup (buffer);
+}
+
+void free_format ()
+{
+ if (format) {
+ free (format);
+ format = NULL;
+ }
+ if (minform) {
+ free (minform);
+ minform = NULL;
+ }
+}
+
+double print (double value)
+{
+ fprintf (stdout, format ? format : DEFAULT_FORMAT, value);
+ fflush (stdout);
+ return value;
+}
+
+/* quit function */
+
+void quit (void)
+{
+ fprintf (stdout, "bye\n");
+ exit (0);
+}
+
+/* program function */
+
+void prog (int id, int nbmems, element_t *root)
+{ }
+
+double call (int id, int nbobs, element_t **ops)
+{
+ return 0;
+}
+
+void list ()
+{ }
+
+void edit (int id)
+{ }
+
+void del (int id)
+{ }
+
+/* help message */
+
+void help (void)
+{
+ fprintf (stdout, "calc is a simple calculator\n\n");
+ fprintf (stdout, "supported operators:");
+ fprintf (stdout, " + - * / %% ^\n");
+ fprintf (stdout, "camparison operators:");
+ fprintf (stdout, " == != >= <= > <\n");
+ fprintf (stdout, "logical operators:");
+ fprintf (stdout, " & | !\n");
+ fprintf (stdout, "mathematic functions:");
+ fprintf (stdout, " pow sqrt log exp\n");
+ fprintf (stdout, "trig. func.:");
+ fprintf (stdout, " cos sin tan acos asin atan\n");
+ fprintf (stdout, "supported functions:");
+ fprintf (stdout, " abs ceil floor\n");
+ fprintf (stdout, "storage functions:");
+ fprintf (stdout, " mem sto rcl inc dec disp\n");
+ fprintf (stdout, "prog. functions:");
+ fprintf (stdout, " cond while print {} ;\n");
+ fprintf (stdout, "misc. functions:");
+ fprintf (stdout, " quit help\n");
+ fprintf (stdout, "supported constants:");
+ fprintf (stdout, " e pi\n");
+}
+