Merge branch 'master' of https://secure.softndesign.org/git/calc
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index 6414c2df28f98b7b5b8ffdf1574a6abbda7d3960..e255bd2ca421baf948ee8400f8685859555d1960 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: atoi.o fdprintf.o */
+/* linker: atoi.o debug.o fdprintf.o parser.o -lm */
 
 //#include <malloc.h>
 #include <stddef.h>
@@ -8,7 +8,9 @@
 #include <unistd.h>
 
 #include "atoi.h"
+#include "debug.h"
 #include "fdprintf.h"
+#include "parser.h"
 
 /* constants */
 
 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
 
-/* verbose */
-
-#define ERROR 0
-#define WARNING 1
-#define INFO 2
-#define DEBUG 3
-
-#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
-
 /* gobal variables */
 
 char *progname = NULL;
-int verbose = 2;
 
 /* help function */
 
@@ -54,6 +46,7 @@ int main (int argc, char *argv[])
     char buffer[BUFFER_SIZE + 1] = {0};
     char *pt = buffer;
     int i = 0, j = 0, n;
+    int ret = 0;
 
     /* program name */
 
@@ -94,16 +87,28 @@ int main (int argc, char *argv[])
     /* read from input stream */
 
     while ((n = read (stdfdin, pt, BUFFER_SIZE - (pt - buffer))) != 0) {
-        VERBOSE (DEBUG, PRINTOUT ("read %d bytes\n", n));
+        VERBOSE (INFO, PRINTOUT ("read %d bytes\n", n));
         n += (pt - buffer);
+        if ((n == 2) && (buffer[0] == '.')) {
+            return 0;
+        }
 
         /* look for end of line */
         for (i = 0, j = 0; i < n; i++) {
             if (buffer[i] == '\n') {
                 buffer[i] = 0;
-                VERBOSE (DEBUG, PRINTOUT ("line(%d): %s\n", j, buffer + j));
+                VERBOSE (INFO, PRINTOUT ("line(%d): %s\n", j, buffer + j));
+                element_t *element = parser (buffer + j, NULL, 0);
+                if (element == ERROR_OP) {
+                    VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer));
+                    ret = 1;
+                       } else {
+                    VERBOSE (INFO, print_element (element, 0));
+                    PRINTOUT ("=> %f\n", evaluate_element (element, 0));
+                    delelement (element);
+                    ret = 0;
+                }
                 //fsync (stdfdout);
-                fflush (stdout);
                 j = i + 1;
             }
         }
@@ -120,13 +125,54 @@ int main (int argc, char *argv[])
         }
     }
 
-    return 0;
+    return ret;
 }
 
 // test: calc.exe -h
 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: echo 1 | calc.exe -v3 | grep -q value
 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
-// test: echo "foo\nbar\nfoobar" | calc.exe -v3
+// test: echo "1 + 2" | calc.exe | grep -q '=> 3'
+// test: echo "1 - 2" | calc.exe | grep -q '=> -1'
+// test: echo "2 * 3" | calc.exe | grep -q '=> 6'
+// test: echo "1 / 2" | calc.exe | grep -q '=> 5e-1'
+// test: echo "8 % 3" | calc.exe | grep -q '=> 2'
+// test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2.8'
+// test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
+// test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2.3401'
+// test: echo "sqrt (2)" | calc.exe | grep -q '=> 1.414213'
+// test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
+// test: echo "cos (2)" | calc.exe | grep -q '=> -4.161468e-1'
+// test: echo "sin (2)" | calc.exe | grep -q '=> 9.092974e-1'
+// test: echo "atan (2)" | calc.exe | grep -q '=> 1.107148'
+// test: echo "exp (2)" | calc.exe | grep -q '=> 7.389056'
+// test: echo "log (2)" | calc.exe | grep -q '=> 6.931471e-1'
+// test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
+// test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1.54030'
+// test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2.63274'
+// test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
+// test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -2.5e-1'
+// test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8.549879'
+// test: echo "1 + -2" | calc.exe | grep -q '=> -1'
+// test: echo "1 - +2" | calc.exe | grep -q '=> -1'
+// test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
+// test: echo "-1+2" | calc.exe | grep -q '=> 1'
+// test: echo "1-2" | calc.exe | grep -q '=> -1'
+// test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4.666666'
+// test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 3.7e1'
+// test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3.074e3'
+// test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 3.267241e1'
+// test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -5e-1'
+// test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -5e-1'
+// test: echo "95-6.3+15" | calc.exe | grep -q '=> 1.037e2'
+// test: echo "-cos (0) + 1" | calc.exe | grep -q '=> 0'
+// test: echo "quit" | calc.exe | grep -q 'bye'
+// test: echo "help" | calc.exe | grep -q 'miscellaneous'
+// test: echo "1 + 2 *" | calc.exe | grep -q 'error'
+// test: echo "* 1 - 2" | calc.exe | grep -q 'error'
+// test: echo "2 + * 3" | calc.exe | grep -q 'error'
+// test: echo "sqrt 2" | calc.exe | grep -q 'error'
+// test: echo "pow (2)" | calc.exe | grep -q 'error'
 
 /* vim: set ts=4 sw=4 et: */