picrin/src/lib.c

53 lines
1.0 KiB
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 21:44:14 -05:00
struct pic_lib *
pic_make_library(pic_state *pic, pic_value name)
2013-12-07 10:05:06 -05:00
{
struct pic_lib *lib;
struct pic_senv *senv;
2013-12-07 10:05:06 -05:00
if ((lib = pic_find_library(pic, name)) != NULL) {
return lib;
}
senv = pic_minimal_syntactic_env(pic);
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 = senv;
2013-12-07 10:05:06 -05:00
lib->exports = xh_new();
2013-12-07 21:59:13 -05:00
lib->name = name;
2013-12-07 10:05:06 -05:00
2013-12-07 21:44:14 -05:00
/* register! */
pic->lib_tbl = pic_acons(pic, name, pic_obj_value(lib), pic->lib_tbl);
2013-12-07 21:44:14 -05:00
return lib;
2013-12-07 10:05:06 -05:00
}
void
pic_in_library(pic_state *pic, pic_value name)
2013-12-07 10:05:06 -05:00
{
pic_value v;
2013-12-07 10:05:06 -05:00
v = pic_assoc(pic, name, pic->lib_tbl);
2013-12-07 10:05:06 -05:00
if (pic_false_p(v)) {
pic_error(pic, "library not found");
}
pic->lib = pic_lib_ptr(pic_cdr(pic, v));
}
2013-12-07 12:42:34 -05:00
struct pic_lib *
pic_find_library(pic_state *pic, pic_value spec)
{
pic_value v;
v = pic_assoc(pic, spec, pic->lib_tbl);
if (pic_false_p(v)) {
return NULL;
}
return pic_lib_ptr(pic_cdr(pic, v));
}