From 5035831d845a4acc32777e96b5eeed79ffdb7074 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 8 Dec 2013 00:52:34 -0800 Subject: [PATCH] [bugfix] syntax objects should be brought to imported library's senv->stx holder --- include/picrin.h | 1 - src/macro.c | 43 ++++++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index ffb8d341..fe8cdedf 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -137,7 +137,6 @@ struct pic_lib *pic_find_library(pic_state *, pic_value); } void pic_export(pic_state *, pic_sym); -void pic_export_as(pic_state *, pic_sym, pic_sym); void pic_abort(pic_state *, const char *); void pic_raise(pic_state *, pic_value); diff --git a/src/macro.c b/src/macro.c index fc783ef5..77e0f3b9 100644 --- a/src/macro.c +++ b/src/macro.c @@ -179,13 +179,13 @@ pic_identifier_p(pic_value obj) void pic_export(pic_state *pic, pic_sym sym) { - pic_export_as(pic, sym, sym); -} + struct xh_entry *e; -void -pic_export_as(pic_state *pic, pic_sym sym, pic_sym alias) -{ - xh_put(pic->lib->exports, pic_symbol_name(pic, sym), (int)alias); + e = xh_get(pic->lib->senv->tbl, pic_symbol_name(pic, sym)); + if (! e) { + pic_error(pic, "symbol not defined"); + } + xh_put(pic->lib->exports, e->key, e->val); } static void @@ -274,21 +274,34 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) #if DEBUG printf("* importing %s as %s\n", it.e->key, pic_symbol_name(pic, (pic_sym)it.e->val)); #endif - xh_put(pic->lib->senv->tbl, it.e->key, it.e->val); + if (it.e->val >= 0) { + xh_put(pic->lib->senv->tbl, it.e->key, it.e->val); + } + else { /* syntax object */ + int idx; + struct pic_senv *senv = pic->lib->senv; + + idx = senv->xlen; + if (idx >= senv->xcapa) { + pic_abort(pic, "macro table overflow"); + } + /* bring macro object from imported lib */ + senv->stx[idx] = lib->senv->stx[~it.e->val]; + xh_put(senv->tbl, it.e->key, ~idx); + senv->xlen++; + } xh_next(lib->exports, &it); } return pic_false_value(); } case PIC_STX_EXPORT: { - pic_sym orig, ren; - pic_value v; - - orig = ren = pic_sym(pic_cadr(pic, expr)); - v = macroexpand(pic, pic_cadr(pic, expr), senv); - if (pic_symbol_p(v)) { - ren = pic_sym(v); + v = pic_cadr(pic, expr); + if (! pic_symbol_p(v)) { + pic_error(pic, "syntax error"); } - pic_export_as(pic, orig, ren); + /* TODO: warn if symbol is shadowed by local variable */ + pic_export(pic, pic_sym(v)); + return pic_false_value(); } case PIC_STX_DEFSYNTAX: {