From 3d1098967c220407cea89edb004abd557c66c3e3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 22 Jan 2015 17:18:38 +0900 Subject: [PATCH] use pic_alloc/pic_free in initializing struct pic_reader --- extlib/benz/include/picrin/read.h | 3 +- extlib/benz/read.c | 89 ++++++++++++++++++++----------- extlib/benz/state.c | 14 ++--- 3 files changed, 63 insertions(+), 43 deletions(-) diff --git a/extlib/benz/include/picrin/read.h b/extlib/benz/include/picrin/read.h index 7a3d4f55..705b4589 100644 --- a/extlib/benz/include/picrin/read.h +++ b/extlib/benz/include/picrin/read.h @@ -21,7 +21,8 @@ struct pic_reader { pic_reader_t dispatch[256]; }; -void pic_init_reader(pic_state *); +struct pic_reader *pic_reader_open(pic_state *); +void pic_reader_close(pic_state *, struct pic_reader *); #if defined(__cplusplus) } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 07b87b2b..5644e739 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -727,57 +727,82 @@ read(pic_state *pic, struct pic_port *port, int c) return val; } -void -pic_init_reader(pic_state *pic) +static void +reader_table_init(struct pic_reader *reader) { int c; - pic->reader->table[0] = NULL; + reader->table[0] = NULL; /* default reader */ for (c = 1; c < 256; ++c) { - pic->reader->table[c] = read_symbol; + reader->table[c] = read_symbol; } - pic->reader->table[')'] = read_unmatch; - pic->reader->table[';'] = read_comment; - pic->reader->table['\''] = read_quote; - pic->reader->table['`'] = read_quasiquote; - pic->reader->table[','] = read_unquote; - pic->reader->table['"'] = read_string; - pic->reader->table['|'] = read_pipe; - pic->reader->table['+'] = read_plus; - pic->reader->table['-'] = read_minus; - pic->reader->table['('] = read_pair; - pic->reader->table['['] = read_pair; - pic->reader->table['#'] = read_dispatch; + 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_pair; + reader->table['#'] = read_dispatch; /* read number */ for (c = '0'; c <= '9'; ++c) { - pic->reader->table[c] = read_number; + reader->table[c] = read_number; } - /* default dispatch reader */ - for (c = 0; c < 256; ++c) { - pic->reader->dispatch[c] = NULL; - } - - pic->reader->dispatch['!'] = read_directive; - pic->reader->dispatch['|'] = read_block_comment; - pic->reader->dispatch[';'] = read_datum_comment; - pic->reader->dispatch['t'] = read_true; - pic->reader->dispatch['f'] = read_false; - pic->reader->dispatch['\\'] = read_char; - pic->reader->dispatch['('] = read_vector; - pic->reader->dispatch['u'] = read_blob; - pic->reader->dispatch['.'] = read_eval; + reader->dispatch['!'] = read_directive; + reader->dispatch['|'] = read_block_comment; + reader->dispatch[';'] = read_datum_comment; + reader->dispatch['t'] = read_true; + reader->dispatch['f'] = read_false; + reader->dispatch['\\'] = read_char; + reader->dispatch['('] = read_vector; + reader->dispatch['u'] = read_blob; + reader->dispatch['.'] = read_eval; /* read labels */ for (c = '0'; c <= '9'; ++c) { - pic->reader->dispatch[c] = read_label; + reader->dispatch[c] = read_label; } } +struct pic_reader * +pic_reader_open(pic_state *pic) +{ + struct pic_reader *reader; + int c; + + reader = pic_alloc(pic, sizeof(struct pic_reader)); + reader->typecase = PIC_CASE_DEFAULT; + xh_init_int(&reader->labels, sizeof(pic_value)); + + for (c = 0; c < 256; ++c) { + reader->table[c] = NULL; + } + + for (c = 0; c < 256; ++c) { + reader->dispatch[c] = NULL; + } + + reader_table_init(reader); + + return reader; +} + +void +pic_reader_close(pic_state *pic, struct pic_reader *reader) +{ + xh_destroy(&reader->labels); + pic_free(pic, reader); +} + pic_value pic_read(pic_state *pic, struct pic_port *port) { diff --git a/extlib/benz/state.c b/extlib/benz/state.c index cc2042f6..c17c841c 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -170,12 +170,7 @@ pic_open(int argc, char *argv[], char **envp) pic->wind->in = pic->wind->out = NULL; /* reader */ - pic->reader = malloc(sizeof(struct pic_reader)); - pic->reader->typecase = PIC_CASE_DEFAULT; - xh_init_int(&pic->reader->labels, sizeof(pic_value)); - - /* init readers */ - pic_init_reader(pic); + pic->reader = pic_reader_open(pic); /* standard libraries */ pic->PICRIN_BASE = pic_open_library(pic, pic_read_cstr(pic, "(picrin base)")); @@ -234,15 +229,14 @@ pic_close(pic_state *pic) /* free heaps */ pic_heap_close(pic, pic->heap); + /* free reader struct */ + pic_reader_close(pic, pic->reader); + /* free runtime context */ free(pic->stbase); free(pic->cibase); free(pic->xpbase); - /* free reader struct */ - xh_destroy(&pic->reader->labels); - free(pic->reader); - /* free global stacks */ xh_destroy(&pic->syms); xh_destroy(&pic->attrs);