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: parser.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: Prolog's parser *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* parser: produces a prolog term from an array of tokens
|
|
|
|
*
|
|
|
|
* parser usage: the parser takes its input from an array of token descriptions
|
|
|
|
* addressed by the global variable 'tokptr' and produces a Term as result. A
|
|
|
|
* macro 'NextToken' should be defined in 'yap.h' for advancing 'tokptr' from
|
|
|
|
* one token to the next. In the distributed version this macro also updates
|
|
|
|
* a variable named 'toktide' for keeping track of how far the parser went
|
|
|
|
* before failling with a syntax error. The parser should be invoked with
|
|
|
|
* 'tokptr' pointing to the first token. The last token should have type
|
|
|
|
* 'eot_tok'. The parser return either a Term. Syntactic errors are signaled
|
|
|
|
* by a return value 0. The parser builds new terms on the 'global stack' and
|
|
|
|
* also uses an auxiliary stack pointed to by 'AuxSp'. In the distributed
|
|
|
|
* version this auxiliary stack is assumed to grow downwards. This
|
|
|
|
* assumption, however, is only relevant to routine 'ParseArgs', and to the
|
|
|
|
* variable toktide. conclusion: set tokptr pointing to first token set AuxSp
|
|
|
|
* Call Parse
|
|
|
|
*
|
|
|
|
* VSC: Working whithout known bugs in 87/4/6
|
|
|
|
*
|
|
|
|
* LD: -I or +I evaluated by parser 87/4/28
|
|
|
|
*
|
|
|
|
* LD: parser extended 87/4/28
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
|
|
|
#include "Heap.h"
|
|
|
|
#include "yapio.h"
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __STDC__XXX
|
|
|
|
#define Volatile volatile
|
|
|
|
#else
|
|
|
|
#define Volatile
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* weak backtraking mechanism based on long_jump */
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
typedef struct jmp_buff_struct {
|
2001-04-09 20:54:03 +01:00
|
|
|
jmp_buf JmpBuff;
|
|
|
|
} JMPBUFF;
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
STATIC_PROTO(void GNextToken, (void));
|
|
|
|
STATIC_PROTO(void checkfor, (Term, JMPBUFF *));
|
|
|
|
STATIC_PROTO(Term ParseArgs, (Atom, JMPBUFF *));
|
|
|
|
STATIC_PROTO(Term ParseList, (JMPBUFF *));
|
|
|
|
STATIC_PROTO(Term ParseTerm, (int, JMPBUFF *));
|
|
|
|
|
|
|
|
|
|
|
|
#define TRY(S,P) \
|
|
|
|
{ Volatile JMPBUFF *saveenv, newenv; \
|
|
|
|
Volatile TokEntry *saveT=Yap_tokptr; \
|
|
|
|
Volatile CELL *saveH=H; \
|
|
|
|
Volatile int savecurprio=curprio; \
|
|
|
|
saveenv=FailBuff; \
|
|
|
|
if(!setjmp(newenv.JmpBuff)) { \
|
|
|
|
FailBuff = &newenv; \
|
|
|
|
S; \
|
|
|
|
FailBuff=saveenv; \
|
|
|
|
P; \
|
|
|
|
} \
|
|
|
|
else { FailBuff=saveenv; \
|
|
|
|
H=saveH; \
|
|
|
|
curprio = savecurprio; \
|
|
|
|
Yap_tokptr=saveT; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TRY3(S,P,F) \
|
|
|
|
{ Volatile JMPBUFF *saveenv, newenv; \
|
|
|
|
Volatile TokEntry *saveT=Yap_tokptr; \
|
|
|
|
Volatile CELL *saveH=H; \
|
|
|
|
saveenv=FailBuff; \
|
|
|
|
if(!setjmp(newenv.JmpBuff)) { \
|
|
|
|
FailBuff = &newenv; \
|
|
|
|
S; \
|
|
|
|
FailBuff=saveenv; \
|
|
|
|
P; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
FailBuff=saveenv; \
|
|
|
|
H=saveH; \
|
|
|
|
Yap_tokptr=saveT; \
|
|
|
|
F } \
|
|
|
|
}
|
|
|
|
|
2005-11-16 01:55:03 +00:00
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
#define FAIL longjmp(FailBuff->JmpBuff,1)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
VarEntry *
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_LookupVar(char *var) /* lookup variable in variables table */
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
VarEntry *p;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_Option[4])
|
|
|
|
fprintf(Yap_stderr,"[LookupVar %s]", var);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
if (var[0] != '_' || var[1] != '\0') {
|
2002-11-18 18:18:05 +00:00
|
|
|
VarEntry **op = &Yap_VarTable;
|
2001-04-09 20:54:03 +01:00
|
|
|
unsigned char *vp = (unsigned char *)var;
|
2003-10-06 14:49:38 +01:00
|
|
|
UInt hv;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
p = Yap_VarTable;
|
2003-10-28 01:16:03 +00:00
|
|
|
hv = HashFunction(vp) % AtomHashTableSize;
|
2001-04-09 20:54:03 +01:00
|
|
|
while (p != NULL) {
|
|
|
|
CELL hpv = p->hv;
|
|
|
|
if (hv == hpv) {
|
|
|
|
Int scmp;
|
|
|
|
if ((scmp = strcmp(var, p->VarRep)) == 0) {
|
|
|
|
return(p);
|
|
|
|
} else if (scmp < 0) {
|
|
|
|
op = &(p->VarLeft);
|
|
|
|
p = p->VarLeft;
|
|
|
|
} else {
|
|
|
|
op = &(p->VarRight);
|
|
|
|
p = p->VarRight;
|
|
|
|
}
|
|
|
|
} else if (hv < hpv) {
|
|
|
|
op = &(p->VarLeft);
|
|
|
|
p = p->VarLeft;
|
|
|
|
} else {
|
|
|
|
op = &(p->VarRight);
|
|
|
|
p = p->VarRight;
|
|
|
|
}
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
p = (VarEntry *) Yap_AllocScannerMemory(strlen(var) + sizeof(VarEntry));
|
2001-04-09 20:54:03 +01:00
|
|
|
*op = p;
|
|
|
|
p->VarLeft = p->VarRight = NULL;
|
|
|
|
p->hv = hv;
|
|
|
|
strcpy(p->VarRep, var);
|
|
|
|
} else {
|
|
|
|
/* anon var */
|
2002-11-18 18:18:05 +00:00
|
|
|
p = (VarEntry *) Yap_AllocScannerMemory(sizeof(VarEntry) + 2);
|
|
|
|
p->VarLeft = Yap_AnonVarTable;
|
|
|
|
Yap_AnonVarTable = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
p->VarRight = NULL;
|
|
|
|
p->hv = 0L;
|
|
|
|
p->VarRep[0] = '_';
|
|
|
|
p->VarRep[1] = '\0';
|
|
|
|
}
|
|
|
|
p->VarAdr = TermNil;
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static Term
|
2001-04-09 20:54:03 +01:00
|
|
|
VarNames(VarEntry *p,Term l)
|
|
|
|
{
|
|
|
|
if (p != NULL) {
|
|
|
|
if (strcmp(p->VarRep, "_") != 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Term o = MkPairTerm(MkPairTerm(Yap_StringToList(p->VarRep), p->VarAdr),
|
2001-04-09 20:54:03 +01:00
|
|
|
VarNames(p->VarRight,
|
|
|
|
VarNames(p->VarLeft,l)));
|
|
|
|
if (H > ASP-4096) {
|
2006-01-02 02:16:19 +00:00
|
|
|
save_machine_regs();
|
2002-11-18 18:18:05 +00:00
|
|
|
longjmp(Yap_IOBotch,1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
return(o);
|
|
|
|
} else {
|
|
|
|
return(VarNames(p->VarRight,VarNames(p->VarLeft,l)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return (l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
Term
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_VarNames(VarEntry *p,Term l)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return VarNames(p,l);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-10-21 17:09:03 +01:00
|
|
|
IsPrefixOp(OpEntry *opp,int *pptr, int *rpptr)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int p;
|
|
|
|
|
2005-10-21 17:09:03 +01:00
|
|
|
READ_LOCK(opp->OpRWLock);
|
|
|
|
if (opp->OpModule &&
|
|
|
|
opp->OpModule != CurrentModule)
|
|
|
|
return FALSE;
|
|
|
|
if ((p = opp->Prefix) != 0) {
|
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
*pptr = *rpptr = p & MaskPrio;
|
|
|
|
if (p & DcrrpFlag)
|
|
|
|
--* rpptr;
|
2005-10-21 17:09:03 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2005-10-21 17:09:03 +01:00
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-10-21 17:09:03 +01:00
|
|
|
Yap_IsPrefixOp(OpEntry *opinfo,int *pptr, int *rpptr)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return IsPrefixOp(opinfo,pptr,rpptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-10-21 17:09:03 +01:00
|
|
|
IsInfixOp(OpEntry *opp, int *pptr, int *lpptr, int *rpptr)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int p;
|
|
|
|
|
2005-10-21 17:09:03 +01:00
|
|
|
READ_LOCK(opp->OpRWLock);
|
|
|
|
if (opp->OpModule &&
|
|
|
|
opp->OpModule != CurrentModule)
|
|
|
|
return FALSE;
|
|
|
|
if ((p = opp->Infix) != 0) {
|
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
*pptr = *rpptr = *lpptr = p & MaskPrio;
|
|
|
|
if (p & DcrrpFlag)
|
|
|
|
--* rpptr;
|
|
|
|
if (p & DcrlpFlag)
|
|
|
|
--* lpptr;
|
2005-10-21 17:09:03 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2005-10-21 17:09:03 +01:00
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-10-21 17:09:03 +01:00
|
|
|
Yap_IsInfixOp(OpEntry *opinfo, int *pptr, int *lpptr, int *rpptr)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return IsInfixOp(opinfo, pptr, lpptr, rpptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-10-21 17:09:03 +01:00
|
|
|
IsPosfixOp(OpEntry *opp, int *pptr, int *lpptr)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int p;
|
2005-10-21 17:09:03 +01:00
|
|
|
|
|
|
|
READ_LOCK(opp->OpRWLock);
|
|
|
|
if (opp->OpModule &&
|
|
|
|
opp->OpModule != CurrentModule)
|
|
|
|
return FALSE;
|
|
|
|
if ((p = opp->Posfix) != 0) {
|
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
*pptr = *lpptr = p & MaskPrio;
|
|
|
|
if (p & DcrlpFlag)
|
|
|
|
--* lpptr;
|
|
|
|
return (TRUE);
|
|
|
|
} else {
|
2005-10-21 17:09:03 +01:00
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
int
|
2005-10-21 17:09:03 +01:00
|
|
|
Yap_IsPosfixOp(OpEntry *opinfo, int *pptr, int *lpptr)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return IsPosfixOp(opinfo, pptr, lpptr);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
inline static void
|
|
|
|
GNextToken(void)
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok == Ord(eot_tok))
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
#ifdef EMACS
|
2002-11-18 18:18:05 +00:00
|
|
|
if ((Yap_tokptr = Yap_tokptr->TokNext)->TokPos > Yap_toktide->TokPos)
|
|
|
|
Yap_toktide = Yap_tokptr;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr == Yap_toktide)
|
|
|
|
Yap_toktide = Yap_tokptr = Yap_tokptr->TokNext;
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_tokptr = Yap_tokptr->TokNext;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static void
|
2004-01-23 02:23:51 +00:00
|
|
|
checkfor(Term c, JMPBUFF *FailBuff)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok != Ord(Ponctuation_tok)
|
|
|
|
|| Yap_tokptr->TokInfo != c)
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
NextToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Term
|
2004-01-23 02:23:51 +00:00
|
|
|
ParseArgs(Atom a, JMPBUFF *FailBuff)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int nargs = 0;
|
|
|
|
Term *p, t;
|
2007-04-18 07:30:41 +01:00
|
|
|
Functor func;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef SFUNC
|
2002-11-18 18:18:05 +00:00
|
|
|
SFEntry *pe = (SFEntry *) Yap_GetAProp(a, SFProperty);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
NextToken;
|
|
|
|
p = (Term *) ParserAuxSp;
|
|
|
|
while (1) {
|
|
|
|
Term *tp = (Term *)ParserAuxSp;
|
2004-10-28 21:12:23 +01:00
|
|
|
if (ParserAuxSp+1 > Yap_TrailTop) {
|
|
|
|
Yap_ErrorMessage = "Trail Overflow";
|
|
|
|
FAIL;
|
|
|
|
}
|
2004-01-23 02:23:51 +00:00
|
|
|
*tp++ = Unsigned(ParseTerm(999, FailBuff));
|
2004-11-19 17:14:15 +00:00
|
|
|
ParserAuxSp = (char *)tp;
|
2001-04-09 20:54:03 +01:00
|
|
|
++nargs;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok != Ord(Ponctuation_tok))
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (((int) Yap_tokptr->TokInfo) != ',')
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
NextToken;
|
|
|
|
}
|
2004-10-28 21:12:23 +01:00
|
|
|
ParserAuxSp = (char *)p;
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
* Needed because the arguments for the functor are placed in reverse
|
|
|
|
* order
|
|
|
|
*/
|
2002-10-29 03:10:00 +00:00
|
|
|
if (H > ASP-(nargs+1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = "Stack Overflow";
|
2002-10-29 03:10:00 +00:00
|
|
|
FAIL;
|
|
|
|
}
|
2007-04-18 07:30:41 +01:00
|
|
|
func = Yap_MkFunctor(a, nargs);
|
|
|
|
if (func == NULL) {
|
|
|
|
Yap_ErrorMessage = "Heap Overflow";
|
|
|
|
FAIL;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef SFUNC
|
|
|
|
if (pe)
|
2002-11-18 18:18:05 +00:00
|
|
|
t = MkSFTerm(Yap_MkFunctor(a, SFArity), nargs, p, pe->NilValue);
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(Yap_MkFunctor(a, nargs), nargs, p);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2004-02-13 18:39:29 +00:00
|
|
|
if (a == AtomDBRef && nargs == 2)
|
|
|
|
t = MkDBRefTerm((DBRef)IntegerOfTerm(p[0]));
|
|
|
|
else
|
2007-04-18 07:30:41 +01:00
|
|
|
t = Yap_MkApplTerm(func, nargs, p);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2005-11-22 12:42:39 +00:00
|
|
|
if (H > ASP-4096) {
|
|
|
|
Yap_ErrorMessage = "Stack Overflow";
|
|
|
|
return TermNil;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
2004-01-23 02:23:51 +00:00
|
|
|
checkfor((Term) ')', FailBuff);
|
2005-11-22 12:42:39 +00:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Term
|
2004-01-23 02:23:51 +00:00
|
|
|
ParseList(JMPBUFF *FailBuff)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2002-11-11 17:38:10 +00:00
|
|
|
Term o;
|
2002-10-28 17:46:55 +00:00
|
|
|
CELL *to_store;
|
|
|
|
o = AbsPair(H);
|
|
|
|
loop:
|
|
|
|
to_store = H;
|
|
|
|
H+=2;
|
2004-01-23 02:23:51 +00:00
|
|
|
to_store[0] = ParseTerm(999, FailBuff);
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok == Ord(Ponctuation_tok)) {
|
|
|
|
if (((int) Yap_tokptr->TokInfo) == ',') {
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok == Ord(Name_tok)
|
|
|
|
&& strcmp(RepAtom((Atom)(Yap_tokptr->TokInfo))->StrOfAE, "..") == 0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
2004-01-23 02:23:51 +00:00
|
|
|
to_store[1] = ParseTerm(999, FailBuff);
|
2002-10-28 17:46:55 +00:00
|
|
|
} else {
|
|
|
|
/* check for possible overflow against local stack */
|
|
|
|
if (H > ASP-4096) {
|
|
|
|
to_store[1] = TermNil;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = "Stack Overflow";
|
2002-10-28 17:46:55 +00:00
|
|
|
FAIL;
|
|
|
|
} else {
|
|
|
|
to_store[1] = AbsPair(H);
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
} else if (((int) Yap_tokptr->TokInfo) == '|') {
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
2004-01-23 02:23:51 +00:00
|
|
|
to_store[1] = ParseTerm(999, FailBuff);
|
2002-10-28 17:46:55 +00:00
|
|
|
} else {
|
|
|
|
to_store[1] = MkAtomTerm(AtomNil);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
FAIL;
|
2002-10-28 17:46:55 +00:00
|
|
|
return (o);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2001-06-19 17:31:59 +01:00
|
|
|
#ifndef INFINITY
|
|
|
|
#define INFINITY (1.0/0.0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NAN
|
|
|
|
#define NAN (0.0/0.0)
|
|
|
|
#endif
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Term
|
2004-01-23 02:23:51 +00:00
|
|
|
ParseTerm(int prio, JMPBUFF *FailBuff)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
/* parse term with priority prio */
|
2005-10-21 17:09:03 +01:00
|
|
|
Volatile OpEntry *opinfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
Volatile Term t;
|
|
|
|
Volatile Functor func;
|
|
|
|
Volatile VarEntry *varinfo;
|
|
|
|
Volatile int curprio = 0, opprio, oplprio, oprprio;
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
switch (Yap_tokptr->Tok) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case Name_tok:
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_tokptr->TokInfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
2002-11-18 18:18:05 +00:00
|
|
|
if ((Yap_tokptr->Tok != Ord(Ponctuation_tok)
|
|
|
|
|| Unsigned(Yap_tokptr->TokInfo) != 'l')
|
2005-10-21 17:09:03 +01:00
|
|
|
&& (opinfo = Yap_GetOpProp((Atom) t))
|
2001-04-09 20:54:03 +01:00
|
|
|
&& IsPrefixOp(opinfo, &opprio, &oprprio)
|
|
|
|
) {
|
|
|
|
/* special rules apply for +1, -2.3, etc... */
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok == Number_tok) {
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((Atom)t == AtomMinus) {
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_tokptr->TokInfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsIntTerm(t))
|
|
|
|
t = MkIntTerm(-IntOfTerm(t));
|
|
|
|
else if (IsFloatTerm(t))
|
|
|
|
t = MkFloatTerm(-FloatOfTerm(t));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
else if (IsBigIntTerm(t)) {
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_t new;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_init(new);
|
2002-11-18 18:18:05 +00:00
|
|
|
mpz_neg(new, Yap_BigIntOfTerm(t));
|
|
|
|
t = Yap_MkBigIntTerm(new);
|
2006-01-18 15:34:54 +00:00
|
|
|
mpz_clear(new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
t = MkLongIntTerm(-LongIntOfTerm(t));
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
} else if ((Atom)t == AtomPlus) {
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_tokptr->TokInfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
} else if (Yap_tokptr->Tok == Name_tok) {
|
|
|
|
Atom at = (Atom)Yap_tokptr->TokInfo;
|
2002-02-04 16:12:54 +00:00
|
|
|
#ifndef _MSC_VER
|
2001-06-19 17:31:59 +01:00
|
|
|
if ((Atom)t == AtomPlus) {
|
|
|
|
if (at == AtomInf) {
|
|
|
|
t = MkFloatTerm(INFINITY);
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
} else if (at == AtomNan) {
|
|
|
|
t = MkFloatTerm(NAN);
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if ((Atom)t == AtomMinus) {
|
|
|
|
if (at == AtomInf) {
|
|
|
|
t = MkFloatTerm(-INFINITY);
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
} else if (at == AtomNan) {
|
|
|
|
t = MkFloatTerm(NAN);
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-02-04 16:12:54 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (opprio <= prio) {
|
|
|
|
/* try to parse as a prefix operator */
|
|
|
|
TRY(
|
|
|
|
/* build appl on the heap */
|
2002-11-18 18:18:05 +00:00
|
|
|
func = Yap_MkFunctor((Atom) t, 1);
|
2007-04-18 07:30:41 +01:00
|
|
|
if (func == NULL) {
|
|
|
|
Yap_ErrorMessage = "Heap Overflow";
|
|
|
|
FAIL;
|
|
|
|
}
|
2004-01-23 02:23:51 +00:00
|
|
|
t = ParseTerm(oprprio, FailBuff);
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(func, 1, &t);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
|
|
|
if (H > ASP-4096) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
curprio = opprio;
|
|
|
|
,
|
|
|
|
break;
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok == Ord(Ponctuation_tok)
|
|
|
|
&& Unsigned(Yap_tokptr->TokInfo) == 'l')
|
2004-01-23 02:23:51 +00:00
|
|
|
t = ParseArgs((Atom) t, FailBuff);
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
|
|
|
t = MkAtomTerm((Atom)t);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Number_tok:
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_tokptr->TokInfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case String_tok: /* build list on the heap */
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
Volatile char *p = (char *) Yap_tokptr->TokInfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (*p == 0)
|
|
|
|
t = MkAtomTerm(AtomNil);
|
|
|
|
else if (yap_flags[YAP_DOUBLE_QUOTES_FLAG] == STRING_AS_CHARS)
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_StringToListOfAtoms(p);
|
2007-04-18 07:30:41 +01:00
|
|
|
else if (yap_flags[YAP_DOUBLE_QUOTES_FLAG] == STRING_AS_ATOM) {
|
|
|
|
Atom at = Yap_LookupAtom(p);
|
|
|
|
if (at == NIL) {
|
|
|
|
Yap_ErrorMessage = "Heap Overflow";
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
t = MkAtomTerm(at);
|
|
|
|
} else
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_StringToList(p);
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-11-27 17:42:03 +00:00
|
|
|
case WString_tok: /* build list on the heap */
|
|
|
|
{
|
|
|
|
Volatile wchar_t *p = (wchar_t *) Yap_tokptr->TokInfo;
|
|
|
|
if (*p == 0)
|
|
|
|
t = MkAtomTerm(AtomNil);
|
|
|
|
else if (yap_flags[YAP_DOUBLE_QUOTES_FLAG] == STRING_AS_CHARS)
|
2008-07-24 17:02:04 +01:00
|
|
|
t = Yap_WideStringToListOfAtoms(p);
|
2006-11-27 17:42:03 +00:00
|
|
|
else if (yap_flags[YAP_DOUBLE_QUOTES_FLAG] == STRING_AS_ATOM)
|
|
|
|
t = MkAtomTerm(Yap_LookupWideAtom(p));
|
|
|
|
else
|
2008-07-24 17:02:04 +01:00
|
|
|
t = Yap_WideStringToList(p);
|
2006-11-27 17:42:03 +00:00
|
|
|
NextToken;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
case Var_tok:
|
2002-11-18 18:18:05 +00:00
|
|
|
varinfo = (VarEntry *) (Yap_tokptr->TokInfo);
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((t = varinfo->VarAdr) == TermNil) {
|
|
|
|
t = varinfo->VarAdr = MkVarTerm();
|
|
|
|
}
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
|
2002-11-19 17:10:45 +00:00
|
|
|
case Error_tok:
|
|
|
|
FAIL;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
case Ponctuation_tok:
|
2002-11-18 18:18:05 +00:00
|
|
|
switch ((int) Yap_tokptr->TokInfo) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case '(':
|
|
|
|
case 'l': /* non solo ( */
|
|
|
|
NextToken;
|
2004-01-23 02:23:51 +00:00
|
|
|
t = ParseTerm(1200, FailBuff);
|
|
|
|
checkfor((Term) ')', FailBuff);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
NextToken;
|
2008-03-10 14:11:38 +00:00
|
|
|
if (Yap_tokptr->Tok == Ord(Ponctuation_tok) &&
|
|
|
|
Unsigned(Yap_tokptr->TokInfo) == ']') {
|
|
|
|
t = TermNil;
|
|
|
|
NextToken;
|
|
|
|
} else {
|
|
|
|
t = ParseList(FailBuff);
|
|
|
|
checkfor((Term) ']', FailBuff);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
NextToken;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok == Ord(Ponctuation_tok) &&
|
|
|
|
Unsigned(Yap_tokptr->TokInfo) == '}') {
|
2001-04-09 20:54:03 +01:00
|
|
|
t = MkAtomTerm(NameOfFunctor(FunctorBraces));
|
|
|
|
NextToken;
|
|
|
|
} else {
|
2004-01-23 02:23:51 +00:00
|
|
|
t = ParseTerm(1200, FailBuff);
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(FunctorBraces, 1, &t);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
|
|
|
if (H > ASP-4096) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
2004-01-23 02:23:51 +00:00
|
|
|
checkfor((Term) '}', FailBuff);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* main loop to parse infix and posfix operators starts here */
|
|
|
|
while (TRUE) {
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok == Ord(Name_tok)
|
2005-10-21 17:09:03 +01:00
|
|
|
&& (opinfo = Yap_GetOpProp((Atom)(Yap_tokptr->TokInfo)))) {
|
|
|
|
OpEntry *save_opinfo = opinfo;
|
2007-10-17 00:17:04 +01:00
|
|
|
if (IsInfixOp(opinfo, &opprio, &oplprio, &oprprio)
|
2001-04-09 20:54:03 +01:00
|
|
|
&& opprio <= prio && oplprio >= curprio) {
|
|
|
|
/* try parsing as infix operator */
|
|
|
|
Volatile int oldprio = curprio;
|
|
|
|
TRY3(
|
2002-11-18 18:18:05 +00:00
|
|
|
func = Yap_MkFunctor((Atom) Yap_tokptr->TokInfo, 2);
|
2007-04-18 07:30:41 +01:00
|
|
|
if (func == NULL) {
|
|
|
|
Yap_ErrorMessage = "Heap Overflow";
|
|
|
|
FAIL;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
|
|
|
{
|
|
|
|
Term args[2];
|
|
|
|
args[0] = t;
|
2004-01-23 02:23:51 +00:00
|
|
|
args[1] = ParseTerm(oprprio, FailBuff);
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(func, 2, args);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
|
|
|
if (H > ASP-4096) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
curprio = opprio;
|
|
|
|
opinfo = save_opinfo;
|
|
|
|
continue;
|
|
|
|
,
|
|
|
|
opinfo = save_opinfo;
|
|
|
|
curprio = oldprio;
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (IsPosfixOp(opinfo, &opprio, &oplprio)
|
|
|
|
&& opprio <= prio && oplprio >= curprio) {
|
|
|
|
/* parse as posfix operator */
|
2007-05-02 12:12:39 +01:00
|
|
|
Functor func = Yap_MkFunctor((Atom) Yap_tokptr->TokInfo, 1);
|
2007-04-18 07:30:41 +01:00
|
|
|
if (func == NULL) {
|
|
|
|
Yap_ErrorMessage = "Heap Overflow";
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
t = Yap_MkApplTerm(func, 1, &t);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
|
|
|
if (H > ASP-4096) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
curprio = opprio;
|
|
|
|
NextToken;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok == Ord(Ponctuation_tok)) {
|
|
|
|
if (Unsigned(Yap_tokptr->TokInfo) == ',' &&
|
2001-04-09 20:54:03 +01:00
|
|
|
prio >= 1000 && curprio <= 999) {
|
|
|
|
Volatile Term args[2];
|
|
|
|
NextToken;
|
|
|
|
args[0] = t;
|
2004-01-23 02:23:51 +00:00
|
|
|
args[1] = ParseTerm(1000, FailBuff);
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(Yap_MkFunctor(AtomComma, 2), 2, args);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
|
|
|
if (H > ASP-4096) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
curprio = 1000;
|
|
|
|
continue;
|
2002-11-18 18:18:05 +00:00
|
|
|
} else if (Unsigned(Yap_tokptr->TokInfo) == '|' && prio >= 1100 &&
|
2001-04-09 20:54:03 +01:00
|
|
|
curprio <= 1099) {
|
|
|
|
Volatile Term args[2];
|
|
|
|
NextToken;
|
|
|
|
args[0] = t;
|
2004-01-23 02:23:51 +00:00
|
|
|
args[1] = ParseTerm(1100, FailBuff);
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(FunctorVBar, 2, args);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
|
|
|
if (H > ASP-4096) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
curprio = 1100;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
if (Yap_tokptr->Tok <= Ord(WString_tok))
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
break;
|
|
|
|
}
|
2005-11-16 02:45:48 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (Yap_Option['p' - 'a' + 1]) {
|
|
|
|
Yap_DebugPutc(Yap_c_error_stream,'[');
|
|
|
|
Yap_plwrite (t, Yap_DebugPutc, 0);
|
|
|
|
Yap_DebugPutc(Yap_c_error_stream,']');
|
|
|
|
Yap_DebugPutc(Yap_c_error_stream,'\n');
|
|
|
|
}
|
|
|
|
#endif
|
2005-11-16 01:55:03 +00:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Term
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Parse(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Volatile Term t;
|
2004-01-23 02:23:51 +00:00
|
|
|
JMPBUFF FailBuff;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (!setjmp(FailBuff.JmpBuff)) {
|
2004-01-23 02:23:51 +00:00
|
|
|
t = ParseTerm(1200, &FailBuff);
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_tokptr->Tok != Ord(eot_tok))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (0L);
|
|
|
|
return (t);
|
|
|
|
} else
|
|
|
|
return (0);
|
|
|
|
}
|