2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2016-02-20 10:58:58 -05:00
|
|
|
#include "picrin/extra.h"
|
2016-02-20 11:13:16 -05:00
|
|
|
#include "picrin/private/object.h"
|
|
|
|
#include "picrin/private/state.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
KHASH_DEFINE(ltable, const char *, struct lib, kh_str_hash_func, kh_str_cmp_func)
|
2016-02-18 03:39:32 -05:00
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
static struct lib *
|
2016-02-18 03:39:32 -05:00
|
|
|
get_library_opt(pic_state *pic, const char *lib)
|
|
|
|
{
|
|
|
|
khash_t(ltable) *h = &pic->ltable;
|
2016-02-20 11:52:34 -05:00
|
|
|
int it;
|
2016-02-18 03:39:32 -05:00
|
|
|
|
|
|
|
it = kh_get(ltable, h, lib);
|
|
|
|
if (it == kh_end(h)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &kh_val(h, it);
|
|
|
|
}
|
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
static struct lib *
|
2016-02-18 03:39:32 -05:00
|
|
|
get_library(pic_state *pic, const char *lib)
|
|
|
|
{
|
2016-02-21 06:32:00 -05:00
|
|
|
struct lib *libp;
|
2016-02-18 03:39:32 -05:00
|
|
|
|
|
|
|
if ((libp = get_library_opt(pic, lib)) == NULL) {
|
|
|
|
pic_errorf(pic, "library not found: %s", lib);
|
|
|
|
}
|
|
|
|
return libp;
|
|
|
|
}
|
|
|
|
|
2016-02-20 02:33:51 -05:00
|
|
|
static pic_value
|
2016-02-19 13:26:52 -05:00
|
|
|
make_library_env(pic_state *pic, pic_value name)
|
2015-06-09 07:20:56 -04:00
|
|
|
{
|
2016-02-21 06:32:00 -05:00
|
|
|
struct env *env;
|
2016-02-20 02:33:51 -05:00
|
|
|
pic_value e;
|
2016-02-05 14:07:37 -05:00
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
env = (struct env *)pic_obj_alloc(pic, sizeof(struct env), PIC_TYPE_ENV);
|
2016-02-19 02:58:39 -05:00
|
|
|
env->up = NULL;
|
2016-02-19 13:26:52 -05:00
|
|
|
env->lib = pic_str_ptr(pic, name);
|
2016-02-19 02:58:39 -05:00
|
|
|
kh_init(env, &env->map);
|
2016-02-05 14:07:37 -05:00
|
|
|
|
2016-02-20 02:33:51 -05:00
|
|
|
e = pic_obj_value(env);
|
|
|
|
|
2016-02-20 14:34:26 -05:00
|
|
|
#define REGISTER(name) pic_put_identifier(pic, pic_intern_lit(pic, name), pic_intern_lit(pic, name), e)
|
|
|
|
|
2016-02-05 14:07:37 -05:00
|
|
|
/* set up default environment */
|
2016-02-20 14:34:26 -05:00
|
|
|
REGISTER("define-library");
|
|
|
|
REGISTER("import");
|
|
|
|
REGISTER("export");
|
|
|
|
REGISTER("cond-expand");
|
2016-02-05 14:07:37 -05:00
|
|
|
|
2016-02-20 02:33:51 -05:00
|
|
|
return e;
|
2015-06-09 07:20:56 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
void
|
|
|
|
pic_make_library(pic_state *pic, const char *lib)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-18 03:39:32 -05:00
|
|
|
khash_t(ltable) *h = &pic->ltable;
|
2016-02-21 07:32:52 -05:00
|
|
|
const char *old_lib = NULL;
|
2016-02-20 02:33:51 -05:00
|
|
|
pic_value name, env, exports;
|
2016-02-20 11:52:34 -05:00
|
|
|
int it;
|
2016-02-18 03:39:32 -05:00
|
|
|
int ret;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
if (pic->lib) {
|
|
|
|
old_lib = pic_current_library(pic);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 09:49:16 -05:00
|
|
|
name = pic_cstr_value(pic, lib);
|
2016-02-05 14:07:37 -05:00
|
|
|
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
|
|
|
|
2016-02-18 09:49:16 -05:00
|
|
|
it = kh_put(ltable, h, pic_str(pic, name), &ret);
|
2016-02-18 03:39:32 -05:00
|
|
|
if (ret == 0) { /* if exists */
|
|
|
|
pic_errorf(pic, "library name already in use: %s", lib);
|
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
kh_val(h, it).name = pic_str_ptr(pic, name);
|
2016-02-20 02:33:51 -05:00
|
|
|
kh_val(h, it).env = pic_env_ptr(pic, env);
|
2016-02-19 05:08:45 -05:00
|
|
|
kh_val(h, it).exports = pic_dict_ptr(pic, exports);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
if (pic->lib) {
|
|
|
|
pic->lib = get_library(pic, old_lib); /* ltable might be rehashed */
|
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-14 23:20:26 -05:00
|
|
|
void
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_in_library(pic_state *pic, const char *lib)
|
2016-02-14 23:20:26 -05:00
|
|
|
{
|
2016-02-18 03:39:32 -05:00
|
|
|
pic->lib = get_library(pic, lib);
|
|
|
|
}
|
2016-02-14 23:20:26 -05:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
bool
|
|
|
|
pic_find_library(pic_state *pic, const char *lib)
|
|
|
|
{
|
|
|
|
return get_library_opt(pic, lib) != NULL;
|
2016-02-14 23:20:26 -05:00
|
|
|
}
|
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
const char *
|
|
|
|
pic_current_library(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-19 13:26:52 -05:00
|
|
|
return pic_str(pic, pic_obj_value(pic->lib->name));
|
2016-02-18 03:39:32 -05:00
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-20 02:33:51 -05:00
|
|
|
pic_value
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_library_environment(pic_state *pic, const char *lib)
|
|
|
|
{
|
2016-02-20 02:33:51 -05:00
|
|
|
return pic_obj_value(get_library(pic, lib)->env);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-06-16 12:52:20 -04:00
|
|
|
void
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_import(pic_state *pic, const char *lib)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-20 01:31:14 -05:00
|
|
|
pic_value name, realname, uid;
|
2016-02-18 10:39:13 -05:00
|
|
|
int it = 0;
|
2016-02-21 06:32:00 -05:00
|
|
|
struct lib *libp;
|
2016-02-18 03:39:32 -05:00
|
|
|
|
|
|
|
libp = get_library(pic, lib);
|
2015-06-09 09:36:04 -04:00
|
|
|
|
2016-02-20 01:31:14 -05:00
|
|
|
while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &name, &realname)) {
|
2016-02-20 02:33:51 -05:00
|
|
|
uid = pic_find_identifier(pic, realname, pic_obj_value(libp->env));
|
2016-02-20 01:31:14 -05:00
|
|
|
if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) {
|
|
|
|
pic_errorf(pic, "attempted to export undefined variable '~s'", realname);
|
2015-06-09 09:36:04 -04:00
|
|
|
}
|
2016-02-20 02:33:51 -05:00
|
|
|
pic_put_identifier(pic, name, uid, pic_obj_value(pic->lib->env));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-20 01:31:14 -05:00
|
|
|
pic_export(pic_state *pic, pic_value name)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-20 01:31:14 -05:00
|
|
|
pic_dict_set(pic, pic_obj_value(pic->lib->exports), name, 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)
|
|
|
|
{
|
2016-02-18 03:39:32 -05:00
|
|
|
const char *lib;
|
2015-06-16 09:51:05 -04:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_get_args(pic, "z", &lib);
|
2015-06-16 09:51:05 -04:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_make_library(pic, lib);
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_undef_value(pic);
|
2015-06-16 09:51:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_lib_find_library(pic_state *pic)
|
|
|
|
{
|
2016-02-18 03:39:32 -05:00
|
|
|
const char *lib;
|
2015-06-16 09:51:05 -04:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_get_args(pic, "z", &lib);
|
2015-06-16 09:51:05 -04:00
|
|
|
|
2016-02-18 06:15:42 -05: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)
|
|
|
|
{
|
2016-02-18 03:39:32 -05:00
|
|
|
const char *lib;
|
2015-08-26 06:04:27 -04:00
|
|
|
int n;
|
2015-06-16 11:03:52 -04:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
n = pic_get_args(pic, "|z", &lib);
|
2015-06-16 11:03:52 -04:00
|
|
|
|
|
|
|
if (n == 0) {
|
2016-02-18 03:39:32 -05:00
|
|
|
return pic_obj_value(pic->lib->name);
|
2015-06-16 11:03:52 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_in_library(pic, lib);
|
2015-06-16 11:03:52 -04:00
|
|
|
|
2016-02-18 06:15:42 -05: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)
|
|
|
|
{
|
2016-02-18 03:39:32 -05:00
|
|
|
const char *lib;
|
2016-02-20 01:34:49 -05:00
|
|
|
pic_value name, alias, realname, uid;
|
2016-02-21 06:32:00 -05:00
|
|
|
struct lib *libp;
|
2016-02-20 01:34:49 -05:00
|
|
|
int n;
|
2015-06-16 11:59:19 -04:00
|
|
|
|
2016-02-20 01:34:49 -05:00
|
|
|
n = pic_get_args(pic, "zm|m", &lib, &name, &alias);
|
2015-06-16 11:59:19 -04:00
|
|
|
|
2016-02-20 01:34:49 -05:00
|
|
|
if (n == 2) {
|
2015-06-16 11:59:19 -04:00
|
|
|
alias = name;
|
|
|
|
}
|
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
libp = get_library(pic, lib);
|
2015-06-16 11:59:19 -04:00
|
|
|
|
2016-02-19 05:08:45 -05:00
|
|
|
if (! pic_dict_has(pic, pic_obj_value(libp->exports), name)) {
|
2016-02-20 01:31:14 -05:00
|
|
|
pic_errorf(pic, "library-import: variable is not exported '~s'", name);
|
2015-06-16 11:59:19 -04:00
|
|
|
} else {
|
2016-02-20 01:31:14 -05:00
|
|
|
realname = pic_dict_ref(pic, pic_obj_value(libp->exports), name);
|
2015-06-16 11:59:19 -04:00
|
|
|
}
|
|
|
|
|
2016-02-20 02:33:51 -05:00
|
|
|
uid = pic_find_identifier(pic, realname, pic_obj_value(libp->env));
|
2016-02-20 01:31:14 -05:00
|
|
|
if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) {
|
|
|
|
pic_errorf(pic, "attempted to export undefined variable '~s'", realname);
|
2015-06-16 11:59:19 -04:00
|
|
|
}
|
|
|
|
|
2016-02-20 02:33:51 -05:00
|
|
|
pic_put_identifier(pic, alias, uid, pic_obj_value(pic->lib->env));
|
2016-02-20 01:31:14 -05:00
|
|
|
|
2016-02-18 06:15:42 -05: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)
|
|
|
|
{
|
2016-02-20 01:31:14 -05:00
|
|
|
pic_value name, alias = pic_false_value(pic);
|
2016-02-20 01:34:49 -05:00
|
|
|
int n;
|
2015-06-16 12:42:44 -04:00
|
|
|
|
2016-02-20 01:34:49 -05:00
|
|
|
n = pic_get_args(pic, "m|m", &name, &alias);
|
2015-06-16 12:42:44 -04:00
|
|
|
|
2016-02-20 01:34:49 -05:00
|
|
|
if (n == 1) {
|
2015-06-16 12:42:44 -04:00
|
|
|
alias = name;
|
|
|
|
}
|
|
|
|
|
2016-02-20 01:31:14 -05:00
|
|
|
pic_dict_set(pic, pic_obj_value(pic->lib->exports), alias, name);
|
2015-06-16 12:42:44 -04:00
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
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)
|
|
|
|
{
|
2016-02-18 03:39:32 -05:00
|
|
|
const char *lib;
|
2016-02-20 01:31:14 -05:00
|
|
|
pic_value sym, exports = pic_nil_value(pic);
|
2016-02-18 10:39:13 -05:00
|
|
|
int it = 0;
|
2016-02-21 06:32:00 -05:00
|
|
|
struct lib *libp;
|
2015-06-16 09:51:05 -04:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_get_args(pic, "z", &lib);
|
2015-06-16 09:51:05 -04:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
libp = get_library(pic, lib);
|
2015-06-16 09:51:05 -04:00
|
|
|
|
2016-02-19 05:08:45 -05:00
|
|
|
while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &sym, NULL)) {
|
2016-02-20 01:31:14 -05:00
|
|
|
pic_push(pic, sym, exports);
|
2015-06-16 09:51:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_lib_library_environment(pic_state *pic)
|
|
|
|
{
|
2016-02-18 03:39:32 -05:00
|
|
|
const char *lib;
|
2015-06-16 09:51:05 -04:00
|
|
|
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_get_args(pic, "z", &lib);
|
2015-06-16 09:51:05 -04:00
|
|
|
|
2016-02-18 03:39:32 -05: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
|
|
|
}
|