2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2013-12-07 10:05:06 -05:00
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/lib.h"
|
|
|
|
#include "picrin/pair.h"
|
|
|
|
#include "picrin/macro.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) {
|
2014-01-08 10:40:13 -05:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
printf("* reopen library: ");
|
|
|
|
pic_debug(pic, name);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
2013-12-08 02:15:16 -05:00
|
|
|
return lib;
|
|
|
|
}
|
|
|
|
|
2014-04-04 14:50:38 -04:00
|
|
|
senv = pic_null_syntactic_environment(pic);
|
2013-12-10 01:59:27 -05:00
|
|
|
|
2013-12-07 10:05:06 -05:00
|
|
|
lib = (struct pic_lib *)pic_obj_alloc(pic, sizeof(struct pic_lib), PIC_TT_LIB);
|
2014-07-20 02:30:48 -04:00
|
|
|
lib->env = senv;
|
2013-12-07 21:59:13 -05:00
|
|
|
lib->name = name;
|
2014-03-25 02:29:26 -04:00
|
|
|
xh_init_int(&lib->exports, sizeof(pic_sym));
|
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) {
|
2014-05-20 11:37:15 -04:00
|
|
|
pic_errorf(pic, "library not found: ~a", spec);
|
2013-12-07 10:05:06 -05:00
|
|
|
}
|
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;
|
|
|
|
|
2014-07-22 00:19:25 -04:00
|
|
|
v = pic_assoc(pic, spec, pic->lib_tbl, NULL);
|
2013-12-07 12:42:34 -05:00
|
|
|
if (pic_false_p(v)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return pic_lib_ptr(pic_cdr(pic, v));
|
|
|
|
}
|
2014-06-03 10:30:00 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
pic_import(pic_state *pic, pic_value spec)
|
|
|
|
{
|
|
|
|
struct pic_lib *lib;
|
|
|
|
xh_iter it;
|
|
|
|
|
|
|
|
lib = pic_find_library(pic, spec);
|
|
|
|
if (! lib) {
|
|
|
|
pic_errorf(pic, "library not found: ~a", spec);
|
|
|
|
}
|
|
|
|
xh_begin(&it, &lib->exports);
|
|
|
|
while (xh_next(&it)) {
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
printf("* importing %s as %s\n", pic_symbol_name(pic, xh_key(it.e, pic_sym)), pic_symbol_name(pic, xh_val(it.e, pic_sym)));
|
|
|
|
#endif
|
|
|
|
|
2014-07-20 02:30:48 -04:00
|
|
|
pic_put_rename(pic, pic->lib->env, xh_key(it.e, pic_sym), xh_val(it.e, pic_sym));
|
2014-06-03 10:30:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_export(pic_state *pic, pic_sym sym)
|
|
|
|
{
|
|
|
|
pic_sym rename;
|
|
|
|
|
2014-07-20 02:30:48 -04:00
|
|
|
if (! pic_find_rename(pic, pic->lib->env, sym, &rename)) {
|
2014-06-03 10:30:00 -04:00
|
|
|
pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, sym));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
printf("* exporting %s as %s\n", pic_symbol_name(pic, sym), pic_symbol_name(pic, rename));
|
|
|
|
#endif
|
|
|
|
|
2014-06-14 07:59:31 -04:00
|
|
|
xh_put_int(&pic->lib->exports, sym, &rename);
|
2014-06-03 10:30:00 -04:00
|
|
|
}
|
2014-06-03 10:54:51 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
pic_export_as(pic_state *pic, pic_sym sym, pic_sym as)
|
|
|
|
{
|
|
|
|
pic_sym rename;
|
|
|
|
|
2014-07-20 02:30:48 -04:00
|
|
|
if (! pic_find_rename(pic, pic->lib->env, sym, &rename)) {
|
2014-06-03 10:54:51 -04:00
|
|
|
pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, sym));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
printf("* exporting %s as %s\n", pic_symbol_name(pic, as), pic_symbol_name(pic, rename));
|
|
|
|
#endif
|
|
|
|
|
2014-06-14 07:59:31 -04:00
|
|
|
xh_put_int(&pic->lib->exports, as, &rename);
|
2014-06-03 10:54:51 -04:00
|
|
|
}
|