picrin/extlib/benz/lib.c

260 lines
5.5 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
KHASH_DEFINE(ltable, const char *, struct pic_lib, kh_str_hash_func, kh_str_cmp_func)
static struct pic_lib *
get_library_opt(pic_state *pic, const char *lib)
{
khash_t(ltable) *h = &pic->ltable;
khiter_t it;
it = kh_get(ltable, h, lib);
if (it == kh_end(h)) {
return NULL;
}
return &kh_val(h, it);
}
static struct pic_lib *
get_library(pic_state *pic, const char *lib)
{
struct pic_lib *libp;
if ((libp = get_library_opt(pic, lib)) == NULL) {
pic_errorf(pic, "library not found: %s", lib);
}
return libp;
}
static struct pic_env *
make_library_env(pic_state *pic, struct pic_string *name)
2015-06-09 07:20:56 -04:00
{
struct pic_env *env;
env = pic_make_topenv(pic, name);
/* set up default environment */
2016-02-06 09:15:53 -05:00
pic_put_identifier(pic, (pic_id *)pic->sDEFINE_LIBRARY, pic->sDEFINE_LIBRARY, env);
pic_put_identifier(pic, (pic_id *)pic->sIMPORT, pic->sIMPORT, env);
pic_put_identifier(pic, (pic_id *)pic->sEXPORT, pic->sEXPORT, env);
pic_put_identifier(pic, (pic_id *)pic->sCOND_EXPAND, pic->sCOND_EXPAND, env);
return env;
2015-06-09 07:20:56 -04:00
}
void
pic_make_library(pic_state *pic, const char *lib)
2014-08-25 00:38:09 -04:00
{
khash_t(ltable) *h = &pic->ltable;
const char *old_lib;
struct pic_string *name;
2015-05-30 09:39:03 -04:00
struct pic_env *env;
2015-01-18 07:32:16 -05:00
struct pic_dict *exports;
khiter_t it;
int ret;
2014-08-25 00:38:09 -04:00
if (pic->lib) {
old_lib = pic_current_library(pic);
2014-08-25 00:38:09 -04:00
}
name = pic_cstr_value(pic, lib);
env = make_library_env(pic, name);
2015-01-18 07:32:16 -05:00
exports = pic_make_dict(pic);
2014-08-25 00:38:09 -04:00
it = kh_put(ltable, h, pic_str(pic, name), &ret);
if (ret == 0) { /* if exists */
pic_errorf(pic, "library name already in use: %s", lib);
}
2014-08-25 00:38:09 -04:00
kh_val(h, it).name = name;
kh_val(h, it).env = env;
kh_val(h, it).exports = exports;
2014-08-25 00:38:09 -04:00
if (pic->lib) {
pic->lib = get_library(pic, old_lib); /* ltable might be rehashed */
}
2014-08-25 00:38:09 -04:00
}
void
pic_in_library(pic_state *pic, const char *lib)
{
pic->lib = get_library(pic, lib);
}
bool
pic_find_library(pic_state *pic, const char *lib)
{
return get_library_opt(pic, lib) != NULL;
}
const char *
pic_current_library(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
return pic_str(pic, pic->lib->name);
}
2014-08-25 00:38:09 -04:00
struct pic_env *
pic_library_environment(pic_state *pic, const char *lib)
{
return get_library(pic, lib)->env;
2014-08-25 00:38:09 -04:00
}
2015-06-16 12:52:20 -04:00
void
pic_import(pic_state *pic, const char *lib)
2014-08-25 00:38:09 -04:00
{
2015-06-16 12:52:20 -04:00
pic_sym *name, *realname, *uid;
2016-02-18 10:39:13 -05:00
int it = 0;
pic_value val;
struct pic_lib *libp;
libp = get_library(pic, lib);
2015-06-09 09:36:04 -04:00
2016-02-18 10:39:13 -05:00
while (pic_dict_next(pic, libp->exports, &it, &name, &val)) {
realname = pic_sym_ptr(val);
2015-06-09 09:36:04 -04:00
if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) {
2015-06-09 09:36:04 -04:00
pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname));
}
2016-02-06 09:15:53 -05:00
pic_put_identifier(pic, (pic_id *)name, uid, pic->lib->env);
2014-08-25 00:38:09 -04:00
}
}
void
2015-06-16 12:52:20 -04:00
pic_export(pic_state *pic, pic_sym *name)
2014-08-25 00:38:09 -04:00
{
2015-06-16 12:52:20 -04:00
pic_dict_set(pic, pic->lib->exports, name, pic_obj_value(name));
2014-08-25 00:38:09 -04:00
}
2015-06-16 09:51:05 -04:00
static pic_value
pic_lib_make_library(pic_state *pic)
{
const char *lib;
2015-06-16 09:51:05 -04:00
pic_get_args(pic, "z", &lib);
2015-06-16 09:51:05 -04:00
pic_make_library(pic, lib);
return pic_undef_value(pic);
2015-06-16 09:51:05 -04:00
}
static pic_value
pic_lib_find_library(pic_state *pic)
{
const char *lib;
2015-06-16 09:51:05 -04:00
pic_get_args(pic, "z", &lib);
2015-06-16 09:51:05 -04:00
return pic_bool_value(pic, pic_find_library(pic, lib));
2015-06-16 09:51:05 -04:00
}
2015-06-16 11:03:52 -04:00
static pic_value
pic_lib_current_library(pic_state *pic)
{
const char *lib;
2015-08-26 06:04:27 -04:00
int n;
2015-06-16 11:03:52 -04:00
n = pic_get_args(pic, "|z", &lib);
2015-06-16 11:03:52 -04:00
if (n == 0) {
return pic_obj_value(pic->lib->name);
2015-06-16 11:03:52 -04:00
}
else {
pic_in_library(pic, lib);
2015-06-16 11:03:52 -04:00
return pic_undef_value(pic);
2015-06-16 11:03:52 -04:00
}
}
2015-06-16 11:59:19 -04:00
static pic_value
pic_lib_library_import(pic_state *pic)
{
const char *lib;
2015-06-16 11:59:19 -04:00
pic_sym *name, *realname, *uid, *alias = NULL;
struct pic_lib *libp;
2015-06-16 11:59:19 -04:00
pic_get_args(pic, "zm|m", &lib, &name, &alias);
2015-06-16 11:59:19 -04:00
if (alias == NULL) {
alias = name;
}
libp = get_library(pic, lib);
2015-06-16 11:59:19 -04:00
if (! pic_dict_has(pic, libp->exports, name)) {
2015-06-16 11:59:19 -04:00
pic_errorf(pic, "attempted to import undefined variable '~s'", pic_obj_value(name));
} else {
realname = pic_sym_ptr(pic_dict_ref(pic, libp->exports, name));
2015-06-16 11:59:19 -04:00
}
if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) {
2015-06-16 11:59:19 -04:00
pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname));
} else {
2016-02-06 09:15:53 -05:00
pic_put_identifier(pic, (pic_id *)alias, uid, pic->lib->env);
2015-06-16 11:59:19 -04:00
}
return pic_undef_value(pic);
2015-06-16 11:59:19 -04:00
}
2015-06-16 12:42:44 -04:00
static pic_value
pic_lib_library_export(pic_state *pic)
{
pic_sym *name, *alias = NULL;
pic_get_args(pic, "m|m", &name, &alias);
if (alias == NULL) {
alias = name;
}
pic_dict_set(pic, pic->lib->exports, alias, pic_obj_value(name));
return pic_undef_value(pic);
2015-06-16 12:42:44 -04:00
}
2015-06-16 09:51:05 -04:00
static pic_value
pic_lib_library_exports(pic_state *pic)
{
const char *lib;
pic_value exports = pic_nil_value(pic);
2015-06-16 09:51:05 -04:00
pic_sym *sym;
2016-02-18 10:39:13 -05:00
int it = 0;
struct pic_lib *libp;
2015-06-16 09:51:05 -04:00
pic_get_args(pic, "z", &lib);
2015-06-16 09:51:05 -04:00
libp = get_library(pic, lib);
2015-06-16 09:51:05 -04:00
2016-02-18 10:39:13 -05:00
while (pic_dict_next(pic, libp->exports, &it, &sym, NULL)) {
2015-06-16 09:51:05 -04:00
pic_push(pic, pic_obj_value(sym), exports);
}
return exports;
}
static pic_value
pic_lib_library_environment(pic_state *pic)
{
const char *lib;
2015-06-16 09:51:05 -04:00
pic_get_args(pic, "z", &lib);
2015-06-16 09:51:05 -04:00
return pic_obj_value(get_library(pic, lib)->env);
2015-06-16 09:51:05 -04:00
}
2014-08-25 00:38:09 -04:00
void
pic_init_lib(pic_state *pic)
{
2015-06-16 09:51:05 -04:00
pic_defun(pic, "make-library", pic_lib_make_library);
pic_defun(pic, "find-library", pic_lib_find_library);
pic_defun(pic, "library-exports", pic_lib_library_exports);
pic_defun(pic, "library-environment", pic_lib_library_environment);
2015-06-16 12:42:44 -04:00
pic_defun(pic, "current-library", pic_lib_current_library);
pic_defun(pic, "library-import", pic_lib_library_import);
pic_defun(pic, "library-export", pic_lib_library_export);
2014-08-25 00:38:09 -04:00
}