From 04860cca6b0ba7200f8d53c0d46c089ea1dd889a Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Wed, 16 May 2018 22:32:59 +0200 Subject: [PATCH] core: reader: Support binary #b101. * src/reader.c (reader_read_binary): New function. (reader_read_hash): Use it. * tests/scm.test ("binary"): Test it. --- src/reader.c | 19 +++++++++++++++++++ tests/scm.test | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/src/reader.c b/src/reader.c index 098f6370..465dfb95 100644 --- a/src/reader.c +++ b/src/reader.c @@ -224,6 +224,8 @@ reader_read_hash (int c, SCM a) cons (reader_read_sexp_ (readchar (), a), cell_nil)); case ':': return MAKE_KEYWORD (CAR (reader_read_sexp_ (readchar (), a))); + case 'b': + return reader_read_binary (); case 'o': return reader_read_octal (); case 'x': @@ -319,6 +321,23 @@ reader_read_character () return MAKE_CHAR (c); } +SCM +reader_read_binary () +{ + int n = 0; + int c = peekchar (); + int s = 1; + if (c == '-') {s = -1; readchar (); c = peekchar ();} + while (c == '0' || c == '1') + { + n <<= 1; + n+= c - '0'; + readchar (); + c = peekchar (); + } + return MAKE_NUMBER (s*n); +} + SCM reader_read_octal () { diff --git a/tests/scm.test b/tests/scm.test index d83f5a3d..7254d184 100755 --- a/tests/scm.test +++ b/tests/scm.test @@ -155,4 +155,8 @@ exit $? (pass-if "make-vector 2" (sequal? (make-vector 3 1) #(1 1 1))) +(pass-if-equal "binary" 5 #b101) +(pass-if-equal "octal" 65 #o101) +(pass-if-equal "hex" 257 #x101) + (result 'report)