pic->lib is now of string type

This commit is contained in:
Yuichi Nishiwaki 2016-02-24 03:59:51 +09:00
parent 17d75f915f
commit 29d7a7fc48
3 changed files with 37 additions and 41 deletions

View File

@ -50,7 +50,7 @@ struct pic_state {
struct code *ip; struct code *ip;
struct lib *lib; const char *lib;
pic_value features; pic_value features;

View File

@ -160,15 +160,10 @@ void
pic_make_library(pic_state *pic, const char *lib) pic_make_library(pic_state *pic, const char *lib)
{ {
khash_t(ltable) *h = &pic->ltable; khash_t(ltable) *h = &pic->ltable;
const char *old_lib = NULL;
pic_value name, env, exports; pic_value name, env, exports;
int it; int it;
int ret; int ret;
if (pic->lib) {
old_lib = pic_current_library(pic);
}
name = pic_cstr_value(pic, lib); name = pic_cstr_value(pic, lib);
env = make_library_env(pic, name); env = make_library_env(pic, name);
exports = pic_make_dict(pic); 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).name = pic_str_ptr(pic, name);
kh_val(h, it).env = pic_env_ptr(pic, env); kh_val(h, it).env = pic_env_ptr(pic, env);
kh_val(h, it).exports = pic_dict_ptr(pic, exports); 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 void
pic_in_library(pic_state *pic, const char *lib) pic_in_library(pic_state *pic, const char *lib)
{ {
pic->lib = get_library(pic, lib); get_library(pic, lib);
pic->lib = lib;
} }
bool bool
@ -202,7 +194,7 @@ pic_find_library(pic_state *pic, const char *lib)
const char * const char *
pic_current_library(pic_state *pic) pic_current_library(pic_state *pic)
{ {
return pic_str(pic, pic_obj_value(pic->lib->name)); return pic->lib;
} }
pic_value pic_value
@ -216,23 +208,24 @@ pic_import(pic_state *pic, const char *lib)
{ {
pic_value name, realname, uid; pic_value name, realname, uid;
int it = 0; 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)) { while (pic_dict_next(pic, pic_obj_value(their->exports), &it, &name, &realname)) {
uid = pic_find_identifier(pic, realname, pic_obj_value(libp->env)); 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)) { 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_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 void
pic_export(pic_state *pic, pic_value name) 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 static pic_value
@ -266,7 +259,7 @@ pic_lib_current_library(pic_state *pic)
n = pic_get_args(pic, "|z", &lib); n = pic_get_args(pic, "|z", &lib);
if (n == 0) { if (n == 0) {
return pic_obj_value(pic->lib->name); return pic_cstr_value(pic, pic_current_library(pic));
} }
else { else {
pic_in_library(pic, lib); 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_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); return pic_undef_value(pic);
} }
@ -319,7 +312,7 @@ pic_lib_library_export(pic_state *pic)
alias = name; 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); return pic_undef_value(pic);
} }

View File

@ -7,9 +7,19 @@
#include "picrin/private/object.h" #include "picrin/private/object.h"
#include "picrin/private/state.h" #include "picrin/private/state.h"
static pic_value
pic_features(pic_state *pic)
{
pic_get_args(pic, "");
return pic->features;
}
static void static void
pic_init_features(pic_state *pic) pic_init_features(pic_state *pic)
{ {
pic_defun(pic, "features", pic_features);
pic_add_feature(pic, "picrin"); pic_add_feature(pic, "picrin");
#if __STDC_IEC_559__ #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); 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 { \ #define import_builtin_syntax(name) do { \
pic_value nick, real; \ pic_value nick, real; \
nick = pic_intern_lit(pic, "builtin:" name); \ nick = pic_intern_lit(pic, "builtin:" name); \
real = pic_intern_lit(pic, name); \ real = pic_intern_lit(pic, name); \
pic_put_identifier(pic, nick, real, pic_obj_value(pic->lib->env)); \ pic_put_identifier(pic, nick, real, env); \
} while (0) } while (0)
#define declare_vm_procedure(name) do { \ #define declare_vm_procedure(name) do { \
pic_value sym; \ pic_value sym; \
sym = pic_intern_lit(pic, name); \ sym = pic_intern_lit(pic, name); \
pic_put_identifier(pic, sym, sym, pic_obj_value(pic->lib->env)); \ pic_put_identifier(pic, sym, sym, env); \
} while (0) } while (0)
void pic_init_bool(pic_state *); void pic_init_bool(pic_state *);
@ -119,8 +121,7 @@ static void
pic_init_core(pic_state *pic) pic_init_core(pic_state *pic)
{ {
size_t ai; size_t ai;
pic_value env;
pic_init_features(pic);
pic_deflibrary(pic, "picrin.base"); pic_deflibrary(pic, "picrin.base");
@ -128,6 +129,8 @@ pic_init_core(pic_state *pic)
#define DONE pic_leave(pic, ai); #define DONE pic_leave(pic, ai);
env = pic_library_environment(pic, pic->lib);
import_builtin_syntax("define"); import_builtin_syntax("define");
import_builtin_syntax("set!"); import_builtin_syntax("set!");
import_builtin_syntax("quote"); import_builtin_syntax("quote");
@ -179,7 +182,7 @@ pic_init_core(pic_state *pic)
pic_init_write(pic); DONE; pic_init_write(pic); DONE;
#endif #endif
pic_defun(pic, "features", pic_features); pic_init_features(pic);
pic_load_cstr(pic, &pic_boot[0][0]); pic_load_cstr(pic, &pic_boot[0][0]);
} }