use dictionary for pic->globals

This commit is contained in:
Yuichi Nishiwaki 2017-04-02 22:19:11 +09:00
parent d319a57422
commit 408bf4cf48
3 changed files with 10 additions and 10 deletions

View File

@ -460,10 +460,10 @@ normalize(pic_state *pic, pic_value expr, pic_value locals, bool in)
var = pic_list_ref(pic, expr, 1);
if (! in) { /* global */
if (pic_weak_has(pic, pic->globals, var)) {
if (pic_dict_has(pic, pic->globals, var)) {
pic_warnf(pic, "redefining variable: %s", pic_sym(pic, var));
}
pic_weak_set(pic, pic->globals, var, pic_invalid_value(pic));
pic_dict_set(pic, pic->globals, var, pic_invalid_value(pic));
} else { /* local */
bool found = false;

View File

@ -211,7 +211,7 @@ pic_open(pic_allocf allocf, void *userdata)
pic->err = pic_invalid_value(pic);
/* root tables */
pic->globals = pic_make_weak(pic);
pic->globals = pic_make_dict(pic);
pic->macros = pic_make_weak(pic);
pic->dyn_env = pic_list(pic, 1, pic_make_weak(pic));
@ -273,11 +273,11 @@ pic_global_ref(pic_state *pic, pic_value sym)
{
pic_value val;
if (! pic_weak_has(pic, pic->globals, sym)) {
if (! pic_dict_has(pic, pic->globals, sym)) {
printf("%s\n", pic_str(pic, pic_sym_name(pic, sym), 0));
pic_error(pic, "undefined variable", 1, sym);
}
val = pic_weak_ref(pic, pic->globals, sym);;
val = pic_dict_ref(pic, pic->globals, sym);
if (pic_invalid_p(pic, val)) {
pic_error(pic, "uninitialized global variable", 1, sym);
}
@ -287,10 +287,10 @@ pic_global_ref(pic_state *pic, pic_value sym)
void
pic_global_set(pic_state *pic, pic_value sym, pic_value value)
{
if (! pic_weak_has(pic, pic->globals, sym)) {
if (! pic_dict_has(pic, pic->globals, sym)) {
pic_error(pic, "undefined variable", 1, sym);
}
pic_weak_set(pic, pic->globals, sym, value);
pic_dict_set(pic, pic->globals, sym, value);
}
pic_value
@ -310,10 +310,10 @@ pic_define(pic_state *pic, const char *name, pic_value val)
{
pic_value sym = pic_intern_cstr(pic, name);
if (pic_weak_has(pic, pic->globals, sym)) {
if (pic_dict_has(pic, pic->globals, sym)) {
pic_warnf(pic, "redefining variable: %s", pic_str(pic, pic_sym_name(pic, sym), NULL));
}
pic_weak_set(pic, pic->globals, sym, val);
pic_dict_set(pic, pic->globals, sym, val);
}
void

View File

@ -45,7 +45,7 @@ struct pic_state {
khash_t(oblist) oblist; /* string to symbol */
int ucnt;
pic_value globals; /* weak */
pic_value globals; /* dict */
pic_value macros; /* weak */
bool gc_enable;