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"
|
2009-05-22 20:44:32 +01:00
|
|
|
#if HAVE_STDARG_H
|
|
|
|
#include <stdarg.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static Term
|
2011-03-07 16:02:55 +00:00
|
|
|
Eval(Term t USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
if (IsVarTerm(t)) {
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ArithError = TRUE;
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(INSTANTIATION_ERROR,t,"in arithmetic");
|
2010-01-11 10:35:36 +00:00
|
|
|
} else if (IsNumTerm(t)) {
|
|
|
|
return t;
|
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
|
|
|
/* error */
|
2010-02-26 12:01:08 +00:00
|
|
|
return Yap_ArithError(TYPE_ERROR_EVALUABLE, t,
|
2009-05-22 17:24:44 +01:00
|
|
|
"atom %s for arithmetic expression",
|
|
|
|
RepAtom(name)->StrOfAE);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
return Yap_eval_atom(p->FOfEE);
|
|
|
|
} else if (IsApplTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Functor fun = FunctorOfTerm(t);
|
2010-01-11 10:35:36 +00:00
|
|
|
if ((Atom)fun == AtomFoundVar) {
|
|
|
|
return Yap_ArithError(TYPE_ERROR_EVALUABLE, TermNil,
|
|
|
|
"cyclic term in arithmetic expression");
|
|
|
|
} else {
|
|
|
|
Int n = ArityOfFunctor(fun);
|
|
|
|
Atom name = NameOfFunctor(fun);
|
|
|
|
ExpEntry *p;
|
|
|
|
Term t1, t2;
|
|
|
|
|
|
|
|
if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, n)))) {
|
|
|
|
Term ti[2];
|
2009-05-22 17:21:39 +01:00
|
|
|
|
2010-01-11 10:35:36 +00:00
|
|
|
/* error */
|
|
|
|
ti[0] = t;
|
|
|
|
ti[1] = MkIntegerTerm(n);
|
|
|
|
t = Yap_MkApplTerm(FunctorSlash, 2, ti);
|
|
|
|
return Yap_ArithError(TYPE_ERROR_EVALUABLE, t,
|
|
|
|
"functor %s/%d for arithmetic expression",
|
|
|
|
RepAtom(name)->StrOfAE,n);
|
|
|
|
}
|
|
|
|
*RepAppl(t) = (CELL)AtomFoundVar;
|
2011-03-07 16:02:55 +00:00
|
|
|
t1 = Eval(ArgOfTerm(1,t) PASS_REGS);
|
2010-01-11 10:35:36 +00:00
|
|
|
if (t1 == 0L) {
|
|
|
|
*RepAppl(t) = (CELL)fun;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (n == 1) {
|
|
|
|
*RepAppl(t) = (CELL)fun;
|
|
|
|
return Yap_eval_unary(p->FOfEE, t1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2011-03-07 16:02:55 +00:00
|
|
|
t2 = Eval(ArgOfTerm(2,t) PASS_REGS);
|
2010-01-11 10:35:36 +00:00
|
|
|
*RepAppl(t) = (CELL)fun;
|
|
|
|
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) {
|
2009-05-22 17:24:44 +01:00
|
|
|
return Yap_ArithError(TYPE_ERROR_EVALUABLE, t,
|
|
|
|
"string must contain a single character to be evaluated as an arithmetic expression");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2011-03-07 16:02:55 +00:00
|
|
|
return Eval(HeadOfTerm(t) PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
2010-01-04 23:16:42 +00:00
|
|
|
Yap_InnerEval(Term t)
|
2008-12-04 23:33:32 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
|
|
|
return Eval(t PASS_REGS);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
p_is( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* X is Y */
|
2010-01-03 17:42:51 +00:00
|
|
|
Term out = 0L;
|
2009-05-22 17:21:39 +01:00
|
|
|
|
2011-03-07 16:02:55 +00:00
|
|
|
while (!(out = Eval(Deref(ARG2) PASS_REGS))) {
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_Error_TYPE == RESOURCE_ERROR_STACK) {
|
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
|
|
|
if (!Yap_gcl(LOCAL_Error_Size, 2, ENV, CP)) {
|
|
|
|
Yap_Error(RESOURCE_ERROR_STACK, ARG2, LOCAL_ErrorMessage);
|
2010-01-03 17:42:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, LOCAL_ErrorMessage);
|
2010-01-03 17:42:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_unify_constant(ARG1,out);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2009-05-22 17:21:39 +01:00
|
|
|
Int
|
|
|
|
Yap_ArithError(yap_error_number type, Term where, char *format,...)
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2009-05-22 17:21:39 +01:00
|
|
|
va_list ap;
|
|
|
|
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ArithError = TRUE;
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_Error_TYPE = type;
|
|
|
|
LOCAL_Error_Term = where;
|
|
|
|
if (!LOCAL_ErrorMessage)
|
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
2009-05-22 17:21:39 +01:00
|
|
|
va_start (ap, format);
|
|
|
|
if (format != NULL) {
|
|
|
|
#if HAVE_VSNPRINTF
|
2011-05-23 16:19:47 +01:00
|
|
|
(void) vsnprintf(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE, format, ap);
|
2009-05-22 17:21:39 +01:00
|
|
|
#else
|
2011-05-23 16:19:47 +01:00
|
|
|
(void) vsprintf(LOCAL_ErrorMessage, format, ap);
|
2009-05-22 17:21:39 +01:00
|
|
|
#endif
|
|
|
|
} else {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage[0] = '\0';
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2009-05-22 17:21:39 +01:00
|
|
|
va_end (ap);
|
|
|
|
return 0L;
|
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();
|
2010-01-03 17:42:51 +00:00
|
|
|
Yap_InitCPred("is", 2, p_is, 0L);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|