From 53f76b7a474dab06648f6bbf5e063e52ae137dc2 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 3 Jun 2014 23:54:51 +0900 Subject: [PATCH] support renaming export --- src/lib.c | 16 ++++++++++++++++ src/macro.c | 24 ++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) 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();