diff --git a/src/lib.c b/src/lib.c index c33f694f..0cdcff73 100644 --- a/src/lib.c +++ b/src/lib.c @@ -97,3 +97,19 @@ pic_export(pic_state *pic, pic_sym sym) 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); +} diff --git a/src/macro.c b/src/macro.c index bcea0909..59e53c0c 100644 --- a/src/macro.c +++ b/src/macro.c @@ -267,14 +267,34 @@ macroexpand_import(pic_state *pic, pic_value expr) static pic_value macroexpand_export(pic_state *pic, pic_value expr) { + extern pic_value pic_export_as(pic_state *, pic_sym, pic_sym); pic_value spec; + pic_sym sRENAME, sym, as; + + sRENAME = pic_intern_cstr(pic, "rename"); 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"); } /* 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();