second preliminary version
authorLaurent Mazet <mazet@softndesign.org>
Sun, 25 Dec 2022 22:13:36 +0000 (23:13 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 25 Dec 2022 22:13:36 +0000 (23:13 +0100)
calc.c
parser.c

diff --git a/calc.c b/calc.c
index 1c4099f13d7df7b11da1a4fc99f440df967c9921..d2fdc8c231ae7a43c2244d9adae1cc5642826490 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -87,7 +87,7 @@ 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;
@@ -97,7 +97,7 @@ int main (int argc, char *argv[])
         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, NULL);
                 if (element == (void *)(-1)) {
                     VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer));
index 2190178ecba23fc5898a4cac2a2dc22db343f982..48fb98009b9ace46f0df19152c1a0ed8a4957a51 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -45,7 +45,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "+ ",  Add, 2, 1 },
     { "+\t", Add, 2, 1 },
     { "- ",  Sub, 2, 1 },
-    { "- ",  Sub, 2, 1 },
+    { "-\t", Sub, 2, 1 },
     { "*",   Mul, 2, 1 },
     { "/",   Div, 2, 1 },
     { "^",   Pow, 2, 1 }
@@ -69,11 +69,13 @@ element_t *parser (char *str, char **next)
     element_t *root = NULL;
     int i, j;
 
+    VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
+
     /* main loop */
     while (*str != '\0') {
         int found = 0;
         element_t *new = NULL;
-        VERBOSE (DEBUG, PRINTOUT ("Processing: %s\n", str));
+        VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
 
         /* skip spaces and tabs */
 
@@ -98,16 +100,11 @@ element_t *parser (char *str, char **next)
 
         /* check for parent */
 
-        if (*str == ')') {
-            if (next != NULL) {
-                *next = str + 1;
-            }
-            return root;
-        }
         if (*str == '(') {
+            VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
             new = parser (str + 1, &str);
-            if (new == (void *)(-1)) {
-                return new;
+            if (new == ERROR_OP) {
+                return ERROR_OP;
             }
             if (root) {
                 for (i = 0, j = 0; i < root->nbops; i++) {
@@ -128,9 +125,18 @@ element_t *parser (char *str, char **next)
                 if (!found) {
                     return ERROR_OP;
                 }
-            } else {
-                return root;
             }
+            VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
+            if (next != NULL) {
+                *next = str;
+            }
+            return root;
+        }
+        if (*str == ')') {
+            if (next != NULL) {
+                *next = str + 1;
+            }
+            return root;
         }
 
         /* look for operators */
@@ -138,34 +144,41 @@ element_t *parser (char *str, char **next)
         for (i = 0; i < NB_OPERATORS; i++) {
             keyword_t *operator = operators + i;
             if (codecmp (operator->keyword, str) == 0) {
-                if ((root) && (root->func == Val)) {
-                    VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", operator->func));
+                VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
+                str += operator->offset;
+                if ((root) && (root->func != Set)) {
+                    VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
                     new = newelement (operator->func, operator->nbops);
                     if (new == NULL) {
                         return ERROR_OP;
                     }
                     new->ops[0] = root;
                     root = new;
-                 } else {
+                    new = parser (str, &str);
+                    if (new == ERROR_OP) {
+                        return ERROR_OP;
+                    }
+                    root->ops[1] = new;
+                } else {
                     return ERROR_OP;
                 }
-                str += operator->offset;
                 found = 1;
+                VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
                 break;
             }
         }
         if (found) {
-            VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
             continue;
         }
+
         /* look for functions */
 
         for (i = 0; i < NB_FUNCTIONS; i++) {
             keyword_t *function = functions + i;
             if (codecmp (function->keyword, str) == 0) {
+                VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
                 if (root == NULL) {
-                    VERBOSE (DEBUG, PRINTOUT ("Func: %d\n", function->func));
+                    VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
                     new = newelement (function->func, function->nbops);
                     if (new == NULL) {
                         return ERROR_OP;
@@ -176,11 +189,11 @@ element_t *parser (char *str, char **next)
                 }
                 str += function->offset;
                 found = 1;
+                VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
                 break;
             }
         }
         if (found) {
-            VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
             continue;
         }
 
@@ -189,7 +202,7 @@ element_t *parser (char *str, char **next)
         if (((*str == '-') || (*str == '+')) &&
             ((*(str + 1) >= '0') && (*(str + 1) <= '9')) &&
             ((root) && (root->func == Val))) {
-            VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", Add));
+            VERBOSE (INFO, PRINTOUT ("Oper: %d\n", Add));
             new = newelement (Add, 2);
             if (new == NULL) {
                 return ERROR_OP;
@@ -200,17 +213,17 @@ element_t *parser (char *str, char **next)
 
         /* look for number */
 
-        if (((*str >= '0') && (*str <= '9')) ||
-            (*str == '.') || (*str == '-') ||(*str == '+')) {
+        if (((*str >= '0') && (*str <= '9')) || (*str == '.')) {
+            VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
             char *pt;
             float value = strtof (str, &pt);
-            VERBOSE (DEBUG, PRINTOUT ("Value: %f\n", value));
+            VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
             if (str != pt) {
                 new = newelement (Val, 1);
-                new->value = value;
                 if (new == NULL) {
                     return ERROR_OP;
                 }
+                new->value = value;
                 if (root == NULL) {
                     root = new;
                 } else {
@@ -236,6 +249,7 @@ element_t *parser (char *str, char **next)
                 str = pt;
                 found = 1;
             }
+            VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
         }
 
         /* error */
@@ -246,6 +260,9 @@ element_t *parser (char *str, char **next)
 
     }
 
+    if (next != NULL) {
+        *next = str;
+    }
     return root;
 }
 
@@ -256,9 +273,10 @@ void print_element (element_t *root, int level)
     char *func = NULL;
     int i;
 
-    if (root == NULL)
+    if ((root == NULL) || (root == ERROR_OP)) {
         return;
-    
+    }
+
     for (i = 0; i < level; i++) {
         PRINTOUT (" ");
     }