diff --git a/include/picrin.h b/include/picrin.h index 29640fa7..ae6f66ef 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -175,9 +175,9 @@ pic_value pic_apply3(pic_state *, struct pic_proc *, pic_value, pic_value, pic_v pic_value pic_apply4(pic_state *, struct pic_proc *, pic_value, pic_value, pic_value, pic_value); pic_value pic_apply5(pic_state *, struct pic_proc *, pic_value, pic_value, pic_value, pic_value, pic_value); pic_value pic_apply_trampoline(pic_state *, struct pic_proc *, pic_value); -pic_value pic_eval(pic_state *, pic_value); -struct pic_proc *pic_compile(pic_state *, pic_value); -pic_value pic_macroexpand(pic_state *, pic_value); +pic_value pic_eval(pic_state *, pic_value, struct pic_lib *); +struct pic_proc *pic_compile(pic_state *, pic_value, struct pic_lib *); +pic_value pic_macroexpand(pic_state *, pic_value, struct pic_lib *); void pic_in_library(pic_state *, pic_value); struct pic_lib *pic_make_library(pic_state *, pic_value); diff --git a/src/codegen.c b/src/codegen.c index 8f8d9aed..1dc7e898 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1442,7 +1442,7 @@ pic_codegen(pic_state *pic, pic_value obj) } struct pic_proc * -pic_compile(pic_state *pic, pic_value obj) +pic_compile(pic_state *pic, pic_value obj, struct pic_lib *lib) { struct pic_irep *irep; size_t ai = pic_gc_arena_preserve(pic); @@ -1458,7 +1458,7 @@ pic_compile(pic_state *pic, pic_value obj) #endif /* macroexpand */ - obj = pic_macroexpand(pic, obj); + obj = pic_macroexpand(pic, obj, lib); #if DEBUG fprintf(stdout, "## macroexpand completed\n"); pic_debug(pic, obj); diff --git a/src/load.c b/src/load.c index f4b4db73..269fc657 100644 --- a/src/load.c +++ b/src/load.c @@ -20,7 +20,7 @@ pic_load_cstr(pic_state *pic, const char *src) pic_for_each (v, exprs) { ai = pic_gc_arena_preserve(pic); - proc = pic_compile(pic, v); + proc = pic_compile(pic, v, pic->lib); if (proc == NULL) { pic_error(pic, "load: compilation failure"); } @@ -54,7 +54,7 @@ pic_load(pic_state *pic, const char *fn) pic_for_each (v, exprs) { ai = pic_gc_arena_preserve(pic); - proc = pic_compile(pic, v); + proc = pic_compile(pic, v, pic->lib); if (proc == NULL) { pic_error(pic, "load: compilation failure"); } diff --git a/src/macro.c b/src/macro.c index 875e7d3f..40167238 100644 --- a/src/macro.c +++ b/src/macro.c @@ -168,7 +168,7 @@ macroexpand_deflibrary(pic_state *pic, pic_value expr) pic_in_library(pic, pic_cadr(pic, expr)); pic_for_each (v, pic_cddr(pic, expr)) { - pic_void(pic_eval(pic, v)); + pic_void(pic_eval(pic, v, pic->lib)); } pic_in_library(pic, prev->name); @@ -293,7 +293,7 @@ macroexpand_defsyntax(pic_state *pic, pic_value expr, struct pic_senv *senv) val = pic_cadr(pic, pic_cdr(pic, expr)); pic_try { - val = pic_eval(pic, val); + val = pic_eval(pic, val, pic->lib); } pic_catch { pic_errorf(pic, "macroexpand error while definition: %s", pic_errmsg(pic)); } @@ -413,8 +413,9 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv) } pic_value -pic_macroexpand(pic_state *pic, pic_value expr) +pic_macroexpand(pic_state *pic, pic_value expr, struct pic_lib *lib) { + struct pic_lib *prev; pic_value v; #if DEBUG @@ -423,7 +424,13 @@ pic_macroexpand(pic_state *pic, pic_value expr) puts(""); #endif - v = macroexpand(pic, expr, pic->lib->env); + /* change library for macro-expansion time processing */ + prev = pic->lib; + pic->lib = lib; + + v = macroexpand(pic, expr, lib->env); + + pic->lib = prev; #if DEBUG puts("after expand:"); @@ -560,7 +567,7 @@ pic_macro_macroexpand(pic_state *pic) pic_get_args(pic, "o", &expr); - return pic_macroexpand(pic, expr); + return pic_macroexpand(pic, expr, pic->lib); } static pic_value diff --git a/src/vm.c b/src/vm.c index 2ab80a1e..52af4411 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1063,11 +1063,11 @@ pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, pic_value args) } pic_value -pic_eval(pic_state *pic, pic_value program) +pic_eval(pic_state *pic, pic_value program, struct pic_lib *lib) { struct pic_proc *proc; - proc = pic_compile(pic, program); + proc = pic_compile(pic, program, lib); return pic_apply(pic, proc, pic_nil_value()); } diff --git a/tools/main.c b/tools/main.c index 5e43f2b7..9771df6e 100644 --- a/tools/main.c +++ b/tools/main.c @@ -134,7 +134,7 @@ repl(pic_state *pic) pic_for_each (v, exprs) { /* eval */ - v = pic_eval(pic, v); + v = pic_eval(pic, v, pic->lib); /* print */ pic_printf(pic, "=> ~s\n", v); @@ -185,7 +185,7 @@ exec_file(pic_state *pic, const char *fname) pic_for_each (v, exprs) { - proc = pic_compile(pic, v); + proc = pic_compile(pic, v, pic->lib); if (proc == NULL) { fputs(pic_errmsg(pic), stderr); fprintf(stderr, "fatal error: %s compilation failure\n", fname); @@ -223,7 +223,7 @@ exec_string(pic_state *pic, const char *str) ai = pic_gc_arena_preserve(pic); pic_for_each (v, exprs) { - proc = pic_compile(pic, v); + proc = pic_compile(pic, v, pic->lib); if (proc == NULL) { goto abort; }