diff --git a/extlib/benz/include/picrin/private/state.h b/extlib/benz/include/picrin/private/state.h index 9a83559a..27c9f560 100644 --- a/extlib/benz/include/picrin/private/state.h +++ b/extlib/benz/include/picrin/private/state.h @@ -50,7 +50,7 @@ struct pic_state { struct code *ip; - struct lib *lib; + const char *lib; pic_value features; diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index b65fa3b0..97881cb1 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -160,15 +160,10 @@ void pic_make_library(pic_state *pic, const char *lib) { khash_t(ltable) *h = &pic->ltable; - const char *old_lib = NULL; pic_value name, env, exports; int it; int ret; - if (pic->lib) { - old_lib = pic_current_library(pic); - } - name = pic_cstr_value(pic, lib); env = make_library_env(pic, name); exports = pic_make_dict(pic); @@ -181,16 +176,13 @@ pic_make_library(pic_state *pic, const char *lib) kh_val(h, it).name = pic_str_ptr(pic, name); kh_val(h, it).env = pic_env_ptr(pic, env); kh_val(h, it).exports = pic_dict_ptr(pic, exports); - - if (pic->lib) { - pic->lib = get_library(pic, old_lib); /* ltable might be rehashed */ - } } void pic_in_library(pic_state *pic, const char *lib) { - pic->lib = get_library(pic, lib); + get_library(pic, lib); + pic->lib = lib; } bool @@ -202,7 +194,7 @@ pic_find_library(pic_state *pic, const char *lib) const char * pic_current_library(pic_state *pic) { - return pic_str(pic, pic_obj_value(pic->lib->name)); + return pic->lib; } pic_value @@ -216,23 +208,24 @@ pic_import(pic_state *pic, const char *lib) { pic_value name, realname, uid; int it = 0; - struct lib *libp; + struct lib *our, *their; - libp = get_library(pic, lib); + our = get_library(pic, pic->lib); + their = get_library(pic, lib); - while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &name, &realname)) { - uid = pic_find_identifier(pic, realname, pic_obj_value(libp->env)); + while (pic_dict_next(pic, pic_obj_value(their->exports), &it, &name, &realname)) { + uid = pic_find_identifier(pic, realname, pic_obj_value(their->env)); if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) { pic_error(pic, "attempted to export undefined variable", 1, realname); } - pic_put_identifier(pic, name, uid, pic_obj_value(pic->lib->env)); + pic_put_identifier(pic, name, uid, pic_obj_value(our->env)); } } void pic_export(pic_state *pic, pic_value name) { - pic_dict_set(pic, pic_obj_value(pic->lib->exports), name, name); + pic_dict_set(pic, pic_obj_value(get_library(pic, pic->lib)->exports), name, name); } static pic_value @@ -266,7 +259,7 @@ pic_lib_current_library(pic_state *pic) n = pic_get_args(pic, "|z", &lib); if (n == 0) { - return pic_obj_value(pic->lib->name); + return pic_cstr_value(pic, pic_current_library(pic)); } else { pic_in_library(pic, lib); @@ -302,7 +295,7 @@ pic_lib_library_import(pic_state *pic) pic_error(pic, "attempted to export undefined variable", 1, realname); } - pic_put_identifier(pic, alias, uid, pic_obj_value(pic->lib->env)); + pic_put_identifier(pic, alias, uid, pic_obj_value(get_library(pic, pic->lib)->env)); return pic_undef_value(pic); } @@ -319,7 +312,7 @@ pic_lib_library_export(pic_state *pic) alias = name; } - pic_dict_set(pic, pic_obj_value(pic->lib->exports), alias, name); + pic_dict_set(pic, pic_obj_value(get_library(pic, pic->lib)->exports), alias, name); return pic_undef_value(pic); } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index f35f4f0e..0ce27179 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -7,9 +7,19 @@ #include "picrin/private/object.h" #include "picrin/private/state.h" +static pic_value +pic_features(pic_state *pic) +{ + pic_get_args(pic, ""); + + return pic->features; +} + static void pic_init_features(pic_state *pic) { + pic_defun(pic, "features", pic_features); + pic_add_feature(pic, "picrin"); #if __STDC_IEC_559__ @@ -71,25 +81,17 @@ pic_add_feature(pic_state *pic, const char *feature) pic_push(pic, pic_intern_cstr(pic, feature), pic->features); } -static pic_value -pic_features(pic_state *pic) -{ - pic_get_args(pic, ""); - - return pic->features; -} - -#define import_builtin_syntax(name) do { \ - pic_value nick, real; \ - nick = pic_intern_lit(pic, "builtin:" name); \ - real = pic_intern_lit(pic, name); \ - pic_put_identifier(pic, nick, real, pic_obj_value(pic->lib->env)); \ +#define import_builtin_syntax(name) do { \ + pic_value nick, real; \ + nick = pic_intern_lit(pic, "builtin:" name); \ + real = pic_intern_lit(pic, name); \ + pic_put_identifier(pic, nick, real, env); \ } while (0) -#define declare_vm_procedure(name) do { \ - pic_value sym; \ - sym = pic_intern_lit(pic, name); \ - pic_put_identifier(pic, sym, sym, pic_obj_value(pic->lib->env)); \ +#define declare_vm_procedure(name) do { \ + pic_value sym; \ + sym = pic_intern_lit(pic, name); \ + pic_put_identifier(pic, sym, sym, env); \ } while (0) void pic_init_bool(pic_state *); @@ -119,8 +121,7 @@ static void pic_init_core(pic_state *pic) { size_t ai; - - pic_init_features(pic); + pic_value env; pic_deflibrary(pic, "picrin.base"); @@ -128,6 +129,8 @@ pic_init_core(pic_state *pic) #define DONE pic_leave(pic, ai); + env = pic_library_environment(pic, pic->lib); + import_builtin_syntax("define"); import_builtin_syntax("set!"); import_builtin_syntax("quote"); @@ -179,7 +182,7 @@ pic_init_core(pic_state *pic) pic_init_write(pic); DONE; #endif - pic_defun(pic, "features", pic_features); + pic_init_features(pic); pic_load_cstr(pic, &pic_boot[0][0]); }