2005-09-18 10:23:19 +00:00
|
|
|
/*
|
2006-10-22 13:10:46 +00:00
|
|
|
* $Id: prim.c,v 1.9 2006-10-22 13:10:46 bacon Exp $
|
2005-09-18 10:23:19 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
#include <sse/lsp/lsp.h>
|
|
|
|
#include <sse/lsp/mem.h>
|
|
|
|
#include <sse/lsp/prim.h>
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
#include <sse/bas/string.h>
|
|
|
|
#include <sse/bas/assert.h>
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
static int __add_prim (sse_lsp_mem_t* mem,
|
|
|
|
const sse_char_t* name, sse_size_t len, sse_lsp_prim_t prim);
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
int sse_lsp_add_prim (
|
|
|
|
sse_lsp_t* lsp, const sse_char_t* name, sse_lsp_prim_t prim)
|
2005-09-20 11:19:15 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
return __add_prim (lsp->mem, name, sse_strlen(name), prim);
|
2005-09-20 11:19:15 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
int sse_lsp_remove_prim (sse_lsp_t* lsp, const sse_char_t* name)
|
2005-09-20 11:19:15 +00:00
|
|
|
{
|
|
|
|
// TODO:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
static int __add_prim (sse_lsp_mem_t* mem,
|
|
|
|
const sse_char_t* name, sse_size_t len, sse_lsp_prim_t prim)
|
2005-09-20 11:19:15 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* n, * p;
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
n = sse_lsp_make_symbolx (mem, name, len);
|
|
|
|
if (n == SSE_NULL) return -1;
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_lock (n);
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
p = sse_lsp_make_prim (mem, prim);
|
|
|
|
if (p == SSE_NULL) return -1;
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_unlock (n);
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (sse_lsp_set_func(mem, n, p) == SSE_NULL) return -1;
|
2005-09-20 11:19:15 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_abort (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 0, 0);
|
|
|
|
lsp->errnum = SSE_LSP_ERR_ABORT;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_eval (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, 1);
|
|
|
|
sse_assert (SSE_LSP_TYPE(args) == SSE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(args));
|
|
|
|
if (tmp == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = sse_lsp_eval (lsp, tmp);
|
|
|
|
if (tmp == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_gc (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 0, 0);
|
|
|
|
sse_lsp_garbage_collect (lsp->mem);
|
2005-09-18 10:23:19 +00:00
|
|
|
return lsp->mem->nil;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_cond (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* (cond
|
|
|
|
* (condition1 result1)
|
|
|
|
* (consition2 result2)
|
|
|
|
* ...
|
|
|
|
* (t resultN))
|
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* tmp, * ret;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 0, SSE_LSP_PRIM_MAX_ARG_COUNT);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
while (SSE_LSP_TYPE(args) == SSE_LSP_OBJ_CONS) {
|
|
|
|
if (SSE_LSP_TYPE(SSE_LSP_CAR(args)) != SSE_LSP_OBJ_CONS) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(SSE_LSP_CAR(args)));
|
|
|
|
if (tmp == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
|
|
|
if (tmp != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = SSE_LSP_CDR(SSE_LSP_CAR(args));
|
2005-09-18 10:23:19 +00:00
|
|
|
ret = lsp->mem->nil;
|
2006-10-22 13:10:46 +00:00
|
|
|
while (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_CONS) {
|
|
|
|
ret = sse_lsp_eval (lsp, SSE_LSP_CAR(tmp));
|
|
|
|
if (ret == SSE_NULL) return SSE_NULL;
|
|
|
|
tmp = SSE_LSP_CDR(tmp);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
if (tmp != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
args = SSE_LSP_CDR(args);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lsp->mem->nil;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_if (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 2, SSE_LSP_PRIM_MAX_ARG_COUNT);
|
|
|
|
sse_assert (SSE_LSP_TYPE(args) == SSE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(args));
|
|
|
|
if (tmp == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
|
|
|
if (tmp != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(SSE_LSP_CDR(args)));
|
|
|
|
if (tmp == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
else {
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* res = lsp->mem->nil;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = SSE_LSP_CDR(SSE_LSP_CDR(args));
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
while (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_CONS) {
|
|
|
|
res = sse_lsp_eval (lsp, SSE_LSP_CAR(tmp));
|
|
|
|
if (res == SSE_NULL) return SSE_NULL;
|
|
|
|
tmp = SSE_LSP_CDR(tmp);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
if (tmp != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_while (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* (setq a 1)
|
|
|
|
* (while (< a 100) (setq a (+ a 1)))
|
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +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);
|
|
|
|
sse_assert (SSE_LSP_TYPE(args) == SSE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(args));
|
|
|
|
if (tmp == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
if (tmp == lsp->mem->nil) break;
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = SSE_LSP_CDR(args);
|
|
|
|
while (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_CONS) {
|
|
|
|
if (sse_lsp_eval(lsp, SSE_LSP_CAR(tmp)) == SSE_NULL)
|
|
|
|
return SSE_NULL;
|
|
|
|
tmp = SSE_LSP_CDR(tmp);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
2005-09-20 12:06:51 +00:00
|
|
|
|
2005-09-18 10:23:19 +00:00
|
|
|
if (tmp != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lsp->mem->nil;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_car (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2005-09-19 12:04:00 +00:00
|
|
|
/*
|
|
|
|
* (car '(10 20 30))
|
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, 1);
|
|
|
|
sse_assert (SSE_LSP_TYPE(args) == SSE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(args));
|
|
|
|
if (tmp == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
if (tmp == lsp->mem->nil) return lsp->mem->nil;
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (SSE_LSP_TYPE(tmp) != SSE_LSP_OBJ_CONS) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
return SSE_LSP_CAR(tmp);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_cdr (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2005-09-19 12:04:00 +00:00
|
|
|
/*
|
|
|
|
* (cdr '(10 20 30))
|
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, 1);
|
|
|
|
sse_assert (SSE_LSP_TYPE(args) == SSE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(args));
|
|
|
|
if (tmp == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
if (tmp == lsp->mem->nil) return lsp->mem->nil;
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (SSE_LSP_TYPE(tmp) != SSE_LSP_OBJ_CONS) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
return SSE_LSP_CDR(tmp);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_cons (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2005-09-19 12:04:00 +00:00
|
|
|
/*
|
|
|
|
* (cons 10 20)
|
|
|
|
* (cons '(10 20) 30)
|
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* car, * cdr, * cons;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 2, 2);
|
|
|
|
sse_assert (SSE_LSP_TYPE(args) == SSE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
car = sse_lsp_eval (lsp, SSE_LSP_CAR(args));
|
|
|
|
if (car == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
cdr = sse_lsp_eval (lsp, SSE_LSP_CAR(SSE_LSP_CDR(args)));
|
|
|
|
if (cdr == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
cons = sse_lsp_make_cons (lsp->mem, car, cdr);
|
|
|
|
if (cons == SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_MEMORY;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cons;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_set (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2005-09-19 12:04:00 +00:00
|
|
|
/*
|
|
|
|
* (set 'flowers 'rose)
|
|
|
|
* (set flowers 20)
|
|
|
|
* (rose)
|
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* p1, * p2;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 2, 2);
|
|
|
|
sse_assert (SSE_LSP_TYPE(args) == SSE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
p1 = sse_lsp_eval (lsp, SSE_LSP_CAR(args));
|
|
|
|
if (p1 == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (SSE_LSP_TYPE(p1) != SSE_LSP_OBJ_SYMBOL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
p2 = sse_lsp_eval (lsp, SSE_LSP_CAR(SSE_LSP_CDR(args)));
|
|
|
|
if (p2 == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (sse_lsp_set_value (lsp->mem, p1, p2) == SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_MEMORY;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p2;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_setq (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2005-09-19 12:04:00 +00:00
|
|
|
/*
|
|
|
|
* (setq x 10)
|
|
|
|
* (setq x "stirng")
|
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* p = args, * p1, * p2 = lsp->mem->nil;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
|
|
|
while (p != lsp->mem->nil) {
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_assert (SSE_LSP_TYPE(p) == SSE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
p1 = SSE_LSP_CAR(p);
|
|
|
|
if (SSE_LSP_TYPE(p1) != SSE_LSP_OBJ_SYMBOL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (SSE_LSP_TYPE(SSE_LSP_CDR(p)) != SSE_LSP_OBJ_CONS) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_TOO_FEW_ARGS;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
p2 = sse_lsp_eval (lsp, SSE_LSP_CAR(SSE_LSP_CDR(p)));
|
|
|
|
if (p2 == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (sse_lsp_set_value (lsp->mem, p1, p2) == SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_MEMORY;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
p = SSE_LSP_CDR(SSE_LSP_CDR(p));
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p2;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_quote (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2005-09-19 12:04:00 +00:00
|
|
|
/*
|
|
|
|
* (quote (10 20 30 50))
|
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, 1);
|
|
|
|
sse_assert (SSE_LSP_TYPE(args) == SSE_LSP_OBJ_CONS);
|
|
|
|
return SSE_LSP_CAR(args);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_defun (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
|
|
|
/*
|
2005-09-19 12:04:00 +00:00
|
|
|
* (defun x (a b) (+ a b 100))
|
|
|
|
* (x 40 50)
|
|
|
|
*
|
|
|
|
* (setq x (lambda (x y) (setq temp 10) (+ x y temp)))
|
|
|
|
* (x 40 50)
|
|
|
|
* temp
|
2005-09-18 10:23:19 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* name, * fun;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 3, SSE_LSP_PRIM_MAX_ARG_COUNT);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
name = SSE_LSP_CAR(args);
|
|
|
|
if (SSE_LSP_TYPE(name) != SSE_LSP_OBJ_SYMBOL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
fun = sse_lsp_make_func (lsp->mem,
|
|
|
|
SSE_LSP_CAR(SSE_LSP_CDR(args)), SSE_LSP_CDR(SSE_LSP_CDR(args)));
|
|
|
|
if (fun == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (sse_lsp_set_func (lsp->mem, SSE_LSP_CAR(args), fun) == SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_MEMORY;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* sse_lsp_prim_demac (sse_lsp_t* lsp, sse_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* (demac x (abc) x y z)
|
|
|
|
*(setq x (macro (abc) x y z))
|
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_obj_t* name, * mac;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
SSE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 3, SSE_LSP_PRIM_MAX_ARG_COUNT);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
name = SSE_LSP_CAR(args);
|
|
|
|
if (SSE_LSP_TYPE(name) != SSE_LSP_OBJ_SYMBOL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_BAD_ARG;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
mac = sse_lsp_make_macro (lsp->mem,
|
|
|
|
SSE_LSP_CAR(SSE_LSP_CDR(args)), SSE_LSP_CDR(SSE_LSP_CDR(args)));
|
|
|
|
if (mac == SSE_NULL) return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (sse_lsp_set_func (lsp->mem, SSE_LSP_CAR(args), mac) == SSE_NULL) {
|
|
|
|
lsp->errnum = SSE_LSP_ERR_MEMORY;
|
|
|
|
return SSE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
return mac;
|
|
|
|
}
|