summary refs log tree commit diff
path: root/.bin
diff options
context:
space:
mode:
authorJune McEnroe <curtis.mcenroe@adgear.com>2016-10-04 11:31:42 -0400
committerJune McEnroe <curtis.mcenroe@adgear.com>2016-10-04 11:31:42 -0400
commit2abf73ebf26b6fb880192797f3eb4243ba1a6967 (patch)
tree9cc9ff27022841de3929c3bae22e0dccfc22384d /.bin
parentAdd radix to rpn (diff)
downloadsrc-2abf73ebf26b6fb880192797f3eb4243ba1a6967.tar.gz
src-2abf73ebf26b6fb880192797f3eb4243ba1a6967.zip
Try ops first in rpn
Diffstat (limited to '.bin')
-rwxr-xr-x.bin/rpn.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/.bin/rpn.c b/.bin/rpn.c
index 504e263a..0fcf7933 100755
--- a/.bin/rpn.c
+++ b/.bin/rpn.c
@@ -47,7 +47,7 @@ static int64_t pop(void) {
     return stack.data[--stack.len];
 }
 
-static void stack_op(char op) {
+static bool stack_op(char op) {
     int64_t a, b;
     switch (op) {
         case '$': pop();
@@ -71,8 +71,9 @@ static void stack_op(char op) {
         break; case 'o': stack.radix = 8;
         break; case 'd': stack.radix = 10;
         break; case 'x': stack.radix = 16;
-        break;
+        break; default: return false;
     }
+    return true;
 }
 
 static char *prompt(EditLine *el __attribute((unused))) {
@@ -94,6 +95,7 @@ static char *prompt(EditLine *el __attribute((unused))) {
 int main(int argc __attribute((unused)), char *argv[]) {
     EditLine *el = el_init(argv[0], stdin, stdout, stderr);
     el_set(el, EL_PROMPT, prompt);
+    el_set(el, EL_SIGNAL, true);
 
     for (;;) {
         int count;
@@ -102,13 +104,18 @@ int main(int argc __attribute((unused)), char *argv[]) {
         if (!line) break;
 
         while (*line) {
-            if (isdigit(*line))
-                push(strtoll(line, (char **) &line, stack.radix)); // XXX: ???
-            else
-                stack_op(*line++);
+            if (stack_op(*line)) {
+                line++;
+            } else {
+                char *rest;
+                int64_t val = strtoll(line, &rest, stack.radix);
+                if (rest != line) {
+                    line = rest;
+                    push(val);
+                } else line++;
+            }
         }
     }
-    putchar('\n');
 
     el_end(el);
     return EX_OK;