picrin/extlib/benz/read.c

880 lines
19 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-18 10:08:39 -05:00
#include "picrin/object.h"
2014-08-25 00:38:09 -04:00
2016-02-19 00:50:12 -05:00
#undef EOF
#define EOF (-1)
2015-06-24 18:34:10 -04:00
KHASH_DEFINE(read, int, pic_value, kh_int_hash_func, kh_int_hash_equal)
2016-02-19 02:51:45 -05:00
static pic_value read(pic_state *pic, xFILE *file, int c);
static pic_value read_nullable(pic_state *pic, xFILE *file, int c);
2014-08-25 00:38:09 -04:00
2015-01-25 22:22:38 -05:00
PIC_NORETURN static void
2016-02-18 11:34:13 -05:00
read_error(pic_state *pic, const char *msg, pic_value irritants)
2014-08-25 00:38:09 -04:00
{
2016-02-18 11:34:13 -05:00
pic_error(pic, "read", msg, irritants);
2014-08-25 00:38:09 -04:00
}
static int
2016-02-19 02:51:45 -05:00
skip(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
while (isspace(c)) {
2016-02-19 02:51:45 -05:00
c = xfgetc(pic, file);
2014-08-25 00:38:09 -04:00
}
return c;
}
static int
2016-02-19 02:51:45 -05:00
next(pic_state *pic, xFILE *file)
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
return xfgetc(pic, file);
2014-08-25 00:38:09 -04:00
}
static int
2016-02-19 02:51:45 -05:00
peek(pic_state *pic, xFILE *file)
2014-08-25 00:38:09 -04:00
{
int c;
2016-02-19 02:51:45 -05:00
xungetc((c = xfgetc(pic, file)), file);
2014-08-25 00:38:09 -04:00
return c;
}
static bool
2016-02-19 02:51:45 -05:00
expect(pic_state *pic, xFILE *file, const char *str)
2014-08-25 00:38:09 -04:00
{
int c;
while ((c = (int)*str++) != 0) {
2016-02-19 02:51:45 -05:00
if (c != peek(pic, file))
2014-08-25 00:38:09 -04:00
return false;
2016-02-19 02:51:45 -05:00
next(pic, file);
2014-08-25 00:38:09 -04:00
}
return true;
}
static bool
isdelim(int c)
{
return c == EOF || strchr("();,|\" \t\n\r", c) != NULL; /* ignores "#", "'" */
}
static bool
strcaseeq(const char *s1, const char *s2)
{
char a, b;
while ((a = *s1++) * (b = *s2++)) {
if (tolower(a) != tolower(b))
return false;
}
return a == b;
}
2015-01-26 00:33:48 -05:00
static int
case_fold(pic_state *pic, int c)
{
2015-06-18 14:14:55 -04:00
if (pic->reader.typecase == PIC_CASE_FOLD) {
2015-01-26 00:33:48 -05:00
c = tolower(c);
}
return c;
}
2014-08-25 00:38:09 -04:00
static pic_value
2016-02-19 02:51:45 -05:00
read_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
do {
2016-02-19 02:51:45 -05:00
c = next(pic, file);
2014-08-25 00:38:09 -04:00
} while (! (c == EOF || c == '\n'));
2015-06-09 03:02:23 -04:00
return pic_invalid_value();
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_block_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int PIC_UNUSED(c))
2014-08-25 00:38:09 -04:00
{
int x, y;
int i = 1;
2016-02-19 02:51:45 -05:00
y = next(pic, file);
2014-08-25 00:38:09 -04:00
while (y != EOF && i > 0) {
x = y;
2016-02-19 02:51:45 -05:00
y = next(pic, file);
2014-08-25 00:38:09 -04:00
if (x == '|' && y == '#') {
i--;
}
if (x == '#' && y == '|') {
i++;
}
}
2015-06-09 03:02:23 -04:00
return pic_invalid_value();
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_datum_comment(pic_state *pic, xFILE *file, int PIC_UNUSED(c))
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
read(pic, file, next(pic, file));
2014-08-25 00:38:09 -04:00
2015-06-09 03:02:23 -04:00
return pic_invalid_value();
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_directive(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
switch (peek(pic, file)) {
2014-08-25 00:38:09 -04:00
case 'n':
2016-02-19 02:51:45 -05:00
if (expect(pic, file, "no-fold-case")) {
2015-06-18 14:14:55 -04:00
pic->reader.typecase = PIC_CASE_DEFAULT;
2015-06-09 03:02:23 -04:00
return pic_invalid_value();
2014-08-25 00:38:09 -04:00
}
break;
case 'f':
2016-02-19 02:51:45 -05:00
if (expect(pic, file, "fold-case")) {
2015-06-18 14:14:55 -04:00
pic->reader.typecase = PIC_CASE_FOLD;
2015-06-09 03:02:23 -04:00
return pic_invalid_value();
2014-08-25 00:38:09 -04:00
}
break;
}
2016-02-19 02:51:45 -05:00
return read_comment(pic, file, c);
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c))
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
return pic_list(pic, 2, pic_obj_value(pic->sQUOTE), read(pic, file, next(pic, file)));
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c))
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
return pic_list(pic, 2, pic_obj_value(pic->sQUASIQUOTE), read(pic, file, next(pic, file)));
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c))
2014-08-25 00:38:09 -04:00
{
pic_sym *tag = pic->sUNQUOTE;
2014-08-25 00:38:09 -04:00
2016-02-19 02:51:45 -05:00
if (peek(pic, file) == '@') {
tag = pic->sUNQUOTE_SPLICING;
2016-02-19 02:51:45 -05:00
next(pic, file);
}
2016-02-19 02:51:45 -05:00
return pic_list(pic, 2, pic_obj_value(tag), read(pic, file, next(pic, file)));
2014-08-25 00:38:09 -04:00
}
2015-06-10 02:18:03 -04:00
static pic_value
2016-02-19 02:51:45 -05:00
read_syntax_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c))
2015-06-10 02:18:03 -04:00
{
2016-02-19 02:51:45 -05:00
return pic_list(pic, 2, pic_obj_value(pic->sSYNTAX_QUOTE), read(pic, file, next(pic, file)));
2015-06-10 02:18:03 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_syntax_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c))
2015-06-10 02:18:03 -04:00
{
2016-02-19 02:51:45 -05:00
return pic_list(pic, 2, pic_obj_value(pic->sSYNTAX_QUASIQUOTE), read(pic, file, next(pic, file)));
2015-06-10 02:18:03 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_syntax_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c))
2015-06-10 02:18:03 -04:00
{
pic_sym *tag = pic->sSYNTAX_UNQUOTE;
2016-02-19 02:51:45 -05:00
if (peek(pic, file) == '@') {
2015-06-10 02:18:03 -04:00
tag = pic->sSYNTAX_UNQUOTE_SPLICING;
2016-02-19 02:51:45 -05:00
next(pic, file);
2015-06-10 02:18:03 -04:00
}
2016-02-19 02:51:45 -05:00
return pic_list(pic, 2, pic_obj_value(tag), read(pic, file, next(pic, file)));
2015-06-10 02:18:03 -04:00
}
2014-08-25 00:38:09 -04:00
static pic_value
2016-02-19 02:51:45 -05:00
read_symbol(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2015-08-26 06:04:27 -04:00
int len;
2014-08-25 00:38:09 -04:00
char *buf;
2015-01-20 02:02:28 -05:00
pic_sym *sym;
2014-08-25 00:38:09 -04:00
len = 1;
2015-05-28 03:42:16 -04:00
buf = pic_malloc(pic, len + 1);
2015-01-26 00:33:48 -05:00
buf[0] = case_fold(pic, c);
buf[1] = 0;
2014-08-25 00:38:09 -04:00
2016-02-19 02:51:45 -05:00
while (! isdelim(peek(pic, file))) {
c = next(pic, file);
2014-08-25 00:38:09 -04:00
len += 1;
buf = pic_realloc(pic, buf, len + 1);
2015-01-26 00:33:48 -05:00
buf[len - 1] = case_fold(pic, c);
buf[len] = 0;
2014-08-25 00:38:09 -04:00
}
2016-02-07 14:10:22 -05:00
sym = pic_intern_cstr(pic, buf);
2014-08-25 00:38:09 -04:00
pic_free(pic, buf);
2015-01-18 21:08:27 -05:00
return pic_obj_value(sym);
2014-08-25 00:38:09 -04:00
}
2015-05-27 10:40:01 -04:00
static unsigned
2016-02-19 02:51:45 -05:00
read_uinteger(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2015-05-27 10:40:01 -04:00
unsigned u = 0;
2014-08-25 00:38:09 -04:00
if (! isdigit(c)) {
2016-02-18 12:29:40 -05:00
read_error(pic, "expected one or more digits", pic_list(pic, 1, pic_char_value(pic, c)));
2014-08-25 00:38:09 -04:00
}
2015-05-27 10:40:01 -04:00
u = c - '0';
2016-02-19 02:51:45 -05:00
while (isdigit(c = peek(pic, file))) {
u = u * 10 + next(pic, file) - '0';
2014-08-25 00:38:09 -04:00
}
2015-05-27 10:40:01 -04:00
return u;
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_unsigned(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
#define ATOF_BUF_SIZE (64)
char buf[ATOF_BUF_SIZE];
double flt;
int idx = 0; /* index into buffer */
int dpe = 0; /* the number of '.' or 'e' characters seen */
2014-08-25 00:38:09 -04:00
if (! isdigit(c)) {
2016-02-18 12:29:40 -05:00
read_error(pic, "expected one or more digits", pic_list(pic, 1, pic_char_value(pic, c)));
2015-07-04 04:32:16 -04:00
}
buf[idx++] = (char )c;
2016-02-19 02:51:45 -05:00
while (isdigit(c = peek(pic, file)) && idx < ATOF_BUF_SIZE) {
buf[idx++] = (char )next(pic, file);
}
2016-02-19 02:51:45 -05:00
if ('.' == peek(pic, file) && idx < ATOF_BUF_SIZE) {
dpe++;
2016-02-19 02:51:45 -05:00
buf[idx++] = (char )next(pic, file);
while (isdigit(c = peek(pic, file)) && idx < ATOF_BUF_SIZE) {
buf[idx++] = (char )next(pic, file);
2015-05-27 10:40:01 -04:00
}
}
2016-02-19 02:51:45 -05:00
c = peek(pic, file);
2015-05-27 10:40:01 -04:00
if ((c == 'e' || c == 'E') && idx < (ATOF_BUF_SIZE - 2)) {
dpe++;
2016-02-19 02:51:45 -05:00
buf[idx++] = (char )next(pic, file);
switch ((c = peek(pic, file))) {
case '-':
case '+':
2016-02-19 02:51:45 -05:00
buf[idx++] = (char )next(pic, file);
break;
default:
break;
2015-05-27 10:40:01 -04:00
}
2016-02-19 02:51:45 -05:00
if (! isdigit(peek(pic, file))) {
2016-02-18 12:29:40 -05:00
read_error(pic, "expected one or more digits", pic_list(pic, 1, pic_char_value(pic, c)));
2015-05-27 10:40:01 -04:00
}
2016-02-19 02:51:45 -05:00
while (isdigit(c = peek(pic, file)) && idx < ATOF_BUF_SIZE) {
buf[idx++] = (char )next(pic, file);
2015-05-27 10:40:01 -04:00
}
2014-08-25 00:38:09 -04:00
}
if (idx >= ATOF_BUF_SIZE)
read_error(pic, "number too large",
pic_obj_value(pic_str_value(pic, (const char *)buf, ATOF_BUF_SIZE)));
if (! isdelim(c))
2016-02-18 12:29:40 -05:00
read_error(pic, "non-delimiter character given after number", pic_list(pic, 1, pic_char_value(pic, c)));
buf[idx] = 0;
flt = PIC_CSTRING_TO_DOUBLE(buf);
if (dpe == 0 && pic_valid_int(flt))
return pic_int_value(pic, (int )flt);
return pic_float_value(pic, flt);
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_number(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
return read_unsigned(pic, file, c);
2014-08-25 00:38:09 -04:00
}
static pic_value
negate(pic_state *pic, pic_value n)
2014-08-25 00:38:09 -04:00
{
if (pic_int_p(pic, n) && (INT_MIN != pic_int(pic, n))) {
return pic_int_value(pic, -pic_int(pic, n));
2014-08-25 00:38:09 -04:00
} else {
return pic_float_value(pic, -pic_float(pic, n));
2014-08-25 00:38:09 -04:00
}
}
static pic_value
2016-02-19 02:51:45 -05:00
read_minus(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
pic_value sym;
2016-02-19 02:51:45 -05:00
if (isdigit(peek(pic, file))) {
return negate(pic, read_unsigned(pic, file, next(pic, file)));
2014-08-25 00:38:09 -04:00
}
else {
2016-02-19 02:51:45 -05:00
sym = read_symbol(pic, file, c);
if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "-inf.0")) {
return pic_float_value(pic, -(1.0 / 0.0));
2014-08-25 00:38:09 -04:00
}
if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "-nan.0")) {
return pic_float_value(pic, -(0.0 / 0.0));
2014-08-25 00:38:09 -04:00
}
return sym;
}
}
static pic_value
2016-02-19 02:51:45 -05:00
read_plus(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
pic_value sym;
2016-02-19 02:51:45 -05:00
if (isdigit(peek(pic, file))) {
return read_unsigned(pic, file, next(pic, file));
2014-08-25 00:38:09 -04:00
}
else {
2016-02-19 02:51:45 -05:00
sym = read_symbol(pic, file, c);
if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "+inf.0")) {
return pic_float_value(pic, 1.0 / 0.0);
2014-08-25 00:38:09 -04:00
}
if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "+nan.0")) {
return pic_float_value(pic, 0.0 / 0.0);
2014-08-25 00:38:09 -04:00
}
return sym;
}
}
static pic_value
2016-02-19 02:51:45 -05:00
read_true(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
if ((c = peek(pic, file)) == 'r') {
if (! expect(pic, file, "rue")) {
read_error(pic, "unexpected character while reading #true", pic_nil_value(pic));
}
} else if (! isdelim(c)) {
2016-02-18 12:29:40 -05:00
read_error(pic, "non-delimiter character given after #t", pic_list(pic, 1, pic_char_value(pic, c)));
}
2014-08-25 00:38:09 -04:00
return pic_true_value(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_false(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
if ((c = peek(pic, file)) == 'a') {
if (! expect(pic, file, "alse")) {
read_error(pic, "unexpected character while reading #false", pic_nil_value(pic));
}
} else if (! isdelim(c)) {
2016-02-18 12:29:40 -05:00
read_error(pic, "non-delimiter character given after #f", pic_list(pic, 1, pic_char_value(pic, c)));
}
2014-08-25 00:38:09 -04:00
return pic_false_value(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_char(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
c = next(pic, file);
2014-08-25 00:38:09 -04:00
2016-02-19 02:51:45 -05:00
if (! isdelim(peek(pic, file))) {
2014-08-25 00:38:09 -04:00
switch (c) {
2016-02-18 12:29:40 -05:00
default: read_error(pic, "unexpected character after char literal", pic_list(pic, 1, pic_char_value(pic, c)));
2016-02-19 02:51:45 -05:00
case 'a': c = '\a'; if (! expect(pic, file, "larm")) goto fail; break;
case 'b': c = '\b'; if (! expect(pic, file, "ackspace")) goto fail; break;
case 'd': c = 0x7F; if (! expect(pic, file, "elete")) goto fail; break;
case 'e': c = 0x1B; if (! expect(pic, file, "scape")) goto fail; break;
2014-08-25 00:38:09 -04:00
case 'n':
2016-02-19 02:51:45 -05:00
if ((c = peek(pic, file)) == 'e') {
2014-08-25 00:38:09 -04:00
c = '\n';
2016-02-19 02:51:45 -05:00
if (! expect(pic, file, "ewline"))
2014-08-25 00:38:09 -04:00
goto fail;
} else {
c = '\0';
2016-02-19 02:51:45 -05:00
if (! expect(pic, file, "ull"))
2014-08-25 00:38:09 -04:00
goto fail;
}
break;
2016-02-19 02:51:45 -05:00
case 'r': c = '\r'; if (! expect(pic, file, "eturn")) goto fail; break;
case 's': c = ' '; if (! expect(pic, file, "pace")) goto fail; break;
case 't': c = '\t'; if (! expect(pic, file, "ab")) goto fail; break;
2014-08-25 00:38:09 -04:00
}
}
return pic_char_value(pic, (char)c);
2014-08-25 00:38:09 -04:00
fail:
2016-02-18 12:29:40 -05:00
read_error(pic, "unexpected character while reading character literal", pic_list(pic, 1, pic_char_value(pic, c)));
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_string(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
char *buf;
2015-08-26 06:04:27 -04:00
int size, cnt;
struct pic_string *str;
2014-08-25 00:38:09 -04:00
size = 256;
2015-05-28 03:42:16 -04:00
buf = pic_malloc(pic, size);
2014-08-25 00:38:09 -04:00
cnt = 0;
/* TODO: intraline whitespaces */
2016-02-19 02:51:45 -05:00
while ((c = next(pic, file)) != '"') {
2014-08-25 00:38:09 -04:00
if (c == '\\') {
2016-02-19 02:51:45 -05:00
switch (c = next(pic, file)) {
2014-08-25 00:38:09 -04:00
case 'a': c = '\a'; break;
case 'b': c = '\b'; break;
case 't': c = '\t'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
}
}
2014-09-27 07:43:31 -04:00
buf[cnt++] = (char)c;
2014-08-25 00:38:09 -04:00
if (cnt >= size) {
buf = pic_realloc(pic, buf, size *= 2);
}
}
buf[cnt] = '\0';
str = pic_str_value(pic, buf, cnt);
2014-08-25 00:38:09 -04:00
pic_free(pic, buf);
return pic_obj_value(str);
}
static pic_value
2016-02-19 02:51:45 -05:00
read_pipe(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
char *buf;
2015-08-26 06:04:27 -04:00
int size, cnt;
2015-01-20 02:02:28 -05:00
pic_sym *sym;
2014-08-25 00:38:09 -04:00
/* Currently supports only ascii chars */
char HEX_BUF[3];
size_t i = 0;
size = 256;
2015-05-28 03:42:16 -04:00
buf = pic_malloc(pic, size);
2014-08-25 00:38:09 -04:00
cnt = 0;
2016-02-19 02:51:45 -05:00
while ((c = next(pic, file)) != '|') {
2014-08-25 00:38:09 -04:00
if (c == '\\') {
2016-02-19 02:51:45 -05:00
switch ((c = next(pic, file))) {
2014-08-25 00:38:09 -04:00
case 'a': c = '\a'; break;
case 'b': c = '\b'; break;
case 't': c = '\t'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 'x':
i = 0;
2016-02-19 02:51:45 -05:00
while ((HEX_BUF[i++] = (char)next(pic, file)) != ';') {
2014-08-25 00:38:09 -04:00
if (i >= sizeof HEX_BUF)
2016-02-18 12:29:40 -05:00
read_error(pic, "expected ';'", pic_list(pic, 1, pic_char_value(pic, HEX_BUF[sizeof(HEX_BUF) - 1])));
2014-08-25 00:38:09 -04:00
}
2014-09-27 07:43:31 -04:00
c = (char)strtol(HEX_BUF, NULL, 16);
2014-08-25 00:38:09 -04:00
break;
}
}
2014-09-27 07:43:31 -04:00
buf[cnt++] = (char)c;
2014-08-25 00:38:09 -04:00
if (cnt >= size) {
buf = pic_realloc(pic, buf, size *= 2);
}
}
buf[cnt] = '\0';
2016-02-07 14:10:22 -05:00
sym = pic_intern_cstr(pic, buf);
2014-08-25 00:38:09 -04:00
pic_free(pic, buf);
2015-01-18 21:08:27 -05:00
return pic_obj_value(sym);
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_blob(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
int nbits, n;
2016-02-18 09:59:33 -05:00
int len;
2014-09-27 07:43:31 -04:00
unsigned char *dat;
struct pic_blob *blob;
2014-08-25 00:38:09 -04:00
nbits = 0;
2016-02-19 02:51:45 -05:00
while (isdigit(c = next(pic, file))) {
2014-08-25 00:38:09 -04:00
nbits = 10 * nbits + c - '0';
}
if (nbits != 8) {
2016-02-18 12:29:40 -05:00
read_error(pic, "unsupported bytevector bit width", pic_list(pic, 1, pic_int_value(pic, nbits)));
2014-08-25 00:38:09 -04:00
}
if (c != '(') {
2016-02-18 12:29:40 -05:00
read_error(pic, "expected '(' character", pic_list(pic, 1, pic_char_value(pic, c)));
2014-08-25 00:38:09 -04:00
}
len = 0;
dat = NULL;
2016-02-19 02:51:45 -05:00
c = next(pic, file);
while ((c = skip(pic, file, c)) != ')') {
n = read_uinteger(pic, file, c);
2014-08-25 00:38:09 -04:00
if (n < 0 || (1 << nbits) <= n) {
2016-02-18 12:29:40 -05:00
read_error(pic, "invalid element in bytevector literal", pic_list(pic, 1, pic_int_value(pic, n)));
2014-08-25 00:38:09 -04:00
}
len += 1;
dat = pic_realloc(pic, dat, len);
2014-09-27 07:43:31 -04:00
dat[len - 1] = (unsigned char)n;
2016-02-19 02:51:45 -05:00
c = next(pic, file);
2014-08-25 00:38:09 -04:00
}
2016-02-18 09:59:33 -05:00
blob = pic_blob_value(pic, dat, len);
2014-08-25 00:38:09 -04:00
pic_free(pic, dat);
return pic_obj_value(blob);
}
2015-06-09 03:19:57 -04:00
static pic_value
2016-02-19 02:51:45 -05:00
read_undef_or_blob(pic_state *pic, xFILE *file, int c)
2015-06-09 03:19:57 -04:00
{
2016-02-19 02:51:45 -05:00
if ((c = peek(pic, file)) == 'n') {
if (! expect(pic, file, "ndefined")) {
read_error(pic, "unexpected character while reading #undefined", pic_nil_value(pic));
2015-06-09 03:19:57 -04:00
}
return pic_undef_value(pic);
2015-06-09 03:19:57 -04:00
}
if (! isdigit(c)) {
2016-02-18 12:29:40 -05:00
read_error(pic, "expect #undefined or #u8(...), but illegal character given", pic_list(pic, 1, pic_char_value(pic, c)));
2015-06-09 03:19:57 -04:00
}
2016-02-19 02:51:45 -05:00
return read_blob(pic, file, 'u');
2015-06-09 03:19:57 -04:00
}
2014-08-25 00:38:09 -04:00
static pic_value
2016-02-19 02:51:45 -05:00
read_pair(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2015-01-25 22:29:29 -05:00
static const int tCLOSE = ')';
2014-08-25 00:38:09 -04:00
pic_value car, cdr;
retry:
2016-02-19 02:51:45 -05:00
c = skip(pic, file, ' ');
2014-08-25 00:38:09 -04:00
if (c == tCLOSE) {
return pic_nil_value(pic);
2014-08-25 00:38:09 -04:00
}
2016-02-19 02:51:45 -05:00
if (c == '.' && isdelim(peek(pic, file))) {
cdr = read(pic, file, next(pic, file));
2014-08-25 00:38:09 -04:00
closing:
2016-02-19 02:51:45 -05:00
if ((c = skip(pic, file, ' ')) != tCLOSE) {
if (pic_invalid_p(pic, read_nullable(pic, file, c))) {
2014-08-25 00:38:09 -04:00
goto closing;
}
read_error(pic, "unmatched parenthesis", pic_nil_value(pic));
2014-08-25 00:38:09 -04:00
}
return cdr;
}
else {
2016-02-19 02:51:45 -05:00
car = read_nullable(pic, file, c);
2014-08-25 00:38:09 -04:00
if (pic_invalid_p(pic, car)) {
2014-08-25 00:38:09 -04:00
goto retry;
}
2016-02-19 02:51:45 -05:00
cdr = read_pair(pic, file, '(');
2014-08-25 00:38:09 -04:00
return pic_cons(pic, car, cdr);
}
}
static pic_value
2016-02-19 02:51:45 -05:00
read_vector(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2015-07-12 19:28:21 -04:00
pic_value list, it, elem;
pic_vec *vec;
2015-08-26 06:04:27 -04:00
int i = 0;
2014-08-25 00:38:09 -04:00
2016-02-19 02:51:45 -05:00
list = read(pic, file, c);
2014-08-25 00:38:09 -04:00
2015-07-12 19:28:21 -04:00
vec = pic_make_vec(pic, pic_length(pic, list));
pic_for_each (elem, list, it) {
vec->data[i++] = elem;
}
return pic_obj_value(vec);
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_label_set(pic_state *pic, xFILE *file, int i)
2014-08-25 00:38:09 -04:00
{
2015-06-24 18:34:10 -04:00
khash_t(read) *h = &pic->reader.labels;
2014-08-25 00:38:09 -04:00
pic_value val;
2015-06-24 18:34:10 -04:00
int c, ret;
khiter_t it;
it = kh_put(read, h, i, &ret);
2014-08-25 00:38:09 -04:00
2016-02-19 02:51:45 -05:00
switch ((c = skip(pic, file, ' '))) {
2015-01-25 22:29:29 -05:00
case '(':
2014-08-25 00:38:09 -04:00
{
pic_value tmp;
kh_val(h, it) = val = pic_cons(pic, pic_undef_value(pic), pic_undef_value(pic));
2014-08-25 00:38:09 -04:00
2016-02-19 02:51:45 -05:00
tmp = read(pic, file, c);
2014-08-25 00:38:09 -04:00
pic_pair_ptr(val)->car = pic_car(pic, tmp);
pic_pair_ptr(val)->cdr = pic_cdr(pic, tmp);
return val;
}
case '#':
{
bool vect;
2016-02-19 02:51:45 -05:00
if (peek(pic, file) == '(') {
2014-08-25 00:38:09 -04:00
vect = true;
} else {
vect = false;
}
if (vect) {
pic_vec *tmp;
2015-06-24 18:34:10 -04:00
kh_val(h, it) = val = pic_obj_value(pic_make_vec(pic, 0));
2014-08-25 00:38:09 -04:00
2016-02-19 02:51:45 -05:00
tmp = pic_vec_ptr(read(pic, file, c));
2015-01-07 16:11:48 -05:00
PIC_SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data);
2015-08-26 06:04:27 -04:00
PIC_SWAP(int, tmp->len, pic_vec_ptr(val)->len);
2014-08-25 00:38:09 -04:00
return val;
}
2015-01-07 16:11:48 -05:00
PIC_FALLTHROUGH;
2014-08-25 00:38:09 -04:00
}
default:
{
2016-02-19 02:51:45 -05:00
kh_val(h, it) = val = read(pic, file, c);
2014-08-25 00:38:09 -04:00
return val;
}
}
}
static pic_value
2016-02-19 02:51:45 -05:00
read_label_ref(pic_state *pic, xFILE PIC_UNUSED(*file), int i)
2014-08-25 00:38:09 -04:00
{
2015-06-24 18:34:10 -04:00
khash_t(read) *h = &pic->reader.labels;
khiter_t it;
2014-08-25 00:38:09 -04:00
2015-06-24 18:34:10 -04:00
it = kh_get(read, h, i);
if (it == kh_end(h)) {
2016-02-18 12:29:40 -05:00
read_error(pic, "label of given index not defined", pic_list(pic, 1, pic_int_value(pic, i)));
2014-08-25 00:38:09 -04:00
}
2015-06-24 18:34:10 -04:00
return kh_val(h, it);
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_label(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
int i;
2014-08-25 00:38:09 -04:00
i = 0;
do {
i = i * 10 + c - '0';
2016-02-19 02:51:45 -05:00
} while (isdigit(c = next(pic, file)));
2014-08-25 00:38:09 -04:00
if (c == '=') {
2016-02-19 02:51:45 -05:00
return read_label_set(pic, file, i);
2014-08-25 00:38:09 -04:00
}
if (c == '#') {
2016-02-19 02:51:45 -05:00
return read_label_ref(pic, file, i);
2014-08-25 00:38:09 -04:00
}
read_error(pic, "broken label expression", pic_nil_value(pic));
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_unmatch(pic_state *pic, xFILE PIC_UNUSED(*file), int PIC_UNUSED(c))
2014-08-25 00:38:09 -04:00
{
read_error(pic, "unmatched parenthesis", pic_nil_value(pic));
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read_dispatch(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
2016-02-19 02:51:45 -05:00
c = next(pic, file);
2014-08-25 00:38:09 -04:00
if (c == EOF) {
read_error(pic, "unexpected EOF", pic_nil_value(pic));
2014-08-25 00:38:09 -04:00
}
2015-06-18 14:14:55 -04:00
if (pic->reader.dispatch[c] == NULL) {
2016-02-18 12:29:40 -05:00
read_error(pic, "invalid character at the seeker head", pic_list(pic, 1, pic_char_value(pic, c)));
2014-08-25 00:38:09 -04:00
}
2016-02-19 02:51:45 -05:00
return pic->reader.dispatch[c](pic, file, c);
}
2014-08-25 00:38:09 -04:00
static pic_value
2016-02-19 02:51:45 -05:00
read_nullable(pic_state *pic, xFILE *file, int c)
{
2016-02-19 02:51:45 -05:00
c = skip(pic, file, c);
2014-08-25 00:38:09 -04:00
if (c == EOF) {
read_error(pic, "unexpected EOF", pic_nil_value(pic));
2014-08-25 00:38:09 -04:00
}
2015-06-18 14:14:55 -04:00
if (pic->reader.table[c] == NULL) {
2016-02-18 12:29:40 -05:00
read_error(pic, "invalid character at the seeker head", pic_list(pic, 1, pic_char_value(pic, c)));
2014-08-25 00:38:09 -04:00
}
2016-02-19 02:51:45 -05:00
return pic->reader.table[c](pic, file, c);
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-19 02:51:45 -05:00
read(pic_state *pic, xFILE *file, int c)
2014-08-25 00:38:09 -04:00
{
pic_value val;
retry:
2016-02-19 02:51:45 -05:00
val = read_nullable(pic, file, c);
2014-08-25 00:38:09 -04:00
if (pic_invalid_p(pic, val)) {
2016-02-19 02:51:45 -05:00
c = next(pic, file);
2014-08-25 00:38:09 -04:00
goto retry;
}
return val;
}
static void
2015-06-18 14:14:55 -04:00
reader_table_init(pic_reader *reader)
2014-08-25 00:38:09 -04:00
{
int c;
reader->table[0] = NULL;
2014-08-25 00:38:09 -04:00
/* default reader */
for (c = 1; c < 256; ++c) {
reader->table[c] = read_symbol;
}
reader->table[')'] = read_unmatch;
reader->table[';'] = read_comment;
reader->table['\''] = read_quote;
reader->table['`'] = read_quasiquote;
reader->table[','] = read_unquote;
reader->table['"'] = read_string;
reader->table['|'] = read_pipe;
reader->table['+'] = read_plus;
reader->table['-'] = read_minus;
reader->table['('] = read_pair;
reader->table['#'] = read_dispatch;
2014-08-25 00:38:09 -04:00
/* read number */
for (c = '0'; c <= '9'; ++c) {
reader->table[c] = read_number;
2014-08-25 00:38:09 -04:00
}
reader->dispatch['!'] = read_directive;
reader->dispatch['|'] = read_block_comment;
reader->dispatch[';'] = read_datum_comment;
reader->dispatch['t'] = read_true;
reader->dispatch['f'] = read_false;
2015-06-10 02:18:03 -04:00
reader->dispatch['\''] = read_syntax_quote;
reader->dispatch['`'] = read_syntax_quasiquote;
reader->dispatch[','] = read_syntax_unquote;
reader->dispatch['\\'] = read_char;
reader->dispatch['('] = read_vector;
2015-06-09 03:19:57 -04:00
reader->dispatch['u'] = read_undef_or_blob;
/* read labels */
for (c = '0'; c <= '9'; ++c) {
reader->dispatch[c] = read_label;
2014-08-25 00:38:09 -04:00
}
}
2015-06-18 14:14:55 -04:00
void
pic_reader_init(pic_state *pic)
{
int c;
2015-06-18 14:14:55 -04:00
pic->reader.typecase = PIC_CASE_DEFAULT;
2015-06-24 18:34:10 -04:00
kh_init(read, &pic->reader.labels);
for (c = 0; c < 256; ++c) {
2015-06-18 14:14:55 -04:00
pic->reader.table[c] = NULL;
}
for (c = 0; c < 256; ++c) {
2015-06-18 14:14:55 -04:00
pic->reader.dispatch[c] = NULL;
}
2015-06-18 14:14:55 -04:00
reader_table_init(&pic->reader);
}
void
2015-06-18 14:14:55 -04:00
pic_reader_destroy(pic_state *pic)
{
2015-06-24 18:34:10 -04:00
kh_destroy(read, &pic->reader.labels);
}
2014-08-25 00:38:09 -04:00
pic_value
pic_read(pic_state *pic, struct pic_port *port)
{
2016-02-19 02:17:13 -05:00
size_t ai = pic_enter(pic);
2014-08-25 00:38:09 -04:00
pic_value val;
2016-02-19 02:51:45 -05:00
xFILE *file = port->file;
2015-06-28 13:04:55 -04:00
int c;
2014-08-25 00:38:09 -04:00
2016-02-19 02:51:45 -05:00
while ((c = skip(pic, file, next(pic, file))) != EOF) {
val = read_nullable(pic, file, c);
2014-08-25 00:38:09 -04:00
if (! pic_invalid_p(pic, val)) {
2015-06-28 13:04:55 -04:00
break;
}
2016-02-19 02:17:13 -05:00
pic_leave(pic, ai);
2015-06-28 13:04:55 -04:00
}
2014-08-25 00:38:09 -04:00
if (c == EOF) {
return pic_eof_object(pic);
2014-08-25 00:38:09 -04:00
}
2016-02-19 02:17:13 -05:00
pic_leave(pic, ai);
return pic_protect(pic, val);
2014-08-25 00:38:09 -04:00
}
pic_value
pic_read_cstr(pic_state *pic, const char *str)
{
2016-02-18 15:54:50 -05:00
struct pic_port *port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
2015-05-28 10:28:55 -04:00
pic_value form;
2014-08-25 00:38:09 -04:00
2015-06-26 10:45:56 -04:00
pic_try {
form = pic_read(pic, port);
}
pic_catch {
pic_close_port(pic, port);
pic_raise(pic, pic->err);
}
2014-08-25 00:38:09 -04:00
2015-05-28 10:28:55 -04:00
pic_close_port(pic, port);
return form;
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_read_read(pic_state *pic)
{
struct pic_port *port = pic_stdin(pic);
pic_get_args(pic, "|p", &port);
return pic_read(pic, port);
}
void
pic_init_read(pic_state *pic)
{
2014-08-31 22:37:52 -04:00
pic_defun(pic, "read", pic_read_read);
2014-08-25 00:38:09 -04:00
}