picrin/lib/eval.c

1151 lines
29 KiB
C
Raw Normal View History

/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-20 10:58:58 -05:00
#include "picrin/extra.h"
2016-02-20 11:13:16 -05:00
#include "picrin/private/object.h"
2016-02-21 05:53:31 -05:00
#include "picrin/private/vm.h"
2016-02-20 11:13:16 -05:00
#include "picrin/private/state.h"
2016-02-06 09:55:39 -05:00
2016-02-22 09:57:07 -05:00
static pic_value pic_compile(pic_state *, pic_value);
2016-02-22 12:37:33 -05:00
#define EQ(sym, lit) (strcmp(pic_sym(pic, sym), lit) == 0)
2016-02-20 14:34:26 -05:00
#define S(lit) (pic_intern_lit(pic, lit))
2016-02-22 09:57:07 -05:00
static void
define_macro(pic_state *pic, pic_value uid, pic_value mac)
{
if (pic_weak_has(pic, pic->macros, uid)) {
pic_warnf(pic, "redefining syntax variable: %s", pic_sym(pic, uid));
2016-02-22 09:57:07 -05:00
}
pic_weak_set(pic, pic->macros, uid, mac);
}
static bool
find_macro(pic_state *pic, pic_value uid, pic_value *mac)
{
if (! pic_weak_has(pic, pic->macros, uid)) {
return false;
}
*mac = pic_weak_ref(pic, pic->macros, uid);
return true;
}
static void
shadow_macro(pic_state *pic, pic_value uid)
{
if (pic_weak_has(pic, pic->macros, uid)) {
pic_weak_del(pic, pic->macros, uid);
}
}
static pic_value expand(pic_state *, pic_value expr, pic_value env, pic_value deferred);
static pic_value expand_lambda(pic_state *, pic_value expr, pic_value env);
static pic_value
expand_var(pic_state *pic, pic_value id, pic_value env, pic_value deferred)
{
pic_value mac, functor;
functor = pic_find_identifier(pic, id, env);
if (find_macro(pic, functor, &mac)) {
return expand(pic, pic_call(pic, mac, 2, id, env), env, deferred);
}
return functor;
}
static pic_value
expand_quote(pic_state *pic, pic_value expr)
{
return pic_cons(pic, S("quote"), pic_cdr(pic, expr));
}
static pic_value
expand_list(pic_state *pic, pic_value obj, pic_value env, pic_value deferred)
{
size_t ai = pic_enter(pic);
pic_value x, head, tail;
if (pic_pair_p(pic, obj)) {
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);
}
pic_leave(pic, ai);
pic_protect(pic, x);
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), pic_invalid_value(pic));
pic_set_car(pic, deferred, pic_cons(pic, pic_cons(pic, expr, skel), pic_car(pic, deferred)));
return skel;
}
static void
expand_deferred(pic_state *pic, pic_value deferred, pic_value 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, pic_value env)
{
pic_value formal, body;
pic_value 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)) {
pic_add_identifier(pic, pic_car(pic, a), in);
}
if (pic_id_p(pic, a)) {
pic_add_identifier(pic, a, in);
}
deferred = pic_list(pic, 1, pic_nil_value(pic));
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);
return pic_list(pic, 3, S("lambda"), formal, body);
}
static pic_value
expand_define(pic_state *pic, pic_value expr, pic_value env, pic_value deferred)
{
pic_value uid, val;
uid = pic_add_identifier(pic, pic_list_ref(pic, expr, 1), env);
shadow_macro(pic, uid);
val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred);
return pic_list(pic, 3, S("define"), uid, val);
}
static pic_value
expand_defmacro(pic_state *pic, pic_value expr, pic_value env)
{
pic_value uid, val;
uid = pic_add_identifier(pic, pic_list_ref(pic, expr, 1), env);
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-22 14:03:42 -05:00
pic_error(pic, "macro definition evaluates to non-procedure object", 1, pic_list_ref(pic, expr, 1));
2016-02-22 09:57:07 -05:00
}
define_macro(pic, uid, val);
return pic_undef_value(pic);
}
static pic_value
expand_node(pic_state *pic, pic_value expr, pic_value env, pic_value deferred)
{
switch (pic_type(pic, expr)) {
case PIC_TYPE_ID:
case PIC_TYPE_SYMBOL: {
return expand_var(pic, expr, env, deferred);
}
case PIC_TYPE_PAIR: {
pic_value mac;
if (! pic_list_p(pic, expr)) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "cannot expand improper list", 1, expr);
2016-02-22 09:57:07 -05:00
}
if (pic_id_p(pic, pic_car(pic, expr))) {
pic_value functor;
functor = pic_find_identifier(pic, pic_car(pic, expr), env);
if (EQ(functor, "define-macro")) {
return expand_defmacro(pic, expr, env);
}
else if (EQ(functor, "lambda")) {
return expand_defer(pic, expr, deferred);
}
else if (EQ(functor, "define")) {
return expand_define(pic, expr, env, deferred);
}
else if (EQ(functor, "quote")) {
return expand_quote(pic, expr);
}
if (find_macro(pic, functor, &mac)) {
return expand(pic, pic_call(pic, mac, 2, expr, env), env, deferred);
}
}
return expand_list(pic, expr, env, deferred);
}
default:
return expr;
}
}
static pic_value
expand(pic_state *pic, pic_value expr, pic_value env, pic_value deferred)
{
size_t ai = pic_enter(pic);
pic_value v;
v = expand_node(pic, expr, env, deferred);
pic_leave(pic, ai);
pic_protect(pic, v);
return v;
}
pic_value
pic_expand(pic_state *pic, pic_value expr, pic_value env)
{
pic_value v, deferred;
deferred = pic_list(pic, 1, pic_nil_value(pic));
v = expand(pic, expr, env, deferred);
expand_deferred(pic, deferred, env);
return v;
}
2016-02-06 09:55:39 -05:00
static pic_value
optimize_beta(pic_state *pic, pic_value expr)
{
2016-02-19 02:17:13 -05:00
size_t ai = pic_enter(pic);
2016-02-06 09:55:39 -05:00
pic_value functor, formals, args, tmp, val, it, defs;
if (! pic_list_p(pic, expr))
2016-02-06 09:55:39 -05:00
return expr;
if (pic_nil_p(pic, expr))
2016-02-06 09:55:39 -05:00
return expr;
if (pic_sym_p(pic, pic_list_ref(pic, expr, 0))) {
2016-02-20 01:31:14 -05:00
pic_value sym = pic_list_ref(pic, expr, 0);
2016-02-06 09:55:39 -05:00
2016-02-20 14:34:26 -05:00
if (EQ(sym, "quote")) {
2016-02-06 09:55:39 -05:00
return expr;
2016-02-20 14:34:26 -05:00
} else if (EQ(sym, "lambda")) {
return pic_list(pic, 3, S("lambda"), pic_list_ref(pic, expr, 1), optimize_beta(pic, pic_list_ref(pic, expr, 2)));
2016-02-06 09:55:39 -05:00
}
}
tmp = pic_nil_value(pic);
2016-02-06 09:55:39 -05:00
pic_for_each (val, expr, it) {
pic_push(pic, optimize_beta(pic, val), tmp);
}
expr = pic_reverse(pic, tmp);
2016-02-19 02:17:13 -05:00
pic_leave(pic, ai);
pic_protect(pic, expr);
2016-02-06 09:55:39 -05:00
functor = pic_list_ref(pic, expr, 0);
2016-02-20 14:34:26 -05:00
if (pic_pair_p(pic, functor) && EQ(pic_car(pic, functor), "lambda")) {
2016-02-06 09:55:39 -05:00
formals = pic_list_ref(pic, functor, 1);
if (! pic_list_p(pic, formals))
2016-02-06 09:55:39 -05:00
goto exit; /* TODO: support ((lambda args x) 1 2) */
args = pic_cdr(pic, expr);
if (pic_length(pic, formals) != pic_length(pic, args))
goto exit;
defs = pic_nil_value(pic);
2016-02-06 09:55:39 -05:00
pic_for_each (val, args, it) {
2016-02-20 14:34:26 -05:00
pic_push(pic, pic_list(pic, 3, S("define"), pic_car(pic, formals), val), defs);
2016-02-06 09:55:39 -05:00
formals = pic_cdr(pic, formals);
}
expr = pic_list_ref(pic, functor, 2);
pic_for_each (val, defs, it) {
2016-02-20 14:34:26 -05:00
expr = pic_list(pic, 3, S("begin"), val, expr);
2016-02-06 09:55:39 -05:00
}
}
exit:
2016-02-19 02:17:13 -05:00
pic_leave(pic, ai);
pic_protect(pic, expr);
2016-02-06 09:55:39 -05:00
return expr;
}
static pic_value
pic_optimize(pic_state *pic, pic_value expr)
{
return optimize_beta(pic, expr);
}
2016-03-13 23:03:49 -04:00
static pic_value normalize(pic_state *pic, pic_value expr, pic_value locals, bool in);
2016-02-06 09:55:39 -05:00
2016-03-13 23:03:49 -04:00
static pic_value
normalize_body(pic_state *pic, pic_value expr, bool in)
2016-02-06 09:55:39 -05:00
{
2016-03-13 23:03:49 -04:00
pic_value v, locals;
locals = pic_list(pic, 1, pic_nil_value(pic));
2016-02-06 09:55:39 -05:00
2016-03-13 23:03:49 -04:00
v = normalize(pic, expr, locals, in);
if (! in) {
return v;
2016-02-06 09:55:39 -05:00
}
2016-03-13 23:03:49 -04:00
return pic_list(pic, 3, S("let"), pic_car(pic, locals), v);
}
static pic_value
normalize(pic_state *pic, pic_value expr, pic_value locals, bool in)
{
pic_value proc, e, it, r;
if (! pic_list_p(pic, expr))
return expr;
if (! pic_pair_p(pic, expr))
return expr;
proc = pic_list_ref(pic, expr, 0);
if (pic_sym_p(pic, proc)) {
pic_value sym = proc;
if (EQ(sym, "define")) {
pic_value var, val;
var = pic_list_ref(pic, expr, 1);
if (! in) { /* global */
if (pic_weak_has(pic, pic->globals, var)) {
pic_warnf(pic, "redefining variable: %s", pic_sym(pic, var));
}
pic_weak_set(pic, pic->globals, var, pic_invalid_value(pic));
} else { /* local */
bool found = false;
pic_for_each (e, pic_car(pic, locals), it) {
if (pic_eq_p(pic, e, var)) {
pic_warnf(pic, "redefining variable: %s", pic_sym(pic, var));
found = true;
break;
}
}
if (! found) {
pic_set_car(pic, locals, pic_cons(pic, var, pic_car(pic, locals)));
}
}
val = normalize(pic, pic_list_ref(pic, expr, 2), locals, in);
return pic_list(pic, 3, S("set!"), var, val);
}
else if (EQ(sym, "lambda")) {
return pic_list(pic, 3, S("lambda"), pic_list_ref(pic, expr, 1), normalize_body(pic, pic_list_ref(pic, expr, 2), true));
}
else if (EQ(sym, "quote")) {
return expr;
}
2016-02-06 09:55:39 -05:00
}
2016-03-13 23:03:49 -04:00
r = pic_nil_value(pic);
pic_for_each (e, expr, it) {
pic_push(pic, normalize(pic, e, locals, in), r);
2016-02-06 09:55:39 -05:00
}
2016-03-13 23:03:49 -04:00
return pic_reverse(pic, r);
}
2016-02-06 09:55:39 -05:00
2016-03-13 23:03:49 -04:00
static pic_value
pic_normalize(pic_state *pic, pic_value expr)
{
return normalize_body(pic, expr, false);
2016-02-06 09:55:39 -05:00
}
2016-03-13 23:03:49 -04:00
typedef struct analyze_scope {
int depth;
pic_value args, locals, captures;
struct analyze_scope *up;
} analyze_scope;
2016-02-06 09:55:39 -05:00
static void
2016-03-13 23:03:49 -04:00
analyzer_scope_init(pic_state *pic, analyze_scope *scope, pic_value args, pic_value locals, analyze_scope *up)
2016-02-06 09:55:39 -05:00
{
2016-03-13 23:03:49 -04:00
scope->args = args;
scope->locals = locals;
scope->captures = pic_make_dict(pic);
scope->up = up;
scope->depth = up ? up->depth + 1 : 0;
2016-02-06 09:55:39 -05:00
}
static bool
2016-02-22 09:49:39 -05:00
find_local_var(pic_state *pic, analyze_scope *scope, pic_value sym)
2016-02-06 09:55:39 -05:00
{
2016-03-13 23:03:49 -04:00
pic_value args, locals;
/* args */
for (args = scope->args; pic_pair_p(pic, args); args = pic_cdr(pic, args)) {
if (pic_eq_p(pic, pic_car(pic, args), sym))
return true;
}
if (! pic_nil_p(pic, args)) {
if (pic_eq_p(pic, args, sym))
return true;
}
/* locals */
for (locals = scope->locals; pic_pair_p(pic, locals); locals = pic_cdr(pic, locals)) {
if (pic_eq_p(pic, pic_car(pic, locals), sym))
return true;
}
return false;
2016-02-06 09:55:39 -05:00
}
static int
2016-02-20 01:31:14 -05:00
find_var(pic_state *pic, analyze_scope *scope, pic_value sym)
2016-02-06 09:55:39 -05:00
{
2016-02-20 01:31:14 -05:00
int depth = 0;
2016-02-06 09:55:39 -05:00
while (scope) {
2016-02-22 09:49:39 -05:00
if (find_local_var(pic, scope, sym)) {
2016-02-06 09:55:39 -05:00
if (depth > 0) {
2016-02-20 01:31:14 -05:00
pic_dict_set(pic, scope->captures, sym, pic_true_value(pic)); /* capture! */
2016-02-06 09:55:39 -05:00
}
return depth;
}
depth++;
scope = scope->up;
}
2016-03-13 23:03:49 -04:00
return depth - 1; /* global variable */
2016-02-06 09:55:39 -05:00
}
static pic_value analyze(pic_state *, analyze_scope *, pic_value);
static pic_value analyze_lambda(pic_state *, analyze_scope *, pic_value);
static pic_value
2016-02-20 01:31:14 -05:00
analyze_var(pic_state *pic, analyze_scope *scope, pic_value sym)
2016-02-06 09:55:39 -05:00
{
int depth;
depth = find_var(pic, scope, sym);
if (depth == scope->depth) {
2016-02-20 14:34:26 -05:00
return pic_list(pic, 2, S("gref"), sym);
2016-02-06 09:55:39 -05:00
} else if (depth == 0) {
2016-02-20 14:34:26 -05:00
return pic_list(pic, 2, S("lref"), sym);
2016-02-06 09:55:39 -05:00
} else {
2016-02-20 14:34:26 -05:00
return pic_list(pic, 3, S("cref"), pic_int_value(pic, depth), sym);
2016-02-06 09:55:39 -05:00
}
}
static pic_value
analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form)
{
analyze_scope s, *scope = &s;
2016-03-13 23:03:49 -04:00
pic_value body, args, locals;
2016-02-06 09:55:39 -05:00
2016-03-13 23:03:49 -04:00
args = pic_list_ref(pic, form, 1);
locals = pic_list_ref(pic, pic_list_ref(pic, form, 2), 1);
body = pic_list_ref(pic, pic_list_ref(pic, form, 2), 2);
2016-02-06 09:55:39 -05:00
2016-03-13 23:03:49 -04:00
analyzer_scope_init(pic, scope, args, locals, up);
2016-02-06 09:55:39 -05:00
/* analyze body */
body = analyze(pic, scope, body);
2016-03-13 23:03:49 -04:00
return pic_list(pic, 5, S("lambda"), args, locals, scope->captures, body);
2016-02-06 09:55:39 -05:00
}
static pic_value
analyze_list(pic_state *pic, analyze_scope *scope, pic_value obj)
{
pic_value seq = pic_nil_value(pic), val, it;
2016-02-06 09:55:39 -05:00
pic_for_each (val, obj, it) {
pic_push(pic, analyze(pic, scope, val), seq);
}
return pic_reverse(pic, seq);
}
static pic_value
analyze_call(pic_state *pic, analyze_scope *scope, pic_value obj)
{
2016-02-20 14:34:26 -05:00
return pic_cons(pic, S("call"), analyze_list(pic, scope, obj));
2016-02-06 09:55:39 -05:00
}
static pic_value
analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj)
{
switch (pic_type(pic, obj)) {
case PIC_TYPE_SYMBOL: {
2016-02-20 01:31:14 -05:00
return analyze_var(pic, scope, obj);
2016-02-06 09:55:39 -05:00
}
case PIC_TYPE_PAIR: {
2016-02-06 09:55:39 -05:00
pic_value proc;
if (! pic_list_p(pic, obj)) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "invalid expression given", 1, obj);
2016-02-06 09:55:39 -05:00
}
proc = pic_list_ref(pic, obj, 0);
if (pic_sym_p(pic, proc)) {
2016-02-20 01:31:14 -05:00
pic_value sym = proc;
2016-02-06 09:55:39 -05:00
2016-03-13 23:03:49 -04:00
if (EQ(sym, "lambda")) {
return analyze_lambda(pic, scope, obj);
2016-02-06 09:55:39 -05:00
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "quote")) {
2016-02-06 09:55:39 -05:00
return obj;
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "begin") || EQ(sym, "set!") || EQ(sym, "if")) {
2016-02-06 09:55:39 -05:00
return pic_cons(pic, pic_car(pic, obj), analyze_list(pic, scope, pic_cdr(pic, obj)));
}
}
return analyze_call(pic, scope, obj);
}
default:
2016-02-20 14:34:26 -05:00
return pic_list(pic, 2, S("quote"), obj);
2016-02-06 09:55:39 -05:00
}
}
static pic_value
analyze(pic_state *pic, analyze_scope *scope, pic_value obj)
{
2016-02-19 02:17:13 -05:00
size_t ai = pic_enter(pic);
2016-02-06 09:55:39 -05:00
pic_value res;
res = analyze_node(pic, scope, obj);
2016-02-19 02:17:13 -05:00
pic_leave(pic, ai);
pic_protect(pic, res);
2016-02-06 09:55:39 -05:00
return res;
}
static pic_value
pic_analyze(pic_state *pic, pic_value obj)
{
analyze_scope s, *scope = &s;
2016-03-13 23:03:49 -04:00
analyzer_scope_init(pic, scope, pic_nil_value(pic), pic_nil_value(pic), NULL);
2016-02-06 09:55:39 -05:00
obj = analyze(pic, scope, obj);
return obj;
}
typedef struct codegen_context {
/* rest args variable is counted as a local */
2016-02-20 01:31:14 -05:00
pic_value rest;
2016-02-19 07:56:45 -05:00
pic_value args, locals, captures;
2016-02-06 09:55:39 -05:00
/* actual bit code sequence */
2016-02-21 06:32:00 -05:00
struct code *code;
2016-02-06 09:55:39 -05:00
size_t clen, ccapa;
/* child ireps */
2016-02-21 06:32:00 -05:00
struct irep **irep;
2016-02-06 09:55:39 -05:00
size_t ilen, icapa;
/* constant object pool */
int *ints;
size_t klen, kcapa;
double *nums;
size_t flen, fcapa;
2016-02-21 06:32:00 -05:00
struct object **pool;
2016-02-06 09:55:39 -05:00
size_t plen, pcapa;
struct codegen_context *up;
} codegen_context;
static void create_activation(pic_state *, codegen_context *);
static void
2016-03-13 23:03:49 -04:00
codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up, pic_value args, pic_value locals, pic_value captures)
2016-02-06 09:55:39 -05:00
{
2016-03-13 23:03:49 -04:00
pic_value tmp;
int i, it;
for (i = 0, tmp = args; pic_pair_p(pic, tmp); tmp = pic_cdr(pic, tmp))
i++;
cxt->args = pic_make_vec(pic, i, NULL);
for (i = 0, tmp = args; pic_pair_p(pic, tmp); tmp = pic_cdr(pic, tmp)) {
pic_vec_set(pic, cxt->args, i++, pic_car(pic, tmp));
}
cxt->rest = tmp;
i = pic_length(pic, locals);
if (pic_sym_p(pic, cxt->rest)) {
i++;
}
cxt->locals = pic_make_vec(pic, i, NULL);
i = 0;
if (pic_sym_p(pic, cxt->rest)) {
pic_vec_set(pic, cxt->locals, i++, cxt->rest);
}
for (tmp = locals; pic_pair_p(pic, tmp); tmp = pic_cdr(pic, tmp)) {
pic_vec_set(pic, cxt->locals, i++, pic_car(pic, tmp));
}
cxt->captures = pic_make_vec(pic, pic_dict_size(pic, captures), NULL);
it = i = 0;
while (pic_dict_next(pic, captures, &it, &tmp, NULL)) {
pic_vec_set(pic, cxt->captures, i++, tmp);
}
2016-02-06 09:55:39 -05:00
2016-03-13 23:03:49 -04:00
cxt->up = up;
2016-02-06 09:55:39 -05:00
2016-02-21 06:32:00 -05:00
cxt->code = pic_calloc(pic, PIC_ISEQ_SIZE, sizeof(struct code));
2016-02-06 09:55:39 -05:00
cxt->clen = 0;
cxt->ccapa = PIC_ISEQ_SIZE;
2016-02-21 06:32:00 -05:00
cxt->irep = pic_calloc(pic, PIC_IREP_SIZE, sizeof(struct irep *));
2016-02-06 09:55:39 -05:00
cxt->ilen = 0;
cxt->icapa = PIC_IREP_SIZE;
2016-02-21 06:32:00 -05:00
cxt->pool = pic_calloc(pic, PIC_POOL_SIZE, sizeof(struct object *));
2016-02-06 09:55:39 -05:00
cxt->plen = 0;
cxt->pcapa = PIC_POOL_SIZE;
cxt->ints = pic_calloc(pic, PIC_POOL_SIZE, sizeof(int));
cxt->klen = 0;
cxt->kcapa = PIC_POOL_SIZE;
cxt->nums = pic_calloc(pic, PIC_POOL_SIZE, sizeof(double));
cxt->flen = 0;
cxt->fcapa = PIC_POOL_SIZE;
create_activation(pic, cxt);
}
2016-02-21 06:32:00 -05:00
static struct irep *
2016-02-06 09:55:39 -05:00
codegen_context_destroy(pic_state *pic, codegen_context *cxt)
{
2016-02-21 06:32:00 -05:00
struct irep *irep;
2016-02-06 09:55:39 -05:00
/* create irep */
2016-02-21 06:32:00 -05:00
irep = pic_malloc(pic, sizeof(struct irep));
2016-03-06 18:23:05 -05:00
irep->list.next = irep->list.prev = 0;
2016-02-06 09:55:39 -05:00
irep->refc = 1;
2016-02-20 01:31:14 -05:00
irep->varg = pic_sym_p(pic, cxt->rest);
2016-02-19 07:56:45 -05:00
irep->argc = pic_vec_len(pic, cxt->args) + 1;
irep->localc = pic_vec_len(pic, cxt->locals);
irep->capturec = pic_vec_len(pic, cxt->captures);
2016-02-21 06:32:00 -05:00
irep->code = pic_realloc(pic, cxt->code, sizeof(struct code) * cxt->clen);
irep->irep = pic_realloc(pic, cxt->irep, sizeof(struct irep *) * cxt->ilen);
2016-02-07 23:51:41 -05:00
irep->ints = pic_realloc(pic, cxt->ints, sizeof(int) * cxt->klen);
irep->nums = pic_realloc(pic, cxt->nums, sizeof(double) * cxt->flen);
2016-02-21 06:32:00 -05:00
irep->pool = pic_realloc(pic, cxt->pool, sizeof(struct object *) * cxt->plen);
2016-02-06 09:55:39 -05:00
irep->ncode = cxt->clen;
irep->nirep = cxt->ilen;
irep->nints = cxt->klen;
irep->nnums = cxt->flen;
irep->npool = cxt->plen;
2016-03-06 18:23:05 -05:00
if (irep->npool > 0) {
irep->list.next = pic->ireps.next;
irep->list.prev = &pic->ireps;
irep->list.next->prev = &irep->list;
irep->list.prev->next = &irep->list;
}
2016-02-06 09:55:39 -05:00
return irep;
}
#define check_size(pic, cxt, x, name, type) do { \
if (cxt->x##len >= cxt->x##capa) { \
cxt->x##capa *= 2; \
cxt->name = pic_realloc(pic, cxt->name, sizeof(type) * cxt->x##capa); \
} \
} while (0)
2016-02-21 06:32:00 -05:00
#define check_code_size(pic, cxt) check_size(pic, cxt, c, code, struct code)
#define check_irep_size(pic, cxt) check_size(pic, cxt, i, irep, struct irep *)
#define check_pool_size(pic, cxt) check_size(pic, cxt, p, pool, struct object *)
2016-02-06 09:55:39 -05:00
#define check_ints_size(pic, cxt) check_size(pic, cxt, k, ints, int)
#define check_nums_size(pic, cxt) check_size(pic, cxt, f, nums, double)
#define emit_n(pic, cxt, ins) do { \
check_code_size(pic, cxt); \
cxt->code[cxt->clen].insn = ins; \
cxt->clen++; \
} while (0) \
#define emit_i(pic, cxt, ins, I) do { \
check_code_size(pic, cxt); \
cxt->code[cxt->clen].insn = ins; \
cxt->code[cxt->clen].a = I; \
cxt->clen++; \
} while (0) \
#define emit_r(pic, cxt, ins, D, I) do { \
check_code_size(pic, cxt); \
cxt->code[cxt->clen].insn = ins; \
cxt->code[cxt->clen].a = D; \
cxt->code[cxt->clen].b = I; \
cxt->clen++; \
} while (0) \
#define emit_ret(pic, cxt, tailpos) if (tailpos) emit_n(pic, cxt, OP_RET)
2016-03-03 07:38:50 -05:00
struct {
const char *name;
int insn;
int argc;
} pic_vm_proc[] = {
{ "picrin.base/cons", OP_CONS, 2 },
{ "picrin.base/car", OP_CAR, 1 },
{ "picrin.base/cdr", OP_CDR, 1 },
{ "picrin.base/null?", OP_NILP, 1 },
{ "picrin.base/symbol?", OP_SYMBOLP, 1 },
{ "picrin.base/pair?", OP_PAIRP, 1 },
{ "picrin.base/not", OP_NOT, 1 },
{ "picrin.base/=", OP_EQ, 2 },
{ "picrin.base/<", OP_LT, 2 },
{ "picrin.base/<=", OP_LE, 2 },
{ "picrin.base/>", OP_GT, 2 },
{ "picrin.base/>=", OP_GE, 2 },
{ "picrin.base/+", OP_ADD, 2 },
{ "picrin.base/-", OP_SUB, 2 },
{ "picrin.base/*", OP_MUL, 2 },
{ "picrin.base//", OP_DIV, 2 }
};
2016-02-06 09:55:39 -05:00
static int
2016-02-20 01:31:14 -05:00
index_capture(pic_state *pic, codegen_context *cxt, pic_value sym, int depth)
2016-02-06 09:55:39 -05:00
{
int i;
while (depth-- > 0) {
cxt = cxt->up;
}
2016-02-19 07:56:45 -05:00
for (i = 0; i < pic_vec_len(pic, cxt->captures); ++i) {
2016-02-20 01:31:14 -05:00
if (pic_eq_p(pic, sym, pic_vec_ref(pic, cxt->captures, i)))
2016-02-06 09:55:39 -05:00
return i;
}
return -1;
}
static int
2016-02-20 01:31:14 -05:00
index_local(pic_state *pic, codegen_context *cxt, pic_value sym)
2016-02-06 09:55:39 -05:00
{
int i, offset;
offset = 1;
2016-02-19 07:56:45 -05:00
for (i = 0; i < pic_vec_len(pic, cxt->args); ++i) {
2016-02-20 01:31:14 -05:00
if (pic_eq_p(pic, sym, pic_vec_ref(pic, cxt->args, i)))
2016-02-06 09:55:39 -05:00
return i + offset;
}
offset += i;
2016-02-19 07:56:45 -05:00
for (i = 0; i < pic_vec_len(pic, cxt->locals); ++i) {
2016-02-20 01:31:14 -05:00
if (pic_eq_p(pic, sym, pic_vec_ref(pic, cxt->locals, i)))
2016-02-06 09:55:39 -05:00
return i + offset;
}
return -1;
}
static int
2016-02-20 01:31:14 -05:00
index_global(pic_state *pic, codegen_context *cxt, pic_value name)
2016-02-06 09:55:39 -05:00
{
int pidx;
check_pool_size(pic, cxt);
pidx = (int)cxt->plen++;
2016-02-21 06:32:00 -05:00
cxt->pool[pidx] = (struct object *)pic_sym_ptr(pic, name);
2016-02-06 09:55:39 -05:00
return pidx;
}
static void
create_activation(pic_state *pic, codegen_context *cxt)
{
int i, n;
2016-02-19 07:56:45 -05:00
for (i = 0; i < pic_vec_len(pic, cxt->captures); ++i) {
2016-02-20 01:31:14 -05:00
pic_value sym = pic_vec_ref(pic, cxt->captures, i);
2016-02-19 07:56:45 -05:00
n = index_local(pic, cxt, sym);
2016-02-06 09:55:39 -05:00
assert(n != -1);
2016-02-20 01:31:14 -05:00
if (n <= pic_vec_len(pic, cxt->args) || pic_eq_p(pic, sym, cxt->rest)) {
2016-02-06 09:55:39 -05:00
/* copy arguments to capture variable area */
emit_i(pic, cxt, OP_LREF, n);
} else {
/* otherwise, just extend the stack */
emit_n(pic, cxt, OP_PUSHUNDEF);
}
}
}
static void codegen(pic_state *, codegen_context *, pic_value, bool);
static void
codegen_ref(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos)
{
2016-02-20 01:31:14 -05:00
pic_value sym;
2016-02-06 09:55:39 -05:00
2016-02-20 01:31:14 -05:00
sym = pic_car(pic, obj);
2016-02-20 14:34:26 -05:00
if (EQ(sym, "gref")) {
2016-02-20 01:31:14 -05:00
pic_value name;
2016-02-06 09:55:39 -05:00
2016-02-20 01:31:14 -05:00
name = pic_list_ref(pic, obj, 1);
2016-02-06 09:55:39 -05:00
emit_i(pic, cxt, OP_GREF, index_global(pic, cxt, name));
emit_ret(pic, cxt, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "cref")) {
2016-02-20 01:31:14 -05:00
pic_value name;
2016-02-06 09:55:39 -05:00
int depth;
depth = pic_int(pic, pic_list_ref(pic, obj, 1));
2016-02-20 01:31:14 -05:00
name = pic_list_ref(pic, obj, 2);
2016-02-19 07:56:45 -05:00
emit_r(pic, cxt, OP_CREF, depth, index_capture(pic, cxt, name, depth));
2016-02-06 09:55:39 -05:00
emit_ret(pic, cxt, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "lref")) {
2016-02-20 01:31:14 -05:00
pic_value name;
2016-02-06 09:55:39 -05:00
int i;
2016-02-20 01:31:14 -05:00
name = pic_list_ref(pic, obj, 1);
2016-02-19 07:56:45 -05:00
if ((i = index_capture(pic, cxt, name, 0)) != -1) {
emit_i(pic, cxt, OP_LREF, i + pic_vec_len(pic, cxt->args) + pic_vec_len(pic, cxt->locals) + 1);
2016-02-06 09:55:39 -05:00
emit_ret(pic, cxt, tailpos);
} else {
2016-02-19 07:56:45 -05:00
emit_i(pic, cxt, OP_LREF, index_local(pic, cxt, name));
2016-02-06 09:55:39 -05:00
emit_ret(pic, cxt, tailpos);
}
}
}
static void
codegen_set(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos)
{
pic_value var, val;
2016-02-20 01:31:14 -05:00
pic_value type;
2016-02-06 09:55:39 -05:00
val = pic_list_ref(pic, obj, 2);
codegen(pic, cxt, val, false);
var = pic_list_ref(pic, obj, 1);
2016-02-20 01:31:14 -05:00
type = pic_list_ref(pic, var, 0);
2016-02-20 14:34:26 -05:00
if (EQ(type, "gref")) {
2016-02-20 01:31:14 -05:00
pic_value name;
2016-03-03 07:41:29 -05:00
size_t i;
2016-02-06 09:55:39 -05:00
2016-02-20 01:31:14 -05:00
name = pic_list_ref(pic, var, 1);
2016-03-03 07:41:29 -05:00
for (i = 0; i < sizeof pic_vm_proc / sizeof pic_vm_proc[0]; ++i) {
if (EQ(name, pic_vm_proc[i].name))
pic_error(pic, "tried to override built-in procedure", 1, name);
}
2016-02-06 09:55:39 -05:00
emit_i(pic, cxt, OP_GSET, index_global(pic, cxt, name));
emit_ret(pic, cxt, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(type, "cref")) {
2016-02-20 01:31:14 -05:00
pic_value name;
2016-02-06 09:55:39 -05:00
int depth;
depth = pic_int(pic, pic_list_ref(pic, var, 1));
2016-02-20 01:31:14 -05:00
name = pic_list_ref(pic, var, 2);
2016-02-19 07:56:45 -05:00
emit_r(pic, cxt, OP_CSET, depth, index_capture(pic, cxt, name, depth));
2016-02-06 09:55:39 -05:00
emit_ret(pic, cxt, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(type, "lref")) {
2016-02-20 01:31:14 -05:00
pic_value name;
2016-02-06 09:55:39 -05:00
int i;
2016-02-20 01:31:14 -05:00
name = pic_list_ref(pic, var, 1);
2016-02-19 07:56:45 -05:00
if ((i = index_capture(pic, cxt, name, 0)) != -1) {
emit_i(pic, cxt, OP_LSET, i + pic_vec_len(pic, cxt->args) + pic_vec_len(pic, cxt->locals) + 1);
2016-02-06 09:55:39 -05:00
emit_ret(pic, cxt, tailpos);
} else {
2016-02-19 07:56:45 -05:00
emit_i(pic, cxt, OP_LSET, index_local(pic, cxt, name));
2016-02-06 09:55:39 -05:00
emit_ret(pic, cxt, tailpos);
}
}
}
static void
codegen_lambda(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos)
{
codegen_context c, *inner_cxt = &c;
2016-03-13 23:03:49 -04:00
pic_value args, locals, captures, body;
2016-02-06 09:55:39 -05:00
check_irep_size(pic, cxt);
/* extract arguments */
2016-03-13 23:03:49 -04:00
args = pic_list_ref(pic, obj, 1);
locals = pic_list_ref(pic, obj, 2);
captures = pic_list_ref(pic, obj, 3);
body = pic_list_ref(pic, obj, 4);
2016-02-06 09:55:39 -05:00
/* emit irep */
2016-03-13 23:03:49 -04:00
codegen_context_init(pic, inner_cxt, cxt, args, locals, captures);
2016-02-06 09:55:39 -05:00
codegen(pic, inner_cxt, body, true);
2016-02-07 23:51:41 -05:00
cxt->irep[cxt->ilen] = codegen_context_destroy(pic, inner_cxt);
2016-02-06 09:55:39 -05:00
/* emit OP_LAMBDA */
emit_i(pic, cxt, OP_LAMBDA, cxt->ilen++);
emit_ret(pic, cxt, tailpos);
}
static void
codegen_if(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos)
{
int s, t;
codegen(pic, cxt, pic_list_ref(pic, obj, 1), false);
s = (int)cxt->clen;
emit_n(pic, cxt, OP_JMPIF);
/* if false branch */
codegen(pic, cxt, pic_list_ref(pic, obj, 3), tailpos);
t = (int)cxt->clen;
emit_n(pic, cxt, OP_JMP);
cxt->code[s].a = (int)cxt->clen - s;
/* if true branch */
codegen(pic, cxt, pic_list_ref(pic, obj, 2), tailpos);
cxt->code[t].a = (int)cxt->clen - t;
}
static void
codegen_begin(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos)
{
codegen(pic, cxt, pic_list_ref(pic, obj, 1), false);
emit_n(pic, cxt, OP_POP);
codegen(pic, cxt, pic_list_ref(pic, obj, 2), tailpos);
}
static void
codegen_quote(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos)
{
int pidx;
obj = pic_list_ref(pic, obj, 1);
switch (pic_type(pic, obj)) {
case PIC_TYPE_UNDEF:
2016-02-06 09:55:39 -05:00
emit_n(pic, cxt, OP_PUSHUNDEF);
break;
case PIC_TYPE_TRUE:
emit_n(pic, cxt, OP_PUSHTRUE);
2016-02-06 09:55:39 -05:00
break;
case PIC_TYPE_FALSE:
emit_n(pic, cxt, OP_PUSHFALSE);
break;
case PIC_TYPE_INT:
2016-02-06 09:55:39 -05:00
check_ints_size(pic, cxt);
pidx = (int)cxt->klen++;
cxt->ints[pidx] = pic_int(pic, obj);
2016-02-06 09:55:39 -05:00
emit_i(pic, cxt, OP_PUSHINT, pidx);
break;
case PIC_TYPE_FLOAT:
2016-02-06 09:55:39 -05:00
check_nums_size(pic, cxt);
pidx = (int)cxt->flen++;
cxt->nums[pidx] = pic_float(pic, obj);
2016-02-06 09:55:39 -05:00
emit_i(pic, cxt, OP_PUSHFLOAT, pidx);
break;
case PIC_TYPE_NIL:
2016-02-06 09:55:39 -05:00
emit_n(pic, cxt, OP_PUSHNIL);
break;
case PIC_TYPE_EOF:
2016-02-06 09:55:39 -05:00
emit_n(pic, cxt, OP_PUSHEOF);
break;
case PIC_TYPE_CHAR:
2016-02-06 09:55:39 -05:00
check_ints_size(pic, cxt);
pidx = (int)cxt->klen++;
cxt->ints[pidx] = pic_char(pic, obj);
2016-02-06 09:55:39 -05:00
emit_i(pic, cxt, OP_PUSHCHAR, pidx);
break;
default:
assert(pic_obj_p(pic,obj));
2016-02-06 09:55:39 -05:00
check_pool_size(pic, cxt);
pidx = (int)cxt->plen++;
cxt->pool[pidx] = pic_obj_ptr(obj);
emit_i(pic, cxt, OP_PUSHCONST, pidx);
break;
}
emit_ret(pic, cxt, tailpos);
}
static void
codegen_call(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos)
{
2016-03-03 04:43:57 -05:00
int len = pic_length(pic, obj);
2016-02-06 09:55:39 -05:00
pic_value elt, it, functor;
functor = pic_list_ref(pic, obj, 1);
2016-02-20 14:34:26 -05:00
if (EQ(pic_list_ref(pic, functor, 0), "gref")) {
2016-02-20 01:31:14 -05:00
pic_value sym;
2016-03-03 07:38:50 -05:00
size_t i;
2016-02-06 09:55:39 -05:00
2016-02-20 01:31:14 -05:00
sym = pic_list_ref(pic, functor, 1);
2016-02-06 09:55:39 -05:00
2016-03-03 07:38:50 -05:00
for (i = 0; i < sizeof pic_vm_proc / sizeof pic_vm_proc[0]; ++i) {
if (EQ(sym, pic_vm_proc[i].name) && len == pic_vm_proc[i].argc + 2) {
pic_for_each (elt, pic_cddr(pic, obj), it) {
codegen(pic, cxt, elt, false);
}
emit_n(pic, cxt, pic_vm_proc[i].insn);
emit_ret(pic, cxt, tailpos);
return;
}
}
2016-02-06 09:55:39 -05:00
}
2016-03-03 07:38:50 -05:00
pic_for_each (elt, pic_cdr(pic, obj), it) {
codegen(pic, cxt, elt, false);
}
2016-02-06 09:55:39 -05:00
emit_i(pic, cxt, (tailpos ? OP_TAILCALL : OP_CALL), len - 1);
}
static void
codegen(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos)
{
2016-02-20 01:31:14 -05:00
pic_value sym;
2016-02-06 09:55:39 -05:00
2016-02-20 01:31:14 -05:00
sym = pic_car(pic, obj);
2016-02-20 14:34:26 -05:00
if (EQ(sym, "gref") || EQ(sym, "cref") || EQ(sym, "lref")) {
2016-02-06 09:55:39 -05:00
codegen_ref(pic, cxt, obj, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "set!") || EQ(sym, "define")) {
2016-02-06 09:55:39 -05:00
codegen_set(pic, cxt, obj, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "lambda")) {
2016-02-06 09:55:39 -05:00
codegen_lambda(pic, cxt, obj, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "if")) {
2016-02-06 09:55:39 -05:00
codegen_if(pic, cxt, obj, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "begin")) {
2016-02-06 09:55:39 -05:00
codegen_begin(pic, cxt, obj, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "quote")) {
2016-02-06 09:55:39 -05:00
codegen_quote(pic, cxt, obj, tailpos);
}
2016-02-20 14:34:26 -05:00
else if (EQ(sym, "call")) {
2016-02-06 09:55:39 -05:00
codegen_call(pic, cxt, obj, tailpos);
}
else {
2016-02-22 14:03:42 -05:00
pic_error(pic, "codegen: unknown AST type", 1, obj);
2016-02-06 09:55:39 -05:00
}
}
2016-02-21 06:32:00 -05:00
static struct irep *
2016-02-06 09:55:39 -05:00
pic_codegen(pic_state *pic, pic_value obj)
{
codegen_context c, *cxt = &c;
2016-03-13 23:03:49 -04:00
codegen_context_init(pic, cxt, NULL, pic_nil_value(pic), pic_nil_value(pic), pic_make_dict(pic));
2016-02-06 09:55:39 -05:00
codegen(pic, cxt, obj, true);
return codegen_context_destroy(pic, cxt);
}
2016-02-19 02:17:13 -05:00
#define SAVE(pic, ai, obj) pic_leave(pic, ai); pic_protect(pic, obj)
2016-02-06 09:55:39 -05:00
2016-02-22 09:57:07 -05:00
static pic_value
2016-02-06 10:07:29 -05:00
pic_compile(pic_state *pic, pic_value obj)
2016-02-06 09:55:39 -05:00
{
2016-02-21 06:32:00 -05:00
struct irep *irep;
2016-02-19 10:03:16 -05:00
pic_value proc;
2016-02-19 02:17:13 -05:00
size_t ai = pic_enter(pic);
2016-02-06 09:55:39 -05:00
2016-02-21 06:13:05 -05:00
#if 0
2016-02-23 06:50:26 -05:00
pic_printf(pic, "# input expression\n~s\n", obj);
2016-02-06 09:55:39 -05:00
#endif
/* optimize */
obj = pic_optimize(pic, obj);
2016-02-21 06:13:05 -05:00
#if 0
2016-02-23 06:50:26 -05:00
pic_printf(pic, "## optimize completed\n~s\n", obj);
2016-03-13 23:03:49 -04:00
#endif
SAVE(pic, ai, obj);
/* normalize */
obj = pic_normalize(pic, obj);
#if 0
pic_printf(pic, "## normalize completed\n~s\n", obj);
2016-02-06 09:55:39 -05:00
#endif
SAVE(pic, ai, obj);
/* analyze */
obj = pic_analyze(pic, obj);
2016-02-21 06:13:05 -05:00
#if 0
2016-02-23 06:50:26 -05:00
pic_printf(pic, "## analyzer completed\n~s\n", obj);
2016-02-06 09:55:39 -05:00
#endif
SAVE(pic, ai, obj);
/* codegen */
irep = pic_codegen(pic, obj);
proc = pic_make_proc_irep(pic, irep, NULL);
pic_irep_decref(pic, irep);
return proc;
}
pic_value
pic_eval(pic_state *pic, pic_value program, const char *lib)
{
const char *prev_lib = pic_current_library(pic);
pic_value env, r, e;
2016-02-06 14:23:46 -05:00
env = pic_library_environment(pic, lib);
pic_in_library(pic, lib);
2016-02-06 14:23:46 -05:00
pic_try {
r = pic_call(pic, pic_compile(pic, pic_expand(pic, program, env)), 0);
2016-02-06 14:23:46 -05:00
}
pic_catch(e) {
pic_in_library(pic, prev_lib);
pic_raise(pic, e);
2016-02-06 14:23:46 -05:00
}
pic_in_library(pic, prev_lib);
2016-02-06 14:23:46 -05:00
return r;
}
static pic_value
pic_eval_eval(pic_state *pic)
{
pic_value program;
const char *str;
pic_get_args(pic, "oz", &program, &str);
return pic_eval(pic, program, str);
}
void
pic_init_eval(pic_state *pic)
{
2014-09-08 06:38:19 -04:00
pic_defun(pic, "eval", pic_eval_eval);
}