2005-09-20 08:05:32 +00:00
|
|
|
/*
|
2005-09-21 12:04:05 +00:00
|
|
|
* $Id: prim_math.c,v 1.5 2005-09-21 12:04:05 bacon Exp $
|
2005-09-20 08:05:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/lsp/prim.h>
|
|
|
|
#include <xp/bas/assert.h>
|
|
|
|
|
|
|
|
xp_lsp_obj_t* xp_lsp_prim_plus (xp_lsp_t* lsp, xp_lsp_obj_t* args)
|
|
|
|
{
|
|
|
|
xp_lsp_obj_t* body, * tmp;
|
2005-09-20 11:19:15 +00:00
|
|
|
xp_lsp_int_t ivalue = 0;
|
2005-09-20 12:06:51 +00:00
|
|
|
xp_lsp_real_t rvalue = .0;
|
2005-09-20 11:19:15 +00:00
|
|
|
xp_bool_t realnum = xp_false;
|
2005-09-20 08:05:32 +00:00
|
|
|
|
|
|
|
XP_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, XP_LSP_PRIM_MAX_ARG_COUNT);
|
|
|
|
xp_assert (XP_LSP_TYPE(args) == XP_LSP_OBJ_CONS);
|
|
|
|
|
|
|
|
body = args;
|
|
|
|
//while (body != lsp->mem->nil) {
|
|
|
|
while (XP_LSP_TYPE(body) == XP_LSP_OBJ_CONS) {
|
|
|
|
tmp = xp_lsp_eval (lsp, XP_LSP_CAR(body));
|
2005-09-20 12:06:51 +00:00
|
|
|
if (tmp == XP_NULL) {
|
|
|
|
/*lsp->errnum = XP_LSP_ERR_BAD_VALUE; */
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
2005-09-20 08:05:32 +00:00
|
|
|
|
2005-09-20 11:19:15 +00:00
|
|
|
if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_INT) {
|
2005-09-20 12:06:51 +00:00
|
|
|
if (body == args) {
|
|
|
|
xp_assert (realnum == xp_false);
|
|
|
|
ivalue = XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!realnum)
|
|
|
|
ivalue = ivalue + XP_LSP_IVALUE(tmp);
|
|
|
|
else
|
|
|
|
rvalue = rvalue + XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
2005-09-20 11:19:15 +00:00
|
|
|
}
|
|
|
|
else if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_REAL) {
|
2005-09-20 12:06:51 +00:00
|
|
|
if (body == args) {
|
|
|
|
xp_assert (realnum == xp_false);
|
2005-09-20 11:19:15 +00:00
|
|
|
realnum = xp_true;
|
2005-09-20 12:06:51 +00:00
|
|
|
rvalue = XP_LSP_RVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!realnum) {
|
|
|
|
realnum = xp_true;
|
|
|
|
rvalue = (xp_lsp_real_t)ivalue;
|
|
|
|
}
|
|
|
|
rvalue = rvalue + XP_LSP_RVALUE(tmp);
|
2005-09-20 11:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2005-09-20 08:05:32 +00:00
|
|
|
lsp->errnum = XP_LSP_ERR_BAD_VALUE;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-20 12:06:51 +00:00
|
|
|
|
2005-09-20 08:05:32 +00:00
|
|
|
body = XP_LSP_CDR(body);
|
|
|
|
}
|
|
|
|
|
2005-09-20 09:17:06 +00:00
|
|
|
xp_assert (body == lsp->mem->nil);
|
|
|
|
|
2005-09-20 11:19:15 +00:00
|
|
|
tmp = (realnum)?
|
|
|
|
xp_lsp_make_real (lsp->mem, rvalue):
|
|
|
|
xp_lsp_make_int (lsp->mem, ivalue);
|
2005-09-20 08:05:32 +00:00
|
|
|
if (tmp == XP_NULL) {
|
|
|
|
lsp->errnum = XP_LSP_ERR_MEM;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_lsp_obj_t* xp_lsp_prim_minus (xp_lsp_t* lsp, xp_lsp_obj_t* args)
|
|
|
|
{
|
|
|
|
xp_lsp_obj_t* body, * tmp;
|
2005-09-20 11:19:15 +00:00
|
|
|
xp_lsp_int_t ivalue = 0;
|
2005-09-20 12:06:51 +00:00
|
|
|
xp_lsp_real_t rvalue = .0;
|
2005-09-20 11:19:15 +00:00
|
|
|
xp_bool_t realnum = xp_false;
|
2005-09-20 08:05:32 +00:00
|
|
|
|
|
|
|
XP_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, XP_LSP_PRIM_MAX_ARG_COUNT);
|
|
|
|
xp_assert (XP_LSP_TYPE(args) == XP_LSP_OBJ_CONS);
|
|
|
|
|
|
|
|
body = args;
|
|
|
|
//while (body != lsp->mem->nil) {
|
|
|
|
while (XP_LSP_TYPE(body) == XP_LSP_OBJ_CONS) {
|
|
|
|
tmp = xp_lsp_eval (lsp, XP_LSP_CAR(body));
|
|
|
|
if (tmp == XP_NULL) return XP_NULL;
|
|
|
|
|
2005-09-20 11:19:15 +00:00
|
|
|
|
|
|
|
if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_INT) {
|
|
|
|
if (body == args) {
|
|
|
|
xp_assert (realnum == xp_false);
|
|
|
|
ivalue = XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!realnum)
|
|
|
|
ivalue = ivalue - XP_LSP_IVALUE(tmp);
|
|
|
|
else
|
|
|
|
rvalue = rvalue - XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_REAL) {
|
|
|
|
if (body == args) {
|
|
|
|
xp_assert (realnum == xp_false);
|
|
|
|
realnum = xp_true;
|
|
|
|
rvalue = XP_LSP_RVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!realnum) {
|
|
|
|
realnum = xp_true;
|
|
|
|
rvalue = (xp_lsp_real_t)ivalue;
|
|
|
|
}
|
|
|
|
rvalue = rvalue - XP_LSP_RVALUE(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2005-09-20 08:05:32 +00:00
|
|
|
lsp->errnum = XP_LSP_ERR_BAD_VALUE;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-20 09:17:06 +00:00
|
|
|
|
2005-09-20 08:05:32 +00:00
|
|
|
body = XP_LSP_CDR(body);
|
|
|
|
}
|
|
|
|
|
2005-09-20 09:17:06 +00:00
|
|
|
xp_assert (body == lsp->mem->nil);
|
|
|
|
|
2005-09-20 11:19:15 +00:00
|
|
|
tmp = (realnum)?
|
|
|
|
xp_lsp_make_real (lsp->mem, rvalue):
|
|
|
|
xp_lsp_make_int (lsp->mem, ivalue);
|
2005-09-20 08:05:32 +00:00
|
|
|
if (tmp == XP_NULL) {
|
|
|
|
lsp->errnum = XP_LSP_ERR_MEM;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
2005-09-20 11:19:15 +00:00
|
|
|
|
2005-09-20 12:06:51 +00:00
|
|
|
xp_lsp_obj_t* xp_lsp_prim_multiply (xp_lsp_t* lsp, xp_lsp_obj_t* args)
|
|
|
|
{
|
|
|
|
xp_lsp_obj_t* body, * tmp;
|
|
|
|
xp_lsp_int_t ivalue = 0;
|
|
|
|
xp_lsp_real_t rvalue = .0;
|
|
|
|
xp_bool_t realnum = xp_false;
|
|
|
|
|
|
|
|
XP_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, XP_LSP_PRIM_MAX_ARG_COUNT);
|
|
|
|
xp_assert (XP_LSP_TYPE(args) == XP_LSP_OBJ_CONS);
|
|
|
|
|
|
|
|
body = args;
|
|
|
|
//while (body != lsp->mem->nil) {
|
|
|
|
while (XP_LSP_TYPE(body) == XP_LSP_OBJ_CONS) {
|
|
|
|
tmp = xp_lsp_eval (lsp, XP_LSP_CAR(body));
|
|
|
|
if (tmp == XP_NULL) return XP_NULL;
|
|
|
|
|
|
|
|
|
|
|
|
if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_INT) {
|
|
|
|
if (body == args) {
|
|
|
|
xp_assert (realnum == xp_false);
|
|
|
|
ivalue = XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!realnum)
|
|
|
|
ivalue = ivalue * XP_LSP_IVALUE(tmp);
|
|
|
|
else
|
|
|
|
rvalue = rvalue * XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_REAL) {
|
|
|
|
if (body == args) {
|
|
|
|
xp_assert (realnum == xp_false);
|
|
|
|
realnum = xp_true;
|
|
|
|
rvalue = XP_LSP_RVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!realnum) {
|
|
|
|
realnum = xp_true;
|
|
|
|
rvalue = (xp_lsp_real_t)ivalue;
|
|
|
|
}
|
|
|
|
rvalue = rvalue * XP_LSP_RVALUE(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lsp->errnum = XP_LSP_ERR_BAD_VALUE;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
body = XP_LSP_CDR(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_assert (body == lsp->mem->nil);
|
|
|
|
|
|
|
|
tmp = (realnum)?
|
|
|
|
xp_lsp_make_real (lsp->mem, rvalue):
|
|
|
|
xp_lsp_make_int (lsp->mem, ivalue);
|
|
|
|
if (tmp == XP_NULL) {
|
|
|
|
lsp->errnum = XP_LSP_ERR_MEM;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_lsp_obj_t* xp_lsp_prim_divide (xp_lsp_t* lsp, xp_lsp_obj_t* args)
|
|
|
|
{
|
|
|
|
xp_lsp_obj_t* body, * tmp;
|
|
|
|
xp_lsp_int_t ivalue = 0;
|
|
|
|
xp_lsp_real_t rvalue = .0;
|
|
|
|
xp_bool_t realnum = xp_false;
|
|
|
|
|
|
|
|
XP_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, XP_LSP_PRIM_MAX_ARG_COUNT);
|
|
|
|
xp_assert (XP_LSP_TYPE(args) == XP_LSP_OBJ_CONS);
|
|
|
|
|
|
|
|
body = args;
|
|
|
|
//while (body != lsp->mem->nil) {
|
|
|
|
while (XP_LSP_TYPE(body) == XP_LSP_OBJ_CONS) {
|
|
|
|
tmp = xp_lsp_eval (lsp, XP_LSP_CAR(body));
|
|
|
|
if (tmp == XP_NULL) return XP_NULL;
|
|
|
|
|
|
|
|
|
|
|
|
if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_INT) {
|
|
|
|
if (body == args) {
|
|
|
|
xp_assert (realnum == xp_false);
|
|
|
|
ivalue = XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
2005-09-21 12:04:05 +00:00
|
|
|
if (!realnum) {
|
|
|
|
if (XP_LSP_IVALUE(tmp) == 0) {
|
|
|
|
lsp->errnum = XP_LSP_ERR_DIVIDE_BY_ZERO;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
2005-09-20 12:06:51 +00:00
|
|
|
ivalue = ivalue / XP_LSP_IVALUE(tmp);
|
2005-09-21 12:04:05 +00:00
|
|
|
}
|
2005-09-20 12:06:51 +00:00
|
|
|
else
|
|
|
|
rvalue = rvalue / XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_REAL) {
|
|
|
|
if (body == args) {
|
|
|
|
xp_assert (realnum == xp_false);
|
|
|
|
realnum = xp_true;
|
|
|
|
rvalue = XP_LSP_RVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!realnum) {
|
|
|
|
realnum = xp_true;
|
|
|
|
rvalue = (xp_lsp_real_t)ivalue;
|
|
|
|
}
|
|
|
|
rvalue = rvalue / XP_LSP_RVALUE(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lsp->errnum = XP_LSP_ERR_BAD_VALUE;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
body = XP_LSP_CDR(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_assert (body == lsp->mem->nil);
|
|
|
|
|
|
|
|
tmp = (realnum)?
|
|
|
|
xp_lsp_make_real (lsp->mem, rvalue):
|
|
|
|
xp_lsp_make_int (lsp->mem, ivalue);
|
|
|
|
if (tmp == XP_NULL) {
|
|
|
|
lsp->errnum = XP_LSP_ERR_MEM;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_lsp_obj_t* xp_lsp_prim_modulus (xp_lsp_t* lsp, xp_lsp_obj_t* args)
|
|
|
|
{
|
|
|
|
xp_lsp_obj_t* body, * tmp;
|
|
|
|
xp_lsp_int_t ivalue = 0;
|
|
|
|
|
|
|
|
XP_LSP_PRIM_CHECK_ARG_COUNT (lsp, args, 1, XP_LSP_PRIM_MAX_ARG_COUNT);
|
|
|
|
xp_assert (XP_LSP_TYPE(args) == XP_LSP_OBJ_CONS);
|
|
|
|
|
|
|
|
body = args;
|
|
|
|
//while (body != lsp->mem->nil) {
|
|
|
|
while (XP_LSP_TYPE(body) == XP_LSP_OBJ_CONS) {
|
|
|
|
tmp = xp_lsp_eval (lsp, XP_LSP_CAR(body));
|
|
|
|
if (tmp == XP_NULL) return XP_NULL;
|
|
|
|
|
|
|
|
if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_INT) {
|
|
|
|
if (body == args) {
|
|
|
|
ivalue = XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
2005-09-21 12:04:05 +00:00
|
|
|
if (XP_LSP_IVALUE(tmp) == 0) {
|
|
|
|
lsp->errnum = XP_LSP_ERR_DIVIDE_BY_ZERO;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
2005-09-20 12:06:51 +00:00
|
|
|
ivalue = ivalue % XP_LSP_IVALUE(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (XP_LSP_TYPE(tmp) == XP_LSP_OBJ_REAL) {
|
|
|
|
if (body == args) {
|
|
|
|
ivalue = (xp_lsp_int_t)XP_LSP_RVALUE(tmp);
|
|
|
|
}
|
|
|
|
else {
|
2005-09-21 12:04:05 +00:00
|
|
|
xp_lsp_int_t tmpi = (xp_lsp_int_t)XP_LSP_RVALUE(tmp);
|
|
|
|
if (tmpi == 0) {
|
|
|
|
lsp->errnum = XP_LSP_ERR_DIVIDE_BY_ZERO;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
ivalue = ivalue % tmpi;
|
2005-09-20 12:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lsp->errnum = XP_LSP_ERR_BAD_VALUE;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
body = XP_LSP_CDR(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_assert (body == lsp->mem->nil);
|
|
|
|
|
|
|
|
tmp = xp_lsp_make_int (lsp->mem, ivalue);
|
|
|
|
if (tmp == XP_NULL) {
|
|
|
|
lsp->errnum = XP_LSP_ERR_MEM;
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|