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"
|
|
|
|
#include "Heap.h"
|
|
|
|
#include "eval.h"
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
yap_error_number Yap_matherror = YAP_NO_ERROR;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static Term
|
2008-12-04 23:33:32 +00:00
|
|
|
Eval(Term t)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
if (IsVarTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,TermNil,"in arithmetic");
|
2002-11-11 17:38:10 +00:00
|
|
|
P = (yamop *)FAILCODE;
|
2009-02-09 21:56:40 +00:00
|
|
|
return 0L;
|
2008-12-04 23:33:32 +00:00
|
|
|
} else if (IsAtomTerm(t)) {
|
2002-11-11 17:38:10 +00:00
|
|
|
ExpEntry *p;
|
2008-12-04 23:33:32 +00:00
|
|
|
Atom name = AtomOfTerm(t);
|
2002-11-11 17:38:10 +00:00
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 0)))) {
|
2007-06-04 13:28:02 +01:00
|
|
|
Term ti[2], terror;
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
/* error */
|
2007-06-04 13:28:02 +01:00
|
|
|
ti[0] = t;
|
|
|
|
ti[1] = MkIntegerTerm(0);
|
|
|
|
/* error */
|
2008-12-23 02:20:22 +00:00
|
|
|
terror = Yap_MkApplTerm(FunctorSlash, 2, ti);
|
2007-06-04 13:28:02 +01:00
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, terror,
|
2002-11-11 17:38:10 +00:00
|
|
|
"atom %s for arithmetic expression",
|
|
|
|
RepAtom(name)->StrOfAE);
|
|
|
|
P = (yamop *)FAILCODE;
|
2008-12-04 23:33:32 +00:00
|
|
|
return 0L;
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
return Yap_eval_atom(p->FOfEE);
|
|
|
|
} else if (IsIntTerm(t)) {
|
|
|
|
return t;
|
|
|
|
} else if (IsApplTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Functor fun = FunctorOfTerm(t);
|
|
|
|
switch ((CELL)fun) {
|
|
|
|
case (CELL)FunctorLongInt:
|
|
|
|
case (CELL)FunctorDouble:
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case (CELL)FunctorBigInt:
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
Int n = ArityOfFunctor(fun);
|
|
|
|
Atom name = NameOfFunctor(fun);
|
|
|
|
ExpEntry *p;
|
2008-12-04 23:33:32 +00:00
|
|
|
Term t1, t2;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, n)))) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term ti[2];
|
|
|
|
|
|
|
|
/* error */
|
|
|
|
ti[0] = t;
|
|
|
|
ti[1] = MkIntegerTerm(n);
|
2008-12-23 01:53:52 +00:00
|
|
|
t = Yap_MkApplTerm(FunctorSlash, 2, ti);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, t,
|
2001-04-09 20:54:03 +01:00
|
|
|
"functor %s/%d for arithmetic expression",
|
|
|
|
RepAtom(name)->StrOfAE,n);
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
t1 = Eval(ArgOfTerm(1,t));
|
|
|
|
if (t1 == 0L)
|
|
|
|
return FALSE;
|
|
|
|
if (n == 1)
|
|
|
|
return Yap_eval_unary(p->FOfEE, t1);
|
|
|
|
t2 = Eval(ArgOfTerm(2,t));
|
|
|
|
if (t2 == 0L)
|
|
|
|
return FALSE;
|
|
|
|
return Yap_eval_binary(p->FOfEE,t1,t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
} /* else if (IsPairTerm(t)) */ {
|
2008-04-08 16:36:53 +01:00
|
|
|
if (TailOfTerm(t) != TermNil) {
|
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, t,
|
|
|
|
"string must contain a single character to be evaluated as an arithmetic expression");
|
|
|
|
P = (yamop *)FAILCODE;
|
2008-12-04 23:33:32 +00:00
|
|
|
return 0L;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
return Eval(HeadOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_Eval(Term t)
|
|
|
|
{
|
|
|
|
return Eval(t);
|
|
|
|
}
|
|
|
|
|
2005-09-08 23:06:45 +01:00
|
|
|
#ifdef BEAM
|
|
|
|
Int BEAM_is(void);
|
|
|
|
|
|
|
|
Int
|
|
|
|
BEAM_is(void)
|
|
|
|
{ /* X is Y */
|
|
|
|
union arith_ret res;
|
|
|
|
blob_type bt;
|
|
|
|
|
|
|
|
bt = Eval(Deref(XREGS[2]), &res);
|
|
|
|
if (bt==db_ref_e) return (NULL);
|
|
|
|
return (EvalToTerm(bt,&res));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static Int
|
2009-02-09 22:21:58 +00:00
|
|
|
p_is(CELL result, CELL in)
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* X is Y */
|
2006-01-02 02:16:19 +00:00
|
|
|
Term out;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2009-02-09 22:21:58 +00:00
|
|
|
out = Eval(Deref(in));
|
2008-12-04 23:33:32 +00:00
|
|
|
if (out == 0L) {
|
2006-01-02 02:16:19 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_INT_OVERFLOW, ARG2, "is/2");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-02-09 22:21:58 +00:00
|
|
|
return Yap_unify_constant(result,out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitEval(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
/* here are the arithmetical predicates */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitConstExps();
|
|
|
|
Yap_InitUnaryExps();
|
|
|
|
Yap_InitBinaryExps();
|
2009-02-09 22:21:58 +00:00
|
|
|
Yap_InitCmpPred("is", 2, p_is, BinaryPredFlag | TestPredFlag | SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|