From 499b68c835c47f894c1ae1bfea83785df5cf2b2e Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 22 Jan 2015 19:33:42 +0900 Subject: [PATCH] change pic_dict_for_each interface --- extlib/benz/codegen.c | 3 ++- extlib/benz/include/picrin/dict.h | 9 +++------ extlib/benz/lib.c | 8 +++++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/extlib/benz/codegen.c b/extlib/benz/codegen.c index 91e0a4f0..c0d87419 100644 --- a/extlib/benz/codegen.c +++ b/extlib/benz/codegen.c @@ -64,6 +64,7 @@ new_analyze_state(pic_state *pic) { analyze_state *state; pic_sym *sym; + xh_entry *it; state = pic_alloc(pic, sizeof(analyze_state)); state->pic = pic; @@ -92,7 +93,7 @@ new_analyze_state(pic_state *pic) /* push initial scope */ push_scope(state, pic_nil_value()); - pic_dict_for_each (sym, pic->globals) { + pic_dict_for_each (sym, pic->globals, it) { xv_push(&state->scope->locals, &sym); } diff --git a/extlib/benz/include/picrin/dict.h b/extlib/benz/include/picrin/dict.h index 13379bf5..4a3bd7ce 100644 --- a/extlib/benz/include/picrin/dict.h +++ b/extlib/benz/include/picrin/dict.h @@ -19,12 +19,9 @@ struct pic_dict { struct pic_dict *pic_make_dict(pic_state *); -#define pic_dict_for_each(sym, dict) \ - pic_dict_for_each_helper_((sym), PIC_GENSYM(tmp), (dict)) -#define pic_dict_for_each_helper_(var, tmp, dict) \ - for (xh_entry *tmp = xh_begin(&dict->hash); \ - (tmp && ((var = xh_key(tmp, pic_sym *)), 1)); \ - tmp = xh_next(tmp)) +#define pic_dict_for_each(sym, dict, it) \ + for (it = xh_begin(&(dict)->hash); it != NULL; it = xh_next(it)) \ + if ((sym = xh_key(it, pic_sym *)), true) pic_value pic_dict_ref(pic_state *, struct pic_dict *, pic_sym *); void pic_dict_set(pic_state *, struct pic_dict *, pic_sym *, pic_value); diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 0c4ad001..30362ef2 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -75,6 +75,7 @@ import_table(pic_state *pic, pic_value spec, struct pic_dict *imports) struct pic_dict *table; pic_value val, tmp, prefix, it; pic_sym *sym, *id, *tag; + xh_entry *iter; table = pic_make_dict(pic); @@ -104,7 +105,7 @@ import_table(pic_state *pic, pic_value spec, struct pic_dict *imports) import_table(pic, pic_cadr(pic, spec), table); prefix = pic_list_ref(pic, spec, 2); - pic_dict_for_each (sym, table) { + pic_dict_for_each (sym, table, iter) { id = pic_intern(pic, pic_format(pic, "~s~s", prefix, pic_obj_value(sym))); pic_dict_set(pic, imports, id, pic_dict_ref(pic, table, sym)); } @@ -122,7 +123,7 @@ import_table(pic_state *pic, pic_value spec, struct pic_dict *imports) if (! lib) { pic_errorf(pic, "library not found: ~a", spec); } - pic_dict_for_each (sym, lib->exports) { + pic_dict_for_each (sym, lib->exports, iter) { pic_dict_set(pic, imports, sym, pic_dict_ref(pic, lib->exports, sym)); } } @@ -132,12 +133,13 @@ import(pic_state *pic, pic_value spec) { struct pic_dict *imports; pic_sym *sym; + xh_entry *it; imports = pic_make_dict(pic); import_table(pic, spec, imports); - pic_dict_for_each (sym, imports) { + pic_dict_for_each (sym, imports, it) { pic_put_rename(pic, pic->lib->env, sym, pic_sym_ptr(pic_dict_ref(pic, imports, sym))); } }