qse/ase/lsp/prim.c

622 lines
13 KiB
C
Raw Normal View History

2005-09-18 10:23:19 +00:00
/*
2007-03-15 14:19:23 +00:00
* $Id: prim.c,v 1.25 2007-03-15 14:19:23 bacon Exp $
2007-02-03 10:52:36 +00:00
*
* {License}
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,
2006-10-29 13:00:39 +00:00
const ase_char_t* name, ase_size_t len,
ase_lsp_prim_t pimpl, ase_size_t min_args, ase_size_t max_args);
2005-09-20 11:19:15 +00:00
2006-10-29 13:00:39 +00:00
int ase_lsp_addprim (
ase_lsp_t* lsp, const ase_char_t* name, ase_size_t name_len,
ase_lsp_prim_t prim, ase_size_t min_args, ase_size_t max_args)
2005-09-20 11:19:15 +00:00
{
2006-10-29 13:00:39 +00:00
return __add_prim (lsp->mem, name, name_len, prim, min_args, max_args);
2005-09-20 11:19:15 +00:00
}
2006-10-29 13:00:39 +00:00
int ase_lsp_removeprim (ase_lsp_t* lsp, const ase_char_t* name)
2005-09-20 11:19:15 +00:00
{
2007-03-15 14:19:23 +00:00
/* TODO: */
2005-09-20 11:19:15 +00:00
return -1;
}
2006-10-24 04:22:40 +00:00
static int __add_prim (ase_lsp_mem_t* mem,
2006-10-29 13:00:39 +00:00
const ase_char_t* name, ase_size_t name_len,
ase_lsp_prim_t pimpl, ase_size_t min_args, ase_size_t max_args)
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-11-02 10:12:01 +00:00
n = ase_lsp_makesym (mem, name, name_len);
2006-10-24 04:22:40 +00:00
if (n == ASE_NULL) return -1;
2005-09-20 11:19:15 +00:00
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (mem->lsp, n) == ASE_NULL) return -1;
2005-09-20 11:19:15 +00:00
2006-10-29 13:00:39 +00:00
p = ase_lsp_makeprim (mem, pimpl, min_args, max_args);
2007-02-03 10:52:36 +00:00
if (p == ASE_NULL)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (mem->lsp);
2007-02-03 10:52:36 +00:00
return -1;
}
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (mem->lsp, p) == ASE_NULL)
{
ase_lsp_poptmp (mem->lsp);
return -1;
}
2007-02-03 10:52:36 +00:00
if (ase_lsp_setfunc(mem, n, p) == ASE_NULL)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (mem->lsp);
ase_lsp_poptmp (mem->lsp);
2007-02-03 10:52:36 +00:00
return -1;
}
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (mem->lsp);
ase_lsp_poptmp (mem->lsp);
2005-09-20 11:19:15 +00:00
return 0;
}
2006-10-29 13:40:33 +00:00
ase_lsp_obj_t* ase_lsp_prim_exit (ase_lsp_t* lsp, ase_lsp_obj_t* args)
2005-09-18 10:23:19 +00:00
{
2007-02-03 10:52:36 +00:00
lsp->errnum = ASE_LSP_EEXIT;
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
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
{
2007-02-03 10:52:36 +00:00
ase_lsp_obj_t* tmp1, * tmp2;
2005-09-18 10:23:19 +00:00
2007-03-07 12:14:29 +00:00
ASE_ASSERT (ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
2005-09-18 10:23:19 +00:00
2007-02-03 10:52:36 +00:00
tmp1 = ase_lsp_eval (lsp, ASE_LSP_CAR(args));
if (tmp1 == ASE_NULL) return ASE_NULL;
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (lsp, tmp1) == ASE_NULL) return ASE_NULL;
2005-09-18 10:23:19 +00:00
2007-02-03 10:52:36 +00:00
tmp2 = ase_lsp_eval (lsp, tmp1);
if (tmp2 == ASE_NULL)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp);
2007-02-03 10:52:36 +00:00
return ASE_NULL;
}
2005-09-18 10:23:19 +00:00
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp);
2007-02-06 10:57:01 +00:00
return tmp2;
2005-09-18 10:23:19 +00:00
}
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
{
2007-02-10 13:52:41 +00:00
ase_lsp_gc (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-29 13:00:39 +00:00
while (ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS)
{
if (ASE_LSP_TYPE(ASE_LSP_CAR(args)) != ASE_LSP_OBJ_CONS)
{
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
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
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
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (lsp, tmp) == ASE_NULL) return ASE_NULL;
2006-10-29 13:00:39 +00:00
if (tmp != lsp->mem->nil)
{
2007-02-10 13:52:41 +00:00
int f = 0;
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;
2007-02-10 13:52:41 +00:00
2006-10-29 13:00:39 +00:00
while (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_CONS)
{
2006-10-24 04:22:40 +00:00
ret = ase_lsp_eval (lsp, ASE_LSP_CAR(tmp));
2007-02-10 13:52:41 +00:00
if (ret == ASE_NULL)
{
if (!f) ase_lsp_poptmp (lsp); /* ret */
ase_lsp_poptmp (lsp); /* tmp */
return ASE_NULL;
}
if (!f) ase_lsp_poptmp (lsp); /* ret */
if (ase_lsp_pushtmp (lsp, ret) == ASE_NULL)
{
ase_lsp_poptmp (lsp); /* tmp */
return ASE_NULL;
}
f = 1;
2006-10-24 04:22:40 +00:00
tmp = ASE_LSP_CDR(tmp);
2005-09-18 10:23:19 +00:00
}
2006-10-29 13:00:39 +00:00
if (tmp != lsp->mem->nil)
{
2007-02-10 13:52:41 +00:00
if (!f) ase_lsp_poptmp (lsp); /* ret */
ase_lsp_poptmp (lsp); /* tmp */
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 10:23:19 +00:00
}
2007-02-10 13:52:41 +00:00
if (!f) ase_lsp_poptmp (lsp); /* ret */
ase_lsp_poptmp (lsp); /* tmp */
2005-09-18 10:23:19 +00:00
return ret;
}
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp); /* tmp */
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
2007-03-07 12:14:29 +00:00
ASE_ASSERT (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
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (lsp, tmp) == ASE_NULL) return ASE_NULL;
2006-10-29 13:00:39 +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)));
2007-02-10 13:52:41 +00:00
if (tmp == ASE_NULL)
{
ase_lsp_poptmp (lsp); /* tmp */
return ASE_NULL;
}
ase_lsp_poptmp (lsp); /* tmp */
2005-09-18 10:23:19 +00:00
return tmp;
}
2006-10-29 13:00:39 +00:00
else
{
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* res = lsp->mem->nil;
2007-02-10 13:52:41 +00:00
int f = 0;
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-29 13:00:39 +00:00
while (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_CONS)
{
2006-10-24 04:22:40 +00:00
res = ase_lsp_eval (lsp, ASE_LSP_CAR(tmp));
2007-02-10 13:52:41 +00:00
if (res == ASE_NULL)
{
if (!f) ase_lsp_poptmp (lsp); /* res */
ase_lsp_poptmp (lsp); /* tmp */
return ASE_NULL;
}
if (!f) ase_lsp_poptmp (lsp); /* res */
if (ase_lsp_pushtmp (lsp, res) == ASE_NULL)
{
ase_lsp_poptmp (lsp); /* tmp */
return ASE_NULL;
}
f = 1;
2006-10-24 04:22:40 +00:00
tmp = ASE_LSP_CDR(tmp);
2005-09-18 10:23:19 +00:00
}
2007-02-10 13:52:41 +00:00
2006-10-29 13:00:39 +00:00
if (tmp != lsp->mem->nil)
{
2007-02-10 13:52:41 +00:00
if (!f) ase_lsp_poptmp (lsp); /* ret */
ase_lsp_poptmp (lsp); /* tmp */
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 10:23:19 +00:00
}
2007-02-10 13:52:41 +00:00
if (!f) ase_lsp_poptmp (lsp); /* ret */
ase_lsp_poptmp (lsp); /* tmp */
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
2007-03-07 12:14:29 +00:00
ASE_ASSERT (ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
2005-09-18 10:23:19 +00:00
2006-10-29 13:40:33 +00:00
while (1)
{
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;
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (lsp, tmp) == ASE_NULL) return ASE_NULL;
2006-10-24 04:22:40 +00:00
tmp = ASE_LSP_CDR(args);
2006-10-29 13:00:39 +00:00
while (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_CONS)
{
2007-02-10 13:52:41 +00:00
if (ase_lsp_eval(lsp, ASE_LSP_CAR(tmp)) == ASE_NULL)
{
ase_lsp_poptmp (lsp); /* tmp */
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2007-02-10 13:52:41 +00:00
}
2006-10-24 04:22:40 +00:00
tmp = ASE_LSP_CDR(tmp);
2005-09-18 10:23:19 +00:00
}
2005-09-20 12:06:51 +00:00
2006-10-29 13:00:39 +00:00
if (tmp != lsp->mem->nil)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp); /* tmp */
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 10:23:19 +00:00
}
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp); /* tmp */
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
2007-03-07 12:14:29 +00:00
ASE_ASSERT (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-29 13:00:39 +00:00
if (ASE_LSP_TYPE(tmp) != ASE_LSP_OBJ_CONS)
{
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
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
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
2007-03-07 12:14:29 +00:00
ASE_ASSERT (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-29 13:00:39 +00:00
if (ASE_LSP_TYPE(tmp) != ASE_LSP_OBJ_CONS)
{
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
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
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
2007-03-07 12:14:29 +00:00
ASE_ASSERT (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
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (lsp, car) == ASE_NULL) return ASE_NULL;
2006-10-24 04:22:40 +00:00
cdr = ase_lsp_eval (lsp, ASE_LSP_CAR(ASE_LSP_CDR(args)));
2007-02-10 13:52:41 +00:00
if (cdr == ASE_NULL)
{
ase_lsp_poptmp (lsp); /* car */
return ASE_NULL;
}
if (ase_lsp_pushtmp (lsp, cdr) == ASE_NULL)
{
ase_lsp_poptmp (lsp); /* car */
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)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp); /* cdr */
ase_lsp_poptmp (lsp); /* car */
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 10:23:19 +00:00
}
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp); /* cdr */
ase_lsp_poptmp (lsp); /* car */
2005-09-18 10:23:19 +00:00
return cons;
}
2007-02-13 06:00:20 +00:00
ase_lsp_obj_t* ase_lsp_prim_length (ase_lsp_t* lsp, ase_lsp_obj_t* args)
{
ase_lsp_obj_t* body, * tmp;
ase_long_t len = 0;
2007-03-07 12:14:29 +00:00
ASE_ASSERT (ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
2007-02-13 06:00:20 +00:00
body = args;
while (ASE_LSP_TYPE(body) == ASE_LSP_OBJ_CONS)
{
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(body));
if (tmp == ASE_NULL) return ASE_NULL;
if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_NIL)
{
len = 0;
}
else if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_STR)
{
len = ASE_LSP_STRLEN(tmp);
}
else if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_SYM)
{
len = ASE_LSP_SYMLEN(tmp);
}
else if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_CONS)
{
len = 0;
do
{
len++;
tmp = ASE_LSP_CDR(tmp);
}
while (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_CONS);
/* TODO: more flexible without the check below?
* both of the following expression evalute
* to 3 without it.
* (length '(9 9 9 . 9))
* (length '(9 9 9))
*/
if (ASE_LSP_TYPE(tmp) != ASE_LSP_OBJ_NIL)
{
ase_lsp_seterror (lsp, ASE_LSP_EVALBAD, ASE_NULL, 0);
return ASE_NULL;
}
}
else
{
ase_lsp_seterror (lsp, ASE_LSP_EVALBAD, ASE_NULL, 0);
return ASE_NULL;
}
body = ASE_LSP_CDR(body);
}
2007-03-07 12:14:29 +00:00
ASE_ASSERT (body == lsp->mem->nil);
2007-02-13 06:00:20 +00:00
return ase_lsp_makeintobj (lsp->mem, len);
}
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)
2007-02-10 13:52:41 +00:00
* rose
2005-09-19 12:04:00 +00:00
*/
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* p1, * p2;
2005-09-18 10:23:19 +00:00
2007-03-07 12:14:29 +00:00
ASE_ASSERT (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
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (lsp, p1) == ASE_NULL) return ASE_NULL;
2006-10-29 13:00:39 +00:00
if (ASE_LSP_TYPE(p1) != ASE_LSP_OBJ_SYM)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp); /* p1 */
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
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
p2 = ase_lsp_eval (lsp, ASE_LSP_CAR(ASE_LSP_CDR(args)));
2007-02-10 13:52:41 +00:00
if (p2 == ASE_NULL)
{
ase_lsp_poptmp (lsp); /* p1 */
return ASE_NULL;
}
if (ase_lsp_pushtmp (lsp, p2) == ASE_NULL)
{
ase_lsp_poptmp (lsp); /* p1 */
return ASE_NULL;
}
2005-09-18 10:23:19 +00:00
2006-10-29 13:00:39 +00:00
if (ase_lsp_setvalue (lsp->mem, p1, p2) == ASE_NULL)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp); /* p2 */
ase_lsp_poptmp (lsp); /* p1 */
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 10:23:19 +00:00
}
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp); /* p2 */
ase_lsp_poptmp (lsp); /* p1 */
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)
2007-02-10 13:52:41 +00:00
* (setq x "string")
2005-09-19 12:04:00 +00:00
*/
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
2006-10-29 13:00:39 +00:00
while (p != lsp->mem->nil)
{
2007-03-07 12:14:29 +00:00
ASE_ASSERT (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-29 13:00:39 +00:00
if (ASE_LSP_TYPE(p1) != ASE_LSP_OBJ_SYM)
{
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 10:23:19 +00:00
}
2006-10-29 13:00:39 +00:00
if (ASE_LSP_TYPE(ASE_LSP_CDR(p)) != ASE_LSP_OBJ_CONS)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGFEW;
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
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
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (lsp, p2) == ASE_NULL) return ASE_NULL;
2006-10-29 13:00:39 +00:00
if (ase_lsp_setvalue (lsp->mem, p1, p2) == ASE_NULL)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp);
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 10:23:19 +00:00
}
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp);
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))
*/
2007-03-07 12:14:29 +00:00
ASE_ASSERT (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
name = ASE_LSP_CAR(args);
2006-10-25 13:42:31 +00:00
if (ASE_LSP_TYPE(name) != ASE_LSP_OBJ_SYM)
{
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
2006-10-24 04:22:40 +00:00
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
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (lsp, fun) == ASE_NULL) return ASE_NULL;
2006-10-25 13:42:31 +00:00
if (ase_lsp_setfunc (lsp->mem, ASE_LSP_CAR(args), fun) == ASE_NULL)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp);
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 10:23:19 +00:00
}
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp);
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)
2007-02-10 13:52:41 +00:00
* (setq x (macro (abc) x y z))
2005-09-18 10:23:19 +00:00
*/
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
name = ASE_LSP_CAR(args);
2006-10-25 13:42:31 +00:00
if (ASE_LSP_TYPE(name) != ASE_LSP_OBJ_SYM)
{
2007-02-11 07:36:55 +00:00
ase_lsp_seterror (lsp, ASE_LSP_EARGBAD, ASE_NULL, 0);
2006-10-24 04:22:40 +00:00
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
2007-02-10 13:52:41 +00:00
if (ase_lsp_pushtmp (lsp, mac) == ASE_NULL) return ASE_NULL;
2006-10-25 13:42:31 +00:00
if (ase_lsp_setfunc (lsp->mem, ASE_LSP_CAR(args), mac) == ASE_NULL)
{
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp);
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 10:23:19 +00:00
}
2007-02-10 13:52:41 +00:00
ase_lsp_poptmp (lsp);
2005-09-18 10:23:19 +00:00
return mac;
}
2006-11-02 11:10:49 +00:00
ase_lsp_obj_t* ase_lsp_prim_or (ase_lsp_t* lsp, ase_lsp_obj_t* args)
{
/*
* (or 10 20 30 40)
* (or (= n 20) (= n 30))
*/
ase_lsp_obj_t* tmp;
/* TODO: this is wrong. redo the work */
while (ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS)
{
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(args));
if (tmp == ASE_NULL) return ASE_NULL;
if (ASE_LSP_TYPE(args) == ASE_LSP_OBJ_INT)
if (tmp != lsp->mem->nil) return lsp->mem->t;
args = ASE_LSP_CDR(args);
}
return lsp->mem->nil;
}