2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2013-10-30 03:37:43 -04:00
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/pair.h"
|
2014-02-22 01:20:53 -05:00
|
|
|
#include "picrin/string.h"
|
2013-10-30 03:37:43 -04:00
|
|
|
#include "picrin/proc.h"
|
2013-11-26 07:03:52 -05:00
|
|
|
#include "picrin/macro.h"
|
2013-12-07 08:36:14 -05:00
|
|
|
#include "picrin/lib.h"
|
2014-03-24 00:25:37 -04:00
|
|
|
#include "picrin/error.h"
|
2014-03-30 02:29:03 -04:00
|
|
|
#include "picrin/box.h"
|
2013-10-30 03:37:43 -04:00
|
|
|
|
2014-04-04 12:21:16 -04:00
|
|
|
struct pic_senv *
|
2014-04-04 14:50:38 -04:00
|
|
|
pic_null_syntactic_environment(pic_state *pic)
|
2013-11-27 00:19:46 -05:00
|
|
|
{
|
|
|
|
struct pic_senv *senv;
|
|
|
|
|
|
|
|
senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
|
2014-04-04 12:21:16 -04:00
|
|
|
senv->up = NULL;
|
2014-03-25 02:29:26 -04:00
|
|
|
xh_init_int(&senv->renames, sizeof(pic_sym));
|
2013-12-06 04:04:36 -05:00
|
|
|
|
2014-04-04 15:01:42 -04:00
|
|
|
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);
|
2014-04-02 11:55:34 -04:00
|
|
|
|
|
|
|
return senv;
|
|
|
|
}
|
|
|
|
|
2014-04-04 15:01:42 -04:00
|
|
|
void
|
|
|
|
pic_define_syntactic_keyword(pic_state *pic, struct pic_senv *senv, pic_sym sym)
|
|
|
|
{
|
|
|
|
pic_put_rename(pic, senv, sym, sym);
|
|
|
|
|
|
|
|
if (pic->lib && pic->lib->senv == senv) {
|
|
|
|
pic_export(pic, sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-04 12:30:51 -04:00
|
|
|
pic_sym
|
|
|
|
pic_add_rename(pic_state *pic, struct pic_senv *senv, pic_sym sym)
|
2013-11-27 00:00:23 -05:00
|
|
|
{
|
2014-04-04 12:30:51 -04:00
|
|
|
pic_sym rename;
|
2013-11-27 00:00:23 -05:00
|
|
|
|
2014-04-04 12:30:51 -04:00
|
|
|
rename = pic_gensym(pic, sym);
|
|
|
|
pic_put_rename(pic, senv, sym, rename);
|
|
|
|
return rename;
|
|
|
|
}
|
2013-11-27 00:00:23 -05:00
|
|
|
|
2014-04-04 12:30:51 -04:00
|
|
|
void
|
|
|
|
pic_put_rename(pic_state *pic, struct pic_senv *senv, pic_sym sym, pic_sym rename)
|
|
|
|
{
|
|
|
|
UNUSED(pic);
|
|
|
|
|
|
|
|
xh_put(&senv->renames, sym, &rename);
|
2014-04-03 23:55:51 -04:00
|
|
|
}
|
|
|
|
|
2014-04-04 12:30:51 -04:00
|
|
|
bool
|
|
|
|
pic_find_rename(pic_state *pic, struct pic_senv *senv, pic_sym sym, pic_sym *rename)
|
2014-04-03 09:16:09 -04:00
|
|
|
{
|
|
|
|
xh_entry *e;
|
|
|
|
|
2014-04-04 12:30:51 -04:00
|
|
|
UNUSED(pic);
|
|
|
|
|
|
|
|
if ((e = xh_get(&senv->renames, sym)) == NULL) {
|
|
|
|
return false;
|
2014-04-03 09:16:09 -04:00
|
|
|
}
|
2014-04-04 12:30:51 -04:00
|
|
|
if (rename != NULL) {
|
|
|
|
*rename = xh_val(e, pic_sym);
|
|
|
|
}
|
|
|
|
return true;
|
2014-04-03 09:16:09 -04:00
|
|
|
}
|
|
|
|
|
2014-01-08 01:38:31 -05:00
|
|
|
void
|
|
|
|
pic_import(pic_state *pic, pic_value spec)
|
|
|
|
{
|
|
|
|
struct pic_lib *lib;
|
2014-02-06 11:15:17 -05:00
|
|
|
xh_iter it;
|
2014-01-08 01:38:31 -05:00
|
|
|
|
|
|
|
lib = pic_find_library(pic, spec);
|
|
|
|
if (! lib) {
|
|
|
|
pic_error(pic, "library not found");
|
|
|
|
}
|
2014-03-25 02:29:26 -04:00
|
|
|
xh_begin(&it, &lib->exports);
|
|
|
|
while (xh_next(&it)) {
|
2014-02-11 20:39:20 -05:00
|
|
|
|
2014-01-08 01:38:31 -05:00
|
|
|
#if DEBUG
|
2014-03-25 02:29:26 -04:00
|
|
|
printf("* importing %s as %s\n", pic_symbol_name(pic, xh_key(it.e, pic_sym)), pic_symbol_name(pic, xh_val(it.e, pic_sym)));
|
2014-01-08 01:38:31 -05:00
|
|
|
#endif
|
|
|
|
|
2014-03-25 02:29:26 -04:00
|
|
|
pic_put_rename(pic, pic->lib->senv, xh_key(it.e, pic_sym), xh_val(it.e, pic_sym));
|
2014-01-08 01:38:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 23:44:39 -05:00
|
|
|
void
|
|
|
|
pic_export(pic_state *pic, pic_sym sym)
|
|
|
|
{
|
2014-03-25 01:12:53 -04:00
|
|
|
pic_sym rename;
|
2013-12-07 23:44:39 -05:00
|
|
|
|
2014-03-27 03:19:55 -04:00
|
|
|
if (! pic_find_rename(pic, pic->lib->senv, sym, &rename)) {
|
2014-02-13 01:00:48 -05:00
|
|
|
pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, sym));
|
2013-12-08 03:52:34 -05:00
|
|
|
}
|
2014-03-25 02:29:26 -04:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
printf("* exporting %s as %s\n", pic_symbol_name(pic, sym), pic_symbol_name(pic, rename));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
xh_put(&pic->lib->exports, sym, &rename);
|
2013-12-07 23:44:39 -05:00
|
|
|
}
|
2014-04-04 12:30:51 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
define_macro(pic_state *pic, pic_sym rename, struct pic_proc *proc, struct pic_senv *senv)
|
|
|
|
{
|
|
|
|
struct pic_macro *mac;
|
|
|
|
|
|
|
|
mac = (struct pic_macro *)pic_obj_alloc(pic, sizeof(struct pic_macro), PIC_TT_MACRO);
|
|
|
|
mac->senv = senv;
|
|
|
|
mac->proc = proc;
|
|
|
|
|
|
|
|
xh_put(&pic->macros, rename, &mac);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pic_macro *
|
|
|
|
find_macro(pic_state *pic, pic_sym rename)
|
|
|
|
{
|
|
|
|
xh_entry *e;
|
|
|
|
|
|
|
|
if ((e = xh_get(&pic->macros, rename)) == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return xh_val(e, struct pic_macro *);
|
|
|
|
}
|
2013-12-07 23:44:39 -05:00
|
|
|
|
2014-02-12 23:42:17 -05:00
|
|
|
void
|
|
|
|
pic_defmacro(pic_state *pic, const char *name, struct pic_proc *macro)
|
2013-11-27 00:00:23 -05:00
|
|
|
{
|
2014-03-25 01:12:53 -04:00
|
|
|
pic_sym sym, rename;
|
2013-11-27 00:00:23 -05:00
|
|
|
|
2014-02-12 23:42:17 -05:00
|
|
|
/* symbol registration */
|
|
|
|
sym = pic_intern_cstr(pic, name);
|
2014-03-25 01:12:53 -04:00
|
|
|
rename = pic_add_rename(pic, pic->lib->senv, sym);
|
2014-04-03 23:55:51 -04:00
|
|
|
define_macro(pic, rename, macro, NULL);
|
2014-02-08 01:24:27 -05:00
|
|
|
|
|
|
|
/* auto export! */
|
2014-02-13 01:01:01 -05:00
|
|
|
pic_export(pic, sym);
|
2013-11-27 01:58:28 -05:00
|
|
|
}
|
|
|
|
|
2014-04-02 12:15:39 -04:00
|
|
|
static pic_value macroexpand_node(pic_state *, pic_value, struct pic_senv *, pic_value);
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv, pic_value assoc_box)
|
|
|
|
{
|
|
|
|
int ai = pic_gc_arena_preserve(pic);
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
v = macroexpand_node(pic, expr, senv, assoc_box);
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
pic_gc_protect(pic, v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2014-04-04 14:49:20 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-04 12:23:20 -04:00
|
|
|
static pic_value
|
|
|
|
macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv, pic_value assoc_box)
|
|
|
|
{
|
|
|
|
int ai = pic_gc_arena_preserve(pic);
|
|
|
|
pic_value v, vs;
|
|
|
|
|
|
|
|
/* macroexpand in order */
|
|
|
|
vs = pic_nil_value();
|
|
|
|
while (pic_pair_p(list)) {
|
|
|
|
v = pic_car(pic, list);
|
|
|
|
|
|
|
|
vs = pic_cons(pic, macroexpand(pic, v, senv, assoc_box), vs);
|
|
|
|
list = pic_cdr(pic, list);
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
pic_gc_protect(pic, vs);
|
|
|
|
pic_gc_protect(pic, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
list = macroexpand(pic, list, senv, assoc_box);
|
|
|
|
|
|
|
|
/* reverse the result */
|
|
|
|
pic_for_each (v, vs) {
|
|
|
|
list = pic_cons(pic, v, list);
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
pic_gc_protect(pic, vs);
|
|
|
|
pic_gc_protect(pic, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
pic_gc_protect(pic, list);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2014-02-11 20:39:20 -05:00
|
|
|
static pic_sym
|
2014-04-02 12:15:39 -04:00
|
|
|
macroexpand_symbol(pic_state *pic, pic_sym sym, struct pic_senv *senv, pic_value assoc_box)
|
2014-02-11 20:39:20 -05:00
|
|
|
{
|
2014-03-25 01:12:53 -04:00
|
|
|
pic_sym rename;
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_value x;
|
2014-02-11 20:39:20 -05:00
|
|
|
|
|
|
|
if (! pic_interned_p(pic, sym)) {
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
while (true) {
|
2014-03-27 03:19:55 -04:00
|
|
|
if (pic_find_rename(pic, senv, sym, &rename)) {
|
2014-03-25 01:12:53 -04:00
|
|
|
return rename;
|
2014-02-11 20:39:20 -05:00
|
|
|
}
|
|
|
|
if (! senv->up)
|
|
|
|
break;
|
|
|
|
senv = senv->up;
|
|
|
|
}
|
2014-03-30 02:29:03 -04:00
|
|
|
x = pic_assq(pic, pic_sym_value(sym), pic_unbox(pic, assoc_box));
|
|
|
|
if (pic_test(x)) {
|
|
|
|
return pic_sym(pic_cdr(pic, x));
|
2014-03-29 08:47:57 -04:00
|
|
|
} else {
|
|
|
|
rename = pic_gensym(pic, sym);
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_set_box(pic, assoc_box, pic_acons(pic, pic_sym_value(sym), pic_sym_value(rename), pic_unbox(pic, assoc_box)));
|
2014-03-29 08:47:57 -04:00
|
|
|
return rename;
|
|
|
|
}
|
2014-02-11 20:39:20 -05:00
|
|
|
}
|
|
|
|
|
2014-04-03 09:01:25 -04:00
|
|
|
static pic_value
|
|
|
|
macroexpand_deflibrary(pic_state *pic, pic_value expr)
|
|
|
|
{
|
|
|
|
struct pic_lib *prev = pic->lib;
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
if (pic_length(pic, expr) < 2) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_make_library(pic, pic_cadr(pic, expr));
|
|
|
|
|
|
|
|
pic_try {
|
|
|
|
pic_in_library(pic, pic_cadr(pic, expr));
|
|
|
|
|
|
|
|
pic_for_each (v, pic_cddr(pic, expr)) {
|
|
|
|
int ai = pic_gc_arena_preserve(pic);
|
|
|
|
|
|
|
|
pic_eval(pic, v);
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_in_library(pic, prev->name);
|
|
|
|
}
|
|
|
|
pic_catch {
|
|
|
|
/* restores pic->lib even if an error occurs */
|
|
|
|
pic_in_library(pic, prev->name);
|
|
|
|
pic_throw(pic, pic->err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
macroexpand_import(pic_state *pic, pic_value expr)
|
|
|
|
{
|
|
|
|
pic_value spec;
|
|
|
|
|
|
|
|
pic_for_each (spec, pic_cdr(pic, expr)) {
|
|
|
|
pic_import(pic, spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
macroexpand_export(pic_state *pic, pic_value expr)
|
|
|
|
{
|
|
|
|
pic_value spec;
|
|
|
|
|
|
|
|
pic_for_each (spec, pic_cdr(pic, expr)) {
|
|
|
|
if (! pic_sym_p(spec)) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
/* TODO: warn if symbol is shadowed by local variable */
|
|
|
|
pic_export(pic, pic_sym(spec));
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
macroexpand_defsyntax(pic_state *pic, pic_value expr, struct pic_senv *senv, pic_value assoc_box)
|
|
|
|
{
|
|
|
|
pic_value var, val;
|
|
|
|
pic_sym sym, rename;
|
|
|
|
|
|
|
|
if (pic_length(pic, expr) != 3) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
|
|
|
|
var = pic_cadr(pic, expr);
|
|
|
|
if (! pic_sym_p(var)) {
|
|
|
|
var = macroexpand(pic, var, senv, assoc_box);
|
|
|
|
}
|
|
|
|
if (! pic_sym_p(var)) {
|
|
|
|
pic_error(pic, "binding to non-symbol object");
|
|
|
|
}
|
|
|
|
sym = pic_sym(var);
|
|
|
|
if (! pic_find_rename(pic, senv, sym, &rename)) {
|
|
|
|
rename = pic_add_rename(pic, senv, sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
val = pic_cadr(pic, pic_cdr(pic, expr));
|
|
|
|
|
|
|
|
pic_try {
|
|
|
|
val = pic_eval(pic, val);
|
|
|
|
} pic_catch {
|
|
|
|
pic_errorf(pic, "macroexpand error: %s", pic_errmsg(pic));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! pic_proc_p(val)) {
|
|
|
|
pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", var);
|
|
|
|
}
|
|
|
|
|
2014-04-03 23:55:51 -04:00
|
|
|
define_macro(pic, rename, pic_proc_ptr(val), senv);
|
2014-04-03 09:01:25 -04:00
|
|
|
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
macroexpand_defmacro(pic_state *pic, pic_value expr, struct pic_senv *senv)
|
|
|
|
{
|
|
|
|
pic_value var, val;
|
|
|
|
pic_sym sym, rename;
|
|
|
|
|
|
|
|
if (pic_length(pic, expr) < 2) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
|
|
|
|
var = pic_car(pic, pic_cdr(pic, expr));
|
|
|
|
if (pic_pair_p(var)) {
|
|
|
|
/* FIXME: unhygienic */
|
2014-04-04 13:21:07 -04:00
|
|
|
val = pic_cons(pic, pic_sym_value(pic->sLAMBDA),
|
2014-04-03 09:01:25 -04:00
|
|
|
pic_cons(pic, pic_cdr(pic, var),
|
|
|
|
pic_cdr(pic, pic_cdr(pic, expr))));
|
|
|
|
var = pic_car(pic, var);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (pic_length(pic, expr) != 3) {
|
|
|
|
pic_error(pic, "syntax_error");
|
|
|
|
}
|
|
|
|
val = pic_car(pic, pic_cdr(pic, pic_cdr(pic, expr)));
|
|
|
|
}
|
|
|
|
if (! pic_sym_p(var)) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
sym = pic_sym(var);
|
|
|
|
if (! pic_find_rename(pic, senv, sym, &rename)) {
|
|
|
|
rename = pic_add_rename(pic, senv, sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_try {
|
|
|
|
val = pic_eval(pic, val);
|
|
|
|
} pic_catch {
|
|
|
|
pic_errorf(pic, "macroexpand error: %s", pic_errmsg(pic));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! pic_proc_p(val)) {
|
|
|
|
pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", var);
|
|
|
|
}
|
|
|
|
|
2014-04-03 23:55:51 -04:00
|
|
|
define_macro(pic, rename, pic_proc_ptr(val), NULL);
|
2014-04-03 09:01:25 -04:00
|
|
|
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
macroexpand_define(pic_state *pic, pic_value expr, struct pic_senv *senv, pic_value assoc_box)
|
|
|
|
{
|
|
|
|
pic_sym sym;
|
|
|
|
pic_value formals;
|
|
|
|
|
|
|
|
if (pic_length(pic, expr) < 2) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
|
|
|
|
formals = pic_cadr(pic, expr);
|
|
|
|
if (pic_pair_p(formals)) {
|
2014-04-04 12:20:19 -04:00
|
|
|
struct pic_senv *in = push_scope(pic, pic_cdr(pic, formals), senv, assoc_box);
|
2014-04-03 09:01:25 -04:00
|
|
|
pic_value a;
|
|
|
|
|
|
|
|
/* defined symbol */
|
|
|
|
a = pic_car(pic, formals);
|
|
|
|
if (! pic_sym_p(a)) {
|
|
|
|
a = macroexpand(pic, a, senv, assoc_box);
|
|
|
|
}
|
|
|
|
if (! pic_sym_p(a)) {
|
|
|
|
pic_error(pic, "binding to non-symbol object");
|
|
|
|
}
|
|
|
|
sym = pic_sym(a);
|
|
|
|
if (! pic_find_rename(pic, senv, sym, NULL)) {
|
|
|
|
pic_add_rename(pic, senv, sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* binding value */
|
|
|
|
return pic_cons(pic, pic_sym_value(pic->sDEFINE),
|
|
|
|
pic_cons(pic,
|
|
|
|
macroexpand_list(pic, pic_cadr(pic, expr), in, assoc_box),
|
|
|
|
macroexpand_list(pic, pic_cddr(pic, expr), in, assoc_box)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! pic_sym_p(formals)) {
|
|
|
|
formals = macroexpand(pic, formals, senv, assoc_box);
|
|
|
|
}
|
|
|
|
if (! pic_sym_p(formals)) {
|
|
|
|
pic_error(pic, "binding to non-symbol object");
|
|
|
|
}
|
|
|
|
sym = pic_sym(formals);
|
|
|
|
if (! pic_find_rename(pic, senv, sym, NULL)) {
|
|
|
|
pic_add_rename(pic, senv, sym);
|
|
|
|
}
|
|
|
|
|
2014-04-04 13:21:07 -04:00
|
|
|
return pic_cons(pic, pic_sym_value(pic->sDEFINE), macroexpand_list(pic, pic_cdr(pic, expr), senv, assoc_box));
|
2014-04-03 09:01:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
macroexpand_lambda(pic_state *pic, pic_value expr, struct pic_senv *senv, pic_value assoc_box)
|
|
|
|
{
|
2014-04-04 12:20:19 -04:00
|
|
|
struct pic_senv *in = push_scope(pic, pic_cadr(pic, expr), senv, assoc_box);
|
2014-04-03 09:01:25 -04:00
|
|
|
|
|
|
|
return pic_cons(pic, pic_sym_value(pic->sLAMBDA),
|
|
|
|
pic_cons(pic,
|
|
|
|
macroexpand_list(pic, pic_cadr(pic, expr), in, assoc_box),
|
|
|
|
macroexpand_list(pic, pic_cddr(pic, expr), in, assoc_box)));
|
|
|
|
}
|
|
|
|
|
2014-04-03 09:27:36 -04:00
|
|
|
static pic_value
|
|
|
|
macroexpand_quote(pic_state *pic, pic_value expr)
|
|
|
|
{
|
|
|
|
return pic_cons(pic, pic_sym_value(pic->sQUOTE), pic_cdr(pic, expr));
|
|
|
|
}
|
|
|
|
|
2014-04-03 09:16:09 -04:00
|
|
|
static pic_value
|
|
|
|
macroexpand_macro(pic_state *pic, struct pic_macro *mac, pic_value expr, struct pic_senv *senv, pic_value assoc_box)
|
|
|
|
{
|
|
|
|
pic_value v, args;
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("before expand-1:");
|
|
|
|
pic_debug(pic, expr);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mac->senv == NULL) { /* legacy macro */
|
|
|
|
args = pic_cdr(pic, expr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
args = pic_list3(pic, expr, pic_obj_value(senv), pic_obj_value(mac->senv));
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_try {
|
|
|
|
v = pic_apply(pic, mac->proc, args);
|
|
|
|
} pic_catch {
|
|
|
|
pic_errorf(pic, "macroexpand error: %s", pic_errmsg(pic));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("after expand-1:");
|
|
|
|
pic_debug(pic, v);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return macroexpand(pic, v, senv, assoc_box);
|
|
|
|
}
|
|
|
|
|
2014-03-25 02:42:20 -04:00
|
|
|
static pic_value
|
2014-03-30 02:29:03 -04:00
|
|
|
macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv, pic_value assoc_box)
|
2014-03-25 02:42:20 -04:00
|
|
|
{
|
2013-12-10 04:50:53 -05:00
|
|
|
#if DEBUG
|
|
|
|
printf("[macroexpand] expanding... ");
|
|
|
|
pic_debug(pic, expr);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
switch (pic_type(expr)) {
|
2013-11-27 01:04:02 -05:00
|
|
|
case PIC_TT_SC: {
|
2014-04-04 00:07:16 -04:00
|
|
|
return macroexpand(pic, pic_sc_ptr(expr)->expr, pic_sc_ptr(expr)->senv, assoc_box);
|
2013-11-27 01:04:02 -05:00
|
|
|
}
|
2013-10-30 03:37:43 -04:00
|
|
|
case PIC_TT_SYMBOL: {
|
2014-04-04 13:21:07 -04:00
|
|
|
return pic_sym_value(macroexpand_symbol(pic, pic_sym(expr), senv, assoc_box));
|
2013-10-30 03:37:43 -04:00
|
|
|
}
|
|
|
|
case PIC_TT_PAIR: {
|
2014-04-03 09:01:25 -04:00
|
|
|
pic_value car;
|
2014-04-03 09:16:09 -04:00
|
|
|
struct pic_macro *mac;
|
2013-10-30 03:37:43 -04:00
|
|
|
|
2014-03-31 22:40:36 -04:00
|
|
|
if (! pic_list_p(expr)) {
|
|
|
|
pic_errorf(pic, "cannot macroexpand improper list: ~s", expr);
|
|
|
|
}
|
|
|
|
|
2014-03-30 02:29:03 -04:00
|
|
|
car = macroexpand(pic, pic_car(pic, expr), senv, assoc_box);
|
2014-02-11 20:39:20 -05:00
|
|
|
if (pic_sym_p(car)) {
|
|
|
|
pic_sym tag = pic_sym(car);
|
|
|
|
|
|
|
|
if (tag == pic->sDEFINE_LIBRARY) {
|
2014-04-03 09:01:25 -04:00
|
|
|
return macroexpand_deflibrary(pic, expr);
|
2013-12-07 06:02:39 -05:00
|
|
|
}
|
2014-02-11 20:39:20 -05:00
|
|
|
else if (tag == pic->sIMPORT) {
|
2014-04-03 09:01:25 -04:00
|
|
|
return macroexpand_import(pic, expr);
|
2013-12-07 12:42:34 -05:00
|
|
|
}
|
2014-02-11 20:39:20 -05:00
|
|
|
else if (tag == pic->sEXPORT) {
|
2014-04-03 09:01:25 -04:00
|
|
|
return macroexpand_export(pic, expr);
|
2013-12-07 06:02:39 -05:00
|
|
|
}
|
2014-02-11 20:39:20 -05:00
|
|
|
else if (tag == pic->sDEFINE_SYNTAX) {
|
2014-04-03 09:01:25 -04:00
|
|
|
return macroexpand_defsyntax(pic, expr, senv, assoc_box);
|
2013-11-27 01:58:28 -05:00
|
|
|
}
|
2014-02-11 20:39:20 -05:00
|
|
|
else if (tag == pic->sDEFINE_MACRO) {
|
2014-04-03 09:01:25 -04:00
|
|
|
return macroexpand_defmacro(pic, expr, senv);
|
2013-10-30 03:37:43 -04:00
|
|
|
}
|
2014-02-11 20:39:20 -05:00
|
|
|
else if (tag == pic->sLAMBDA) {
|
2014-04-03 09:01:25 -04:00
|
|
|
return macroexpand_lambda(pic, expr, senv, assoc_box);
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
2014-02-11 20:39:20 -05:00
|
|
|
else if (tag == pic->sDEFINE) {
|
2014-04-03 09:01:25 -04:00
|
|
|
return macroexpand_define(pic, expr, senv, assoc_box);
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
2014-02-11 20:39:20 -05:00
|
|
|
else if (tag == pic->sQUOTE) {
|
2014-04-03 09:27:36 -04:00
|
|
|
return macroexpand_quote(pic, expr);
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
2014-02-11 20:39:20 -05:00
|
|
|
|
2014-04-03 09:16:09 -04:00
|
|
|
if ((mac = find_macro(pic, tag)) != NULL) {
|
|
|
|
return macroexpand_macro(pic, mac, expr, senv, assoc_box);
|
2014-02-11 20:39:20 -05:00
|
|
|
}
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
|
|
|
|
2014-03-30 02:29:03 -04:00
|
|
|
return pic_cons(pic, car, macroexpand_list(pic, pic_cdr(pic, expr), senv, assoc_box));
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
|
|
|
case PIC_TT_EOF:
|
|
|
|
case PIC_TT_NIL:
|
|
|
|
case PIC_TT_BOOL:
|
|
|
|
case PIC_TT_FLOAT:
|
|
|
|
case PIC_TT_INT:
|
|
|
|
case PIC_TT_CHAR:
|
|
|
|
case PIC_TT_STRING:
|
|
|
|
case PIC_TT_VECTOR:
|
|
|
|
case PIC_TT_BLOB: {
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
case PIC_TT_PROC:
|
|
|
|
case PIC_TT_PORT:
|
|
|
|
case PIC_TT_ERROR:
|
|
|
|
case PIC_TT_ENV:
|
|
|
|
case PIC_TT_CONT:
|
|
|
|
case PIC_TT_UNDEF:
|
|
|
|
case PIC_TT_SENV:
|
2014-02-11 21:13:29 -05:00
|
|
|
case PIC_TT_MACRO:
|
2013-12-07 06:58:18 -05:00
|
|
|
case PIC_TT_LIB:
|
2014-01-08 10:39:13 -05:00
|
|
|
case PIC_TT_VAR:
|
2014-01-18 02:51:54 -05:00
|
|
|
case PIC_TT_IREP:
|
2014-03-29 07:42:06 -04:00
|
|
|
case PIC_TT_DATA:
|
2014-03-29 03:55:37 -04:00
|
|
|
case PIC_TT_BOX:
|
2014-03-30 19:27:04 -04:00
|
|
|
pic_errorf(pic, "unexpected value type: ~s", expr);
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
2014-03-24 02:30:32 -04:00
|
|
|
UNREACHABLE();
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
2013-11-26 11:43:58 -05:00
|
|
|
pic_macroexpand(pic_state *pic, pic_value expr)
|
2013-11-26 07:06:46 -05:00
|
|
|
{
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_value v, box;
|
2013-11-26 07:06:46 -05:00
|
|
|
|
2013-11-26 11:38:26 -05:00
|
|
|
#if DEBUG
|
|
|
|
puts("before expand:");
|
|
|
|
pic_debug(pic, expr);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
2014-03-30 02:29:03 -04:00
|
|
|
box = pic_box(pic, pic_nil_value());
|
2014-03-29 08:47:57 -04:00
|
|
|
|
2014-03-30 02:29:03 -04:00
|
|
|
v = macroexpand(pic, expr, pic->lib->senv, box);
|
2013-11-26 11:38:26 -05:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("after expand:");
|
|
|
|
pic_debug(pic, v);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return v;
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
2013-11-27 01:04:44 -05:00
|
|
|
|
2014-02-08 01:05:28 -05:00
|
|
|
/* once read.c is implemented move there */
|
|
|
|
static pic_value
|
|
|
|
pic_macro_include(pic_state *pic)
|
|
|
|
{
|
|
|
|
size_t argc, i;
|
|
|
|
pic_value *argv, exprs, body;
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
pic_get_args(pic, "*", &argc, &argv);
|
|
|
|
|
|
|
|
/* FIXME unhygienic */
|
2014-04-04 13:21:07 -04:00
|
|
|
body = pic_list1(pic, pic_sym_value(pic->sBEGIN));
|
2014-02-08 01:05:28 -05:00
|
|
|
|
|
|
|
for (i = 0; i < argc; ++i) {
|
2014-02-27 07:54:37 -05:00
|
|
|
const char *filename;
|
2014-02-08 01:05:28 -05:00
|
|
|
if (! pic_str_p(argv[i])) {
|
|
|
|
pic_error(pic, "expected string");
|
|
|
|
}
|
2014-02-27 07:54:37 -05:00
|
|
|
filename = pic_str_cstr(pic_str_ptr(argv[i]));
|
2014-02-08 01:05:28 -05:00
|
|
|
file = fopen(filename, "r");
|
|
|
|
if (file == NULL) {
|
|
|
|
pic_error(pic, "could not open file");
|
|
|
|
}
|
2014-03-03 10:26:33 -05:00
|
|
|
exprs = pic_parse_file(pic, file);
|
2014-03-01 06:21:44 -05:00
|
|
|
if (pic_undef_p(exprs)) {
|
2014-02-08 01:05:28 -05:00
|
|
|
pic_error(pic, "parse error");
|
|
|
|
}
|
|
|
|
body = pic_append(pic, body, exprs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2014-02-13 00:59:51 -05:00
|
|
|
static pic_value
|
|
|
|
pic_macro_gensym(pic_state *pic)
|
|
|
|
{
|
|
|
|
static const char skel[] = ".g";
|
|
|
|
pic_sym uniq;
|
|
|
|
|
|
|
|
pic_get_args(pic, "");
|
|
|
|
|
|
|
|
uniq = pic_gensym(pic, pic_intern_cstr(pic, skel));
|
2014-04-04 13:21:07 -04:00
|
|
|
return pic_sym_value(uniq);
|
2014-02-13 00:59:51 -05:00
|
|
|
}
|
|
|
|
|
2014-02-14 23:02:06 -05:00
|
|
|
static pic_value
|
|
|
|
pic_macro_macroexpand(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value expr;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &expr);
|
|
|
|
|
|
|
|
return pic_macroexpand(pic, expr);
|
|
|
|
}
|
|
|
|
|
2014-04-04 12:28:57 -04:00
|
|
|
static struct pic_sc *
|
|
|
|
sc_new(pic_state *pic, pic_value expr, struct pic_senv *senv)
|
|
|
|
{
|
|
|
|
struct pic_sc *sc;
|
|
|
|
|
|
|
|
sc = (struct pic_sc *)pic_obj_alloc(pic, sizeof(struct pic_sc), PIC_TT_SC);
|
|
|
|
sc->expr = expr;
|
|
|
|
sc->senv = senv;
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sc_identifier_p(pic_value obj)
|
|
|
|
{
|
|
|
|
if (pic_sym_p(obj)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (pic_sc_p(obj)) {
|
|
|
|
return sc_identifier_p(pic_sc_ptr(obj)->expr);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sc_identifier_eq_p(pic_state *pic, struct pic_senv *e1, pic_value x, struct pic_senv *e2, pic_value y)
|
|
|
|
{
|
|
|
|
pic_value box;
|
|
|
|
|
|
|
|
if (! (sc_identifier_p(x) && sc_identifier_p(y))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
box = pic_box(pic, pic_nil_value());
|
|
|
|
|
|
|
|
x = macroexpand(pic, x, e1, box);
|
|
|
|
y = macroexpand(pic, y, e2, box);
|
|
|
|
|
|
|
|
return pic_eq_p(x, y);
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:04:44 -05:00
|
|
|
static pic_value
|
2013-11-27 03:26:07 -05:00
|
|
|
pic_macro_make_sc(pic_state *pic)
|
2013-11-27 01:04:44 -05:00
|
|
|
{
|
|
|
|
pic_value senv, free_vars, expr;
|
|
|
|
struct pic_sc *sc;
|
|
|
|
|
|
|
|
pic_get_args(pic, "ooo", &senv, &free_vars, &expr);
|
|
|
|
|
|
|
|
if (! pic_senv_p(senv))
|
|
|
|
pic_error(pic, "make-syntactic-closure: senv required");
|
|
|
|
|
|
|
|
/* just ignore free_vars for now */
|
2014-04-04 00:07:16 -04:00
|
|
|
sc = sc_new(pic, expr, pic_senv_ptr(senv));
|
2013-11-27 01:04:44 -05:00
|
|
|
|
|
|
|
return pic_obj_value(sc);
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:26:07 -05:00
|
|
|
static pic_value
|
|
|
|
pic_macro_identifier_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value obj;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &obj);
|
|
|
|
|
2014-04-04 12:28:57 -04:00
|
|
|
return pic_bool_value(sc_identifier_p(obj));
|
2013-11-27 03:26:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_macro_identifier_eq_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value e, x, f, y;
|
|
|
|
struct pic_senv *e1, *e2;
|
|
|
|
|
|
|
|
pic_get_args(pic, "oooo", &e, &x, &f, &y);
|
|
|
|
|
|
|
|
if (! pic_senv_p(e)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 1");
|
|
|
|
}
|
2014-04-04 00:07:16 -04:00
|
|
|
e1 = pic_senv_ptr(e);
|
2013-11-27 03:26:07 -05:00
|
|
|
if (! pic_senv_p(f)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 3");
|
|
|
|
}
|
2014-04-04 00:07:16 -04:00
|
|
|
e2 = pic_senv_ptr(f);
|
2013-11-27 03:26:07 -05:00
|
|
|
|
2014-04-04 12:28:57 -04:00
|
|
|
return pic_bool_value(sc_identifier_eq_p(pic, e1, x, e2, y));
|
2013-11-27 03:26:07 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 02:34:22 -05:00
|
|
|
static pic_value
|
|
|
|
er_macro_rename(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_sym sym;
|
|
|
|
struct pic_senv *mac_env;
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_value assoc_box;
|
2014-01-09 02:34:22 -05:00
|
|
|
|
|
|
|
pic_get_args(pic, "m", &sym);
|
|
|
|
|
|
|
|
mac_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 1));
|
2014-03-30 02:29:03 -04:00
|
|
|
assoc_box = pic_proc_cv_ref(pic, pic_get_proc(pic), 2);
|
2014-01-09 02:34:22 -05:00
|
|
|
|
2014-04-04 13:21:07 -04:00
|
|
|
return pic_sym_value(macroexpand_symbol(pic, sym, mac_env, assoc_box));
|
2014-01-09 02:34:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
er_macro_compare(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value a, b;
|
2014-01-10 03:52:31 -05:00
|
|
|
struct pic_senv *use_env;
|
2014-02-11 21:20:38 -05:00
|
|
|
pic_sym m, n;
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_value assoc_box;
|
2014-01-10 03:52:31 -05:00
|
|
|
|
|
|
|
pic_get_args(pic, "oo", &a, &b);
|
2014-01-09 02:34:22 -05:00
|
|
|
|
2014-01-30 13:03:36 -05:00
|
|
|
if (! pic_sym_p(a) || ! pic_sym_p(b))
|
2014-01-10 03:52:31 -05:00
|
|
|
return pic_false_value(); /* should be an error? */
|
2014-01-09 02:34:22 -05:00
|
|
|
|
|
|
|
use_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
|
2014-03-30 02:29:03 -04:00
|
|
|
assoc_box = pic_proc_cv_ref(pic, pic_get_proc(pic), 2);
|
2014-01-09 02:34:22 -05:00
|
|
|
|
2014-04-02 12:15:39 -04:00
|
|
|
m = macroexpand_symbol(pic, pic_sym(a), use_env, assoc_box);
|
|
|
|
n = macroexpand_symbol(pic, pic_sym(b), use_env, assoc_box);
|
2014-01-09 02:34:22 -05:00
|
|
|
|
2014-02-11 21:20:38 -05:00
|
|
|
return pic_bool_value(m == n);
|
2014-01-09 02:34:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
er_macro_call(pic_state *pic)
|
|
|
|
{
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_value expr, use_env, mac_env, box;
|
2014-01-09 02:34:22 -05:00
|
|
|
struct pic_proc *rename, *compare, *cb;
|
|
|
|
|
|
|
|
pic_get_args(pic, "ooo", &expr, &use_env, &mac_env);
|
|
|
|
|
|
|
|
if (! pic_senv_p(use_env)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 1");
|
|
|
|
}
|
|
|
|
if (! pic_senv_p(mac_env)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 3");
|
|
|
|
}
|
|
|
|
|
2014-03-30 02:29:03 -04:00
|
|
|
box = pic_box(pic, pic_nil_value());
|
2014-03-29 08:47:57 -04:00
|
|
|
|
2014-03-26 08:20:06 -04:00
|
|
|
rename = pic_proc_new(pic, er_macro_rename, "<er-macro-renamer>");
|
2014-03-29 08:47:57 -04:00
|
|
|
pic_proc_cv_init(pic, rename, 3);
|
2014-01-09 02:34:22 -05:00
|
|
|
pic_proc_cv_set(pic, rename, 0, use_env);
|
|
|
|
pic_proc_cv_set(pic, rename, 1, mac_env);
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_proc_cv_set(pic, rename, 2, box);
|
2014-01-09 02:34:22 -05:00
|
|
|
|
2014-03-26 08:20:06 -04:00
|
|
|
compare = pic_proc_new(pic, er_macro_compare, "<er-macro-comparator>");
|
2014-03-29 08:47:57 -04:00
|
|
|
pic_proc_cv_init(pic, compare, 3);
|
2014-01-09 02:34:22 -05:00
|
|
|
pic_proc_cv_set(pic, compare, 0, use_env);
|
|
|
|
pic_proc_cv_set(pic, compare, 1, mac_env);
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_proc_cv_set(pic, compare, 2, box);
|
2014-01-09 02:34:22 -05:00
|
|
|
|
|
|
|
cb = pic_proc_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
|
|
|
|
|
2014-03-25 08:04:30 -04:00
|
|
|
return pic_apply3(pic, cb, expr, pic_obj_value(rename), pic_obj_value(compare));
|
2014-01-09 02:34:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_macro_er_macro_transformer(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *cb, *proc;
|
|
|
|
|
|
|
|
pic_get_args(pic, "l", &cb);
|
|
|
|
|
2014-03-26 08:20:06 -04:00
|
|
|
proc = pic_proc_new(pic, er_macro_call, "<er-macro-procedure>");
|
2014-01-11 23:02:16 -05:00
|
|
|
pic_proc_cv_init(pic, proc, 1);
|
2014-01-09 02:34:22 -05:00
|
|
|
pic_proc_cv_set(pic, proc, 0, pic_obj_value(cb));
|
|
|
|
|
|
|
|
return pic_obj_value(proc);
|
|
|
|
}
|
|
|
|
|
2014-01-19 04:14:32 -05:00
|
|
|
static pic_value
|
|
|
|
ir_macro_inject(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_sym sym;
|
|
|
|
struct pic_senv *use_env;
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_value assoc_box;
|
2014-01-19 04:14:32 -05:00
|
|
|
|
|
|
|
pic_get_args(pic, "m", &sym);
|
|
|
|
|
|
|
|
use_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
|
2014-03-30 02:29:03 -04:00
|
|
|
assoc_box = pic_proc_cv_ref(pic, pic_get_proc(pic), 2);
|
2014-01-19 04:14:32 -05:00
|
|
|
|
2014-04-04 13:21:07 -04:00
|
|
|
return pic_sym_value(macroexpand_symbol(pic, sym, use_env, assoc_box));
|
2014-01-19 04:14:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
ir_macro_compare(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value a, b;
|
2014-02-19 21:58:27 -05:00
|
|
|
struct pic_senv *mac_env;
|
2014-02-11 21:20:38 -05:00
|
|
|
pic_sym m, n;
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_value assoc_box;
|
2014-01-19 04:14:32 -05:00
|
|
|
|
|
|
|
pic_get_args(pic, "oo", &a, &b);
|
|
|
|
|
|
|
|
if (! pic_sym_p(a) || ! pic_sym_p(b))
|
|
|
|
return pic_false_value(); /* should be an error? */
|
|
|
|
|
2014-02-19 21:58:27 -05:00
|
|
|
mac_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 1));
|
2014-03-30 02:29:03 -04:00
|
|
|
assoc_box = pic_proc_cv_ref(pic, pic_get_proc(pic), 2);
|
2014-01-19 04:14:32 -05:00
|
|
|
|
2014-04-02 12:15:39 -04:00
|
|
|
m = macroexpand_symbol(pic, pic_sym(a), mac_env, assoc_box);
|
|
|
|
n = macroexpand_symbol(pic, pic_sym(b), mac_env, assoc_box);
|
2014-01-19 04:14:32 -05:00
|
|
|
|
2014-02-11 21:20:38 -05:00
|
|
|
return pic_bool_value(m == n);
|
2014-01-19 04:14:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-03-30 02:29:03 -04:00
|
|
|
ir_macro_wrap(pic_state *pic, pic_value expr, struct pic_senv *use_env, pic_value assoc_box, pic_value *ir)
|
2014-01-19 04:14:32 -05:00
|
|
|
{
|
|
|
|
if (pic_sym_p(expr)) {
|
2014-03-28 20:04:14 -04:00
|
|
|
pic_value r;
|
2014-04-02 12:15:39 -04:00
|
|
|
r = pic_sym_value(macroexpand_symbol(pic, pic_sym(expr), use_env, assoc_box));
|
2014-03-30 02:29:03 -04:00
|
|
|
*ir = pic_acons(pic, r, expr, *ir);
|
2014-03-28 20:04:14 -04:00
|
|
|
return r;
|
2014-01-19 04:14:32 -05:00
|
|
|
}
|
|
|
|
else if (pic_pair_p(expr)) {
|
|
|
|
return pic_cons(pic,
|
2014-03-30 02:29:03 -04:00
|
|
|
ir_macro_wrap(pic, pic_car(pic, expr), use_env, assoc_box, ir),
|
|
|
|
ir_macro_wrap(pic, pic_cdr(pic, expr), use_env, assoc_box, ir));
|
2014-01-19 04:14:32 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-03-30 02:29:03 -04:00
|
|
|
ir_macro_unwrap(pic_state *pic, pic_value expr, struct pic_senv *mac_env, pic_value assoc_box, pic_value *ir)
|
2014-01-19 04:14:32 -05:00
|
|
|
{
|
2014-03-28 19:57:15 -04:00
|
|
|
if (pic_sym_p(expr)) {
|
2014-01-19 04:14:32 -05:00
|
|
|
pic_value r;
|
2014-03-30 02:29:03 -04:00
|
|
|
if (pic_test(r = pic_assq(pic, expr, *ir))) {
|
2014-01-19 04:14:32 -05:00
|
|
|
return pic_cdr(pic, r);
|
|
|
|
}
|
2014-04-02 12:15:39 -04:00
|
|
|
return pic_sym_value(macroexpand_symbol(pic, pic_sym(expr), mac_env, assoc_box));
|
2014-01-19 04:14:32 -05:00
|
|
|
}
|
|
|
|
else if (pic_pair_p(expr)) {
|
|
|
|
return pic_cons(pic,
|
2014-03-30 02:29:03 -04:00
|
|
|
ir_macro_unwrap(pic, pic_car(pic, expr), mac_env, assoc_box, ir),
|
|
|
|
ir_macro_unwrap(pic, pic_cdr(pic, expr), mac_env, assoc_box, ir));
|
2014-01-19 04:14:32 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
ir_macro_call(pic_state *pic)
|
|
|
|
{
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_value expr, use_env, mac_env, box;
|
2014-01-19 04:14:32 -05:00
|
|
|
struct pic_proc *inject, *compare, *cb;
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_value ir = pic_nil_value();
|
2014-01-19 04:14:32 -05:00
|
|
|
|
|
|
|
pic_get_args(pic, "ooo", &expr, &use_env, &mac_env);
|
|
|
|
|
|
|
|
if (! pic_senv_p(use_env)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 1");
|
|
|
|
}
|
|
|
|
if (! pic_senv_p(mac_env)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 3");
|
|
|
|
}
|
|
|
|
|
2014-03-30 02:29:03 -04:00
|
|
|
box = pic_box(pic, pic_nil_value());
|
2014-03-29 08:47:57 -04:00
|
|
|
|
2014-03-26 08:20:06 -04:00
|
|
|
inject = pic_proc_new(pic, ir_macro_inject, "<ir-macro-injecter>");
|
2014-03-29 08:47:57 -04:00
|
|
|
pic_proc_cv_init(pic, inject, 3);
|
2014-01-19 04:14:32 -05:00
|
|
|
pic_proc_cv_set(pic, inject, 0, use_env);
|
|
|
|
pic_proc_cv_set(pic, inject, 1, mac_env);
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_proc_cv_set(pic, inject, 2, box);
|
2014-01-19 04:14:32 -05:00
|
|
|
|
2014-03-26 08:20:06 -04:00
|
|
|
compare = pic_proc_new(pic, ir_macro_compare, "<ir-macro-comparator>");
|
2014-03-29 08:47:57 -04:00
|
|
|
pic_proc_cv_init(pic, compare, 3);
|
2014-01-19 04:14:32 -05:00
|
|
|
pic_proc_cv_set(pic, compare, 0, use_env);
|
|
|
|
pic_proc_cv_set(pic, compare, 1, mac_env);
|
2014-03-30 02:29:03 -04:00
|
|
|
pic_proc_cv_set(pic, compare, 2, box);
|
2014-01-19 04:14:32 -05:00
|
|
|
|
|
|
|
cb = pic_proc_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
|
|
|
|
|
2014-03-30 02:29:03 -04:00
|
|
|
expr = ir_macro_wrap(pic, expr, pic_senv_ptr(use_env), box, &ir);
|
2014-03-25 08:04:30 -04:00
|
|
|
expr = pic_apply3(pic, cb, expr, pic_obj_value(inject), pic_obj_value(compare));
|
2014-03-30 02:29:03 -04:00
|
|
|
expr = ir_macro_unwrap(pic, expr, pic_senv_ptr(mac_env), box, &ir);
|
2014-03-29 08:47:57 -04:00
|
|
|
|
2014-01-19 04:14:32 -05:00
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_macro_ir_macro_transformer(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *cb, *proc;
|
|
|
|
|
|
|
|
pic_get_args(pic, "l", &cb);
|
|
|
|
|
2014-03-26 08:20:06 -04:00
|
|
|
proc = pic_proc_new(pic, ir_macro_call, "<ir-macro-procedure>");
|
2014-01-19 04:14:32 -05:00
|
|
|
pic_proc_cv_init(pic, proc, 1);
|
|
|
|
pic_proc_cv_set(pic, proc, 0, pic_obj_value(cb));
|
|
|
|
|
|
|
|
return pic_obj_value(proc);
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:04:44 -05:00
|
|
|
void
|
|
|
|
pic_init_macro(pic_state *pic)
|
|
|
|
{
|
2014-03-26 08:20:06 -04:00
|
|
|
pic_defmacro(pic, "include", pic_proc_new(pic, pic_macro_include, "<include-procedure>"));
|
2014-02-08 01:05:28 -05:00
|
|
|
|
2014-02-01 01:41:30 -05:00
|
|
|
pic_deflibrary ("(picrin macro)") {
|
2014-02-13 00:59:17 -05:00
|
|
|
|
|
|
|
/* export define-macro syntax */
|
2014-04-04 15:01:42 -04:00
|
|
|
pic_define_syntactic_keyword(pic, pic->lib->senv, pic->sDEFINE_MACRO);
|
2014-02-13 00:59:17 -05:00
|
|
|
|
2014-02-13 00:59:51 -05:00
|
|
|
pic_defun(pic, "gensym", pic_macro_gensym);
|
2014-02-14 23:02:06 -05:00
|
|
|
pic_defun(pic, "macroexpand", pic_macro_macroexpand);
|
2013-12-10 04:49:48 -05:00
|
|
|
pic_defun(pic, "make-syntactic-closure", pic_macro_make_sc);
|
|
|
|
pic_defun(pic, "identifier?", pic_macro_identifier_p);
|
|
|
|
pic_defun(pic, "identifier=?", pic_macro_identifier_eq_p);
|
2014-01-09 02:34:22 -05:00
|
|
|
pic_defun(pic, "er-macro-transformer", pic_macro_er_macro_transformer);
|
2014-01-19 04:14:32 -05:00
|
|
|
pic_defun(pic, "ir-macro-transformer", pic_macro_ir_macro_transformer);
|
2013-12-10 04:49:48 -05:00
|
|
|
}
|
2013-11-27 01:04:44 -05:00
|
|
|
}
|