This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/CXX/yapi.cpp

986 lines
22 KiB
C++
Raw Normal View History

2014-04-28 11:47:09 +01:00
#define YAP_CPP_INTERFACE 1
#include "yapi.hh"
2016-09-27 18:28:54 +01:00
#include <string>
extern "C" {
2016-07-31 16:22:24 +01:00
#if __ANDROID__
#include "android/log.h"
#endif
2015-06-18 00:52:31 +01:00
#include "YapInterface.h"
#include "blobs.h"
2015-09-21 23:05:36 +01:00
X_API char *Yap_TermToString(Term t, size_t *length, encoding_t encodingp,
int flags);
2016-07-31 16:22:24 +01:00
X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, arity_t arity);
X_API void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, arity_t,
2016-04-12 16:22:53 +01:00
YAP_Term);
2016-04-10 14:21:17 +01:00
X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred,
2016-07-31 16:22:24 +01:00
arity_t, arity_t);
}
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
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;
2014-06-11 19:27:54 +01:00
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;
2014-06-11 19:27:54 +01:00
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk(MkAtomTerm(out.val.a));
else
t = 0L;
2014-06-11 19:27:54 +01:00
RECOVER_H();
2014-04-28 11:47:09 +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;
2014-06-11 19:27:54 +01:00
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;
2014-06-11 19:27:54 +01:00
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk(MkAtomTerm(out.val.a));
else
t = 0L;
2014-06-11 19:27:54 +01:00
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;
2014-06-11 19:27:54 +01:00
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;
2014-06-11 19:27:54 +01:00
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk(out.val.t);
else
t = 0L;
2014-06-11 19:27:54 +01:00
RECOVER_H();
2014-04-28 11:47:09 +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;
2014-06-11 19:27:54 +01:00
RECOVER_H();
2014-04-28 11:47:09 +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;
2014-06-11 19:27:54 +01:00
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk(out.val.t);
else
t = 0L;
2014-06-11 19:27:54 +01:00
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();
2016-07-31 16:22:24 +01:00
arity_t arity = ArityOfFunctor(f.f);
Term *tt = new Term[arity];
for (arity_t i = 0; i < arity; i++)
tt[i] = ts[i].term();
mk(Yap_MkApplTerm(f.f, arity, tt));
delete[] tt;
2015-04-13 13:28:17 +01:00
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2016-12-10 07:01:10 +00:00
#if 0
2016-09-21 20:41:23 +01:00
YAPApplTerm::YAPApplTerm(const char *f, std::vector<YAPTerm> ts) : YAPTerm() {
BACKUP_H();
arity_t arity = ts.size();
std::vector<Term> tt(arity);
for (arity_t i = 0; i < arity; i++)
tt[i] = ts[i].term();
Functor ff = Yap_MkFunctor(Yap_LookupAtom(f), arity);
t = Yap_MkApplTerm(ff, arity, &tt[0]);
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();
2016-07-31 16:22:24 +01:00
arity_t 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
}
2016-12-10 07:01:10 +00:00
#endif
2014-04-28 11:47:09 +01:00
2016-07-31 16:22:24 +01:00
YAPFunctor YAPApplTerm::getFunctor() { return YAPFunctor(FunctorOfTerm(gt())); }
2016-12-10 07:01:10 +00:00
Term &YAPTerm::operator[](arity_t i) {
2016-07-31 16:22:24 +01:00
BACKUP_MACHINE_REGS();
Term t0 = gt();
Term tf = 0;
if (IsApplTerm(t0)) {
2016-12-10 07:01:10 +00:00
// Functor f = FunctorOfTerm(t0);
// if (IsExtensionFunctor(f))
// return 0;
RECOVER_MACHINE_REGS();
return RepAppl(t0)[(i + 1)];
2016-07-31 16:22:24 +01:00
} else if (IsPairTerm(t0)) {
if (i == 0)
tf = HeadOfTerm(t0);
else if (i == 1)
tf = TailOfTerm(t0);
2016-12-10 07:01:10 +00:00
RECOVER_MACHINE_REGS();
return RepPair(tf)[i];
2016-07-31 16:22:24 +01:00
}
2014-04-29 11:45:19 +01:00
}
2016-12-10 07:01:10 +00:00
Term &YAPListTerm::operator[](arity_t i) {
2016-07-31 16:22:24 +01:00
BACKUP_MACHINE_REGS();
Term t0 = gt();
Term tf = 0;
while (IsPairTerm(t0)) {
if (i == 0) {
tf = HeadOfTerm(t0);
break;
} else {
t0 = TailOfTerm(t0);
i--;
}
}
RECOVER_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
return RepPair(tf)[i];
2016-07-31 16:22:24 +01:00
}
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
BACKUP_H();
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();
t = Yap_MkNewPairTerm();
2015-04-13 13:28:17 +01:00
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
YAP_tag_t YAPTerm::tag() {
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;
2014-06-11 19:27:54 +01:00
}
if (IsPairTerm(tt))
return YAP_TAG_PAIR;
if (IsAtomOrIntTerm(tt)) {
if (IsAtomTerm(tt))
return YAP_TAG_ATOM;
return YAP_TAG_INT;
2014-06-11 19:27:54 +01:00
} 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;
}
2014-06-11 19:27:54 +01:00
}
}
return YAP_TAG_APPL;
2014-06-11 19:27:54 +01:00
}
2014-04-28 11:47:09 +01:00
}
2016-12-10 07:01:10 +00:00
Term YAPTerm::deepCopy() {
2016-07-31 16:22:24 +01:00
yhandle_t tn;
2014-06-11 19:27:54 +01:00
BACKUP_MACHINE_REGS();
2014-04-28 11:47:09 +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();
2016-12-10 07:01:10 +00:00
return (tn);
2016-07-31 16:22:24 +01:00
}
2016-12-10 07:01:10 +00:00
Term YAPListTerm::dup() {
2016-07-31 16:22:24 +01:00
yhandle_t tn;
BACKUP_MACHINE_REGS();
tn = Yap_CopyTerm(gt());
RECOVER_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
return tn;
2016-07-31 16:22:24 +01:00
}
intptr_t YAPTerm::numberVars(intptr_t i0, bool skip_singletons) {
BACKUP_MACHINE_REGS();
intptr_t i = Yap_NumberVars(gt(), i0, skip_singletons);
RECOVER_MACHINE_REGS();
return i;
2014-04-28 11:47:09 +01:00
}
2016-12-10 07:01:10 +00:00
const char *YAPQuery::text() { return YAPTerm(goal).text(); }
2014-09-19 20:20:22 +01:00
YAPIntegerTerm::YAPIntegerTerm(intptr_t i) {
CACHE_REGS Term tn = MkIntegerTerm(i);
mk(tn);
}
/*
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
*/
2015-07-06 12:01:55 +01:00
YAPTerm::YAPTerm(void *ptr) {
CACHE_REGS
mk(MkIntegerTerm((Int)ptr));
2015-07-06 12:01:55 +01:00
}
2014-06-11 19:27:54 +01:00
2016-12-10 07:01:10 +00:00
Term YAPListTerm::car() {
2014-06-11 19:27:54 +01:00
Term to = gt();
if (IsPairTerm(to))
2016-12-10 07:01:10 +00:00
return (HeadOfTerm(to));
2016-09-21 20:41:23 +01:00
else {
Yap_Error(TYPE_ERROR_LIST, to, "");
throw YAPError();
}
}
2016-07-31 16:22:24 +01:00
YAPListTerm::YAPListTerm(YAPTerm ts[], arity_t n) {
CACHE_REGS
BACKUP_H();
if (n == 0)
t = TermNil;
while (HR + n * 2 > ASP - 1024) {
RECOVER_H();
if (!Yap_dogc(0, NULL PASS_REGS)) {
t = TermNil;
}
BACKUP_H();
}
t = AbsPair(HR);
for (arity_t i = 0; i < n; i++) {
HR[2 * i] = ts[i].gt();
HR[2 * i + 1] = AbsPair(HR + (2 * i + 2));
}
}
2015-07-06 12:01:55 +01:00
YAPVarTerm::YAPVarTerm() {
CACHE_REGS
mk(MkVarTerm());
2015-07-06 12:01:55 +01:00
}
2016-12-10 07:01:10 +00:00
const char *YAPAtom::getName(void) { return Yap_AtomToUTF8Text(a, nullptr); }
2016-10-16 23:18:51 +01:00
2016-12-10 07:01:10 +00:00
void YAPQuery::openQuery() {
CACHE_REGS
arity_t arity = ap->ArityOfPE;
if (arity) {
Term *ts;
Term t = goal;
if (IsPairTerm(t)) {
ts = RepPair(t);
} else {
ts = RepAppl(t) + 1;
2016-09-21 20:41:23 +01:00
}
2016-12-10 07:01:10 +00:00
for (arity_t i = 0; i < arity; i++) {
XREGS[i + 1] = ts[i];
}
}
// oq = LOCAL_execution;
// LOCAL_execution = this;
q_open = true;
q_state = 0;
q_flags = true; // PL_Q_PASS_EXCEPTION;
2015-04-13 13:28:17 +01:00
2016-12-10 07:01:10 +00:00
q_p = P;
q_cp = CP;
// make sure this is safe
q_handles = Yap_StartSlots();
}
2015-07-06 12:01:55 +01:00
2016-09-21 20:41:23 +01:00
bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) {
2016-07-31 16:22:24 +01:00
CACHE_REGS
BACKUP_MACHINE_REGS();
2016-09-21 20:41:23 +01:00
arity_t arity = ap.getArity();
bool result;
YAP_dogoalinfo q;
Term terr;
jmp_buf q_env;
2016-11-11 07:23:34 +00:00
2016-09-21 20:41:23 +01:00
for (arity_t i = 0; i < arity; i++)
2016-11-11 07:23:34 +00:00
XREGS[i + 1] = ts[i].term();
2016-09-21 20:41:23 +01:00
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
// make sure this is safe
if (setjmp(q_env)) {
if ((terr = Yap_PeekException())) {
YAP_LeaveGoal(false, &q);
Yap_CloseHandles(q.CurSlot);
throw YAPError();
2015-04-13 13:28:17 +01:00
}
2016-09-21 20:41:23 +01:00
return false;
}
2016-11-08 07:37:36 +00:00
// don't forget, on success these bindings will still be there);
2016-12-10 07:01:10 +00:00
YAP_LeaveGoal(false, &q);
2016-09-27 18:28:54 +01:00
RECOVER_MACHINE_REGS();
return result;
}
2016-12-10 07:01:10 +00:00
bool YAPEngine::goalt(YAPTerm Yt) { return Yt.term(); }
2016-10-16 23:18:51 +01:00
bool YAPEngine::goal(Term t) {
2016-09-27 18:28:54 +01:00
CACHE_REGS
BACKUP_MACHINE_REGS();
2016-10-16 23:18:51 +01:00
Term terr, tmod = CurrentModule, *ts = nullptr;
2016-09-27 18:28:54 +01:00
PredEntry *ap = Yap_get_pred(t, tmod, "C++");
arity_t arity = ap->ArityOfPE;
bool result;
jmp_buf q_env;
if (IsApplTerm(t)) {
ts = RepAppl(t) + 1;
} else {
ts = RepPair(t);
}
for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = ts[i];
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
// make sure this is safe
if (setjmp(q_env)) {
if ((terr = Yap_PeekException())) {
YAP_LeaveGoal(false, &q);
Yap_CloseHandles(q.CurSlot);
throw YAPError();
}
return false;
}
// don't forget, on success these guys may create slots
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
2016-10-16 23:18:51 +01:00
2016-09-27 18:28:54 +01:00
result = (bool)YAP_EnterGoal(ap, nullptr, &q);
2016-09-21 20:41:23 +01:00
if ((terr = Yap_GetException())) {
YAP_LeaveGoal(false, &q);
throw YAPError();
}
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "out %d", result);
if (!result) {
YAP_LeaveGoal(false, &q);
2015-04-13 13:28:17 +01:00
} else {
2016-09-21 20:41:23 +01:00
YAP_LeaveGoal(FALSE, &q);
2015-04-13 13:28:17 +01:00
}
RECOVER_MACHINE_REGS();
2016-09-21 20:41:23 +01:00
return result;
2014-04-28 11:47:09 +01:00
}
2016-10-16 23:18:51 +01:00
void YAPEngine::release() {
BACKUP_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
YAP_LeaveGoal(FALSE, &q);
2016-10-16 23:18:51 +01:00
RECOVER_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
}
2016-10-16 23:18:51 +01:00
Term YAPEngine::fun(Term t) {
CACHE_REGS
BACKUP_MACHINE_REGS();
Term tmod = CurrentModule, *ts = nullptr;
2016-12-10 07:01:10 +00:00
PredEntry *ap;
2016-11-08 07:37:36 +00:00
arity_t arity;
2016-10-16 23:18:51 +01:00
Functor f;
jmp_buf q_env;
Atom name;
BACKUP_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
if (IsApplTerm(t)) {
2016-10-16 23:18:51 +01:00
ts = RepAppl(t) + 1;
f = (Functor)ts[-1];
2016-12-10 07:01:10 +00:00
name = NameOfFunctor(f);
arity = ArityOfFunctor(f);
2016-10-16 23:18:51 +01:00
for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = ts[i];
} else if (IsAtomTerm(t)) {
name = AtomOfTerm(t);
f = nullptr;
2016-11-08 07:37:36 +00:00
arity = 0;
2016-10-20 04:44:59 +01:00
} else if (IsPairTerm(t)) {
2016-10-16 23:18:51 +01:00
XREGS[1] = ts[0];
XREGS[2] = ts[1];
2016-11-08 07:37:36 +00:00
arity = 2;
2016-10-16 23:18:51 +01:00
name = AtomDot;
f = FunctorDot;
2016-10-20 04:44:59 +01:00
} else {
2016-12-10 07:01:10 +00:00
Yap_Error(TYPE_ERROR_CALLABLE, t, 0);
return 0L;
}
XREGS[arity + 1] = MkVarTerm();
arity++;
f = Yap_MkFunctor(name, arity);
ap = (PredEntry *)(PredPropByFunc(f, tmod));
q.CurSlot = Yap_StartSlots();
2016-10-16 23:18:51 +01:00
q.p = P;
q.cp = CP;
// make sure this is safe
yhandle_t o = Yap_InitHandle(XREGS[arity]);
if (setjmp(q_env)) {
Term terr;
2016-12-10 07:01:10 +00:00
if ((terr = Yap_PeekException())) {
2016-10-16 23:18:51 +01:00
YAP_LeaveGoal(false, &q);
Yap_CloseHandles(q.CurSlot);
throw YAPError();
}
return 0;
}
// don't forget, on success these guys may create slots
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
2016-12-10 07:01:10 +00:00
if ((o = (Term)YAP_EnterGoal(ap, nullptr, &q)) == 0)
2016-10-16 23:18:51 +01:00
return 0;
Term terr;
if ((terr = Yap_GetException())) {
YAP_LeaveGoal(false, &q);
2016-12-10 07:01:10 +00:00
Yap_CloseHandles(q.CurSlot);
2016-10-16 23:18:51 +01:00
throw YAPError();
}
2016-11-11 07:23:34 +00:00
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "out %ld", o);
2016-10-16 23:18:51 +01:00
t = Yap_GetFromSlot(q.CurSlot);
Yap_CloseHandles(q.CurSlot);
if (!t) {
YAP_LeaveGoal(false, &q);
2016-11-08 07:37:36 +00:00
t = 0;
2016-10-16 23:18:51 +01:00
}
RECOVER_MACHINE_REGS();
return t;
}
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
: YAPPredicate(f, mod) {
2014-06-11 19:27:54 +01:00
/* ignore flags for now */
2016-09-21 20:41:23 +01:00
BACKUP_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
goal = YAPApplTerm(f, ts).gt();
names = TermNil;
2016-09-21 20:41:23 +01:00
openQuery();
RECOVER_MACHINE_REGS();
2014-04-29 11:45:19 +01:00
}
2014-04-28 11:47:09 +01:00
2016-12-10 07:01:10 +00:00
#if 0
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) {
2016-12-10 07:01:10 +00:00
/* ignore flags for now */
2016-09-21 20:41:23 +01:00
BACKUP_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
goal = YAPApplTerm(f, ts).gt();
names = TermNil;
2016-09-21 20:41:23 +01:00
openQuery();
RECOVER_MACHINE_REGS();
2014-04-28 11:47:09 +01:00
}
2016-12-10 07:01:10 +00:00
#endif
2014-04-28 11:47:09 +01:00
YAPQuery::YAPQuery(YAPPredicate p, YAPTerm ts[]) : YAPPredicate(p.ap) {
2016-09-21 20:41:23 +01:00
BACKUP_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
goal = YAPApplTerm(YAPFunctor(p.ap->FunctorOfPred), ts).term();
names = TermNil;
2016-09-21 20:41:23 +01:00
openQuery();
RECOVER_MACHINE_REGS();
2014-04-28 11:47:09 +01:00
}
2016-12-10 07:01:10 +00:00
Term YAPQuery::namedVars() {
2014-06-11 19:27:54 +01:00
CACHE_REGS
2016-11-11 07:23:34 +00:00
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "vnames %s %ld",
2016-12-10 07:01:10 +00:00
names.text(), LOCAL_CurSlot);
return (names); // should be o
2016-07-31 16:22:24 +01:00
}
2016-12-10 07:01:10 +00:00
Term YAPQuery::namedVarsCopy() {
2016-07-31 16:22:24 +01:00
CACHE_REGS
2016-12-10 07:01:10 +00:00
__android_log_print(NDROID_LOG_INFO, "YAPDroid", "vnames %s %ld",
names.text(), LOCAL_CurSlot);
return (YAP_CopyTerm(names)); // should be o
2014-06-11 19:27:54 +01:00
}
bool YAPQuery::next() {
2014-06-11 19:27:54 +01:00
CACHE_REGS
2016-07-31 16:22:24 +01:00
bool result;
2016-09-21 20:41:23 +01:00
Term terr;
2015-04-13 13:28:17 +01:00
BACKUP_MACHINE_REGS();
2016-07-31 16:22:24 +01:00
if (!q_open)
return false;
2016-08-15 20:56:05 +01:00
if (setjmp(q_env)) {
2016-09-21 20:41:23 +01:00
if ((terr = Yap_GetException())) {
YAP_LeaveGoal(false, &q_h);
Yap_CloseHandles(q_handles);
throw YAPError();
}
2014-06-11 19:27:54 +01:00
return false;
2016-08-15 20:56:05 +01:00
}
2016-07-31 16:22:24 +01:00
// don't forget, on success these guys may create slots
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
if (q_state == 0) {
2016-09-21 20:41:23 +01:00
result = (bool)YAP_EnterGoal(ap, nullptr, &q_h);
2014-06-11 19:27:54 +01:00
} else {
LOCAL_AllowRestart = q_open;
result = (bool)YAP_RetryGoal(&q_h);
2014-06-11 19:27:54 +01:00
}
2016-08-30 17:03:42 +01:00
if (result) {
2016-11-11 07:23:34 +00:00
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "vnames %d %s %ld",
2016-09-21 20:41:23 +01:00
q_state, vnames.text(), LOCAL_CurSlot);
2016-08-30 17:03:42 +01:00
} else {
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "fail");
2016-07-31 16:22:24 +01:00
}
2015-04-13 13:28:17 +01:00
q_state = 1;
2016-09-21 20:41:23 +01:00
if ((terr = Yap_GetException())) {
2016-09-30 23:11:13 +01:00
Yap_DebugPlWriteln(terr);
2016-09-21 20:41:23 +01:00
YAP_LeaveGoal(false, &q_h);
Yap_CloseHandles(q_handles);
2016-09-30 23:11:13 +01:00
q_open = false;
2016-09-21 20:41:23 +01:00
throw YAPError();
2016-04-12 16:22:53 +01:00
}
2016-07-31 16:22:24 +01:00
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "out %d", result);
2014-06-11 19:27:54 +01:00
if (!result) {
2016-09-21 20:41:23 +01:00
YAP_LeaveGoal(false, &q_h);
2016-08-30 17:03:42 +01:00
Yap_CloseHandles(q_handles);
2016-07-31 16:22:24 +01:00
q_open = false;
} else {
q_handles = Yap_StartSlots();
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();
2016-07-31 16:22:24 +01:00
if (!q_open || q_state == 0)
return;
2015-04-13 13:28:17 +01:00
YAP_LeaveGoal(FALSE, &q_h);
q_open = 0;
2016-07-31 16:22:24 +01:00
// LOCAL_execution = this;
2015-04-13 13:28:17 +01:00
RECOVER_MACHINE_REGS();
2014-04-28 11:47:09 +01:00
}
2016-04-12 16:22:53 +01:00
bool YAPQuery::deterministic() {
CACHE_REGS
BACKUP_MACHINE_REGS();
2016-07-31 16:22:24 +01:00
if (!q_open || q_state == 0)
2016-04-12 16:22:53 +01:00
return false;
choiceptr myB = (choiceptr)(LCL0 - q_h.b);
return (B >= myB);
RECOVER_MACHINE_REGS();
}
2016-07-31 16:22:24 +01:00
YAPTerm YAPQuery::getTerm(yhandle_t t) { return YAPTerm(t); }
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();
Yap_ResetException(worker_id);
2014-06-11 19:27:54 +01:00
/* need to implement backtracking here */
2016-09-21 20:41:23 +01:00
if (q_open != true || q_state == 0) {
RECOVER_MACHINE_REGS();
return;
2014-06-11 19:27:54 +01:00
}
2015-04-13 13:28:17 +01:00
YAP_LeaveGoal(FALSE, &q_h);
q_open = 0;
2016-08-30 17:03:42 +01:00
Yap_CloseHandles(q_handles);
2016-07-31 16:22:24 +01:00
// LOCAL_execution = this;
2015-04-13 13:28:17 +01:00
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;
extern JNIEXPORT jint JNICALL JNI_MySQLOnLoad(JavaVM *vm, void *reserved);
JNIEXPORT jint JNICALL JNI_MySQLOnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
return -1;
2015-04-13 13:28:17 +01:00
}
Yap_jenv = env;
2015-04-13 13:28:17 +01:00
return JNI_VERSION_1_6;
}
2014-06-11 19:27:54 +01:00
char *Yap_AndroidBufp;
2014-06-11 19:27:54 +01:00
static size_t Yap_AndroidMax, Yap_AndroidSz;
extern void (*Yap_DisplayWithJava)(int c);
2014-06-11 19:27:54 +01:00
void Yap_displayWithJava(int c) {
2014-06-11 19:27:54 +01:00
char *ptr = Yap_AndroidBufp;
if (!ptr)
ptr = Yap_AndroidBufp = (char *)malloc(Yap_AndroidSz);
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);
2014-06-11 19:27:54 +01:00
}
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
if (c == '\n') {
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
curren->run(Yap_AndroidBufp);
Yap_AndroidSz = 0;
2014-06-11 19:27:54 +01:00
}
}
2016-07-31 16:22:24 +01:00
#endif
void YAPEngine::doInit(YAP_file_type_t BootMode) {
if ((BootMode = YAP_Init(&init_args)) == YAP_FOUND_BOOT_ERROR) {
2016-09-21 20:41:23 +01:00
throw YAPError();
2016-07-31 16:22:24 +01:00
}
/* Begin preprocessor code */
/* live */
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "$init_system");
#if __ANDROID__
Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096);
Yap_AndroidBufp[0] = '\0';
Yap_AndroidSz = 0;
#endif
yerror = YAPError();
2015-04-13 13:28:17 +01:00
2016-07-31 16:22:24 +01:00
YAPQuery initq = YAPQuery(YAPAtom("$init_system"));
if (initq.next()) {
initq.cut();
2015-04-13 13:28:17 +01:00
} else {
2016-07-31 16:22:24 +01:00
// should throw exception
2015-04-13 13:28:17 +01:00
}
}
2016-07-31 16:22:24 +01:00
YAPEngine::YAPEngine(char *savedState, char *bootFile, size_t stackSize,
size_t trailSize, size_t maxStackSize, size_t maxTrailSize,
char *libDir, char *goal, char *topLevel, bool script,
2016-12-10 07:01:10 +00:00
bool fastBoot, bool embedded,
YAPCallback *cb)
: _callback(0) { // a single engine can be active
2016-07-31 16:22:24 +01:00
YAP_file_type_t BootMode;
int Argc = 1;
char **Argv;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "YAP %s ", bootFile);
// delYAPCallback()b
// if (cb)
// setYAPCallback(cb);
curren = this;
{
size_t l1 = 2 * sizeof(char *);
if (!(Argv = (char **)malloc(l1)))
return;
Argv[0] = (char *)malloc(4);
strcpy(Argv[0], "yap");
Argv[1] = NULL;
}
BootMode = Yap_InitDefaults(&init_args, NULL, Argc, Argv);
2014-06-11 19:27:54 +01:00
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;
2016-04-12 16:22:53 +01:00
init_args.HaltAfterConsult = script;
2014-06-11 19:27:54 +01:00
init_args.FastBoot = fastBoot;
2016-10-20 04:38:17 +01:00
init_args.Embedded = embedded;
2016-07-31 16:22:24 +01:00
doInit(BootMode);
}
YAPEngine::YAPEngine(int argc, char *argv[],
YAPCallback *cb)
: _callback(0) { // a single engine can be active
YAP_file_type_t BootMode;
BootMode = YAP_parse_yap_arguments(argc, argv, &init_args);
// delYAPCallback()b
// if (cb)
// setYAPCallback(cb);
2014-06-11 19:27:54 +01:00
curren = this;
2016-07-31 16:22:24 +01:00
doInit(BootMode);
2014-06-11 19:27:54 +01:00
}
2015-02-09 01:52:10 +00:00
YAPPredicate::YAPPredicate(YAPAtom at) {
CACHE_REGS
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);
ap = RepPredProp(PredPropByFunc(f, Yap_CurrentModule()));
} else {
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.
PredEntry *YAPPredicate::getPred(Term &t, Term *&outp) {
2015-02-09 01:52:10 +00:00
CACHE_REGS
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)) {
2016-09-21 20:41:23 +01:00
if (IsVarTerm(t))
Yap_Error(INSTANTIATION_ERROR, t, 0);
else if (IsNumTerm(t))
Yap_Error(TYPE_ERROR_CALLABLE, t, 0);
throw YAPError();
2015-02-09 01:52:10 +00:00
}
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
outp = (Term *)NULL;
2015-04-13 13:28:17 +01:00
return ap;
} else if (IsPairTerm(t)) {
Term ts[2];
2015-04-13 13:28:17 +01:00
ts[0] = t;
ts[1] = m;
t = Yap_MkApplTerm(FunctorCsult, 2, ts);
2016-07-31 16:22:24 +01:00
outp = RepAppl(t) + 1;
2015-04-13 13:28:17 +01:00
}
Functor f = FunctorOfTerm(t);
if (IsExtensionFunctor(f)) {
2016-09-21 20:41:23 +01:00
Yap_Error(TYPE_ERROR_CALLABLE, t, 0);
;
throw YAPError();
2015-04-13 13:28:17 +01:00
} else {
ap = RepPredProp(PredPropByFunc(f, m));
outp = RepAppl(t) + 1;
2015-02-09 01:52:10 +00:00
}
return ap;
}
2016-09-27 18:28:54 +01:00
void *YAPPrologPredicate::assertClause(YAPTerm cl, bool last, YAPTerm source) {
2015-02-09 01:52:10 +00:00
CACHE_REGS
2015-04-13 13:28:17 +01:00
RECOVER_MACHINE_REGS();
2016-09-27 18:28:54 +01:00
Term tt = cl.gt();
Term sourcet;
Term ntt = cl.gt();
if (source.initialized())
sourcet = source.gt();
else
sourcet = TermZERO;
2016-09-30 23:11:13 +01:00
yamop *codeaddr = Yap_cclause(tt, ap->ArityOfPE, Yap_CurrentModule(),
sourcet); /* vsc: give the number of arguments
to cclause in case there is overflow */
2015-04-13 13:28:17 +01:00
if (LOCAL_ErrorMessage) {
RECOVER_MACHINE_REGS();
return 0;
2015-04-13 13:28:17 +01:00
}
2015-02-09 01:52:10 +00:00
Term *tref = &ntt;
if (Yap_addclause(ntt, codeaddr, (last ? TermAssertz : TermAsserta),
Yap_CurrentModule(), tref)) {
2015-04-13 13:28:17 +01:00
RECOVER_MACHINE_REGS();
}
return tref;
2015-02-09 01:52:10 +00:00
}
2016-09-27 18:28:54 +01:00
2016-09-30 23:11:13 +01:00
void *YAPPrologPredicate::assertFact(YAPTerm *cl, bool last) {
CACHE_REGS
2016-12-10 07:01:10 +00:00
arity_t i;
2016-09-30 23:11:13 +01:00
RECOVER_MACHINE_REGS();
Term tt = AbsAppl(HR);
*HR++ = (CELL)(ap->FunctorOfPred);
2016-12-10 07:01:10 +00:00
for (i = 0; i < ap->ArityOfPE; i++, cl++)
*HR++ = cl->gt();
2016-09-30 23:11:13 +01:00
yamop *codeaddr = Yap_cclause(tt, ap->ArityOfPE, Yap_CurrentModule(),
tt); /* vsc: give the number of arguments
to cclause in case there is overflow */
if (LOCAL_ErrorMessage) {
RECOVER_MACHINE_REGS();
return 0;
}
Term *tref = &tt;
if (Yap_addclause(tt, codeaddr, (last ? TermAssertz : TermAsserta),
Yap_CurrentModule(), tref)) {
RECOVER_MACHINE_REGS();
}
return tref;
}
void *YAPPrologPredicate::retractClause(YAPTerm skeleton, bool all) {
2015-02-09 01:52:10 +00:00
return 0;
}
2016-09-21 20:41:23 +01:00
const char *YAPError::text() {
2016-09-27 18:28:54 +01:00
2016-09-23 03:53:42 +01:00
char buf[256];
2016-09-21 20:41:23 +01:00
std::string s = "";
2016-10-20 04:44:59 +01:00
if (LOCAL_ActiveError->errorFunction) {
s += LOCAL_ActiveError->errorFile;
2016-09-21 20:41:23 +01:00
s += ":";
2016-10-20 04:44:59 +01:00
sprintf(buf, "%ld", (long int)LOCAL_ActiveError->errorLine);
2016-09-27 18:28:54 +01:00
s += buf;
2016-09-23 03:53:42 +01:00
s += ":0 in C-code";
2016-09-21 20:41:23 +01:00
}
2016-10-20 04:44:59 +01:00
if (LOCAL_ActiveError->prologPredLine) {
2016-09-27 18:28:54 +01:00
s += "\n";
2016-10-20 04:44:59 +01:00
s += LOCAL_ActiveError->prologPredFile->StrOfAE;
2016-09-27 18:28:54 +01:00
s += ":";
2016-10-20 04:44:59 +01:00
sprintf(buf, "%ld", (long int)LOCAL_ActiveError->prologPredLine);
s += buf; // std::to_string(LOCAL_ActiveError->prologPredLine) ;
// YAPIntegerTerm(LOCAL_ActiveError->prologPredLine).text();
2016-09-27 18:28:54 +01:00
s += ":0 ";
2016-10-20 04:44:59 +01:00
s += LOCAL_ActiveError->prologPredModule;
2016-09-27 18:28:54 +01:00
s += ":";
2016-10-20 04:44:59 +01:00
s += (LOCAL_ActiveError->prologPredName)->StrOfAE;
2016-09-27 18:28:54 +01:00
s += "/";
2016-10-20 04:44:59 +01:00
sprintf(buf, "%ld", (long int)LOCAL_ActiveError->prologPredArity);
s += // std::to_string(LOCAL_ActiveError->prologPredArity);
2016-09-27 18:28:54 +01:00
buf;
2016-09-23 03:53:42 +01:00
}
s += " error ";
2016-10-20 04:44:59 +01:00
if (LOCAL_ActiveError->classAsText != nullptr)
s += LOCAL_ActiveError->classAsText->StrOfAE;
2016-09-27 18:28:54 +01:00
s += ".";
2016-10-20 04:44:59 +01:00
s += LOCAL_ActiveError->errorAsText->StrOfAE;
2016-09-27 18:28:54 +01:00
s += ".\n";
2016-10-20 04:44:59 +01:00
if (LOCAL_ActiveError->errorTerm) {
Term t = Yap_PopTermFromDB(LOCAL_ActiveError->errorTerm);
2016-09-21 20:41:23 +01:00
if (t) {
s += "error term is: ";
s += YAPTerm(t).text();
s += "\n";
}
}
printf("%s\n", s.c_str());
return s.c_str();
}
2016-10-16 23:18:51 +01:00
2016-12-10 07:01:10 +00:00
void YAPEngine::reSet() {
2016-10-16 23:18:51 +01:00
/* ignore flags for now */
BACKUP_MACHINE_REGS();
Yap_RebootHandles(worker_id);
2016-12-10 07:01:10 +00:00
while (B->cp_b)
B = B->cp_b;
P = FAILCODE;
Yap_exec_absmi(true, YAP_EXEC_ABSMI);
/* recover stack space */
HR = B->cp_h;
TR = B->cp_tr;
2016-10-16 23:18:51 +01:00
#ifdef DEPTH_LIMIT
2016-12-10 07:01:10 +00:00
DEPTH = B->cp_depth;
2016-10-16 23:18:51 +01:00
#endif /* DEPTH_LIMIT */
2016-12-10 07:01:10 +00:00
YENV = ENV = B->cp_env;
2016-10-16 23:18:51 +01:00
RECOVER_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
}