extend a test
[calc.git] / parser.c
index 4da6b6b64b31497d702bffba03a42135cc206e95..38b7c2692fad24c3a4c9422caa92d3214ad70c3c 100644 (file)
--- a/parser.c
+++ b/parser.c
 
 double answer = 0;
 
-#define STORAGE_SIZE 10
-double storage[STORAGE_SIZE] = {0};
+#define DEFAULT_STORAGE_SIZE 10
+int storage_size = -1;
 
+double *storage = NULL;
+
+#define DEFAULT_FORMAT "=> %.6g\n"
 char *format = NULL;
+char *minform = NULL;
 
 /* compare codes */
 
@@ -118,7 +122,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 24
+#define NB_FUNCTIONS 26
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -138,6 +142,8 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "inc",  Inc, 1, 3, 5},
     { "dec",  Dec, 1, 3, 5},
     { "disp", Disp, 0, 4, 9},
+    { "mem",  Mem, 1, 3, 9},
+    { "clr",  Clear, 0, 3, 9},
     { "quit", Quit, 0, 4, 9},
     { "help", Help, 0, 4, 9},
     { "!",    Not, 1, 1, 6},
@@ -195,6 +201,15 @@ element_t *parser (char *str, char **next, int prio)
         element_t *new = NULL;
         VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
 
+        /* end without printing */
+
+        if (*str == ';') {
+            if (root) {
+                root->hidden = 1;
+            }
+            break;
+        }
+
         /* skip spaces and tabs */
 
         if ((*str == ' ') || (*str == '\t')) {
@@ -492,6 +507,8 @@ void print_element (element_t *root, int level)
     case Inc: func = "Increase"; break;
     case Dec: func = "Decrease"; break;
     case Disp: func = "Display"; break;
+    case Mem: func = "Memory"; break;
+    case Clear: func = "Clear"; break;
     case Quit: func = "Quit"; break;
     case Help: func = "Help"; break;
     case Ans: func = "Ans"; break;
@@ -528,42 +545,76 @@ void print_element (element_t *root, int level)
 
 /* 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 ((index > 0) && (index <= STORAGE_SIZE)) {
+    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) [1, %d]\n", index, STORAGE_SIZE));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
     return value;
 }
 
 double recall (int index)
 {
-    if ((index > 0) && (index <= STORAGE_SIZE)) {
+    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) [1, %d]\n", index, STORAGE_SIZE));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
     return 0;
 }
 
 double increase (int index)
 {
-    if ((index > 0) && (index <= STORAGE_SIZE)) {
+    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) [1, %d]\n", index, STORAGE_SIZE));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
     return 0;
 }
 
 double decrease (int index)
 {
-    if ((index > 0) && (index <= STORAGE_SIZE)) {
+    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) [1, %d]\n", index, STORAGE_SIZE));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
     return 0;
 }
@@ -571,13 +622,25 @@ double decrease (int index)
 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, " %g", storage[i]);
+    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)
@@ -622,9 +685,11 @@ double program_do (element_t **prog, int nbcalls)
 void set_format (char *prompt, int precision)
 {
     char buffer[128] = {0};
-    sprintf (buffer, "%s%%.%dg\n", prompt, precision);
     free_format ();
+    sprintf (buffer, "%s%%.%dg\n", prompt, precision);
     format = strdup (buffer);
+    sprintf (buffer, "%%.%dg", precision);
+    minform = strdup (buffer);
 }
 
 void free_format ()
@@ -633,11 +698,15 @@ void free_format ()
         free (format);
         format = NULL;
     }
+    if (minform) {
+        free (minform);
+        minform = NULL;
+    }
 }
 
 double print (double value)
 {
-    fprintf (stdout, format ? format : "=> %.6g\n", value);
+    fprintf (stdout, format ? format : DEFAULT_FORMAT, value);
     fflush (stdout);
     return value;
 }
@@ -663,15 +732,15 @@ void help (void)
     fprintf (stdout, " & | !\n");
     fprintf (stdout, "mathematic functions:");
     fprintf (stdout, " pow sqrt log exp\n");
-    fprintf (stdout, "trigonometric functions:");
+    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, " sto rcl inc dec\n");
-    fprintf (stdout, "conditional functions:");
-    fprintf (stdout, " cond while print {}\n");
-    fprintf (stdout, "miscellaneous 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");
@@ -755,6 +824,7 @@ double evaluate_element (element_t *root, char mask)
     case Inc:
     case Dec:
     case Not:
+    case Mem:
     case Cond:
         if (root->ops[0]) {
             op0 = evaluate_element (root->ops[0], 0);
@@ -764,6 +834,7 @@ double evaluate_element (element_t *root, char mask)
         }
         break;
     case Disp:
+    case Clear:
     case Quit:
     case Help:
     case Ans:
@@ -808,6 +879,8 @@ double evaluate_element (element_t *root, char mask)
     case Inc: return increase ((int)op0);
     case Dec: return decrease ((int)op0);
     case Disp: display (); break;
+    case Mem: memory ((int)op0); break;
+    case Clear: clear (); break;
     case Quit: quit (); break;
     case Help: help (); break;
     case Ans: return answer;