qse/ase/lsp/prim.c

407 lines
8.2 KiB
C
Raw Normal View History

2005-09-18 10:23:19 +00:00
/*
2006-10-30 03:34:41 +00:00
* $Id: prim.c,v 1.16 2006-10-30 03:34:41 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,
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
{
// TODO:
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-10-29 13:00:39 +00:00
n = ase_lsp_makesymobj (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
2006-10-26 08:17:38 +00:00
ase_lsp_lockobj (mem->lsp, n);
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);
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-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
{
2006-10-29 13:40:33 +00:00
lsp->errnum = ASE_LSP_ERR_EXIT;
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
{
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* tmp;
2005-09-18 10:23:19 +00:00
2006-10-26 09:31:28 +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-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-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)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
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
2006-10-29 13:00:39 +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-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));
if (ret == ASE_NULL) return ASE_NULL;
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)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
2006-10-24 04:22:40 +00:00
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-26 09:31:28 +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-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)));
if (tmp == ASE_NULL) return ASE_NULL;
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;
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));
if (res == ASE_NULL) return ASE_NULL;
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)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
2006-10-24 04:22:40 +00:00
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-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, 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;
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)
{
2006-10-24 04:22:40 +00:00
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
2006-10-29 13:00:39 +00:00
if (tmp != lsp->mem->nil)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
2006-10-24 04:22:40 +00:00
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-26 09:31:28 +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-29 13:00:39 +00:00
if (ASE_LSP_TYPE(tmp) != ASE_LSP_OBJ_CONS)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
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
2006-10-26 09:31:28 +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-29 13:00:39 +00:00
if (ASE_LSP_TYPE(tmp) != ASE_LSP_OBJ_CONS)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
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
2006-10-26 09:31:28 +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-26 09:31:28 +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-29 13:00:39 +00:00
if (ASE_LSP_TYPE(p1) != ASE_LSP_OBJ_SYM)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
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)));
if (p2 == ASE_NULL) 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)
{
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
2006-10-29 13:00:39 +00:00
while (p != lsp->mem->nil)
{
2006-10-26 09:31:28 +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-29 13:00:39 +00:00
if (ASE_LSP_TYPE(p1) != ASE_LSP_OBJ_SYM)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
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
2006-10-29 13:00:39 +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-26 09:31:28 +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
name = ASE_LSP_CAR(args);
2006-10-25 13:42:31 +00:00
if (ASE_LSP_TYPE(name) != ASE_LSP_OBJ_SYM)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
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
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
name = ASE_LSP_CAR(args);
2006-10-25 13:42:31 +00:00
if (ASE_LSP_TYPE(name) != ASE_LSP_OBJ_SYM)
{
2006-10-30 03:34:41 +00:00
lsp->errnum = ASE_LSP_EARGBAD;
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
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;
}