support renaming export

This commit is contained in:
Yuichi Nishiwaki 2014-06-03 23:54:51 +09:00
parent 81c3147695
commit 53f76b7a47
2 changed files with 38 additions and 2 deletions

View File

@ -97,3 +97,19 @@ pic_export(pic_state *pic, pic_sym sym)
xh_put(&pic->lib->exports, sym, &rename); xh_put(&pic->lib->exports, sym, &rename);
} }
void
pic_export_as(pic_state *pic, pic_sym sym, pic_sym as)
{
pic_sym rename;
if (! pic_find_rename(pic, pic->lib->senv, sym, &rename)) {
pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, sym));
}
#if DEBUG
printf("* exporting %s as %s\n", pic_symbol_name(pic, as), pic_symbol_name(pic, rename));
#endif
xh_put(&pic->lib->exports, as, &rename);
}

View File

@ -267,14 +267,34 @@ macroexpand_import(pic_state *pic, pic_value expr)
static pic_value static pic_value
macroexpand_export(pic_state *pic, pic_value expr) macroexpand_export(pic_state *pic, pic_value expr)
{ {
extern pic_value pic_export_as(pic_state *, pic_sym, pic_sym);
pic_value spec; pic_value spec;
pic_sym sRENAME, sym, as;
sRENAME = pic_intern_cstr(pic, "rename");
pic_for_each (spec, pic_cdr(pic, expr)) { pic_for_each (spec, pic_cdr(pic, expr)) {
if (! pic_sym_p(spec)) { if (pic_sym_p(spec)) {
sym = as = pic_sym(spec);
}
else if (pic_list_p(spec) && pic_eq_p(pic_car(pic, spec), pic_sym_value(sRENAME))) {
if (pic_length(pic, spec) != 3) {
pic_error(pic, "syntax error");
}
if (! pic_sym_p(pic_list_ref(pic, spec, 1))) {
pic_error(pic, "syntax error");
}
sym = pic_sym(pic_list_ref(pic, spec, 1));
if (! pic_sym_p(pic_list_ref(pic, spec, 2))) {
pic_error(pic, "syntax error");
}
as = pic_sym(pic_list_ref(pic, spec, 2));
}
else {
pic_error(pic, "syntax error"); pic_error(pic, "syntax error");
} }
/* TODO: warn if symbol is shadowed by local variable */ /* TODO: warn if symbol is shadowed by local variable */
pic_export(pic, pic_sym(spec)); pic_export_as(pic, sym, as);
} }
return pic_none_value(); return pic_none_value();