core: reader: Support binary #b101.

* src/reader.c (reader_read_binary): New function.
  (reader_read_hash): Use it.
* tests/scm.test ("binary"): Test it.
This commit is contained in:
Jan Nieuwenhuizen 2018-05-16 22:32:59 +02:00
parent 10bd43d222
commit 04860cca6b
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
2 changed files with 23 additions and 0 deletions

View file

@ -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 ()
{

View file

@ -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)