2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
|
2015-06-09 07:20:56 -04:00
|
|
|
static void
|
|
|
|
setup_default_env(pic_state *pic, struct pic_env *env)
|
|
|
|
{
|
|
|
|
void pic_define_syntactic_keyword(pic_state *, struct pic_env *, pic_sym *, pic_sym *);
|
|
|
|
|
2015-06-09 12:06:56 -04:00
|
|
|
pic_define_syntactic_keyword(pic, env, pic->sDEFINE_LIBRARY, pic->uDEFINE_LIBRARY);
|
|
|
|
pic_define_syntactic_keyword(pic, env, pic->sIMPORT, pic->uIMPORT);
|
|
|
|
pic_define_syntactic_keyword(pic, env, pic->sEXPORT, pic->uEXPORT);
|
|
|
|
pic_define_syntactic_keyword(pic, env, pic->sCOND_EXPAND, pic->uCOND_EXPAND);
|
2015-06-09 07:20:56 -04:00
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
struct pic_lib *
|
2015-05-30 11:25:40 -04:00
|
|
|
pic_make_library(pic_state *pic, pic_value name)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_lib *lib;
|
2015-05-30 09:39:03 -04:00
|
|
|
struct pic_env *env;
|
2015-01-18 07:32:16 -05:00
|
|
|
struct pic_dict *exports;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
if ((lib = pic_find_library(pic, name)) != NULL) {
|
2015-05-30 11:25:40 -04:00
|
|
|
pic_errorf(pic, "library name already in use: ~s", name);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 07:20:56 -04:00
|
|
|
env = pic_make_env(pic, NULL);
|
2015-01-18 07:32:16 -05:00
|
|
|
exports = pic_make_dict(pic);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-09 07:20:56 -04:00
|
|
|
setup_default_env(pic, env);
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
lib = (struct pic_lib *)pic_obj_alloc(pic, sizeof(struct pic_lib), PIC_TT_LIB);
|
|
|
|
lib->name = name;
|
2015-05-30 09:39:03 -04:00
|
|
|
lib->env = env;
|
2015-01-18 07:32:16 -05:00
|
|
|
lib->exports = exports;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
/* register! */
|
|
|
|
pic->libs = pic_acons(pic, name, pic_obj_value(lib), pic->libs);
|
|
|
|
|
|
|
|
return lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pic_lib *
|
|
|
|
pic_find_library(pic_state *pic, pic_value spec)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
v = pic_assoc(pic, spec, pic->libs, NULL);
|
|
|
|
if (pic_false_p(v)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return pic_lib_ptr(pic_cdr(pic, v));
|
|
|
|
}
|
|
|
|
|
2014-09-14 23:32:20 -04:00
|
|
|
static void
|
2015-01-18 07:55:17 -05:00
|
|
|
import_table(pic_state *pic, pic_value spec, struct pic_dict *imports)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_lib *lib;
|
2015-01-18 07:55:17 -05:00
|
|
|
struct pic_dict *table;
|
2015-01-22 05:28:31 -05:00
|
|
|
pic_value val, tmp, prefix, it;
|
2015-06-09 09:36:04 -04:00
|
|
|
pic_sym *sym, *id, *tag, *nick;
|
2015-01-22 05:33:42 -05:00
|
|
|
xh_entry *iter;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-01-18 07:55:17 -05:00
|
|
|
table = pic_make_dict(pic);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-16 02:02:47 -04:00
|
|
|
if (pic_pair_p(spec) && pic_sym_p(pic_car(pic, spec))) {
|
|
|
|
|
2015-01-20 01:31:17 -05:00
|
|
|
tag = pic_sym_ptr(pic_car(pic, spec));
|
2014-09-16 02:02:47 -04:00
|
|
|
|
|
|
|
if (tag == pic->sONLY) {
|
2015-01-18 07:55:17 -05:00
|
|
|
import_table(pic, pic_cadr(pic, spec), table);
|
|
|
|
|
2015-01-22 05:28:31 -05:00
|
|
|
pic_for_each (val, pic_cddr(pic, spec), it) {
|
2015-01-20 01:31:17 -05:00
|
|
|
pic_dict_set(pic, imports, pic_sym_ptr(val), pic_dict_ref(pic, table, pic_sym_ptr(val)));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-01-18 07:55:17 -05:00
|
|
|
return;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2014-09-16 02:02:47 -04:00
|
|
|
if (tag == pic->sRENAME) {
|
2014-09-14 23:32:20 -04:00
|
|
|
import_table(pic, pic_cadr(pic, spec), imports);
|
2015-01-18 07:55:17 -05:00
|
|
|
|
2015-01-22 05:28:31 -05:00
|
|
|
pic_for_each (val, pic_cddr(pic, spec), it) {
|
2015-01-20 01:31:17 -05:00
|
|
|
tmp = pic_dict_ref(pic, imports, pic_sym_ptr(pic_car(pic, val)));
|
|
|
|
pic_dict_del(pic, imports, pic_sym_ptr(pic_car(pic, val)));
|
|
|
|
pic_dict_set(pic, imports, pic_sym_ptr(pic_cadr(pic, val)), tmp);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-01-18 07:55:17 -05:00
|
|
|
return;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2014-09-16 02:02:47 -04:00
|
|
|
if (tag == pic->sPREFIX) {
|
2015-01-18 07:55:17 -05:00
|
|
|
import_table(pic, pic_cadr(pic, spec), table);
|
|
|
|
|
|
|
|
prefix = pic_list_ref(pic, spec, 2);
|
2015-01-22 05:33:42 -05:00
|
|
|
pic_dict_for_each (sym, table, iter) {
|
2015-01-18 21:08:27 -05:00
|
|
|
id = pic_intern(pic, pic_format(pic, "~s~s", prefix, pic_obj_value(sym)));
|
2015-01-18 07:55:17 -05:00
|
|
|
pic_dict_set(pic, imports, id, pic_dict_ref(pic, table, sym));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-01-18 07:55:17 -05:00
|
|
|
return;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2014-09-16 02:02:47 -04:00
|
|
|
if (tag == pic->sEXCEPT) {
|
2014-09-14 23:32:20 -04:00
|
|
|
import_table(pic, pic_cadr(pic, spec), imports);
|
2015-01-22 05:28:31 -05:00
|
|
|
pic_for_each (val, pic_cddr(pic, spec), it) {
|
2015-01-20 01:31:17 -05:00
|
|
|
pic_dict_del(pic, imports, pic_sym_ptr(val));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-01-18 07:55:17 -05:00
|
|
|
return;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
lib = pic_find_library(pic, spec);
|
|
|
|
if (! lib) {
|
|
|
|
pic_errorf(pic, "library not found: ~a", spec);
|
|
|
|
}
|
2015-06-09 09:36:04 -04:00
|
|
|
pic_dict_for_each (nick, lib->exports, iter) {
|
2015-06-09 13:16:38 -04:00
|
|
|
pic_sym *realname, *uid;
|
2015-06-09 09:36:04 -04:00
|
|
|
|
|
|
|
realname = pic_sym_ptr(pic_dict_ref(pic, lib->exports, nick));
|
|
|
|
|
2015-06-09 13:16:38 -04:00
|
|
|
if ((uid = pic_find_variable(pic, lib->env, pic_obj_value(realname))) == NULL) {
|
2015-06-09 09:36:04 -04:00
|
|
|
pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname));
|
|
|
|
}
|
2015-06-09 13:16:38 -04:00
|
|
|
pic_dict_set(pic, imports, nick, pic_obj_value(uid));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
import(pic_state *pic, pic_value spec)
|
|
|
|
{
|
2015-01-18 07:55:17 -05:00
|
|
|
struct pic_dict *imports;
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *sym;
|
2015-01-22 05:33:42 -05:00
|
|
|
xh_entry *it;
|
2014-09-14 23:32:20 -04:00
|
|
|
|
2015-01-18 07:55:17 -05:00
|
|
|
imports = pic_make_dict(pic);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-01-18 07:55:17 -05:00
|
|
|
import_table(pic, spec, imports);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-01-22 05:33:42 -05:00
|
|
|
pic_dict_for_each (sym, imports, it) {
|
2015-06-09 13:16:38 -04:00
|
|
|
pic_put_variable(pic, pic->lib->env, pic_obj_value(sym), pic_sym_ptr(pic_dict_ref(pic, imports, sym)));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
export(pic_state *pic, pic_value spec)
|
|
|
|
{
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *sRENAME = pic_intern_cstr(pic, "rename");
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value a, b;
|
|
|
|
|
|
|
|
if (pic_sym_p(spec)) { /* (export a) */
|
|
|
|
a = b = spec;
|
|
|
|
} else { /* (export (rename a b)) */
|
|
|
|
if (! pic_list_p(spec))
|
|
|
|
goto fail;
|
2014-09-19 05:28:55 -04:00
|
|
|
if (! (pic_length(pic, spec) == 3))
|
2014-08-25 00:38:09 -04:00
|
|
|
goto fail;
|
2015-01-18 21:08:27 -05:00
|
|
|
if (! pic_eq_p(pic_car(pic, spec), pic_obj_value(sRENAME)))
|
2014-08-25 00:38:09 -04:00
|
|
|
goto fail;
|
|
|
|
if (! pic_sym_p(a = pic_list_ref(pic, spec, 1)))
|
|
|
|
goto fail;
|
|
|
|
if (! pic_sym_p(b = pic_list_ref(pic, spec, 2)))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
2015-06-09 09:49:49 -04:00
|
|
|
printf("* exporting %s as %s\n", pic_symbol_name(pic, pic_sym_ptr(b)), pic_symbol_name(pic, pic_sym_ptr(a)));
|
2014-08-25 00:38:09 -04:00
|
|
|
#endif
|
|
|
|
|
2015-06-09 09:36:04 -04:00
|
|
|
pic_dict_set(pic, pic->lib->exports, pic_sym_ptr(b), a);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
pic_errorf(pic, "illegal export spec: ~s", spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_import(pic_state *pic, pic_value spec)
|
|
|
|
{
|
|
|
|
import(pic, spec);
|
|
|
|
}
|
|
|
|
|
2014-09-14 05:14:23 -04:00
|
|
|
void
|
|
|
|
pic_import_library(pic_state *pic, struct pic_lib *lib)
|
|
|
|
{
|
|
|
|
import(pic, lib->name);
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
void
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_export(pic_state *pic, pic_sym *sym)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-01-18 21:08:27 -05:00
|
|
|
export(pic, pic_obj_value(sym));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_lib_export(pic_state *pic)
|
|
|
|
{
|
|
|
|
size_t argc, i;
|
|
|
|
pic_value *argv;
|
|
|
|
|
|
|
|
pic_get_args(pic, "*", &argc, &argv);
|
|
|
|
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
export(pic, argv[i]);
|
|
|
|
}
|
|
|
|
|
2015-06-09 03:34:45 -04:00
|
|
|
return pic_undef_value();
|
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)
|
|
|
|
{
|
|
|
|
pic_value name;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &name);
|
|
|
|
|
|
|
|
return pic_obj_value(pic_make_library(pic, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_lib_find_library(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value name;
|
|
|
|
struct pic_lib *lib;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &name);
|
|
|
|
|
|
|
|
if ((lib = pic_find_library(pic, name)) == NULL) {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
return pic_obj_value(lib);
|
|
|
|
}
|
|
|
|
|
2015-06-16 11:03:52 -04:00
|
|
|
static pic_value
|
|
|
|
pic_lib_current_library(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value lib;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "|o", &lib);
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return pic_obj_value(pic->lib);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pic_assert_type(pic, lib, lib);
|
|
|
|
|
|
|
|
pic->lib = pic_lib_ptr(lib);
|
|
|
|
|
|
|
|
return pic_undef_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 11:59:19 -04:00
|
|
|
static pic_value
|
|
|
|
pic_lib_library_import(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value lib_opt;
|
|
|
|
pic_sym *name, *realname, *uid, *alias = NULL;
|
|
|
|
struct pic_lib *lib;
|
|
|
|
|
|
|
|
pic_get_args(pic, "om|m", &lib_opt, &name, &alias);
|
|
|
|
|
|
|
|
pic_assert_type(pic, lib_opt, lib);
|
|
|
|
|
|
|
|
if (alias == NULL) {
|
|
|
|
alias = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
lib = pic_lib_ptr(lib_opt);
|
|
|
|
|
|
|
|
if (! pic_dict_has(pic, lib->exports, name)) {
|
|
|
|
pic_errorf(pic, "attempted to import undefined variable '~s'", pic_obj_value(name));
|
|
|
|
} else {
|
|
|
|
realname = pic_sym_ptr(pic_dict_ref(pic, lib->exports, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((uid = pic_find_variable(pic, lib->env, pic_obj_value(realname))) == NULL) {
|
|
|
|
pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname));
|
|
|
|
} else {
|
|
|
|
pic_put_variable(pic, pic->lib->env, pic_obj_value(alias), uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_undef_value();
|
|
|
|
}
|
|
|
|
|
2015-06-16 11:03:52 -04:00
|
|
|
static pic_value
|
|
|
|
pic_lib_library_name(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value lib;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &lib);
|
|
|
|
|
|
|
|
pic_assert_type(pic, lib, lib);
|
|
|
|
|
|
|
|
return pic_lib_ptr(lib)->name;
|
|
|
|
}
|
|
|
|
|
2015-06-16 09:51:05 -04:00
|
|
|
static pic_value
|
|
|
|
pic_lib_library_exports(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value lib, exports = pic_nil_value();
|
|
|
|
pic_sym *sym;
|
|
|
|
xh_entry *it;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &lib);
|
|
|
|
|
|
|
|
pic_assert_type(pic, lib, lib);
|
|
|
|
|
|
|
|
pic_dict_for_each (sym, pic_lib_ptr(lib)->exports, it) {
|
|
|
|
pic_push(pic, pic_obj_value(sym), exports);
|
|
|
|
}
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_lib_library_environment(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value lib;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &lib);
|
|
|
|
|
|
|
|
pic_assert_type(pic, lib, lib);
|
|
|
|
|
|
|
|
return pic_obj_value(pic_lib_ptr(lib)->env);
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
void
|
|
|
|
pic_init_lib(pic_state *pic)
|
|
|
|
{
|
2015-01-20 02:02:28 -05:00
|
|
|
void pic_defmacro(pic_state *, pic_sym *, pic_sym *, pic_func_t);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-09 12:06:56 -04:00
|
|
|
pic_defmacro(pic, pic->sEXPORT, pic->uEXPORT, pic_lib_export);
|
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);
|
2015-06-16 11:03:52 -04:00
|
|
|
pic_defun(pic, "current-library", pic_lib_current_library);
|
2015-06-16 11:59:19 -04:00
|
|
|
pic_defun(pic, "library-import", pic_lib_library_import);
|
2015-06-16 11:03:52 -04:00
|
|
|
pic_defun(pic, "library-name", pic_lib_library_name);
|
2015-06-16 09:51:05 -04:00
|
|
|
pic_defun(pic, "library-exports", pic_lib_library_exports);
|
|
|
|
pic_defun(pic, "library-environment", pic_lib_library_environment);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|