don't malloc pic_reader

This commit is contained in:
Yuichi Nishiwaki 2015-06-19 03:14:55 +09:00
parent db0767c931
commit 10f81512d8
4 changed files with 29 additions and 35 deletions

View File

@ -49,6 +49,7 @@ typedef struct pic_state pic_state;
#include "picrin/irep.h" #include "picrin/irep.h"
#include "picrin/file.h" #include "picrin/file.h"
#include "picrin/read.h"
typedef struct pic_jmpbuf { typedef struct pic_jmpbuf {
PIC_JMPBUF buf; PIC_JMPBUF buf;
@ -137,7 +138,7 @@ struct pic_state {
pic_value libs; pic_value libs;
struct pic_reg *attrs; struct pic_reg *attrs;
struct pic_reader *reader; pic_reader reader;
xFILE files[XOPEN_MAX]; xFILE files[XOPEN_MAX];
pic_code iseq[2]; /* for pic_apply_trampoline */ pic_code iseq[2]; /* for pic_apply_trampoline */
@ -277,11 +278,9 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *);
#include "picrin/pair.h" #include "picrin/pair.h"
#include "picrin/port.h" #include "picrin/port.h"
#include "picrin/proc.h" #include "picrin/proc.h"
#include "picrin/read.h"
#include "picrin/record.h" #include "picrin/record.h"
#include "picrin/string.h" #include "picrin/string.h"
#include "picrin/symbol.h" #include "picrin/symbol.h"
#include "picrin/read.h"
#include "picrin/vector.h" #include "picrin/vector.h"
#include "picrin/reg.h" #include "picrin/reg.h"

View File

@ -11,7 +11,7 @@ extern "C" {
typedef pic_value (*pic_reader_t)(pic_state *, struct pic_port *port, int c); typedef pic_value (*pic_reader_t)(pic_state *, struct pic_port *port, int c);
struct pic_reader { typedef struct {
enum pic_typecase { enum pic_typecase {
PIC_CASE_DEFAULT, PIC_CASE_DEFAULT,
PIC_CASE_FOLD PIC_CASE_FOLD
@ -19,10 +19,10 @@ struct pic_reader {
xhash labels; xhash labels;
pic_reader_t table[256]; pic_reader_t table[256];
pic_reader_t dispatch[256]; pic_reader_t dispatch[256];
}; } pic_reader;
struct pic_reader *pic_reader_open(pic_state *); void pic_reader_init(pic_state *);
void pic_reader_close(pic_state *, struct pic_reader *); void pic_reader_destroy(pic_state *);
#if defined(__cplusplus) #if defined(__cplusplus)
} }

View File

@ -79,7 +79,7 @@ strcaseeq(const char *s1, const char *s2)
static int static int
case_fold(pic_state *pic, int c) case_fold(pic_state *pic, int c)
{ {
if (pic->reader->typecase == PIC_CASE_FOLD) { if (pic->reader.typecase == PIC_CASE_FOLD) {
c = tolower(c); c = tolower(c);
} }
return c; return c;
@ -131,13 +131,13 @@ read_directive(pic_state *pic, struct pic_port *port, int c)
switch (peek(pic, port)) { switch (peek(pic, port)) {
case 'n': case 'n':
if (expect(pic, port, "no-fold-case")) { if (expect(pic, port, "no-fold-case")) {
pic->reader->typecase = PIC_CASE_DEFAULT; pic->reader.typecase = PIC_CASE_DEFAULT;
return pic_invalid_value(); return pic_invalid_value();
} }
break; break;
case 'f': case 'f':
if (expect(pic, port, "fold-case")) { if (expect(pic, port, "fold-case")) {
pic->reader->typecase = PIC_CASE_FOLD; pic->reader.typecase = PIC_CASE_FOLD;
return pic_invalid_value(); return pic_invalid_value();
} }
break; break;
@ -649,7 +649,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i)
val = pic_cons(pic, pic_undef_value(), pic_undef_value()); val = pic_cons(pic, pic_undef_value(), pic_undef_value());
xh_put_int(&pic->reader->labels, i, &val); xh_put_int(&pic->reader.labels, i, &val);
tmp = read(pic, port, c); tmp = read(pic, port, c);
pic_pair_ptr(val)->car = pic_car(pic, tmp); pic_pair_ptr(val)->car = pic_car(pic, tmp);
@ -672,7 +672,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i)
val = pic_obj_value(pic_make_vec(pic, 0)); val = pic_obj_value(pic_make_vec(pic, 0));
xh_put_int(&pic->reader->labels, i, &val); xh_put_int(&pic->reader.labels, i, &val);
tmp = pic_vec_ptr(read(pic, port, c)); tmp = pic_vec_ptr(read(pic, port, c));
PIC_SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data); PIC_SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data);
@ -687,7 +687,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i)
{ {
val = read(pic, port, c); val = read(pic, port, c);
xh_put_int(&pic->reader->labels, i, &val); xh_put_int(&pic->reader.labels, i, &val);
return val; return val;
} }
@ -699,7 +699,7 @@ read_label_ref(pic_state *pic, struct pic_port PIC_UNUSED(*port), int i)
{ {
xh_entry *e; xh_entry *e;
e = xh_get_int(&pic->reader->labels, i); e = xh_get_int(&pic->reader.labels, i);
if (! e) { if (! e) {
read_error(pic, "label of given index not defined"); read_error(pic, "label of given index not defined");
} }
@ -740,11 +740,11 @@ read_dispatch(pic_state *pic, struct pic_port *port, int c)
read_error(pic, "unexpected EOF"); read_error(pic, "unexpected EOF");
} }
if (pic->reader->dispatch[c] == NULL) { if (pic->reader.dispatch[c] == NULL) {
read_error(pic, "invalid character at the seeker head"); read_error(pic, "invalid character at the seeker head");
} }
return pic->reader->dispatch[c](pic, port, c); return pic->reader.dispatch[c](pic, port, c);
} }
static pic_value static pic_value
@ -756,11 +756,11 @@ read_nullable(pic_state *pic, struct pic_port *port, int c)
read_error(pic, "unexpected EOF"); read_error(pic, "unexpected EOF");
} }
if (pic->reader->table[c] == NULL) { if (pic->reader.table[c] == NULL) {
read_error(pic, "invalid character at the seeker head"); read_error(pic, "invalid character at the seeker head");
} }
return pic->reader->table[c](pic, port, c); return pic->reader.table[c](pic, port, c);
} }
static pic_value static pic_value
@ -780,7 +780,7 @@ read(pic_state *pic, struct pic_port *port, int c)
} }
static void static void
reader_table_init(struct pic_reader *reader) reader_table_init(pic_reader *reader)
{ {
int c; int c;
@ -826,34 +826,29 @@ reader_table_init(struct pic_reader *reader)
} }
} }
struct pic_reader * void
pic_reader_open(pic_state *pic) pic_reader_init(pic_state *pic)
{ {
struct pic_reader *reader;
int c; int c;
reader = pic_malloc(pic, sizeof(struct pic_reader)); pic->reader.typecase = PIC_CASE_DEFAULT;
reader->typecase = PIC_CASE_DEFAULT; xh_init_int(&pic->reader.labels, sizeof(pic_value));
xh_init_int(&reader->labels, sizeof(pic_value));
for (c = 0; c < 256; ++c) { for (c = 0; c < 256; ++c) {
reader->table[c] = NULL; pic->reader.table[c] = NULL;
} }
for (c = 0; c < 256; ++c) { for (c = 0; c < 256; ++c) {
reader->dispatch[c] = NULL; pic->reader.dispatch[c] = NULL;
} }
reader_table_init(reader); reader_table_init(&pic->reader);
return reader;
} }
void void
pic_reader_close(pic_state *pic, struct pic_reader *reader) pic_reader_destroy(pic_state *pic)
{ {
xh_destroy(&reader->labels); xh_destroy(&pic->reader.labels);
pic_free(pic, reader);
} }
pic_value pic_value

View File

@ -364,7 +364,7 @@ pic_open(int argc, char *argv[], char **envp, pic_allocf allocf)
pic->cp->in = pic->cp->out = NULL; pic->cp->in = pic->cp->out = NULL;
/* reader */ /* reader */
pic->reader = pic_reader_open(pic); pic_reader_init(pic);
/* parameter table */ /* parameter table */
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_dict(pic)), pic->ptable); pic->ptable = pic_cons(pic, pic_obj_value(pic_make_dict(pic)), pic->ptable);
@ -440,7 +440,7 @@ pic_close(pic_state *pic)
pic_heap_close(pic, pic->heap); pic_heap_close(pic, pic->heap);
/* free reader struct */ /* free reader struct */
pic_reader_close(pic, pic->reader); pic_reader_destroy(pic);
/* free runtime context */ /* free runtime context */
allocf(pic->stbase, 0); allocf(pic->stbase, 0);