X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=src%2Fread.c;h=0f6a63d840108567ed2180c37dbe70afca1b5df0;hb=e3dcc1d3966fb95a5a232daa193c6b5d89a06b7e;hp=68efbf3eef5c2c5434c4550a23f692f464508976;hpb=c25d7ca5386582feba3d02f69a7425ea5b868bc7;p=muddle-interpreter.git diff --git a/src/read.c b/src/read.c index 68efbf3..0f6a63d 100644 --- a/src/read.c +++ b/src/read.c @@ -1,5 +1,5 @@ /* -Copyright (C) 2017 Keziah Wesley +Copyright (C) 2017-2018 Keziah Wesley You can redistribute and/or modify this file under the terms of the GNU Affero General Public License as published by the Free Software @@ -18,6 +18,7 @@ License along with this file. If not, see #include "read.h" #include "object.h" +#include "oblist.h" #include #include @@ -114,7 +115,7 @@ static uint32_t obj_get_fix32(const object *o) { */ static int -read_num (const char *p, reader_stack *st) +read_num (const char *p, reader_stack * st) { int i = 0; // Use an unsigned intermediate to simplify overflow checks. @@ -151,7 +152,7 @@ read_num (const char *p, reader_stack *st) if (p[0] != '-') { if (x <= INT32_MAX) - (--(st->pos))->fix32 = new_fix32 ((int32_t)x); + (--(st->pos))->fix32 = new_fix32 ((int32_t) x); else if (x <= INT64_MAX) (--(st->pos))->fix64 = new_fix64 (x); else @@ -159,24 +160,24 @@ read_num (const char *p, reader_stack *st) } else { - if (-x >= (uint64_t)INT32_MIN) - (--(st->pos))->fix32 = new_fix32 (0 - (int32_t)x); - else if (-x >= (uint64_t)INT64_MIN) - (--(st->pos))->fix64 = new_fix64 (0 - (int64_t)x); + if (-x >= (uint64_t) INT32_MIN) + (--(st->pos))->fix32 = new_fix32 (0 - (int32_t) x); + else if (-x >= (uint64_t) INT64_MIN) + (--(st->pos))->fix64 = new_fix64 (0 - (int64_t) x); else goto read_float; } st->framelen++; return i; - read_float: - assert(0 && "unimplemented: promote num to float"); +read_float: + assert (0 && "unimplemented: promote num to float"); return i; } // stack[0..len]: objs in current list // stack[len]: parent len const char * -read_token (const char *p, reader_stack *st) +read_token (const char *p, reader_stack * st) { p += count_whitespace (p); switch (p[0]) @@ -206,7 +207,10 @@ read_token (const char *p, reader_stack *st) } *--(st->pos) = (object) { - .fix32.type = type,.fix32.rest = 0,.fix32.val = st->framelen,}; + .fix32.type = type,.fix32.rest = 0,.fix32.val = (fix32_val) + { + .n = st->framelen} + ,}; st->framelen = 0; break; } @@ -231,11 +235,10 @@ read_token (const char *p, reader_stack *st) // pop frame, push new LIST st->pos += st->framelen; assert (st->pos->type == type); - st->framelen = st->pos->fix32.val + 1; + st->framelen = st->pos->fix32.val.n + 1; // overwrite the frame marker with the collection it became - st->pos->list = (list_object) - { - .type = type,.rest = 0,.head = o}; + st->pos->list = new_list (o); + st->pos->list.type = type; break; } case ']': @@ -247,7 +250,7 @@ read_token (const char *p, reader_stack *st) uint32_t len = st->framelen; st->pos += st->framelen; assert (st->pos->type == EVALTYPE_VECTOR); - st->framelen = st->pos->fix32.val + 1; + st->framelen = st->pos->fix32.val.n + 1; st->pos->vector = new_vector (h, len); break; } @@ -257,7 +260,13 @@ read_token (const char *p, reader_stack *st) if (n) return p + n; - // TODO: try read pname + n = count_pname (p); + if (n > 0) + { + (--(st->pos))->atom = oblist_find_or_insert (root, p, n); + st->framelen++; + return p + n; + } fprintf (stderr, "read unimplemented for char: '%c'\n", *p); assert (0 && "read unimplemented for char");