2014-04-28 11:47:09 +01:00
|
|
|
|
|
|
|
#define YAP_CPP_INTERFACE 1
|
|
|
|
|
|
|
|
#include "yapi.hh"
|
2014-06-04 22:08:37 +01:00
|
|
|
|
2015-06-18 00:52:31 +01:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
#include "YapInterface.h"
|
|
|
|
#include "blobs.h"
|
|
|
|
|
|
|
|
char *Yap_TermToString(Term t, char *s, size_t sz, size_t *length, encoding_t encoding, int flags);
|
|
|
|
|
|
|
|
void YAP_UserCPredicate(const char *, YAP_UserCPred, YAP_Arity arity);
|
|
|
|
void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, YAP_Arity, YAP_Term);
|
|
|
|
void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred, YAP_Arity, unsigned int);
|
|
|
|
}
|
2014-06-04 22:08:37 +01:00
|
|
|
|
2015-02-09 01:52:10 +00:00
|
|
|
|
2014-06-19 15:06:15 +01:00
|
|
|
YAPAtomTerm::YAPAtomTerm(char *s) { // build string
|
2014-06-11 19:27:54 +01:00
|
|
|
BACKUP_H();
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
2015-02-09 01:52:10 +00:00
|
|
|
seq_tv_t inp, out;
|
2014-06-11 19:27:54 +01:00
|
|
|
inp.val.c = s;
|
|
|
|
inp.type = YAP_STRING_CHARS;
|
|
|
|
out.type = YAP_STRING_ATOM;
|
|
|
|
if (Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
mk ( MkAtomTerm(out.val.a) );
|
|
|
|
else t = 0L;
|
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 15:06:15 +01:00
|
|
|
YAPAtomTerm::YAPAtomTerm(char *s, size_t len) { // build string
|
2014-06-11 19:27:54 +01:00
|
|
|
BACKUP_H();
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
|
|
|
inp.val.c = s;
|
|
|
|
inp.type = YAP_STRING_CHARS;
|
|
|
|
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
|
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
if (Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
mk ( MkAtomTerm(out.val.a) );
|
|
|
|
else t = 0L;
|
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2014-06-19 15:06:15 +01:00
|
|
|
YAPAtomTerm::YAPAtomTerm(wchar_t *s): YAPTerm() { // build string
|
2014-06-11 19:27:54 +01:00
|
|
|
BACKUP_H();
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
|
|
|
inp.val.w = s;
|
|
|
|
inp.type = YAP_STRING_WCHARS;
|
|
|
|
out.type = YAP_STRING_ATOM;
|
|
|
|
if (Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
mk ( MkAtomTerm(out.val.a) );
|
|
|
|
else t = 0L;
|
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 15:06:15 +01:00
|
|
|
YAPAtomTerm::YAPAtomTerm(wchar_t *s, size_t len) : YAPTerm() { // build string
|
2014-06-11 19:27:54 +01:00
|
|
|
BACKUP_H();
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
|
|
|
inp.val.w = s;
|
|
|
|
inp.type = YAP_STRING_WCHARS;
|
|
|
|
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
|
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
if (Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
mk ( MkAtomTerm(out.val.a) );
|
|
|
|
else t = 0L;
|
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 15:06:15 +01:00
|
|
|
YAPStringTerm::YAPStringTerm(char *s) { // build string
|
2014-06-11 19:27:54 +01:00
|
|
|
BACKUP_H();
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
|
|
|
inp.val.c = s;
|
|
|
|
inp.type = YAP_STRING_CHARS;
|
|
|
|
out.type = YAP_STRING_STRING;
|
|
|
|
if (Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
mk ( out.val.t );
|
|
|
|
else t = 0L;
|
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 15:06:15 +01:00
|
|
|
YAPStringTerm::YAPStringTerm(char *s, size_t len) { // build string
|
2014-06-11 19:27:54 +01:00
|
|
|
BACKUP_H();
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
seq_tv_t inp, out;
|
|
|
|
inp.val.c = s;
|
|
|
|
inp.type = YAP_STRING_CHARS;
|
|
|
|
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
|
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
if (Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
mk ( out.val.t );
|
|
|
|
else t = 0L;
|
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2014-06-19 15:06:15 +01:00
|
|
|
YAPStringTerm::YAPStringTerm(wchar_t *s): YAPTerm() { // build string
|
2014-06-11 19:27:54 +01:00
|
|
|
BACKUP_H();
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
seq_tv_t inp, out;
|
|
|
|
inp.val.w = s;
|
|
|
|
inp.type = YAP_STRING_WCHARS;
|
|
|
|
out.type = YAP_STRING_STRING;
|
|
|
|
if (Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
mk ( out.val.t );
|
|
|
|
else t = 0L;
|
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 15:06:15 +01:00
|
|
|
YAPStringTerm::YAPStringTerm(wchar_t *s, size_t len) : YAPTerm() { // build string
|
2014-06-11 19:27:54 +01:00
|
|
|
BACKUP_H();
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
2015-04-13 13:28:17 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
seq_tv_t inp, out;
|
|
|
|
inp.val.w = s;
|
|
|
|
inp.type = YAP_STRING_WCHARS;
|
|
|
|
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
|
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
if (Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
mk ( out.val.t );
|
|
|
|
else t = 0L;
|
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) : YAPTerm() {
|
2015-04-13 13:28:17 +01:00
|
|
|
BACKUP_H();
|
2014-06-11 19:27:54 +01:00
|
|
|
UInt arity = ArityOfFunctor(f.f);
|
|
|
|
mk ( Yap_MkApplTerm( f.f, arity, (Term *)ts) );
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() {
|
2015-04-13 13:28:17 +01:00
|
|
|
BACKUP_H();
|
2014-06-11 19:27:54 +01:00
|
|
|
UInt arity = ArityOfFunctor(f.f);
|
|
|
|
mk ( Yap_MkNewApplTerm( f.f, arity) );
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 22:08:37 +01:00
|
|
|
YAPTerm YAPApplTerm::getArg(int arg) {
|
2015-04-13 13:28:17 +01:00
|
|
|
BACKUP_H();
|
2015-03-11 22:18:00 +00:00
|
|
|
YAPTerm to = YAPTerm( ArgOfTerm(arg, gt() ) );
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_H();
|
2015-03-11 22:18:00 +00:00
|
|
|
return to;
|
2014-04-29 11:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
YAPFunctor YAPApplTerm::getFunctor() {
|
2014-06-11 19:27:54 +01:00
|
|
|
return YAPFunctor( FunctorOfTerm( gt( )) );
|
2014-04-29 11:45:19 +01:00
|
|
|
}
|
|
|
|
|
2014-04-28 11:47:09 +01:00
|
|
|
YAPPairTerm::YAPPairTerm(YAPTerm th, YAPTerm tl) : YAPTerm() {
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
2015-04-13 13:28:17 +01:00
|
|
|
BACKUP_H();
|
2014-06-11 19:27:54 +01:00
|
|
|
mk ( MkPairTerm( th.term(), tl.term() ) );
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
YAPPairTerm::YAPPairTerm() : YAPTerm() {
|
2015-04-13 13:28:17 +01:00
|
|
|
BACKUP_H();
|
2014-06-11 19:27:54 +01:00
|
|
|
t = Yap_MkNewPairTerm( );
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_H();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
void YAPTerm::mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 ); }
|
2014-06-11 19:27:54 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
Term YAPTerm::gt() { CACHE_REGS return Yap_GetFromSlot( t ); }
|
2014-06-11 19:27:54 +01:00
|
|
|
|
2014-04-28 11:47:09 +01:00
|
|
|
YAP_tag_t YAPTerm::tag() {
|
2014-06-04 22:08:37 +01:00
|
|
|
Term tt = gt( );
|
2014-06-11 19:27:54 +01:00
|
|
|
if (IsVarTerm(tt)) {
|
|
|
|
CELL *pt = VarOfTerm(tt);
|
|
|
|
if (IsUnboundVar(pt)) {
|
|
|
|
CACHE_REGS
|
|
|
|
if (IsAttVar(pt))
|
|
|
|
return YAP_TAG_ATT;
|
|
|
|
return YAP_TAG_UNBOUND;
|
|
|
|
}
|
|
|
|
return YAP_TAG_REF;
|
|
|
|
}
|
|
|
|
if (IsPairTerm(tt))
|
|
|
|
return YAP_TAG_PAIR;
|
|
|
|
if (IsAtomOrIntTerm(tt)) {
|
|
|
|
if (IsAtomTerm(tt))
|
2015-04-13 13:28:17 +01:00
|
|
|
return YAP_TAG_ATOM;
|
2014-06-11 19:27:54 +01:00
|
|
|
return YAP_TAG_INT;
|
|
|
|
} else {
|
2015-03-16 17:25:09 +00:00
|
|
|
Functor f = FunctorOfTerm(tt);
|
2014-06-11 19:27:54 +01:00
|
|
|
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
if (f == FunctorDBRef) {
|
|
|
|
return YAP_TAG_DBREF;
|
|
|
|
}
|
|
|
|
if (f == FunctorLongInt) {
|
|
|
|
return YAP_TAG_LONG_INT;
|
|
|
|
}
|
|
|
|
if (f == FunctorBigInt) {
|
|
|
|
big_blob_type bt = (big_blob_type)RepAppl(tt)[1];
|
|
|
|
switch (bt) {
|
|
|
|
case BIG_INT:
|
|
|
|
return YAP_TAG_BIG_INT;
|
|
|
|
case BIG_RATIONAL:
|
|
|
|
return YAP_TAG_RATIONAL;
|
|
|
|
default:
|
|
|
|
return YAP_TAG_OPAQUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return YAP_TAG_APPL;
|
|
|
|
}
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
YAPTerm YAPTerm::deepCopy() {
|
2014-06-11 19:27:54 +01:00
|
|
|
Term tn;
|
|
|
|
BACKUP_MACHINE_REGS();
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
tn = Yap_CopyTerm( gt() );
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return new YAPTerm( tn );
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool YAPTerm::exactlyEqual(YAPTerm t1) {
|
2014-06-11 19:27:54 +01:00
|
|
|
int out;
|
|
|
|
BACKUP_MACHINE_REGS();
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
out = Yap_eq(gt(), t1.term());
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2014-04-28 11:47:09 +01:00
|
|
|
bool YAPTerm::unify(YAPTerm t1) {
|
2014-06-11 19:27:54 +01:00
|
|
|
int out;
|
|
|
|
BACKUP_MACHINE_REGS();
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
out = Yap_unify(gt(), t1.term());
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool YAPTerm::unifiable(YAPTerm t1) {
|
2014-06-11 19:27:54 +01:00
|
|
|
int out;
|
|
|
|
BACKUP_MACHINE_REGS();
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
out = Yap_Unifiable(gt(), t1.term());
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool YAPTerm::variant(YAPTerm t1) {
|
2014-06-11 19:27:54 +01:00
|
|
|
int out;
|
|
|
|
BACKUP_MACHINE_REGS();
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
out = Yap_Variant(gt(), t1.term());
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2014-06-22 17:35:05 +01:00
|
|
|
intptr_t YAPTerm::hashTerm(size_t sz, size_t depth, bool variant) {
|
2014-06-04 22:08:37 +01:00
|
|
|
intptr_t out;
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
out = Yap_TermHash(gt(), sz, depth, variant) ;
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2015-03-11 22:18:00 +00:00
|
|
|
const char *YAPTerm::text() {
|
2015-06-18 00:52:31 +01:00
|
|
|
CACHE_REGS
|
2014-06-11 19:27:54 +01:00
|
|
|
size_t sze = 4096, length;
|
2015-06-18 00:52:31 +01:00
|
|
|
char *os = new char[4097];
|
|
|
|
encoding_t enc = LOCAL_encoding;
|
2014-06-04 22:08:37 +01:00
|
|
|
|
|
|
|
BACKUP_MACHINE_REGS();
|
2015-06-18 00:52:31 +01:00
|
|
|
if (!(os = Yap_TermToString(t, os, sze, &length, enc, 0))) { RECOVER_MACHINE_REGS();
|
2014-09-19 20:20:22 +01:00
|
|
|
return (char *)NULL;
|
|
|
|
}
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
const char *YAPQuery::text() {
|
2014-09-19 20:20:22 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
return goal.text();
|
2014-06-04 22:08:37 +01:00
|
|
|
}
|
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
|
|
|
|
YAPIntegerTerm::YAPIntegerTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-04 22:08:37 +01:00
|
|
|
/*
|
|
|
|
YAPTerm *YAPTerm::vars()
|
|
|
|
{
|
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
CACHE_REGS
|
|
|
|
YAPPairTerm lv = YAPPairTerm(Yap_TermVariables(gt(), 0 PASS_REGS));
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return lv;
|
|
|
|
}
|
2014-06-11 19:27:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
Term to = gt();
|
2015-04-13 13:28:17 +01:00
|
|
|
{ LOG( "to=%lx", to) ; }
|
2014-06-11 19:27:54 +01:00
|
|
|
if (IsPairTerm(to))
|
|
|
|
return YAPTerm(HeadOfTerm(to));
|
|
|
|
else
|
2015-03-16 17:25:09 +00:00
|
|
|
throw YAPError::YAP_DOMAIN_ERROR;
|
2014-06-11 19:27:54 +01:00
|
|
|
}
|
|
|
|
|
2015-03-16 17:25:09 +00:00
|
|
|
YAPVarTerm::YAPVarTerm() { CACHE_REGS mk( MkVarTerm( ) ); }
|
2014-06-04 22:08:37 +01:00
|
|
|
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2014-06-19 15:06:15 +01:00
|
|
|
char *YAPAtom::getName(void) {
|
2014-06-11 19:27:54 +01:00
|
|
|
if (IsWideAtom(a)) {
|
|
|
|
// return an UTF-8 version
|
|
|
|
size_t sz = 512;
|
|
|
|
wchar_t * ptr = a->WStrOfAE;
|
|
|
|
int ch = -1;
|
|
|
|
char *s = new char[sz], *op = s;
|
|
|
|
while (ch) {
|
|
|
|
ch = *ptr++;
|
|
|
|
utf8_put_char( op, ch );
|
|
|
|
}
|
|
|
|
sz = strlen(s)+1;
|
|
|
|
char *os = new char[sz];
|
|
|
|
memcpy(os, s, sz);
|
2014-06-14 10:27:39 +01:00
|
|
|
delete[] s;
|
2014-06-11 19:27:54 +01:00
|
|
|
return os;
|
|
|
|
} else if (IsBlob(a)) {
|
2015-06-18 00:52:31 +01:00
|
|
|
size_t sz = 1024;
|
|
|
|
char *s = new char [sz+1];
|
|
|
|
return Yap_blob_to_string( RepAtom(a) , s, sz);
|
2014-06-11 19:27:54 +01:00
|
|
|
} else {
|
|
|
|
return a->StrOfAE;
|
|
|
|
}
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
void YAPQuery::initOpenQ() {
|
|
|
|
CACHE_REGS
|
|
|
|
oq = LOCAL_execution;
|
|
|
|
LOCAL_execution = this;
|
|
|
|
q_open=1;
|
|
|
|
q_state=0;
|
2015-06-18 00:52:31 +01:00
|
|
|
q_flags = true ; //PL_Q_PASS_EXCEPTION;
|
2015-04-13 13:28:17 +01:00
|
|
|
|
|
|
|
q_p = P;
|
|
|
|
q_cp = CP;
|
|
|
|
}
|
|
|
|
|
2014-04-29 11:45:19 +01:00
|
|
|
void
|
2015-04-13 13:28:17 +01:00
|
|
|
YAPQuery::initQuery( Term t )
|
2014-04-28 11:47:09 +01:00
|
|
|
{
|
2015-04-13 13:28:17 +01:00
|
|
|
CACHE_REGS
|
|
|
|
BACKUP_MACHINE_REGS();
|
2015-04-15 11:21:15 +01:00
|
|
|
arity_t arity = ap->ArityOfPE;
|
2015-04-13 13:28:17 +01:00
|
|
|
goal = YAPTerm( t );
|
|
|
|
if (arity) {
|
2015-04-15 11:21:15 +01:00
|
|
|
q_g = Yap_InitSlots( arity, RepAppl(t) + 1);
|
2015-04-13 13:28:17 +01:00
|
|
|
} else {
|
|
|
|
q_g = 0;
|
|
|
|
}
|
|
|
|
initOpenQ();
|
|
|
|
RECOVER_MACHINE_REGS();
|
2014-06-04 22:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-13 13:28:17 +01:00
|
|
|
YAPQuery::initQuery( YAPTerm ts[], arity_t arity )
|
2014-06-04 22:08:37 +01:00
|
|
|
{
|
2015-04-13 13:28:17 +01:00
|
|
|
CACHE_REGS
|
2014-06-04 22:08:37 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
if (arity) {
|
|
|
|
q_g = Yap_NewSlots( arity );
|
2015-04-15 11:21:15 +01:00
|
|
|
for (arity_t i=0; i < arity; i++) {
|
2015-04-13 13:28:17 +01:00
|
|
|
Yap_PutInSlot(q_g+i, ts[i].term() PASS_REGS);
|
|
|
|
}
|
|
|
|
Term t = Yap_MkApplTerm(ap->FunctorOfPred, ap->ArityOfPE, Yap_AddressFromSlot(q_g));
|
|
|
|
goal = YAPTerm( t );
|
|
|
|
} else {
|
|
|
|
q_g = 0;
|
|
|
|
goal = YAPTerm( MkAtomTerm((Atom)ap->FunctorOfPred) );
|
|
|
|
}
|
|
|
|
initOpenQ();
|
|
|
|
RECOVER_MACHINE_REGS();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2014-04-29 11:45:19 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[]): YAPPredicate(f, mod)
|
2014-04-28 11:47:09 +01:00
|
|
|
{
|
2014-06-11 19:27:54 +01:00
|
|
|
/* ignore flags for now */
|
2015-04-13 13:28:17 +01:00
|
|
|
initQuery( ts , f.arity());
|
2014-04-29 11:45:19 +01:00
|
|
|
}
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]): YAPPredicate(f)
|
2014-04-29 11:45:19 +01:00
|
|
|
{
|
2014-06-11 19:27:54 +01:00
|
|
|
/* ignore flags for now */
|
2015-04-13 13:28:17 +01:00
|
|
|
initQuery( ts , f.arity());
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
YAPQuery::YAPQuery(YAPPredicate p, YAPTerm ts[]): YAPPredicate(p.ap)
|
2014-04-28 11:47:09 +01:00
|
|
|
{
|
2015-04-13 13:28:17 +01:00
|
|
|
initQuery( ts , p.ap->ArityOfPE);
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
YAPListTerm YAPQuery::namedVars() {
|
|
|
|
CACHE_REGS
|
2015-04-13 13:28:17 +01:00
|
|
|
Term o = Yap_GetFromSlot( vnames );
|
2014-06-11 19:27:54 +01:00
|
|
|
return YAPListTerm( o );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool YAPQuery::next()
|
2014-04-28 11:47:09 +01:00
|
|
|
{
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
|
|
|
int result;
|
2015-04-13 13:28:17 +01:00
|
|
|
|
|
|
|
BACKUP_MACHINE_REGS();
|
2014-09-19 20:20:22 +01:00
|
|
|
if (q_open != 1) return false;
|
2015-04-13 13:28:17 +01:00
|
|
|
if (setjmp(q_env))
|
2014-06-11 19:27:54 +01:00
|
|
|
return false;
|
2015-04-13 13:28:17 +01:00
|
|
|
// don't forget, on success these guys must create slots
|
|
|
|
if (this->q_state == 0) {
|
|
|
|
result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)ap, q_g, &q_h);
|
2014-06-11 19:27:54 +01:00
|
|
|
} else {
|
2015-04-13 13:28:17 +01:00
|
|
|
LOCAL_AllowRestart = q_open;
|
2014-09-19 20:20:22 +01:00
|
|
|
result = (bool)YAP_RetryGoal(&q_h);
|
2014-06-11 19:27:54 +01:00
|
|
|
}
|
2015-04-13 13:28:17 +01:00
|
|
|
q_state = 1;
|
2014-06-11 19:27:54 +01:00
|
|
|
if (!result) {
|
2014-09-19 20:20:22 +01:00
|
|
|
YAP_LeaveGoal(FALSE, &q_h);
|
2015-04-13 13:28:17 +01:00
|
|
|
q_open = 0;
|
2014-06-11 19:27:54 +01:00
|
|
|
}
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2014-06-11 19:27:54 +01:00
|
|
|
return result;
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void YAPQuery::cut()
|
|
|
|
{
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
2014-04-28 11:47:09 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
if (q_open != 1 || q_state == 0) return;
|
|
|
|
YAP_LeaveGoal(FALSE, &q_h);
|
|
|
|
q_open = 0;
|
|
|
|
LOCAL_execution = this;
|
|
|
|
RECOVER_MACHINE_REGS();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void YAPQuery::close()
|
|
|
|
{
|
2014-06-11 19:27:54 +01:00
|
|
|
CACHE_REGS
|
2014-05-04 22:31:22 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2015-06-18 00:52:31 +01:00
|
|
|
if (EX /* && !(q_flags & (true PL_Q_CATCH_EXCEPTION)) */) {
|
2014-06-12 01:25:50 +01:00
|
|
|
EX = (struct DB_TERM *)NULL;
|
2014-06-11 19:27:54 +01:00
|
|
|
}
|
|
|
|
/* need to implement backtracking here */
|
2015-04-13 13:28:17 +01:00
|
|
|
if (q_open != 1 || q_state == 0) {
|
|
|
|
RECOVER_MACHINE_REGS();
|
2014-06-11 19:27:54 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-04-13 13:28:17 +01:00
|
|
|
YAP_LeaveGoal(FALSE, &q_h);
|
|
|
|
q_open = 0;
|
|
|
|
LOCAL_execution = this;
|
|
|
|
RECOVER_MACHINE_REGS();
|
2014-04-28 11:47:09 +01:00
|
|
|
}
|
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
static YAPEngine *curren;
|
|
|
|
|
|
|
|
#if __ANDROID__
|
2014-04-29 11:45:19 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
#include <jni.h>
|
|
|
|
#include <string.h>
|
2014-04-29 11:45:19 +01:00
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
JNIEnv *Yap_jenv;
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
|
|
|
JNIEnv* env;
|
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Yap_jenv = env;
|
|
|
|
return JNI_VERSION_1_6;
|
|
|
|
}
|
2014-06-11 19:27:54 +01:00
|
|
|
|
|
|
|
extern char *Yap_AndroidBufp;
|
|
|
|
static size_t Yap_AndroidMax, Yap_AndroidSz;
|
|
|
|
|
|
|
|
extern void(*Yap_DisplayWithJava)(int c);
|
|
|
|
|
|
|
|
static void
|
|
|
|
displayWithJava(int c)
|
2014-06-04 22:08:37 +01:00
|
|
|
{
|
2014-06-11 19:27:54 +01:00
|
|
|
char *ptr = Yap_AndroidBufp;
|
|
|
|
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';
|
2014-06-19 15:06:15 +01:00
|
|
|
curren->run(Yap_AndroidBufp);
|
2014-06-11 19:27:54 +01:00
|
|
|
Yap_AndroidSz = 0;
|
|
|
|
}
|
2014-06-04 22:08:37 +01:00
|
|
|
}
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
extern "C" void Java_pt_up_fc_dcc_yap_JavaYap_load(JNIEnv *env0, jobject obj, jobject mgr);
|
|
|
|
|
|
|
|
extern "C" void Java_pt_up_fc_dcc_yap_JavaYap_load
|
|
|
|
(JNIEnv *env0, jobject obj, jobject asset_manager)
|
|
|
|
{
|
|
|
|
AAssetManager *mgr = AAssetManager_fromJava(Yap_jenv, asset_manager);
|
|
|
|
if (mgr == NULL) {
|
|
|
|
LOG( "we're doomed, mgr = 0; bip bip bip");
|
|
|
|
} else {
|
|
|
|
Yap_assetManager = mgr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:08:37 +01:00
|
|
|
|
2014-06-11 19:27:54 +01:00
|
|
|
#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__
|
|
|
|
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;
|
2015-04-15 11:21:15 +01:00
|
|
|
if (YAP_Init( &init_args ) == YAP_BOOT_ERROR)
|
|
|
|
throw(YAPError::YAP_OTHER_ERROR);
|
2014-06-11 19:27:54 +01:00
|
|
|
}
|
|
|
|
|
2015-02-09 01:52:10 +00:00
|
|
|
|
|
|
|
YAPPredicate::YAPPredicate(YAPAtom at) {
|
|
|
|
CACHE_REGS
|
2015-04-15 11:21:15 +01:00
|
|
|
ap = RepPredProp(PredPropByAtom(at.a,Yap_CurrentModule()));
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
YAPPredicate::YAPPredicate(YAPAtom at, arity_t arity) {
|
|
|
|
CACHE_REGS
|
|
|
|
if (arity) {
|
|
|
|
Functor f = Yap_MkFunctor(at.a, arity);
|
2015-04-15 11:21:15 +01:00
|
|
|
ap = RepPredProp(PredPropByFunc(f,Yap_CurrentModule()));
|
2015-02-09 01:52:10 +00:00
|
|
|
} else {
|
2015-04-15 11:21:15 +01:00
|
|
|
ap = RepPredProp(PredPropByAtom(at.a,Yap_CurrentModule()));
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// auxiliary routine to find a predicate in the current module.
|
2015-04-13 13:28:17 +01:00
|
|
|
PredEntry *YAPPredicate::getPred( Term &t, Term* &outp ) {
|
2015-02-09 01:52:10 +00:00
|
|
|
CACHE_REGS
|
2015-04-15 11:21:15 +01:00
|
|
|
Term m = Yap_CurrentModule() ;
|
2015-04-13 13:28:17 +01:00
|
|
|
t = Yap_StripModule(t, &m);
|
2015-02-09 01:52:10 +00:00
|
|
|
if (IsVarTerm(t) || IsNumTerm(t)) {
|
2015-04-15 11:21:15 +01:00
|
|
|
throw YAPError::YAP_TYPE_ERROR;
|
2015-02-09 01:52:10 +00:00
|
|
|
}
|
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
2015-04-13 13:28:17 +01:00
|
|
|
outp = (Term *)NULL;
|
|
|
|
return ap;
|
2015-02-09 01:52:10 +00:00
|
|
|
} else if (IsPairTerm(t)) {
|
2015-04-15 11:21:15 +01:00
|
|
|
Term ts[2];
|
2015-04-13 13:28:17 +01:00
|
|
|
ts[0] = t;
|
2015-04-15 11:21:15 +01:00
|
|
|
ts[1] = m;
|
|
|
|
t = Yap_MkApplTerm(FunctorCsult, 2, ts);
|
2015-04-13 13:28:17 +01:00
|
|
|
}
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
if (IsExtensionFunctor(f)) {
|
2015-04-15 11:21:15 +01:00
|
|
|
throw YAPError::YAP_TYPE_ERROR;
|
2015-04-13 13:28:17 +01:00
|
|
|
} else {
|
2015-02-09 01:52:10 +00:00
|
|
|
ap = RepPredProp(PredPropByFunc(f, m));
|
|
|
|
outp = RepAppl(t)+1;
|
|
|
|
}
|
|
|
|
return ap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
YAPPrologPredicate::YAPPrologPredicate(YAPAtom name,
|
|
|
|
arity_t arity,
|
|
|
|
YAPModule mod,
|
|
|
|
bool tabled,
|
|
|
|
bool logical_updates,
|
|
|
|
bool thread_local,
|
|
|
|
bool sourced,
|
|
|
|
bool discontiguous,
|
|
|
|
bool multiFile,
|
|
|
|
bool hidden,
|
|
|
|
bool untraceable,
|
|
|
|
bool unspyable,
|
|
|
|
bool meta,
|
|
|
|
bool moduleTransparent,
|
|
|
|
bool quasiQuotable,
|
|
|
|
size_t mega_clause
|
|
|
|
) : YAPPredicate(name, arity, mod) {
|
|
|
|
if (!ap) return;
|
|
|
|
if (thread_local) {
|
|
|
|
if (ap->cs.p_code.NOfClauses || tabled)
|
|
|
|
return;
|
|
|
|
ap->PredFlags |= (ThreadLocalPredFlag|LogUpdatePredFlag);
|
|
|
|
} else if (logical_updates) {
|
|
|
|
if (ap->cs.p_code.NOfClauses || tabled)
|
|
|
|
return;
|
|
|
|
ap->PredFlags |= LogUpdatePredFlag;
|
|
|
|
ap->CodeOfPred = FAILCODE;
|
|
|
|
ap->OpcodeOfPred = FAILCODE->opc;
|
|
|
|
}
|
|
|
|
if (tabled) {
|
|
|
|
ap->PredFlags |= TabledPredFlag;
|
|
|
|
if (ap->cs.p_code.NOfClauses || tabled)
|
|
|
|
return;
|
|
|
|
ap->PredFlags |= TabledPredFlag;
|
|
|
|
}
|
|
|
|
if (sourced) {
|
|
|
|
ap->PredFlags |= SourcePredFlag;
|
|
|
|
}
|
|
|
|
if (discontiguous) {
|
|
|
|
ap->PredFlags |= DiscontiguousPredFlag;
|
|
|
|
}
|
|
|
|
if (multiFile) {
|
|
|
|
ap->PredFlags |= MultiFileFlag;
|
|
|
|
}
|
|
|
|
if (hidden) {
|
|
|
|
ap->PredFlags |= HiddenPredFlag;
|
|
|
|
}
|
|
|
|
if (untraceable) {
|
|
|
|
ap->PredFlags |= SourcePredFlag;
|
|
|
|
}
|
|
|
|
if (unspyable) {
|
|
|
|
ap->PredFlags |= NoSpyPredFlag;
|
|
|
|
}
|
|
|
|
if (meta) {
|
|
|
|
ap->PredFlags |= MetaPredFlag;
|
|
|
|
} else if (moduleTransparent) {
|
|
|
|
ap->PredFlags |= ModuleTransparentPredFlag;
|
|
|
|
}
|
|
|
|
if (quasiQuotable) {
|
|
|
|
ap->PredFlags |= QuasiQuotationPredFlag;
|
|
|
|
}
|
|
|
|
if (untraceable) {
|
|
|
|
ap->PredFlags |= SourcePredFlag;
|
|
|
|
}
|
|
|
|
if (hidden) {
|
|
|
|
ap->PredFlags |= SourcePredFlag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *YAPPrologPredicate::assertClause( YAPTerm clause, bool last, YAPTerm source) {
|
|
|
|
CACHE_REGS
|
|
|
|
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2015-02-09 01:52:10 +00:00
|
|
|
Term tt = clause.gt();
|
|
|
|
Term sourcet = source.gt();
|
2015-04-15 11:21:15 +01:00
|
|
|
yamop *codeaddr = Yap_cclause(tt, PP->ArityOfPE, Yap_CurrentModule(), sourcet); /* vsc: give the number of arguments
|
2015-02-09 01:52:10 +00:00
|
|
|
to cclause in case there is overflow */
|
|
|
|
Term ntt = clause.gt();
|
2015-04-13 13:28:17 +01:00
|
|
|
if (LOCAL_ErrorMessage) {
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-09 01:52:10 +00:00
|
|
|
Term *tref = &ntt;
|
2015-04-15 11:21:15 +01:00
|
|
|
if (Yap_addclause(ntt, codeaddr, (last ? 0 : 2), Yap_CurrentModule(), tref)) {
|
2015-04-13 13:28:17 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
}
|
2015-02-09 01:52:10 +00:00
|
|
|
return tref;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void* YAPPrologPredicate::retractClause( YAPTerm skeleton, bool all) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void* YAPPrologPredicate::clause( YAPTerm skeleton, YAPTerm &body ) {
|
|
|
|
return 0;
|
|
|
|
}
|