add some coverage tests
[calc.git] / parser.c
index c023a6e959a5fe6fb3f86f76fba752bb97a0745b..c70a15cfde5b65282851d52587cd3920971c317f 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -2,6 +2,7 @@
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "debug.h"
 
@@ -14,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)
@@ -115,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},
@@ -139,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
@@ -149,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)
@@ -500,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);
@@ -627,13 +637,15 @@ void help (void)
     fprintf (stdout, "logical operators:");
     fprintf (stdout, " & | !\n");
     fprintf (stdout, "mathematic functions:");
-    fprintf (stdout, " pow sqrt cos sin tan acos asin atan log exp\n");
+    fprintf (stdout, " pow sqrt log exp\n");
+    fprintf (stdout, "trigonometric functions:");
+    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\n");
+    fprintf (stdout, " cond while print {}\n");
     fprintf (stdout, "miscellaneous functions:");
     fprintf (stdout, " quit help\n");
     fprintf (stdout, "supported constants:");
@@ -740,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) {
@@ -792,9 +807,70 @@ 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;
 }
 
+char **generate_completion_list ()
+{
+    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) {
+            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_FUNCTIONS + NB_CONSTANTS; i++) {
+            if (list[i] != NULL) {
+                free (list[i]);
+            }
+        }
+        free (list);
+    }
+}
+
+
 /* vim: set ts=4 sw=4 et: */