picrin/src/lib.c

45 lines
958 B
C
Raw Normal View History

2013-12-07 10:05:06 -05:00
#include "picrin.h"
#include "picrin/lib.h"
#include "picrin/pair.h"
#include "picrin/macro.h"
#include "xhash/xhash.h"
2013-12-07 10:11:22 -05:00
static pic_value
lib_spec(pic_state *pic, const char *name)
{
pic_value vs;
int r;
r = pic_parse_cstr(pic, name, &vs);
if (r != 1) {
pic_error(pic, "invalid library spec given");
}
return pic_car(pic, vs);
}
2013-12-07 10:05:06 -05:00
void
2013-12-07 10:11:22 -05:00
pic_make_library(pic_state *pic, const char *name)
2013-12-07 10:05:06 -05:00
{
struct pic_lib *lib;
2013-12-07 10:11:22 -05:00
pic_value spec = lib_spec(pic, name);
2013-12-07 10:05:06 -05:00
lib = (struct pic_lib *)pic_obj_alloc(pic, sizeof(struct pic_lib), PIC_TT_LIB);
lib->senv = pic_minimal_syntactic_env(pic);
lib->exports = xh_new();
2013-12-07 10:11:22 -05:00
pic->lib_tbl = pic_acons(pic, spec, pic_obj_value(lib), pic->lib_tbl);
2013-12-07 10:05:06 -05:00
}
void
2013-12-07 10:11:22 -05:00
pic_in_library(pic_state *pic, const char *name)
2013-12-07 10:05:06 -05:00
{
2013-12-07 10:11:22 -05:00
pic_value v, spec = lib_spec(pic, name);
2013-12-07 10:05:06 -05:00
2013-12-07 10:11:22 -05:00
v = pic_assoc(pic, spec, pic->lib_tbl);
2013-12-07 10:05:06 -05:00
if (pic_false_p(v)) {
pic_error(pic, "library not found");
}
2013-12-07 10:11:22 -05:00
2013-12-07 10:05:06 -05:00
pic->lib = pic_lib_ptr(pic_cdr(pic, v));
}