From 44319f8b69fdec663a80e34a840e311948f26ae1 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 6 Feb 2014 14:26:39 +0900 Subject: [PATCH 01/17] update submodule again --- extlib/xfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/xfile b/extlib/xfile index 70abe4ff..5179bfb4 160000 --- a/extlib/xfile +++ b/extlib/xfile @@ -1 +1 @@ -Subproject commit 70abe4ffd48e60b2a7fdeb54ad1a793bc786b27b +Subproject commit 5179bfb4bde3534d320e80f104dde934336ec03c From d6216c54ea1b509aa8038b3b36e649609d3f3ee6 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 6 Feb 2014 21:41:34 +0900 Subject: [PATCH 02/17] update submodule --- extlib/xhash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/xhash b/extlib/xhash index b9d10d4f..f841f50e 160000 --- a/extlib/xhash +++ b/extlib/xhash @@ -1 +1 @@ -Subproject commit b9d10d4fc9bed39c95f0155f520ea1a8b37d70fe +Subproject commit f841f50ed8126c993d22ca4dc2425aab6001e1de From 0ecdb5a260bfbe4e7bbb5429d3f8608ff7006c5a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 6 Feb 2014 22:12:22 +0900 Subject: [PATCH 03/17] update submodule --- extlib/xhash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/xhash b/extlib/xhash index f841f50e..44c9f36d 160000 --- a/extlib/xhash +++ b/extlib/xhash @@ -1 +1 @@ -Subproject commit f841f50ed8126c993d22ca4dc2425aab6001e1de +Subproject commit 44c9f36dca1bbc2b158c812359a7e9d5a5f7e9bb From 104fd823b2216351610f0f3986cbc22b52304396 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 6 Feb 2014 22:13:42 +0900 Subject: [PATCH 04/17] use a pointer to newly allocated region to pass to xh_put --- src/symbol.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/symbol.c b/src/symbol.c index 62057ab7..816e72bc 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -21,6 +21,8 @@ pic_intern_cstr(pic_state *pic, const char *str) return e->val; } + str = pic_strdup(pic, str); + if (pic->slen >= pic->scapa) { #if DEBUG @@ -31,7 +33,7 @@ pic_intern_cstr(pic_state *pic, const char *str) pic->sym_pool = pic_realloc(pic, pic->sym_pool, sizeof(const char *) * pic->scapa); } id = pic->slen++; - pic->sym_pool[id] = pic_strdup(pic, str); + pic->sym_pool[id] = str; xh_put(pic->sym_tbl, str, id); return id; } From a4062b5b769fb117ee196c6342aca419b9d8d940 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 01:08:57 +0900 Subject: [PATCH 05/17] use xh_new_int --- src/codegen.c | 68 +++++++++++++++++++++++++-------------------------- src/lib.c | 2 +- src/macro.c | 37 +++++++++++++++------------- src/state.c | 4 +-- 4 files changed, 57 insertions(+), 54 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index 588b4f56..af10e92a 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -104,7 +104,7 @@ static void pop_scope(analyze_state *); #define register_renamed_symbol(pic, state, slot, lib, name) do { \ struct xh_entry *e; \ - if (! (e = xh_get(lib->senv->tbl, name))) \ + if (! (e = xh_get_int(lib->senv->tbl, pic_intern_cstr(pic, name)))) \ pic_error(pic, "internal error! native VM procedure not found"); \ state->slot = e->val; \ } while (0) @@ -149,7 +149,7 @@ new_analyze_state(pic_state *pic) global_tbl = pic->global_tbl; for (xh_begin(global_tbl, &it); ! xh_isend(&it); xh_next(&it)) { - xh_put(state->scope->var_tbl, it.e->key, 0); + xh_put_int(state->scope->var_tbl, (long)it.e->key, 0); } return state; @@ -171,7 +171,7 @@ push_scope(analyze_state *state, pic_value args) scope = (analyze_scope *)pic_alloc(pic, sizeof(analyze_scope)); scope->up = state->scope; - scope->var_tbl = xh_new(); + scope->var_tbl = xh_new_int(); scope->varg = false; scope->vars = analyze_args(pic, args, &scope->varg, &scope->argc, &scope->localc); @@ -180,7 +180,7 @@ push_scope(analyze_state *state, pic_value args) } for (i = 1; i < scope->argc + scope->localc; ++i) { - xh_put(scope->var_tbl, pic_symbol_name(pic, scope->vars[i]), 0); + xh_put_int(scope->var_tbl, scope->vars[i], 0); } state->scope = scope; @@ -206,14 +206,13 @@ lookup_var(analyze_state *state, pic_sym sym) analyze_scope *scope = state->scope; struct xh_entry *e; int depth = 0; - const char *key = pic_symbol_name(state->pic, sym); enter: - e = xh_get(scope->var_tbl, key); + e = xh_get_int(scope->var_tbl, sym); if (e) { if (depth > 0) { /* mark dirty */ - xh_put(scope->var_tbl, key, 1); + xh_put_int(scope->var_tbl, sym, 1); } return depth; } @@ -230,9 +229,8 @@ define_var(analyze_state *state, pic_sym sym) { pic_state *pic = state->pic; analyze_scope *scope = state->scope; - const char *name = pic_symbol_name(pic, sym); - xh_put(state->scope->var_tbl, name, 0); + xh_put_int(state->scope->var_tbl, sym, 0); scope->localc++; scope->vars = (pic_sym *)pic_realloc(pic, scope->vars, sizeof(pic_sym) * (scope->argc + scope->localc)); @@ -638,7 +636,7 @@ analyze_lambda(analyze_state *state, pic_value obj) closes = pic_nil_value(); for (i = 1; i < scope->argc + scope->localc; ++i) { pic_sym var = scope->vars[i]; - if (xh_get(scope->var_tbl, pic_symbol_name(pic, var))->val == 1) { + if (xh_get_int(scope->var_tbl, var)->val == 1) { closes = pic_cons(pic, pic_symbol_value(var), closes); } } @@ -721,26 +719,26 @@ push_resolver_scope(resolver_state *state, pic_value args, pic_value locals, boo scope = (resolver_scope *)pic_alloc(pic, sizeof(resolver_scope)); scope->up = state->scope; scope->depth = scope->up ? scope->up->depth + 1 : 0; - scope->lvs = xh_new(); - scope->cvs = xh_new(); + scope->lvs = xh_new_int(); + scope->cvs = xh_new_int(); scope->argc = pic_length(pic, args) + 1; scope->localc = pic_length(pic, locals); scope->varg = varg; /* arguments */ for (i = 1; i < scope->argc; ++i) { - xh_put(scope->lvs, pic_symbol_name(pic, pic_sym(pic_list_ref(pic, args, i - 1))), i); + xh_put_int(scope->lvs, pic_sym(pic_list_ref(pic, args, i - 1)), i); } /* locals */ for (i = 0; i < scope->localc; ++i) { - xh_put(scope->lvs, pic_symbol_name(pic, pic_sym(pic_list_ref(pic, locals, i))), scope->argc + i); + xh_put_int(scope->lvs, pic_sym(pic_list_ref(pic, locals, i)), scope->argc + i); } /* closed variables */ scope->cv_num = 0; for (i = 0, c = pic_length(pic, closes); i < c; ++i) { - xh_put(scope->cvs, pic_symbol_name(pic, pic_sym(pic_list_ref(pic, closes, i))), scope->cv_num++); + xh_put_int(scope->cvs, pic_sym(pic_list_ref(pic, closes, i)), scope->cv_num++); } state->scope = scope; @@ -763,18 +761,17 @@ pop_resolver_scope(resolver_state *state) static bool is_closed(resolver_state *state, pic_sym sym) { - return xh_get(state->scope->cvs, pic_symbol_name(state->pic, sym)) != NULL; + return xh_get_int(state->scope->cvs, sym) != NULL; } static pic_value resolve_gref(resolver_state *state, pic_sym sym) { pic_state *pic = state->pic; - const char *name = pic_symbol_name(pic, sym); struct xh_entry *e; size_t i; - if ((e = xh_get(pic->global_tbl, name))) { + if ((e = xh_get_int(pic->global_tbl, sym))) { i = e->val; } else { @@ -782,7 +779,7 @@ resolve_gref(resolver_state *state, pic_sym sym) if (i >= pic->gcapa) { pic_error(pic, "global table overflow"); } - xh_put(pic->global_tbl, name, i); + xh_put_int(pic->global_tbl, sym, i); } return pic_list(pic, 2, pic_symbol_value(state->sGREF), pic_int_value(i)); } @@ -793,7 +790,7 @@ resolve_lref(resolver_state *state, pic_sym sym) pic_state *pic = state->pic; int i; - i = xh_get(state->scope->lvs, pic_symbol_name(pic, sym))->val; + i = xh_get_int(state->scope->lvs, sym)->val; return pic_list(pic, 2, pic_symbol_value(state->sLREF), pic_int_value(i)); } @@ -810,7 +807,7 @@ resolve_cref(resolver_state *state, int depth, pic_sym sym) scope = scope->up; } - i = xh_get(scope->cvs, pic_symbol_name(pic, sym))->val; + i = xh_get_int(scope->cvs, sym)->val; return pic_list(pic, 3, pic_symbol_value(state->sCREF), @@ -993,12 +990,12 @@ push_codegen_context(codegen_state *state, pic_value args, pic_value locals, boo cxt->varg = varg; /* number local variables */ - vars = xh_new(); + vars = xh_new_int(); for (i = 1; i < cxt->argc; ++i) { - xh_put(vars, pic_symbol_name(pic, pic_sym(pic_list_ref(pic, args, i - 1))), i); + xh_put_int(vars, pic_sym(pic_list_ref(pic, args, i - 1)), i); } for (i = 0; i < cxt->localc; ++i) { - xh_put(vars, pic_symbol_name(pic, pic_sym(pic_list_ref(pic, locals, i))), cxt->argc + i); + xh_put_int(vars, pic_sym(pic_list_ref(pic, locals, i)), cxt->argc + i); } /* closed variables */ @@ -1007,7 +1004,7 @@ push_codegen_context(codegen_state *state, pic_value args, pic_value locals, boo for (i = 0, c = pic_length(pic, closes); i < c; ++i) { i = cxt->cv_num++; cxt->cv_tbl = (unsigned *)pic_realloc(pic, cxt->cv_tbl, sizeof(unsigned) * cxt->cv_num); - cxt->cv_tbl[i] = xh_get(vars, pic_symbol_name(pic, pic_sym(pic_list_ref(pic, closes, i))))->val; + cxt->cv_tbl[i] = xh_get_int(vars, pic_sym(pic_list_ref(pic, closes, i)))->val; } xh_destroy(vars); @@ -1439,15 +1436,15 @@ pic_compile(pic_state *pic, pic_value obj) } static int -scope_global_define(pic_state *pic, const char *name) +scope_global_define(pic_state *pic, pic_sym sym) { struct xh_entry *e; - if ((e = xh_get(pic->global_tbl, name))) { + if ((e = xh_get_int(pic->global_tbl, sym))) { pic_warn(pic, "redefining global"); return e->val; } - e = xh_put(pic->global_tbl, name, pic->glen++); + e = xh_put_int(pic->global_tbl, sym, pic->glen++); if (pic->glen >= pic->gcapa) { pic_error(pic, "global table overflow"); } @@ -1458,16 +1455,17 @@ void pic_define(pic_state *pic, const char *name, pic_value val) { int idx; - pic_sym gsym; + pic_sym sym, gsym; - gsym = pic_gensym(pic, pic_intern_cstr(pic, name)); + sym = pic_intern_cstr(pic, name); + gsym = pic_gensym(pic, sym); /* push to the global arena */ - idx = scope_global_define(pic, pic_symbol_name(pic, gsym)); + idx = scope_global_define(pic, gsym); pic->globals[idx] = val; /* register to the senv */ - xh_put(pic->lib->senv->tbl, name, gsym); + xh_put_int(pic->lib->senv->tbl, sym, gsym); /* export! */ pic_export(pic, pic_intern_cstr(pic, name)); @@ -1477,12 +1475,14 @@ static int global_ref(pic_state *pic, const char *name) { struct xh_entry *e; + pic_sym sym; - if (! (e = xh_get(pic->lib->senv->tbl, name))) { + sym = pic_intern_cstr(pic, name); + if (! (e = xh_get_int(pic->lib->senv->tbl, sym))) { pic_error(pic, "symbol not defined"); } assert(e->val >= 0); - if (! (e = xh_get(pic->global_tbl, pic_symbol_name(pic, (pic_sym)e->val)))) { + if (! (e = xh_get_int(pic->global_tbl, e->val))) { pic_abort(pic, "logic flaw"); } return e->val; diff --git a/src/lib.c b/src/lib.c index 683b42bc..de47cae2 100644 --- a/src/lib.c +++ b/src/lib.c @@ -29,7 +29,7 @@ pic_make_library(pic_state *pic, pic_value name) lib = (struct pic_lib *)pic_obj_alloc(pic, sizeof(struct pic_lib), PIC_TT_LIB); lib->senv = senv; - lib->exports = xh_new(); + lib->exports = xh_new_int(); lib->name = name; /* register! */ diff --git a/src/macro.c b/src/macro.c index 2d2c3e85..04f2e65e 100644 --- a/src/macro.c +++ b/src/macro.c @@ -23,7 +23,7 @@ pic_null_syntactic_env(pic_state *pic) senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV); senv->up = NULL; - senv->tbl = xh_new(); + senv->tbl = xh_new_int(); senv->stx = (struct pic_syntax **)pic_calloc(pic, PIC_MACROS_SIZE, sizeof(struct pic_syntax *)); senv->xlen = 0; senv->xcapa = PIC_MACROS_SIZE; @@ -32,8 +32,9 @@ pic_null_syntactic_env(pic_state *pic) } #define register_core_syntax(pic,senv,kind,name) do { \ - senv->stx[senv->xlen] = pic_syntax_new(pic, kind, pic_intern_cstr(pic, name)); \ - xh_put(senv->tbl, name, ~senv->xlen); \ + pic_sym sym = pic_intern_cstr(pic, name); \ + senv->stx[senv->xlen] = pic_syntax_new(pic, kind, sym); \ + xh_put_int(senv->tbl, sym, ~senv->xlen); \ senv->xlen++; \ } while (0) @@ -83,7 +84,7 @@ new_local_senv(pic_state *pic, pic_value formals, struct pic_senv *up) senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV); senv->up = up; - senv->tbl = xh_new(); + senv->tbl = xh_new_int(); senv->stx = NULL; senv->xlen = 0; senv->xcapa = 0; @@ -98,14 +99,14 @@ new_local_senv(pic_state *pic, pic_value formals, struct pic_senv *up) pic_error(pic, "syntax error"); } sym = pic_sym(v); - xh_put(senv->tbl, pic_symbol_name(pic, sym), (int)pic_gensym(pic, sym)); + xh_put_int(senv->tbl, sym, pic_gensym(pic, sym)); } if (! pic_sym_p(a)) { a = macroexpand(pic, a, up); } if (pic_sym_p(a)) { sym = pic_sym(a); - xh_put(senv->tbl, pic_symbol_name(pic, sym), (int)pic_gensym(pic, sym)); + xh_put_int(senv->tbl, sym, pic_gensym(pic, sym)); } else if (! pic_nil_p(a)) { pic_error(pic, "syntax error"); @@ -196,7 +197,7 @@ pic_import(pic_state *pic, pic_value spec) } #endif if (it.e->val >= 0) { - xh_put(pic->lib->senv->tbl, it.e->key, it.e->val); + xh_put_int(pic->lib->senv->tbl, (long)it.e->key, it.e->val); } else { /* syntax object */ size_t idx; @@ -208,7 +209,7 @@ pic_import(pic_state *pic, pic_value spec) } /* bring macro object from imported lib */ senv->stx[idx] = lib->senv->stx[~it.e->val]; - xh_put(senv->tbl, it.e->key, ~idx); + xh_put_int(senv->tbl, (long)it.e->key, ~idx); senv->xlen++; } } @@ -219,28 +220,30 @@ pic_export(pic_state *pic, pic_sym sym) { struct xh_entry *e; - e = xh_get(pic->lib->senv->tbl, pic_symbol_name(pic, sym)); + e = xh_get_int(pic->lib->senv->tbl, sym); if (! e) { pic_error(pic, "symbol not defined"); } - xh_put(pic->lib->exports, e->key, e->val); + xh_put_int(pic->lib->exports, (long)e->key, e->val); } static void pic_defsyntax(pic_state *pic, const char *name, struct pic_proc *macro, struct pic_senv *mac_env) { + pic_sym sym; struct pic_syntax *stx; struct pic_senv *global_senv = pic->lib->senv; size_t idx; - stx = pic_syntax_new_macro(pic, pic_intern_cstr(pic, name), macro, mac_env); + sym = pic_intern_cstr(pic, name); + stx = pic_syntax_new_macro(pic, sym, macro, mac_env); idx = global_senv->xlen; if (idx >= global_senv->xcapa) { pic_abort(pic, "macro table overflow"); } global_senv->stx[idx] = stx; - xh_put(global_senv->tbl, name, ~idx); + xh_put_int(global_senv->tbl, sym, ~idx); global_senv->xlen++; } @@ -276,9 +279,9 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) return expr; } while (true) { - if ((e = xh_get(senv->tbl, pic_symbol_name(pic, pic_sym(expr)))) != NULL) { + if ((e = xh_get_int(senv->tbl, pic_sym(expr))) != NULL) { if (e->val >= 0) - return pic_symbol_value((pic_sym)e->val); + return pic_symbol_value(e->val); else return pic_obj_value(senv->stx[~e->val]); } @@ -287,7 +290,7 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) senv = senv->up; } uniq = pic_gensym(pic, pic_sym(expr)); - xh_put(senv->tbl, pic_symbol_name(pic, pic_sym(expr)), (int)uniq); + xh_put_int(senv->tbl, pic_sym(expr), uniq); return pic_symbol_value(uniq); } case PIC_TT_PAIR: { @@ -475,7 +478,7 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) pic_error(pic, "binding to non-symbol object"); } sym = pic_sym(a); - xh_put(senv->tbl, pic_symbol_name(pic, sym), (int)pic_gensym(pic, sym)); + xh_put_int(senv->tbl, sym, pic_gensym(pic, sym)); /* binding value */ v = pic_cons(pic, pic_symbol_value(pic_syntax(car)->sym), @@ -495,7 +498,7 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) pic_error(pic, "binding to non-symbol object"); } uniq = pic_gensym(pic, pic_sym(var)); - xh_put(senv->tbl, pic_symbol_name(pic, pic_sym(var)), (int)uniq); + xh_put_int(senv->tbl, pic_sym(var), (int)uniq); } FALLTHROUGH; case PIC_STX_SET: diff --git a/src/state.c b/src/state.c index f1a78dc5..d064dc38 100644 --- a/src/state.c +++ b/src/state.c @@ -53,14 +53,14 @@ pic_open(int argc, char *argv[], char **envp) init_heap(pic->heap); /* symbol table */ - pic->sym_tbl = xh_new(); + pic->sym_tbl = xh_new_str(); pic->sym_pool = (const char **)calloc(PIC_SYM_POOL_SIZE, sizeof(const char *)); pic->slen = 0; pic->scapa = pic->slen + PIC_SYM_POOL_SIZE; pic->uniq_sym_count = 0; /* global variables */ - pic->global_tbl = xh_new(); + pic->global_tbl = xh_new_int(); pic->globals = (pic_value *)calloc(PIC_GLOBALS_SIZE, sizeof(pic_value)); pic->glen = 0; pic->gcapa = PIC_GLOBALS_SIZE; From 1901a7ab3005203ec05c1483e4b5921a2e5bcbde Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 01:09:05 +0900 Subject: [PATCH 06/17] e->key is no longer of const char * type --- src/macro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/macro.c b/src/macro.c index 04f2e65e..78936281 100644 --- a/src/macro.c +++ b/src/macro.c @@ -190,10 +190,10 @@ pic_import(pic_state *pic, pic_value spec) for (xh_begin(lib->exports, &it); ! xh_isend(&it); xh_next(&it)) { #if DEBUG if (it.e->val >= 0) { - printf("* importing %s as %s\n", it.e->key, pic_symbol_name(pic, (pic_sym)it.e->val)); + printf("* importing %s as %s\n", pic_symbol_name(pic, (long)it.e->key), pic_symbol_name(pic, it.e->val)); } else { - printf("* importing %s\n", it.e->key); + printf("* importing %s\n", pic_symbol_name(pic, (long)it.e->key)); } #endif if (it.e->val >= 0) { From 53979bf848d33a9380735399959dcddb7ae730a5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 01:15:17 +0900 Subject: [PATCH 07/17] prefer type alias to types with 'struct' --- include/picrin.h | 4 ++-- include/picrin/lib.h | 2 +- include/picrin/macro.h | 2 +- src/codegen.c | 20 ++++++++++---------- src/macro.c | 6 +++--- src/symbol.c | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index f4b7f529..08f01796 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -94,12 +94,12 @@ typedef struct { pic_sym sADD, sSUB, sMUL, sDIV, sMINUS; pic_sym sEQ, sLT, sLE, sGT, sGE, sNOT; - struct xhash *sym_tbl; + xhash *sym_tbl; const char **sym_pool; size_t slen, scapa; int uniq_sym_count; - struct xhash *global_tbl; + xhash *global_tbl; pic_value *globals; size_t glen, gcapa; diff --git a/include/picrin/lib.h b/include/picrin/lib.h index 3d85cf1a..85812788 100644 --- a/include/picrin/lib.h +++ b/include/picrin/lib.h @@ -13,7 +13,7 @@ struct pic_lib { PIC_OBJECT_HEADER pic_value name; struct pic_senv *senv; - struct xhash *exports; + xhash *exports; }; #define pic_lib_ptr(o) ((struct pic_lib *)pic_ptr(o)) diff --git a/include/picrin/macro.h b/include/picrin/macro.h index b02ebf11..3b2f4ab9 100644 --- a/include/picrin/macro.h +++ b/include/picrin/macro.h @@ -13,7 +13,7 @@ struct pic_senv { PIC_OBJECT_HEADER struct pic_senv *up; /* positive for variables, negative for macros (bitwise-not) */ - struct xhash *tbl; + xhash *tbl; struct pic_syntax **stx; size_t xlen, xcapa; }; diff --git a/src/codegen.c b/src/codegen.c index af10e92a..9352807f 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -80,7 +80,7 @@ typedef struct analyze_scope { bool varg; int argc, localc; /* if variable v is captured, then xh_get(var_tbl, v) == 1 */ - struct xhash *var_tbl; + xhash *var_tbl; pic_sym *vars; struct analyze_scope *up; @@ -103,7 +103,7 @@ static void pop_scope(analyze_state *); } while (0) #define register_renamed_symbol(pic, state, slot, lib, name) do { \ - struct xh_entry *e; \ + xh_entry *e; \ if (! (e = xh_get_int(lib->senv->tbl, pic_intern_cstr(pic, name)))) \ pic_error(pic, "internal error! native VM procedure not found"); \ state->slot = e->val; \ @@ -113,8 +113,8 @@ static analyze_state * new_analyze_state(pic_state *pic) { analyze_state *state; - struct xhash *global_tbl; - struct xh_iter it; + xhash *global_tbl; + xh_iter it; struct pic_lib *stdlib; state = (analyze_state *)pic_alloc(pic, sizeof(analyze_state)); @@ -204,7 +204,7 @@ static int lookup_var(analyze_state *state, pic_sym sym) { analyze_scope *scope = state->scope; - struct xh_entry *e; + xh_entry *e; int depth = 0; enter: @@ -667,7 +667,7 @@ typedef struct resolver_scope { int depth; bool varg; int argc, localc; - struct xhash *cvs, *lvs; + xhash *cvs, *lvs; unsigned cv_num; struct resolver_scope *up; @@ -768,7 +768,7 @@ static pic_value resolve_gref(resolver_state *state, pic_sym sym) { pic_state *pic = state->pic; - struct xh_entry *e; + xh_entry *e; size_t i; if ((e = xh_get_int(pic->global_tbl, sym))) { @@ -981,7 +981,7 @@ push_codegen_context(codegen_state *state, pic_value args, pic_value locals, boo pic_state *pic = state->pic; codegen_context *cxt; int i, c; - struct xhash *vars; + xhash *vars; cxt = (codegen_context *)pic_alloc(pic, sizeof(codegen_context)); cxt->up = state->cxt; @@ -1438,7 +1438,7 @@ pic_compile(pic_state *pic, pic_value obj) static int scope_global_define(pic_state *pic, pic_sym sym) { - struct xh_entry *e; + xh_entry *e; if ((e = xh_get_int(pic->global_tbl, sym))) { pic_warn(pic, "redefining global"); @@ -1474,7 +1474,7 @@ pic_define(pic_state *pic, const char *name, pic_value val) static int global_ref(pic_state *pic, const char *name) { - struct xh_entry *e; + xh_entry *e; pic_sym sym; sym = pic_intern_cstr(pic, name); diff --git a/src/macro.c b/src/macro.c index 78936281..818e8921 100644 --- a/src/macro.c +++ b/src/macro.c @@ -181,7 +181,7 @@ void pic_import(pic_state *pic, pic_value spec) { struct pic_lib *lib; - struct xh_iter it; + xh_iter it; lib = pic_find_library(pic, spec); if (! lib) { @@ -218,7 +218,7 @@ pic_import(pic_state *pic, pic_value spec) void pic_export(pic_state *pic, pic_sym sym) { - struct xh_entry *e; + xh_entry *e; e = xh_get_int(pic->lib->senv->tbl, sym); if (! e) { @@ -272,7 +272,7 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) return macroexpand(pic, sc->expr, sc->senv); } case PIC_TT_SYMBOL: { - struct xh_entry *e; + xh_entry *e; pic_sym uniq; if (! pic_interned_p(pic, pic_sym(expr))) { diff --git a/src/symbol.c b/src/symbol.c index 816e72bc..4ed5ba04 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -13,7 +13,7 @@ pic_sym pic_intern_cstr(pic_state *pic, const char *str) { - struct xh_entry *e; + xh_entry *e; pic_sym id; e = xh_get(pic->sym_tbl, str); From faac3f7e9d0a604926fecd5548981f8ed97af75d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 01:24:51 +0900 Subject: [PATCH 08/17] include xhash in picrin.h --- include/picrin.h | 2 ++ src/codegen.c | 1 - src/gc.c | 1 - src/init.c | 1 - src/lib.c | 1 - src/macro.c | 1 - src/state.c | 1 - src/symbol.c | 1 - 8 files changed, 2 insertions(+), 7 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index 08f01796..b34012b7 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -33,6 +33,8 @@ extern "C" { #include #include +#include "xhash/xhash.h" + #if __STDC_VERSION__ >= 201112L # define NORETURN _Noreturn #elif __GNUC__ || __clang__ diff --git a/src/codegen.c b/src/codegen.c index 9352807f..93bf5556 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -11,7 +11,6 @@ #include "picrin/proc.h" #include "picrin/lib.h" #include "picrin/macro.h" -#include "xhash/xhash.h" #if PIC_NONE_IS_FALSE # define OP_PUSHNONE OP_PUSHFALSE diff --git a/src/gc.c b/src/gc.c index bab5505f..8bdd10e0 100644 --- a/src/gc.c +++ b/src/gc.c @@ -15,7 +15,6 @@ #include "picrin/macro.h" #include "picrin/lib.h" #include "picrin/var.h" -#include "xhash/xhash.h" #if GC_DEBUG # include diff --git a/src/init.c b/src/init.c index de8c2bb6..c2b9fdf6 100644 --- a/src/init.c +++ b/src/init.c @@ -9,7 +9,6 @@ #include "picrin/pair.h" #include "picrin/lib.h" #include "picrin/macro.h" -#include "xhash/xhash.h" void pic_init_bool(pic_state *); void pic_init_pair(pic_state *); diff --git a/src/lib.c b/src/lib.c index de47cae2..90e5419a 100644 --- a/src/lib.c +++ b/src/lib.c @@ -6,7 +6,6 @@ #include "picrin/lib.h" #include "picrin/pair.h" #include "picrin/macro.h" -#include "xhash/xhash.h" struct pic_lib * pic_make_library(pic_state *pic, pic_value name) diff --git a/src/macro.c b/src/macro.c index 818e8921..ca84cbfc 100644 --- a/src/macro.c +++ b/src/macro.c @@ -11,7 +11,6 @@ #include "picrin/proc.h" #include "picrin/macro.h" #include "picrin/lib.h" -#include "xhash/xhash.h" static pic_value macroexpand(pic_state *, pic_value, struct pic_senv *); static pic_value macroexpand_list(pic_state *, pic_value, struct pic_senv *); diff --git a/src/state.c b/src/state.c index d064dc38..a431f0cb 100644 --- a/src/state.c +++ b/src/state.c @@ -9,7 +9,6 @@ #include "picrin/proc.h" #include "picrin/macro.h" #include "picrin/cont.h" -#include "xhash/xhash.h" void pic_init_core(pic_state *); diff --git a/src/symbol.c b/src/symbol.c index 4ed5ba04..8cd1f62c 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -8,7 +8,6 @@ #include #include "picrin.h" -#include "xhash/xhash.h" pic_sym pic_intern_cstr(pic_state *pic, const char *str) From e7d38ea165472920dc1e7f8460331558408c9456 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 01:31:45 +0900 Subject: [PATCH 09/17] rename scope_global_define -> global_def --- src/codegen.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index 93bf5556..f4a85c36 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1435,7 +1435,7 @@ pic_compile(pic_state *pic, pic_value obj) } static int -scope_global_define(pic_state *pic, pic_sym sym) +global_def(pic_state *pic, pic_sym sym) { xh_entry *e; @@ -1450,26 +1450,6 @@ scope_global_define(pic_state *pic, pic_sym sym) return e->val; } -void -pic_define(pic_state *pic, const char *name, pic_value val) -{ - int idx; - pic_sym sym, gsym; - - sym = pic_intern_cstr(pic, name); - gsym = pic_gensym(pic, sym); - - /* push to the global arena */ - idx = scope_global_define(pic, gsym); - 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)); -} - static int global_ref(pic_state *pic, const char *name) { @@ -1487,6 +1467,26 @@ global_ref(pic_state *pic, const char *name) return e->val; } +void +pic_define(pic_state *pic, const char *name, pic_value val) +{ + int idx; + pic_sym sym, gsym; + + sym = pic_intern_cstr(pic, name); + gsym = pic_gensym(pic, sym); + + /* push to the global arena */ + idx = global_def(pic, gsym); + 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)); +} + pic_value pic_ref(pic_state *pic, const char *name) { From ae5acd005ca74fa5a6eb634beb6edc802cd870f0 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 01:42:15 +0900 Subject: [PATCH 10/17] refactor global_ref --- src/codegen.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index f4a85c36..be5bdd25 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1458,11 +1458,11 @@ global_ref(pic_state *pic, const char *name) sym = pic_intern_cstr(pic, name); if (! (e = xh_get_int(pic->lib->senv->tbl, sym))) { - pic_error(pic, "symbol not defined"); + return -1; } assert(e->val >= 0); if (! (e = xh_get_int(pic->global_tbl, e->val))) { - pic_abort(pic, "logic flaw"); + return -1; } return e->val; } @@ -1493,6 +1493,9 @@ pic_ref(pic_state *pic, const char *name) int gid; gid = global_ref(pic, name); + if (gid == -1) { + pic_error(pic, "symbol not defined"); + } return pic->globals[gid]; } @@ -1502,6 +1505,9 @@ pic_set(pic_state *pic, const char *name, pic_value value) int gid; gid = global_ref(pic, name); + if (gid == -1) { + pic_error(pic, "symbol not defined"); + } pic->globals[gid] = value; } From c75d6e578918dabba52c97f50b6a544131d306d6 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 01:42:28 +0900 Subject: [PATCH 11/17] 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)); } From 620fee42363d7b36e253406064b6b7628dcca336 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 01:48:56 +0900 Subject: [PATCH 12/17] refactor global_ref and global_def --- src/codegen.c | 56 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index f6932357..482e9222 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -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)); From d74b82b1a5d7a3530262912d662e298982a5d34a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 02:12:43 +0900 Subject: [PATCH 13/17] warn global variable redefinition --- src/codegen.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/codegen.c b/src/codegen.c index 482e9222..33129291 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -228,8 +228,14 @@ define_var(analyze_state *state, pic_sym sym) { pic_state *pic = state->pic; analyze_scope *scope = state->scope; + xh_entry *e; - xh_put_int(state->scope->var_tbl, sym, 0); + if ((e = xh_get_int(scope->var_tbl, sym))) { + pic_warn(pic, "redefining global variable"); + return; + } + + xh_put_int(scope->var_tbl, sym, 0); scope->localc++; scope->vars = (pic_sym *)pic_realloc(pic, scope->vars, sizeof(pic_sym) * (scope->argc + scope->localc)); From fd0553ac25e578b1c2330d5843f1d70a2d914452 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 02:13:16 +0900 Subject: [PATCH 14/17] warn variable redefinition in macroexpansion level --- src/macro.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/macro.c b/src/macro.c index ca84cbfc..69ce6c87 100644 --- a/src/macro.c +++ b/src/macro.c @@ -497,6 +497,9 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) pic_error(pic, "binding to non-symbol object"); } uniq = pic_gensym(pic, pic_sym(var)); + if (xh_get_int(senv->tbl, pic_sym(var)) != NULL) { + pic_warn(pic, "redefining variable"); + } xh_put_int(senv->tbl, pic_sym(var), (int)uniq); } FALLTHROUGH; From c4040949ff5c288a0cc41e752d1ebf0e6f96c02b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 02:14:39 +0900 Subject: [PATCH 15/17] stop warning in macroexpand, and creating a duplicate slot --- src/macro.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/macro.c b/src/macro.c index 69ce6c87..b4e91bb0 100644 --- a/src/macro.c +++ b/src/macro.c @@ -496,11 +496,11 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) if (! pic_sym_p(var)) { pic_error(pic, "binding to non-symbol object"); } - uniq = pic_gensym(pic, pic_sym(var)); - if (xh_get_int(senv->tbl, pic_sym(var)) != NULL) { - pic_warn(pic, "redefining variable"); + /* do not make duplicate variable slot*/ + if (xh_get_int(senv->tbl, pic_sym(var)) == NULL) { + uniq = pic_gensym(pic, pic_sym(var)); + xh_put_int(senv->tbl, pic_sym(var), (int)uniq); } - xh_put_int(senv->tbl, pic_sym(var), (int)uniq); } FALLTHROUGH; case PIC_STX_SET: From 7288cd0614a6b16d762b72df815808feb07cc264 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 02:15:08 +0900 Subject: [PATCH 16/17] redefinition might be against a local variable --- src/codegen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen.c b/src/codegen.c index 33129291..7e36dcb6 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -231,7 +231,7 @@ define_var(analyze_state *state, pic_sym sym) xh_entry *e; if ((e = xh_get_int(scope->var_tbl, sym))) { - pic_warn(pic, "redefining global variable"); + pic_warn(pic, "redefining variable"); return; } From 1c633b3cda998b8c4ae536992609b742016ab918 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 7 Feb 2014 02:23:55 +0900 Subject: [PATCH 17/17] cleanup --- src/macro.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/macro.c b/src/macro.c index b4e91bb0..3865d702 100644 --- a/src/macro.c +++ b/src/macro.c @@ -455,29 +455,28 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) return v; } case PIC_STX_DEFINE: { - pic_sym uniq; - pic_value var; + pic_sym var; + pic_value formals; if (pic_length(pic, expr) < 2) { pic_error(pic, "syntax error"); } - var = pic_cadr(pic, expr); - if (pic_pair_p(var)) { - struct pic_senv *in = new_local_senv(pic, pic_cdr(pic, var), senv); + formals = pic_cadr(pic, expr); + if (pic_pair_p(formals)) { + struct pic_senv *in = new_local_senv(pic, pic_cdr(pic, formals), senv); pic_value a; - pic_sym sym; /* defined symbol */ - a = pic_car(pic, var); + a = pic_car(pic, formals); if (! pic_sym_p(a)) { a = macroexpand(pic, a, senv); } if (! pic_sym_p(a)) { pic_error(pic, "binding to non-symbol object"); } - sym = pic_sym(a); - xh_put_int(senv->tbl, sym, pic_gensym(pic, sym)); + var = pic_sym(a); + xh_put_int(senv->tbl, var, pic_gensym(pic, var)); /* binding value */ v = pic_cons(pic, pic_symbol_value(pic_syntax(car)->sym), @@ -490,16 +489,16 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) return v; } - if (! pic_sym_p(var)) { - var = macroexpand(pic, var, senv); + if (! pic_sym_p(formals)) { + formals = macroexpand(pic, formals, senv); } - if (! pic_sym_p(var)) { + if (! pic_sym_p(formals)) { pic_error(pic, "binding to non-symbol object"); } - /* do not make duplicate variable slot*/ - if (xh_get_int(senv->tbl, pic_sym(var)) == NULL) { - uniq = pic_gensym(pic, pic_sym(var)); - xh_put_int(senv->tbl, pic_sym(var), (int)uniq); + var = pic_sym(formals); + /* do not make duplicate variable slot */ + if (xh_get_int(senv->tbl, var) == NULL) { + xh_put_int(senv->tbl, var, pic_gensym(pic, var)); } } FALLTHROUGH;