refactor load
This commit is contained in:
parent
6cb34f063d
commit
ed9b14630b
|
@ -163,8 +163,8 @@ char *pic_strndup(pic_state *, const char *, size_t);
|
||||||
pic_value pic_read(pic_state *, struct pic_port *);
|
pic_value pic_read(pic_state *, struct pic_port *);
|
||||||
pic_value pic_read_cstr(pic_state *, const char *);
|
pic_value pic_read_cstr(pic_state *, const char *);
|
||||||
|
|
||||||
pic_value pic_load(pic_state *, const char *);
|
void pic_load(pic_state *, const char *);
|
||||||
pic_value pic_load_cstr(pic_state *, const char *);
|
void pic_load_cstr(pic_state *, const char *);
|
||||||
|
|
||||||
pic_value pic_apply(pic_state *, struct pic_proc *, pic_value);
|
pic_value pic_apply(pic_state *, struct pic_proc *, pic_value);
|
||||||
pic_value pic_apply0(pic_state *, struct pic_proc *);
|
pic_value pic_apply0(pic_state *, struct pic_proc *);
|
||||||
|
|
90
load.c
90
load.c
|
@ -4,67 +4,57 @@
|
||||||
|
|
||||||
#include "picrin.h"
|
#include "picrin.h"
|
||||||
#include "picrin/pair.h"
|
#include "picrin/pair.h"
|
||||||
|
#include "picrin/port.h"
|
||||||
|
#include "picrin/error.h"
|
||||||
|
|
||||||
pic_value
|
static void
|
||||||
|
pic_load_port(pic_state *pic, struct pic_port *port)
|
||||||
|
{
|
||||||
|
pic_value form;
|
||||||
|
|
||||||
|
pic_try {
|
||||||
|
size_t ai = pic_gc_arena_preserve(pic);
|
||||||
|
|
||||||
|
while (! pic_eof_p(form = pic_read(pic, port))) {
|
||||||
|
pic_eval(pic, form, pic->lib);
|
||||||
|
|
||||||
|
pic_gc_arena_restore(pic, ai);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pic_catch {
|
||||||
|
pic_errorf(pic, "load error: %s", pic_errmsg(pic));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
pic_load_cstr(pic_state *pic, const char *src)
|
pic_load_cstr(pic_state *pic, const char *src)
|
||||||
{
|
{
|
||||||
size_t ai;
|
struct pic_port *port = pic_open_input_string(pic, src);
|
||||||
pic_value v, exprs;
|
|
||||||
struct pic_proc *proc;
|
|
||||||
|
|
||||||
exprs = pic_parse_cstr(pic, src);
|
pic_load_port(pic, port);
|
||||||
if (pic_undef_p(exprs)) {
|
|
||||||
pic_errorf(pic, "load: read failure (%s)", pic_errmsg(pic));
|
pic_close_port(pic, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
pic_for_each (v, exprs) {
|
void
|
||||||
ai = pic_gc_arena_preserve(pic);
|
pic_load(pic_state *pic, const char *filename)
|
||||||
|
|
||||||
proc = pic_compile(pic, v, pic->lib);
|
|
||||||
if (proc == NULL) {
|
|
||||||
pic_error(pic, "load: compilation failure");
|
|
||||||
}
|
|
||||||
|
|
||||||
pic_apply(pic, proc, pic_nil_value());
|
|
||||||
|
|
||||||
pic_gc_arena_restore(pic, ai);
|
|
||||||
}
|
|
||||||
|
|
||||||
return pic_none_value();
|
|
||||||
}
|
|
||||||
|
|
||||||
pic_value
|
|
||||||
pic_load(pic_state *pic, const char *fn)
|
|
||||||
{
|
{
|
||||||
FILE *file;
|
struct pic_port *port;
|
||||||
size_t ai;
|
xFILE *file;
|
||||||
pic_value v, exprs;
|
|
||||||
struct pic_proc *proc;
|
|
||||||
|
|
||||||
file = fopen(fn, "r");
|
file = xfopen(filename, "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
pic_errorf(pic, "load: could not read file \"%s\"", fn);
|
pic_errorf(pic, "could not open file: %s", filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
exprs = pic_parse_file(pic, file);
|
port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT);
|
||||||
if (pic_undef_p(exprs)) {
|
port->file = file;
|
||||||
pic_errorf(pic, "load: read failure (%s)", pic_errmsg(pic));
|
port->flags = PIC_PORT_IN | PIC_PORT_TEXT;
|
||||||
}
|
port->status = PIC_PORT_OPEN;
|
||||||
|
|
||||||
pic_for_each (v, exprs) {
|
pic_load_port(pic, port);
|
||||||
ai = pic_gc_arena_preserve(pic);
|
|
||||||
|
|
||||||
proc = pic_compile(pic, v, pic->lib);
|
pic_close_port(pic, port);
|
||||||
if (proc == NULL) {
|
|
||||||
pic_error(pic, "load: compilation failure");
|
|
||||||
}
|
|
||||||
|
|
||||||
pic_apply(pic, proc, pic_nil_value());
|
|
||||||
|
|
||||||
pic_gc_arena_restore(pic, ai);
|
|
||||||
}
|
|
||||||
|
|
||||||
return pic_none_value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static pic_value
|
static pic_value
|
||||||
|
@ -75,7 +65,9 @@ pic_load_load(pic_state *pic)
|
||||||
|
|
||||||
pic_get_args(pic, "z|o", &fn, &envid);
|
pic_get_args(pic, "z|o", &fn, &envid);
|
||||||
|
|
||||||
return pic_load(pic, fn);
|
pic_load(pic, fn);
|
||||||
|
|
||||||
|
return pic_none_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Reference in New Issue