From c75d6e578918dabba52c97f50b6a544131d306d6 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 01:42:28 +0900 Subject: [PATCH] refactor global_def --- src/codegen.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index be5bdd25..f6932357 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1437,17 +1437,22 @@ pic_compile(pic_state *pic, pic_value obj) static int global_def(pic_state *pic, pic_sym sym) { - xh_entry *e; + pic_sym gsym; + size_t gidx; - if ((e = xh_get_int(pic->global_tbl, sym))) { - pic_warn(pic, "redefining global"); - return e->val; - } - e = xh_put_int(pic->global_tbl, sym, pic->glen++); + 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"); } - return e->val; + xh_put_int(pic->global_tbl, gsym, gidx); + + return gidx; } static int @@ -1471,18 +1476,14 @@ void pic_define(pic_state *pic, const char *name, pic_value val) { int idx; - pic_sym sym, gsym; + pic_sym sym; sym = pic_intern_cstr(pic, name); - gsym = pic_gensym(pic, sym); /* push to the global arena */ - idx = global_def(pic, gsym); + idx = global_def(pic, sym); pic->globals[idx] = val; - /* register to the senv */ - xh_put_int(pic->lib->senv->tbl, sym, gsym); - /* export! */ pic_export(pic, pic_intern_cstr(pic, name)); }