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: attvar.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: YAP support for attributed vars *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[]="%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
|
|
|
|
#include "Yatom.h"
|
|
|
|
#include "Heap.h"
|
|
|
|
#include "heapgc.h"
|
|
|
|
#include "attvar.h"
|
|
|
|
#ifndef NULL
|
|
|
|
#define NULL (void *)0
|
|
|
|
#endif
|
|
|
|
|
2004-06-23 18:24:20 +01:00
|
|
|
#ifdef COROUTINING
|
|
|
|
|
2006-01-07 02:12:32 +00:00
|
|
|
#define TermVoidAtt TermFoundVar
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static CELL *
|
|
|
|
AddToQueue(attvar_record *attv)
|
|
|
|
{
|
|
|
|
Term t[2];
|
2004-06-05 04:37:01 +01:00
|
|
|
Term WGs, ng;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
t[0] = (CELL)&(attv->Done);
|
|
|
|
t[1] = attv->Value;
|
|
|
|
/* follow the chain */
|
2004-06-05 04:37:01 +01:00
|
|
|
WGs = Yap_ReadTimedVar(WokenGoals);
|
|
|
|
ng = Yap_MkApplTerm(FunctorAttGoal, 2, t);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2004-06-05 04:37:01 +01:00
|
|
|
Yap_UpdateTimedVar(WokenGoals, MkPairTerm(ng, WGs));
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((Term)WGs == TermNil) {
|
|
|
|
/* from now on, we have to start waking up goals */
|
2004-01-23 02:23:51 +00:00
|
|
|
Yap_signal(YAP_WAKEUP_SIGNAL);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2004-06-05 04:37:01 +01:00
|
|
|
return(RepAppl(ng)+2);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2004-06-05 04:37:01 +01:00
|
|
|
static void
|
2001-04-09 20:54:03 +01:00
|
|
|
AddFailToQueue(void)
|
|
|
|
{
|
2004-06-05 04:37:01 +01:00
|
|
|
Term WGs;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* follow the chain */
|
2004-06-05 04:37:01 +01:00
|
|
|
WGs = Yap_ReadTimedVar(WokenGoals);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2004-06-05 04:37:01 +01:00
|
|
|
Yap_UpdateTimedVar(WokenGoals, MkPairTerm(MkAtomTerm(AtomFail),WGs));
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((Term)WGs == TermNil) {
|
|
|
|
/* from now on, we have to start waking up goals */
|
2004-01-23 02:23:51 +00:00
|
|
|
Yap_signal(YAP_WAKEUP_SIGNAL);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-12-11 03:34:03 +00:00
|
|
|
CopyAttVar(CELL *orig, CELL ***to_visit_ptr, CELL *res)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
register attvar_record *attv = (attvar_record *)orig;
|
|
|
|
register attvar_record *newv;
|
|
|
|
CELL **to_visit = *to_visit_ptr;
|
2005-09-09 18:24:39 +01:00
|
|
|
CELL *vt;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* add a new attributed variable */
|
2005-02-18 21:34:02 +00:00
|
|
|
newv = DelayTop();
|
2005-09-09 18:24:39 +01:00
|
|
|
if (H0 - (CELL *)newv < 1024)
|
2004-09-09 21:00:59 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
RESET_VARIABLE(&(newv->Value));
|
2005-09-09 18:24:39 +01:00
|
|
|
RESET_VARIABLE(&(newv->Done));
|
|
|
|
vt = &(attv->Atts);
|
|
|
|
to_visit[0] = vt-1;
|
|
|
|
to_visit[1] = vt;
|
2005-10-28 18:38:50 +01:00
|
|
|
if (IsVarTerm(attv->Atts)) {
|
|
|
|
newv->Atts = (CELL)H;
|
|
|
|
to_visit[2] = H;
|
|
|
|
H++;
|
|
|
|
} else {
|
|
|
|
to_visit[2] = &(newv->Atts);
|
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
to_visit[3] = (CELL *)vt[-1];
|
|
|
|
*to_visit_ptr = to_visit+4;
|
2001-12-11 03:34:03 +00:00
|
|
|
*res = (CELL)&(newv->Done);
|
2005-09-09 18:24:39 +01:00
|
|
|
SetDelayTop(newv+1);
|
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2001-12-17 18:31:11 +00:00
|
|
|
static Term
|
|
|
|
AttVarToTerm(CELL *orig)
|
|
|
|
{
|
2005-09-09 18:24:39 +01:00
|
|
|
attvar_record *attv = (attvar_record *)orig;
|
|
|
|
|
|
|
|
return attv->Atts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static attvar_record *
|
|
|
|
BuildNewAttVar(void)
|
|
|
|
{
|
|
|
|
attvar_record *attv = DelayTop();
|
|
|
|
if (H0 - (CELL *)(attv+1) < 1024) {
|
|
|
|
return NULL;
|
2001-12-17 18:31:11 +00:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
RESET_VARIABLE(&(attv->Done));
|
|
|
|
RESET_VARIABLE(&(attv->Value));
|
|
|
|
RESET_VARIABLE(&(attv->Atts));
|
|
|
|
SetDelayTop(attv+1);
|
|
|
|
return attv;
|
2001-12-17 18:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
TermToAttVar(Term attvar, Term to)
|
|
|
|
{
|
2005-09-09 18:24:39 +01:00
|
|
|
attvar_record *attv = BuildNewAttVar();
|
|
|
|
if (!attv)
|
|
|
|
return FALSE;
|
|
|
|
attv->Atts = attvar;
|
2005-11-26 02:57:25 +00:00
|
|
|
*VarOfTerm(to) = (CELL)attv;
|
2005-09-09 18:24:39 +01:00
|
|
|
return TRUE;
|
2001-12-17 18:31:11 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
|
|
|
WakeAttVar(CELL* pt1, CELL reg2)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* if bound to someone else, follow until we find the last one */
|
|
|
|
attvar_record *attv = (attvar_record *)pt1;
|
|
|
|
CELL *myH = H;
|
|
|
|
CELL *bind_ptr;
|
|
|
|
|
|
|
|
if (IsVarTerm(reg2)) {
|
2004-09-10 21:18:01 +01:00
|
|
|
if (pt1 == VarOfTerm(reg2))
|
|
|
|
return;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsAttachedTerm(reg2)) {
|
|
|
|
attvar_record *susp2 = (attvar_record *)VarOfTerm(reg2);
|
|
|
|
|
|
|
|
/* binding two suspended variables, be careful */
|
|
|
|
if (susp2 >= attv) {
|
2004-09-18 15:03:42 +01:00
|
|
|
if (!IsVarTerm(susp2->Value) || !IsUnboundVar(&susp2->Value)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* oops, our goal is on the queue to be woken */
|
2002-11-18 18:18:05 +00:00
|
|
|
if (!Yap_unify(susp2->Value, (CELL)pt1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
AddFailToQueue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Bind_Global(&(susp2->Value), (CELL)pt1);
|
|
|
|
AddToQueue(susp2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Bind(VarOfTerm(reg2), (CELL)pt1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-09-18 15:03:42 +01:00
|
|
|
if (!IsVarTerm(attv->Value) || !IsUnboundVar(&attv->Value)) {
|
2002-04-13 05:39:03 +01:00
|
|
|
/* oops, our goal is on the queue to be woken */
|
2002-11-18 18:18:05 +00:00
|
|
|
if (!Yap_unify(attv->Value, reg2)) {
|
2002-04-13 05:39:03 +01:00
|
|
|
AddFailToQueue();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
bind_ptr = AddToQueue(attv);
|
|
|
|
if (IsNonVarTerm(reg2)) {
|
|
|
|
if (IsPairTerm(reg2) && RepPair(reg2) == myH)
|
|
|
|
reg2 = AbsPair(H);
|
|
|
|
else if (IsApplTerm(reg2) && RepAppl(reg2) == myH)
|
|
|
|
reg2 = AbsAppl(H);
|
|
|
|
}
|
|
|
|
*bind_ptr = reg2;
|
|
|
|
Bind_Global(&(attv->Value), reg2);
|
|
|
|
}
|
|
|
|
|
2004-06-05 04:37:01 +01:00
|
|
|
void
|
|
|
|
Yap_WakeUp(CELL *pt0) {
|
|
|
|
CELL d0 = *pt0;
|
|
|
|
RESET_VARIABLE(pt0);
|
|
|
|
TR--;
|
|
|
|
WakeAttVar(pt0, d0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
|
|
|
mark_attvar(CELL *orig)
|
|
|
|
{
|
|
|
|
register attvar_record *attv = (attvar_record *)orig;
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_mark_external_reference(&(attv->Value));
|
|
|
|
Yap_mark_external_reference(&(attv->Done));
|
2005-09-09 18:24:39 +01:00
|
|
|
Yap_mark_external_reference(&(attv->Atts));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2001-07-04 17:48:54 +01:00
|
|
|
#if FROZEN_STACKS
|
|
|
|
static Term
|
|
|
|
CurrentTime(void) {
|
2002-11-18 18:18:05 +00:00
|
|
|
return(MkIntegerTerm(TR-(tr_fr_ptr)Yap_TrailBase));
|
2001-07-04 17:48:54 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static Term
|
2005-09-09 18:24:39 +01:00
|
|
|
BuildAttTerm(Functor mfun, UInt ar)
|
|
|
|
{
|
|
|
|
CELL *h0 = H;
|
|
|
|
UInt i;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
if (H+(1024+ar) > ASP) {
|
|
|
|
return 0L;
|
2001-07-04 17:48:54 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
H[0] = (CELL)mfun;
|
|
|
|
RESET_VARIABLE(H+1);
|
|
|
|
H += 2;
|
|
|
|
for (i = 1; i< ar; i++) {
|
2006-01-07 02:12:32 +00:00
|
|
|
*H = TermVoidAtt;
|
2005-09-09 18:24:39 +01:00
|
|
|
H++;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
return AbsAppl(h0);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
static Term
|
|
|
|
SearchAttsForModule(Term start, Functor mfun)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
if (IsVarTerm(start) ||
|
|
|
|
FunctorOfTerm(start) == mfun)
|
|
|
|
return start;
|
|
|
|
start = ArgOfTerm(1,start);
|
|
|
|
} while (TRUE);
|
2004-06-05 04:37:01 +01:00
|
|
|
}
|
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
static Term
|
|
|
|
SearchAttsForModuleName(Term start, Atom mname)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
if (IsVarTerm(start) ||
|
|
|
|
NameOfFunctor(FunctorOfTerm(start)) == mname)
|
|
|
|
return start;
|
|
|
|
start = ArgOfTerm(1,start);
|
|
|
|
} while (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-01-07 02:12:32 +00:00
|
|
|
AddNewModule(attvar_record *attv, Term t, int new, int do_it)
|
2005-09-09 18:24:39 +01:00
|
|
|
{
|
2006-01-07 02:12:32 +00:00
|
|
|
CELL *newp = RepAppl(t)+2;
|
|
|
|
UInt i, ar = ArityOfFunctor((Functor)newp[-2]);
|
|
|
|
|
|
|
|
for (i=1; i< ar; i++) {
|
|
|
|
Term n = Deref(*newp);
|
|
|
|
if (n == TermFreeTerm) {
|
|
|
|
*newp = TermVoidAtt;
|
|
|
|
} else {
|
|
|
|
if (n != TermVoidAtt)
|
|
|
|
do_it = TRUE;
|
|
|
|
}
|
|
|
|
newp++;
|
|
|
|
}
|
|
|
|
if (!do_it)
|
|
|
|
return;
|
2005-09-09 18:24:39 +01:00
|
|
|
if (IsVarTerm(attv->Atts)) {
|
|
|
|
if (new) {
|
|
|
|
attv->Atts = t;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2005-09-09 18:24:39 +01:00
|
|
|
Bind(&(attv->Atts),t);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
} else {
|
|
|
|
Term *wherep = &attv->Atts;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (IsVarTerm(*wherep)) {
|
|
|
|
Bind_Global(wherep,t);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
wherep = RepAppl(Deref(*wherep))+1;
|
|
|
|
}
|
|
|
|
} while (TRUE);
|
2003-02-14 10:52:00 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
static void
|
|
|
|
ReplaceAtts(attvar_record *attv, Term oatt, Term att)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2005-09-09 18:24:39 +01:00
|
|
|
UInt ar = ArityOfFunctor(FunctorOfTerm(oatt)), i;
|
|
|
|
CELL *oldp = RepAppl(oatt)+1;
|
2006-01-02 02:16:19 +00:00
|
|
|
CELL *newp;
|
2005-09-09 18:24:39 +01:00
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
if (oldp > HB) {
|
|
|
|
oldp++;
|
|
|
|
newp = RepAppl(att)+2;
|
|
|
|
/* if deterministic */
|
2006-01-07 02:12:32 +00:00
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
for (i=1; i< ar; i++) {
|
2006-01-07 02:12:32 +00:00
|
|
|
Term n = Deref(*newp);
|
|
|
|
if (n != TermFreeTerm) {
|
|
|
|
*oldp = n;
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
|
|
|
oldp++;
|
|
|
|
newp++;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newp = RepAppl(att)+1;
|
2005-09-09 18:24:39 +01:00
|
|
|
*newp++ = *oldp++;
|
|
|
|
for (i=1; i< ar; i++) {
|
2006-01-07 02:12:32 +00:00
|
|
|
Term n = Deref(*newp);
|
|
|
|
|
|
|
|
if (n == TermFreeTerm) {
|
|
|
|
*newp = Deref(*oldp);
|
2002-04-06 04:54:18 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
oldp++;
|
|
|
|
newp++;
|
2002-04-06 04:54:18 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
if (attv->Atts == oatt) {
|
|
|
|
if (RepAppl(attv->Atts) >= HB)
|
|
|
|
attv->Atts = att;
|
|
|
|
else
|
|
|
|
MaBind(&(attv->Atts), att);
|
|
|
|
} else {
|
|
|
|
Term *wherep = &attv->Atts;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
do {
|
|
|
|
if (*wherep == oatt) {
|
|
|
|
MaBind(wherep, att);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
wherep = RepAppl(Deref(*wherep))+1;
|
|
|
|
}
|
|
|
|
} while (TRUE);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
static void
|
|
|
|
DelAtts(attvar_record *attv, Term oatt)
|
|
|
|
{
|
|
|
|
if (attv->Atts == oatt) {
|
|
|
|
if (RepAppl(attv->Atts) >= HB)
|
|
|
|
attv->Atts = ArgOfTerm(1,oatt);
|
|
|
|
else
|
|
|
|
MaBind(&(attv->Atts), ArgOfTerm(1,oatt));
|
|
|
|
} else {
|
|
|
|
Term *wherep = &attv->Atts;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (*wherep == oatt) {
|
|
|
|
MaBind(wherep, ArgOfTerm(1,oatt));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
wherep = RepAppl(Deref(*wherep))+1;
|
|
|
|
}
|
|
|
|
} while (TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
static void
|
|
|
|
PutAtt(Int pos, Term atts, Term att)
|
|
|
|
{
|
|
|
|
if (IsVarTerm(att) && (CELL *)att > H && (CELL *)att < LCL0) {
|
|
|
|
/* globalise locals */
|
|
|
|
Term tnew = MkVarTerm();
|
|
|
|
Bind((CELL *)att, tnew);
|
|
|
|
att = tnew;
|
|
|
|
}
|
|
|
|
MaBind(RepAppl(atts)+pos, att);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
BindAttVar(attvar_record *attv) {
|
2004-09-18 15:03:42 +01:00
|
|
|
if (IsVarTerm(attv->Done) && IsUnboundVar(&attv->Done)) {
|
2003-02-14 10:52:00 +00:00
|
|
|
/* make sure we are not trying to bind a variable against itself */
|
|
|
|
if (!IsVarTerm(attv->Value)) {
|
|
|
|
Bind_Global(&(attv->Done), attv->Value);
|
|
|
|
} else if (IsVarTerm(attv->Value)) {
|
|
|
|
Term t = Deref(attv->Value);
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
if (IsAttachedTerm(t)) {
|
|
|
|
attvar_record *attv2 = (attvar_record *)VarOfTerm(t);
|
|
|
|
if (attv2 < attv) {
|
|
|
|
Bind_Global(&(attv->Done), t);
|
|
|
|
} else {
|
|
|
|
Bind_Global(&(attv2->Done), (CELL)attv);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Yap_Error(SYSTEM_ERROR,(CELL)&(attv->Done),"attvar was bound when unset");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Bind_Global(&(attv->Done), t);
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
return(TRUE);
|
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,(CELL)&(attv->Done),"attvar was bound when set");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Term
|
|
|
|
GetAllAtts(attvar_record *attv) {
|
2005-09-09 18:24:39 +01:00
|
|
|
/* check if we are already there */
|
|
|
|
return attv->Atts;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Term
|
2005-09-09 18:24:39 +01:00
|
|
|
AllAttVars(attvar_record *attv) {
|
2005-08-17 21:13:50 +01:00
|
|
|
CELL *h0 = H;
|
2005-09-09 18:24:39 +01:00
|
|
|
attvar_record *max = DelayTop();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
while (attv != max) {
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-08-17 21:13:50 +01:00
|
|
|
if (ASP - H < 1024) {
|
|
|
|
H = h0;
|
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
if (IsVarTerm(attv->Done) && IsUnboundVar(&attv->Done)) {
|
2005-09-09 18:24:39 +01:00
|
|
|
if (IsIntegerTerm(attv->Atts)) {
|
|
|
|
/* skip call residue(s) */
|
|
|
|
UInt n = IntegerOfTerm(attv->Atts)-1;
|
|
|
|
attv += n;
|
|
|
|
} else {
|
|
|
|
if (H != h0) {
|
|
|
|
H[-1] = AbsPair(H);
|
|
|
|
}
|
|
|
|
H[0] = (CELL)attv;
|
|
|
|
H += 2;
|
2005-08-17 21:13:50 +01:00
|
|
|
}
|
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
attv++;
|
2005-08-17 21:13:50 +01:00
|
|
|
}
|
|
|
|
if (H != h0) {
|
|
|
|
H[-1] = TermNil;
|
|
|
|
return AbsPair(h0);
|
|
|
|
} else {
|
|
|
|
return TermNil;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_put_att(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
2005-09-09 18:24:39 +01:00
|
|
|
attvar_record *attv;
|
|
|
|
Atom modname = AtomOfTerm(Deref(ARG2));
|
|
|
|
UInt ar = IntegerOfTerm(Deref(ARG3));
|
|
|
|
Functor mfun;
|
|
|
|
Term tatts;
|
|
|
|
int new = FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
} else {
|
|
|
|
while (!(attv = BuildNewAttVar())) {
|
|
|
|
if (!Yap_growglobal(NULL)) {
|
|
|
|
Yap_Error(OUT_OF_ATTVARS_ERROR, ARG1, Yap_ErrorMessage);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
inp = Deref(ARG1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
new = TRUE;
|
2004-09-27 21:45:04 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
mfun= Yap_MkFunctor(modname,ar);
|
|
|
|
if (IsVarTerm(tatts = SearchAttsForModule(attv->Atts,mfun))) {
|
|
|
|
while (!(tatts = BuildAttTerm(mfun,ar))) {
|
|
|
|
if (!Yap_gc(5, ENV, P)) {
|
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, Yap_ErrorMessage);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-09-27 21:45:04 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
Yap_unify(ARG1, (Term)attv);
|
2006-01-07 02:12:32 +00:00
|
|
|
AddNewModule(attv,tatts,new,TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
PutAtt(IntegerOfTerm(Deref(ARG4)), tatts, Deref(ARG5));
|
2004-09-27 21:45:04 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"put_attributes/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-31 18:12:51 +00:00
|
|
|
static Int
|
|
|
|
p_put_att_term(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
|
|
|
attvar_record *attv;
|
|
|
|
int new = FALSE;
|
|
|
|
|
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
} else {
|
|
|
|
while (!(attv = BuildNewAttVar())) {
|
|
|
|
if (!Yap_growglobal(NULL)) {
|
|
|
|
Yap_Error(OUT_OF_ATTVARS_ERROR, ARG1, Yap_ErrorMessage);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
inp = Deref(ARG1);
|
|
|
|
}
|
|
|
|
new = TRUE;
|
|
|
|
}
|
|
|
|
if (new) {
|
|
|
|
attv->Atts = Deref(ARG2);
|
|
|
|
} else {
|
|
|
|
MaBind(&(attv->Atts), Deref(ARG2));
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"put_attributes/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-05 04:37:01 +01:00
|
|
|
static Int
|
2005-09-09 18:24:39 +01:00
|
|
|
p_rm_att(void) {
|
2004-06-05 04:37:01 +01:00
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
2005-09-09 18:24:39 +01:00
|
|
|
attvar_record *attv;
|
|
|
|
Atom modname = AtomOfTerm(Deref(ARG2));
|
|
|
|
UInt ar = IntegerOfTerm(Deref(ARG3));
|
|
|
|
Functor mfun;
|
|
|
|
Term tatts;
|
|
|
|
int new = FALSE;
|
2004-06-05 04:37:01 +01:00
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
} else {
|
|
|
|
while (!(attv = BuildNewAttVar())) {
|
|
|
|
if (!Yap_growglobal(NULL)) {
|
|
|
|
Yap_Error(OUT_OF_ATTVARS_ERROR, ARG1, Yap_ErrorMessage);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
inp = Deref(ARG1);
|
2004-06-05 04:37:01 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
new = TRUE;
|
|
|
|
Yap_unify(ARG1, (Term)attv);
|
2004-06-05 04:37:01 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
mfun= Yap_MkFunctor(modname,ar);
|
|
|
|
if (IsVarTerm(tatts = SearchAttsForModule(attv->Atts,mfun))) {
|
|
|
|
while (!(tatts = BuildAttTerm(mfun,ar))) {
|
|
|
|
if (!Yap_gc(4, ENV, P)) {
|
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, Yap_ErrorMessage);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-09-27 21:45:04 +01:00
|
|
|
}
|
2006-01-07 02:12:32 +00:00
|
|
|
AddNewModule(attv,tatts,new, FALSE);
|
2005-09-09 18:24:39 +01:00
|
|
|
} else {
|
2006-01-07 02:12:32 +00:00
|
|
|
PutAtt(IntegerOfTerm(Deref(ARG4)), tatts, TermVoidAtt);
|
2004-09-27 21:45:04 +01:00
|
|
|
}
|
|
|
|
return TRUE;
|
2004-06-05 04:37:01 +01:00
|
|
|
} else {
|
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"put_attributes/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
2005-09-09 18:24:39 +01:00
|
|
|
p_put_atts(void) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
2005-09-09 18:24:39 +01:00
|
|
|
Term otatts;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
2005-09-09 18:24:39 +01:00
|
|
|
attvar_record *attv;
|
|
|
|
Term tatts = Deref(ARG2);
|
|
|
|
Functor mfun = FunctorOfTerm(tatts);
|
|
|
|
int new = FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
} else {
|
|
|
|
while (!(attv = BuildNewAttVar())) {
|
|
|
|
if (!Yap_growglobal(NULL)) {
|
|
|
|
Yap_Error(OUT_OF_ATTVARS_ERROR, ARG1, Yap_ErrorMessage);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
tatts = Deref(ARG2);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
new = TRUE;
|
|
|
|
Yap_unify(ARG1, (Term)attv);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
if (IsVarTerm(otatts = SearchAttsForModule(attv->Atts,mfun))) {
|
2006-01-07 02:12:32 +00:00
|
|
|
AddNewModule(attv,tatts,new,FALSE);
|
2005-09-09 18:24:39 +01:00
|
|
|
} else {
|
|
|
|
ReplaceAtts(attv, otatts, tatts);
|
|
|
|
}
|
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2005-09-09 18:24:39 +01:00
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"put_attributes/2");
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
static Int
|
|
|
|
p_del_atts(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
Term otatts;
|
|
|
|
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
|
|
|
attvar_record *attv;
|
|
|
|
Term tatts = Deref(ARG2);
|
|
|
|
Functor mfun = FunctorOfTerm(tatts);
|
|
|
|
|
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
} else {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (IsVarTerm(otatts = SearchAttsForModule(attv->Atts,mfun))) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
DelAtts(attv, otatts);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
|
|
|
p_get_att(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
2005-09-09 18:24:39 +01:00
|
|
|
Term inp = Deref(ARG1);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
2005-09-09 18:24:39 +01:00
|
|
|
Atom modname = AtomOfTerm(Deref(ARG2));
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsAttachedTerm(inp)) {
|
2005-09-09 18:24:39 +01:00
|
|
|
attvar_record *attv;
|
|
|
|
Term tout, tatts;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
if (IsVarTerm(tatts = SearchAttsForModuleName(attv->Atts,modname)))
|
2004-12-07 06:01:55 +00:00
|
|
|
return FALSE;
|
2005-09-09 18:24:39 +01:00
|
|
|
tout = ArgOfTerm(IntegerOfTerm(Deref(ARG3)),tatts);
|
2006-01-07 02:12:32 +00:00
|
|
|
if (tout == TermVoidAtt) return FALSE;
|
2005-09-09 18:24:39 +01:00
|
|
|
return Yap_unify(tout, ARG4);
|
|
|
|
} else {
|
|
|
|
/* Yap_Error(INSTANTIATION_ERROR,inp,"get_att/2"); */
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
2005-09-09 18:24:39 +01:00
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"put_attributes/2");
|
|
|
|
return(FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_free_att(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
2005-09-09 18:24:39 +01:00
|
|
|
Term inp = Deref(ARG1);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
2005-09-09 18:24:39 +01:00
|
|
|
Atom modname = AtomOfTerm(Deref(ARG2));
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsAttachedTerm(inp)) {
|
2005-09-09 18:24:39 +01:00
|
|
|
attvar_record *attv;
|
|
|
|
Term tout, tatts;
|
|
|
|
|
|
|
|
attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
if (IsVarTerm(tatts = SearchAttsForModuleName(attv->Atts,modname)))
|
|
|
|
return TRUE;
|
|
|
|
tout = ArgOfTerm(IntegerOfTerm(Deref(ARG3)),tatts);
|
2006-01-07 02:12:32 +00:00
|
|
|
return (tout == TermVoidAtt);
|
2005-09-09 18:24:39 +01:00
|
|
|
} else {
|
|
|
|
/* Yap_Error(INSTANTIATION_ERROR,inp,"get_att/2"); */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"put_attributes/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-09-09 18:24:39 +01:00
|
|
|
static Int
|
|
|
|
p_get_atts(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attvar_record *attv;
|
|
|
|
Term tatts;
|
|
|
|
Term access = Deref(ARG2);
|
|
|
|
Functor mfun = FunctorOfTerm(access);
|
|
|
|
UInt ar, i;
|
|
|
|
CELL *old, *new;
|
|
|
|
|
|
|
|
attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
if (IsVarTerm(tatts = SearchAttsForModule(attv->Atts,mfun)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ar = ArityOfFunctor(mfun);
|
|
|
|
new = RepAppl(access)+2;
|
|
|
|
old = RepAppl(tatts)+2;
|
|
|
|
for (i = 1; i < ar; i++,new++,old++) {
|
|
|
|
if (*new != TermFreeTerm) {
|
2006-01-07 02:12:32 +00:00
|
|
|
if (*old == TermVoidAtt && *new != TermVoidAtt)
|
|
|
|
return FALSE;
|
|
|
|
if (*new == TermVoidAtt && *old != TermVoidAtt)
|
2005-09-09 18:24:39 +01:00
|
|
|
return FALSE;
|
|
|
|
if (!Yap_unify(*new,*old)) return FALSE;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
/* Yap_Error(INSTANTIATION_ERROR,inp,"get_att/2"); */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
2005-10-18 18:04:43 +01:00
|
|
|
// Yap_Error(TYPE_ERROR_VARIABLE,inp,"get_attributes/2");
|
2005-09-09 18:24:39 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_has_atts(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attvar_record *attv;
|
|
|
|
Term tatts;
|
|
|
|
Term access = Deref(ARG2);
|
|
|
|
Functor mfun = FunctorOfTerm(access);
|
|
|
|
|
|
|
|
attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
return !IsVarTerm(tatts = SearchAttsForModule(attv->Atts,mfun));
|
|
|
|
} else {
|
|
|
|
/* Yap_Error(INSTANTIATION_ERROR,inp,"get_att/2"); */
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
2005-10-18 18:04:43 +01:00
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"has_attributes/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_bind_attvar(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attvar_record *attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
return(BindAttVar(attv));
|
|
|
|
}
|
|
|
|
return(TRUE);
|
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"bind_att/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_get_all_atts(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attvar_record *attv = (attvar_record *)VarOfTerm(inp);
|
2004-12-16 05:57:32 +00:00
|
|
|
return Yap_unify(ARG2,GetAllAtts(attv));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2004-12-16 05:57:32 +00:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"get_att/2");
|
2004-12-16 05:57:32 +00:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-18 18:04:43 +01:00
|
|
|
static int
|
|
|
|
ActiveAtt(Term tatt, UInt ar)
|
|
|
|
{
|
2006-01-04 03:47:49 +00:00
|
|
|
CELL *cp = RepAppl(tatt)+1;
|
2005-10-18 18:04:43 +01:00
|
|
|
UInt i;
|
|
|
|
|
|
|
|
for (i = 1; i < ar; i++) {
|
2006-01-07 02:12:32 +00:00
|
|
|
if (cp[i] != TermVoidAtt)
|
2005-10-18 18:04:43 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
2005-09-09 18:24:39 +01:00
|
|
|
p_modules_with_atts(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attvar_record *attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
CELL *h0 = H;
|
|
|
|
Term tatt;
|
|
|
|
|
|
|
|
if (IsVarTerm(tatt = attv->Atts))
|
|
|
|
return Yap_unify(ARG2,TermNil);
|
|
|
|
while (!IsVarTerm(tatt)) {
|
2005-10-18 18:04:43 +01:00
|
|
|
Functor f = FunctorOfTerm(tatt);
|
2006-01-08 03:12:02 +00:00
|
|
|
if (H != h0)
|
2005-09-09 18:24:39 +01:00
|
|
|
H[-1] = AbsPair(H);
|
2005-10-18 18:04:43 +01:00
|
|
|
if (ActiveAtt(tatt, ArityOfFunctor(f))) {
|
|
|
|
*H = MkAtomTerm(NameOfFunctor(f));
|
|
|
|
H+=2;
|
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
tatt = ArgOfTerm(1,tatt);
|
|
|
|
}
|
2005-10-18 18:04:43 +01:00
|
|
|
if (h0 != H) {
|
|
|
|
H[-1] = TermNil;
|
|
|
|
return Yap_unify(ARG2,AbsPair(h0));
|
|
|
|
}
|
2005-09-09 18:24:39 +01:00
|
|
|
}
|
2005-10-18 18:04:43 +01:00
|
|
|
return Yap_unify(ARG2,TermNil);
|
|
|
|
} else {
|
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"get_att/2");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_swi_all_atts(void) {
|
|
|
|
/* receive a variable in ARG1 */
|
|
|
|
Term inp = Deref(ARG1);
|
|
|
|
Functor attf = Yap_MkFunctor(Yap_LookupAtom("att"),3);
|
|
|
|
|
|
|
|
/* if this is unbound, ok */
|
|
|
|
if (IsVarTerm(inp)) {
|
|
|
|
if (IsAttachedTerm(inp)) {
|
|
|
|
attvar_record *attv = (attvar_record *)VarOfTerm(inp);
|
|
|
|
CELL *h0 = H;
|
|
|
|
Term tatt;
|
|
|
|
|
|
|
|
if (IsVarTerm(tatt = attv->Atts))
|
|
|
|
return Yap_unify(ARG2,TermNil);
|
|
|
|
while (!IsVarTerm(tatt)) {
|
|
|
|
Functor f = FunctorOfTerm(tatt);
|
|
|
|
|
|
|
|
if (ArityOfFunctor(f) == 2) {
|
|
|
|
if (H != h0)
|
|
|
|
H[-1] = AbsAppl(H);
|
|
|
|
H[0] = (CELL) attf;
|
|
|
|
H[1] = MkAtomTerm(NameOfFunctor(f));
|
|
|
|
H[2] = ArgOfTerm(2,tatt);
|
|
|
|
H+=4;
|
|
|
|
}
|
|
|
|
tatt = ArgOfTerm(1,tatt);
|
|
|
|
}
|
|
|
|
if (h0 != H) {
|
|
|
|
H[-1] = TermNil;
|
|
|
|
return Yap_unify(ARG2,AbsAppl(h0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Yap_unify(ARG2,TermNil);
|
2005-09-09 18:24:39 +01:00
|
|
|
} else {
|
|
|
|
Yap_Error(TYPE_ERROR_VARIABLE,inp,"get_att/2");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_all_attvars(void)
|
|
|
|
{
|
2005-09-09 18:24:39 +01:00
|
|
|
do {
|
|
|
|
Term out;
|
|
|
|
attvar_record *base;
|
|
|
|
|
|
|
|
base = (attvar_record *)Yap_GlobalBase+IntegerOfTerm(Yap_ReadTimedVar(AttsMutableList));
|
|
|
|
if (!(out = AllAttVars(base))) {
|
|
|
|
if (!Yap_gc(1, ENV, P)) {
|
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, Yap_ErrorMessage);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Yap_unify(ARG1,out);
|
|
|
|
}
|
|
|
|
} while (TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_is_attvar(void)
|
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
return(IsVarTerm(t) &&
|
2005-09-09 18:24:39 +01:00
|
|
|
IsAttachedTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2002-07-17 21:25:30 +01:00
|
|
|
/* check if we are not redoing effort */
|
|
|
|
static Int
|
|
|
|
p_attvar_bound(void)
|
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
return(IsVarTerm(t) &&
|
|
|
|
IsAttachedTerm(t) &&
|
2004-09-18 15:03:42 +01:00
|
|
|
!IsUnboundVar(&((attvar_record *)VarOfTerm(t))->Done));
|
2002-07-17 21:25:30 +01:00
|
|
|
}
|
|
|
|
|
2006-01-17 14:10:42 +00:00
|
|
|
static Int
|
|
|
|
p_void_term(void)
|
|
|
|
{
|
|
|
|
return Yap_unify(ARG1,TermVoidAtt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_free_term(void)
|
|
|
|
{
|
|
|
|
return Yap_unify(ARG1,TermFreeTerm);
|
|
|
|
}
|
|
|
|
|
2004-06-23 18:24:20 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_all_attvars(void)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_is_attvar(void)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_attvar_bound(void)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* COROUTINING */
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_InitAttVarPreds(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2004-05-13 21:54:58 +01:00
|
|
|
Term OldCurrentModule = CurrentModule;
|
2004-06-23 18:24:20 +01:00
|
|
|
CurrentModule = ATTRIBUTES_MODULE;
|
|
|
|
#ifdef COROUTINING
|
2001-04-09 20:54:03 +01:00
|
|
|
attas[attvars_ext].bind_op = WakeAttVar;
|
|
|
|
attas[attvars_ext].copy_term_op = CopyAttVar;
|
2001-12-17 18:31:11 +00:00
|
|
|
attas[attvars_ext].to_term_op = AttVarToTerm;
|
|
|
|
attas[attvars_ext].term_to_op = TermToAttVar;
|
2001-04-09 20:54:03 +01:00
|
|
|
attas[attvars_ext].mark_op = mark_attvar;
|
2005-09-09 18:24:39 +01:00
|
|
|
Yap_InitCPred("get_att", 4, p_get_att, SafePredFlag);
|
|
|
|
Yap_InitCPred("get_module_atts", 2, p_get_atts, SafePredFlag);
|
|
|
|
Yap_InitCPred("has_module_atts", 2, p_has_atts, SafePredFlag);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("get_all_atts", 2, p_get_all_atts, SafePredFlag);
|
2005-10-18 18:04:43 +01:00
|
|
|
Yap_InitCPred("get_all_swi_atts", 2, p_swi_all_atts, SafePredFlag);
|
2005-09-09 18:24:39 +01:00
|
|
|
Yap_InitCPred("free_att", 3, p_free_att, SafePredFlag);
|
|
|
|
Yap_InitCPred("put_att", 5, p_put_att, 0);
|
2005-10-31 18:12:51 +00:00
|
|
|
Yap_InitCPred("put_att_term", 5, p_put_att_term, 0);
|
2005-09-09 18:24:39 +01:00
|
|
|
Yap_InitCPred("put_module_atts", 2, p_put_atts, 0);
|
2005-10-18 18:04:43 +01:00
|
|
|
Yap_InitCPred("del_all_module_atts", 2, p_del_atts, 0);
|
2005-09-09 18:24:39 +01:00
|
|
|
Yap_InitCPred("rm_att", 4, p_rm_att, 0);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("bind_attvar", 1, p_bind_attvar, SafePredFlag);
|
2006-01-17 14:10:42 +00:00
|
|
|
Yap_InitCPred("modules_with_attributes", 2, p_modules_with_atts, SafePredFlag);
|
2005-09-09 18:24:39 +01:00
|
|
|
Yap_InitCPred("void_term", 1, p_void_term, SafePredFlag);
|
|
|
|
Yap_InitCPred("free_term", 1, p_free_term, SafePredFlag);
|
2004-06-23 18:24:20 +01:00
|
|
|
#endif /* COROUTINING */
|
2005-08-23 22:27:39 +01:00
|
|
|
Yap_InitCPred("all_attvars", 1, p_all_attvars, 0);
|
2004-05-13 21:54:58 +01:00
|
|
|
CurrentModule = OldCurrentModule;
|
2004-09-10 21:18:01 +01:00
|
|
|
Yap_InitCPred("attvar", 1, p_is_attvar, SafePredFlag|TestPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$att_bound", 1, p_attvar_bound, SafePredFlag|TestPredFlag|HiddenPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|