qse/ase/lsp/prim_math.c

334 lines
7.4 KiB
C
Raw Normal View History

2005-09-20 08:05:32 +00:00
/*
2006-10-26 09:31:28 +00:00
* $Id: prim_math.c,v 1.11 2006-10-26 09:31:28 bacon Exp $
2005-09-20 08:05:32 +00:00
*/
2006-10-26 08:17:38 +00:00
#include <ase/lsp/lsp_i.h>
2005-09-20 08:05:32 +00:00
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* ase_lsp_prim_plus (ase_lsp_t* lsp, ase_lsp_obj_t* args)
2005-09-20 08:05:32 +00:00
{
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* body, * tmp;
2006-10-25 13:42:31 +00:00
ase_long_t ivalue = 0;
ase_real_t rvalue = .0;
2006-10-24 04:22:40 +00:00
ase_bool_t realnum = ase_false;
2005-09-20 08:05:32 +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 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
2005-09-20 08:05:32 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-24 04:22:40 +00:00
while (ASE_LSP_TYPE(body) == ASE_LSP_OBJ_CONS) {
tmp = ase_lsp_eval (lsp, ASE_LSP_CAR(body));
if (tmp == ASE_NULL) {
/*lsp->errnum = ASE_LSP_ERR_BAD_VALUE; */
return ASE_NULL;
2005-09-20 12:06:51 +00:00
}
2005-09-20 08:05:32 +00:00
2006-10-24 04:22:40 +00:00
if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_INT) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, realnum == ase_false);
2006-10-24 04:22:40 +00:00
ivalue = ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum)
2006-10-24 04:22:40 +00:00
ivalue = ivalue + ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
else
2006-10-24 04:22:40 +00:00
rvalue = rvalue + ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
2005-09-20 11:19:15 +00:00
}
2006-10-24 04:22:40 +00:00
else if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_REAL) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, realnum == ase_false);
2006-10-24 04:22:40 +00:00
realnum = ase_true;
rvalue = ASE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum) {
2006-10-24 04:22:40 +00:00
realnum = ase_true;
2006-10-25 13:42:31 +00:00
rvalue = (ase_real_t)ivalue;
2005-09-20 12:06:51 +00:00
}
2006-10-24 04:22:40 +00:00
rvalue = rvalue + ASE_LSP_RVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
}
else {
2006-10-24 04:22:40 +00:00
lsp->errnum = ASE_LSP_ERR_BAD_VALUE;
return ASE_NULL;
2005-09-20 08:05:32 +00:00
}
2005-09-20 12:06:51 +00:00
2006-10-24 04:22:40 +00:00
body = ASE_LSP_CDR(body);
2005-09-20 08:05:32 +00:00
}
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, body == lsp->mem->nil);
2005-09-20 09:17:06 +00:00
2005-09-20 11:19:15 +00:00
tmp = (realnum)?
2006-10-25 13:42:31 +00:00
ase_lsp_makerealobj (lsp->mem, rvalue):
ase_lsp_makeintobj (lsp->mem, ivalue);
2006-10-24 04:22:40 +00:00
if (tmp == 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-20 08:05:32 +00:00
}
return tmp;
}
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* ase_lsp_prim_minus (ase_lsp_t* lsp, ase_lsp_obj_t* args)
2005-09-20 08:05:32 +00:00
{
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* body, * tmp;
2006-10-25 13:42:31 +00:00
ase_long_t ivalue = 0;
ase_real_t rvalue = .0;
2006-10-24 04:22:40 +00:00
ase_bool_t realnum = ase_false;
2005-09-20 08:05:32 +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 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
2005-09-20 08:05:32 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-24 04:22:40 +00:00
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;
2005-09-20 08:05:32 +00:00
2005-09-20 11:19:15 +00:00
2006-10-24 04:22:40 +00:00
if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_INT) {
2005-09-20 11:19:15 +00:00
if (body == args) {
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, realnum == ase_false);
2006-10-24 04:22:40 +00:00
ivalue = ASE_LSP_IVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
else {
if (!realnum)
2006-10-24 04:22:40 +00:00
ivalue = ivalue - ASE_LSP_IVALUE(tmp);
2005-09-20 11:19:15 +00:00
else
2006-10-24 04:22:40 +00:00
rvalue = rvalue - ASE_LSP_IVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
}
2006-10-24 04:22:40 +00:00
else if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_REAL) {
2005-09-20 11:19:15 +00:00
if (body == args) {
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, realnum == ase_false);
2006-10-24 04:22:40 +00:00
realnum = ase_true;
rvalue = ASE_LSP_RVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
else {
if (!realnum) {
2006-10-24 04:22:40 +00:00
realnum = ase_true;
2006-10-25 13:42:31 +00:00
rvalue = (ase_real_t)ivalue;
2005-09-20 11:19:15 +00:00
}
2006-10-24 04:22:40 +00:00
rvalue = rvalue - ASE_LSP_RVALUE(tmp);
2005-09-20 11:19:15 +00:00
}
}
else {
2006-10-24 04:22:40 +00:00
lsp->errnum = ASE_LSP_ERR_BAD_VALUE;
return ASE_NULL;
2005-09-20 08:05:32 +00:00
}
2005-09-20 09:17:06 +00:00
2006-10-24 04:22:40 +00:00
body = ASE_LSP_CDR(body);
2005-09-20 08:05:32 +00:00
}
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, body == lsp->mem->nil);
2005-09-20 09:17:06 +00:00
2005-09-20 11:19:15 +00:00
tmp = (realnum)?
2006-10-25 13:42:31 +00:00
ase_lsp_makerealobj (lsp->mem, rvalue):
ase_lsp_makeintobj (lsp->mem, ivalue);
2006-10-24 04:22:40 +00:00
if (tmp == 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-20 08:05:32 +00:00
}
return tmp;
}
2005-09-20 11:19:15 +00:00
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* ase_lsp_prim_multiply (ase_lsp_t* lsp, ase_lsp_obj_t* args)
2005-09-20 12:06:51 +00:00
{
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* body, * tmp;
2006-10-25 13:42:31 +00:00
ase_long_t ivalue = 0;
ase_real_t rvalue = .0;
2006-10-24 04:22:40 +00:00
ase_bool_t realnum = ase_false;
2005-09-20 12:06:51 +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 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
2005-09-20 12:06:51 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-24 04:22:40 +00:00
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;
2005-09-20 12:06:51 +00:00
2006-10-24 04:22:40 +00:00
if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_INT) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, realnum == ase_false);
2006-10-24 04:22:40 +00:00
ivalue = ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum)
2006-10-24 04:22:40 +00:00
ivalue = ivalue * ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
else
2006-10-24 04:22:40 +00:00
rvalue = rvalue * ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
2006-10-24 04:22:40 +00:00
else if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_REAL) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, realnum == ase_false);
2006-10-24 04:22:40 +00:00
realnum = ase_true;
rvalue = ASE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum) {
2006-10-24 04:22:40 +00:00
realnum = ase_true;
2006-10-25 13:42:31 +00:00
rvalue = (ase_real_t)ivalue;
2005-09-20 12:06:51 +00:00
}
2006-10-24 04:22:40 +00:00
rvalue = rvalue * ASE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
else {
2006-10-24 04:22:40 +00:00
lsp->errnum = ASE_LSP_ERR_BAD_VALUE;
return ASE_NULL;
2005-09-20 12:06:51 +00:00
}
2006-10-24 04:22:40 +00:00
body = ASE_LSP_CDR(body);
2005-09-20 12:06:51 +00:00
}
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, body == lsp->mem->nil);
2005-09-20 12:06:51 +00:00
tmp = (realnum)?
2006-10-25 13:42:31 +00:00
ase_lsp_makerealobj (lsp->mem, rvalue):
ase_lsp_makeintobj (lsp->mem, ivalue);
2006-10-24 04:22:40 +00:00
if (tmp == 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-20 12:06:51 +00:00
}
return tmp;
}
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* ase_lsp_prim_divide (ase_lsp_t* lsp, ase_lsp_obj_t* args)
2005-09-20 12:06:51 +00:00
{
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* body, * tmp;
2006-10-25 13:42:31 +00:00
ase_long_t ivalue = 0;
ase_real_t rvalue = .0;
2006-10-24 04:22:40 +00:00
ase_bool_t realnum = ase_false;
2005-09-20 12:06:51 +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 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
2005-09-20 12:06:51 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-24 04:22:40 +00:00
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;
2005-09-20 12:06:51 +00:00
2006-10-24 04:22:40 +00:00
if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_INT) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, realnum == ase_false);
2006-10-24 04:22:40 +00:00
ivalue = ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
2005-09-21 12:04:05 +00:00
if (!realnum) {
2006-10-24 04:22:40 +00:00
if (ASE_LSP_IVALUE(tmp) == 0) {
2006-10-26 08:17:38 +00:00
lsp->errnum = ASE_LSP_EDIVBYZERO;
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-21 12:04:05 +00:00
}
2006-10-24 04:22:40 +00:00
ivalue = ivalue / ASE_LSP_IVALUE(tmp);
2005-09-21 12:04:05 +00:00
}
2005-09-20 12:06:51 +00:00
else
2006-10-24 04:22:40 +00:00
rvalue = rvalue / ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
2006-10-24 04:22:40 +00:00
else if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_REAL) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, realnum == ase_false);
2006-10-24 04:22:40 +00:00
realnum = ase_true;
rvalue = ASE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
if (!realnum) {
2006-10-24 04:22:40 +00:00
realnum = ase_true;
2006-10-25 13:42:31 +00:00
rvalue = (ase_real_t)ivalue;
2005-09-20 12:06:51 +00:00
}
2006-10-24 04:22:40 +00:00
rvalue = rvalue / ASE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
else {
2006-10-24 04:22:40 +00:00
lsp->errnum = ASE_LSP_ERR_BAD_VALUE;
return ASE_NULL;
2005-09-20 12:06:51 +00:00
}
2006-10-24 04:22:40 +00:00
body = ASE_LSP_CDR(body);
2005-09-20 12:06:51 +00:00
}
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, body == lsp->mem->nil);
2005-09-20 12:06:51 +00:00
tmp = (realnum)?
2006-10-25 13:42:31 +00:00
ase_lsp_makerealobj (lsp->mem, rvalue):
ase_lsp_makeintobj (lsp->mem, ivalue);
2006-10-26 08:17:38 +00:00
if (tmp == ASE_NULL)
{
lsp->errnum = ASE_LSP_ENOMEM;
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-20 12:06:51 +00:00
}
return tmp;
}
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* ase_lsp_prim_modulus (ase_lsp_t* lsp, ase_lsp_obj_t* args)
2005-09-20 12:06:51 +00:00
{
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* body, * tmp;
2006-10-25 13:42:31 +00:00
ase_long_t ivalue = 0;
2005-09-20 12:06:51 +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 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, ASE_LSP_TYPE(args) == ASE_LSP_OBJ_CONS);
2005-09-20 12:06:51 +00:00
body = args;
//while (body != lsp->mem->nil) {
2006-10-24 04:22:40 +00:00
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;
2005-09-20 12:06:51 +00:00
2006-10-24 04:22:40 +00:00
if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_INT) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-24 04:22:40 +00:00
ivalue = ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
2006-10-24 04:22:40 +00:00
if (ASE_LSP_IVALUE(tmp) == 0) {
2006-10-26 08:17:38 +00:00
lsp->errnum = ASE_LSP_EDIVBYZERO;
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-21 12:04:05 +00:00
}
2006-10-24 04:22:40 +00:00
ivalue = ivalue % ASE_LSP_IVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
}
2006-10-24 04:22:40 +00:00
else if (ASE_LSP_TYPE(tmp) == ASE_LSP_OBJ_REAL) {
2005-09-20 12:06:51 +00:00
if (body == args) {
2006-10-25 13:42:31 +00:00
ivalue = (ase_long_t)ASE_LSP_RVALUE(tmp);
2005-09-20 12:06:51 +00:00
}
else {
2006-10-25 13:42:31 +00:00
ase_long_t tmpi = (ase_long_t)ASE_LSP_RVALUE(tmp);
2005-09-21 12:04:05 +00:00
if (tmpi == 0) {
2006-10-26 08:17:38 +00:00
lsp->errnum = ASE_LSP_EDIVBYZERO;
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-21 12:04:05 +00:00
}
ivalue = ivalue % tmpi;
2005-09-20 12:06:51 +00:00
}
}
else {
2006-10-24 04:22:40 +00:00
lsp->errnum = ASE_LSP_ERR_BAD_VALUE;
return ASE_NULL;
2005-09-20 12:06:51 +00:00
}
2006-10-24 04:22:40 +00:00
body = ASE_LSP_CDR(body);
2005-09-20 12:06:51 +00:00
}
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, body == lsp->mem->nil);
2005-09-20 12:06:51 +00:00
2006-10-25 13:42:31 +00:00
tmp = ase_lsp_makeintobj (lsp->mem, ivalue);
if (tmp == ASE_NULL)
{
lsp->errnum = ASE_LSP_ENOMEM;
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-20 12:06:51 +00:00
}
return tmp;
}