new option: base
[calc.git] / parser.c
index b9823abaa25f10acd08cc4f40e2667a2e94ea902..94f4c73fd0d752910b28d70ca02261c81446ea65 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -371,51 +371,48 @@ element_t *parser (char *str, char **next, int prio)
 
         /* look for number */
 
-        if (((*str >= '0') && (*str <= '9')) ||
-            (*str == '.') || (*str == '+') || (*str == '-')) {
-            VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
-            char *pt;
-            double value = strtod (str, &pt);
-            VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
-            if (str != pt) {
-                if ((root == NULL) || (root->prio == 6)) {
-                    new = newelement (Val, 1, 5);
-                    new->value = value;
-                    if (root == NULL) {
-                        root = new;
-                    } else {
-                        for (i = 0; i < root->nbops; i++) {
-                            if (root->ops[i] == NULL) {
-                                root->ops[i] = new;
-                                found = 1;
-                                break;
-                            }
-                        }
-                        if (!found) {
-                            delelement (new);
-                            delelement (root);
-                            return ERROR_OP;
+        VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
+        char *pt;
+        double value = (ibase == 10) ? strtod (str, &pt) : strtoul (str, &pt, ibase);
+        VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
+        if (str != pt) {
+            if ((root == NULL) || (root->prio == 6)) {
+                new = newelement (Val, 1, 5);
+                new->value = value;
+                if (root == NULL) {
+                    root = new;
+                } else {
+                    for (i = 0; i < root->nbops; i++) {
+                        if (root->ops[i] == NULL) {
+                            root->ops[i] = new;
+                            found = 1;
+                            break;
                         }
                     }
-                    str = pt;
-                } else if ((*str == '+') || (*str == '-')) {
-                    if ((prio) && (prio > 1)) {
-                        VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
-                        *next = str;
-                        return root;
-                    }
-                    if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
+                    if (!found) {
+                        delelement (new);
                         delelement (root);
                         return ERROR_OP;
                     }
-                } else {
+                }
+                str = pt;
+            } else if ((*str == '+') || (*str == '-')) {
+                if ((prio) && (prio > 1)) {
+                    VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
+                    *next = str;
+                    return root;
+                }
+                if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
                     delelement (root);
                     return ERROR_OP;
                 }
-                found = 1;
+            } else {
+                delelement (root);
+                return ERROR_OP;
             }
-            VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
+            found = 1;
         }
+        VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
 
         /* error */