From 98857c07265c1878d6717c1c97ddda8c98f97925 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 9 Jun 2015 22:26:21 +0900 Subject: [PATCH 1/3] change pic_find_rename interface --- extlib/benz/include/picrin/macro.h | 2 +- extlib/benz/lib.c | 2 +- extlib/benz/macro.c | 21 +++++++++------------ extlib/benz/vm.c | 6 +++--- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/extlib/benz/include/picrin/macro.h b/extlib/benz/include/picrin/macro.h index c0f6fef3..7d150777 100644 --- a/extlib/benz/include/picrin/macro.h +++ b/extlib/benz/include/picrin/macro.h @@ -25,7 +25,7 @@ bool pic_identifier_eq_p(pic_state *, struct pic_env *, pic_sym *, struct pic_en struct pic_env *pic_make_env(pic_state *, struct pic_env *); pic_sym *pic_add_rename(pic_state *, struct pic_env *, pic_sym *); -bool pic_find_rename(pic_state *, struct pic_env *, pic_sym *, pic_sym ** /* = NULL */); +pic_sym *pic_find_rename(pic_state *, struct pic_env *, pic_sym *); void pic_put_rename(pic_state *, struct pic_env *, pic_sym *, pic_sym *); #if defined(__cplusplus) diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index e39e0f48..9c5468bf 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -165,7 +165,7 @@ export(pic_state *pic, pic_value spec) goto fail; } - if (! pic_find_rename(pic, pic->lib->env, pic_sym_ptr(a), &rename)) { + if ((rename = pic_find_rename(pic, pic->lib->env, pic_sym_ptr(a))) == NULL) { pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, pic_sym_ptr(a))); } diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index b12c6bc6..a36a8c8c 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -7,10 +7,10 @@ pic_sym * pic_add_rename(pic_state *pic, struct pic_env *env, pic_sym *sym) { - pic_sym *rename; + pic_sym *rename = pic_gensym(pic, sym); - rename = pic_gensym(pic, sym); pic_put_rename(pic, env, sym, rename); + return rename; } @@ -20,16 +20,13 @@ pic_put_rename(pic_state *pic, struct pic_env *env, pic_sym *sym, pic_sym *renam pic_dict_set(pic, env->map, sym, pic_obj_value(rename)); } -bool -pic_find_rename(pic_state *pic, struct pic_env *env, pic_sym *sym, pic_sym **rename) +pic_sym * +pic_find_rename(pic_state *pic, struct pic_env *env, pic_sym *sym) { if (! pic_dict_has(pic, env->map, sym)) { - return false; + return NULL; } - if (rename != NULL) { - *rename = pic_sym_ptr(pic_dict_ref(pic, env->map, sym)); - } - return true; + return pic_sym_ptr(pic_dict_ref(pic, env->map, sym)); } static void @@ -53,7 +50,7 @@ make_identifier(pic_state *pic, pic_sym *sym, struct pic_env *env) pic_sym *rename; while (true) { - if (pic_find_rename(pic, env, sym, &rename)) { + if ((rename = pic_find_rename(pic, env, sym)) != NULL) { return rename; } if (! env->up) @@ -189,7 +186,7 @@ macroexpand_define(pic_state *pic, pic_value expr, struct pic_env *env) pic_errorf(pic, "binding to non-symbol object"); } sym = pic_sym_ptr(var); - if (! pic_find_rename(pic, env, sym, &rename)) { + if ((rename = pic_find_rename(pic, env, sym)) == NULL) { rename = pic_add_rename(pic, env, sym); } val = macroexpand(pic, pic_list_ref(pic, expr, 2), env); @@ -212,7 +209,7 @@ macroexpand_defsyntax(pic_state *pic, pic_value expr, struct pic_env *env) pic_errorf(pic, "binding to non-symbol object"); } sym = pic_sym_ptr(var); - if (! pic_find_rename(pic, env, sym, &rename)) { + if ((rename = pic_find_rename(pic, env, sym)) == NULL) { rename = pic_add_rename(pic, env, sym); } else { pic_warnf(pic, "redefining syntax variable: ~s", pic_obj_value(sym)); diff --git a/extlib/benz/vm.c b/extlib/benz/vm.c index ab9916fb..7a062019 100644 --- a/extlib/benz/vm.c +++ b/extlib/benz/vm.c @@ -410,7 +410,7 @@ pic_define_noexport(pic_state *pic, const char *name, pic_value val) sym = pic_intern_cstr(pic, name); - if (! pic_find_rename(pic, pic->lib->env, sym, &rename)) { + if ((rename = pic_find_rename(pic, pic->lib->env, sym)) == NULL) { rename = pic_add_rename(pic, pic->lib->env, sym); } else { pic_warnf(pic, "redefining global"); @@ -434,7 +434,7 @@ pic_ref(pic_state *pic, struct pic_lib *lib, const char *name) sym = pic_intern_cstr(pic, name); - if (! pic_find_rename(pic, lib->env, sym, &rename)) { + if ((rename = pic_find_rename(pic, lib->env, sym)) == NULL) { pic_errorf(pic, "symbol \"%s\" not defined in library ~s", name, lib->name); } @@ -448,7 +448,7 @@ pic_set(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) sym = pic_intern_cstr(pic, name); - if (! pic_find_rename(pic, lib->env, sym, &rename)) { + if ((rename = pic_find_rename(pic, lib->env, sym)) == NULL) { pic_errorf(pic, "symbol \"%s\" not defined in library ~s", name, lib->name); } From cf411fd62a522972510f3866d2069d9dc111b06f Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 9 Jun 2015 22:36:04 +0900 Subject: [PATCH 2/3] lazy export --- extlib/benz/lib.c | 20 +++++++++++--------- piclib/picrin/macro.scm | 28 ++++++++++++++-------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 9c5468bf..1ac5b879 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -73,7 +73,7 @@ import_table(pic_state *pic, pic_value spec, struct pic_dict *imports) struct pic_lib *lib; struct pic_dict *table; pic_value val, tmp, prefix, it; - pic_sym *sym, *id, *tag; + pic_sym *sym, *id, *tag, *nick; xh_entry *iter; table = pic_make_dict(pic); @@ -122,8 +122,15 @@ import_table(pic_state *pic, pic_value spec, struct pic_dict *imports) if (! lib) { pic_errorf(pic, "library not found: ~a", spec); } - pic_dict_for_each (sym, lib->exports, iter) { - pic_dict_set(pic, imports, sym, pic_dict_ref(pic, lib->exports, sym)); + pic_dict_for_each (nick, lib->exports, iter) { + pic_sym *realname, *rename; + + realname = pic_sym_ptr(pic_dict_ref(pic, lib->exports, nick)); + + if ((rename = pic_find_rename(pic, lib->env, realname)) == NULL) { + pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname)); + } + pic_dict_set(pic, imports, nick, pic_obj_value(rename)); } } @@ -148,7 +155,6 @@ export(pic_state *pic, pic_value spec) { pic_sym *sRENAME = pic_intern_cstr(pic, "rename"); pic_value a, b; - pic_sym *rename; if (pic_sym_p(spec)) { /* (export a) */ a = b = spec; @@ -165,15 +171,11 @@ export(pic_state *pic, pic_value spec) goto fail; } - if ((rename = pic_find_rename(pic, pic->lib->env, pic_sym_ptr(a))) == NULL) { - pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, pic_sym_ptr(a))); - } - #if DEBUG printf("* exporting %s as %s\n", pic_symbol_name(pic, pic_sym_ptr(b)), pic_symbol_name(pic, rename)); #endif - pic_dict_set(pic, pic->lib->exports, pic_sym_ptr(b), pic_obj_value(rename)); + pic_dict_set(pic, pic->lib->exports, pic_sym_ptr(b), a); return; diff --git a/piclib/picrin/macro.scm b/piclib/picrin/macro.scm index 985b5d94..e11d4eb7 100644 --- a/piclib/picrin/macro.scm +++ b/piclib/picrin/macro.scm @@ -1,6 +1,19 @@ (define-library (picrin macro) (import (picrin base)) + (export identifier? + identifier=? + make-identifier + make-syntactic-closure + close-syntax + capture-syntactic-environment + sc-macro-transformer + rsc-macro-transformer + er-macro-transformer + ir-macro-transformer + ;; strip-syntax + define-macro) + ;; assumes no derived expressions are provided yet (define (walk proc expr) @@ -125,17 +138,4 @@ (list (r 'define-macro) (car formal) (cons (r 'lambda) (cons (cdr formal) - body))))))) - - (export identifier? - identifier=? - make-identifier - make-syntactic-closure - close-syntax - capture-syntactic-environment - sc-macro-transformer - rsc-macro-transformer - er-macro-transformer - ir-macro-transformer - ;; strip-syntax - define-macro)) + body)))))))) From 2b1fa0692dbfc7fad686b01dee022f343c7ce4d7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 9 Jun 2015 22:49:49 +0900 Subject: [PATCH 3/3] fix debug print --- extlib/benz/lib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 1ac5b879..853b61f1 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -172,7 +172,7 @@ export(pic_state *pic, pic_value spec) } #if DEBUG - printf("* exporting %s as %s\n", pic_symbol_name(pic, pic_sym_ptr(b)), pic_symbol_name(pic, rename)); + printf("* exporting %s as %s\n", pic_symbol_name(pic, pic_sym_ptr(b)), pic_symbol_name(pic, pic_sym_ptr(a))); #endif pic_dict_set(pic, pic->lib->exports, pic_sym_ptr(b), a);