From 603c3105dc86442b97e51c2895e651aaac1625d1 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Tue, 4 Oct 2016 11:06:40 -0400 Subject: Add radix to rpn --- .bin/rpn.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/.bin/rpn.c b/.bin/rpn.c index c2dc4ccf..504e263a 100755 --- a/.bin/rpn.c +++ b/.bin/rpn.c @@ -12,6 +12,25 @@ exec cc -Wall -Wextra $@ -ledit -o $(dirname $0)/rpn $0 #include #include +static char *fmt(int radix, int64_t val) { + static char buf[65]; + if (radix == 2) { + uint64_t u = val; + int i = sizeof(buf); + do { + buf[--i] = '0' + (u & 1); + } while (u >>= 1); + return &buf[i]; + } else if (radix == 8) { + snprintf(buf, sizeof(buf), "%llo", val); + } else if (radix == 10) { + snprintf(buf, sizeof(buf), "%lld", val); + } else if (radix == 16) { + snprintf(buf, sizeof(buf), "%llx", val); + } else abort(); + return buf; +} + static struct { int64_t data[1024]; size_t len; @@ -28,7 +47,7 @@ static int64_t pop(void) { return stack.data[--stack.len]; } -static bool stack_op(char op) { +static void stack_op(char op) { int64_t a, b; switch (op) { case '$': pop(); @@ -46,11 +65,14 @@ static bool stack_op(char op) { break; case '^': a = pop(); push(pop() ^ a); break; case '<': a = pop(); push(pop() << a); break; case '>': a = pop(); push(pop() >> a); - break; case '.': a = pop(); printf("%lld\n", a); + break; case '.': a = pop(); printf("%s\n", fmt(stack.radix, a)); break; case ',': a = pop(); printf("%c\n", (char) a); - break; default: return false; + break; case 'b': stack.radix = 2; + break; case 'o': stack.radix = 8; + break; case 'd': stack.radix = 10; + break; case 'x': stack.radix = 16; + break; } - return true; } static char *prompt(EditLine *el __attribute((unused))) { @@ -59,7 +81,7 @@ static char *prompt(EditLine *el __attribute((unused))) { size_t q = 0; for (size_t i = 0; i < stack.len; ++i) { - q += (size_t) snprintf(&p[q], sizeof(p) - 3 - q, " %lld", stack.data[i]); + q += (size_t) snprintf(&p[q], sizeof(p) - 2 - q, " %s", fmt(stack.radix, stack.data[i])); } p[0] = '['; p[q] = ']'; @@ -79,13 +101,9 @@ int main(int argc __attribute((unused)), char *argv[]) { if (count < 0) err(EX_IOERR, "el_gets"); if (!line) break; - while (*line) { - char *rest; - int64_t val = strtoll(line, &rest, stack.radix); - while (*line) { if (isdigit(*line)) - push(strtoll(line, (char **) &line, 0)); // XXX: ??? + push(strtoll(line, (char **) &line, stack.radix)); // XXX: ??? else stack_op(*line++); } -- cgit 1.4.1 20ec37075a0846&follow=1'>Use $() in install.shJune McEnroe Something gave me the impression that sh did not like this, but it's specified by POSIX. 2017-08-02Create Code Tarmak 3 layoutJune McEnroe 2017-07-31Add tupJune McEnroe 2017-07-31Use designated initializer for hnel tableJune McEnroe I did not know this syntax worked! 2017-07-30Add juneJune McEnroe 2017-07-30Play nethack as ValkyrieJune McEnroe 2017-07-28Add toggle to hnelJune McEnroe 2017-07-28Install slJune McEnroe 2017-07-25Add up, supJune McEnroe 2017-07-24Autopickup ringsJune McEnroe 2017-07-24Name dogJune McEnroe 2017-07-23Add nethackrcJune McEnroe 2017-07-23Remove useless setuid in briJune McEnroe Don't you think it would be better if the setuid bit only gave you permission to do it and didn't do it for you? 2017-07-23Clean up hnel a tiny bitJune McEnroe 2017-07-21Set window size in hnelJune McEnroe 2017-07-21Add hnelJune McEnroe 2017-07-19chmod 600 in dtchJune McEnroe