refactor global_ref and global_def

This commit is contained in:
Yuichi Nishiwaki 2014-02-07 01:48:56 +09:00
parent c75d6e5789
commit 620fee4236
1 changed files with 28 additions and 28 deletions

View File

@ -1434,27 +1434,6 @@ pic_compile(pic_state *pic, pic_value obj)
return proc;
}
static int
global_def(pic_state *pic, pic_sym sym)
{
pic_sym gsym;
size_t gidx;
gsym = pic_gensym(pic, sym);
/* register to the senv */
xh_put_int(pic->lib->senv->tbl, sym, gsym);
/* register to the global table */
gidx = pic->glen++;
if (pic->glen >= pic->gcapa) {
pic_error(pic, "global table overflow");
}
xh_put_int(pic->global_tbl, gsym, gidx);
return gidx;
}
static int
global_ref(pic_state *pic, const char *name)
{
@ -1472,17 +1451,38 @@ global_ref(pic_state *pic, const char *name)
return e->val;
}
static int
global_def(pic_state *pic, const char *name)
{
pic_sym sym, gsym;
size_t gidx;
sym = pic_intern_cstr(pic, name);
if ((gidx = global_ref(pic, name)) != -1) {
pic_warn(pic, "redefining global");
return gidx;
}
gsym = pic_gensym(pic, sym);
/* register to the senv */
xh_put_int(pic->lib->senv->tbl, sym, gsym);
/* register to the global table */
gidx = pic->glen++;
if (pic->glen >= pic->gcapa) {
pic_error(pic, "global table overflow");
}
xh_put_int(pic->global_tbl, gsym, gidx);
return gidx;
}
void
pic_define(pic_state *pic, const char *name, pic_value val)
{
int idx;
pic_sym sym;
sym = pic_intern_cstr(pic, name);
/* push to the global arena */
idx = global_def(pic, sym);
pic->globals[idx] = val;
pic->globals[global_def(pic, name)] = val;
/* export! */
pic_export(pic, pic_intern_cstr(pic, name));