2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
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>
|
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-12-07 08:36:14 -05:00
|
|
|
#include "picrin/lib.h"
|
2013-10-30 03:37:43 -04:00
|
|
|
#include "xhash/xhash.h"
|
|
|
|
|
2013-11-28 04:40:31 -05:00
|
|
|
static pic_value macroexpand(pic_state *, pic_value, struct pic_senv *);
|
|
|
|
static pic_value macroexpand_list(pic_state *, pic_value, struct pic_senv *);
|
|
|
|
|
2013-12-06 04:04:36 -05:00
|
|
|
struct pic_senv *
|
2013-12-07 10:04:26 -05:00
|
|
|
pic_null_syntactic_env(pic_state *pic)
|
2013-11-27 00:19:46 -05:00
|
|
|
{
|
|
|
|
struct pic_senv *senv;
|
|
|
|
|
|
|
|
senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
|
|
|
|
senv->up = NULL;
|
2013-12-06 04:04:36 -05:00
|
|
|
senv->tbl = xh_new();
|
|
|
|
senv->stx = (struct pic_syntax **)pic_calloc(pic, PIC_MACROS_SIZE, sizeof(struct pic_syntax *));
|
|
|
|
senv->xlen = 0;
|
|
|
|
senv->xcapa = PIC_MACROS_SIZE;
|
|
|
|
|
2013-12-07 10:04:26 -05:00
|
|
|
return senv;
|
|
|
|
}
|
|
|
|
|
2013-12-06 04:04:36 -05:00
|
|
|
#define register_core_syntax(pic,senv,kind,name) do { \
|
|
|
|
senv->stx[senv->xlen] = pic_syntax_new(pic, kind, pic_intern_cstr(pic, name)); \
|
|
|
|
xh_put(senv->tbl, name, ~senv->xlen); \
|
|
|
|
senv->xlen++; \
|
|
|
|
} while (0)
|
|
|
|
|
2013-12-07 10:04:26 -05:00
|
|
|
struct pic_senv *
|
|
|
|
pic_minimal_syntactic_env(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_senv *senv = pic_null_syntactic_env(pic);
|
|
|
|
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_DEFLIBRARY, "define-library");
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_IMPORT, "import");
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_EXPORT, "export");
|
|
|
|
|
|
|
|
return senv;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pic_senv *
|
|
|
|
pic_core_syntactic_env(pic_state *pic)
|
|
|
|
{
|
2013-12-07 23:43:57 -05:00
|
|
|
struct pic_senv *senv = pic_minimal_syntactic_env(pic);
|
2013-12-07 10:04:26 -05:00
|
|
|
|
2013-12-06 04:04:36 -05:00
|
|
|
register_core_syntax(pic, senv, PIC_STX_DEFINE, "define");
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_SET, "set!");
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_QUOTE, "quote");
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_LAMBDA, "lambda");
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_IF, "if");
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_BEGIN, "begin");
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_DEFMACRO, "define-macro");
|
|
|
|
register_core_syntax(pic, senv, PIC_STX_DEFSYNTAX, "define-syntax");
|
|
|
|
|
2013-11-27 00:19:46 -05:00
|
|
|
return senv;
|
|
|
|
}
|
|
|
|
|
2013-12-07 10:04:26 -05:00
|
|
|
#undef register_core_syntax
|
|
|
|
|
2013-12-06 04:04:36 -05:00
|
|
|
static struct pic_senv *
|
|
|
|
new_global_senv(pic_state *pic)
|
|
|
|
{
|
2013-12-07 08:36:14 -05:00
|
|
|
return pic->lib->senv;
|
2013-12-06 04:04:36 -05:00
|
|
|
}
|
|
|
|
|
2013-11-27 00:19:46 -05:00
|
|
|
static struct pic_senv *
|
|
|
|
new_local_senv(pic_state *pic, pic_value formals, struct pic_senv *up)
|
|
|
|
{
|
|
|
|
struct pic_senv *senv;
|
|
|
|
pic_value a;
|
|
|
|
pic_sym sym;
|
|
|
|
|
|
|
|
senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
|
|
|
|
senv->up = up;
|
|
|
|
senv->tbl = xh_new();
|
|
|
|
senv->stx = NULL;
|
|
|
|
senv->xlen = 0;
|
|
|
|
senv->xcapa = 0;
|
|
|
|
|
|
|
|
for (a = formals; pic_pair_p(a); a = pic_cdr(pic, a)) {
|
2013-11-28 04:40:31 -05:00
|
|
|
pic_value v = pic_car(pic, a);
|
|
|
|
|
|
|
|
if (! pic_symbol_p(v)) {
|
|
|
|
v = macroexpand(pic, v, up);
|
|
|
|
}
|
|
|
|
if (! pic_symbol_p(v)) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
sym = pic_sym(v);
|
2014-01-12 02:03:36 -05:00
|
|
|
xh_put(senv->tbl, pic_symbol_name(pic, sym), (int)pic_gensym(pic, sym));
|
2013-11-27 00:19:46 -05:00
|
|
|
}
|
2013-11-28 04:40:31 -05:00
|
|
|
if (! pic_symbol_p(a)) {
|
|
|
|
a = macroexpand(pic, a, up);
|
|
|
|
}
|
2013-11-27 00:19:46 -05:00
|
|
|
if (pic_symbol_p(a)) {
|
|
|
|
sym = pic_sym(a);
|
2014-01-12 02:03:36 -05:00
|
|
|
xh_put(senv->tbl, pic_symbol_name(pic, sym), (int)pic_gensym(pic, sym));
|
2013-11-27 00:19:46 -05:00
|
|
|
}
|
2013-11-28 04:40:31 -05:00
|
|
|
else if (! pic_nil_p(a)) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
2013-11-27 00:19:46 -05:00
|
|
|
return senv;
|
|
|
|
}
|
|
|
|
|
2013-11-27 00:00:23 -05:00
|
|
|
struct pic_syntax *
|
|
|
|
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;
|
|
|
|
stx->macro = NULL;
|
2013-11-27 01:58:28 -05:00
|
|
|
stx->senv = NULL;
|
2013-11-27 00:00:23 -05:00
|
|
|
return stx;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pic_syntax *
|
2013-11-27 01:58:28 -05:00
|
|
|
pic_syntax_new_macro(pic_state *pic, pic_sym sym, struct pic_proc *macro, struct pic_senv *mac_env)
|
2013-11-27 00:00:23 -05:00
|
|
|
{
|
|
|
|
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-27 01:58:28 -05:00
|
|
|
stx->senv = mac_env;
|
2013-11-27 00:00:23 -05:00
|
|
|
return stx;
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:04:44 -05:00
|
|
|
static struct pic_sc *
|
|
|
|
sc_new(pic_state *pic, pic_value expr, struct pic_senv *senv)
|
|
|
|
{
|
|
|
|
struct pic_sc *sc;
|
|
|
|
|
|
|
|
sc = (struct pic_sc *)pic_obj_alloc(pic, sizeof(struct pic_sc), PIC_TT_SC);
|
|
|
|
sc->expr = expr;
|
|
|
|
sc->senv = senv;
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:26:07 -05:00
|
|
|
static bool
|
|
|
|
pic_identifier_p(pic_value obj)
|
|
|
|
{
|
|
|
|
if (pic_symbol_p(obj)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (pic_sc_p(obj)) {
|
|
|
|
return pic_identifier_p(pic_sc(obj)->expr);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-09 10:26:51 -05:00
|
|
|
static pic_value
|
|
|
|
strip(pic_state *pic, pic_value expr)
|
|
|
|
{
|
|
|
|
if (pic_sc_p(expr)) {
|
|
|
|
return strip(pic, pic_sc(expr)->expr);
|
|
|
|
}
|
|
|
|
else if (pic_pair_p(expr)) {
|
|
|
|
return pic_cons(pic,
|
|
|
|
strip(pic, pic_car(pic, expr)),
|
|
|
|
strip(pic, pic_cdr(pic, expr)));
|
|
|
|
}
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2014-01-08 01:38:31 -05:00
|
|
|
void
|
|
|
|
pic_import(pic_state *pic, pic_value spec)
|
|
|
|
{
|
|
|
|
struct pic_lib *lib;
|
2014-01-12 05:53:20 -05:00
|
|
|
struct xh_iter it;
|
2014-01-08 01:38:31 -05:00
|
|
|
|
|
|
|
lib = pic_find_library(pic, spec);
|
|
|
|
if (! lib) {
|
|
|
|
pic_error(pic, "library not found");
|
|
|
|
}
|
|
|
|
it = xh_begin(lib->exports);
|
|
|
|
while (! xh_isend(&it)) {
|
|
|
|
#if DEBUG
|
|
|
|
if (it.e->val >= 0) {
|
|
|
|
printf("* importing %s as %s\n", it.e->key, pic_symbol_name(pic, (pic_sym)it.e->val));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("* importing %s\n", it.e->key);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (it.e->val >= 0) {
|
|
|
|
xh_put(pic->lib->senv->tbl, it.e->key, it.e->val);
|
|
|
|
}
|
|
|
|
else { /* syntax object */
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t idx;
|
2014-01-08 01:38:31 -05:00
|
|
|
struct pic_senv *senv = pic->lib->senv;
|
|
|
|
|
|
|
|
idx = senv->xlen;
|
|
|
|
if (idx >= senv->xcapa) {
|
|
|
|
pic_abort(pic, "macro table overflow");
|
|
|
|
}
|
|
|
|
/* bring macro object from imported lib */
|
|
|
|
senv->stx[idx] = lib->senv->stx[~it.e->val];
|
|
|
|
xh_put(senv->tbl, it.e->key, ~idx);
|
|
|
|
senv->xlen++;
|
|
|
|
}
|
|
|
|
xh_next(lib->exports, &it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-07 23:44:39 -05:00
|
|
|
void
|
|
|
|
pic_export(pic_state *pic, pic_sym sym)
|
|
|
|
{
|
2013-12-08 03:52:34 -05:00
|
|
|
struct xh_entry *e;
|
2013-12-07 23:44:39 -05:00
|
|
|
|
2013-12-08 03:52:34 -05:00
|
|
|
e = xh_get(pic->lib->senv->tbl, pic_symbol_name(pic, sym));
|
|
|
|
if (! e) {
|
|
|
|
pic_error(pic, "symbol not defined");
|
|
|
|
}
|
|
|
|
xh_put(pic->lib->exports, e->key, e->val);
|
2013-12-07 23:44:39 -05:00
|
|
|
}
|
|
|
|
|
2013-11-27 01:58:28 -05:00
|
|
|
static void
|
|
|
|
pic_defsyntax(pic_state *pic, const char *name, struct pic_proc *macro, struct pic_senv *mac_env)
|
2013-11-27 00:00:23 -05:00
|
|
|
{
|
2013-12-06 10:51:32 -05:00
|
|
|
struct pic_syntax *stx;
|
2013-12-07 08:36:14 -05:00
|
|
|
struct pic_senv *global_senv = pic->lib->senv;
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t idx;
|
2013-11-27 00:00:23 -05:00
|
|
|
|
2013-12-06 10:51:32 -05:00
|
|
|
stx = pic_syntax_new_macro(pic, pic_intern_cstr(pic, name), macro, mac_env);
|
|
|
|
|
2013-12-07 08:36:14 -05:00
|
|
|
idx = global_senv->xlen;
|
|
|
|
if (idx >= global_senv->xcapa) {
|
2013-11-27 00:00:23 -05:00
|
|
|
pic_abort(pic, "macro table overflow");
|
|
|
|
}
|
2013-12-07 08:36:14 -05:00
|
|
|
global_senv->stx[idx] = stx;
|
|
|
|
xh_put(global_senv->tbl, name, ~idx);
|
|
|
|
global_senv->xlen++;
|
2013-11-27 00:00:23 -05:00
|
|
|
}
|
|
|
|
|
2013-11-27 01:58:28 -05:00
|
|
|
void
|
|
|
|
pic_defmacro(pic_state *pic, const char *name, struct pic_proc *macro)
|
|
|
|
{
|
|
|
|
pic_defsyntax(pic, name, macro, NULL);
|
|
|
|
}
|
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
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-12-10 04:50:53 -05:00
|
|
|
#if DEBUG
|
|
|
|
printf("[macroexpand] expanding... ");
|
|
|
|
pic_debug(pic, expr);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
2013-11-26 11:43:58 -05:00
|
|
|
switch (pic_type(expr)) {
|
2013-11-27 01:04:02 -05:00
|
|
|
case PIC_TT_SC: {
|
|
|
|
struct pic_sc *sc;
|
|
|
|
|
|
|
|
sc = pic_sc(expr);
|
|
|
|
return macroexpand(pic, sc->expr, sc->senv);
|
|
|
|
}
|
2013-10-30 03:37:43 -04:00
|
|
|
case PIC_TT_SYMBOL: {
|
2013-11-26 11:43:58 -05:00
|
|
|
struct xh_entry *e;
|
2013-12-09 12:32:30 -05:00
|
|
|
pic_sym uniq;
|
2014-01-10 03:55:58 -05:00
|
|
|
|
2014-01-12 02:03:36 -05:00
|
|
|
if (! pic_interned_p(pic, pic_sym(expr))) {
|
2014-01-10 03:55:58 -05:00
|
|
|
return expr;
|
|
|
|
}
|
2013-12-09 12:32:30 -05:00
|
|
|
while (true) {
|
2013-11-26 11:43:58 -05:00
|
|
|
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]);
|
|
|
|
}
|
2013-12-09 12:32:30 -05:00
|
|
|
if (! senv->up)
|
|
|
|
break;
|
2013-11-26 11:43:58 -05:00
|
|
|
senv = senv->up;
|
|
|
|
}
|
2014-01-12 02:03:36 -05:00
|
|
|
uniq = pic_gensym(pic, pic_sym(expr));
|
2013-12-09 12:32:30 -05:00
|
|
|
xh_put(senv->tbl, pic_symbol_name(pic, pic_sym(expr)), (int)uniq);
|
|
|
|
return pic_symbol_value(uniq);
|
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
|
|
|
car = macroexpand(pic, pic_car(pic, expr), senv);
|
|
|
|
if (pic_syntax_p(car)) {
|
|
|
|
switch (pic_syntax(car)->kind) {
|
2013-12-07 06:02:39 -05:00
|
|
|
case PIC_STX_DEFLIBRARY: {
|
2013-12-10 02:00:13 -05:00
|
|
|
pic_value exprs;
|
|
|
|
struct pic_lib *prev = pic->lib;
|
2013-12-07 06:02:39 -05:00
|
|
|
|
2013-12-07 12:42:34 -05:00
|
|
|
if (pic_length(pic, expr) < 2) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
2013-12-10 02:00:13 -05:00
|
|
|
pic_make_library(pic, pic_cadr(pic, expr));
|
2013-12-07 21:59:35 -05:00
|
|
|
|
2013-12-10 02:00:13 -05:00
|
|
|
/* proceed expressions in new library */
|
2013-12-07 21:59:35 -05:00
|
|
|
pic_in_library(pic, pic_cadr(pic, expr));
|
|
|
|
{
|
2013-12-10 02:00:13 -05:00
|
|
|
struct pic_proc *proc;
|
|
|
|
|
|
|
|
for (exprs = pic_cddr(pic, expr); ! pic_nil_p(exprs); exprs = pic_cdr(pic, exprs)) {
|
|
|
|
v = pic_car(pic, exprs);
|
|
|
|
|
2014-01-20 02:57:39 -05:00
|
|
|
proc = pic_compile(pic, v);
|
2013-12-10 02:00:13 -05:00
|
|
|
if (proc == NULL) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
pic_apply_argv(pic, proc, 0);
|
|
|
|
if (pic_undef_p(v)) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 21:59:35 -05:00
|
|
|
}
|
|
|
|
pic_in_library(pic, prev->name);
|
2013-12-07 06:02:39 -05:00
|
|
|
|
2014-01-08 01:22:23 -05:00
|
|
|
return pic_none_value();
|
2013-12-07 06:02:39 -05:00
|
|
|
}
|
2013-12-07 12:42:34 -05:00
|
|
|
case PIC_STX_IMPORT: {
|
2013-12-09 12:22:38 -05:00
|
|
|
for (v = pic_cdr(pic, expr); ! pic_nil_p(v); v = pic_cdr(pic, v)) {
|
|
|
|
pic_value spec = pic_car(pic, v);
|
|
|
|
|
2014-01-08 01:38:31 -05:00
|
|
|
pic_import(pic, spec);
|
2013-12-07 12:42:34 -05:00
|
|
|
}
|
2014-01-08 01:22:23 -05:00
|
|
|
return pic_none_value();
|
2013-12-07 12:42:34 -05:00
|
|
|
}
|
2013-12-07 06:02:39 -05:00
|
|
|
case PIC_STX_EXPORT: {
|
2013-12-09 12:22:38 -05:00
|
|
|
for (v = pic_cdr(pic, expr); ! pic_nil_p(v); v = pic_cdr(pic, v)) {
|
|
|
|
pic_value spec = pic_car(pic, v);
|
|
|
|
if (! pic_symbol_p(spec)) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
/* TODO: warn if symbol is shadowed by local variable */
|
|
|
|
pic_export(pic, pic_sym(spec));
|
2013-12-07 12:42:34 -05:00
|
|
|
}
|
2014-01-08 01:22:23 -05:00
|
|
|
return pic_none_value();
|
2013-12-07 06:02:39 -05:00
|
|
|
}
|
2013-11-27 01:58:28 -05:00
|
|
|
case PIC_STX_DEFSYNTAX: {
|
|
|
|
pic_value var, val;
|
|
|
|
struct pic_proc *proc;
|
|
|
|
|
|
|
|
if (pic_length(pic, expr) != 3) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
|
2013-12-09 12:26:28 -05:00
|
|
|
var = strip(pic, pic_cadr(pic, expr));
|
2013-11-27 01:58:28 -05:00
|
|
|
if (! pic_symbol_p(var)) {
|
|
|
|
pic_error(pic, "syntax error");
|
|
|
|
}
|
|
|
|
|
|
|
|
val = pic_cadr(pic, pic_cdr(pic, expr));
|
2014-01-20 02:57:39 -05:00
|
|
|
proc = pic_compile(pic, val);
|
2013-11-27 01:58:28 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
assert(pic_proc_p(v));
|
|
|
|
pic_defsyntax(pic, pic_symbol_name(pic, pic_sym(var)), pic_proc_ptr(v), senv);
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
2014-01-08 01:22:23 -05:00
|
|
|
return pic_none_value();
|
2013-11-27 01:58:28 -05:00
|
|
|
}
|
2013-11-26 11:43:58 -05:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-01-20 02:57:39 -05:00
|
|
|
proc = pic_compile(pic, val);
|
2013-10-30 11:31:33 -04:00
|
|
|
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);
|
2014-01-08 01:22:23 -05:00
|
|
|
return pic_none_value();
|
2013-10-30 03:37:43 -04:00
|
|
|
}
|
2013-11-26 11:43:58 -05:00
|
|
|
case PIC_STX_MACRO: {
|
2013-11-27 01:58:28 -05:00
|
|
|
if (pic_syntax(car)->senv == NULL) { /* legacy macro */
|
|
|
|
v = pic_apply(pic, pic_syntax(car)->macro, pic_cdr(pic, expr));
|
|
|
|
if (pic->errmsg) {
|
|
|
|
printf("macroexpand error: %s\n", pic->errmsg);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
v = pic_apply_argv(pic, pic_syntax(car)->macro, 3, expr, pic_obj_value(senv), pic_obj_value(pic_syntax(car)->senv));
|
|
|
|
if (pic->errmsg) {
|
|
|
|
printf("macroexpand error: %s\n", pic->errmsg);
|
|
|
|
abort();
|
|
|
|
}
|
2013-10-30 03:37:43 -04:00
|
|
|
}
|
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-27 01:58:28 -05:00
|
|
|
#if DEBUG
|
|
|
|
puts("after expand-1:");
|
|
|
|
pic_debug(pic, v);
|
|
|
|
puts("");
|
|
|
|
#endif
|
|
|
|
|
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: {
|
2013-11-27 00:19:46 -05:00
|
|
|
struct pic_senv *in = new_local_senv(pic, pic_cadr(pic, expr), senv);
|
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;
|
|
|
|
|
|
|
|
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)) {
|
2013-11-27 00:19:46 -05:00
|
|
|
struct pic_senv *in = new_local_senv(pic, pic_cdr(pic, var), senv);
|
2013-11-26 07:06:46 -05:00
|
|
|
pic_value a;
|
2013-11-27 00:19:46 -05:00
|
|
|
pic_sym sym;
|
2013-11-26 07:06:46 -05:00
|
|
|
|
2013-11-26 19:51:48 -05:00
|
|
|
/* defined symbol */
|
|
|
|
a = pic_car(pic, var);
|
2013-12-09 10:27:11 -05:00
|
|
|
if (! pic_symbol_p(a)) {
|
|
|
|
a = macroexpand(pic, a, senv);
|
|
|
|
}
|
2013-11-27 00:19:46 -05:00
|
|
|
if (! pic_symbol_p(a)) {
|
|
|
|
pic_error(pic, "binding to non-symbol object");
|
2013-11-26 07:06:46 -05:00
|
|
|
}
|
2013-11-27 00:19:46 -05:00
|
|
|
sym = pic_sym(a);
|
2014-01-12 02:03:36 -05:00
|
|
|
xh_put(senv->tbl, pic_symbol_name(pic, sym), (int)pic_gensym(pic, sym));
|
2013-11-26 07:06:46 -05:00
|
|
|
|
2013-11-27 00:19:46 -05:00
|
|
|
/* binding value */
|
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)));
|
2013-11-27 00:19:46 -05:00
|
|
|
|
2013-11-26 07:06:46 -05:00
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
pic_gc_protect(pic, v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-12-09 10:27:11 -05:00
|
|
|
if (! pic_symbol_p(var)) {
|
|
|
|
var = macroexpand(pic, var, senv);
|
|
|
|
}
|
2013-11-27 01:57:23 -05:00
|
|
|
if (! pic_symbol_p(var)) {
|
|
|
|
pic_error(pic, "binding to non-symbol object");
|
|
|
|
}
|
2014-01-12 02:03:36 -05:00
|
|
|
uniq = pic_gensym(pic, pic_sym(var));
|
2013-11-26 07:06:46 -05:00
|
|
|
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:
|
2014-01-12 06:21:07 -05:00
|
|
|
v = pic_cons(pic, pic_symbol_value(pic_syntax(car)->sym), pic_cdr(pic, expr));
|
2013-11-26 07:06:46 -05:00
|
|
|
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:
|
2013-12-07 06:58:18 -05:00
|
|
|
case PIC_TT_LIB:
|
2014-01-08 10:39:13 -05:00
|
|
|
case PIC_TT_VAR:
|
2014-01-18 02:51:54 -05:00
|
|
|
case PIC_TT_IREP:
|
2013-11-26 07:06:46 -05:00
|
|
|
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-12-02 23:07:25 -05:00
|
|
|
if (! pic_pair_p(list))
|
2013-11-26 12:07:57 -05:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-11-27 00:19:46 -05:00
|
|
|
senv = new_global_senv(pic);
|
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
|
|
|
}
|
2013-11-27 01:04:44 -05:00
|
|
|
|
|
|
|
static pic_value
|
2013-11-27 03:26:07 -05:00
|
|
|
pic_macro_make_sc(pic_state *pic)
|
2013-11-27 01:04:44 -05:00
|
|
|
{
|
|
|
|
pic_value senv, free_vars, expr;
|
|
|
|
struct pic_sc *sc;
|
|
|
|
|
|
|
|
pic_get_args(pic, "ooo", &senv, &free_vars, &expr);
|
|
|
|
|
|
|
|
if (! pic_senv_p(senv))
|
|
|
|
pic_error(pic, "make-syntactic-closure: senv required");
|
|
|
|
|
|
|
|
/* just ignore free_vars for now */
|
|
|
|
sc = sc_new(pic, expr, pic_senv(senv));
|
|
|
|
|
|
|
|
return pic_obj_value(sc);
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:26:07 -05: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(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_macro_identifier_eq_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value e, x, f, y;
|
|
|
|
struct pic_senv *e1, *e2;
|
|
|
|
|
|
|
|
pic_get_args(pic, "oooo", &e, &x, &f, &y);
|
|
|
|
|
|
|
|
if (! pic_senv_p(e)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 1");
|
|
|
|
}
|
|
|
|
e1 = pic_senv(e);
|
|
|
|
if (! pic_senv_p(f)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 3");
|
|
|
|
}
|
|
|
|
e2 = pic_senv(f);
|
|
|
|
|
2013-12-10 04:50:53 -05:00
|
|
|
if (! (pic_identifier_p(x) && pic_identifier_p(y))) {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:26:07 -05:00
|
|
|
x = macroexpand(pic, x, e1);
|
|
|
|
y = macroexpand(pic, y, e2);
|
|
|
|
|
|
|
|
return pic_bool_value(pic_eq_p(x, y));
|
|
|
|
}
|
|
|
|
|
2014-01-09 02:34:22 -05:00
|
|
|
static pic_value
|
|
|
|
er_macro_rename(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_sym sym;
|
|
|
|
struct pic_senv *mac_env;
|
2014-01-10 03:55:02 -05:00
|
|
|
pic_value v;
|
2014-01-09 02:34:22 -05:00
|
|
|
|
|
|
|
pic_get_args(pic, "m", &sym);
|
|
|
|
|
|
|
|
mac_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 1));
|
|
|
|
|
2014-01-10 03:55:02 -05:00
|
|
|
v = macroexpand(pic, pic_symbol_value(sym), mac_env);
|
|
|
|
if (pic_syntax_p(v)) {
|
|
|
|
return pic_symbol_value(sym);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return v;
|
|
|
|
}
|
2014-01-09 02:34:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
er_macro_compare(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value a, b;
|
2014-01-10 03:52:31 -05:00
|
|
|
struct pic_senv *use_env;
|
|
|
|
|
|
|
|
pic_get_args(pic, "oo", &a, &b);
|
2014-01-09 02:34:22 -05:00
|
|
|
|
2014-01-10 03:52:31 -05:00
|
|
|
if (! pic_symbol_p(a) || ! pic_symbol_p(b))
|
|
|
|
return pic_false_value(); /* should be an error? */
|
2014-01-09 02:34:22 -05:00
|
|
|
|
|
|
|
use_env = pic_senv_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
|
|
|
|
|
2014-01-10 03:52:31 -05:00
|
|
|
a = macroexpand(pic, a, use_env);
|
|
|
|
b = macroexpand(pic, b, use_env);
|
2014-01-09 02:34:22 -05:00
|
|
|
|
|
|
|
return pic_bool_value(pic_eq_p(a, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
er_macro_call(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value expr, use_env, mac_env;
|
|
|
|
struct pic_proc *rename, *compare, *cb;
|
|
|
|
|
|
|
|
pic_get_args(pic, "ooo", &expr, &use_env, &mac_env);
|
|
|
|
|
|
|
|
if (! pic_senv_p(use_env)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 1");
|
|
|
|
}
|
|
|
|
if (! pic_senv_p(mac_env)) {
|
|
|
|
pic_error(pic, "unexpected type of argument 3");
|
|
|
|
}
|
|
|
|
|
|
|
|
rename = pic_proc_new(pic, er_macro_rename);
|
2014-01-11 23:02:16 -05:00
|
|
|
pic_proc_cv_init(pic, rename, 2);
|
2014-01-09 02:34:22 -05:00
|
|
|
pic_proc_cv_set(pic, rename, 0, use_env);
|
|
|
|
pic_proc_cv_set(pic, rename, 1, mac_env);
|
|
|
|
|
|
|
|
compare = pic_proc_new(pic, er_macro_compare);
|
2014-01-11 23:02:16 -05:00
|
|
|
pic_proc_cv_init(pic, compare, 2);
|
2014-01-09 02:34:22 -05:00
|
|
|
pic_proc_cv_set(pic, compare, 0, use_env);
|
|
|
|
pic_proc_cv_set(pic, compare, 1, mac_env);
|
|
|
|
|
|
|
|
cb = pic_proc_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
|
|
|
|
|
|
|
|
return pic_apply_argv(pic, cb, 3, expr, pic_obj_value(rename), pic_obj_value(compare));
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_macro_er_macro_transformer(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *cb, *proc;
|
|
|
|
|
|
|
|
pic_get_args(pic, "l", &cb);
|
|
|
|
|
|
|
|
proc = pic_proc_new(pic, er_macro_call);
|
2014-01-11 23:02:16 -05:00
|
|
|
pic_proc_cv_init(pic, proc, 1);
|
2014-01-09 02:34:22 -05:00
|
|
|
pic_proc_cv_set(pic, proc, 0, pic_obj_value(cb));
|
|
|
|
|
|
|
|
return pic_obj_value(proc);
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:04:44 -05:00
|
|
|
void
|
|
|
|
pic_init_macro(pic_state *pic)
|
|
|
|
{
|
2013-12-10 04:49:48 -05:00
|
|
|
DEFLIBRARY(pic, "(picrin macro)")
|
|
|
|
{
|
|
|
|
pic_defun(pic, "make-syntactic-closure", pic_macro_make_sc);
|
|
|
|
pic_defun(pic, "identifier?", pic_macro_identifier_p);
|
|
|
|
pic_defun(pic, "identifier=?", pic_macro_identifier_eq_p);
|
2014-01-09 02:34:22 -05:00
|
|
|
pic_defun(pic, "er-macro-transformer", pic_macro_er_macro_transformer);
|
2013-12-10 04:49:48 -05:00
|
|
|
}
|
2013-12-10 10:26:43 -05:00
|
|
|
ENDLIBRARY(pic);
|
2013-11-27 01:04:44 -05:00
|
|
|
}
|