From e3fc2d500967bb5cc1a0a3ca2909ea62d647d779 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 11 Aug 2014 18:36:23 +0900 Subject: [PATCH] add reader struct --- include/picrin.h | 3 +-- include/picrin/read.h | 26 ++++++++++++++++++++++++++ src/read.c | 15 ++++++++------- src/state.c | 11 ++++++++--- 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 include/picrin/read.h diff --git a/include/picrin.h b/include/picrin.h index c3373a7e..e58d5a61 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -94,8 +94,7 @@ typedef struct { xhash macros; pic_value libs; - bool rfcase; - xhash rlabels; + struct pic_reader *reader; jmp_buf *jmp; struct pic_error *err; diff --git a/include/picrin/read.h b/include/picrin/read.h new file mode 100644 index 00000000..31bdbf6c --- /dev/null +++ b/include/picrin/read.h @@ -0,0 +1,26 @@ +/** + * See Copyright Notice in picrin.h + */ + +#ifndef PICRIN_READ_H__ +#define PICRIN_READ_H__ + +#if defined(__cplusplus) +extern "C" { +#endif + +enum pic_typecase { + PIC_CASE_DEFAULT, + PIC_CASE_FOLD, +}; + +struct pic_reader { + short typecase; + xhash labels; +}; + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/src/read.c b/src/read.c index a7a199ca..4681a2ce 100644 --- a/src/read.c +++ b/src/read.c @@ -6,6 +6,7 @@ #include #include #include "picrin.h" +#include "picrin/read.h" #include "picrin/error.h" #include "picrin/pair.h" #include "picrin/string.h" @@ -134,13 +135,13 @@ read_directive(pic_state *pic, struct pic_port *port, int c) switch (peek(port)) { case 'n': if (expect(port, "no-fold-case")) { - pic->rfcase = false; + pic->reader->typecase = PIC_CASE_DEFAULT; return pic_undef_value(); } break; case 'f': if (expect(port, "fold-case")) { - pic->rfcase = true; + pic->reader->typecase = PIC_CASE_FOLD; return pic_undef_value(); } break; @@ -203,7 +204,7 @@ read_symbol(pic_state *pic, struct pic_port *port, int c) if (len != 0) { c = next(port); } - if (pic->rfcase) { + if (pic->reader->typecase == PIC_CASE_FOLD) { c = tolower(c); } len += 1; @@ -579,7 +580,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) val = pic_cons(pic, pic_none_value(), pic_none_value()); - xh_put_int(&pic->rlabels, i, &val); + xh_put_int(&pic->reader->labels, i, &val); tmp = read(pic, port, c); pic_pair_ptr(val)->car = pic_car(pic, tmp); @@ -602,7 +603,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) val = pic_obj_value(pic_vec_new(pic, 0)); - xh_put_int(&pic->rlabels, i, &val); + xh_put_int(&pic->reader->labels, i, &val); tmp = pic_vec_ptr(read(pic, port, c)); SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data); @@ -617,7 +618,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) { val = read(pic, port, c); - xh_put_int(&pic->rlabels, i, &val); + xh_put_int(&pic->reader->labels, i, &val); return val; } @@ -631,7 +632,7 @@ read_label_ref(pic_state *pic, struct pic_port *port, int i) UNUSED(port); - e = xh_get_int(&pic->rlabels, i); + e = xh_get_int(&pic->reader->labels, i); if (! e) { read_error(pic, "label of given index not defined"); } diff --git a/src/state.c b/src/state.c index a5ffe343..7b32566c 100644 --- a/src/state.c +++ b/src/state.c @@ -6,6 +6,7 @@ #include "picrin.h" #include "picrin/gc.h" +#include "picrin/read.h" #include "picrin/proc.h" #include "picrin/macro.h" #include "picrin/cont.h" @@ -59,8 +60,9 @@ pic_open(int argc, char *argv[], char **envp) pic->lib = NULL; /* reader */ - pic->rfcase = false; - xh_init_int(&pic->rlabels, sizeof(pic_value)); + pic->reader = malloc(sizeof(struct pic_reader)); + pic->reader->typecase = PIC_CASE_DEFAULT; + xh_init_int(&pic->reader->labels, sizeof(pic_value)); /* error handling */ pic->jmp = NULL; @@ -177,12 +179,15 @@ pic_close(pic_state *pic) free(pic->stbase); free(pic->cibase); + /* free reader struct */ + xh_destroy(&pic->reader->labels); + free(pic->reader); + /* free global stacks */ free(pic->try_jmps); xh_destroy(&pic->syms); xh_destroy(&pic->globals); xh_destroy(&pic->macros); - xh_destroy(&pic->rlabels); /* free GC arena */ free(pic->arena);