2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: eval.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: arithmetical expression evaluation *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file implements arithmetic operations
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "eval.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef PI
|
|
|
|
#ifdef M_PI
|
|
|
|
#define PI M_PI
|
|
|
|
#else
|
|
|
|
#define PI 3.14159265358979323846
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef M_E
|
|
|
|
#define M_E 2.7182818284590452354
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef INFINITY
|
|
|
|
#define INFINITY (1.0/0.0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NAN
|
|
|
|
#define NAN (0.0/0.0)
|
|
|
|
#endif
|
|
|
|
|
2009-10-20 09:21:59 +01:00
|
|
|
/* copied from SWI-Prolog */
|
|
|
|
#ifndef DBL_EPSILON /* normal for IEEE 64-bit double */
|
|
|
|
#define DBL_EPSILON 0.00000000000000022204
|
|
|
|
#endif
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
eval0(Int fi) {
|
|
|
|
arith0_op fop = fi;
|
|
|
|
switch (fop) {
|
|
|
|
case op_pi:
|
|
|
|
{
|
|
|
|
RFLOAT(PI);
|
|
|
|
}
|
|
|
|
case op_e:
|
|
|
|
{
|
|
|
|
RFLOAT(M_E);
|
|
|
|
}
|
2009-10-20 09:21:59 +01:00
|
|
|
case op_epsilon:
|
|
|
|
{
|
|
|
|
RFLOAT(DBL_EPSILON);
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_inf:
|
|
|
|
{
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef _MSC_VER /* Microsoft's Visual C++ Compiler */
|
2008-12-04 23:33:32 +00:00
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, TermNil, "evaluating infinity");
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2008-12-04 23:33:32 +00:00
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) {/* iso */
|
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, TermNil, "evaluating infinity");
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(INFINITY);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
case op_nan:
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER /* Microsoft's Visual C++ Compiler */
|
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, TermNil, "evaluating infinity");
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#else
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) {/* iso */
|
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, TermNil, "evaluating not-a-number");
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(NAN);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
case op_random:
|
|
|
|
{
|
|
|
|
RFLOAT(Yap_random());
|
|
|
|
}
|
|
|
|
case op_cputime:
|
|
|
|
{
|
|
|
|
RFLOAT((Float)Yap_cputime()/1000.0);
|
|
|
|
}
|
|
|
|
case op_heapused:
|
|
|
|
RINT(HeapUsed);
|
|
|
|
case op_localsp:
|
2001-04-09 20:54:03 +01:00
|
|
|
#if SBA
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT((Int)ASP);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(LCL0 - ASP);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_b:
|
2001-04-09 20:54:03 +01:00
|
|
|
#if SBA
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT((Int)B);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(LCL0 - (CELL *)B);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_env:
|
2001-12-17 18:31:11 +00:00
|
|
|
#if SBA
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT((Int)YENV);
|
2001-12-17 18:31:11 +00:00
|
|
|
#else
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(LCL0 - YENV);
|
2002-01-07 06:28:04 +00:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_tr:
|
2002-01-07 06:28:04 +00:00
|
|
|
#if SBA
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(TR);
|
2002-01-07 06:28:04 +00:00
|
|
|
#else
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(((CELL *)TR)-LCL0);
|
2001-12-17 18:31:11 +00:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_stackfree:
|
|
|
|
RINT(Unsigned(ASP) - Unsigned(H));
|
|
|
|
case op_globalsp:
|
2001-04-09 20:54:03 +01:00
|
|
|
#if SBA
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT((Int)H);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(H - H0);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term Yap_eval_atom(Int f)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
return eval0(f);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct init_const_eval {
|
|
|
|
char *OpName;
|
2008-12-04 23:33:32 +00:00
|
|
|
arith0_op f;
|
2001-04-09 20:54:03 +01:00
|
|
|
} InitConstEntry;
|
|
|
|
|
|
|
|
static InitConstEntry InitConstTab[] = {
|
2008-12-04 23:33:32 +00:00
|
|
|
{"pi", op_pi},
|
|
|
|
{"e", op_e},
|
2009-10-20 09:21:59 +01:00
|
|
|
{"epsilon", op_epsilon},
|
2008-12-04 23:33:32 +00:00
|
|
|
{"inf", op_inf},
|
|
|
|
{"nan", op_nan},
|
|
|
|
{"random", op_random},
|
|
|
|
{"cputime", op_cputime},
|
|
|
|
{"heapused", op_heapused},
|
|
|
|
{"local_sp", op_localsp},
|
|
|
|
{"global_sp", op_globalsp},
|
|
|
|
{"$last_choice_pt", op_b},
|
|
|
|
{"$env", op_env},
|
|
|
|
{"$tr", op_tr},
|
|
|
|
{"stackfree", op_stackfree},
|
2001-04-09 20:54:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitConstExps(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
ExpEntry *p;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(InitConstTab)/sizeof(InitConstEntry); ++i) {
|
2002-11-18 18:18:05 +00:00
|
|
|
AtomEntry *ae = RepAtom(Yap_LookupAtom(InitConstTab[i].OpName));
|
2005-03-01 22:25:09 +00:00
|
|
|
if (ae == NULL) {
|
|
|
|
Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"at InitConstExps");
|
|
|
|
return;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_LOCK(ae->ARWLock);
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_GetExpPropHavingLock(ae, 0)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
break;
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
p = (ExpEntry *) Yap_AllocAtomSpace(sizeof(ExpEntry));
|
2001-04-09 20:54:03 +01:00
|
|
|
p->KindOfPE = ExpProperty;
|
|
|
|
p->ArityOfEE = 0;
|
|
|
|
p->ENoOfEE = 0;
|
2008-12-04 23:33:32 +00:00
|
|
|
p->FOfEE = InitConstTab[i].f;
|
2001-10-30 16:42:05 +00:00
|
|
|
p->NextOfPE = ae->PropsOfAE;
|
|
|
|
ae->PropsOfAE = AbsExpProp(p);
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This routine is called from Restore to make sure we have the same arithmetic operators */
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ReInitConstExps(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2005-10-31 12:27:54 +00:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|