use dictionaries for temporary import table

This commit is contained in:
Yuichi Nishiwaki 2015-01-18 21:55:17 +09:00
parent 14a93ccf0f
commit c1bba1ec83
1 changed files with 29 additions and 38 deletions

View File

@ -68,51 +68,53 @@ pic_find_library(pic_state *pic, pic_value spec)
} }
static void static void
import_table(pic_state *pic, pic_value spec, xhash *imports) import_table(pic_state *pic, pic_value spec, struct pic_dict *imports)
{ {
struct pic_lib *lib; struct pic_lib *lib;
xhash table; struct pic_dict *table;
pic_value val; pic_value val, tmp, prefix;
pic_sym sym, id, tag; pic_sym sym, id, tag;
xh_entry *it;
xh_init_int(&table, sizeof(pic_sym)); table = pic_make_dict(pic);
if (pic_pair_p(spec) && pic_sym_p(pic_car(pic, spec))) { if (pic_pair_p(spec) && pic_sym_p(pic_car(pic, spec))) {
tag = pic_sym(pic_car(pic, spec)); tag = pic_sym(pic_car(pic, spec));
if (tag == pic->sONLY) { if (tag == pic->sONLY) {
import_table(pic, pic_cadr(pic, spec), &table); import_table(pic, pic_cadr(pic, spec), table);
pic_for_each (val, pic_cddr(pic, spec)) { pic_for_each (val, pic_cddr(pic, spec)) {
xh_put_int(imports, pic_sym(val), &xh_val(xh_get_int(&table, pic_sym(val)), pic_sym)); pic_dict_set(pic, imports, pic_sym(val), pic_dict_ref(pic, table, pic_sym(val)));
} }
goto exit; return;
} }
if (tag == pic->sRENAME) { if (tag == pic->sRENAME) {
import_table(pic, pic_cadr(pic, spec), imports); import_table(pic, pic_cadr(pic, spec), imports);
pic_for_each (val, pic_cddr(pic, spec)) { pic_for_each (val, pic_cddr(pic, spec)) {
id = xh_val(xh_get_int(imports, pic_sym(pic_car(pic, val))), pic_sym); tmp = pic_dict_ref(pic, imports, pic_sym(pic_car(pic, val)));
xh_del_int(imports, pic_sym(pic_car(pic, val))); pic_dict_del(pic, imports, pic_sym(pic_car(pic, val)));
xh_put_int(imports, pic_sym(pic_cadr(pic, val)), &id); pic_dict_set(pic, imports, pic_sym(pic_cadr(pic, val)), tmp);
} }
goto exit; return;
} }
if (tag == pic->sPREFIX) { if (tag == pic->sPREFIX) {
import_table(pic, pic_cadr(pic, spec), &table); import_table(pic, pic_cadr(pic, spec), table);
for (it = xh_begin(&table); it != NULL; it = xh_next(it)) {
val = pic_list_ref(pic, spec, 2); prefix = pic_list_ref(pic, spec, 2);
sym = pic_intern_str(pic, pic_format(pic, "~s~s", val, pic_sym_value(xh_key(it, pic_sym)))); pic_dict_for_each (sym, table) {
xh_put_int(imports, sym, &xh_val(it, pic_sym)); id = pic_intern_str(pic, pic_format(pic, "~s~s", prefix, pic_sym_value(sym)));
pic_dict_set(pic, imports, id, pic_dict_ref(pic, table, sym));
} }
goto exit; return;
} }
if (tag == pic->sEXCEPT) { if (tag == pic->sEXCEPT) {
import_table(pic, pic_cadr(pic, spec), imports); import_table(pic, pic_cadr(pic, spec), imports);
pic_for_each (val, pic_cddr(pic, spec)) { pic_for_each (val, pic_cddr(pic, spec)) {
xh_del_int(imports, pic_sym(val)); pic_dict_del(pic, imports, pic_sym(val));
} }
goto exit; return;
} }
} }
lib = pic_find_library(pic, spec); lib = pic_find_library(pic, spec);
@ -120,34 +122,23 @@ import_table(pic_state *pic, pic_value spec, xhash *imports)
pic_errorf(pic, "library not found: ~a", spec); pic_errorf(pic, "library not found: ~a", spec);
} }
pic_dict_for_each (sym, lib->exports) { pic_dict_for_each (sym, lib->exports) {
id = pic_sym(pic_dict_ref(pic, lib->exports, sym)); pic_dict_set(pic, imports, sym, pic_dict_ref(pic, lib->exports, sym));
xh_put_int(imports, sym, &id);
} }
exit:
xh_destroy(&table);
} }
static void static void
import(pic_state *pic, pic_value spec) import(pic_state *pic, pic_value spec)
{ {
xhash imports; struct pic_dict *imports;
xh_entry *it; pic_sym sym;
xh_init_int(&imports, sizeof(pic_sym)); /* pic_sym to pic_sym */ imports = pic_make_dict(pic);
import_table(pic, spec, &imports); import_table(pic, spec, imports);
for (it = xh_begin(&imports); it != NULL; it = xh_next(it)) { pic_dict_for_each (sym, imports) {
pic_put_rename(pic, pic->lib->env, sym, pic_sym(pic_dict_ref(pic, imports, sym)));
#if DEBUG
printf("* importing %s as %s\n", pic_symbol_name(pic, xh_key(it, pic_sym)), pic_symbol_name(pic, xh_val(it, pic_sym)));
#endif
pic_put_rename(pic, pic->lib->env, xh_key(it, pic_sym), xh_val(it, pic_sym));
} }
xh_destroy(&imports);
} }
static void static void