2005-09-18 10:23:19 +00:00
|
|
|
/*
|
2006-10-26 08:17:38 +00:00
|
|
|
* $Id: prim.c,v 1.12 2006-10-26 08:17:37 bacon Exp $
|
2005-09-18 10:23:19 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-26 08:17:38 +00:00
|
|
|
#include <ase/lsp/lsp_i.h>
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
static int __add_prim (ase_lsp_mem_t* mem,
|
|
|
|
const ase_char_t* name, ase_size_t len, ase_lsp_prim_t prim);
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
int ase_lsp_add_prim (
|
|
|
|
ase_lsp_t* lsp, const ase_char_t* name, ase_lsp_prim_t prim)
|
2005-09-20 11:19:15 +00:00
|
|
|
{
|
2006-10-26 08:17:38 +00:00
|
|
|
return __add_prim (lsp->mem, name, ase_lsp_strlen(name), prim);
|
2005-09-20 11:19:15 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
int ase_lsp_remove_prim (ase_lsp_t* lsp, const ase_char_t* name)
|
2005-09-20 11:19:15 +00:00
|
|
|
{
|
|
|
|
// TODO:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
static int __add_prim (ase_lsp_mem_t* mem,
|
|
|
|
const ase_char_t* name, ase_size_t len, ase_lsp_prim_t prim)
|
2005-09-20 11:19:15 +00:00
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* n, * p;
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
n = ase_lsp_makesymobj (mem, name, len);
|
2006-10-24 04:22:40 +00:00
|
|
|
if (n == ASE_NULL) return -1;
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_lockobj (mem->lsp, n);
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
p = ase_lsp_makeprim (mem, prim);
|
2006-10-24 04:22:40 +00:00
|
|
|
if (p == ASE_NULL) return -1;
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_unlockobj (mem->lsp, n);
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
if (ase_lsp_setfunc(mem, n, p) == ASE_NULL) return -1;
|
2005-09-20 11:19:15 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_abort (ase_lsp_t* lsp, ase_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 0, 0);
|
|
|
|
lsp->errnum = ASE_LSP_ERR_ABORT;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_eval (ase_lsp_t* lsp, ase_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, 1);
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_assert (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(args));
|
|
|
|
if (tmp == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ase_lsp_eval (lsp, tmp);
|
|
|
|
if (tmp == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_gc (ase_lsp_t* lsp, ase_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 0, 0);
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_collectgarbage (lsp->mem);
|
2005-09-18 10:23:19 +00:00
|
|
|
return lsp->mem->nil;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_cond (ase_lsp_t* lsp, ase_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* (cond
|
|
|
|
* (condition1 result1)
|
|
|
|
* (consition2 result2)
|
|
|
|
* ...
|
|
|
|
* (t resultN))
|
|
|
|
*/
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* tmp, * ret;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 0, ASE_LSP_PRIM_MAX_ARG_COUNT);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
while (ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS) {
|
|
|
|
if (ASE_LSP_TYPE(ASE_LSP_CAR(args)) != ASE_LSP_OBJ_CONS) {
|
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(ASE_LSP_CAR(args)));
|
|
|
|
if (tmp == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
|
|
|
if (tmp != lsp->mem->nil) {
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ASE_LSP_CDR(ASE_LSP_CAR(args));
|
2005-09-18 10:23:19 +00:00
|
|
|
ret = lsp->mem->nil;
|
2006-10-24 04:22:40 +00:00
|
|
|
while (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_CONS) {
|
|
|
|
ret = ase_lsp_eval (lsp, ASE_LSP_CAR(tmp));
|
|
|
|
if (ret == ASE_NULL) return ASE_NULL;
|
|
|
|
tmp = ASE_LSP_CDR(tmp);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
if (tmp != lsp->mem->nil) {
|
2006-10-24 04:22:40 +00:00
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
args = ASE_LSP_CDR(args);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lsp->mem->nil;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_if (ase_lsp_t* lsp, ase_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 2, ASE_LSP_PRIM_MAX_ARG_COUNT);
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_assert (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(args));
|
|
|
|
if (tmp == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
|
|
|
if (tmp != lsp->mem->nil) {
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(ASE_LSP_CDR(args)));
|
|
|
|
if (tmp == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
else {
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* res = lsp->mem->nil;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ASE_LSP_CDR(ASE_LSP_CDR(args));
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
while (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_CONS) {
|
|
|
|
res = ase_lsp_eval (lsp, ASE_LSP_CAR(tmp));
|
|
|
|
if (res == ASE_NULL) return ASE_NULL;
|
|
|
|
tmp = ASE_LSP_CDR(tmp);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
if (tmp != lsp->mem->nil) {
|
2006-10-24 04:22:40 +00:00
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_while (ase_lsp_t* lsp, ase_lsp_obj_t* args)
|
2005-09-18 10:23:19 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* (setq a 1)
|
|
|
|
* (while (< a 100) (setq a (+ a 1)))
|
|
|
|
*/
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, ASE_LSP_PRIM_MAX_ARG_COUNT);
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_assert (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(args));
|
|
|
|
if (tmp == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
if (tmp == lsp->mem->nil) break;
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ASE_LSP_CDR(args);
|
|
|
|
while (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_CONS) {
|
|
|
|
if (ase_lsp_eval(lsp, ASE_LSP_CAR(tmp)) == ASE_NULL)
|
|
|
|
return ASE_NULL;
|
|
|
|
tmp = ASE_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-24 04:22:40 +00:00
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lsp->mem->nil;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_car (ase_lsp_t* lsp, ase_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-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, 1);
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_assert (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(args));
|
|
|
|
if (tmp == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
if (tmp == lsp->mem->nil) return lsp->mem->nil;
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
if (ASE_LSP_TYPE(tmp) != ASE_LSP_OBJ_CONS) {
|
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
return ASE_LSP_CAR(tmp);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_cdr (ase_lsp_t* lsp, ase_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-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* tmp;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, 1);
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_assert (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(args));
|
|
|
|
if (tmp == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
if (tmp == lsp->mem->nil) return lsp->mem->nil;
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
if (ASE_LSP_TYPE(tmp) != ASE_LSP_OBJ_CONS) {
|
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
return ASE_LSP_CDR(tmp);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_cons (ase_lsp_t* lsp, ase_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-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* car, * cdr, * cons;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 2, 2);
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_assert (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
car = ase_lsp_eval (lsp, ASE_LSP_CAR(args));
|
|
|
|
if (car == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
cdr = ase_lsp_eval (lsp, ASE_LSP_CAR(ASE_LSP_CDR(args)));
|
|
|
|
if (cdr == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
cons = ase_lsp_makecons (lsp->mem, car, cdr);
|
|
|
|
if (cons == ASE_NULL)
|
|
|
|
{
|
|
|
|
lsp->errnum = ASE_LSP_ENOMEM;
|
2006-10-24 04:22:40 +00:00
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cons;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_set (ase_lsp_t* lsp, ase_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-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* p1, * p2;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 2, 2);
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_assert (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
p1 = ase_lsp_eval (lsp, ASE_LSP_CAR(args));
|
|
|
|
if (p1 == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
if (ASE_LSP_TYPE(p1) != ASE_LSP_OBJ_SYM) {
|
2006-10-24 04:22:40 +00:00
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
p2 = ase_lsp_eval (lsp, ASE_LSP_CAR(ASE_LSP_CDR(args)));
|
|
|
|
if (p2 == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
if (ase_lsp_setvalue (lsp->mem, p1, p2) == ASE_NULL) {
|
2006-10-26 08:17:38 +00:00
|
|
|
lsp->errnum = ASE_LSP_ENOMEM;
|
2006-10-24 04:22:40 +00:00
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p2;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_setq (ase_lsp_t* lsp, ase_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-24 04:22:40 +00:00
|
|
|
ase_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-26 08:17:38 +00:00
|
|
|
ase_lsp_assert (lsp, ASE_LSP_TYPE(p) == ASE_LSP_OBJ_CONS);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
p1 = ASE_LSP_CAR(p);
|
2006-10-25 13:42:31 +00:00
|
|
|
if (ASE_LSP_TYPE(p1) != ASE_LSP_OBJ_SYM) {
|
2006-10-24 04:22:40 +00:00
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
if (ASE_LSP_TYPE(ASE_LSP_CDR(p)) != ASE_LSP_OBJ_CONS) {
|
|
|
|
lsp->errnum = ASE_LSP_ERR_TOO_FEW_ARGS;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
p2 = ase_lsp_eval (lsp, ASE_LSP_CAR(ASE_LSP_CDR(p)));
|
|
|
|
if (p2 == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
if (ase_lsp_setvalue (lsp->mem, p1, p2) == ASE_NULL) {
|
2006-10-26 08:17:38 +00:00
|
|
|
lsp->errnum = ASE_LSP_ENOMEM;
|
2006-10-24 04:22:40 +00:00
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
p = ASE_LSP_CDR(ASE_LSP_CDR(p));
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p2;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_quote (ase_lsp_t* lsp, ase_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-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, 1);
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_lsp_assert (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
|
2006-10-24 04:22:40 +00:00
|
|
|
return ASE_LSP_CAR(args);
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_defun (ase_lsp_t* lsp, ase_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-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* name, * fun;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 3, ASE_LSP_PRIM_MAX_ARG_COUNT);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
name = ASE_LSP_CAR(args);
|
2006-10-25 13:42:31 +00:00
|
|
|
if (ASE_LSP_TYPE(name) != ASE_LSP_OBJ_SYM)
|
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
fun = ase_lsp_makefunc (lsp->mem,
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_CAR(ASE_LSP_CDR(args)), ASE_LSP_CDR(ASE_LSP_CDR(args)));
|
|
|
|
if (fun == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
if (ase_lsp_setfunc (lsp->mem, ASE_LSP_CAR(args), fun) == ASE_NULL)
|
|
|
|
{
|
2006-10-26 08:17:38 +00:00
|
|
|
lsp->errnum = ASE_LSP_ENOMEM;
|
2006-10-24 04:22:40 +00:00
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* ase_lsp_prim_demac (ase_lsp_t* lsp, ase_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-24 04:22:40 +00:00
|
|
|
ase_lsp_obj_t* name, * mac;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 3, ASE_LSP_PRIM_MAX_ARG_COUNT);
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
name = ASE_LSP_CAR(args);
|
2006-10-25 13:42:31 +00:00
|
|
|
if (ASE_LSP_TYPE(name) != ASE_LSP_OBJ_SYM)
|
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
lsp->errnum = ASE_LSP_ERR_BAD_ARG;
|
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
mac = ase_lsp_makemacro (lsp->mem,
|
2006-10-24 04:22:40 +00:00
|
|
|
ASE_LSP_CAR(ASE_LSP_CDR(args)), ASE_LSP_CDR(ASE_LSP_CDR(args)));
|
|
|
|
if (mac == ASE_NULL) return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
|
2006-10-25 13:42:31 +00:00
|
|
|
if (ase_lsp_setfunc (lsp->mem, ASE_LSP_CAR(args), mac) == ASE_NULL)
|
|
|
|
{
|
2006-10-26 08:17:38 +00:00
|
|
|
lsp->errnum = ASE_LSP_ENOMEM;
|
2006-10-24 04:22:40 +00:00
|
|
|
return ASE_NULL;
|
2005-09-18 10:23:19 +00:00
|
|
|
}
|
|
|
|
return mac;
|
|
|
|
}
|