reader: factor out read_negative
This commit is contained in:
41
src/reader.c
41
src/reader.c
@@ -95,6 +95,28 @@ read_err_t read_int(sys_t *sys, stream_t *stream, lisp_t **ret)
|
||||
return READ_ERR_OK;
|
||||
}
|
||||
|
||||
read_err_t read_negative(sys_t *sys, stream_t *stream, lisp_t **ret)
|
||||
{
|
||||
char c = stream_next(stream);
|
||||
if (isdigit(c))
|
||||
{
|
||||
read_err_t err = read_int(sys, stream, ret);
|
||||
if (err)
|
||||
return err;
|
||||
i64 n = as_int(*ret);
|
||||
n *= -1;
|
||||
*ret = make_int(n);
|
||||
return READ_ERR_OK;
|
||||
}
|
||||
else if (is_sym(c) || isspace(c))
|
||||
{
|
||||
stream_seek_backward(stream, 1);
|
||||
return read_sym(sys, stream, ret);
|
||||
}
|
||||
else
|
||||
return READ_ERR_UNKNOWN_CHAR;
|
||||
}
|
||||
|
||||
read_err_t read_list(sys_t *sys, stream_t *stream, lisp_t **ret)
|
||||
{
|
||||
// skip past the open parentheses '('
|
||||
@@ -172,24 +194,7 @@ read_err_t read(sys_t *sys, stream_t *stream, lisp_t **ret)
|
||||
if (isdigit(c))
|
||||
return read_int(sys, stream, ret);
|
||||
else if (c == '-')
|
||||
{
|
||||
char c1 = stream_next(stream);
|
||||
if (isdigit(c1))
|
||||
{
|
||||
read_err_t err = read_int(sys, stream, ret);
|
||||
if (err)
|
||||
return err;
|
||||
i64 n = as_int(*ret);
|
||||
n *= -1;
|
||||
*ret = make_int(n);
|
||||
return READ_ERR_OK;
|
||||
}
|
||||
else if (is_sym(c1))
|
||||
{
|
||||
stream_seek_backward(stream, 1);
|
||||
return read_sym(sys, stream, ret);
|
||||
}
|
||||
}
|
||||
return read_negative(sys, stream, ret);
|
||||
else if (is_sym(c))
|
||||
return read_sym(sys, stream, ret);
|
||||
else if (c == '\'')
|
||||
|
||||
Reference in New Issue
Block a user