picrin/src/macro.c

213 lines
4.2 KiB
C
Raw Normal View History

2013-10-30 03:37:43 -04:00
#include <stdio.h>
#include <assert.h>
2013-11-26 02:38:39 -05:00
#include <string.h>
#include <math.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-10-30 03:37:43 -04:00
#include "xhash/xhash.h"
#define FALLTHROUGH ((void)0)
struct syntactic_env {
struct syntactic_env *up;
struct xhash *tbl;
};
static void
define_macro(pic_state *pic, const char *name, struct pic_proc *macro)
{
int idx;
idx = pic->mlen++;
if (idx >= pic->mcapa) {
pic_abort(pic, "macro table overflow");
}
2013-10-30 03:37:43 -04:00
pic->macros[idx] = macro;
xh_put(pic->global_tbl, name, ~idx);
}
static struct pic_proc *
lookup_macro(pic_state *pic, struct syntactic_env *env, const char *name)
{
struct xh_entry *e;
e = xh_get(env->tbl, name);
if (! e)
return NULL;
if (e->val >= 0)
return NULL;
return pic->macros[~e->val];
}
pic_value
expand(pic_state *pic, pic_value obj, struct syntactic_env *env)
{
int ai = pic_gc_arena_preserve(pic);
2013-11-01 06:02:46 -04:00
#if DEBUG
2013-11-21 09:31:32 -05:00
printf("current ai = %d\n", ai);
2013-11-01 06:02:46 -04:00
printf("expanding...");
pic_debug(pic, obj);
puts("");
#endif
2013-10-30 03:37:43 -04:00
switch (pic_type(obj)) {
case PIC_TT_SYMBOL: {
return obj;
}
case PIC_TT_PAIR: {
pic_value v;
if (! pic_list_p(pic, obj))
return obj;
if (pic_symbol_p(pic_car(pic, obj))) {
struct pic_proc *macro;
pic_sym sym;
sym = pic_sym(pic_car(pic, obj));
if (sym == pic->sDEFINE_MACRO) {
pic_value var, val;
struct pic_proc *proc;
if (pic_length(pic, obj) < 2) {
pic_error(pic, "syntax error");
}
var = pic_car(pic, pic_cdr(pic, obj));
if (pic_pair_p(var)) {
val = pic_cons(pic, pic_symbol_value(pic->sLAMBDA),
pic_cons(pic, pic_cdr(pic, var),
pic_cdr(pic, pic_cdr(pic, obj))));
var = pic_car(pic, var);
}
else {
if (pic_length(pic, obj) != 3) {
pic_error(pic, "syntax_error");
}
val = pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj)));
}
if (! pic_symbol_p(var)) {
pic_error(pic, "syntax error");
}
proc = pic_codegen(pic, val);
if (pic->errmsg) {
printf("macroexpand error: %s\n", pic->errmsg);
abort();
}
v = pic_apply(pic, proc, pic_nil_value());
if (pic->errmsg) {
printf("macroexpand error: %s\n", pic->errmsg);
abort();
}
2013-10-30 03:37:43 -04:00
assert(pic_proc_p(v));
define_macro(pic, pic_symbol_name(pic, pic_sym(var)), pic_proc_ptr(v));
2013-11-15 05:54:47 -05:00
pic_gc_arena_restore(pic, ai);
2013-10-30 03:37:43 -04:00
return pic_false_value();
}
macro = lookup_macro(pic, env, pic_symbol_name(pic, sym));
if (macro) {
v = pic_apply(pic, macro, pic_cdr(pic, obj));
if (pic->errmsg) {
printf("macroexpand error: %s\n", pic->errmsg);
abort();
}
2013-11-15 05:54:47 -05:00
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
2013-11-17 03:33:28 -05:00
v = expand(pic, v, env);
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
return v;
2013-10-30 03:37:43 -04:00
}
}
v = pic_nil_value();
2013-11-01 06:04:07 -04:00
while (! pic_nil_p(obj)) {
2013-10-30 03:37:43 -04:00
v = pic_cons(pic, expand(pic, pic_car(pic, obj), env), v);
2013-11-01 06:04:07 -04:00
obj = pic_cdr(pic, obj);
2013-11-15 05:54:47 -05:00
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
2013-10-30 03:37:43 -04:00
}
v = pic_reverse(pic, v);
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, v);
return v;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
case PIC_TT_INT:
2013-11-04 21:37:18 -05:00
case PIC_TT_CHAR:
2013-10-30 03:37:43 -04:00
case PIC_TT_EOF:
case PIC_TT_STRING:
2013-11-04 22:38:23 -05:00
case PIC_TT_VECTOR:
case PIC_TT_BLOB: {
2013-10-30 03:37:43 -04:00
return obj;
}
case PIC_TT_PROC:
case PIC_TT_PORT:
2013-11-17 03:25:26 -05:00
case PIC_TT_ERROR:
2013-10-30 03:37:43 -04:00
case PIC_TT_ENV:
2013-11-09 00:14:25 -05:00
case PIC_TT_CONT:
2013-10-30 03:37:43 -04:00
case PIC_TT_UNDEF:
2013-11-26 07:05:02 -05:00
case PIC_TT_SENV:
case PIC_TT_SYNTAX:
pic_error(pic, "unexpected value type");
return pic_undef_value(); /* unreachable */
2013-10-30 03:37:43 -04:00
}
2013-11-26 02:22:49 -05:00
/* logic flaw (suppress warnings gcc will emit) */
2013-11-09 01:24:58 -05:00
abort();
2013-10-30 03:37:43 -04:00
}
pic_value
2013-11-18 02:36:44 -05:00
pic_macroexpand(pic_state *pic, pic_value obj)
2013-10-30 03:37:43 -04:00
{
struct syntactic_env env;
pic_value v;
env.tbl = pic->global_tbl;
2013-11-01 06:02:46 -04:00
#if DEBUG
puts("before expand:");
pic_debug(pic, obj);
puts("");
#endif
2013-10-30 03:37:43 -04:00
v = expand(pic, obj, &env);
#if DEBUG
2013-11-01 06:02:46 -04:00
puts("after expand:");
2013-10-30 03:37:43 -04:00
pic_debug(pic, v);
2013-11-01 06:02:46 -04:00
puts("");
2013-10-30 03:37:43 -04:00
#endif
return v;
}
2013-11-26 02:38:39 -05:00
static pic_sym
new_uniq_sym(pic_state *pic, pic_sym base)
{
int s = pic->uniq_sym_count++;
char *str;
pic_sym uniq;
2013-11-26 07:05:37 -05:00
str = (char *)pic_alloc(pic, strlen(pic_symbol_name(pic, base)) + (int)log10(s) + 2);
2013-11-26 02:38:39 -05:00
sprintf(str, "%s@%d", pic_symbol_name(pic, base), s);
uniq = pic_intern_cstr(pic, str);
pic_free(pic, str);
return uniq;
}