support for init, callbacks

This commit is contained in:
Vítor Santos Costa 2014-06-11 19:27:54 +01:00
parent 2455a1d83c
commit 0241151599
2 changed files with 815 additions and 657 deletions

View File

@ -2,290 +2,299 @@
#define YAP_CPP_INTERFACE 1 #define YAP_CPP_INTERFACE 1
#include "yapi.hh" #include "yapi.hh"
#include "SWI-Stream.h"
YAPAtomTerm::YAPAtomTerm(char *s) { // build string YAPAtomTerm::YAPAtomTerm(char *s) { // build string
BACKUP_H(); BACKUP_H();
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.c = s; inp.val.c = s;
inp.type = YAP_STRING_CHARS; inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_ATOM; out.type = YAP_STRING_ATOM;
if (Yap_CVT_Text(&inp, &out PASS_REGS)) if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) ); mk ( MkAtomTerm(out.val.a) );
else t = 0L; else t = 0L;
RECOVER_H(); RECOVER_H();
} }
YAPAtomTerm::YAPAtomTerm(char *s, size_t len) { // build string YAPAtomTerm::YAPAtomTerm(char *s, size_t len) { // build string
BACKUP_H(); BACKUP_H();
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.c = s; inp.val.c = s;
inp.type = YAP_STRING_CHARS; inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC; out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len; out.sz = len;
out.max = len; out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS)) if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) ); mk ( MkAtomTerm(out.val.a) );
else t = 0L; else t = 0L;
RECOVER_H(); RECOVER_H();
} }
YAPAtomTerm::YAPAtomTerm(wchar_t *s): YAPTerm() { // build string YAPAtomTerm::YAPAtomTerm(wchar_t *s): YAPTerm() { // build string
BACKUP_H(); BACKUP_H();
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.w = s; inp.val.w = s;
inp.type = YAP_STRING_WCHARS; inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_ATOM; out.type = YAP_STRING_ATOM;
if (Yap_CVT_Text(&inp, &out PASS_REGS)) if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) ); mk ( MkAtomTerm(out.val.a) );
else t = 0L; else t = 0L;
RECOVER_H(); RECOVER_H();
} }
YAPAtomTerm::YAPAtomTerm(wchar_t *s, size_t len) : YAPTerm() { // build string YAPAtomTerm::YAPAtomTerm(wchar_t *s, size_t len) : YAPTerm() { // build string
BACKUP_H(); BACKUP_H();
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.w = s; inp.val.w = s;
inp.type = YAP_STRING_WCHARS; inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC; out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len; out.sz = len;
out.max = len; out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS)) if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) ); mk ( MkAtomTerm(out.val.a) );
else t = 0L; else t = 0L;
RECOVER_H(); RECOVER_H();
} }
YAPStringTerm::YAPStringTerm(char *s) { // build string YAPStringTerm::YAPStringTerm(char *s) { // build string
BACKUP_H(); BACKUP_H();
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.c = s; inp.val.c = s;
inp.type = YAP_STRING_CHARS; inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_STRING; out.type = YAP_STRING_STRING;
if (Yap_CVT_Text(&inp, &out PASS_REGS)) if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t ); mk ( out.val.t );
else t = 0L; else t = 0L;
RECOVER_H(); RECOVER_H();
} }
YAPStringTerm::YAPStringTerm(char *s, size_t len) { // build string YAPStringTerm::YAPStringTerm(char *s, size_t len) { // build string
BACKUP_H(); BACKUP_H();
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.c = s; inp.val.c = s;
inp.type = YAP_STRING_CHARS; inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC; out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len; out.sz = len;
out.max = len; out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS)) if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t ); mk ( out.val.t );
else t = 0L; else t = 0L;
RECOVER_H(); RECOVER_H();
} }
YAPStringTerm::YAPStringTerm(wchar_t *s): YAPTerm() { // build string YAPStringTerm::YAPStringTerm(wchar_t *s): YAPTerm() { // build string
BACKUP_H(); BACKUP_H();
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.w = s; inp.val.w = s;
inp.type = YAP_STRING_WCHARS; inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_STRING; out.type = YAP_STRING_STRING;
if (Yap_CVT_Text(&inp, &out PASS_REGS)) if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t ); mk ( out.val.t );
else t = 0L; else t = 0L;
RECOVER_H(); RECOVER_H();
} }
YAPStringTerm::YAPStringTerm(wchar_t *s, size_t len) : YAPTerm() { // build string YAPStringTerm::YAPStringTerm(wchar_t *s, size_t len) : YAPTerm() { // build string
BACKUP_H(); BACKUP_H();
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.w = s; inp.val.w = s;
inp.type = YAP_STRING_WCHARS; inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC; out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len; out.sz = len;
out.max = len; out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS)) if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t ); mk ( out.val.t );
else t = 0L; else t = 0L;
RECOVER_H(); RECOVER_H();
} }
YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) : YAPTerm() { YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) : YAPTerm() {
UInt arity = ArityOfFunctor(f.f); UInt arity = ArityOfFunctor(f.f);
mk ( Yap_MkApplTerm( f.f, arity, (Term *)ts) ); mk ( Yap_MkApplTerm( f.f, arity, (Term *)ts) );
} }
YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() { YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() {
UInt arity = ArityOfFunctor(f.f); UInt arity = ArityOfFunctor(f.f);
mk ( Yap_MkNewApplTerm( f.f, arity) ); mk ( Yap_MkNewApplTerm( f.f, arity) );
} }
YAPTerm YAPApplTerm::getArg(int arg) { YAPTerm YAPApplTerm::getArg(int arg) {
return YAPTerm( ArgOfTerm(arg, gt() ) ); return YAPTerm( ArgOfTerm(arg, gt() ) );
} }
YAPFunctor YAPApplTerm::getFunctor() { YAPFunctor YAPApplTerm::getFunctor() {
return YAPFunctor( FunctorOfTerm( gt( )) ); return YAPFunctor( FunctorOfTerm( gt( )) );
} }
YAPPairTerm::YAPPairTerm(YAPTerm th, YAPTerm tl) : YAPTerm() { YAPPairTerm::YAPPairTerm(YAPTerm th, YAPTerm tl) : YAPTerm() {
CACHE_REGS CACHE_REGS
mk ( MkPairTerm( th.term(), tl.term() ) ); mk ( MkPairTerm( th.term(), tl.term() ) );
} }
YAPPairTerm::YAPPairTerm() : YAPTerm() { YAPPairTerm::YAPPairTerm() : YAPTerm() {
t = Yap_MkNewPairTerm( ); t = Yap_MkNewPairTerm( );
} }
void YAPTerm::mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 PASS_REGS); }
Term YAPTerm::gt() { CACHE_REGS return Yap_GetFromSlot( t PASS_REGS); }
YAP_tag_t YAPTerm::tag() { YAP_tag_t YAPTerm::tag() {
Term tt = gt( ); Term tt = gt( );
if (IsVarTerm(tt)) { if (IsVarTerm(tt)) {
CELL *pt = VarOfTerm(tt); CELL *pt = VarOfTerm(tt);
if (IsUnboundVar(pt)) { if (IsUnboundVar(pt)) {
CACHE_REGS CACHE_REGS
if (IsAttVar(pt)) if (IsAttVar(pt))
return YAP_TAG_ATT; return YAP_TAG_ATT;
return YAP_TAG_UNBOUND; return YAP_TAG_UNBOUND;
} }
return YAP_TAG_REF; return YAP_TAG_REF;
} }
if (IsPairTerm(tt)) if (IsPairTerm(tt))
return YAP_TAG_PAIR; return YAP_TAG_PAIR;
if (IsAtomOrIntTerm(tt)) { if (IsAtomOrIntTerm(tt)) {
if (IsAtomTerm(tt)) if (IsAtomTerm(tt))
return YAP_TAG_ATOM; return YAP_TAG_ATOM;
return YAP_TAG_INT; return YAP_TAG_INT;
} else { } else {
Functor f = FunctorOfTerm(tt); Functor f = FunctorOfTerm(tt);
if (IsExtensionFunctor(f)) { if (IsExtensionFunctor(f)) {
if (f == FunctorDBRef) { if (f == FunctorDBRef) {
return YAP_TAG_DBREF; return YAP_TAG_DBREF;
} }
if (f == FunctorLongInt) { if (f == FunctorLongInt) {
return YAP_TAG_LONG_INT; return YAP_TAG_LONG_INT;
} }
if (f == FunctorBigInt) { if (f == FunctorBigInt) {
big_blob_type bt = (big_blob_type)RepAppl(tt)[1]; big_blob_type bt = (big_blob_type)RepAppl(tt)[1];
switch (bt) { switch (bt) {
case BIG_INT: case BIG_INT:
return YAP_TAG_BIG_INT; return YAP_TAG_BIG_INT;
case BIG_RATIONAL: case BIG_RATIONAL:
return YAP_TAG_RATIONAL; return YAP_TAG_RATIONAL;
default: default:
return YAP_TAG_OPAQUE; return YAP_TAG_OPAQUE;
} }
} }
} }
return YAP_TAG_APPL; return YAP_TAG_APPL;
} }
} }
YAPTerm YAPTerm::deepCopy() { YAPTerm YAPTerm::deepCopy() {
Term tn; Term tn;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
tn = Yap_CopyTerm( gt() ); tn = Yap_CopyTerm( gt() );
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return new YAPTerm( tn ); return new YAPTerm( tn );
} }
bool YAPTerm::exactlyEqual(YAPTerm t1) { bool YAPTerm::exactlyEqual(YAPTerm t1) {
int out; int out;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
out = Yap_eq(gt(), t1.term()); out = Yap_eq(gt(), t1.term());
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
} }
bool YAPTerm::unify(YAPTerm t1) { bool YAPTerm::unify(YAPTerm t1) {
int out; int out;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
out = Yap_unify(gt(), t1.term()); out = Yap_unify(gt(), t1.term());
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
} }
bool YAPTerm::unifiable(YAPTerm t1) { bool YAPTerm::unifiable(YAPTerm t1) {
int out; int out;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
out = Yap_Unifiable(gt(), t1.term()); out = Yap_Unifiable(gt(), t1.term());
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
} }
bool YAPTerm::variant(YAPTerm t1) { bool YAPTerm::variant(YAPTerm t1) {
int out; int out;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
out = Yap_Variant(gt(), t1.term()); out = Yap_Variant(gt(), t1.term());
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
} }
intptr_t YAPTerm::hash(size_t sz, size_t depth, bool variant) { intptr_t YAPTerm::hash(size_t sz, size_t depth, bool variant) {
intptr_t out; intptr_t out;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
out = Yap_TermHash(gt(), sz, depth, variant) ; out = Yap_TermHash(gt(), sz, depth, variant) ;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
} }
char *YAPTerm::text(void) { char *YAPTerm::text(void) {
size_t sze = 4096, sz, length; size_t sze = 4096, length;
char *buf = new char[sze], *b; char *os;
int enc; int enc;
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "I LCL0+%p/t=(%d) %x", LCL0, t, LCL0[-15]) ; }
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if ((b = Yap_TermToString(gt(), buf, sze, &length, &enc, 0)) != buf) { if (!(os = Yap_HandleToString(t, sze, &length, &enc, 0))) {
if (b) free(b); { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "IIa LCL0+t=(%p) %x", LCL0, LCL0[-15]) ; }
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return NULL; return NULL;
} }
sz = length+1; { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "II LCL0+t=(%p) %x", LCL0, LCL0[-15]) ; }
char *os = new char[sz];
if (!os) {
RECOVER_MACHINE_REGS();
return NULL;
}
memcpy(os, buf, sz);
delete buf;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return os; return os;
} }
bool YAPListTerm::nil() {
CACHE_REGS
__android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "t=%d LCL0+t=(%p)", t, LCL0+t) ;
return gt() == TermNil;
}
YAPIntegerTerm::YAPIntegerTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
/* /*
YAPTerm *YAPTerm::vars() YAPTerm *YAPTerm::vars()
{ {
@ -295,185 +304,327 @@ YAPTerm *YAPTerm::vars()
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return lv; return lv;
} }
*/ */
YAPTerm::YAPTerm(void *ptr) { CACHE_REGS mk( MkIntegerTerm( (Int)ptr ) );}
YAPTerm::YAPTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
YAPTerm YAPListTerm::car()
{
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "t=%d LCL0+t=(%p)", t, LCL0+t) ; }
Term to = gt();
if (IsPairTerm(to))
return YAPTerm(HeadOfTerm(to));
else
return MkIntTerm(-1);
}
YAPVarTerm::YAPVarTerm() { CACHE_REGS mk( MkVarTerm( ) ); }
char *YAPAtom::name(void) { char *YAPAtom::name(void) {
if (IsWideAtom(a)) { if (IsWideAtom(a)) {
// return an UTF-8 version // return an UTF-8 version
size_t sz = 512; size_t sz = 512;
wchar_t * ptr = a->WStrOfAE; wchar_t * ptr = a->WStrOfAE;
int ch = -1; int ch = -1;
char *s = new char[sz], *op = s; char *s = new char[sz], *op = s;
while (ch) { while (ch) {
ch = *ptr++; ch = *ptr++;
utf8_put_char( op, ch ); utf8_put_char( op, ch );
} }
sz = strlen(s)+1; sz = strlen(s)+1;
char *os = new char[sz]; char *os = new char[sz];
memcpy(os, s, sz); memcpy(os, s, sz);
delete s; delete s;
return os; return os;
} else if (IsBlob(a)) { } else if (IsBlob(a)) {
PL_blob_t *type = RepBlobProp(a->PropsOfAE)->blob_t; PL_blob_t *type = RepBlobProp(a->PropsOfAE)->blob_t;
size_t sz = 512; size_t sz = 512;
if (type->write) { if (type->write) {
char *s = new char[sz]; char *s = new char[sz];
IOSTREAM *stream = Sopenmem(&s, &sz, "w"); IOSTREAM *stream = Sopenmem(&s, &sz, "w");
stream->encoding = ENC_UTF8; stream->encoding = ENC_UTF8;
atom_t at = YAP_SWIAtomFromAtom(AbsAtom(a)); atom_t at = YAP_SWIAtomFromAtom(AbsAtom(a));
type->write(stream, at, 0); type->write(stream, at, 0);
Sclose(stream); Sclose(stream);
popOutputContext(); popOutputContext();
sz = strlen(s)+1; sz = strlen(s)+1;
char *os = new char[sz]; char *os = new char[sz];
memcpy(os, s, sz); memcpy(os, s, sz);
delete s; delete s;
return os; return os;
} else { } else {
char *s = new char[sz]; char *s = new char[sz];
#if defined(__linux__) || defined(__APPLE__) #if defined(__linux__) || defined(__APPLE__)
snprintf(s, sz, "'%s'(%p)", AtomSWIStream->StrOfAE, a); snprintf(s, sz, "'%s'(%p)", AtomSWIStream->StrOfAE, a);
#else #else
snprintf(s, sz, "'%s'(0x%p)", AtomSWIStream->StrOfAE, a); snprintf(s, sz, "'%s'(0x%p)", AtomSWIStream->StrOfAE, a);
#endif #endif
char *os = new char[sz]; char *os = new char[sz];
memcpy(os, s, sz); memcpy(os, s, sz);
delete s; delete s;
return os; return os;
} }
} else { } else {
return a->StrOfAE; return a->StrOfAE;
} }
} }
YAPPredicate::YAPPredicate(const char *s, Term **outp, term_t &vnames) { YAPPredicate::YAPPredicate(const char *s, Term **outp, term_t &vnames) {
CACHE_REGS CACHE_REGS
vnames = Yap_NewSlots(1 PASS_REGS); vnames = Yap_NewSlots(1 PASS_REGS);
Term t = Yap_StringToTerm(s, strlen(s)+1, vnames), m; Term t = Yap_StringToTerm(s, strlen(s)+1, vnames);
t = Yap_StripModule(t, &m); ap = getPred( t, outp );
if (IsVarTerm(t) || IsNumTerm(t)) { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "OUT vnames=%d ap=%p LCL0=%p", vnames, ap, LCL0) ; }
ap = NULL;
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
*outp = NULL;
} else if (IsApplTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
*outp = RepAppl(t)+1;
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
*outp = RepPair(t);
}
} }
YAPPredicate::YAPPredicate(YAPAtom at) {
CACHE_REGS
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
}
YAPPredicate::YAPPredicate(YAPAtom at, arity_t arity) {
CACHE_REGS
if (arity) {
Functor f = Yap_MkFunctor(at.a, arity);
ap = RepPredProp(PredPropByFunc(f,CurrentModule));
} else {
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
}
}
/// auxiliary routine to find a predicate in the current module.
PredEntry *YAPPredicate::getPred( Term t, Term **outp ) {
CACHE_REGS
Term m = CurrentModule ;
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t)) {
ap = NULL;
if (outp) *outp = NULL;
}
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
if (outp) *outp = NULL;
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE));
if (outp) *outp = HR;
HR[0] = RepPair(t)[0];
HR[1] = m;
HR+=2;
} else {
Functor f = FunctorOfTerm(t);
if (IsExtensionFunctor(f)) {
ap = NULL;
if (outp) *outp = NULL;
}
ap = RepPredProp(PredPropByFunc(f, m));
if (outp) *outp = RepAppl(t)+1;
}
return ap;
}
YAPPredicate::YAPPredicate(YAPFunctor f) {
CACHE_REGS
ap = RepPredProp(PredPropByFunc(f.f,CurrentModule));
}
void void
YAPQuery::initQuery( Term *ts ) YAPQuery::initQuery( Term *ts )
{ {
CACHE_REGS CACHE_REGS
this->oq = (YAPQuery *)LOCAL_execution; this->oq = (YAPQuery *)LOCAL_execution;
LOCAL_execution = (struct open_query_struct *)this; LOCAL_execution = (struct open_query_struct *)this;
this->q_open=1; this->q_open=1;
this->q_state=0; this->q_state=0;
this->q_flags = 0; this->q_flags = 0;
this->q_g = ts; this->q_g = ts;
} }
void void
YAPQuery::initQuery( YAPTerm t[], arity_t arity ) YAPQuery::initQuery( YAPTerm t[], arity_t arity )
{ {
Term *ts = new Term[arity]; Term *ts = new Term[arity];
for (int i = 0; i < arity; i++) for (arity_t i = 0; i < arity; i++)
ts[i] = t[i].term(); ts[i] = t[i].term();
return initQuery( ts ); return initQuery( ts );
} }
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]): YAPPredicate(f, mod) YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]): YAPPredicate(f, mod)
{ {
/* ignore flags for now */ /* ignore flags for now */
initQuery( t , f.arity()); initQuery( t , f.arity());
} }
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm t[]): YAPPredicate(f) YAPQuery::YAPQuery(YAPFunctor f, YAPTerm t[]): YAPPredicate(f)
{ {
/* ignore flags for now */ /* ignore flags for now */
initQuery( t , f.arity()); initQuery( t , f.arity());
} }
YAPQuery::YAPQuery(YAPPredicate p, YAPTerm t[]): YAPPredicate(p.ap) YAPQuery::YAPQuery(YAPPredicate p, YAPTerm t[]): YAPPredicate(p.ap)
{ {
initQuery( t , p.ap->ArityOfPE); initQuery( t , p.ap->ArityOfPE);
} }
int YAPQuery::next() YAPListTerm YAPQuery::namedVars() {
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "vnames=%d LCL0=%p %x", vnames, LCL0, LCL0[vnames]) ; }
CACHE_REGS
Term o = Yap_GetFromSlot( vnames PASS_REGS );
return YAPListTerm( o );
}
bool YAPQuery::next()
{ {
CACHE_REGS CACHE_REGS
int result; int result;
if (this->q_open != 1) return 0; { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, " CALL vnames=%d ap=%p LCL0=%p %x", vnames, ap, LCL0, LCL0[vnames]) ; }
if (setjmp(((YAPQuery *)LOCAL_execution)->q_env)) if (this->q_open != 1) return false;
return 0; if (setjmp(((YAPQuery *)LOCAL_execution)->q_env))
// don't forget, on success these guys must create slots return false;
if (this->q_state == 0) { // don't forget, on success these guys must create slots
result = YAP_EnterGoal((YAP_PredEntryPtr)this->ap, this->q_g, &this->q_h); if (this->q_state == 0) {
} else { extern void toggle_low_level_trace(void);
LOCAL_AllowRestart = this->q_open; //toggle_low_level_trace();
result = YAP_RetryGoal(&this->q_h); result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)this->ap, this->q_g, &this->q_h);
} } else {
this->q_state = 1; LOCAL_AllowRestart = this->q_open;
if (result == 0) { result = (bool)YAP_RetryGoal(&this->q_h);
YAP_LeaveGoal(FALSE, &this->q_h); }
this->q_open = 0; this->q_state = 1;
} { CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "FAIL vnames=%d %d LCL0=(%p) %x", vnames, result, LCL0, LCL0[vnames]) ; }
return result; if (!result) {
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
}
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "SUCCESS vnames=%d ap=%p LCL0=(%p) %x", vnames, ap, LCL0, LCL0[vnames]) ; }
return result;
} }
void YAPQuery::cut() void YAPQuery::cut()
{ {
CACHE_REGS CACHE_REGS
if (this->q_open != 1 || this->q_state == 0) return; if (this->q_open != 1 || this->q_state == 0) return;
YAP_LeaveGoal(FALSE, &this->q_h); YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0; this->q_open = 0;
LOCAL_execution = (struct open_query_struct *)this->oq; LOCAL_execution = (struct open_query_struct *)this->oq;
} }
void YAPQuery::close() void YAPQuery::close()
{ {
CACHE_REGS CACHE_REGS
if (EX && !(this->q_flags & (PL_Q_CATCH_EXCEPTION))) { if (EX && !(this->q_flags & (PL_Q_CATCH_EXCEPTION))) {
EX = NULL; EX = NULL;
} }
/* need to implement backtracking here */ /* need to implement backtracking here */
if (this->q_open != 1 || this->q_state == 0) { if (this->q_open != 1 || this->q_state == 0) {
return; return;
} }
YAP_LeaveGoal(FALSE, &this->q_h); YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0; this->q_open = 0;
LOCAL_execution = (struct open_query_struct *)this->oq; LOCAL_execution = (struct open_query_struct *)this->oq;
} }
int YAPPredicate::call(YAPTerm t[]) int YAPPredicate::call(YAPTerm t[])
{ {
YAPQuery q = YAPQuery(*this, t); YAPQuery q = YAPQuery(*this, t);
int ret = q.next(); int ret = q.next();
q.cut(); q.cut();
q.close(); q.close();
return ret; return ret;
} }
YAPEngine::YAPEngine(YAPParams const& params) static YAPEngine *curren;
{ YAP_Init( (YAP_init_args *)&params.init_args ); }
#if __ANDROID__
YAPEngine::YAPEngine() #include <jni.h>
#include <string.h>
extern AAssetManager *assetManager;
extern char *Yap_AndroidBufp;
static size_t Yap_AndroidMax, Yap_AndroidSz;
extern void(*Yap_DisplayWithJava)(int c);
static void
displayWithJava(int c)
{ {
YAPParams *params = new YAPParams(); char *ptr = Yap_AndroidBufp;
YAP_Init( &params->init_args ); ptr[ Yap_AndroidSz++ ] = c;
if (Yap_AndroidMax-1 == Yap_AndroidSz) {
if (Yap_AndroidMax < 32*1024) {
Yap_AndroidMax *= 2;
} else {
Yap_AndroidMax += 32*1024;
}
Yap_AndroidBufp = (char *)realloc( ptr, Yap_AndroidMax);
}
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
if (c == '\n' ) {
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
__android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "after char %c:%s %p",c, Yap_AndroidBufp, curren);
curren->display(Yap_AndroidBufp);
Yap_AndroidSz = 0;
}
} }
#endif
YAPEngine::YAPEngine( char *savedState,
size_t stackSize,
size_t trailSize,
size_t maxStackSize,
size_t maxTrailSize,
char *libDir,
char *bootFile,
char *goal,
char *topLevel,
bool script,
bool fastBoot,
YAPCallback *cb): _callback(0)
{ // a single engine can be active
#if __ANDROID__
if (assetManager == NULL)
return;
Yap_DisplayWithJava = displayWithJava;
Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096);
Yap_AndroidBufp[0] = '\0';
Yap_AndroidSz = 0;
#endif
memset((void *)&init_args, 0, sizeof(init_args));
init_args.SavedState = savedState;
init_args.StackSize = stackSize;
init_args.TrailSize = trailSize;
init_args.MaxStackSize = maxStackSize;
init_args.MaxTrailSize = maxTrailSize;
init_args.YapLibDir = libDir;
init_args.YapPrologBootFile = bootFile;
init_args.YapPrologGoal = goal;
init_args.YapPrologTopLevelGoal = topLevel;
init_args.HaltAfterConsult = script;
init_args.FastBoot = fastBoot;
delYAPCallback();
if (cb) setYAPCallback(cb);
curren = this;
YAP_Init( &init_args );
}
YAPQuery *YAPEngine::query( char *s ) {
YAPQuery *n = new YAPQuery( s );
return n;
}

