qse/ase/lsp/prim_math.c

333 lines
7.3 KiB
C
Raw Normal View History

2005-09-20 08:05:32 +00:00
/*
2006-10-22 13:10:46 +00:00
* $Id: prim_math.c,v 1.7 2006-10-22 13:10:46 bacon Exp $
2005-09-20 08:05:32 +00:00
*/
2006-10-22 13:10:46 +00:00
#include <sse/lsp/prim.h>
#include <sse/bas/assert.h>
2005-09-20 08:05:32 +00:00
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* sse_lsp_prim_plus (sse_lsp_t* lsp, sse_lsp_obj_t* args)
2005-09-20 08:05:32 +00:00
{
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* body, * tmp;
sse_lsp_int_t ivalue = 0;
sse_lsp_real_t rvalue = .0;
sse_bool_t realnum = sse_false;
2005-09-20 08:05:32 +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-20 08:05:32 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-22 13:10:46 +00:00
while (SSE_LSP_TYPE(body) == SSE_LSP_OBJ_CONS) {
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(body));
if (tmp == SSE_NULL) {
/*lsp->errnum = SSE_LSP_ERR_BAD_VALUE; */
return SSE_NULL;
2005-09-20 12:06:51 +00:00
}
2005-09-20 08:05:32 +00:00
2006-10-22 13:10:46 +00:00
if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_INT) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
sse_assert (realnum == sse_false);
ivalue = SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum)
2006-10-22 13:10:46 +00:00
ivalue = ivalue + SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
else
2006-10-22 13:10:46 +00:00
rvalue = rvalue + SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
2005-09-20 11:19:15 +00:00
}
2006-10-22 13:10:46 +00:00
else if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_REAL) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
sse_assert (realnum == sse_false);
realnum = sse_true;
rvalue = SSE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum) {
2006-10-22 13:10:46 +00:00
realnum = sse_true;
rvalue = (sse_lsp_real_t)ivalue;
2005-09-20 12:06:51 +00:00
}
2006-10-22 13:10:46 +00:00
rvalue = rvalue + SSE_LSP_RVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
}
else {
2006-10-22 13:10:46 +00:00
lsp->errnum = SSE_LSP_ERR_BAD_VALUE;
return SSE_NULL;
2005-09-20 08:05:32 +00:00
}
2005-09-20 12:06:51 +00:00
2006-10-22 13:10:46 +00:00
body = SSE_LSP_CDR(body);
2005-09-20 08:05:32 +00:00
}
2006-10-22 13:10:46 +00:00
sse_assert (body == lsp->mem->nil);
2005-09-20 09:17:06 +00:00
2005-09-20 11:19:15 +00:00
tmp = (realnum)?
2006-10-22 13:10:46 +00:00
sse_lsp_make_real (lsp->mem, rvalue):
sse_lsp_make_int (lsp->mem, ivalue);
if (tmp == SSE_NULL) {
lsp->errnum = SSE_LSP_ERR_MEMORY;
return SSE_NULL;
2005-09-20 08:05:32 +00:00
}
return tmp;
}
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* sse_lsp_prim_minus (sse_lsp_t* lsp, sse_lsp_obj_t* args)
2005-09-20 08:05:32 +00:00
{
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* body, * tmp;
sse_lsp_int_t ivalue = 0;
sse_lsp_real_t rvalue = .0;
sse_bool_t realnum = sse_false;
2005-09-20 08:05:32 +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-20 08:05:32 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-22 13:10:46 +00:00
while (SSE_LSP_TYPE(body) == SSE_LSP_OBJ_CONS) {
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(body));
if (tmp == SSE_NULL) return SSE_NULL;
2005-09-20 08:05:32 +00:00
2005-09-20 11:19:15 +00:00
2006-10-22 13:10:46 +00:00
if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_INT) {
2005-09-20 11:19:15 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
sse_assert (realnum == sse_false);
ivalue = SSE_LSP_IVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
else {
if (!realnum)
2006-10-22 13:10:46 +00:00
ivalue = ivalue - SSE_LSP_IVALUE(tmp);
2005-09-20 11:19:15 +00:00
else
2006-10-22 13:10:46 +00:00
rvalue = rvalue - SSE_LSP_IVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
}
2006-10-22 13:10:46 +00:00
else if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_REAL) {
2005-09-20 11:19:15 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
sse_assert (realnum == sse_false);
realnum = sse_true;
rvalue = SSE_LSP_RVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
else {
if (!realnum) {
2006-10-22 13:10:46 +00:00
realnum = sse_true;
rvalue = (sse_lsp_real_t)ivalue;
2005-09-20 11:19:15 +00:00
}
2006-10-22 13:10:46 +00:00
rvalue = rvalue - SSE_LSP_RVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
}
else {
2006-10-22 13:10:46 +00:00
lsp->errnum = SSE_LSP_ERR_BAD_VALUE;
return SSE_NULL;
2005-09-20 08:05:32 +00:00
}
2005-09-20 09:17:06 +00:00
2006-10-22 13:10:46 +00:00
body = SSE_LSP_CDR(body);
2005-09-20 08:05:32 +00:00
}
2006-10-22 13:10:46 +00:00
sse_assert (body == lsp->mem->nil);
2005-09-20 09:17:06 +00:00
2005-09-20 11:19:15 +00:00
tmp = (realnum)?
2006-10-22 13:10:46 +00:00
sse_lsp_make_real (lsp->mem, rvalue):
sse_lsp_make_int (lsp->mem, ivalue);
if (tmp == SSE_NULL) {
lsp->errnum = SSE_LSP_ERR_MEMORY;
return SSE_NULL;
2005-09-20 08:05:32 +00:00
}
return tmp;
}
2005-09-20 11:19:15 +00:00
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* sse_lsp_prim_multiply (sse_lsp_t* lsp, sse_lsp_obj_t* args)
2005-09-20 12:06:51 +00:00
{
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* body, * tmp;
sse_lsp_int_t ivalue = 0;
sse_lsp_real_t rvalue = .0;
sse_bool_t realnum = sse_false;
2005-09-20 12:06:51 +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-20 12:06:51 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-22 13:10:46 +00:00
while (SSE_LSP_TYPE(body) == SSE_LSP_OBJ_CONS) {
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(body));
if (tmp == SSE_NULL) return SSE_NULL;
2005-09-20 12:06:51 +00:00
2006-10-22 13:10:46 +00:00
if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_INT) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
sse_assert (realnum == sse_false);
ivalue = SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum)
2006-10-22 13:10:46 +00:00
ivalue = ivalue * SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
else
2006-10-22 13:10:46 +00:00
rvalue = rvalue * SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
2006-10-22 13:10:46 +00:00
else if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_REAL) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
sse_assert (realnum == sse_false);
realnum = sse_true;
rvalue = SSE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum) {
2006-10-22 13:10:46 +00:00
realnum = sse_true;
rvalue = (sse_lsp_real_t)ivalue;
2005-09-20 12:06:51 +00:00
}
2006-10-22 13:10:46 +00:00
rvalue = rvalue * SSE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
else {
2006-10-22 13:10:46 +00:00
lsp->errnum = SSE_LSP_ERR_BAD_VALUE;
return SSE_NULL;
2005-09-20 12:06:51 +00:00
}
2006-10-22 13:10:46 +00:00
body = SSE_LSP_CDR(body);
2005-09-20 12:06:51 +00:00
}
2006-10-22 13:10:46 +00:00
sse_assert (body == lsp->mem->nil);
2005-09-20 12:06:51 +00:00
tmp = (realnum)?
2006-10-22 13:10:46 +00:00
sse_lsp_make_real (lsp->mem, rvalue):
sse_lsp_make_int (lsp->mem, ivalue);
if (tmp == SSE_NULL) {
lsp->errnum = SSE_LSP_ERR_MEMORY;
return SSE_NULL;
2005-09-20 12:06:51 +00:00
}
return tmp;
}
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* sse_lsp_prim_divide (sse_lsp_t* lsp, sse_lsp_obj_t* args)
2005-09-20 12:06:51 +00:00
{
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* body, * tmp;
sse_lsp_int_t ivalue = 0;
sse_lsp_real_t rvalue = .0;
sse_bool_t realnum = sse_false;
2005-09-20 12:06:51 +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-20 12:06:51 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-22 13:10:46 +00:00
while (SSE_LSP_TYPE(body) == SSE_LSP_OBJ_CONS) {
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(body));
if (tmp == SSE_NULL) return SSE_NULL;
2005-09-20 12:06:51 +00:00
2006-10-22 13:10:46 +00:00
if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_INT) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
sse_assert (realnum == sse_false);
ivalue = SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
2005-09-21 12:04:05 +00:00
if (!realnum) {
2006-10-22 13:10:46 +00:00
if (SSE_LSP_IVALUE(tmp) == 0) {
lsp->errnum = SSE_LSP_ERR_DIVIDE_BY_ZERO;
return SSE_NULL;
2005-09-21 12:04:05 +00:00
}
2006-10-22 13:10:46 +00:00
ivalue = ivalue / SSE_LSP_IVALUE(tmp);
2005-09-21 12:04:05 +00:00
}
2005-09-20 12:06:51 +00:00
else
2006-10-22 13:10:46 +00:00
rvalue = rvalue / SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
2006-10-22 13:10:46 +00:00
else if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_REAL) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
sse_assert (realnum == sse_false);
realnum = sse_true;
rvalue = SSE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum) {
2006-10-22 13:10:46 +00:00
realnum = sse_true;
rvalue = (sse_lsp_real_t)ivalue;
2005-09-20 12:06:51 +00:00
}
2006-10-22 13:10:46 +00:00
rvalue = rvalue / SSE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
else {
2006-10-22 13:10:46 +00:00
lsp->errnum = SSE_LSP_ERR_BAD_VALUE;
return SSE_NULL;
2005-09-20 12:06:51 +00:00
}
2006-10-22 13:10:46 +00:00
body = SSE_LSP_CDR(body);
2005-09-20 12:06:51 +00:00
}
2006-10-22 13:10:46 +00:00
sse_assert (body == lsp->mem->nil);
2005-09-20 12:06:51 +00:00
tmp = (realnum)?
2006-10-22 13:10:46 +00:00
sse_lsp_make_real (lsp->mem, rvalue):
sse_lsp_make_int (lsp->mem, ivalue);
if (tmp == SSE_NULL) {
lsp->errnum = SSE_LSP_ERR_MEMORY;
return SSE_NULL;
2005-09-20 12:06:51 +00:00
}
return tmp;
}
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* sse_lsp_prim_modulus (sse_lsp_t* lsp, sse_lsp_obj_t* args)
2005-09-20 12:06:51 +00:00
{
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* body, * tmp;
sse_lsp_int_t ivalue = 0;
2005-09-20 12:06:51 +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-20 12:06:51 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-22 13:10:46 +00:00
while (SSE_LSP_TYPE(body) == SSE_LSP_OBJ_CONS) {
tmp = sse_lsp_eval (lsp, SSE_LSP_CAR(body));
if (tmp == SSE_NULL) return SSE_NULL;
2005-09-20 12:06:51 +00:00
2006-10-22 13:10:46 +00:00
if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_INT) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
ivalue = SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
2006-10-22 13:10:46 +00:00
if (SSE_LSP_IVALUE(tmp) == 0) {
lsp->errnum = SSE_LSP_ERR_DIVIDE_BY_ZERO;
return SSE_NULL;
2005-09-21 12:04:05 +00:00
}
2006-10-22 13:10:46 +00:00
ivalue = ivalue % SSE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
2006-10-22 13:10:46 +00:00
else if (SSE_LSP_TYPE(tmp) == SSE_LSP_OBJ_REAL) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-22 13:10:46 +00:00
ivalue = (sse_lsp_int_t)SSE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
2006-10-22 13:10:46 +00:00
sse_lsp_int_t tmpi = (sse_lsp_int_t)SSE_LSP_RVALUE(tmp);
2005-09-21 12:04:05 +00:00
if (tmpi == 0) {
2006-10-22 13:10:46 +00:00
lsp->errnum = SSE_LSP_ERR_DIVIDE_BY_ZERO;
return SSE_NULL;
2005-09-21 12:04:05 +00:00
}
ivalue = ivalue % tmpi;
2005-09-20 12:06:51 +00:00
}
}
else {
2006-10-22 13:10:46 +00:00
lsp->errnum = SSE_LSP_ERR_BAD_VALUE;
return SSE_NULL;
2005-09-20 12:06:51 +00:00
}
2006-10-22 13:10:46 +00:00
body = SSE_LSP_CDR(body);
2005-09-20 12:06:51 +00:00
}
2006-10-22 13:10:46 +00:00
sse_assert (body == lsp->mem->nil);
2005-09-20 12:06:51 +00:00
2006-10-22 13:10:46 +00:00
tmp = sse_lsp_make_int (lsp->mem, ivalue);
if (tmp == SSE_NULL) {
lsp->errnum = SSE_LSP_ERR_MEMORY;
return SSE_NULL;
2005-09-20 12:06:51 +00:00
}
return tmp;
}