picrin/src/macro.c

908 lines
21 KiB
C
Raw Normal View History

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"
#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"
2013-10-30 03:37:43 -04:00
static pic_value macroexpand(pic_state *, pic_value, struct pic_senv *);
static pic_value macroexpand_list(pic_state *, pic_value, struct pic_senv *);
2014-02-11 20:48:44 -05:00
static struct pic_senv *
2014-02-12 23:44:30 -05:00
senv_new(pic_state *pic, struct pic_senv *up)
{
struct pic_senv *senv;
senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
2014-02-11 20:48:44 -05:00
senv->up = up;
2014-02-11 20:39:20 -05:00
senv->name = xh_new_int();
2013-12-06 04:04:36 -05:00
return senv;
}
static struct pic_senv *
2014-02-12 23:44:30 -05:00
senv_new_local(pic_state *pic, pic_value formals, struct pic_senv *up)
{
struct pic_senv *senv;
pic_value a;
pic_sym sym;
2014-02-12 23:44:30 -05:00
senv = senv_new(pic, up);
for (a = formals; pic_pair_p(a); a = pic_cdr(pic, a)) {
pic_value v = pic_car(pic, a);
2014-01-30 13:03:36 -05:00
if (! pic_sym_p(v)) {
v = macroexpand(pic, v, up);
}
2014-01-30 13:03:36 -05:00
if (! pic_sym_p(v)) {
pic_error(pic, "syntax error");
}
sym = pic_sym(v);
2014-02-11 20:39:20 -05:00
xh_put_int(senv->name, sym, pic_gensym(pic, sym));
}
2014-01-30 13:03:36 -05:00
if (! pic_sym_p(a)) {
a = macroexpand(pic, a, up);
}
2014-01-30 13:03:36 -05:00
if (pic_sym_p(a)) {
sym = pic_sym(a);
2014-02-11 20:39:20 -05:00
xh_put_int(senv->name, sym, pic_gensym(pic, sym));
}
else if (! pic_nil_p(a)) {
pic_error(pic, "syntax error");
}
return senv;
}
2014-02-11 21:13:29 -05:00
struct pic_macro *
macro_new(pic_state *pic, struct pic_proc *proc, struct pic_senv *mac_env)
2013-11-27 00:00:23 -05:00
{
2014-02-11 21:13:29 -05:00
struct pic_macro *mac;
2013-11-27 00:00:23 -05:00
2014-02-11 21:13:29 -05:00
mac = (struct pic_macro *)pic_obj_alloc(pic, sizeof(struct pic_macro), PIC_TT_MACRO);
mac->senv = mac_env;
mac->proc = proc;
return mac;
2013-11-27 00:00:23 -05:00
}
2013-11-27 01:04:44 -05: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;
}
2013-11-27 03:26:07 -05:00
static bool
2014-02-11 20:50:26 -05:00
identifier_p(pic_value obj)
2013-11-27 03:26:07 -05:00
{
2014-01-30 13:03:36 -05:00
if (pic_sym_p(obj)) {
2013-11-27 03:26:07 -05:00
return true;
}
if (pic_sc_p(obj)) {
2014-02-11 20:50:26 -05:00
return identifier_p(pic_sc(obj)->expr);
2013-11-27 03:26:07 -05:00
}
return false;
}
2014-02-11 20:57:58 -05:00
static bool
identifier_eq_p(pic_state *pic, struct pic_senv *e1, pic_value x, struct pic_senv *e2, pic_value y)
{
if (! (identifier_p(x) && identifier_p(y))) {
return false;
}
x = macroexpand(pic, x, e1);
y = macroexpand(pic, y, e2);
return pic_eq_p(x, y);
}
2014-01-08 01:38:31 -05:00
void
pic_import(pic_state *pic, pic_value spec)
{
struct pic_lib *lib;
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-02-01 06:01:26 -05:00
for (xh_begin(lib->exports, &it); ! xh_isend(&it); xh_next(&it)) {
2014-02-11 20:39:20 -05:00
2014-01-08 01:38:31 -05:00
#if DEBUG
if (it.e->val >= 0) {
2014-02-11 20:39:20 -05:00
printf("* importing %s as %s\n",
pic_symbol_name(pic, (long)it.e->key),
pic_symbol_name(pic, it.e->val));
2014-01-08 01:38:31 -05:00
}
else {
printf("* importing %s\n", pic_symbol_name(pic, (long)it.e->key));
2014-01-08 01:38:31 -05:00
}
#endif
2014-02-11 20:39:20 -05:00
xh_put_int(pic->lib->senv->name, (long)it.e->key, it.e->val);
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)
{
xh_entry *e;
2013-12-07 23:44:39 -05:00
2014-02-11 20:39:20 -05:00
e = xh_get_int(pic->lib->senv->name, sym);
if (! e) {
pic_error(pic, "symbol not defined");
}
2014-02-06 11:08:57 -05:00
xh_put_int(pic->lib->exports, (long)e->key, e->val);
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-02-11 21:13:29 -05:00
struct pic_macro *mac;
2014-02-12 23:42:17 -05:00
pic_sym sym, uniq;
2013-11-27 00:00:23 -05:00
2014-02-12 23:42:17 -05:00
/* new macro */
mac = macro_new(pic, macro, NULL);
2013-12-06 10:51:32 -05:00
2014-02-12 23:42:17 -05:00
/* symbol registration */
sym = pic_intern_cstr(pic, name);
2014-02-11 21:02:59 -05:00
uniq = pic_gensym(pic, sym);
2014-02-11 20:39:20 -05:00
xh_put_int(pic->lib->senv->name, sym, uniq);
2014-02-11 21:13:29 -05:00
xh_put_int(pic->macros, uniq, (long)mac);
/* auto export! */
pic_export(pic, pic_intern_cstr(pic, name));
2013-11-27 01:58:28 -05:00
}
2014-02-11 20:39:20 -05:00
static pic_sym
symbol_rename(pic_state *pic, pic_sym sym, struct pic_senv *senv)
{
xh_entry *e;
pic_sym uniq;
if (! pic_interned_p(pic, sym)) {
return sym;
}
while (true) {
if ((e = xh_get_int(senv->name, sym)) != NULL) {
return (pic_sym)e->val;
}
if (! senv->up)
break;
senv = senv->up;
}
uniq = pic_gensym(pic, sym);
xh_put_int(senv->name, sym, uniq);
return uniq;
}
static pic_value
macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv)
2013-10-30 03:37:43 -04:00
{
int ai = pic_gc_arena_preserve(pic);
#if DEBUG
printf("[macroexpand] expanding... ");
pic_debug(pic, expr);
puts("");
#endif
switch (pic_type(expr)) {
case PIC_TT_SC: {
struct pic_sc *sc;
sc = pic_sc(expr);
return macroexpand(pic, sc->expr, sc->senv);
}
2013-10-30 03:37:43 -04:00
case PIC_TT_SYMBOL: {
2014-02-11 20:39:20 -05:00
return pic_symbol_value(symbol_rename(pic, pic_sym(expr), senv));
2013-10-30 03:37:43 -04:00
}
case PIC_TT_PAIR: {
pic_value car, v;
2014-02-11 20:39:20 -05:00
xh_entry *e;
2013-10-30 03:37:43 -04:00
car = macroexpand(pic, pic_car(pic, expr), senv);
2014-02-11 20:39:20 -05:00
if (pic_sym_p(car)) {
pic_sym tag = pic_sym(car);
if (tag == pic->sDEFINE_LIBRARY) {
struct pic_lib *prev = pic->lib;
2013-12-07 06:02:39 -05:00
2013-12-07 12:42:34 -05:00
if (pic_length(pic, expr) < 2) {
pic_error(pic, "syntax error");
}
pic_make_library(pic, pic_cadr(pic, expr));
/* proceed expressions in new library */
pic_in_library(pic, pic_cadr(pic, expr));
{
int ai = pic_gc_arena_preserve(pic);
struct pic_proc *proc;
2014-02-01 02:05:29 -05:00
pic_for_each (v, pic_cddr(pic, expr)) {
2014-01-20 02:57:39 -05:00
proc = pic_compile(pic, v);
if (proc == NULL) {
abort();
}
pic_apply_argv(pic, proc, 0);
if (pic_undef_p(v)) {
abort();
}
pic_gc_arena_restore(pic, ai);
}
}
pic_in_library(pic, prev->name);
2013-12-07 06:02:39 -05:00
2014-01-08 01:22:23 -05:00
return pic_none_value();
2013-12-07 06:02:39 -05:00
}
2014-02-11 20:39:20 -05:00
else if (tag == pic->sIMPORT) {
2014-02-01 02:05:29 -05:00
pic_value spec;
pic_for_each (spec, pic_cdr(pic, expr)) {
2014-01-08 01:38:31 -05:00
pic_import(pic, spec);
2013-12-07 12:42:34 -05:00
}
2014-01-08 01:22:23 -05:00
return pic_none_value();
2013-12-07 12:42:34 -05:00
}
2014-02-11 21:30:32 -05:00
2014-02-11 20:39:20 -05:00
else if (tag == pic->sEXPORT) {
2014-02-01 02:05:29 -05:00
pic_value spec;
pic_for_each (spec, pic_cdr(pic, expr)) {
2014-01-30 13:03:36 -05:00
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));
2013-12-07 12:42:34 -05:00
}
2014-01-08 01:22:23 -05:00
return pic_none_value();
2013-12-07 06:02:39 -05:00
}
2014-02-11 20:39:20 -05:00
else if (tag == pic->sDEFINE_SYNTAX) {
2013-11-27 01:58:28 -05:00
pic_value var, val;
struct pic_proc *proc;
2014-02-12 07:49:28 -05:00
pic_sym uniq;
struct pic_macro *mac;
2013-11-27 01:58:28 -05:00
if (pic_length(pic, expr) != 3) {
pic_error(pic, "syntax error");
}
2014-02-11 20:39:20 -05:00
var = pic_cadr(pic, expr);
2014-01-30 13:03:36 -05:00
if (! pic_sym_p(var)) {
2014-02-12 07:49:28 -05:00
var = macroexpand(pic, var, senv);
2013-11-27 01:58:28 -05:00
}
2014-02-12 07:49:28 -05:00
if (! pic_sym_p(var)) {
pic_error(pic, "binding to non-symbol object");
}
uniq = pic_gensym(pic, pic_sym(var));
xh_put_int(senv->name, pic_sym(var), uniq);
2013-11-27 01:58:28 -05:00
val = pic_cadr(pic, pic_cdr(pic, expr));
2014-01-20 02:57:39 -05:00
proc = pic_compile(pic, val);
if (pic->err) {
printf("macroexpand error: %s\n", pic_errmsg(pic));
2013-11-27 01:58:28 -05:00
abort();
}
v = pic_apply(pic, proc, pic_nil_value());
if (pic->err) {
printf("macroexpand error: %s\n", pic_errmsg(pic));
2013-11-27 01:58:28 -05:00
abort();
}
assert(pic_proc_p(v));
2014-02-12 07:49:28 -05:00
mac = macro_new(pic, pic_proc_ptr(v), senv);
xh_put_int(pic->macros, uniq, (long)mac);
2013-11-27 01:58:28 -05:00
pic_gc_arena_restore(pic, ai);
2014-01-08 01:22:23 -05:00
return pic_none_value();
2013-11-27 01:58:28 -05:00
}
2014-02-11 20:39:20 -05:00
else if (tag == pic->sDEFINE_MACRO) {
pic_value var, val;
struct pic_proc *proc;
2014-02-12 23:26:32 -05:00
pic_sym uniq;
struct pic_macro *mac;
if (pic_length(pic, expr) < 2) {
pic_error(pic, "syntax error");
}
var = pic_car(pic, pic_cdr(pic, expr));
if (pic_pair_p(var)) {
2013-11-26 20:43:49 -05:00
/* FIXME: unhygienic */
val = pic_cons(pic, pic_symbol_value(pic->sLAMBDA),
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)));
}
2014-01-30 13:03:36 -05:00
if (! pic_sym_p(var)) {
pic_error(pic, "syntax error");
}
2014-02-12 23:26:32 -05:00
uniq = pic_gensym(pic, pic_sym(var));
xh_put_int(senv->name, pic_sym(var), uniq);
2014-01-20 02:57:39 -05:00
proc = pic_compile(pic, val);
if (pic->err) {
printf("macroexpand error: %s\n", pic_errmsg(pic));
abort();
}
v = pic_apply(pic, proc, pic_nil_value());
if (pic->err) {
printf("macroexpand error: %s\n", pic_errmsg(pic));
abort();
}
2013-10-30 03:37:43 -04:00
assert(pic_proc_p(v));
2014-02-12 23:26:32 -05:00
mac = macro_new(pic, pic_proc_ptr(v), NULL);
xh_put_int(pic->macros, uniq, (long)mac);
2013-11-15 05:54:47 -05:00
pic_gc_arena_restore(pic, ai);
2014-01-08 01:22:23 -05:00
return pic_none_value();
2013-10-30 03:37:43 -04:00
}
2013-11-17 03:33:28 -05:00
2014-02-11 20:39:20 -05:00
else if (tag == pic->sLAMBDA) {
2014-02-12 23:44:30 -05:00
struct pic_senv *in = senv_new_local(pic, pic_cadr(pic, expr), senv);
2013-11-26 07:06:46 -05:00
2014-02-11 20:39:20 -05:00
v = pic_cons(pic, car,
2013-11-26 07:06:46 -05:00
pic_cons(pic,
macroexpand_list(pic, pic_cadr(pic, expr), in),
macroexpand_list(pic, pic_cddr(pic, expr), in)));
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
return v;
}
2014-02-11 20:39:20 -05:00
else if (tag == pic->sDEFINE) {
2014-02-06 12:23:55 -05:00
pic_sym var;
pic_value formals;
2013-11-26 07:06:46 -05:00
if (pic_length(pic, expr) < 2) {
pic_error(pic, "syntax error");
}
2014-02-06 12:23:55 -05:00
formals = pic_cadr(pic, expr);
if (pic_pair_p(formals)) {
2014-02-12 23:44:30 -05:00
struct pic_senv *in = senv_new_local(pic, pic_cdr(pic, formals), senv);
2013-11-26 07:06:46 -05:00
pic_value a;
2013-11-26 19:51:48 -05:00
/* defined symbol */
2014-02-06 12:23:55 -05:00
a = pic_car(pic, formals);
2014-01-30 13:03:36 -05:00
if (! pic_sym_p(a)) {
2013-12-09 10:27:11 -05:00
a = macroexpand(pic, a, senv);
}
2014-01-30 13:03:36 -05:00
if (! pic_sym_p(a)) {
pic_error(pic, "binding to non-symbol object");
2013-11-26 07:06:46 -05:00
}
2014-02-06 12:23:55 -05:00
var = pic_sym(a);
2014-02-11 20:39:20 -05:00
xh_put_int(senv->name, var, pic_gensym(pic, var));
2013-11-26 07:06:46 -05:00
/* binding value */
2014-02-11 20:39:20 -05:00
v = pic_cons(pic, car,
pic_cons(pic,
2013-11-26 07:06:46 -05:00
macroexpand_list(pic, pic_cadr(pic, expr), in),
macroexpand_list(pic, pic_cddr(pic, expr), in)));
2013-11-26 07:06:46 -05:00
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
return v;
}
2014-02-06 12:23:55 -05:00
if (! pic_sym_p(formals)) {
formals = macroexpand(pic, formals, senv);
2013-12-09 10:27:11 -05:00
}
2014-02-06 12:23:55 -05:00
if (! pic_sym_p(formals)) {
2013-11-27 01:57:23 -05:00
pic_error(pic, "binding to non-symbol object");
}
2014-02-06 12:23:55 -05:00
var = pic_sym(formals);
/* do not make duplicate variable slot */
2014-02-11 20:39:20 -05:00
if (xh_get_int(senv->name, var) == NULL) {
xh_put_int(senv->name, var, pic_gensym(pic, var));
}
2014-02-11 20:39:20 -05:00
v = pic_cons(pic, pic_symbol_value(tag),
macroexpand_list(pic, pic_cdr(pic, expr), senv));
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
return v;
2013-11-26 07:06:46 -05:00
}
2014-02-11 20:39:20 -05:00
else if (tag == pic->sSETBANG || tag == pic->sIF || tag == pic->sBEGIN) {
v = pic_cons(pic, car, macroexpand_list(pic, pic_cdr(pic, expr), senv));
2013-11-26 07:06:46 -05:00
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
return v;
2014-02-11 20:39:20 -05:00
}
else if (tag == pic->sQUOTE) {
v = pic_cons(pic, car, pic_cdr(pic, expr));
2013-11-26 07:06:46 -05:00
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
return v;
}
2014-02-11 20:39:20 -05:00
/* macro */
if ((e = xh_get_int(pic->macros, tag)) != NULL) {
pic_value v;
2014-02-11 21:13:29 -05:00
struct pic_macro *mac;
2014-02-12 08:30:46 -05:00
#if DEBUG
puts("before expand-1:");
pic_debug(pic, expr);
puts("");
#endif
2014-02-11 21:13:29 -05:00
mac = (struct pic_macro *)e->val;
if (mac->senv == NULL) { /* legacy macro */
v = pic_apply(pic, mac->proc, pic_cdr(pic, expr));
2014-02-11 20:39:20 -05:00
if (pic->err) {
printf("macroexpand error: %s\n", pic_errmsg(pic));
abort();
}
}
else {
2014-02-11 21:13:29 -05:00
v = pic_apply_argv(pic, mac->proc, 3, expr, pic_obj_value(senv), pic_obj_value(mac->senv));
2014-02-11 20:39:20 -05:00
if (pic->err) {
printf("macroexpand error: %s\n", pic_errmsg(pic));
abort();
}
}
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
#if DEBUG
puts("after expand-1:");
pic_debug(pic, v);
puts("");
#endif
return macroexpand(pic, v, senv);
}
2013-11-26 07:06:46 -05:00
}
v = pic_cons(pic, car, macroexpand_list(pic, pic_cdr(pic, expr), senv));
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
return v;
}
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:
case PIC_TT_VAR:
2014-01-18 02:51:54 -05:00
case PIC_TT_IREP:
2013-11-26 07:06:46 -05:00
pic_error(pic, "unexpected value type");
return pic_undef_value(); /* unreachable */
}
/* suppress warnings, never be called */
abort();
}
static pic_value
macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv)
{
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);
2013-11-26 07:06:46 -05:00
vs = pic_cons(pic, macroexpand(pic, v, senv), 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);
/* 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);
}
2013-11-26 07:06:46 -05:00
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, list);
return list;
2013-11-26 07:06:46 -05:00
}
pic_value
pic_macroexpand(pic_state *pic, pic_value expr)
2013-11-26 07:06:46 -05:00
{
2013-11-26 11:38:26 -05:00
pic_value v;
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-02-11 20:42:17 -05:00
v = macroexpand(pic, expr, pic->lib->senv);
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-11 20:48:44 -05:00
struct pic_senv *
pic_null_syntactic_env(pic_state *pic)
{
2014-02-12 23:44:30 -05:00
return senv_new(pic, NULL);
2014-02-11 20:48:44 -05:00
}
#define register_core_syntax(pic,senv,id) do { \
pic_sym sym = pic_intern_cstr(pic, id); \
xh_put_int(senv->name, sym, sym); \
} while (0)
struct pic_senv *
pic_minimal_syntactic_env(pic_state *pic)
{
struct pic_senv *senv = pic_null_syntactic_env(pic);
register_core_syntax(pic, senv, "define-library");
register_core_syntax(pic, senv, "import");
register_core_syntax(pic, senv, "export");
return senv;
}
struct pic_senv *
pic_core_syntactic_env(pic_state *pic)
{
struct pic_senv *senv = pic_minimal_syntactic_env(pic);
register_core_syntax(pic, senv, "define");
register_core_syntax(pic, senv, "set!");
register_core_syntax(pic, senv, "quote");
register_core_syntax(pic, senv, "lambda");
register_core_syntax(pic, senv, "if");
register_core_syntax(pic, senv, "begin");
register_core_syntax(pic, senv, "define-syntax");
return senv;
}
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;
int res;
pic_get_args(pic, "*", &argc, &argv);
/* FIXME unhygienic */
body = pic_list(pic, 1, pic_symbol_value(pic->sBEGIN));
for (i = 0; i < argc; ++i) {
char *filename;
if (! pic_str_p(argv[i])) {
pic_error(pic, "expected string");
}
filename = pic_str_ptr(argv[i])->str;
file = fopen(filename, "r");
if (file == NULL) {
pic_error(pic, "could not open file");
}
res = pic_parse_file(pic, file, &exprs);
if (res < 0) {
pic_error(pic, "parse error");
}
body = pic_append(pic, body, exprs);
}
return body;
}
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 */
sc = sc_new(pic, expr, pic_senv(senv));
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-02-11 20:50:26 -05:00
return pic_bool_value(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");
}
e1 = pic_senv(e);
if (! pic_senv_p(f)) {
pic_error(pic, "unexpected type of argument 3");
}
e2 = pic_senv(f);
2014-02-11 20:57:58 -05:00
return pic_bool_value(identifier_eq_p(pic, e1, x, e2, y));
2013-11-27 03:26:07 -05:00
}
static pic_value
er_macro_rename(pic_state *pic)
{
pic_sym sym;
struct pic_senv *mac_env;
pic_get_args(pic, "m", &sym);
mac_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 1));
return pic_symbol_value(symbol_rename(pic, sym, mac_env));
}
static pic_value
er_macro_compare(pic_state *pic)
{
pic_value a, b;
struct pic_senv *use_env;
pic_sym m, n;
pic_get_args(pic, "oo", &a, &b);
2014-01-30 13:03:36 -05:00
if (! pic_sym_p(a) || ! pic_sym_p(b))
return pic_false_value(); /* should be an error? */
use_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
m = symbol_rename(pic, pic_sym(a), use_env);
n = symbol_rename(pic, pic_sym(b), use_env);
return pic_bool_value(m == n);
}
static pic_value
er_macro_call(pic_state *pic)
{
pic_value expr, use_env, mac_env;
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");
}
rename = pic_proc_new(pic, er_macro_rename);
pic_proc_cv_init(pic, rename, 2);
pic_proc_cv_set(pic, rename, 0, use_env);
pic_proc_cv_set(pic, rename, 1, mac_env);
compare = pic_proc_new(pic, er_macro_compare);
pic_proc_cv_init(pic, compare, 2);
pic_proc_cv_set(pic, compare, 0, use_env);
pic_proc_cv_set(pic, compare, 1, mac_env);
cb = pic_proc_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
return pic_apply_argv(pic, cb, 3, expr, pic_obj_value(rename), pic_obj_value(compare));
}
static pic_value
pic_macro_er_macro_transformer(pic_state *pic)
{
struct pic_proc *cb, *proc;
pic_get_args(pic, "l", &cb);
proc = pic_proc_new(pic, er_macro_call);
pic_proc_cv_init(pic, proc, 1);
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;
pic_get_args(pic, "m", &sym);
use_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
return pic_symbol_value(symbol_rename(pic, sym, use_env));
2014-01-19 04:14:32 -05:00
}
static pic_value
ir_macro_compare(pic_state *pic)
{
pic_value a, b;
struct pic_senv *use_env;
pic_sym m, n;
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? */
use_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
m = symbol_rename(pic, pic_sym(a), use_env);
n = symbol_rename(pic, pic_sym(b), use_env);
2014-01-19 04:14:32 -05:00
return pic_bool_value(m == n);
2014-01-19 04:14:32 -05:00
}
static pic_value
ir_macro_wrap(pic_state *pic, pic_value expr, struct pic_senv *use_env, pic_value *assoc)
{
if (pic_sym_p(expr)) {
pic_value ren;
ren = macroexpand(pic, expr, use_env);
*assoc = pic_acons(pic, ren, expr, *assoc);
return ren;
}
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));
}
else {
return expr;
}
}
static pic_value
ir_macro_unwrap(pic_state *pic, pic_value expr, struct pic_senv *mac_env, pic_value *assoc)
{
2014-02-11 21:13:29 -05:00
if (pic_sym_p(expr) || pic_macro_p(expr)) {
2014-01-19 04:14:32 -05:00
pic_value r;
if (pic_test(r = pic_assq(pic, expr, *assoc))) {
return pic_cdr(pic, r);
}
r = macroexpand(pic, expr, mac_env);
2014-02-11 21:13:29 -05:00
if (pic_macro_p(r)) {
2014-01-19 04:14:32 -05:00
return expr;
}
else {
return r;
}
}
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));
}
else {
return expr;
}
}
static pic_value
ir_macro_call(pic_state *pic)
{
pic_value expr, use_env, mac_env;
struct pic_proc *inject, *compare, *cb;
pic_value assoc = pic_nil_value();
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");
}
inject = pic_proc_new(pic, ir_macro_inject);
pic_proc_cv_init(pic, inject, 2);
pic_proc_cv_set(pic, inject, 0, use_env);
pic_proc_cv_set(pic, inject, 1, mac_env);
compare = pic_proc_new(pic, ir_macro_compare);
pic_proc_cv_init(pic, compare, 2);
pic_proc_cv_set(pic, compare, 0, use_env);
pic_proc_cv_set(pic, compare, 1, mac_env);
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 = pic_apply_argv(pic, cb, 3, expr, pic_obj_value(inject), pic_obj_value(compare));
expr = ir_macro_unwrap(pic, expr, pic_senv_ptr(mac_env), &assoc);
return expr;
}
static pic_value
pic_macro_ir_macro_transformer(pic_state *pic)
{
struct pic_proc *cb, *proc;
pic_get_args(pic, "l", &cb);
proc = pic_proc_new(pic, ir_macro_call);
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-02-08 01:05:28 -05:00
pic_defmacro(pic, "include", pic_proc_new(pic, pic_macro_include));
2014-02-01 01:41:30 -05:00
pic_deflibrary ("(picrin macro)") {
/* export define-macro syntax */
xh_put_int(pic->lib->senv->name, pic->sDEFINE_MACRO, pic->sDEFINE_MACRO);
pic_export(pic, pic->sDEFINE_MACRO);
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);
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-11-27 01:04:44 -05:00
}