View File

@ -13,16 +13,26 @@
* with the swig interface language generator. It uses ideas from the old YAP * with the swig interface language generator. It uses ideas from the old YAP
* interface and from the SWI foreign language interface. * interface and from the SWI foreign language interface.
* *
* @{
*
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <config.h> #include <config.h>
extern "C" {
#ifdef __cplusplus
#define old_cplusplus __cplusplus
#undef __cplusplus
#endif
#if USE_GMP #if USE_GMP
#include <gmp.h> #include <gmp.h>
#endif #endif
#ifdef old_cplusplus
extern "C" { #define __cplusplus old_cplusplus
#undef old_cplusplus
#endif
#include "Yap.h" #include "Yap.h"
@ -60,348 +70,374 @@ extern "C" {
#include <windows.h> #include <windows.h>
#endif #endif
// taken from yap_structs.h // taken from yap_structs.h
#include "iopreds.h" #include "iopreds.h"
extern Term Yap_StringToTerm(const char *s, size_t len, term_t bindings); extern Term Yap_StringToTerm(const char *s, size_t len, term_t bindings);
// we cannot consult YapInterface.h, that conflicts with what we declare, though // we cannot consult YapInterface.h, that conflicts with what we declare, though
// it shouldn't // it shouldn't
} }
//#include <vector> //#include <vector>
class YAPEngine; class YAPEngine;
class YAPTermHandle;
class YAPAtom; class YAPAtom;
class YAPFunctor; class YAPFunctor;
class YAPApplTerm; class YAPApplTerm;
class YAPPairTerm; class YAPPairTerm;
class YAPQuery; class YAPQuery;
class TypeError {};
/** /**
* @brief Generic Prolog Term * @brief Generic Prolog Term
*/ */
class YAPTerm { class YAPTerm {
friend class YAPPredicate; friend class YAPPredicate;
friend class YAPTermHandle; friend class YAPApplTerm;
friend class YAPApplTerm; friend class YAPPairTerm;
friend class YAPPairTerm; friend class YAPListTerm;
protected: protected:
term_t t; handle_t t;
inline void mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 PASS_REGS); } void mk(Term t0);
inline Term gt(void) { CACHE_REGS return Yap_GetFromSlot( t PASS_REGS); } Term gt();
YAPTerm(Term tn) { mk( tn ); }
public: public:
YAPTerm() { mk(TermNil); } // do nothing constructor YAPTerm() { mk(TermNil); } // do nothing constructor
YAPTerm(void *ptr) { CACHE_REGS mk( MkIntegerTerm( (Int)ptr ) );} YAPTerm(intptr_t i);
YAPTerm(Term tn) { mk( tn ); } YAPTerm(void *ptr);
YAPTerm(char *s) { Term tp ; mk( YAP_ReadBuffer(s,&tp) ); } YAPTerm(char *s) { Term tp ; mk( YAP_ReadBuffer(s,&tp) ); }
/*~YAPTerm(void) { /*~YAPTerm(void) {
CACHE_REGS CACHE_REGS
Yap_RecoverSlots(1, t PASS_REGS); Yap_RecoverSlots(1, t PASS_REGS);
}*/ }*/
Term term() { return gt(); } Term term() { return gt(); }
YAP_tag_t tag(); YAP_tag_t tag();
YAPTerm deepCopy(); YAPTerm deepCopy();
//const YAPTerm *vars(); //const YAPTerm *vars();
bool exactlyEqual(YAPTerm t1); bool exactlyEqual(YAPTerm t1);
bool unify(YAPTerm t1); bool unify(YAPTerm t1);
bool unifiable(YAPTerm t1); bool unifiable(YAPTerm t1);
bool variant(YAPTerm t1); bool variant(YAPTerm t1);
intptr_t hash(size_t sz, size_t depth, bool variant); intptr_t hash(size_t sz, size_t depth, bool variant);
bool isVar() { return IsVarTerm( gt() ); } bool isVar() { return IsVarTerm( gt() ); }
bool isAtom() { return IsAtomTerm( gt() ); } bool isAtom() { return IsAtomTerm( gt() ); }
bool isInteger() { return IsIntegerTerm( gt() ); } bool isInteger() { return IsIntegerTerm( gt() ); }
bool isFloat() { return IsFloatTerm( gt() ); } bool isFloat() { return IsFloatTerm( gt() ); }
bool isCompound() { return !(IsVarTerm( gt() ) || IsNumTerm( gt() )); } bool isCompound() { return !(IsVarTerm( gt() ) || IsNumTerm( gt() )); }
bool isAppl() { return IsApplTerm( gt() ); } bool isAppl() { return IsApplTerm( gt() ); }
bool isPair() { return IsPairTerm( gt() ); } bool isPair() { return IsPairTerm( gt() ); }
bool isGround() { return Yap_IsGroundTerm( gt() ); } bool isGround() { return Yap_IsGroundTerm( gt() ); }
bool isList() { return Yap_IsListTerm( gt() ); } bool isList() { return Yap_IsListTerm( gt() ); }
bool isString() { return IsStringTerm( gt() ); } bool isString() { return IsStringTerm( gt() ); }
inline YAPTerm getArg(int i) { inline YAPTerm getArg(int i) {
Term t0 = gt(); Term t0 = gt();
if (IsApplTerm(t0)) if (IsApplTerm(t0))
return YAPTerm(ArgOfTerm(i, t0)); return YAPTerm(ArgOfTerm(i, t0));
else if (IsPairTerm(t0)) { else if (IsPairTerm(t0)) {
if (i==1) if (i==1)
return YAPTerm(HeadOfTerm(t0)); return YAPTerm(HeadOfTerm(t0));
if (i==2) if (i==2)
return YAPTerm(TailOfTerm(t0)); return YAPTerm(TailOfTerm(t0));
} }
return YAPTerm((Term)0); return YAPTerm((Term)0);
} }
char *text(); char *text();
}; };
/** /**
* @brief Variable Term * @brief Variable Term
*/ */
class YAPVarTerm: private YAPTerm { class YAPVarTerm: public YAPTerm {
YAPVarTerm(Term t) { if (IsVarTerm(t)) mk( t ); } YAPVarTerm(Term t) { if (IsVarTerm(t)) mk( t ); }
public: public:
YAPVarTerm() { CACHE_REGS mk( MkVarTerm( ) ); } YAPVarTerm();
CELL *getVar() { return VarOfTerm( gt() ); } CELL *getVar() { return VarOfTerm( gt() ); }
bool unbound() { return IsUnboundVar(VarOfTerm( gt() )); } bool unbound() { return IsUnboundVar(VarOfTerm( gt() )); }
}; };
/** /**
* @brief Compound Term * @brief Compound Term
*/ */
class YAPApplTerm: private YAPTerm { class YAPApplTerm: public YAPTerm {
friend class YAPTerm; friend class YAPTerm;
YAPApplTerm(Term t0) { mk(t0); } YAPApplTerm(Term t0) { mk(t0); }
public: public:
YAPApplTerm(YAPTerm t0) { mk(t0.term()); } YAPApplTerm(YAPTerm t0) { mk(t0.term()); }
YAPApplTerm(YAPFunctor f, YAPTerm ts[]); YAPApplTerm(YAPFunctor f, YAPTerm ts[]);
YAPApplTerm(YAPFunctor f); YAPApplTerm(YAPFunctor f);
YAPFunctor getFunctor(); YAPFunctor getFunctor();
YAPTerm getArg(int i); YAPTerm getArg(int i);
}; };
/** /**
* @brief List Constructor Term * @brief List Constructor Term
*/ */
class YAPPairTerm: private YAPTerm { class YAPPairTerm: public YAPTerm {
friend class YAPTerm; friend class YAPTerm;
YAPPairTerm(Term t0) { if (IsPairTerm(t0)) mk( t0 ); else mk(0); } YAPPairTerm(Term t0) { if (IsPairTerm(t0)) mk( t0 ); else mk(0); }
public: public:
YAPPairTerm(YAPTerm hd, YAPTerm tl); YAPPairTerm(YAPTerm hd, YAPTerm tl);
YAPPairTerm(); YAPPairTerm();
YAPTerm getHead() { return YAPTerm(HeadOfTerm( gt() )); } YAPTerm getHead() { return YAPTerm(HeadOfTerm( gt() )); }
YAPTerm getTail() { return YAPTerm(TailOfTerm( gt() )); } YAPTerm getTail() { return YAPTerm(TailOfTerm( gt() )); }
}; };
/** /**
* @brief Integer Term * @brief Integer Term
*/ */
class YAPIntegerTerm: private YAPTerm { class YAPIntegerTerm: public YAPTerm {
public: public:
YAPIntegerTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); } YAPIntegerTerm(intptr_t i);
intptr_t getInteger() { return IntegerOfTerm( gt() ); } intptr_t getInteger() { return IntegerOfTerm( gt() ); }
bool isTagged() { return IsIntTerm( gt() ); } bool isTagged() { return IsIntTerm( gt() ); }
}; };
class YAPListTerm: private YAPTerm { class YAPListTerm: public YAPTerm {
public: public:
/// Create a list term out of a standard term. Check if a valid operation. /// Create a list term out of a standard term. Check if a valid operation.
/// ///
/// @param[in] the term /// @param[in] the term
YAPListTerm(Term t0) { mk(t0); /* else type_error */ } YAPListTerm(Term t0) { mk(t0); /* else type_error */ }
/* /// Create a list term out of an array of terms. /* /// Create a list term out of an array of terms.
/// ///
/// @param[in] the array of terms /// @param[in] the array of terms
/// @param[in] the length of the array /// @param[in] the length of the array
YAPListTerm(YAPTerm ts[], size_t n); YAPListTerm(YAPTerm ts[], size_t n);
/// Create a list term out of an array of Prolog terms. */
/// // YAPListTerm( vector<YAPTerm> v );
/// @param[in] the array of terms
/// @param[in] the length of the array
YAPListTerm(Term ts[], size_t n);
*/
// YAPListTerm( vector<YAPTerm> v );
/// Return the number of elements in a list term. /// Return the number of elements in a list term.
size_t length() { Term *tailp; Term t1 = gt(); return Yap_SkipList(&t1, &tailp); } size_t length() { Term *tailp; Term t1 = gt(); return Yap_SkipList(&t1, &tailp); }
// vector<YAPTerm> toVector(); /// Extract the first element of a list.
/// Create an array of term out of a list. ///
/// /// @param[in] the list
/// @param[in] the list YAPTerm car();
YAPTerm car() { if (IsPairTerm(gt())) return YAPTerm(HeadOfTerm(gt())); else return YAPTerm((term_t)0); } /// Extract the tail elements of a list.
YAPListTerm cdr() { if (IsPairTerm(gt())) return YAPListTerm(TailOfTerm(gt())); else return YAPListTerm((term_t)0); } ///
bool nil() { return gt() == TermNil; } /// @param[in] the list
YAPListTerm cdr()
{
Term to = gt();
if (IsPairTerm( to ))
return YAPListTerm(TailOfTerm( to ));
else
return MkIntTerm(-1);
}
/// Check if the list is empty.
///
/// @param[in] the list
bool nil();
}; };
/** /**
* @brief Atom * @brief Atom
*/ */
class YAPAtom { class YAPAtom {
friend class YAPPredicate; friend class YAPPredicate;
friend class YAPFunctor; friend class YAPFunctor;
friend class YAPAtomTerm; friend class YAPAtomTerm;
Atom a; Atom a;
public: public:
YAPAtom( Atom at ) { a = at; } YAPAtom( Atom at ) { a = at; }
YAPAtom( char * s) { a = Yap_LookupAtom( s ); } YAPAtom( char * s) { a = Yap_LookupAtom( s ); }
YAPAtom( wchar_t * s) { a = Yap_LookupMaybeWideAtom( s ); } YAPAtom( wchar_t * s) { a = Yap_LookupMaybeWideAtom( s ); }
YAPAtom( char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); } YAPAtom( char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); }
YAPAtom( wchar_t * s, size_t len) { a = Yap_LookupMaybeWideAtomWithLength( s, len ); } YAPAtom( wchar_t * s, size_t len) { a = Yap_LookupMaybeWideAtomWithLength( s, len ); }
char *name(void); char *name(void);
}; };
/** /**
* @brief String Term * @brief String Term
*/ */
class YAPStringTerm: private YAPTerm { class YAPStringTerm: public YAPTerm {
public: public:
YAPStringTerm(char *s) ; YAPStringTerm(char *s) ;
YAPStringTerm(char *s, size_t len); YAPStringTerm(char *s, size_t len);
YAPStringTerm(wchar_t *s) ; YAPStringTerm(wchar_t *s) ;
YAPStringTerm(wchar_t *s, size_t len); YAPStringTerm(wchar_t *s, size_t len);
const char *getString() { return StringOfTerm( gt() ); } const char *getString() { return StringOfTerm( gt() ); }
}; };
/** /**
* @brief Atom Term * @brief Atom Term
*/ */
class YAPAtomTerm: private YAPTerm { class YAPAtomTerm: public YAPTerm {
public: public:
YAPAtomTerm(YAPAtom a): YAPTerm() { mk( MkAtomTerm(a.a) ); } YAPAtomTerm(YAPAtom a): YAPTerm() { mk( MkAtomTerm(a.a) ); }
YAPAtomTerm(Atom a): YAPTerm() { mk( MkAtomTerm(a) ); } YAPAtomTerm(Atom a): YAPTerm() { mk( MkAtomTerm(a) ); }
YAPAtomTerm(char *s) ; YAPAtomTerm(char *s) ;
YAPAtomTerm(char *s, size_t len); YAPAtomTerm(char *s, size_t len);
YAPAtomTerm(wchar_t *s) ; YAPAtomTerm(wchar_t *s) ;
YAPAtomTerm(wchar_t *s, size_t len); YAPAtomTerm(wchar_t *s, size_t len);
YAPAtom getAtom() { return YAPAtom(AtomOfTerm( gt() )); } YAPAtom getAtom() { return YAPAtom(AtomOfTerm( gt() )); }
}; };
/** /**
* @brief Functor * @brief YAPFunctor represents Prolog functors Name/Arity
*/ */
class YAPFunctor { class YAPFunctor {
friend class YAPApplTerm; friend class YAPApplTerm;
friend class YAPPredicate; friend class YAPPredicate;
Functor f; Functor f;
/// Constructor: receives Prolog functor and casts it to YAPFunctor
///
/// Notice that this is designed for internal use only.
YAPFunctor( Functor ff) { f = ff; }
public: public:
YAPFunctor( char * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupAtom( s ), arity ); } /// Constructor: receives name as a string plus arity
YAPFunctor( wchar_t * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupWideAtom( s ), arity ); } ///
YAPFunctor( YAPAtom at, arity_t arity) { f = Yap_MkFunctor( at.a, arity ); } /// Notice that this is designed for ISO-LATIN-1 right now
YAPFunctor( Functor ff) { f = ff; } YAPFunctor( char * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupAtom( s ), arity ); }
/// Constructor: receives name as a wide string plus arity
///
/// Notice that this is designed for UNICODE right now
YAPFunctor( wchar_t * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupWideAtom( s ), arity ); }
/// Constructor: receives name as an atom, plus arity
///
/// This is the default method, and the most popi;at
YAPFunctor( YAPAtom at, arity_t arity) { f = Yap_MkFunctor( at.a, arity ); }
Atom name(void) { /// Getter: extract name of functor as an atom
return NameOfFunctor( f ); ///
} /// this is for external usage.
YAPAtom name(void) {
return YAPAtom( NameOfFunctor( f ) );
}
arity_t arity(void) { /// Getter: extract arity of functor as an unsigned integer
return ArityOfFunctor( f ); ///
} /// this is for external usage.
arity_t arity(void) {
return ArityOfFunctor( f );
}
}; };
/** /**
* @brief Term Handle * @brief Predicates
*/ *
class YAPTermHandle { * This class interfaces with PredEntry in Yatom.g
long int handle;
public:
YAPTermHandle(Term t) {
CACHE_REGS
handle = Yap_InitSlot(t PASS_REGS);
}
void set(YAPTerm t) {
CACHE_REGS
Yap_PutInSlot(handle, t.t PASS_REGS);
}
};
/**
* @brief Predicate
*/ */
class YAPPredicate { class YAPPredicate {
friend class YAPQuery; friend class YAPQuery;
PredEntry *ap;
// trick to communicate t[] back to yapquery private:
YAPPredicate(const char *s, Term **outp, term_t& vnames ); PredEntry *ap;
inline YAPPredicate(Term t) {
CACHE_REGS /// auxiliary routine to find a predicate in the current module.
Term m = CurrentModule ; PredEntry *getPred( Term t, Term **outp ) ;
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t)) /// String constructor for predicates
ap = NULL; ///
if (IsAtomTerm(t)) { /// It also communicates the array of arguments t[] abd the array of variables
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m)); /// back to yapquery
} else if (IsApplTerm(t)) { YAPPredicate(const char *s, Term **outp, handle_t& vnames );
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
} else if (IsPairTerm(t)) { /// Term constructor for predicates
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE)); ///
} else { /// It is just a call to getPred
ap = NULL; inline YAPPredicate(Term t) {
} ap = getPred( t , NULL );
} }
inline YAPPredicate(PredEntry *pe) {
ap = pe; /// Cast constructor for predicates,
} /// if we have the implementation data.
///
inline YAPPredicate(PredEntry *pe) {
ap = pe;
}
public: public:
inline YAPPredicate(YAPFunctor f) {
CACHE_REGS /// Functor constructor for predicates
ap = RepPredProp(PredPropByFunc(f.f,CurrentModule)); ///
} /// Asssumes that we use the current module.
inline YAPPredicate(YAPFunctor f, YAPTerm mod) { YAPPredicate(YAPFunctor f);
ap = RepPredProp(PredPropByFunc(f.f,mod.t));
} /// Functor constructor for predicates, is given a specific module.
inline YAPPredicate(YAPAtom at, YAPTerm mod) { ///
ap = RepPredProp(PredPropByAtom(at.a,mod.t)); inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
} ap = RepPredProp(PredPropByFunc(f.f,mod.t));
inline YAPPredicate(YAPAtom at) { }
CACHE_REGS
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule)); /// Name/arity constructor for predicates.
} ///
inline YAPPredicate(YAPAtom at, arity_t arity, YAPTerm mod) { inline YAPPredicate(YAPAtom at, YAPTerm mod) {
if (arity) { ap = RepPredProp(PredPropByAtom(at.a,mod.t));
Functor f = Yap_MkFunctor(at.a, arity); }
ap = RepPredProp(PredPropByFunc(f,mod.t));
} else {
ap = RepPredProp(PredPropByAtom(at.a,mod.t)); /// Name/0 constructor for predicates.
} ///
} YAPPredicate(YAPAtom at);
inline YAPPredicate(YAPAtom at, arity_t arity) {
CACHE_REGS /// Mod:Name/Arity constructor for predicates.
if (arity) { ///
Functor f = Yap_MkFunctor(at.a, arity); inline YAPPredicate(YAPAtom at, arity_t arity, YAPTerm mod) {
ap = RepPredProp(PredPropByFunc(f,CurrentModule)); if (arity) {
} else { Functor f = Yap_MkFunctor(at.a, arity);
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule)); ap = RepPredProp(PredPropByFunc(f,mod.t));
} } else {
} ap = RepPredProp(PredPropByAtom(at.a,mod.t));
inline YAPPredicate(char *s) { }
CACHE_REGS }
Term t, tp, m = CurrentModule ;
t = YAP_ReadBuffer(s,&tp); /// Atom/Arity constructor for predicates.
t = Yap_StripModule(t, &m); ///
if (IsVarTerm(t) || IsNumTerm(t)) YAPPredicate(YAPAtom at, arity_t arity);
ap = NULL;
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m)); /// String constructor for predicates.
} else { ///
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m)); /// String is a Prolog term, we extract the main functor after considering the module qualifiers.
} inline YAPPredicate(char *s) {
} Term t, tp;
inline YAPPredicate(char *s, Term **outp) { t = YAP_ReadBuffer(s,&tp);
CACHE_REGS ap = getPred( t, NULL );
Term t, tp, m = CurrentModule ; }
t = YAP_ReadBuffer(s,&tp);
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t)) /// String constructor for predicates, also keeps arguments in tp[]
ap = NULL; ///
if (IsAtomTerm(t)) { /// String is a Prolog term, we extract the main functor after considering the module qualifiers.
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m)); inline YAPPredicate(char *s, Term **outp) {
*outp = NULL; Term t, tp;
} else if (IsApplTerm(t)) { t = YAP_ReadBuffer(s,&tp);
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m)); ap = getPred( t, NULL );
*outp = RepAppl(t)+1; }
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m)); /// meta-call this predicate, with arguments ts[]
*outp = RepPair(t); ///
} int call(YAPTerm ts[]);
}
int call(YAPTerm ts[]); /// module of a predicate
arity_t arity() { return ap->ArityOfPE; } ///
/// module of predicate /// notice that modules are currently treated as atoms, this should change.
/// YAPAtom module() {
/// notice that modules are currently treated as atoms, this should change. if (ap->ModuleOfPred == PROLOG_MODULE)
YAPAtom module() { if (ap->ModuleOfPred == PROLOG_MODULE) return YAPAtom(AtomProlog);
return YAPAtom(AtomProlog); else
else return YAPAtom(AtomOfTerm(ap->ModuleOfPred));
return YAPAtom(AtomOfTerm(ap->ModuleOfPred)); } }
/// name of predicate
/// /// name of predicate
/// notice that we return the atom, not a string. ///
YAPAtom name() { if (ap->ArityOfPE) /// notice that we return the atom, not a string.
return YAPAtom((Atom)ap->FunctorOfPred); YAPAtom name() { if (ap->ArityOfPE)
else return YAPAtom((Atom)ap->FunctorOfPred);
return YAPAtom(NameOfFunctor(ap->FunctorOfPred)); else
} return YAPAtom(NameOfFunctor(ap->FunctorOfPred));
}
/// arity of predicate
///
/// we return a positive number.
arity_t getArity() { return ap->ArityOfPE; }
}; };
/** /**
@ -410,129 +446,100 @@ public:
* interface to a YAP Query; * interface to a YAP Query;
* uses an SWI-like status info internally. * uses an SWI-like status info internally.
*/ */
class YAPQuery: private YAPPredicate { class YAPQuery: public YAPPredicate {
int q_open; int q_open;
int q_state; int q_state;
Term *q_g; Term *q_g;
yamop *q_p, *q_cp; yamop *q_p, *q_cp;
jmp_buf q_env; jmp_buf q_env;
int q_flags; int q_flags;
YAP_dogoalinfo q_h; YAP_dogoalinfo q_h;
YAPQuery *oq; YAPQuery *oq;
term_t vnames; handle_t vnames;
void initQuery( Term ts[] ); void initQuery( Term ts[] );
void initQuery( YAPTerm t[], arity_t arity ); void initQuery( YAPTerm t[], arity_t arity );
public: public:
/// main constructor, uses a predicate and an array of terms /// main constructor, uses a predicate and an array of terms
/// ///
/// It is given a YAPPredicate _p_ , and an array of terms that must have at least /// It is given a YAPPredicate _p_ , and an array of terms that must have at least
/// the same arity as the functor. /// the same arity as the functor.
YAPQuery(YAPPredicate p, YAPTerm t[]); YAPQuery(YAPPredicate p, YAPTerm t[]);
/// full constructor, /// full constructor,
/// ///
/// It is given a functor, module, and an array of terms that must have at least /// It is given a functor, module, and an array of terms that must have at least
/// the same arity as the functor. /// the same arity as the functor.
YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]); YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]);
/// functor/term constructor, /// functor/term constructor,
/// ///
/// It is given a functor, and an array of terms that must have at least /// It is given a functor, and an array of terms that must have at least
/// the same arity as the functor. Works within the current module. /// the same arity as the functor. Works within the current module.
YAPQuery(YAPFunctor f, YAPTerm t[]); YAPQuery(YAPFunctor f, YAPTerm t[]);
/// string constructor with varnames /// string constructor with varnames
/// ///
/// It is given a string, calls the parser and obtains a Prolog term that should be a callable /// It is given a string, calls the parser and obtains a Prolog term that should be a callable
/// goal and a list of variables. Useful for top-level simulation. Works within the current module. /// goal and a list of variables. Useful for top-level simulation. Works within the current module.
inline YAPQuery(char *s): YAPPredicate(s, &this->q_g, vnames) inline YAPQuery(char *s): YAPPredicate(s, &this->q_g, vnames)
{ {
Term *ts = this->q_g; Term *ts = this->q_g;
initQuery( ts ); initQuery( ts );
} }
/// first query /// first query
/// ///
/// actually implemented by calling the next(); /// actually implemented by calling the next();
inline int first() { return next(); } inline bool first() { return next(); }
/// ask for the next solution of the current query /// ask for the next solution of the current query
/// same call for every solution /// same call for every solution
int next(); bool next();
/// remove alternatives in the current search space, and finish the current query /// remove alternatives in the current search space, and finish the current query
void cut(); void cut();
/// finish the current query: undo all bindings. /// finish the current query: undo all bindings.
void close(); void close();
/// query variables. /// query variables.
YAPListTerm namedVars() { CACHE_REGS Term o = Yap_GetFromSlot( vnames PASS_REGS ); return YAPListTerm( o ); } YAPListTerm namedVars();
}; };
// Java support
class YAPParams; // This class implements a callback Prolog-side
class YAPCallback {
public:
virtual ~YAPCallback() { printf("~YAPCallback\n"); }
virtual void run() { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "callback"); }
virtual void displayInWindow(char *s) { }
};
/** /**
* @brief YAP Constructor * @brief YAP Engine: takes care of constructing an execution environment where we can go executing goals
* *
*/ */
class YAPEngine { class YAPEngine {
private:
YAPCallback *_callback;
char **buf;
YAP_init_args init_args;
public: public:
YAPEngine(YAPParams const& params); /// construct a new engine YAPEngine(char *savedState = NULL,
YAPEngine(); /// construct a new engine, no arguments size_t stackSize = 0,
YAPQuery *query( char *s ) { return new YAPQuery( s ); } /// build a query on the engine size_t trailSize = 0,
size_t maxStackSize = 0,
size_t maxTrailSize = 0,
char *libDir = NULL,
char *bootFile = NULL,
char *goal = NULL,
char *topLevel = NULL,
bool script = FALSE,
bool fastBoot = FALSE,
YAPCallback *callback=NULL); /// construct a new engine, including aaccess to callbacks
~YAPEngine() { delYAPCallback(); } /// kill engine
void delYAPCallback() { _callback = 0; } /// remove current callback
void setYAPCallback(YAPCallback *cb) { delYAPCallback(); _callback = cb; __android_log_print(ANDROID_LOG_INFO, __FILE__, "after loading startup %p",cb); } /// set a new callback
void call() { if (_callback) _callback->run(); } /// execute the callback.
void display( char *s) { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "bef calling disp %s %p",s, _callback); if (_callback) _callback->displayInWindow(s); } /// execute the callback.
YAPQuery *query( char *s ); /// build a query on the engine
}; };
/** /*
* @brief Parameters for YAP Constructor * @}
* *
*/ */
class YAPParams {
friend class YAPEngine;
YAP_init_args init_args;
public:
YAPParams();
// sets all the default values for each data member
YAPParams& savedState( char * f);
YAPParams& stackSize(size_t sz);
YAPParams& trailSize(size_t sz);
YAPParams& maxStackSize(size_t sz);
YAPParams& maxTrailSize(size_t sz);
YAPParams& libDir(char *p);
YAPParams& bootFile(char *f);
YAPParams& goal(char *g);
YAPParams& topLevel(char *g);
YAPParams& script(bool v);
YAPParams& fastBoot(bool v);
};
inline YAPParams::YAPParams()
{ Yap_InitDefaults( &init_args, NULL ); }
inline YAPParams& YAPParams::savedState( char * f)
{ init_args.SavedState = f; return *this; }
inline YAPParams& YAPParams::stackSize(size_t sz)
{ init_args.StackSize = sz; return *this; }
inline YAPParams& YAPParams::trailSize(size_t sz)
{ init_args.TrailSize = sz; return *this; }
inline YAPParams& YAPParams::maxStackSize(size_t sz)
{ init_args.MaxStackSize = sz; return *this; }
inline YAPParams& YAPParams::maxTrailSize(size_t sz)
{ init_args.MaxTrailSize = sz; return *this; }
inline YAPParams& YAPParams::libDir(char *p)
{ init_args.YapLibDir = p; return *this; }
inline YAPParams& YAPParams::bootFile(char *f)
{ init_args.YapPrologBootFile = f; return *this; }
inline YAPParams& YAPParams::goal(char *g)
{ init_args.YapPrologGoal = g; return *this; }
inline YAPParams& YAPParams::topLevel(char *g)
{ init_args.YapPrologTopLevelGoal = g; return *this; }
inline YAPParams& YAPParams::script(bool v)
{ init_args.HaltAfterConsult = v; return *this; }
inline YAPParams& YAPParams::fastBoot(bool v)
{ init_args.FastBoot = v; return *this; }