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 *
|
2013-12-07 21:27:08 -05:00
|
|
|
pic_make_library(pic_state *pic, pic_value name)
|
2013-12-07 10:05:06 -05:00
|
|
|
{
|
|
|
|
struct pic_lib *lib;
|
2013-12-10 01:59:27 -05:00
|
|
|
struct pic_senv *senv;
|
2013-12-07 10:05:06 -05:00
|
|
|
|
2013-12-08 02:15:16 -05:00
|
|
|
if ((lib = pic_find_library(pic, name)) != NULL) {
|
|
|
|
return lib;
|
|
|
|
}
|
|
|
|
|
2013-12-10 01:59:27 -05:00
|
|
|
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);
|
2013-12-10 01:59:27 -05:00
|
|
|
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! */
|
2013-12-07 21:27:08 -05:00
|
|
|
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
|
2013-12-10 08:40:03 -05:00
|
|
|
pic_in_library(pic_state *pic, pic_value spec)
|
2013-12-07 10:05:06 -05:00
|
|
|
{
|
2013-12-10 08:40:03 -05:00
|
|
|
struct pic_lib *lib;
|
2013-12-07 10:05:06 -05:00
|
|
|
|
2013-12-10 08:40:03 -05:00
|
|
|
lib = pic_find_library(pic, spec);
|
|
|
|
if (! lib) {
|
2013-12-07 10:05:06 -05:00
|
|
|
pic_error(pic, "library not found");
|
|
|
|
}
|
2013-12-10 08:40:03 -05:00
|
|
|
pic->lib = lib;
|
2013-12-07 10:05:06 -05:00
|
|
|
}
|
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));
|
|
|
|
}
|