Support negative fixnums in input, and perform some extra validation.

This commit is contained in:
Jesse D. McDonald 2009-11-13 01:34:55 -06:00
parent 449b0cf478
commit 439491a80e
1 changed files with 13 additions and 0 deletions

View File

@ -74,6 +74,7 @@ static value_t read_one_value(reader_state_t *state)
return read_special(state); return read_special(state);
case '(': case '(':
return read_list(state); return read_list(state);
case '-':
case '0' ... '9': case '0' ... '9':
return read_number(state); return read_number(state);
case '\"': case '\"':
@ -218,6 +219,14 @@ static value_t read_list(reader_state_t *state)
static value_t read_number(reader_state_t *state) static value_t read_number(reader_state_t *state)
{ {
fixnum_t num = 0; fixnum_t num = 0;
bool negative = false;
if (state->ch == '-')
{
negative = true;
next_char(state);
release_assert(isdigit(state->ch));
}
while (isdigit(state->ch)) while (isdigit(state->ch))
{ {
@ -227,6 +236,10 @@ static value_t read_number(reader_state_t *state)
next_char(state); next_char(state);
} }
if (negative)
num = -num;
release_assert((FIXNUM_MIN <= num) && (num <= FIXNUM_MAX));
return fixnum_value(num); return fixnum_value(num);
} }