diff --git a/include/picrin/macro.h b/include/picrin/macro.h index 4dce6987..151eb144 100644 --- a/include/picrin/macro.h +++ b/include/picrin/macro.h @@ -42,6 +42,8 @@ pic_sym pic_add_rename(pic_state *, struct pic_senv *, pic_sym); bool pic_find_rename(pic_state *, struct pic_senv *, pic_sym, pic_sym * /* = NULL */); void pic_put_rename(pic_state *, struct pic_senv *, pic_sym, pic_sym); +void pic_define_syntactic_keyword(pic_state *, struct pic_senv *, pic_sym); + #if defined(__cplusplus) } #endif diff --git a/src/init.c b/src/init.c index 7a5b3d23..a322caff 100644 --- a/src/init.c +++ b/src/init.c @@ -86,20 +86,13 @@ pic_init_core(pic_state *pic) /* load core syntaces */ pic->lib->senv = pic_null_syntactic_environment(pic); - pic_put_rename(pic, pic->lib->senv, pic->sDEFINE, pic->sDEFINE); - pic_put_rename(pic, pic->lib->senv, pic->sSETBANG, pic->sSETBANG); - pic_put_rename(pic, pic->lib->senv, pic->sQUOTE, pic->sQUOTE); - pic_put_rename(pic, pic->lib->senv, pic->sLAMBDA, pic->sLAMBDA); - pic_put_rename(pic, pic->lib->senv, pic->sIF, pic->sIF); - pic_put_rename(pic, pic->lib->senv, pic->sBEGIN, pic->sBEGIN); - pic_put_rename(pic, pic->lib->senv, pic->sDEFINE_SYNTAX, pic->sDEFINE_SYNTAX); - pic_export(pic, pic->sDEFINE); - pic_export(pic, pic->sSETBANG); - pic_export(pic, pic->sQUOTE); - pic_export(pic, pic->sLAMBDA); - pic_export(pic, pic->sIF); - pic_export(pic, pic->sBEGIN); - pic_export(pic, pic->sDEFINE_SYNTAX); + pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sDEFINE); + pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sSETBANG); + pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sQUOTE); + pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sLAMBDA); + pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sIF); + pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sBEGIN); + pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sDEFINE_SYNTAX); pic_init_bool(pic); DONE; pic_init_pair(pic); DONE; diff --git a/src/macro.c b/src/macro.c index 6f3fea29..8930281e 100644 --- a/src/macro.c +++ b/src/macro.c @@ -20,13 +20,23 @@ pic_null_syntactic_environment(pic_state *pic) senv->up = NULL; xh_init_int(&senv->renames, sizeof(pic_sym)); - pic_put_rename(pic, senv, pic->sDEFINE_LIBRARY, pic->sDEFINE_LIBRARY); - pic_put_rename(pic, senv, pic->sIMPORT, pic->sIMPORT); - pic_put_rename(pic, senv, pic->sEXPORT, pic->sEXPORT); + pic_define_syntactic_keyword(pic, senv, pic->sDEFINE_LIBRARY); + pic_define_syntactic_keyword(pic, senv, pic->sIMPORT); + pic_define_syntactic_keyword(pic, senv, pic->sEXPORT); return senv; } +void +pic_define_syntactic_keyword(pic_state *pic, struct pic_senv *senv, pic_sym sym) +{ + pic_put_rename(pic, senv, sym, sym); + + if (pic->lib && pic->lib->senv == senv) { + pic_export(pic, sym); + } +} + pic_sym pic_add_rename(pic_state *pic, struct pic_senv *senv, pic_sym sym) { @@ -969,8 +979,7 @@ pic_init_macro(pic_state *pic) pic_deflibrary ("(picrin macro)") { /* export define-macro syntax */ - pic_put_rename(pic, pic->lib->senv, pic->sDEFINE_MACRO, pic->sDEFINE_MACRO); - pic_export(pic, pic->sDEFINE_MACRO); + pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sDEFINE_MACRO); pic_defun(pic, "gensym", pic_macro_gensym); pic_defun(pic, "macroexpand", pic_macro_macroexpand);