picrin/src/load.c

60 lines
1014 B
C

/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "picrin/pair.h"
pic_value
pic_load(pic_state *pic, const char *fn)
{
FILE *file;
int ai;
pic_value v, exprs;
struct pic_proc *proc;
file = fopen(fn, "r");
if (file == NULL) {
pic_errorf(pic, "load: could not read file \"%s\"", fn);
}
exprs = pic_parse_file(pic, file);
if (pic_undef_p(exprs)) {
pic_error(pic, "load: unexpected EOF");
}
pic_for_each (v, exprs) {
ai = pic_gc_arena_preserve(pic);
proc = pic_compile(pic, v);
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
pic_load_load(pic_state *pic)
{
pic_value envid;
char *fn;
pic_get_args(pic, "z|o", &fn, &envid);
return pic_load(pic, fn);
}
void
pic_init_load(pic_state *pic)
{
pic_deflibrary ("(scheme load)") {
pic_defun(pic, "load", pic_load_load);
}
}