picrin/extlib/benz/macro.c

355 lines
8.0 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-18 10:56:56 -05:00
#include "picrin/object.h"
2014-08-25 00:38:09 -04:00
2016-02-06 09:15:53 -05:00
KHASH_DEFINE(env, pic_id *, pic_sym *, kh_ptr_hash_func, kh_ptr_hash_equal)
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;
assert(up != NULL);
env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV);
env->up = up;
env->lib = NULL;
kh_init(env, &env->map);
return env;
}
2015-06-09 09:26:21 -04:00
pic_sym *
2016-02-06 09:15:53 -05:00
pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env)
{
const char *name;
pic_sym *uid;
2016-02-19 13:26:52 -05:00
pic_value str;
2016-02-18 11:05:28 -05:00
name = pic_str(pic, pic_id_name(pic, id));
if (env->up == NULL && pic_sym_p(pic, pic_obj_value(id))) { /* toplevel & public */
2016-02-19 13:26:52 -05:00
str = pic_strf_value(pic, "%s/%s", pic_str(pic, pic_obj_value(env->lib)), name);
} else {
str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++);
}
2016-02-07 14:10:22 -05:00
uid = pic_intern(pic, str);
2016-02-06 09:15:53 -05:00
return pic_put_identifier(pic, id, uid, env);
}
pic_sym *
2016-02-06 09:15:53 -05:00
pic_put_identifier(pic_state *pic, pic_id *id, pic_sym *uid, struct pic_env *env)
{
2015-06-24 18:26:48 -04:00
khiter_t it;
int ret;
2016-02-06 09:15:53 -05:00
it = kh_put(env, &env->map, id, &ret);
2015-06-24 18:26:48 -04:00
kh_val(&env->map, it) = uid;
return uid;
}
pic_sym *
search_scope(pic_state *pic, pic_id *id, struct pic_env *env)
{
2015-06-24 18:26:48 -04:00
khiter_t it;
2016-02-06 09:15:53 -05:00
it = kh_get(env, &env->map, id);
2015-06-24 18:26:48 -04:00
if (it == kh_end(&env->map)) {
return NULL;
2014-08-25 00:38:09 -04:00
}
2015-06-24 18:26:48 -04:00
return kh_val(&env->map, it);
}
2016-02-05 07:01:36 -05:00
static pic_sym *
search(pic_state *pic, pic_id *id, struct pic_env *env)
2016-02-05 07:01:36 -05:00
{
2016-02-06 09:15:53 -05:00
pic_sym *uid = NULL;
2016-02-05 07:01:36 -05:00
while (env != NULL) {
uid = search_scope(pic, id, env);
2016-02-06 09:15:53 -05:00
if (uid != NULL) {
break;
2016-02-05 07:01:36 -05:00
}
env = env->up;
}
2016-02-06 09:15:53 -05:00
return uid;
2016-02-05 07:01:36 -05:00
}
pic_sym *
pic_find_identifier(pic_state *pic, pic_id *id, struct pic_env *env)
2016-02-05 07:01:36 -05:00
{
pic_sym *uid;
while ((uid = search(pic, id, env)) == NULL) {
if (pic_sym_p(pic, pic_obj_value(id))) {
2016-02-05 07:01:36 -05:00
break;
}
2016-02-06 09:15:53 -05:00
env = id->u.id.env; /* do not overwrite id first */
id = id->u.id.id;
2016-02-05 07:01:36 -05:00
}
if (uid == NULL) {
while (env->up != NULL) {
env = env->up;
}
2016-02-06 09:15:53 -05:00
uid = pic_add_identifier(pic, id, env);
2016-02-05 07:01:36 -05:00
}
return uid;
}
2016-02-06 10:07:29 -05:00
/**
* macro expander
*/
static void
2016-02-19 10:03:16 -05:00
define_macro(pic_state *pic, pic_sym *uid, pic_value mac)
2016-02-06 10:07:29 -05:00
{
2016-02-10 07:50:39 -05:00
if (pic_weak_has(pic, pic->macros, uid)) {
2016-02-06 10:07:29 -05:00
pic_warnf(pic, "redefining syntax variable: ~s", pic_obj_value(uid));
}
2016-02-19 10:03:16 -05:00
pic_weak_set(pic, pic->macros, uid, mac);
2016-02-06 10:07:29 -05:00
}
2016-02-19 10:03:16 -05:00
static pic_value
2016-02-06 10:07:29 -05:00
find_macro(pic_state *pic, pic_sym *uid)
{
2016-02-10 07:50:39 -05:00
if (! pic_weak_has(pic, pic->macros, uid)) {
2016-02-19 10:03:16 -05:00
return pic_false_value(pic);
2016-02-06 10:07:29 -05:00
}
2016-02-19 10:03:16 -05:00
return pic_weak_ref(pic, pic->macros, uid);
2016-02-06 10:07:29 -05:00
}
static void
shadow_macro(pic_state *pic, pic_sym *uid)
{
2016-02-10 07:50:39 -05:00
if (pic_weak_has(pic, pic->macros, uid)) {
pic_weak_del(pic, pic->macros, uid);
2016-02-06 10:07:29 -05:00
}
}
static pic_value expand(pic_state *, pic_value, struct pic_env *, pic_value);
static pic_value expand_lambda(pic_state *, pic_value, struct pic_env *);
static pic_value
expand_var(pic_state *pic, pic_id *id, struct pic_env *env, pic_value deferred)
{
2016-02-19 10:03:16 -05:00
pic_value mac;
2016-02-06 10:07:29 -05:00
pic_sym *functor;
functor = pic_find_identifier(pic, id, env);
2016-02-06 10:07:29 -05:00
2016-02-19 10:03:16 -05:00
if (! pic_false_p(pic, mac = find_macro(pic, functor))) {
2016-02-13 23:46:55 -05:00
return expand(pic, pic_call(pic, mac, 2, pic_obj_value(id), pic_obj_value(env)), env, deferred);
2016-02-06 10:07:29 -05:00
}
return pic_obj_value(functor);
}
static pic_value
expand_quote(pic_state *pic, pic_value expr)
{
return pic_cons(pic, pic_obj_value(pic->sQUOTE), pic_cdr(pic, expr));
}
static pic_value
expand_list(pic_state *pic, pic_value obj, struct pic_env *env, pic_value deferred)
{
2016-02-19 02:17:13 -05:00
size_t ai = pic_enter(pic);
2016-02-06 10:07:29 -05:00
pic_value x, head, tail;
if (pic_pair_p(pic, obj)) {
2016-02-06 10:07:29 -05:00
head = expand(pic, pic_car(pic, obj), env, deferred);
tail = expand_list(pic, pic_cdr(pic, obj), env, deferred);
x = pic_cons(pic, head, tail);
} else {
x = expand(pic, obj, env, deferred);
}
2016-02-19 02:17:13 -05:00
pic_leave(pic, ai);
pic_protect(pic, x);
2016-02-06 10:07:29 -05:00
return x;
}
static pic_value
expand_defer(pic_state *pic, pic_value expr, pic_value deferred)
{
pic_value skel = pic_cons(pic, pic_invalid_value(), pic_invalid_value());
2016-02-18 12:29:40 -05:00
pic_set_car(pic, deferred, pic_cons(pic, pic_cons(pic, expr, skel), pic_car(pic, deferred)));
2016-02-06 10:07:29 -05:00
return skel;
}
static void
expand_deferred(pic_state *pic, pic_value deferred, struct pic_env *env)
{
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);
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));
}
}
static pic_value
expand_lambda(pic_state *pic, pic_value expr, struct pic_env *env)
{
pic_value formal, body;
struct pic_env *in;
pic_value a, deferred;
in = pic_make_env(pic, env);
for (a = pic_cadr(pic, expr); pic_pair_p(pic, a); a = pic_cdr(pic, a)) {
2016-02-06 10:07:29 -05:00
pic_add_identifier(pic, pic_id_ptr(pic_car(pic, a)), in);
}
if (pic_id_p(pic, a)) {
2016-02-06 10:07:29 -05:00
pic_add_identifier(pic, pic_id_ptr(a), in);
}
2016-02-18 12:29:40 -05:00
deferred = pic_list(pic, 1, pic_nil_value(pic));
2016-02-06 10:07:29 -05:00
formal = expand_list(pic, pic_list_ref(pic, expr, 1), in, deferred);
body = expand(pic, pic_list_ref(pic, expr, 2), in, deferred);
expand_deferred(pic, deferred, in);
2016-02-18 12:29:40 -05:00
return pic_list(pic, 3, pic_obj_value(pic->sLAMBDA), formal, body);
2016-02-06 10:07:29 -05:00
}
static pic_value
expand_define(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred)
{
pic_sym *uid;
pic_id *id;
pic_value val;
id = pic_id_ptr(pic_cadr(pic, expr));
if ((uid = search_scope(pic, id, env)) == NULL) {
2016-02-06 10:07:29 -05:00
uid = pic_add_identifier(pic, id, env);
} else {
shadow_macro(pic, uid);
}
val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred);
2016-02-18 12:29:40 -05:00
return pic_list(pic, 3, pic_obj_value(pic->sDEFINE), pic_obj_value(uid), val);
2016-02-06 10:07:29 -05:00
}
static pic_value
expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env)
{
2016-02-19 10:03:16 -05:00
pic_value pic_compile(pic_state *, pic_value);
2016-02-06 10:07:29 -05:00
pic_id *id;
pic_value val;
pic_sym *uid;
id = pic_id_ptr(pic_cadr(pic, expr));
if ((uid = search_scope(pic, id, env)) == NULL) {
2016-02-06 10:07:29 -05:00
uid = pic_add_identifier(pic, id, env);
}
2016-02-13 23:46:55 -05:00
val = pic_call(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env)), 0);
if (! pic_proc_p(pic, val)) {
2016-02-18 11:05:28 -05:00
pic_errorf(pic, "macro definition \"%s\" evaluates to non-procedure object", pic_str(pic, pic_id_name(pic, id)));
2016-02-06 10:07:29 -05:00
}
2016-02-19 10:03:16 -05:00
define_macro(pic, uid, val);
2016-02-06 10:07:29 -05:00
return pic_undef_value(pic);
2016-02-06 10:07:29 -05:00
}
static pic_value
expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred)
{
switch (pic_type(pic, expr)) {
case PIC_TYPE_ID:
case PIC_TYPE_SYMBOL: {
2016-02-06 10:07:29 -05:00
return expand_var(pic, pic_id_ptr(expr), env, deferred);
}
case PIC_TYPE_PAIR: {
2016-02-19 10:03:16 -05:00
pic_value mac;
2016-02-06 10:07:29 -05:00
if (! pic_list_p(pic, expr)) {
2016-02-06 10:07:29 -05:00
pic_errorf(pic, "cannot expand improper list: ~s", expr);
}
if (pic_id_p(pic, pic_car(pic, expr))) {
2016-02-06 10:07:29 -05:00
pic_sym *functor;
functor = pic_find_identifier(pic, pic_id_ptr(pic_car(pic, expr)), env);
2016-02-06 10:07:29 -05:00
if (functor == pic->sDEFINE_MACRO) {
return expand_defmacro(pic, expr, env);
}
else if (functor == pic->sLAMBDA) {
return expand_defer(pic, expr, deferred);
}
else if (functor == pic->sDEFINE) {
return expand_define(pic, expr, env, deferred);
}
else if (functor == pic->sQUOTE) {
return expand_quote(pic, expr);
}
2016-02-19 10:03:16 -05:00
if (! pic_false_p(pic, mac = find_macro(pic, functor))) {
2016-02-13 23:46:55 -05:00
return expand(pic, pic_call(pic, mac, 2, expr, pic_obj_value(env)), env, deferred);
2016-02-06 10:07:29 -05:00
}
}
return expand_list(pic, expr, env, deferred);
}
default:
return expr;
}
}
static pic_value
expand(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred)
{
2016-02-19 02:17:13 -05:00
size_t ai = pic_enter(pic);
2016-02-06 10:07:29 -05:00
pic_value v;
v = expand_node(pic, expr, env, deferred);
2016-02-19 02:17:13 -05:00
pic_leave(pic, ai);
pic_protect(pic, v);
2016-02-06 10:07:29 -05:00
return v;
}
pic_value
pic_expand(pic_state *pic, pic_value expr, struct pic_env *env)
{
pic_value v, deferred;
#if DEBUG
puts("before expand:");
pic_debug(pic, expr);
puts("");
#endif
2016-02-18 12:29:40 -05:00
deferred = pic_list(pic, 1, pic_nil_value(pic));
2016-02-06 10:07:29 -05:00
v = expand(pic, expr, env, deferred);
expand_deferred(pic, deferred, env);
#if DEBUG
puts("after expand:");
pic_debug(pic, v);
puts("");
#endif
return v;
}