implement load procedure

This commit is contained in:
Yuichi Nishiwaki 2014-01-13 13:54:52 +09:00
parent 9d54244424
commit 5b068d7cc7
2 changed files with 60 additions and 0 deletions

View File

@ -23,6 +23,7 @@ void pic_init_error(pic_state *);
void pic_init_str(pic_state *);
void pic_init_macro(pic_state *);
void pic_init_var(pic_state *);
void pic_init_load(pic_state *);
void
pic_load_stdlib(pic_state *pic)
@ -126,6 +127,7 @@ pic_init_core(pic_state *pic)
pic_init_str(pic); DONE;
pic_init_macro(pic); DONE;
pic_init_var(pic); DONE;
pic_init_load(pic); DONE;
pic_load_stdlib(pic); DONE;

58
src/load.c Normal file
View File

@ -0,0 +1,58 @@
#include "picrin.h"
#include "picrin/pair.h"
pic_value
pic_load(pic_state *pic, const char *fn)
{
FILE *file;
int n, i, ai;
pic_value v, vs;
struct pic_proc *proc;
file = fopen(fn, "r");
if (file == NULL) {
pic_error(pic, "load: could not read file");
}
n = pic_parse_file(pic, file, &vs);
if (n < 0) {
pic_error(pic, "load: parse failure");
}
ai = pic_gc_arena_preserve(pic);
for (i = 0; i < n; ++i, vs = pic_cdr(pic, vs)) {
v = pic_car(pic, vs);
proc = pic_codegen(pic, v);
if (proc == NULL) {
pic_error(pic, "load: compilation failure");
}
v = pic_apply(pic, proc, pic_nil_value());
if (pic_undef_p(v)) {
pic_error(pic, "load: evaluation failure");
}
pic_gc_arena_restore(pic, ai);
}
return pic_none_value();
}
static pic_value
pic_load_load(pic_state *pic)
{
pic_value envid;
char *fn;
size_t len;
pic_get_args(pic, "s|o", &fn, &len, &envid);
return pic_load(pic, fn);
}
void
pic_init_load(pic_state *pic)
{
pic_defun(pic, "load", pic_load_load);
}