2005-09-19 12:04:00 +00:00
|
|
|
/*
|
2006-10-22 13:10:46 +00:00
|
|
|
* $Id: prim_let.c,v 1.4 2006-10-22 13:10:46 bacon Exp $
|
2005-09-19 12:04:00 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
#include <sse/lsp/prim.h>
|
2005-09-19 12:04:00 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
static sse_lsp_obj_t* __prim_let (
|
|
|
|
sse_lsp_t* lsp, sse_lsp_obj_t* args, int sequential)
|
2005-09-19 12:04:00 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_t* frame;
|
|
|
|
sse_lsp_obj_t* assoc;
|
|
|
|
sse_lsp_obj_t* body;
|
|
|
|
sse_lsp_obj_t* value;
|
2005-09-19 12:04:00 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, SSE_LSP_PRIM_MAX_ARG_COUNT);
|
2005-09-19 12:04:00 +00:00
|
|
|
|
|
|
|
// create a new frame
|
2006-10-22 13:10:46 +00:00
|
|
|
frame = sse_lsp_frame_new ();
|
|
|
|
if (frame == SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_MEMORY;
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
|
|
|
//frame->link = lsp->mem->frame;
|
|
|
|
|
|
|
|
if (sequential) {
|
|
|
|
frame->link = lsp->mem->frame;
|
|
|
|
lsp->mem->frame = frame;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
frame->link = lsp->mem->brooding_frame;
|
|
|
|
lsp->mem->brooding_frame = frame;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
assoc = SSE_LSP_CAR(args);
|
2005-09-19 12:04:00 +00:00
|
|
|
|
|
|
|
//while (assoc != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
while (SSE_LSP_TYPE(assoc) == SSE_LSP_OBJ_CONS) {
|
|
|
|
sse_lsp_obj_t* ass = SSE_LSP_CAR(assoc);
|
|
|
|
if (SSE_LSP_TYPE(ass) == SSE_LSP_OBJ_CONS) {
|
|
|
|
sse_lsp_obj_t* n = SSE_LSP_CAR(ass);
|
|
|
|
sse_lsp_obj_t* v = SSE_LSP_CDR(ass);
|
|
|
|
|
|
|
|
if (SSE_LSP_TYPE(n) != SSE_LSP_OBJ_SYMBOL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG; // must be a symbol
|
2005-09-19 12:04:00 +00:00
|
|
|
if (sequential) lsp->mem->frame = frame->link;
|
|
|
|
else lsp->mem->brooding_frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (v != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
if (SSE_LSP_CDR(v) != lsp->mem->nil) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_TOO_MANY_ARGS; // must be a symbol
|
2005-09-19 12:04:00 +00:00
|
|
|
if (sequential) lsp->mem->frame = frame->link;
|
|
|
|
else lsp->mem->brooding_frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
2006-10-22 13:10:46 +00:00
|
|
|
if ((v = sse_lsp_eval(lsp, SSE_LSP_CAR(v))) == SSE_NULL) {
|
2005-09-19 12:04:00 +00:00
|
|
|
if (sequential) lsp->mem->frame = frame->link;
|
|
|
|
else lsp->mem->brooding_frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (sse_lsp_frame_lookup (frame, n) != SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_DUP_FORMAL;
|
2005-09-19 12:04:00 +00:00
|
|
|
if (sequential) lsp->mem->frame = frame->link;
|
|
|
|
else lsp->mem->brooding_frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
2006-10-22 13:10:46 +00:00
|
|
|
if (sse_lsp_frame_insert_value(frame, n, v) == SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_MEMORY;
|
2005-09-19 12:04:00 +00:00
|
|
|
if (sequential) lsp->mem->frame = frame->link;
|
|
|
|
else lsp->mem->brooding_frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-22 13:10:46 +00:00
|
|
|
else if (SSE_LSP_TYPE(ass) == SSE_LSP_OBJ_SYMBOL) {
|
|
|
|
if (sse_lsp_frame_lookup(frame, ass) != SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_DUP_FORMAL;
|
2005-09-19 12:04:00 +00:00
|
|
|
if (sequential) lsp->mem->frame = frame->link;
|
|
|
|
else lsp->mem->brooding_frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
2006-10-22 13:10:46 +00:00
|
|
|
if (sse_lsp_frame_insert_value(frame, ass, lsp->mem->nil) == SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_MEMORY;
|
2005-09-19 12:04:00 +00:00
|
|
|
if (sequential) lsp->mem->frame = frame->link;
|
|
|
|
else lsp->mem->brooding_frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2006-10-22 13:10:46 +00:00
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
2005-09-19 12:04:00 +00:00
|
|
|
if (sequential) lsp->mem->frame = frame->link;
|
|
|
|
else lsp->mem->brooding_frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
assoc = SSE_LSP_CDR(assoc);
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (assoc != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
2005-09-19 12:04:00 +00:00
|
|
|
if (sequential) lsp->mem->frame = frame->link;
|
|
|
|
else lsp->mem->brooding_frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// push the frame
|
|
|
|
if (!sequential) {
|
|
|
|
lsp->mem->brooding_frame = frame->link;
|
|
|
|
frame->link = lsp->mem->frame;
|
|
|
|
lsp->mem->frame = frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
// evaluate forms in the body
|
|
|
|
value = lsp->mem->nil;
|
2006-10-22 13:10:46 +00:00
|
|
|
body = SSE_LSP_CDR(args);
|
2005-09-19 12:04:00 +00:00
|
|
|
while (body != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
value = sse_lsp_eval (lsp, SSE_LSP_CAR(body));
|
|
|
|
if (value == SSE_NULL) {
|
2005-09-19 12:04:00 +00:00
|
|
|
lsp->mem->frame = frame->link;
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
|
|
|
return SSE_NULL;
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
2006-10-22 13:10:46 +00:00
|
|
|
body = SSE_LSP_CDR(body);
|
2005-09-19 12:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pop the frame
|
|
|
|
lsp->mem->frame = frame->link;
|
|
|
|
|
|
|
|
// destroy the frame
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_frame_free (frame);
|
2005-09-19 12:04:00 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_let (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-19 12:04:00 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* (defun x (x y)
|
|
|
|
* (let ((temp1 10) (temp2 20))
|
|
|
|
* (+ x y temp1 temp2)))
|
|
|
|
* (x 40 50)
|
|
|
|
* temp1
|
|
|
|
*/
|
|
|
|
return __prim_let (lsp, args, 0);
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_letx (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-19 12:04:00 +00:00
|
|
|
{
|
|
|
|
return __prim_let (lsp, args, 1);
|
|
|
|
}
|