2005-02-04 15:39:11 +00:00
|
|
|
/*
|
2005-09-18 10:18:35 +00:00
|
|
|
* $Id: eval.c,v 1.8 2005-09-18 10:18:35 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-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);
|
|
|
|
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-18 10:18:35 +00:00
|
|
|
if ((assoc = xp_lsp_lookup (lsp->mem, obj)) == 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
|
|
|
{
|
|
|
|
// TODO: lambda expression syntax check.
|
2005-09-18 10:18:35 +00:00
|
|
|
xp_lsp_obj_t* func, * formal, * body;
|
2005-02-04 15:39:11 +00:00
|
|
|
|
2005-09-18 10:18:35 +00:00
|
|
|
xp_printf (XP_TEXT("about to create a function or a macro ....\n"));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-18 10:18:35 +00:00
|
|
|
lsp->errnum = XP_LSP_ERR_MEM;
|
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-18 10:18:35 +00:00
|
|
|
if ((assoc = xp_lsp_lookup (lsp->mem, car)) != XP_NULL) {
|
|
|
|
xp_lsp_obj_t* func = assoc->value;
|
|
|
|
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-02-04 15:39:11 +00:00
|
|
|
// primitive function
|
2005-09-18 10:18:35 +00:00
|
|
|
return XP_LSP_PIMPL(func) (lsp, cdr);
|
2005-02-04 15:39:11 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf ("undefined function: ");
|
2005-09-18 10:18:35 +00:00
|
|
|
xp_lsp_print (lsp, car);
|
2005-02-04 15:39:11 +00:00
|
|
|
printf ("\n");
|
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.
|
|
|
|
printf ("undefined function: ");
|
2005-09-18 10:18:35 +00:00
|
|
|
xp_lsp_print (lsp, car);
|
2005-02-04 15:39:11 +00:00
|
|
|
printf ("\n");
|
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-02-04 16:23:34 +00:00
|
|
|
xp_printf (XP_TEXT("bad function: "));
|
2005-09-18 10:18:35 +00:00
|
|
|
xp_lsp_print (lsp, car);
|
2005-02-04 16:23:34 +00:00
|
|
|
xp_printf (XP_TEXT("\n"));
|
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-18 10:18:35 +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-18 10:18:35 +00:00
|
|
|
lsp->errnum = XP_LSP_ERR_MEM;
|
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-18 10:18:35 +00:00
|
|
|
if (xp_lsp_frame_lookup (frame, XP_LSP_CAR(formal)) != XP_NULL) {
|
|
|
|
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-18 10:18:35 +00:00
|
|
|
if (xp_lsp_frame_insert (frame, XP_LSP_CAR(formal), value) == XP_NULL) {
|
|
|
|
lsp->errnum = XP_LSP_ERR_MEM;
|
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;
|
|
|
|
}
|
|
|
|
|