2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *
|
2015-05-30 09:39:03 -04:00
|
|
|
pic_add_rename(pic_state *pic, struct pic_env *env, pic_sym *sym)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-06-09 09:26:21 -04:00
|
|
|
pic_sym *rename = pic_gensym(pic, sym);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
pic_put_rename(pic, env, sym, rename);
|
2015-06-09 09:26:21 -04:00
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
return rename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-30 09:39:03 -04:00
|
|
|
pic_put_rename(pic_state *pic, struct pic_env *env, pic_sym *sym, pic_sym *rename)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-05-30 09:39:03 -04:00
|
|
|
pic_dict_set(pic, env->map, sym, pic_obj_value(rename));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 09:26:21 -04:00
|
|
|
pic_sym *
|
|
|
|
pic_find_rename(pic_state *pic, struct pic_env *env, pic_sym *sym)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-05-30 09:39:03 -04:00
|
|
|
if (! pic_dict_has(pic, env->map, sym)) {
|
2015-06-09 09:26:21 -04:00
|
|
|
return NULL;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-06-09 09:26:21 -04:00
|
|
|
return pic_sym_ptr(pic_dict_ref(pic, env->map, sym));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-01-20 02:02:28 -05:00
|
|
|
define_macro(pic_state *pic, pic_sym *rename, struct pic_proc *mac)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-01-18 07:25:34 -05:00
|
|
|
pic_dict_set(pic, pic->macros, rename, pic_obj_value(mac));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-01-18 01:48:05 -05:00
|
|
|
static struct pic_proc *
|
2015-01-20 02:02:28 -05:00
|
|
|
find_macro(pic_state *pic, pic_sym *rename)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-01-18 07:25:34 -05:00
|
|
|
if (! pic_dict_has(pic, pic->macros, rename)) {
|
2014-08-25 00:38:09 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-18 07:25:34 -05:00
|
|
|
return pic_proc_ptr(pic_dict_ref(pic, pic->macros, rename));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-01-20 02:02:28 -05:00
|
|
|
static pic_sym *
|
2015-05-30 09:39:03 -04:00
|
|
|
make_identifier(pic_state *pic, pic_sym *sym, struct pic_env *env)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *rename;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
while (true) {
|
2015-06-09 09:26:21 -04:00
|
|
|
if ((rename = pic_find_rename(pic, env, sym)) != NULL) {
|
2014-08-25 00:38:09 -04:00
|
|
|
return rename;
|
|
|
|
}
|
2015-05-30 09:39:03 -04:00
|
|
|
if (! env->up)
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2015-05-30 09:39:03 -04:00
|
|
|
env = env->up;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
if (! pic_interned_p(pic, sym)) {
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pic_gensym(pic, sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
static pic_value macroexpand(pic_state *, pic_value, struct pic_env *);
|
|
|
|
static pic_value macroexpand_lambda(pic_state *, pic_value, struct pic_env *);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
static pic_value
|
2015-05-30 09:39:03 -04:00
|
|
|
macroexpand_symbol(pic_state *pic, pic_sym *sym, struct pic_env *env)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-05-30 09:39:03 -04:00
|
|
|
return pic_obj_value(make_identifier(pic, sym, env));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
macroexpand_quote(pic_state *pic, pic_value expr)
|
|
|
|
{
|
2015-01-18 21:08:27 -05:00
|
|
|
return pic_cons(pic, pic_obj_value(pic->rQUOTE), pic_cdr(pic, expr));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2015-05-30 09:39:03 -04:00
|
|
|
macroexpand_list(pic_state *pic, pic_value obj, struct pic_env *env)
|
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)) {
|
2015-05-30 09:39:03 -04:00
|
|
|
head = macroexpand(pic, pic_car(pic, obj), env);
|
|
|
|
tail = macroexpand_list(pic, pic_cdr(pic, obj), env);
|
2014-08-25 00:38:09 -04:00
|
|
|
x = pic_cons(pic, head, tail);
|
|
|
|
} else {
|
2015-05-30 09:39:03 -04:00
|
|
|
x = macroexpand(pic, obj, env);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
pic_gc_protect(pic, x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2014-09-10 01:42:36 -04:00
|
|
|
static pic_value
|
2015-05-30 09:39:03 -04:00
|
|
|
macroexpand_defer(pic_state *pic, pic_value expr, struct pic_env *env)
|
2014-09-10 01:42:36 -04:00
|
|
|
{
|
2015-06-09 03:34:45 -04:00
|
|
|
pic_value skel = pic_list1(pic, pic_invalid_value()); /* (#<invalid>) */
|
2014-09-10 01:42:36 -04:00
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
pic_push(pic, pic_cons(pic, expr, skel), env->defer);
|
2014-09-10 01:42:36 -04:00
|
|
|
|
|
|
|
return skel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-05-30 09:39:03 -04:00
|
|
|
macroexpand_deferred(pic_state *pic, struct pic_env *env)
|
2014-09-10 01:42:36 -04:00
|
|
|
{
|
2015-01-22 05:28:31 -05:00
|
|
|
pic_value defer, val, src, dst, it;
|
2014-09-10 01:42:36 -04:00
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
pic_for_each (defer, pic_reverse(pic, env->defer), it) {
|
2014-09-10 01:42:36 -04:00
|
|
|
src = pic_car(pic, defer);
|
|
|
|
dst = pic_cdr(pic, defer);
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
val = macroexpand_lambda(pic, src, env);
|
2014-09-10 01:42:36 -04:00
|
|
|
|
|
|
|
/* copy */
|
|
|
|
pic_pair_ptr(dst)->car = pic_car(pic, val);
|
|
|
|
pic_pair_ptr(dst)->cdr = pic_cdr(pic, val);
|
|
|
|
}
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
env->defer = pic_nil_value();
|
2014-09-10 01:42:36 -04:00
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
static pic_value
|
2015-05-30 09:39:03 -04:00
|
|
|
macroexpand_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;
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value a;
|
|
|
|
|
|
|
|
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 v = pic_car(pic, a);
|
|
|
|
|
|
|
|
if (! pic_sym_p(v)) {
|
2014-09-16 10:43:15 -04:00
|
|
|
pic_errorf(pic, "syntax error");
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-01-20 01:31:17 -05:00
|
|
|
pic_add_rename(pic, in, pic_sym_ptr(v));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
if (pic_sym_p(a)) {
|
2015-01-20 01:31:17 -05:00
|
|
|
pic_add_rename(pic, in, pic_sym_ptr(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
|
|
|
}
|
|
|
|
|
|
|
|
formal = macroexpand_list(pic, pic_cadr(pic, expr), in);
|
|
|
|
body = macroexpand_list(pic, pic_cddr(pic, expr), in);
|
|
|
|
|
2014-09-10 01:42:36 -04:00
|
|
|
macroexpand_deferred(pic, in);
|
|
|
|
|
2015-01-18 21:08:27 -05:00
|
|
|
return pic_cons(pic, pic_obj_value(pic->rLAMBDA), pic_cons(pic, formal, body));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2015-05-30 09:39:03 -04:00
|
|
|
macroexpand_define(pic_state *pic, pic_value expr, struct pic_env *env)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *sym, *rename;
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value var, val;
|
|
|
|
|
2014-08-25 04:41:29 -04:00
|
|
|
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));
|
|
|
|
|
2015-01-18 21:08:27 -05:00
|
|
|
expr = pic_list3(pic, pic_obj_value(pic->rDEFINE), var, pic_cons(pic, pic_obj_value(pic->rLAMBDA), pic_cons(pic, val, pic_cddr(pic, expr))));
|
2014-08-25 04:41:29 -04:00
|
|
|
}
|
|
|
|
|
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_sym_p(var)) {
|
2014-09-16 10:43:15 -04:00
|
|
|
pic_errorf(pic, "binding to non-symbol object");
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-01-20 01:31:17 -05:00
|
|
|
sym = pic_sym_ptr(var);
|
2015-06-09 09:26:21 -04:00
|
|
|
if ((rename = pic_find_rename(pic, env, sym)) == NULL) {
|
2015-05-30 09:39:03 -04:00
|
|
|
rename = pic_add_rename(pic, env, sym);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-05-30 09:39:03 -04:00
|
|
|
val = macroexpand(pic, pic_list_ref(pic, expr, 2), env);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-01-18 21:08:27 -05:00
|
|
|
return pic_list3(pic, pic_obj_value(pic->rDEFINE), pic_obj_value(rename), val);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2015-05-30 09:39:03 -04:00
|
|
|
macroexpand_defsyntax(pic_state *pic, pic_value expr, struct pic_env *env)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
pic_value var, val;
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *sym, *rename;
|
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_sym_p(var)) {
|
2014-09-16 10:43:15 -04:00
|
|
|
pic_errorf(pic, "binding to non-symbol object");
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-01-20 01:31:17 -05:00
|
|
|
sym = pic_sym_ptr(var);
|
2015-06-09 09:26:21 -04:00
|
|
|
if ((rename = pic_find_rename(pic, env, sym)) == NULL) {
|
2015-05-30 09:39:03 -04:00
|
|
|
rename = pic_add_rename(pic, env, sym);
|
2014-08-25 00:38:09 -04:00
|
|
|
} else {
|
2015-01-18 21:08:27 -05:00
|
|
|
pic_warnf(pic, "redefining syntax variable: ~s", pic_obj_value(sym));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
val = pic_cadr(pic, pic_cdr(pic, expr));
|
|
|
|
|
|
|
|
pic_try {
|
|
|
|
val = pic_eval(pic, val, pic->lib);
|
|
|
|
} pic_catch {
|
|
|
|
pic_errorf(pic, "macroexpand error while definition: %s", pic_errmsg(pic));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! pic_proc_p(val)) {
|
|
|
|
pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", var);
|
|
|
|
}
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
val = pic_apply1(pic, pic_proc_ptr(val), pic_obj_value(env));
|
2015-01-18 01:48:05 -05:00
|
|
|
|
|
|
|
if (! pic_proc_p(val)) {
|
|
|
|
pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", var);
|
|
|
|
}
|
|
|
|
|
|
|
|
define_macro(pic, rename, 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-05-30 09:39:03 -04:00
|
|
|
macroexpand_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, args;
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("before expand-1:");
|
|
|
|
pic_debug(pic, expr);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
args = pic_list2(pic, expr, pic_obj_value(env));
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
pic_try {
|
2015-01-18 01:48:05 -05:00
|
|
|
v = pic_apply(pic, mac, args);
|
2014-08-25 00:38:09 -04:00
|
|
|
} pic_catch {
|
|
|
|
pic_errorf(pic, "macroexpand error while application: %s", pic_errmsg(pic));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("after expand-1:");
|
|
|
|
pic_debug(pic, v);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2015-05-30 09:39:03 -04:00
|
|
|
macroexpand_node(pic_state *pic, pic_value expr, struct pic_env *env)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
switch (pic_type(expr)) {
|
|
|
|
case PIC_TT_SYMBOL: {
|
2015-05-30 09:39:03 -04:00
|
|
|
return macroexpand_symbol(pic, pic_sym_ptr(expr), env);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
case PIC_TT_PAIR: {
|
|
|
|
pic_value car;
|
2015-01-18 01:48:05 -05:00
|
|
|
struct pic_proc *mac;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
if (! pic_list_p(expr)) {
|
|
|
|
pic_errorf(pic, "cannot macroexpand improper list: ~s", expr);
|
|
|
|
}
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
car = macroexpand(pic, pic_car(pic, expr), env);
|
2014-08-25 00:38:09 -04:00
|
|
|
if (pic_sym_p(car)) {
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *tag = pic_sym_ptr(car);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
if (tag == pic->rDEFINE_SYNTAX) {
|
2015-05-30 09:39:03 -04:00
|
|
|
return macroexpand_defsyntax(pic, expr, env);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
else if (tag == pic->rLAMBDA) {
|
2015-05-30 09:39:03 -04:00
|
|
|
return macroexpand_defer(pic, expr, env);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
else if (tag == pic->rDEFINE) {
|
2015-05-30 09:39:03 -04:00
|
|
|
return macroexpand_define(pic, expr, env);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
else if (tag == pic->rQUOTE) {
|
|
|
|
return macroexpand_quote(pic, expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((mac = find_macro(pic, tag)) != NULL) {
|
2015-05-30 09:39:03 -04:00
|
|
|
return macroexpand_node(pic, macroexpand_macro(pic, mac, expr, env), env);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
return pic_cons(pic, car, macroexpand_list(pic, pic_cdr(pic, expr), env));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2015-05-30 09:39:03 -04:00
|
|
|
macroexpand(pic_state *pic, pic_value expr, struct pic_env *env)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
size_t ai = pic_gc_arena_preserve(pic);
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
printf("[macroexpand] expanding... ");
|
|
|
|
pic_debug(pic, expr);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
v = macroexpand_node(pic, expr, env);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
pic_gc_protect(pic, v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_macroexpand(pic_state *pic, pic_value expr, struct pic_lib *lib)
|
|
|
|
{
|
|
|
|
struct pic_lib *prev;
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("before expand:");
|
|
|
|
pic_debug(pic, expr);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* change library for macro-expansion time processing */
|
|
|
|
prev = pic->lib;
|
|
|
|
pic->lib = lib;
|
|
|
|
|
2014-09-10 02:20:36 -04:00
|
|
|
lib->env->defer = pic_nil_value(); /* the last expansion could fail and leave defer field old */
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
v = macroexpand(pic, expr, lib->env);
|
|
|
|
|
2014-09-10 01:42:36 -04:00
|
|
|
macroexpand_deferred(pic, lib->env);
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
pic->lib = prev;
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("after expand:");
|
|
|
|
pic_debug(pic, v);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
struct pic_env *
|
|
|
|
pic_make_env(pic_state *pic, struct pic_env *up)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-05-30 09:39:03 -04:00
|
|
|
struct pic_env *env;
|
2015-01-18 07:58:40 -05:00
|
|
|
struct pic_dict *map;
|
|
|
|
|
|
|
|
map = pic_make_dict(pic);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV);
|
|
|
|
env->up = up;
|
|
|
|
env->defer = pic_nil_value();
|
|
|
|
env->map = map;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
return env;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-01-18 01:48:05 -05:00
|
|
|
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
|
|
|
{
|
2015-01-18 01:48:05 -05:00
|
|
|
struct pic_proc *proc, *trans;
|
|
|
|
|
|
|
|
trans = pic_make_proc(pic, func, pic_symbol_name(pic, name));
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_put_rename(pic, pic->lib->env, name, id);
|
|
|
|
|
2015-01-18 01:48:05 -05: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 */
|
2015-01-18 01:48:05 -05:00
|
|
|
define_macro(pic, id, proc);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
/* auto export! */
|
|
|
|
pic_export(pic, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
pic_identifier_p(pic_state *pic, pic_value obj)
|
|
|
|
{
|
2015-01-20 01:31:17 -05:00
|
|
|
return pic_sym_p(obj) && ! pic_interned_p(pic, pic_sym_ptr(obj));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 05:43:12 -04:00
|
|
|
bool
|
2015-05-30 09:39:03 -04:00
|
|
|
pic_identifier_eq_p(pic_state *pic, struct pic_env *env1, pic_sym *sym1, struct pic_env *env2, pic_sym *sym2)
|
2014-09-04 05:43:12 -04:00
|
|
|
{
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *a, *b;
|
2014-09-04 05:43:12 -04:00
|
|
|
|
|
|
|
a = make_identifier(pic, sym1, env1);
|
|
|
|
if (a != make_identifier(pic, sym1, env1)) {
|
|
|
|
a = sym1;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = make_identifier(pic, sym2, env2);
|
|
|
|
if (b != make_identifier(pic, sym2, env2)) {
|
|
|
|
b = sym2;
|
|
|
|
}
|
|
|
|
|
2015-01-18 21:08:27 -05:00
|
|
|
return pic_eq_p(pic_obj_value(a), pic_obj_value(b));
|
2014-09-04 05:43:12 -04:00
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
static pic_value
|
|
|
|
pic_macro_identifier_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value obj;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &obj);
|
|
|
|
|
|
|
|
return pic_bool_value(pic_identifier_p(pic, obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_macro_make_identifier(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value obj;
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *sym;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
pic_get_args(pic, "mo", &sym, &obj);
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
pic_assert_type(pic, obj, env);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
return pic_obj_value(make_identifier(pic, sym, pic_env_ptr(obj)));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 05:43:12 -04:00
|
|
|
static pic_value
|
|
|
|
pic_macro_identifier_eq_p(pic_state *pic)
|
|
|
|
{
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *sym1, *sym2;
|
2014-09-04 05:43:12 -04:00
|
|
|
pic_value env1, env2;
|
|
|
|
|
|
|
|
pic_get_args(pic, "omom", &env1, &sym1, &env2, &sym2);
|
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
pic_assert_type(pic, env1, env);
|
|
|
|
pic_assert_type(pic, env2, env);
|
2014-09-04 05:43:12 -04:00
|
|
|
|
2015-05-30 09:39:03 -04:00
|
|
|
return pic_bool_value(pic_identifier_eq_p(pic, pic_env_ptr(env1), sym1, pic_env_ptr(env2), sym2));
|
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, "identifier?", pic_macro_identifier_p);
|
2014-09-04 05:43:12 -04:00
|
|
|
pic_defun(pic, "identifier=?", pic_macro_identifier_eq_p);
|
2014-08-31 22:37:52 -04:00
|
|
|
pic_defun(pic, "make-identifier", pic_macro_make_identifier);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|