Merge branch 'lazy-export'

This commit is contained in:
Yuichi Nishiwaki 2015-06-09 22:55:07 +09:00
commit f00e0d7462
5 changed files with 39 additions and 40 deletions

View File

@ -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 *); struct pic_env *pic_make_env(pic_state *, struct pic_env *);
pic_sym *pic_add_rename(pic_state *, struct pic_env *, pic_sym *); 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 *); void pic_put_rename(pic_state *, struct pic_env *, pic_sym *, pic_sym *);
#if defined(__cplusplus) #if defined(__cplusplus)

View File

@ -73,7 +73,7 @@ import_table(pic_state *pic, pic_value spec, struct pic_dict *imports)
struct pic_lib *lib; struct pic_lib *lib;
struct pic_dict *table; struct pic_dict *table;
pic_value val, tmp, prefix, it; pic_value val, tmp, prefix, it;
pic_sym *sym, *id, *tag; pic_sym *sym, *id, *tag, *nick;
xh_entry *iter; xh_entry *iter;
table = pic_make_dict(pic); table = pic_make_dict(pic);
@ -122,8 +122,15 @@ import_table(pic_state *pic, pic_value spec, struct pic_dict *imports)
if (! lib) { if (! lib) {
pic_errorf(pic, "library not found: ~a", spec); pic_errorf(pic, "library not found: ~a", spec);
} }
pic_dict_for_each (sym, lib->exports, iter) { pic_dict_for_each (nick, lib->exports, iter) {
pic_dict_set(pic, imports, sym, pic_dict_ref(pic, lib->exports, sym)); 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_sym *sRENAME = pic_intern_cstr(pic, "rename");
pic_value a, b; pic_value a, b;
pic_sym *rename;
if (pic_sym_p(spec)) { /* (export a) */ if (pic_sym_p(spec)) { /* (export a) */
a = b = spec; a = b = spec;
@ -165,15 +171,11 @@ export(pic_state *pic, pic_value spec)
goto fail; goto fail;
} }
if (! pic_find_rename(pic, pic->lib->env, pic_sym_ptr(a), &rename)) {
pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, pic_sym_ptr(a)));
}
#if DEBUG #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 #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; return;

View File

@ -7,10 +7,10 @@
pic_sym * pic_sym *
pic_add_rename(pic_state *pic, struct pic_env *env, pic_sym *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); pic_put_rename(pic, env, sym, rename);
return 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)); pic_dict_set(pic, env->map, sym, pic_obj_value(rename));
} }
bool pic_sym *
pic_find_rename(pic_state *pic, struct pic_env *env, pic_sym *sym, pic_sym **rename) pic_find_rename(pic_state *pic, struct pic_env *env, pic_sym *sym)
{ {
if (! pic_dict_has(pic, env->map, sym)) { if (! pic_dict_has(pic, env->map, sym)) {
return false; return NULL;
} }
if (rename != NULL) { return pic_sym_ptr(pic_dict_ref(pic, env->map, sym));
*rename = pic_sym_ptr(pic_dict_ref(pic, env->map, sym));
}
return true;
} }
static void static void
@ -53,7 +50,7 @@ make_identifier(pic_state *pic, pic_sym *sym, struct pic_env *env)
pic_sym *rename; pic_sym *rename;
while (true) { while (true) {
if (pic_find_rename(pic, env, sym, &rename)) { if ((rename = pic_find_rename(pic, env, sym)) != NULL) {
return rename; return rename;
} }
if (! env->up) 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"); pic_errorf(pic, "binding to non-symbol object");
} }
sym = pic_sym_ptr(var); 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); rename = pic_add_rename(pic, env, sym);
} }
val = macroexpand(pic, pic_list_ref(pic, expr, 2), env); 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"); pic_errorf(pic, "binding to non-symbol object");
} }
sym = pic_sym_ptr(var); 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); rename = pic_add_rename(pic, env, sym);
} else { } else {
pic_warnf(pic, "redefining syntax variable: ~s", pic_obj_value(sym)); pic_warnf(pic, "redefining syntax variable: ~s", pic_obj_value(sym));

View File

@ -410,7 +410,7 @@ pic_define_noexport(pic_state *pic, const char *name, pic_value val)
sym = pic_intern_cstr(pic, name); 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); rename = pic_add_rename(pic, pic->lib->env, sym);
} else { } else {
pic_warnf(pic, "redefining global"); 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); 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); 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); 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); pic_errorf(pic, "symbol \"%s\" not defined in library ~s", name, lib->name);
} }

View File

@ -1,6 +1,19 @@
(define-library (picrin macro) (define-library (picrin macro)
(import (picrin base)) (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 ;; assumes no derived expressions are provided yet
(define (walk proc expr) (define (walk proc expr)
@ -125,17 +138,4 @@
(list (r 'define-macro) (car formal) (list (r 'define-macro) (car formal)
(cons (r 'lambda) (cons (r 'lambda)
(cons (cdr formal) (cons (cdr formal)
body))))))) 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))