This commit is contained in:
Yuichi Nishiwaki 2014-03-29 21:47:57 +09:00
parent 2151640331
commit 01ed285d40
3 changed files with 103 additions and 52 deletions

View File

@ -96,6 +96,8 @@ typedef struct {
size_t glen, gcapa;
xhash macros;
int macroexpand_cnt;
xhash unresolved_symbols;
pic_value lib_tbl;
struct pic_lib *lib;

View File

@ -44,8 +44,8 @@ pic_find_rename(pic_state *pic, struct pic_senv *senv, pic_sym sym, pic_sym *ren
return true;
}
static pic_value macroexpand(pic_state *, pic_value, struct pic_senv *);
static pic_value macroexpand_list(pic_state *, pic_value, struct pic_senv *);
static pic_value macroexpand(pic_state *, pic_value, struct pic_senv *, int);
static pic_value macroexpand_list(pic_state *, pic_value, struct pic_senv *, int);
static struct pic_senv *
senv_new(pic_state *pic, struct pic_senv *up)
@ -60,7 +60,7 @@ senv_new(pic_state *pic, struct pic_senv *up)
}
static struct pic_senv *
senv_new_local(pic_state *pic, pic_value formals, struct pic_senv *up)
senv_new_local(pic_state *pic, pic_value formals, struct pic_senv *up, int id)
{
struct pic_senv *senv;
pic_value a;
@ -71,7 +71,7 @@ senv_new_local(pic_state *pic, pic_value formals, struct pic_senv *up)
pic_value v = pic_car(pic, a);
if (! pic_sym_p(v)) {
v = macroexpand(pic, v, up);
v = macroexpand(pic, v, up, id);
}
if (! pic_sym_p(v)) {
pic_error(pic, "syntax error");
@ -79,7 +79,7 @@ senv_new_local(pic_state *pic, pic_value formals, struct pic_senv *up)
pic_add_rename(pic, senv, pic_sym(v));
}
if (! pic_sym_p(a)) {
a = macroexpand(pic, a, up);
a = macroexpand(pic, a, up, id);
}
if (pic_sym_p(a)) {
pic_add_rename(pic, senv, pic_sym(a));
@ -131,8 +131,8 @@ identifier_eq_p(pic_state *pic, struct pic_senv *e1, pic_value x, struct pic_sen
return false;
}
x = macroexpand(pic, x, e1);
y = macroexpand(pic, y, e2);
x = macroexpand(pic, x, e1, 0);
y = macroexpand(pic, y, e2, 0);
return pic_eq_p(x, y);
}
@ -193,9 +193,10 @@ pic_defmacro(pic_state *pic, const char *name, struct pic_proc *macro)
}
static pic_sym
symbol_rename(pic_state *pic, pic_sym sym, struct pic_senv *senv)
symbol_rename(pic_state *pic, pic_sym sym, struct pic_senv *senv, int id)
{
pic_sym rename;
xhash *x;
if (! pic_interned_p(pic, sym)) {
return sym;
@ -208,18 +209,25 @@ symbol_rename(pic_state *pic, pic_sym sym, struct pic_senv *senv)
break;
senv = senv->up;
}
return sym;
x = &xh_val(xh_get(&pic->unresolved_symbols, id), xhash);
if (xh_get(x, sym)) {
return xh_val(xh_get(x, sym), pic_sym);
} else {
rename = pic_gensym(pic, sym);
xh_put(x, sym, &rename);
return rename;
}
}
static pic_value macroexpand_node(pic_state *, pic_value, struct pic_senv *);
static pic_value macroexpand_node(pic_state *, pic_value, struct pic_senv *, int);
static pic_value
macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv)
macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv, int id)
{
int ai = pic_gc_arena_preserve(pic);
pic_value v;
v = macroexpand_node(pic, expr, senv);
v = macroexpand_node(pic, expr, senv, id);
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
@ -227,7 +235,7 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv)
}
static pic_value
macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv, int id)
{
#if DEBUG
printf("[macroexpand] expanding... ");
@ -240,16 +248,16 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
struct pic_sc *sc;
sc = pic_sc(expr);
return macroexpand(pic, sc->expr, sc->senv);
return macroexpand(pic, sc->expr, sc->senv, id);
}
case PIC_TT_SYMBOL: {
return pic_symbol_value(symbol_rename(pic, pic_sym(expr), senv));
return pic_symbol_value(symbol_rename(pic, pic_sym(expr), senv, id));
}
case PIC_TT_PAIR: {
pic_value car, v;
xh_entry *e;
car = macroexpand(pic, pic_car(pic, expr), senv);
car = macroexpand(pic, pic_car(pic, expr), senv, id);
if (pic_sym_p(car)) {
pic_sym tag = pic_sym(car);
@ -314,7 +322,7 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
var = pic_cadr(pic, expr);
if (! pic_sym_p(var)) {
var = macroexpand(pic, var, senv);
var = macroexpand(pic, var, senv, id);
}
if (! pic_sym_p(var)) {
pic_error(pic, "binding to non-symbol object");
@ -390,12 +398,12 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
}
else if (tag == pic->sLAMBDA) {
struct pic_senv *in = senv_new_local(pic, pic_cadr(pic, expr), senv);
struct pic_senv *in = senv_new_local(pic, pic_cadr(pic, expr), senv, id);
return pic_cons(pic, car,
pic_cons(pic,
macroexpand_list(pic, pic_cadr(pic, expr), in),
macroexpand_list(pic, pic_cddr(pic, expr), in)));
macroexpand_list(pic, pic_cadr(pic, expr), in, id),
macroexpand_list(pic, pic_cddr(pic, expr), in, id)));
}
else if (tag == pic->sDEFINE) {
@ -408,13 +416,13 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
formals = pic_cadr(pic, expr);
if (pic_pair_p(formals)) {
struct pic_senv *in = senv_new_local(pic, pic_cdr(pic, formals), senv);
struct pic_senv *in = senv_new_local(pic, pic_cdr(pic, formals), senv, id);
pic_value a;
/* defined symbol */
a = pic_car(pic, formals);
if (! pic_sym_p(a)) {
a = macroexpand(pic, a, senv);
a = macroexpand(pic, a, senv, id);
}
if (! pic_sym_p(a)) {
pic_error(pic, "binding to non-symbol object");
@ -427,12 +435,12 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
/* binding value */
return pic_cons(pic, car,
pic_cons(pic,
macroexpand_list(pic, pic_cadr(pic, expr), in),
macroexpand_list(pic, pic_cddr(pic, expr), in)));
macroexpand_list(pic, pic_cadr(pic, expr), in, id),
macroexpand_list(pic, pic_cddr(pic, expr), in, id)));
}
if (! pic_sym_p(formals)) {
formals = macroexpand(pic, formals, senv);
formals = macroexpand(pic, formals, senv, id);
}
if (! pic_sym_p(formals)) {
pic_error(pic, "binding to non-symbol object");
@ -442,7 +450,7 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
pic_add_rename(pic, senv, sym);
}
return pic_cons(pic, pic_symbol_value(tag), macroexpand_list(pic, pic_cdr(pic, expr), senv));
return pic_cons(pic, pic_symbol_value(tag), macroexpand_list(pic, pic_cdr(pic, expr), senv, id));
}
else if (tag == pic->sQUOTE) {
@ -480,11 +488,11 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
puts("");
#endif
return macroexpand(pic, v, senv);
return macroexpand(pic, v, senv, id);
}
}
return pic_cons(pic, car, macroexpand_list(pic, pic_cdr(pic, expr), senv));
return pic_cons(pic, car, macroexpand_list(pic, pic_cdr(pic, expr), senv, id));
}
case PIC_TT_EOF:
case PIC_TT_NIL:
@ -516,7 +524,7 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
}
static pic_value
macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv)
macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv, int id)
{
int ai = pic_gc_arena_preserve(pic);
pic_value v, vs;
@ -526,7 +534,7 @@ macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv)
while (pic_pair_p(list)) {
v = pic_car(pic, list);
vs = pic_cons(pic, macroexpand(pic, v, senv), vs);
vs = pic_cons(pic, macroexpand(pic, v, senv, id), vs);
list = pic_cdr(pic, list);
pic_gc_arena_restore(pic, ai);
@ -534,7 +542,7 @@ macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv)
pic_gc_protect(pic, list);
}
list = macroexpand(pic, list, senv);
list = macroexpand(pic, list, senv, id);
/* reverse the result */
pic_for_each (v, vs) {
@ -550,6 +558,16 @@ macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv)
return list;
}
static void
init_table(pic_state *pic, int id)
{
xhash x;
xh_init_int(&x, sizeof(pic_sym));
xh_put(&pic->unresolved_symbols, id, &x);
}
pic_value
pic_macroexpand(pic_state *pic, pic_value expr)
{
@ -561,7 +579,9 @@ pic_macroexpand(pic_state *pic, pic_value expr)
puts("");
#endif
v = macroexpand(pic, expr, pic->lib->senv);
init_table(pic, pic->macroexpand_cnt);
v = macroexpand(pic, expr, pic->lib->senv, pic->macroexpand_cnt++);
#if DEBUG
puts("after expand:");
@ -718,12 +738,14 @@ er_macro_rename(pic_state *pic)
{
pic_sym sym;
struct pic_senv *mac_env;
int id;
pic_get_args(pic, "m", &sym);
mac_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 1));
id = pic_int(pic_proc_cv_ref(pic, pic_get_proc(pic), 2));
return pic_symbol_value(symbol_rename(pic, sym, mac_env));
return pic_symbol_value(symbol_rename(pic, sym, mac_env, id));
}
static pic_value
@ -732,6 +754,7 @@ er_macro_compare(pic_state *pic)
pic_value a, b;
struct pic_senv *use_env;
pic_sym m, n;
int id;
pic_get_args(pic, "oo", &a, &b);
@ -739,9 +762,10 @@ er_macro_compare(pic_state *pic)
return pic_false_value(); /* should be an error? */
use_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
id = pic_int(pic_proc_cv_ref(pic, pic_get_proc(pic), 2));
m = symbol_rename(pic, pic_sym(a), use_env);
n = symbol_rename(pic, pic_sym(b), use_env);
m = symbol_rename(pic, pic_sym(a), use_env, id);
n = symbol_rename(pic, pic_sym(b), use_env, id);
return pic_bool_value(m == n);
}
@ -761,15 +785,21 @@ er_macro_call(pic_state *pic)
pic_error(pic, "unexpected type of argument 3");
}
init_table(pic, pic->macroexpand_cnt);
rename = pic_proc_new(pic, er_macro_rename, "<er-macro-renamer>");
pic_proc_cv_init(pic, rename, 2);
pic_proc_cv_init(pic, rename, 3);
pic_proc_cv_set(pic, rename, 0, use_env);
pic_proc_cv_set(pic, rename, 1, mac_env);
pic_proc_cv_set(pic, rename, 2, pic_int_value(pic->macroexpand_cnt));
compare = pic_proc_new(pic, er_macro_compare, "<er-macro-comparator>");
pic_proc_cv_init(pic, compare, 2);
pic_proc_cv_init(pic, compare, 3);
pic_proc_cv_set(pic, compare, 0, use_env);
pic_proc_cv_set(pic, compare, 1, mac_env);
pic_proc_cv_set(pic, compare, 2, pic_int_value(pic->macroexpand_cnt));
pic->macroexpand_cnt++;
cb = pic_proc_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
@ -795,12 +825,14 @@ ir_macro_inject(pic_state *pic)
{
pic_sym sym;
struct pic_senv *use_env;
int id;
pic_get_args(pic, "m", &sym);
use_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
id = pic_int(pic_proc_cv_ref(pic, pic_get_proc(pic), 2));
return pic_symbol_value(symbol_rename(pic, sym, use_env));
return pic_symbol_value(symbol_rename(pic, sym, use_env, id));
}
static pic_value
@ -809,6 +841,7 @@ ir_macro_compare(pic_state *pic)
pic_value a, b;
struct pic_senv *mac_env;
pic_sym m, n;
int id;
pic_get_args(pic, "oo", &a, &b);
@ -816,26 +849,27 @@ ir_macro_compare(pic_state *pic)
return pic_false_value(); /* should be an error? */
mac_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 1));
id = pic_int(pic_proc_cv_ref(pic, pic_get_proc(pic), 2));
m = symbol_rename(pic, pic_sym(a), mac_env);
n = symbol_rename(pic, pic_sym(b), mac_env);
m = symbol_rename(pic, pic_sym(a), mac_env, id);
n = symbol_rename(pic, pic_sym(b), mac_env, id);
return pic_bool_value(m == n);
}
static pic_value
ir_macro_wrap(pic_state *pic, pic_value expr, struct pic_senv *use_env, pic_value *assoc)
ir_macro_wrap(pic_state *pic, pic_value expr, struct pic_senv *use_env, int id, pic_value *assoc)
{
if (pic_sym_p(expr)) {
pic_value r;
r = pic_sym_value(symbol_rename(pic, pic_sym(expr), use_env));
r = pic_sym_value(symbol_rename(pic, pic_sym(expr), use_env, id));
*assoc = pic_acons(pic, r, expr, *assoc);
return r;
}
else if (pic_pair_p(expr)) {
return pic_cons(pic,
ir_macro_wrap(pic, pic_car(pic, expr), use_env, assoc),
ir_macro_wrap(pic, pic_cdr(pic, expr), use_env, assoc));
ir_macro_wrap(pic, pic_car(pic, expr), use_env, id, assoc),
ir_macro_wrap(pic, pic_cdr(pic, expr), use_env, id, assoc));
}
else {
return expr;
@ -843,19 +877,19 @@ ir_macro_wrap(pic_state *pic, pic_value expr, struct pic_senv *use_env, pic_valu
}
static pic_value
ir_macro_unwrap(pic_state *pic, pic_value expr, struct pic_senv *mac_env, pic_value *assoc)
ir_macro_unwrap(pic_state *pic, pic_value expr, struct pic_senv *mac_env, int id, pic_value *assoc)
{
if (pic_sym_p(expr)) {
pic_value r;
if (pic_test(r = pic_assq(pic, expr, *assoc))) {
return pic_cdr(pic, r);
}
return pic_sym_value(symbol_rename(pic, pic_sym(expr), mac_env));
return pic_sym_value(symbol_rename(pic, pic_sym(expr), mac_env, id));
}
else if (pic_pair_p(expr)) {
return pic_cons(pic,
ir_macro_unwrap(pic, pic_car(pic, expr), mac_env, assoc),
ir_macro_unwrap(pic, pic_cdr(pic, expr), mac_env, assoc));
ir_macro_unwrap(pic, pic_car(pic, expr), mac_env, id, assoc),
ir_macro_unwrap(pic, pic_cdr(pic, expr), mac_env, id, assoc));
}
else {
return expr;
@ -878,21 +912,28 @@ ir_macro_call(pic_state *pic)
pic_error(pic, "unexpected type of argument 3");
}
init_table(pic, pic->macroexpand_cnt);
inject = pic_proc_new(pic, ir_macro_inject, "<ir-macro-injecter>");
pic_proc_cv_init(pic, inject, 2);
pic_proc_cv_init(pic, inject, 3);
pic_proc_cv_set(pic, inject, 0, use_env);
pic_proc_cv_set(pic, inject, 1, mac_env);
pic_proc_cv_set(pic, inject, 2, pic_int_value(pic->macroexpand_cnt));
compare = pic_proc_new(pic, ir_macro_compare, "<ir-macro-comparator>");
pic_proc_cv_init(pic, compare, 2);
pic_proc_cv_init(pic, compare, 3);
pic_proc_cv_set(pic, compare, 0, use_env);
pic_proc_cv_set(pic, compare, 1, mac_env);
pic_proc_cv_set(pic, compare, 2, pic_int_value(pic->macroexpand_cnt));
cb = pic_proc_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
expr = ir_macro_wrap(pic, expr, pic_senv_ptr(use_env), &assoc);
expr = ir_macro_wrap(pic, expr, pic_senv_ptr(use_env), pic->macroexpand_cnt, &assoc);
expr = pic_apply3(pic, cb, expr, pic_obj_value(inject), pic_obj_value(compare));
expr = ir_macro_unwrap(pic, expr, pic_senv_ptr(mac_env), &assoc);
expr = ir_macro_unwrap(pic, expr, pic_senv_ptr(mac_env), pic->macroexpand_cnt, &assoc);
pic->macroexpand_cnt++;
return expr;
}

View File

@ -59,6 +59,8 @@ pic_open(int argc, char *argv[], char **envp)
/* macros */
xh_init_int(&pic->macros, sizeof(struct pic_macro *));
pic->macroexpand_cnt = 0;
xh_init_int(&pic->unresolved_symbols, sizeof(xhash));
/* libraries */
pic->lib_tbl = pic_nil_value();
@ -160,5 +162,11 @@ pic_close(pic_state *pic)
}
xh_destroy(&pic->sym_names);
xh_begin(&it, &pic->unresolved_symbols);
while (xh_next(&it)) {
xh_destroy(&xh_val(it.e, xhash));
}
xh_destroy(&pic->unresolved_symbols);
free(pic);
}