From f7a2b491208ad805dddc27c54123d40d16ff7eda Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Mon, 13 May 2024 12:32:44 -0700 Subject: [PATCH] Added hex output --- examples/binary.modal | 5 +++++ makefile | 2 +- src/modal.c | 33 +++++++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 examples/binary.modal diff --git a/examples/binary.modal b/examples/binary.modal new file mode 100644 index 0000000..9a98afc --- /dev/null +++ b/examples/binary.modal @@ -0,0 +1,5 @@ +?(?: ?:) \#48 +?(?: ?:) \101 +?(?: ?:) \#6c +?(?: ?:) \108 +?(?: ?:) \#6f \ No newline at end of file diff --git a/makefile b/makefile index afebb2f..a7e9458 100644 --- a/makefile +++ b/makefile @@ -10,7 +10,7 @@ dest: run: all bin/modal @ bin/modal -q examples/hello.modal debug: all bin/modal-debug - @ bin/modal-debug -a examples/unicode.modal + @ bin/modal-debug -a examples/binary.modal test: all bin/modal-debug bin/modal @ bin/modal -v @ bin/modal-debug -q examples/fizzbuzz.modal diff --git a/src/modal.c b/src/modal.c index 91f31c3..e74ec10 100644 --- a/src/modal.c +++ b/src/modal.c @@ -21,14 +21,14 @@ copy(char *src, char *dst, int length) return dst; } -static int -sint(char *s) +int +chex(char c) { - char c; - int r = 0, n = 1; - if(*s == '-') { n = -1, s++; } - while((c = *s++) && !spacer(c)) r = r * 10 + c - '0'; - return r * n; + c -= 0x30; + if(c < 10) return c; + c -= 0x31; + if(c < 6) return 10 + c; + return 0; } static char * @@ -47,6 +47,21 @@ walk(char *s) return s; } +static int +sint(char *s) +{ + char c = *s, *cap = walk(s); + int r = 0, n = 1; + if(c == '#') { + s++; + while((c = *s) && s++ < cap) r = (r << 4) | chex(c); + return r; + } + if(c == '-') { n = -1, s++; } + while((c = *s) && s++ < cap) r = r * 10 + c - '0'; + return r * n; +} + static void device_write(char *s) { @@ -83,7 +98,9 @@ device_write(char *s) case 't': putc(0x09, stdout); break; case 'n': putc(0x0a, stdout); break; case 's': putc(0x20, stdout); break; + default: putc(sint(--s), stdout), s = walk(s); } + } else putc(c, stdout); } @@ -314,7 +331,7 @@ main(int argc, char **argv) return !printf("usage: modal [-vqn] source.modal\n"); for(i = 1; i < argc && *argv[i] == '-'; i++) { switch(argv[i][1]) { - case 'v': /* version */ return !printf("Modal Interpreter, 10 May 2024.\n"); + case 'v': /* version */ return !printf("Modal Interpreter, 13 May 2024.\n"); case 'q': /* quiet */ quiet = 1; break; case 'p': /* debug */ debug = 1; break; case 'a': /* access */ access = 1; break;