picrin/extlib/benz/macro.c

555 lines
12 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
static bool
pic_var_p(pic_value obj)
{
return pic_sym_p(obj) || pic_id_p(obj);
}
struct pic_id *
pic_make_id(pic_state *pic, pic_value var, struct pic_env *env)
2014-08-25 00:38:09 -04:00
{
struct pic_id *id;
2014-08-25 00:38:09 -04:00
assert(pic_var_p(var));
2015-06-09 09:26:21 -04:00
id = (struct pic_id *)pic_obj_alloc(pic, sizeof(struct pic_id), PIC_TT_ID);
id->var = var;
id->env = env;
return id;
2014-08-25 00:38:09 -04:00
}
struct pic_env *
pic_make_env(pic_state *pic, struct pic_env *up)
2014-08-25 00:38:09 -04:00
{
struct pic_env *env;
env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV);
env->up = up;
xh_init_ptr(&env->map, sizeof(pic_sym *));
return env;
2014-08-25 00:38:09 -04:00
}
2015-06-09 09:26:21 -04:00
pic_sym *
pic_var_name(pic_state PIC_UNUSED(*pic), pic_value var)
2014-08-25 00:38:09 -04:00
{
assert(pic_var_p(var));
while (pic_id_p(var)) {
var = pic_id_ptr(var)->var;
2014-08-25 00:38:09 -04:00
}
return pic_sym_ptr(var);
2014-08-25 00:38:09 -04:00
}
pic_sym *
pic_uniq(pic_state *pic, pic_value var)
2014-08-25 00:38:09 -04:00
{
pic_str *str;
assert(pic_var_p(var));
str = pic_format(pic, "%s.%d", pic_symbol_name(pic, pic_var_name(pic, var)), pic->ucnt++);
return pic_intern(pic, str);
2014-08-25 00:38:09 -04:00
}
static pic_sym *
lookup(pic_state PIC_UNUSED(*pic), pic_value var, struct pic_env *env)
2014-08-25 00:38:09 -04:00
{
xh_entry *e;
assert(pic_var_p(var));
while (env != NULL) {
if ((e = xh_get_ptr(&env->map, pic_ptr(var))) != NULL) {
return xh_val(e, pic_sym *);
}
env = env->up;
2014-08-25 00:38:09 -04:00
}
return NULL;
2014-08-25 00:38:09 -04:00
}
2015-01-20 02:02:28 -05:00
static pic_sym *
resolve(pic_state *pic, pic_value var, struct pic_env *env)
2014-08-25 00:38:09 -04:00
{
pic_sym *uid;
assert(pic_var_p(var));
assert(env != NULL);
2014-08-25 00:38:09 -04:00
while ((uid = lookup(pic, var, env)) == NULL) {
if (pic_sym_p(var)) {
break;
2014-08-25 00:38:09 -04:00
}
env = pic_id_ptr(var)->env;
var = pic_id_ptr(var)->var;
2014-08-25 00:38:09 -04:00
}
if (uid == NULL) {
while (env->up != NULL) {
env = env->up;
}
uid = pic_add_variable(pic, env, var);
}
return uid;
}
pic_sym *
pic_add_variable(pic_state *pic, struct pic_env *env, pic_value var)
{
pic_sym *uid;
assert(pic_var_p(var));
uid = pic_uniq(pic, var);
pic_put_variable(pic, env, var, uid);
return uid;
}
void
pic_put_variable(pic_state PIC_UNUSED(*pic), struct pic_env *env, pic_value var, pic_sym *uid)
{
assert(pic_var_p(var));
xh_put_ptr(&env->map, pic_ptr(var), &uid);
}
pic_sym *
pic_find_variable(pic_state PIC_UNUSED(*pic), struct pic_env *env, pic_value var)
{
xh_entry *e;
assert(pic_var_p(var));
if ((e = xh_get_ptr(&env->map, pic_ptr(var))) == NULL) {
return NULL;
2014-08-25 00:38:09 -04:00
}
return xh_val(e, pic_sym *);
}
static void
define_macro(pic_state *pic, pic_sym *uid, struct pic_proc *mac)
{
pic_dict_set(pic, pic->macros, uid, pic_obj_value(mac));
}
static struct pic_proc *
find_macro(pic_state *pic, pic_sym *uid)
{
if (! pic_dict_has(pic, pic->macros, uid)) {
return NULL;
2014-08-25 00:38:09 -04:00
}
return pic_proc_ptr(pic_dict_ref(pic, pic->macros, uid));
2014-08-25 00:38:09 -04:00
}
static pic_value expand(pic_state *, pic_value, struct pic_env *, pic_value);
2015-06-16 07:19:04 -04:00
static pic_value expand_lambda(pic_state *, pic_value, struct pic_env *);
2014-08-25 00:38:09 -04:00
static pic_value
2015-06-16 07:19:04 -04:00
expand_var(pic_state *pic, pic_value var, struct pic_env *env)
2014-08-25 00:38:09 -04:00
{
return pic_obj_value(resolve(pic, var, env));
2014-08-25 00:38:09 -04:00
}
static pic_value
2015-06-16 07:19:04 -04:00
expand_quote(pic_state *pic, pic_value expr)
2014-08-25 00:38:09 -04:00
{
2015-06-09 12:06:56 -04:00
return pic_cons(pic, pic_obj_value(pic->uQUOTE), pic_cdr(pic, expr));
2014-08-25 00:38:09 -04:00
}
static pic_value
expand_list(pic_state *pic, pic_value obj, struct pic_env *env, pic_value deferred)
2014-08-25 00:38:09 -04:00
{
size_t ai = pic_gc_arena_preserve(pic);
pic_value x, head, tail;
if (pic_pair_p(obj)) {
head = expand(pic, pic_car(pic, obj), env, deferred);
tail = expand_list(pic, pic_cdr(pic, obj), env, deferred);
2014-08-25 00:38:09 -04:00
x = pic_cons(pic, head, tail);
} else {
x = expand(pic, obj, env, deferred);
2014-08-25 00:38:09 -04:00
}
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, x);
return x;
}
static pic_value
expand_defer(pic_state *pic, pic_value expr, pic_value deferred)
{
2015-06-09 03:34:45 -04:00
pic_value skel = pic_list1(pic, pic_invalid_value()); /* (#<invalid>) */
pic_set_car(pic, deferred, pic_acons(pic, expr, skel, pic_car(pic, deferred)));
return skel;
}
static void
expand_deferred(pic_state *pic, pic_value deferred, struct pic_env *env)
{
2015-01-22 05:28:31 -05:00
pic_value defer, val, src, dst, it;
deferred = pic_car(pic, deferred);
pic_for_each (defer, pic_reverse(pic, deferred), it) {
src = pic_car(pic, defer);
dst = pic_cdr(pic, defer);
2015-06-16 07:19:04 -04:00
val = expand_lambda(pic, src, env);
/* copy */
pic_set_car(pic, dst, pic_car(pic, val));
pic_set_cdr(pic, dst, pic_cdr(pic, val));
}
}
2014-08-25 00:38:09 -04:00
static pic_value
2015-06-16 07:19:04 -04:00
expand_lambda(pic_state *pic, pic_value expr, struct pic_env *env)
2014-08-25 00:38:09 -04:00
{
pic_value formal, body;
2015-05-30 09:39:03 -04:00
struct pic_env *in;
pic_value a, deferred;
2014-08-25 00:38:09 -04:00
if (pic_length(pic, expr) < 2) {
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "syntax error");
2014-08-25 00:38:09 -04:00
}
2015-05-30 09:39:03 -04:00
in = pic_make_env(pic, env);
2014-08-25 00:38:09 -04:00
for (a = pic_cadr(pic, expr); pic_pair_p(a); a = pic_cdr(pic, a)) {
pic_value var = pic_car(pic, a);
2014-08-25 00:38:09 -04:00
if (! pic_var_p(var)) {
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "syntax error");
2014-08-25 00:38:09 -04:00
}
pic_add_variable(pic, in, var);
2014-08-25 00:38:09 -04:00
}
if (pic_var_p(a)) {
pic_add_variable(pic, in, a);
2014-08-25 00:38:09 -04:00
}
else if (! pic_nil_p(a)) {
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "syntax error");
2014-08-25 00:38:09 -04:00
}
deferred = pic_list1(pic, pic_nil_value());
2014-08-25 00:38:09 -04:00
formal = expand_list(pic, pic_cadr(pic, expr), in, deferred);
body = expand_list(pic, pic_cddr(pic, expr), in, deferred);
expand_deferred(pic, deferred, in);
2015-06-09 12:06:56 -04:00
return pic_cons(pic, pic_obj_value(pic->uLAMBDA), pic_cons(pic, formal, body));
2014-08-25 00:38:09 -04:00
}
static pic_value
expand_define(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred)
2014-08-25 00:38:09 -04:00
{
pic_sym *uid;
2014-08-25 00:38:09 -04:00
pic_value var, val;
while (pic_length(pic, expr) >= 2 && pic_pair_p(pic_cadr(pic, expr))) {
var = pic_car(pic, pic_cadr(pic, expr));
val = pic_cdr(pic, pic_cadr(pic, expr));
expr = pic_list3(pic, pic_obj_value(pic->uDEFINE), var, pic_cons(pic, pic_obj_value(pic->sLAMBDA), pic_cons(pic, val, pic_cddr(pic, expr))));
}
2014-08-25 00:38:09 -04:00
if (pic_length(pic, expr) != 3) {
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "syntax error");
2014-08-25 00:38:09 -04:00
}
var = pic_cadr(pic, expr);
if (! pic_var_p(var)) {
pic_errorf(pic, "binding to non-variable object");
2014-08-25 00:38:09 -04:00
}
if ((uid = pic_find_variable(pic, env, var)) == NULL) {
uid = pic_add_variable(pic, env, var);
2014-08-25 00:38:09 -04:00
}
val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred);
2014-08-25 00:38:09 -04:00
return pic_list3(pic, pic_obj_value(pic->uDEFINE), pic_obj_value(uid), val);
2014-08-25 00:38:09 -04:00
}
static pic_value
2015-06-16 07:19:04 -04:00
expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env)
2014-08-25 00:38:09 -04:00
{
pic_value var, val;
pic_sym *uid;
2014-08-25 00:38:09 -04:00
if (pic_length(pic, expr) != 3) {
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "syntax error");
2014-08-25 00:38:09 -04:00
}
var = pic_cadr(pic, expr);
if (! pic_var_p(var)) {
pic_errorf(pic, "binding to non-variable object");
2014-08-25 00:38:09 -04:00
}
if ((uid = pic_find_variable(pic, env, var)) == NULL) {
uid = pic_add_variable(pic, env, var);
2014-08-25 00:38:09 -04:00
} else {
pic_warnf(pic, "redefining syntax variable: ~s", var);
2014-08-25 00:38:09 -04:00
}
val = pic_cadr(pic, pic_cdr(pic, expr));
pic_try {
val = pic_eval(pic, val, env);
2014-08-25 00:38:09 -04:00
} pic_catch {
2015-06-16 07:19:04 -04:00
pic_errorf(pic, "expand error while definition: %s", pic_errmsg(pic));
2014-08-25 00:38:09 -04:00
}
if (! pic_proc_p(val)) {
pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", var);
}
define_macro(pic, uid, pic_proc_ptr(val));
2014-08-25 00:38:09 -04:00
2015-06-09 03:34:45 -04:00
return pic_undef_value();
2014-08-25 00:38:09 -04:00
}
static pic_value
2015-06-16 07:19:04 -04:00
expand_macro(pic_state *pic, struct pic_proc *mac, pic_value expr, struct pic_env *env)
2014-08-25 00:38:09 -04:00
{
pic_value v;
2014-08-25 00:38:09 -04:00
#if DEBUG
puts("before expand-1:");
pic_debug(pic, expr);
puts("");
#endif
pic_try {
v = pic_apply2(pic, mac, expr, pic_obj_value(env));
2014-08-25 00:38:09 -04:00
} pic_catch {
2015-06-16 07:19:04 -04:00
pic_errorf(pic, "expand error while application: %s", pic_errmsg(pic));
2014-08-25 00:38:09 -04:00
}
#if DEBUG
puts("after expand-1:");
pic_debug(pic, v);
puts("");
#endif
return v;
}
static pic_value
expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred)
2014-08-25 00:38:09 -04:00
{
switch (pic_type(expr)) {
case PIC_TT_ID:
2014-08-25 00:38:09 -04:00
case PIC_TT_SYMBOL: {
2015-06-16 07:19:04 -04:00
return expand_var(pic, expr, env);
2014-08-25 00:38:09 -04:00
}
case PIC_TT_PAIR: {
struct pic_proc *mac;
2014-08-25 00:38:09 -04:00
if (! pic_list_p(expr)) {
2015-06-16 07:19:04 -04:00
pic_errorf(pic, "cannot expand improper list: ~s", expr);
2014-08-25 00:38:09 -04:00
}
if (pic_var_p(pic_car(pic, expr))) {
pic_sym *functor;
functor = resolve(pic, pic_car(pic, expr), env);
2014-08-25 00:38:09 -04:00
if (functor == pic->uDEFINE_MACRO) {
2015-06-16 07:19:04 -04:00
return expand_defmacro(pic, expr, env);
2014-08-25 00:38:09 -04:00
}
else if (functor == pic->uLAMBDA) {
return expand_defer(pic, expr, deferred);
2014-08-25 00:38:09 -04:00
}
else if (functor == pic->uDEFINE) {
return expand_define(pic, expr, env, deferred);
2014-08-25 00:38:09 -04:00
}
else if (functor == pic->uQUOTE) {
2015-06-16 07:19:04 -04:00
return expand_quote(pic, expr);
2014-08-25 00:38:09 -04:00
}
if ((mac = find_macro(pic, functor)) != NULL) {
return expand_node(pic, expand_macro(pic, mac, expr, env), env, deferred);
2014-08-25 00:38:09 -04:00
}
}
return expand_list(pic, expr, env, deferred);
2014-08-25 00:38:09 -04:00
}
default:
return expr;
}
}
static pic_value
expand(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred)
2014-08-25 00:38:09 -04:00
{
size_t ai = pic_gc_arena_preserve(pic);
pic_value v;
#if DEBUG
2015-06-16 07:19:04 -04:00
printf("[expand] expanding... ");
2014-08-25 00:38:09 -04:00
pic_debug(pic, expr);
puts("");
#endif
v = expand_node(pic, expr, env, deferred);
2014-08-25 00:38:09 -04:00
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
return v;
}
pic_value
2015-06-16 07:19:04 -04:00
pic_expand(pic_state *pic, pic_value expr, struct pic_env *env)
2014-08-25 00:38:09 -04:00
{
pic_value v, deferred;
2014-08-25 00:38:09 -04:00
#if DEBUG
puts("before expand:");
pic_debug(pic, expr);
puts("");
#endif
deferred = pic_list1(pic, pic_nil_value());
2014-08-25 00:38:09 -04:00
v = expand(pic, expr, env, deferred);
expand_deferred(pic, deferred, env);
2014-08-25 00:38:09 -04:00
#if DEBUG
puts("after expand:");
pic_debug(pic, v);
puts("");
#endif
return v;
}
static pic_value
defmacro_call(pic_state *pic)
{
struct pic_proc *self = pic_get_proc(pic);
pic_value args, tmp, proc;
pic_get_args(pic, "oo", &args, &tmp);
proc = pic_attr_ref(pic, pic_obj_value(self), "@@transformer");
return pic_apply_trampoline(pic, pic_proc_ptr(proc), pic_cdr(pic, args));
}
2014-08-25 00:38:09 -04:00
void
2015-01-20 02:02:28 -05:00
pic_defmacro(pic_state *pic, pic_sym *name, pic_sym *id, pic_func_t func)
2014-08-25 00:38:09 -04:00
{
struct pic_proc *proc, *trans;
trans = pic_make_proc(pic, func, pic_symbol_name(pic, name));
pic_put_variable(pic, pic->lib->env, pic_obj_value(name), id);
2014-08-25 00:38:09 -04:00
proc = pic_make_proc(pic, defmacro_call, "defmacro_call");
pic_attr_set(pic, pic_obj_value(proc), "@@transformer", pic_obj_value(trans));
2014-08-25 00:38:09 -04:00
/* symbol registration */
define_macro(pic, id, proc);
2014-08-25 00:38:09 -04:00
/* auto export! */
pic_export(pic, name);
}
static pic_value
pic_macro_identifier_p(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
pic_value obj;
pic_get_args(pic, "o", &obj);
return pic_bool_value(pic_id_p(obj));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_macro_make_identifier(pic_state *pic)
2014-09-04 05:43:12 -04:00
{
pic_value var, env;
2014-09-04 05:43:12 -04:00
pic_get_args(pic, "oo", &var, &env);
2014-09-04 05:43:12 -04:00
pic_assert_type(pic, var, var);
pic_assert_type(pic, env, env);
2014-09-04 05:43:12 -04:00
return pic_obj_value(pic_make_id(pic, var, pic_env_ptr(env)));
2014-09-04 05:43:12 -04:00
}
static pic_value
pic_macro_identifier_variable(pic_state *pic)
{
pic_value id;
pic_get_args(pic, "o", &id);
pic_assert_type(pic, id, id);
return pic_id_ptr(id)->var;
}
static pic_value
pic_macro_identifier_environment(pic_state *pic)
{
pic_value id;
pic_get_args(pic, "o", &id);
pic_assert_type(pic, id, id);
return pic_obj_value(pic_id_ptr(id)->env);
}
2014-08-25 00:38:09 -04:00
static pic_value
pic_macro_variable_p(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
pic_value obj;
pic_get_args(pic, "o", &obj);
return pic_bool_value(pic_var_p(obj));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_macro_variable_eq_p(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
pic_value var1, var2;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "oo", &var1, &var2);
2014-08-25 00:38:09 -04:00
pic_assert_type(pic, var1, var);
pic_assert_type(pic, var2, var);
2014-09-04 05:43:12 -04:00
if (pic_sym_p(var1) && pic_sym_p(var2)) {
return pic_bool_value(pic_eq_p(var1, var2));
}
if (pic_id_p(var1) && pic_id_p(var2)) {
struct pic_id *id1, *id2;
2014-09-04 05:43:12 -04:00
id1 = pic_id_ptr(var1);
id2 = pic_id_ptr(var2);
return pic_bool_value(resolve(pic, id1->var, id1->env) == resolve(pic, id2->var, id2->env));
}
return pic_false_value();
2014-09-04 05:43:12 -04:00
}
2014-08-25 00:38:09 -04:00
void
pic_init_macro(pic_state *pic)
{
2014-08-31 22:37:52 -04:00
pic_defun(pic, "make-identifier", pic_macro_make_identifier);
pic_defun(pic, "identifier?", pic_macro_identifier_p);
pic_defun(pic, "identifier-variable", pic_macro_identifier_variable);
pic_defun(pic, "identifier-environment", pic_macro_identifier_environment);
pic_defun(pic, "variable?", pic_macro_variable_p);
pic_defun(pic, "variable=?", pic_macro_variable_eq_p);
2014-08-25 00:38:09 -04:00
}