print function
[calc.git] / parser.c
index b4381ac338dab2381eab0516100b276ad4779939..9b6d5bb2f3f101a2d49c664c404f85fa789b70f4 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -15,6 +15,8 @@ double answer = 0;
 #define STORAGE_SIZE 10
 double storage[STORAGE_SIZE] = {0};
 
+char format[9] = "=> %..g\n";
+
 /* compare codes */
 
 int codecmp (char *ref, char *str)
@@ -116,7 +118,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 23
+#define NB_FUNCTIONS 24
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -140,7 +142,8 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "help", Help, 0, 4, 9},
     { "!",    Not, 1, 1, 6},
     { "cond", Cond, 3, 4, 5},
-    { "while", While, 2, 5, 5}
+    { "while", While, 2, 5, 5},
+    { "print", Print, 1, 5, 5}
 };
 
 #define NB_CONSTANTS 3
@@ -150,6 +153,11 @@ keyword_t constants[NB_CONSTANTS] = {
     { "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)
@@ -501,6 +509,7 @@ void print_element (element_t *root, int level)
     case Cond: func = "Condition"; break;
     case While: func = "While"; break;
     case Prog: func = "Program"; break;
+    case Print: func = "Print"; break;
     }
 
     fprintf (stdout, "Function: %s\n", func);
@@ -636,7 +645,7 @@ void help (void)
     fprintf (stdout, "storage functions:");
     fprintf (stdout, " sto rcl inc dec\n");
     fprintf (stdout, "conditional functions:");
-    fprintf (stdout, " cond while\n");
+    fprintf (stdout, " cond while print {}\n");
     fprintf (stdout, "miscellaneous functions:");
     fprintf (stdout, " quit help\n");
     fprintf (stdout, "supported constants:");
@@ -743,6 +752,9 @@ double evaluate_element (element_t *root, char mask)
             return 0;
         }
         break;
+    case Print:
+        op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : answer;
+        break;
     }
 
     switch (root->func) {
@@ -795,6 +807,7 @@ double evaluate_element (element_t *root, char mask)
         }
     case While: return while_do (root->ops[0], root->ops[1]);
     case Prog: return program_do (root->ops, root->nbops);
+    case Print: printf (format, op0); return op0;
     }
 
     return 0;
@@ -802,13 +815,27 @@ double evaluate_element (element_t *root, char mask)
 
 char **generate_completion_list ()
 {
-    int i, l = 0;
-    char **list = (char **) calloc (NB_FUNCTIONS + NB_CONSTANTS + 1, sizeof (char *));
+    int i, j, l = 0;
+    char **list = (char **) calloc (NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1, sizeof (char *));
     if (list == NULL) {
         VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
         exit (1);
     }
 
+    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) {
+            VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+            exit (1);
+        }
+        l++;
+    }
+
     for (i = 0; i < NB_FUNCTIONS; i++) {
         list[l] = strdup ((functions + i)->keyword);
         if (list[l] == NULL) {
@@ -827,6 +854,15 @@ char **generate_completion_list ()
         l++;
     }
 
+    for (i = 0; i < NB_SYMBOLS; i++) {
+        list[l] = strdup (symbols[i]);
+        if (list[l] == NULL) {
+            VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+            exit (1);
+        }
+        l++;
+    }
+
     return (list);
 }