Merge branch 'refactor-macroexpand2'

This commit is contained in:
Yuichi Nishiwaki 2014-04-05 11:30:39 +09:00
commit 716a76c8a0
4 changed files with 55 additions and 72 deletions

View File

@ -36,14 +36,14 @@ struct pic_sc {
#define pic_senv_p(v) (pic_type(v) == PIC_TT_SENV) #define pic_senv_p(v) (pic_type(v) == PIC_TT_SENV)
#define pic_senv_ptr(v) ((struct pic_senv *)pic_ptr(v)) #define pic_senv_ptr(v) ((struct pic_senv *)pic_ptr(v))
struct pic_senv *pic_null_syntactic_env(pic_state *); struct pic_senv *pic_null_syntactic_environment(pic_state *);
struct pic_senv *pic_minimal_syntactic_env(pic_state *);
struct pic_senv *pic_core_syntactic_env(pic_state *);
pic_sym pic_add_rename(pic_state *, struct pic_senv *, pic_sym); 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 */); 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_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) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -85,14 +85,14 @@ pic_init_core(pic_state *pic)
pic_deflibrary ("(scheme base)") { pic_deflibrary ("(scheme base)") {
/* load core syntaces */ /* load core syntaces */
pic->lib->senv = pic_core_syntactic_env(pic); pic->lib->senv = pic_null_syntactic_environment(pic);
pic_export(pic, pic_intern_cstr(pic, "define")); pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sDEFINE);
pic_export(pic, pic_intern_cstr(pic, "set!")); pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sSETBANG);
pic_export(pic, pic_intern_cstr(pic, "quote")); pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sQUOTE);
pic_export(pic, pic_intern_cstr(pic, "lambda")); pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sLAMBDA);
pic_export(pic, pic_intern_cstr(pic, "if")); pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sIF);
pic_export(pic, pic_intern_cstr(pic, "begin")); pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sBEGIN);
pic_export(pic, pic_intern_cstr(pic, "define-syntax")); pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sDEFINE_SYNTAX);
pic_init_bool(pic); DONE; pic_init_bool(pic); DONE;
pic_init_pair(pic); DONE; pic_init_pair(pic); DONE;

View File

@ -24,7 +24,7 @@ pic_make_library(pic_state *pic, pic_value name)
return lib; return lib;
} }
senv = pic_minimal_syntactic_env(pic); senv = pic_null_syntactic_environment(pic);
lib = (struct pic_lib *)pic_obj_alloc(pic, sizeof(struct pic_lib), PIC_TT_LIB); lib = (struct pic_lib *)pic_obj_alloc(pic, sizeof(struct pic_lib), PIC_TT_LIB);
lib->senv = senv; lib->senv = senv;

View File

@ -12,7 +12,7 @@
#include "picrin/box.h" #include "picrin/box.h"
struct pic_senv * struct pic_senv *
pic_null_syntactic_env(pic_state *pic) pic_null_syntactic_environment(pic_state *pic)
{ {
struct pic_senv *senv; struct pic_senv *senv;
@ -20,70 +20,21 @@ pic_null_syntactic_env(pic_state *pic)
senv->up = NULL; senv->up = NULL;
xh_init_int(&senv->renames, sizeof(pic_sym)); xh_init_int(&senv->renames, sizeof(pic_sym));
return senv; 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);
struct pic_senv *
pic_minimal_syntactic_env(pic_state *pic)
{
struct pic_senv *senv = pic_null_syntactic_env(pic);
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);
return senv; return senv;
} }
struct pic_senv * void
pic_core_syntactic_env(pic_state *pic) pic_define_syntactic_keyword(pic_state *pic, struct pic_senv *senv, pic_sym sym)
{ {
struct pic_senv *senv = pic_minimal_syntactic_env(pic); pic_put_rename(pic, senv, sym, sym);
pic_put_rename(pic, senv, pic->sDEFINE, pic->sDEFINE); if (pic->lib && pic->lib->senv == senv) {
pic_put_rename(pic, senv, pic->sSETBANG, pic->sSETBANG); pic_export(pic, sym);
pic_put_rename(pic, senv, pic->sQUOTE, pic->sQUOTE);
pic_put_rename(pic, senv, pic->sLAMBDA, pic->sLAMBDA);
pic_put_rename(pic, senv, pic->sIF, pic->sIF);
pic_put_rename(pic, senv, pic->sBEGIN, pic->sBEGIN);
pic_put_rename(pic, senv, pic->sDEFINE_SYNTAX, pic->sDEFINE_SYNTAX);
return senv;
}
static pic_value macroexpand(pic_state *, pic_value, struct pic_senv *, pic_value);
static struct pic_senv *
push_scope(pic_state *pic, pic_value formals, struct pic_senv *up, pic_value assoc_box)
{
struct pic_senv *senv;
pic_value a;
senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
senv->up = up;
xh_init_int(&senv->renames, sizeof(pic_sym));
for (a = formals; pic_pair_p(a); a = pic_cdr(pic, a)) {
pic_value v = pic_car(pic, a);
if (! pic_sym_p(v)) {
v = macroexpand(pic, v, up, assoc_box);
}
if (! pic_sym_p(v)) {
pic_error(pic, "syntax error");
}
pic_add_rename(pic, senv, pic_sym(v));
} }
if (! pic_sym_p(a)) {
a = macroexpand(pic, a, up, assoc_box);
}
if (pic_sym_p(a)) {
pic_add_rename(pic, senv, pic_sym(a));
}
else if (! pic_nil_p(a)) {
pic_error(pic, "syntax error");
}
return senv;
} }
pic_sym pic_sym
@ -209,6 +160,39 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv, pic_value ass
return v; return v;
} }
static struct pic_senv *
push_scope(pic_state *pic, pic_value formals, struct pic_senv *up, pic_value assoc_box)
{
struct pic_senv *senv;
pic_value a;
senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
senv->up = up;
xh_init_int(&senv->renames, sizeof(pic_sym));
for (a = formals; pic_pair_p(a); a = pic_cdr(pic, a)) {
pic_value v = pic_car(pic, a);
if (! pic_sym_p(v)) {
v = macroexpand(pic, v, up, assoc_box);
}
if (! pic_sym_p(v)) {
pic_error(pic, "syntax error");
}
pic_add_rename(pic, senv, pic_sym(v));
}
if (! pic_sym_p(a)) {
a = macroexpand(pic, a, up, assoc_box);
}
if (pic_sym_p(a)) {
pic_add_rename(pic, senv, pic_sym(a));
}
else if (! pic_nil_p(a)) {
pic_error(pic, "syntax error");
}
return senv;
}
static pic_value static pic_value
macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv, pic_value assoc_box) macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv, pic_value assoc_box)
{ {
@ -995,8 +979,7 @@ pic_init_macro(pic_state *pic)
pic_deflibrary ("(picrin macro)") { pic_deflibrary ("(picrin macro)") {
/* export define-macro syntax */ /* export define-macro syntax */
pic_put_rename(pic, pic->lib->senv, pic->sDEFINE_MACRO, pic->sDEFINE_MACRO); pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sDEFINE_MACRO);
pic_export(pic, pic->sDEFINE_MACRO);
pic_defun(pic, "gensym", pic_macro_gensym); pic_defun(pic, "gensym", pic_macro_gensym);
pic_defun(pic, "macroexpand", pic_macro_macroexpand); pic_defun(pic, "macroexpand", pic_macro_macroexpand);