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)
|
|
|
|
|
2013-11-26 12:09:15 -05:00
|
|
|
void
|
2013-11-26 12:07:06 -05:00
|
|
|
pic_defmacro(pic_state *pic, const char *name, struct pic_proc *macro)
|
2013-10-30 03:37:43 -04:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
2013-11-26 19:50:38 -05:00
|
|
|
idx = pic->xlen;
|
2013-11-26 11:43:58 -05:00
|
|
|
if (idx >= pic->xcapa) {
|
2013-11-22 09:35:51 -05:00
|
|
|
pic_abort(pic, "macro table overflow");
|
|
|
|
}
|
2013-11-26 11:43:58 -05:00
|
|
|
pic->stx[idx] = pic_syntax_new_macro(pic, pic_intern_cstr(pic, name), macro);
|
2013-11-26 20:43:59 -05:00
|
|
|
xh_put(pic->var_tbl, name, ~idx);
|
2013-11-26 19:50:38 -05:00
|
|
|
pic->xlen++;
|
2013-10-30 03:37:43 -04:00
|
|
|
}
|
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
static pic_sym
|
|
|
|
new_uniq_sym(pic_state *pic, pic_sym base)
|
2013-10-30 03:37:43 -04:00
|
|
|
{
|
2013-11-26 11:43:58 -05:00
|
|
|
int s = ++pic->uniq_sym_count;
|
|
|
|
char *str;
|
|
|
|
pic_sym uniq;
|
2013-10-30 03:37:43 -04:00
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
str = (char *)pic_alloc(pic, strlen(pic_symbol_name(pic, base)) + (int)log10(s) + 2);
|
|
|
|
sprintf(str, "%s@%d", pic_symbol_name(pic, base), s);
|
|
|
|
uniq = pic_intern_cstr(pic, str);
|
2013-10-30 03:37:43 -04:00
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
pic_free(pic, str);
|
|
|
|
return uniq;
|
2013-10-30 03:37:43 -04:00
|
|
|
}
|
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
static pic_value macroexpand_list(pic_state *, pic_value, struct pic_senv *);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
switch (pic_type(expr)) {
|
2013-10-30 03:37:43 -04:00
|
|
|
case PIC_TT_SYMBOL: {
|
2013-11-26 11:43:58 -05:00
|
|
|
struct xh_entry *e;
|
|
|
|
while (senv) {
|
|
|
|
if ((e = xh_get(senv->tbl, pic_symbol_name(pic, pic_sym(expr)))) != NULL) {
|
|
|
|
if (e->val >= 0)
|
|
|
|
return pic_symbol_value((pic_sym)e->val);
|
|
|
|
else
|
|
|
|
return pic_obj_value(senv->stx[~e->val]);
|
|
|
|
}
|
|
|
|
senv = senv->up;
|
|
|
|
}
|
|
|
|
return expr;
|
2013-10-30 03:37:43 -04:00
|
|
|
}
|
|
|
|
case PIC_TT_PAIR: {
|
2013-11-26 11:43:58 -05:00
|
|
|
pic_value car, v;
|
2013-10-30 03:37:43 -04:00
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
if (! pic_list_p(pic, expr))
|
|
|
|
return expr;
|
2013-10-30 03:37:43 -04:00
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
car = macroexpand(pic, pic_car(pic, expr), senv);
|
|
|
|
if (pic_syntax_p(car)) {
|
|
|
|
switch (pic_syntax(car)->kind) {
|
|
|
|
case PIC_STX_DEFMACRO: {
|
2013-10-30 11:31:33 -04:00
|
|
|
pic_value var, val;
|
|
|
|
struct pic_proc *proc;
|
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
if (pic_length(pic, expr) < 2) {
|
2013-10-30 11:31:33 -04:00
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
var = pic_car(pic, pic_cdr(pic, expr));
|
2013-10-30 11:31:33 -04:00
|
|
|
if (pic_pair_p(var)) {
|
2013-11-26 20:43:49 -05:00
|
|
|
/* FIXME: unhygienic */
|
2013-10-30 11:31:33 -04:00
|
|
|
val = pic_cons(pic, pic_symbol_value(pic->sLAMBDA),
|
|
|
|
pic_cons(pic, pic_cdr(pic, var),
|
2013-11-26 11:43:58 -05:00
|
|
|
pic_cdr(pic, pic_cdr(pic, expr))));
|
2013-10-30 11:31:33 -04:00
|
|
|
var = pic_car(pic, var);
|
|
|
|
}
|
|
|
|
else {
|
2013-11-26 11:43:58 -05:00
|
|
|
if (pic_length(pic, expr) != 3) {
|
2013-10-30 11:31:33 -04:00
|
|
|
pic_error(pic, "syntax_error");
|
|
|
|
}
|
2013-11-26 11:43:58 -05:00
|
|
|
val = pic_car(pic, pic_cdr(pic, pic_cdr(pic, expr)));
|
2013-10-30 11:31:33 -04:00
|
|
|
}
|
|
|
|
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));
|
2013-11-26 12:07:06 -05:00
|
|
|
pic_defmacro(pic, pic_symbol_name(pic, pic_sym(var)), pic_proc_ptr(v));
|
2013-10-30 11:31:33 -04:00
|
|
|
|
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();
|
|
|
|
}
|
2013-11-26 11:43:58 -05:00
|
|
|
case PIC_STX_MACRO: {
|
|
|
|
v = pic_apply(pic, pic_syntax(car)->macro, pic_cdr(pic, expr));
|
2013-10-30 03:37:43 -04:00
|
|
|
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
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
return macroexpand(pic, v, senv);
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
|
|
|
case PIC_STX_LAMBDA: {
|
|
|
|
struct pic_senv *in;
|
|
|
|
pic_value a;
|
|
|
|
|
|
|
|
in = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
|
|
|
|
in->up = senv;
|
|
|
|
in->tbl = xh_new();
|
|
|
|
in->stx = NULL;
|
|
|
|
|
2013-11-26 19:51:17 -05:00
|
|
|
for (a = pic_cadr(pic, expr); pic_pair_p(a); a = pic_cdr(pic, a)) {
|
2013-11-26 07:06:46 -05:00
|
|
|
pic_sym gen, orig;
|
|
|
|
|
|
|
|
orig = pic_sym(pic_car(pic, a));
|
|
|
|
gen = new_uniq_sym(pic, orig);
|
2013-11-26 19:51:48 -05:00
|
|
|
xh_put(in->tbl, pic_symbol_name(pic, orig), (int)gen);
|
|
|
|
}
|
2013-11-26 19:51:17 -05:00
|
|
|
if (pic_symbol_p(a)) {
|
2013-11-26 19:51:48 -05:00
|
|
|
xh_put(in->tbl, pic_symbol_name(pic, pic_sym(a)), (int)new_uniq_sym(pic, pic_sym(a)));
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
v = pic_cons(pic, pic_symbol_value(pic_syntax(car)->sym),
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
case PIC_STX_DEFINE: {
|
|
|
|
pic_sym uniq;
|
|
|
|
pic_value var;
|
|
|
|
struct pic_senv *in = senv;
|
|
|
|
|
|
|
|
if (pic_length(pic, expr) < 2) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
var = pic_cadr(pic, expr);
|
2013-11-26 07:06:46 -05:00
|
|
|
if (pic_pair_p(var)) {
|
|
|
|
pic_value a;
|
|
|
|
|
|
|
|
in = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
|
|
|
|
in->up = senv;
|
|
|
|
in->tbl = xh_new();
|
|
|
|
in->stx = NULL;
|
2013-11-26 11:36:58 -05:00
|
|
|
in->xlen = 0;
|
|
|
|
in->xcapa = 0;
|
2013-11-26 07:06:46 -05:00
|
|
|
|
2013-11-26 19:51:48 -05:00
|
|
|
/* defined symbol */
|
|
|
|
a = pic_car(pic, var);
|
|
|
|
xh_put(senv->tbl, pic_symbol_name(pic, pic_sym(a)), (int)new_uniq_sym(pic, pic_sym(a)));
|
|
|
|
var = pic_cdr(pic, var);
|
|
|
|
|
2013-11-26 07:06:46 -05:00
|
|
|
for (a = var; pic_pair_p(a); a = pic_cdr(pic, a)) {
|
|
|
|
pic_sym gen, orig;
|
|
|
|
|
|
|
|
orig = pic_sym(pic_car(pic, a));
|
|
|
|
gen = new_uniq_sym(pic, orig);
|
2013-11-26 19:51:48 -05:00
|
|
|
xh_put(in->tbl, pic_symbol_name(pic, orig), (int)gen);
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
|
|
|
if (pic_symbol_p(a)) {
|
2013-11-26 19:51:48 -05:00
|
|
|
xh_put(in->tbl, pic_symbol_name(pic, pic_sym(a)), (int)new_uniq_sym(pic, pic_sym(a)));
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
v = pic_cons(pic, pic_symbol_value(pic_syntax(car)->sym),
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
uniq = new_uniq_sym(pic, pic_sym(var));
|
|
|
|
xh_put(senv->tbl, pic_symbol_name(pic, pic_sym(var)), (int)uniq);
|
|
|
|
}
|
|
|
|
FALLTHROUGH;
|
|
|
|
case PIC_STX_SET:
|
|
|
|
case PIC_STX_IF:
|
|
|
|
case PIC_STX_BEGIN:
|
|
|
|
v = pic_cons(pic, pic_symbol_value(pic_syntax(car)->sym), macroexpand_list(pic, pic_cdr(pic, expr), senv));
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
pic_gc_protect(pic, v);
|
|
|
|
return v;
|
|
|
|
case PIC_STX_QUOTE:
|
|
|
|
v = pic_cons(pic, pic_symbol_value(pic_syntax(car)->sym), pic_cdr(pic, expr));
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
pic_gc_protect(pic, v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
case PIC_TT_SYNTAX:
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
2013-11-26 12:07:57 -05:00
|
|
|
if (! pic_pair_p(list))
|
|
|
|
return macroexpand(pic, list, senv);
|
2013-11-26 07:06:46 -05:00
|
|
|
|
|
|
|
v = macroexpand(pic, pic_car(pic, list), senv);
|
|
|
|
return pic_cons(pic, v, macroexpand_list(pic, pic_cdr(pic, list), senv));
|
|
|
|
}
|
|
|
|
|
2013-11-26 11:32:05 -05:00
|
|
|
struct pic_syntax *
|
2013-11-26 07:06:46 -05:00
|
|
|
pic_syntax_new(pic_state *pic, int kind, pic_sym sym)
|
|
|
|
{
|
|
|
|
struct pic_syntax *stx;
|
|
|
|
|
|
|
|
stx = (struct pic_syntax *)pic_obj_alloc(pic, sizeof(struct pic_syntax), PIC_TT_SYNTAX);
|
|
|
|
stx->kind = kind;
|
|
|
|
stx->sym = sym;
|
2013-11-26 11:42:13 -05:00
|
|
|
stx->macro = NULL;
|
|
|
|
return stx;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pic_syntax *
|
|
|
|
pic_syntax_new_macro(pic_state *pic, pic_sym sym, struct pic_proc *macro)
|
|
|
|
{
|
|
|
|
struct pic_syntax *stx;
|
|
|
|
|
|
|
|
stx = (struct pic_syntax *)pic_obj_alloc(pic, sizeof(struct pic_syntax), PIC_TT_SYNTAX);
|
|
|
|
stx->kind = PIC_STX_MACRO;
|
|
|
|
stx->sym = sym;
|
|
|
|
stx->macro = macro;
|
2013-11-26 07:06:46 -05:00
|
|
|
return stx;
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
2013-11-26 11:43:58 -05:00
|
|
|
pic_macroexpand(pic_state *pic, pic_value expr)
|
2013-11-26 07:06:46 -05:00
|
|
|
{
|
|
|
|
struct pic_senv *senv;
|
2013-11-26 11:38:26 -05:00
|
|
|
pic_value v;
|
2013-11-26 07:06:46 -05:00
|
|
|
|
|
|
|
senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
|
|
|
|
senv->up = NULL;
|
2013-11-26 11:32:05 -05:00
|
|
|
senv->tbl = pic->var_tbl;
|
|
|
|
senv->stx = pic->stx;
|
2013-11-26 11:36:58 -05:00
|
|
|
senv->xlen = pic->xlen;
|
|
|
|
senv->xcapa = pic->xcapa;
|
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
|
|
|
|
|
|
|
|
v = macroexpand(pic, expr, senv);
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("after expand:");
|
|
|
|
pic_debug(pic, v);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return v;
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|