qse/ase/lsp/eval.c

288 lines
6.7 KiB
C
Raw Normal View History

2005-02-04 15:39:11 +00:00
/*
2005-09-24 08:16:02 +00:00
* $Id: eval.c,v 1.13 2005-09-24 08:16:02 bacon Exp $
2005-02-04 15:39:11 +00:00
*/
2005-09-18 10:18:35 +00:00
#include <xp/lsp/lsp.h>
2005-05-28 13:34:26 +00:00
#include <xp/lsp/env.h>
2005-09-18 10:18:35 +00:00
#include <xp/lsp/prim.h>
2005-04-24 07:48:16 +00:00
#include <xp/bas/assert.h>
2005-02-04 15:39:11 +00:00
2005-09-20 08:05:32 +00:00
static xp_lsp_obj_t* make_func (
xp_lsp_t* lsp, xp_lsp_obj_t* cdr, int is_macro);
static xp_lsp_obj_t* eval_cons (
xp_lsp_t* lsp, xp_lsp_obj_t* cons);
static xp_lsp_obj_t* apply (
xp_lsp_t* lsp, xp_lsp_obj_t* func, xp_lsp_obj_t* actual);
2005-02-04 15:39:11 +00:00
2005-09-18 10:18:35 +00:00
xp_lsp_obj_t* xp_lsp_eval (xp_lsp_t* lsp, xp_lsp_obj_t* obj)
2005-02-04 15:39:11 +00:00
{
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_NONE;
2005-02-04 15:39:11 +00:00
2005-09-18 10:18:35 +00:00
if (XP_LSP_TYPE(obj) == XP_LSP_OBJ_CONS)
2005-02-04 15:39:11 +00:00
return eval_cons (lsp, obj);
2005-09-18 10:18:35 +00:00
else if (XP_LSP_TYPE(obj) == XP_LSP_OBJ_SYMBOL) {
xp_lsp_assoc_t* assoc;
2005-02-04 15:39:11 +00:00
/*
if (obj == lsp->mem->lambda || obj == lsp->mem->macro) {
printf ("lambda or macro can't be used as a normal symbol\n");
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_BAD_SYMBOL;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
*/
2005-09-20 12:06:51 +00:00
assoc = xp_lsp_lookup(lsp->mem, obj);
if (assoc == XP_NULL || assoc->value == XP_NULL) {
2005-02-04 15:39:11 +00:00
if (lsp->opt_undef_symbol) {
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_UNDEF_SYMBOL;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
return lsp->mem->nil;
}
obj = assoc->value;
}
return obj;
}
2005-09-18 10:18:35 +00:00
static xp_lsp_obj_t* make_func (xp_lsp_t* lsp, xp_lsp_obj_t* cdr, int is_macro)
2005-02-04 15:39:11 +00:00
{
2005-09-19 16:13:18 +00:00
xp_lsp_obj_t* func, * formal, * body, * p;
2005-02-04 15:39:11 +00:00
if (cdr == lsp->mem->nil) {
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_TOO_FEW_ARGS;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
2005-09-18 10:18:35 +00:00
if (XP_LSP_TYPE(cdr) != XP_LSP_OBJ_CONS) {
lsp->errnum = XP_LSP_ERR_BAD_ARG;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
2005-09-18 10:18:35 +00:00
formal = XP_LSP_CAR(cdr);
body = XP_LSP_CDR(cdr);
2005-02-04 15:39:11 +00:00
if (body == lsp->mem->nil) {
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_EMPTY_BODY;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
2005-09-20 08:05:32 +00:00
// TODO: more lambda expression syntax checks required???.
/* check if the lambda express has non-nil value
* at the terminating cdr */
2005-09-19 16:13:18 +00:00
for (p = body; XP_LSP_TYPE(p) == XP_LSP_OBJ_CONS; p = XP_LSP_CDR(p));
if (p != lsp->mem->nil) {
2005-09-20 08:05:32 +00:00
/* like in (lambda (x) (+ x 10) . 4) */
2005-09-19 16:13:18 +00:00
lsp->errnum = XP_LSP_ERR_BAD_ARG;
return XP_NULL;
}
2005-02-04 15:39:11 +00:00
func = (is_macro)?
2005-09-18 10:18:35 +00:00
xp_lsp_make_macro (lsp->mem, formal, body):
xp_lsp_make_func (lsp->mem, formal, body);
2005-02-04 15:39:11 +00:00
if (func == XP_NULL) {
2005-09-24 08:16:02 +00:00
lsp->errnum = XP_LSP_ERR_MEMORY;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
return func;
}
2005-09-18 10:18:35 +00:00
static xp_lsp_obj_t* eval_cons (xp_lsp_t* lsp, xp_lsp_obj_t* cons)
2005-02-04 15:39:11 +00:00
{
2005-09-18 10:18:35 +00:00
xp_lsp_obj_t* car, * cdr;
2005-02-04 15:39:11 +00:00
2005-09-18 10:18:35 +00:00
xp_assert (XP_LSP_TYPE(cons) == XP_LSP_OBJ_CONS);
2005-02-04 15:39:11 +00:00
2005-09-18 10:18:35 +00:00
car = XP_LSP_CAR(cons);
cdr = XP_LSP_CDR(cons);
2005-02-04 15:39:11 +00:00
if (car == lsp->mem->lambda) {
return make_func (lsp, cdr, 0);
}
else if (car == lsp->mem->macro) {
return make_func (lsp, cdr, 1);
}
2005-09-18 10:18:35 +00:00
else if (XP_LSP_TYPE(car) == XP_LSP_OBJ_SYMBOL) {
xp_lsp_assoc_t* assoc;
2005-02-04 15:39:11 +00:00
2005-09-20 09:17:06 +00:00
if ((assoc = xp_lsp_lookup(lsp->mem, car)) != XP_NULL) {
//xp_lsp_obj_t* func = assoc->value;
xp_lsp_obj_t* func = assoc->func;
if (func == XP_NULL) {
/* the symbol's function definition is void */
lsp->errnum = XP_LSP_ERR_UNDEF_FUNC;
return XP_NULL;
}
2005-09-18 10:18:35 +00:00
if (XP_LSP_TYPE(func) == XP_LSP_OBJ_FUNC ||
XP_LSP_TYPE(func) == XP_LSP_OBJ_MACRO) {
2005-02-04 15:39:11 +00:00
return apply (lsp, func, cdr);
}
2005-09-18 10:18:35 +00:00
else if (XP_LSP_TYPE(func) == XP_LSP_OBJ_PRIM) {
2005-09-20 12:06:51 +00:00
/* primitive function */
return XP_LSP_PRIM(func) (lsp, cdr);
2005-02-04 15:39:11 +00:00
}
else {
2005-09-20 08:05:32 +00:00
//TODO: emit the name for debugging
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_UNDEF_FUNC;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
}
else {
//TODO: better error handling.
2005-09-20 08:05:32 +00:00
//TODO: emit the name for debugging
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_UNDEF_FUNC;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
}
2005-09-18 10:18:35 +00:00
else if (XP_LSP_TYPE(car) == XP_LSP_OBJ_FUNC ||
XP_LSP_TYPE(car) == XP_LSP_OBJ_MACRO) {
2005-02-04 15:39:11 +00:00
return apply (lsp, car, cdr);
}
2005-09-18 10:18:35 +00:00
else if (XP_LSP_TYPE(car) == XP_LSP_OBJ_CONS) {
if (XP_LSP_CAR(car) == lsp->mem->lambda) {
xp_lsp_obj_t* func = make_func (lsp, XP_LSP_CDR(car), 0);
2005-02-04 15:39:11 +00:00
if (func == XP_NULL) return XP_NULL;
return apply (lsp, func, cdr);
}
2005-09-18 10:18:35 +00:00
else if (XP_LSP_CAR(car) == lsp->mem->macro) {
xp_lsp_obj_t* func = make_func (lsp, XP_LSP_CDR(car), 1);
2005-02-04 15:39:11 +00:00
if (func == XP_NULL) return XP_NULL;
return apply (lsp, func, cdr);
}
}
2005-09-20 08:05:32 +00:00
//TODO: emit the name for debugging
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_BAD_FUNC;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
2005-09-20 09:17:06 +00:00
static xp_lsp_obj_t* apply (
xp_lsp_t* lsp, xp_lsp_obj_t* func, xp_lsp_obj_t* actual)
2005-02-04 15:39:11 +00:00
{
2005-09-18 10:18:35 +00:00
xp_lsp_frame_t* frame;
xp_lsp_obj_t* formal;
xp_lsp_obj_t* body;
xp_lsp_obj_t* value;
xp_lsp_mem_t* mem;
2005-02-04 15:39:11 +00:00
2005-02-05 05:18:20 +00:00
xp_assert (
2005-09-18 10:18:35 +00:00
XP_LSP_TYPE(func) == XP_LSP_OBJ_FUNC ||
XP_LSP_TYPE(func) == XP_LSP_OBJ_MACRO);
2005-02-04 15:39:11 +00:00
2005-09-18 10:18:35 +00:00
xp_assert (XP_LSP_TYPE(XP_LSP_CDR(func)) == XP_LSP_OBJ_CONS);
2005-02-04 15:39:11 +00:00
mem = lsp->mem;
2005-09-18 10:18:35 +00:00
if (XP_LSP_TYPE(func) == XP_LSP_OBJ_MACRO) {
formal = XP_LSP_MFORMAL (func);
body = XP_LSP_MBODY (func);
2005-02-04 15:39:11 +00:00
}
else {
2005-09-18 10:18:35 +00:00
formal = XP_LSP_FFORMAL (func);
body = XP_LSP_FBODY (func);
2005-02-04 15:39:11 +00:00
}
// make a new frame.
2005-09-18 10:18:35 +00:00
frame = xp_lsp_frame_new ();
2005-02-04 15:39:11 +00:00
if (frame == XP_NULL) {
2005-09-24 08:16:02 +00:00
lsp->errnum = XP_LSP_ERR_MEMORY;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
// attach it to the brooding frame list to
// make them not to be garbage collected.
frame->link = mem->brooding_frame;
mem->brooding_frame = frame;
// evaluate arguments and push them into the frame.
while (formal != mem->nil) {
if (actual == mem->nil) {
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_TOO_FEW_ARGS;
2005-02-04 15:39:11 +00:00
mem->brooding_frame = frame->link;
2005-09-18 10:18:35 +00:00
xp_lsp_frame_free (frame);
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
2005-09-18 10:18:35 +00:00
value = XP_LSP_CAR(actual);
if (XP_LSP_TYPE(func) != XP_LSP_OBJ_MACRO) {
2005-02-04 15:39:11 +00:00
// macro doesn't evaluate actual arguments.
2005-09-18 10:18:35 +00:00
value = xp_lsp_eval (lsp, value);
2005-02-04 15:39:11 +00:00
if (value == XP_NULL) {
mem->brooding_frame = frame->link;
2005-09-18 10:18:35 +00:00
xp_lsp_frame_free (frame);
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
}
2005-09-20 09:17:06 +00:00
if (xp_lsp_frame_lookup (
frame, XP_LSP_CAR(formal)) != XP_NULL) {
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_DUP_FORMAL;
2005-02-04 15:39:11 +00:00
mem->brooding_frame = frame->link;
2005-09-18 10:18:35 +00:00
xp_lsp_frame_free (frame);
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
2005-09-20 08:05:32 +00:00
2005-09-20 09:17:06 +00:00
if (xp_lsp_frame_insert_value (
frame, XP_LSP_CAR(formal), value) == XP_NULL) {
2005-09-24 08:16:02 +00:00
lsp->errnum = XP_LSP_ERR_MEMORY;
2005-02-04 15:39:11 +00:00
mem->brooding_frame = frame->link;
2005-09-18 10:18:35 +00:00
xp_lsp_frame_free (frame);
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
2005-09-18 10:18:35 +00:00
actual = XP_LSP_CDR(actual);
formal = XP_LSP_CDR(formal);
2005-02-04 15:39:11 +00:00
}
2005-09-18 10:18:35 +00:00
if (XP_LSP_TYPE(actual) == XP_LSP_OBJ_CONS) {
lsp->errnum = XP_LSP_ERR_TOO_MANY_ARGS;
2005-02-04 15:39:11 +00:00
mem->brooding_frame = frame->link;
2005-09-18 10:18:35 +00:00
xp_lsp_frame_free (frame);
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
else if (actual != mem->nil) {
2005-09-18 10:18:35 +00:00
lsp->errnum = XP_LSP_ERR_BAD_ARG;
2005-02-04 15:39:11 +00:00
mem->brooding_frame = frame->link;
2005-09-18 10:18:35 +00:00
xp_lsp_frame_free (frame);
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
// push the frame
mem->brooding_frame = frame->link;
frame->link = mem->frame;
mem->frame = frame;
// do the evaluation of the body
value = mem->nil;
while (body != mem->nil) {
2005-09-18 10:18:35 +00:00
value = xp_lsp_eval(lsp, XP_LSP_CAR(body));
2005-02-04 15:39:11 +00:00
if (value == XP_NULL) {
mem->frame = frame->link;
2005-09-18 10:18:35 +00:00
xp_lsp_frame_free (frame);
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
2005-09-18 10:18:35 +00:00
body = XP_LSP_CDR(body);
2005-02-04 15:39:11 +00:00
}
// pop the frame.
mem->frame = frame->link;
// destroy the frame.
2005-09-18 10:18:35 +00:00
xp_lsp_frame_free (frame);
2005-02-04 15:39:11 +00:00
2005-09-18 10:18:35 +00:00
//if (XP_LSP_CAR(func) == mem->macro) {
if (XP_LSP_TYPE(func) == XP_LSP_OBJ_MACRO) {
value = xp_lsp_eval(lsp, value);
2005-02-04 15:39:11 +00:00
if (value == XP_NULL) return XP_NULL;
}
return value;
}