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

1196 lines
28 KiB
C++
Raw Normal View History

2014-04-28 11:47:09 +01:00
2018-11-23 14:00:20 +00:00
extern "C" {
2018-06-18 12:16:36 +01:00
#include "inline-only.h"
2017-06-12 18:00:47 +01:00
#define _EXPORT_KERNEL 1
2018-11-23 14:00:20 +00:00
}
2014-04-28 11:47:09 +01:00
#include "yapi.hh"
2018-01-18 14:47:27 +00:00
extern "C" {
2016-07-31 16:22:24 +01:00
#if __ANDROID__
#include "android/log.h"
#endif
2018-03-19 14:26:29 +00:00
#if YAP_PYTHON
2018-03-17 10:38:56 +00:00
#include "Python.h"
#endif
2018-02-23 14:09:58 +00:00
#include "YapBlobs.h"
2018-03-26 11:03:08 +01:00
#include "YapInterface.h"
2018-02-14 00:13:13 +00:00
#include "iopreds.h"
2018-10-10 12:33:05 +01:00
#include "YapInit.h"
2015-09-21 23:05:36 +01:00
2018-07-10 23:21:19 +01:00
X_API char *Yap_TermToBuffer(Term t, int flags);
2017-12-20 00:29:15 +00:00
X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, arity_t arity);
X_API void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, arity_t,
YAP_Term);
X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred,
YAP_Arity, YAP_Arity);
2017-05-14 11:36:09 +01:00
2017-09-23 02:17:55 +01:00
#if YAP_PYTHON
2017-12-20 00:29:15 +00:00
X_API bool do_init_python(void);
2017-09-23 02:17:55 +01:00
#endif
2018-05-22 21:33:34 +01:00
}
2018-07-10 23:21:19 +01:00
static void YAPCatchError() {
if (LOCAL_CommittedError != nullptr &&
LOCAL_CommittedError->errorNo != YAP_NO_ERROR) {
// Yap_PopTermFromDB(info->errorTerm);
// throw throw YAPError( );
Term es[2];
es[0] = TermError;
es[1] = MkErrorTerm(LOCAL_CommittedError);
Functor f = Yap_MkFunctor(Yap_LookupAtom("print_message"), 2);
YAP_RunGoalOnce(Yap_MkApplTerm(f, 2, es));
// Yap_PopTermFromDB(info->errorTerm);
// throw throw YAPError( SOURCE(), );
} else if (LOCAL_ActiveError != nullptr &&
LOCAL_ActiveError->errorNo != YAP_NO_ERROR) {
// Yap_PopTermFromDB(info->errorTerm);
// throw throw YAPError( );
Term es[2];
es[0] = TermError;
es[1] = MkErrorTerm(LOCAL_ActiveError);
Functor f = Yap_MkFunctor(Yap_LookupAtom("print_message"), 2);
YAP_RunGoalOnce(Yap_MkApplTerm(f, 2, es));
// Yap_PopTermFromDB(info->errorTerm);
// throw throw YAPError( SOURCE(), );
}
}
2015-02-09 01:52:10 +00:00
2018-03-26 11:03:08 +01:00
YAPPredicate::YAPPredicate(Term &t, Term &tmod, CELL *&ts, const char *pname) {
Term t0 = t;
ap = nullptr;
restart:
if (IsVarTerm(t)) {
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), INSTANTIATION_ERROR, t0, pname);
2018-03-26 11:03:08 +01:00
} else if (IsAtomTerm(t)) {
ap = RepPredProp(Yap_GetPredPropByAtom(AtomOfTerm(t), tmod));
ts = nullptr;
} else if (IsIntegerTerm(t) && tmod == IDB_MODULE) {
ts = nullptr;
ap = Yap_FindLUIntKey(IntegerOfTerm(t));
} else if (IsPairTerm(t)) {
t = Yap_MkApplTerm(FunctorCsult, 1, &t);
goto restart;
} else if (IsApplTerm(t)) {
Functor fun = FunctorOfTerm(t);
if (IsExtensionFunctor(fun)) {
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE,
2019-02-28 21:57:48 +00:00
Yap_TermToIndicator(t, tmod), pname);
2018-03-26 11:03:08 +01:00
}
if (fun == FunctorModule) {
tmod = ArgOfTerm(1, t);
if (IsVarTerm(tmod)) {
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), INSTANTIATION_ERROR, t0, pname);
2018-03-26 11:03:08 +01:00
}
if (!IsAtomTerm(tmod)) {
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), TYPE_ERROR_ATOM, t0, pname);
2018-03-26 11:03:08 +01:00
}
t = ArgOfTerm(2, t);
goto restart;
2018-01-18 14:47:27 +00:00
}
2018-03-26 11:03:08 +01:00
ap = RepPredProp(Yap_GetPredPropByFunc(fun, tmod));
ts = RepAppl(t) + 1;
} else {
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t0, pname);
2018-03-26 11:03:08 +01:00
}
2018-01-18 14:47:27 +00:00
}
2018-05-21 14:45:24 +01:00
Term YAPTerm::getArg(arity_t i) {
BACKUP_MACHINE_REGS();
Term tf = 0;
Term t0 = gt();
if (IsApplTerm(t0)) {
if (i > ArityOfFunctor(FunctorOfTerm(t0)))
throw YAPError(SOURCE(), DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()");
tf = (ArgOfTerm(i, t0));
} else if (IsPairTerm(t0)) {
if (i == 1)
tf = (HeadOfTerm(t0));
else if (i == 2)
tf = (TailOfTerm(t0));
else
throw YAPError(SOURCE(), DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()");
} else {
throw YAPError(SOURCE(), TYPE_ERROR_COMPOUND, t0, "t0.getArg()");
2018-05-20 00:47:27 +01:00
}
2018-05-21 14:45:24 +01:00
RECOVER_MACHINE_REGS();
return tf;
}
2018-05-20 00:47:27 +01:00
2018-05-08 23:42:02 +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
2017-09-06 01:16:36 +01:00
seq_tv_t inp, out;
2018-07-15 13:46:26 +01:00
inp.enc = LOCAL_encoding;
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
}
2017-12-20 00:29:15 +00: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;
2018-07-15 13:46:26 +01:00
inp.enc = LOCAL_encoding;
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
}
2017-12-20 00:29:15 +00: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
}
2017-12-20 00:29:15 +00: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
}
2017-12-20 00:29:15 +00: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
2018-07-03 00:54:34 +01:00
Term ts = MkStringTerm(s);
mk(ts);
2014-06-11 19:27:54 +01:00
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2017-12-20 00:29:15 +00: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
}
2017-12-20 00:29:15 +00: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)
2017-12-20 00:29:15 +00:00
: 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
}
2017-12-20 00:29:15 +00:00
YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) {
2015-04-13 13:28:17 +01:00
BACKUP_H();
2016-07-31 16:22:24 +01:00
arity_t arity = ArityOfFunctor(f.f);
2019-04-24 12:10:06 +01:00
Term o = Yap_MkNewApplTerm(f.f, arity);
2017-12-20 00:29:15 +00:00
Term *tt = RepAppl(o) + 1;
2016-07-31 16:22:24 +01:00
for (arity_t i = 0; i < arity; i++)
2019-04-24 12:10:06 +01:00
tt[i] = ts[i].term();
2017-05-27 22:54:00 +01:00
mk(o);
2015-04-13 13:28:17 +01:00
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2018-08-07 17:20:43 +01:00
YAPApplTerm::YAPApplTerm(const std::string f, std::vector<Term> ts) {
BACKUP_H();
arity_t arity = ts.size();
Functor ff = Yap_MkFunctor(Yap_LookupAtom(f.c_str()), arity);
Term o = Yap_MkNewApplTerm(ff, arity);
Term *tt = RepAppl(o) + 1;
for (arity_t i = 0; i < arity; i++)
tt[i] = ts[i];
mk(o);
RECOVER_H();
}
YAPApplTerm::YAPApplTerm(const std::string f, std::vector<YAPTerm> ts) {
BACKUP_H();
arity_t arity = ts.size();
Functor ff = Yap_MkFunctor(Yap_LookupAtom(f.c_str()), arity);
Term o = Yap_MkNewApplTerm(ff, arity);
Term *tt = RepAppl(o) + 1;
for (arity_t i = 0; i < arity; i++)
tt[i] = ts[i].term();
mk(o);
2018-07-15 13:46:26 +01:00
RECOVER_H();
}
2018-08-07 17:20:43 +01:00
YAPApplTerm::YAPApplTerm(const std::string f, YAPTerm a1) {
2018-07-15 13:46:26 +01:00
BACKUP_H();
arity_t arity = 1;
Functor ff = Yap_MkFunctor(Yap_LookupAtom(f.c_str()), arity);
Term o = Yap_MkNewApplTerm(ff, arity);
Term *tt = RepAppl(o) + 1;
tt[0] = a1.term();
mk(o);
RECOVER_H();
}
2018-08-07 17:20:43 +01:00
YAPApplTerm::YAPApplTerm(const std::string f, YAPTerm a1, YAPTerm a2) {
2018-07-15 13:46:26 +01:00
BACKUP_H();
arity_t arity = 2;
Functor ff = Yap_MkFunctor(Yap_LookupAtom(f.c_str()), arity);
Term o = Yap_MkNewApplTerm(ff, arity);
Term *tt = RepAppl(o) + 1;
tt[0] = a1.term();
tt[1] = a2.term();
2017-05-27 22:54:00 +01:00
mk(o);
2018-07-15 13:46:26 +01:00
RECOVER_H();
}
2018-08-07 17:20:43 +01:00
YAPApplTerm::YAPApplTerm(const std::string f, YAPTerm a1, YAPTerm a2, YAPTerm a3) {
2018-07-15 13:46:26 +01:00
BACKUP_H();
arity_t arity = 3;
Functor ff = Yap_MkFunctor(Yap_LookupAtom(f.c_str()), arity);
Term o = Yap_MkNewApplTerm(ff, arity);
Term *tt = RepAppl(o) + 1;
tt[0] = a1.term();
tt[2] = a2.term();
tt[3] = a3.term();
mk(o);
RECOVER_H();
}
2018-08-07 17:20:43 +01:00
YAPApplTerm::YAPApplTerm(const std::string f, YAPTerm a1, YAPTerm a2, YAPTerm a3, YAPTerm a4) {
2018-07-15 13:46:26 +01:00
BACKUP_H();
arity_t arity = 4;
Functor ff = Yap_MkFunctor(Yap_LookupAtom(f.c_str()), arity);
Term o = Yap_MkNewApplTerm(ff, arity);
Term *tt = RepAppl(o) + 1;
tt[0] = a1.term();
tt[2] = a2.term();
tt[3] = a3.term();
tt[4] = a4.term();
mk(o);
RECOVER_H();
2016-09-21 20:41:23 +01:00
}
2017-12-20 00:29:15 +00: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
}
2017-12-20 00:29:15 +00:00
Term &YAPTerm::operator[](arity_t i) {
2016-07-31 16:22:24 +01:00
BACKUP_MACHINE_REGS();
Term t0 = gt();
2018-03-26 11:03:08 +01:00
Term *tf = nullptr;
2017-12-20 00:29:15 +00:00
if (IsApplTerm(t0)) {
2016-12-10 07:01:10 +00:00
// Functor f = FunctorOfTerm(t0);
// if (IsExtensionFunctor(f))
// return 0;
2018-03-26 11:03:08 +01:00
tf = RepAppl(t0) + (i + 1);
2017-12-20 00:29:15 +00:00
} else if (IsPairTerm(t0)) {
2016-07-31 16:22:24 +01:00
if (i == 0)
2018-03-26 11:03:08 +01:00
tf = RepPair(t0);
2016-07-31 16:22:24 +01:00
else if (i == 1)
2018-03-26 11:03:08 +01:00
tf = RepPair(t0) + 1;
2016-12-10 07:01:10 +00:00
RECOVER_MACHINE_REGS();
2018-03-12 15:11:59 +00:00
} else {
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), TYPE_ERROR_COMPOUND, t0, "");
2018-03-26 11:03:08 +01:00
}
RECOVER_MACHINE_REGS();
return *tf;
2014-04-29 11:45:19 +01:00
}
2017-12-20 00:29:15 +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;
2017-12-20 00:29:15 +00:00
while (IsPairTerm(t0)) {
if (i == 0) {
2018-03-17 10:38:56 +00:00
2016-07-31 16:22:24 +01:00
tf = HeadOfTerm(t0);
break;
2017-12-20 00:29:15 +00:00
} else {
2016-07-31 16:22:24 +01:00
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
2017-12-20 00:29:15 +00:00
YAPPairTerm::YAPPairTerm(YAPTerm th, YAPTerm tl) {
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
}
2017-12-20 00:29:15 +00:00
YAPPairTerm::YAPPairTerm() {
2015-04-13 13:28:17 +01:00
BACKUP_H();
2017-05-02 07:38:23 +01:00
mk(TermNil);
2015-04-13 13:28:17 +01:00
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2018-05-20 00:47:27 +01:00
std::vector<Term> YAPPairTerm::listToArray() {
2018-05-21 14:45:24 +01:00
Term *tailp;
Term t1 = gt();
Int l = Yap_SkipList(&t1, &tailp);
if (l < 0) {
throw YAPError(SOURCE(), TYPE_ERROR_LIST, (t), nullptr);
2018-05-20 00:47:27 +01:00
}
2018-07-10 23:21:19 +01:00
std::vector<Term> o = *new std::vector<Term>(l);
2018-05-21 14:45:24 +01:00
int i = 0;
Term t = gt();
while (t != TermNil) {
o[i++] = HeadOfTerm(t);
t = TailOfTerm(t);
}
return o;
}
2018-05-20 00:47:27 +01:00
2019-03-18 14:47:29 +00:00
std::vector<YAPTerm> YAPPairTerm::listToVector() {
Term *tailp;
Term t1 = gt();
Int l = Yap_SkipList(&t1, &tailp);
if (l < 0) {
throw YAPError(SOURCE(), TYPE_ERROR_LIST, (t), nullptr);
}
std::vector<YAPTerm> o = *new std::vector<YAPTerm>(l);
int i = 0;
Term t = gt();
while (t != TermNil) {
o[i++] = YAPTerm(HeadOfTerm(t));
t = TailOfTerm(t);
}
return o;
}
2017-12-20 00:29:15 +00:00
YAP_tag_t YAPTerm::tag() {
Term tt = gt();
2017-12-20 00:29:15 +00:00
if (IsVarTerm(tt)) {
CELL *pt = VarOfTerm(tt);
2017-12-20 00:29:15 +00:00
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;
2017-12-20 00:29:15 +00:00
if (IsAtomOrIntTerm(tt)) {
if (IsAtomTerm(tt))
return YAP_TAG_ATOM;
return YAP_TAG_INT;
2017-12-20 00:29:15 +00:00
} else {
2015-03-16 17:25:09 +00:00
Functor f = FunctorOfTerm(tt);
2014-06-11 19:27:54 +01:00
2017-12-20 00:29:15 +00:00
if (IsExtensionFunctor(f)) {
if (f == FunctorDBRef) {
return YAP_TAG_DBREF;
}
2017-12-20 00:29:15 +00:00
if (f == FunctorLongInt) {
return YAP_TAG_LONG_INT;
}
2017-12-20 00:29:15 +00:00
if (f == FunctorBigInt) {
big_blob_type bt = (big_blob_type)RepAppl(tt)[1];
2017-12-20 00:29:15 +00:00
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
}
2017-12-20 00:29:15 +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
}
2018-05-20 00:47:27 +01:00
Term YAPListTerm::cdr() {
2018-05-21 14:45:24 +01:00
Term to = gt();
if (IsPairTerm(to))
return (TailOfTerm(to));
else if (to == TermNil)
return TermNil;
/* error */
throw YAPError(SOURCE(), TYPE_ERROR_LIST, to, "");
}
2018-05-20 00:47:27 +01:00
2017-12-20 00:29:15 +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
}
2017-12-20 00:29:15 +00:00
intptr_t YAPTerm::numberVars(intptr_t i0, bool skip_singletons) {
2016-07-31 16:22:24 +01:00
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
2017-12-20 00:29:15 +00: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
*/
2017-12-20 00:29:15 +00:00
YAPTerm::YAPTerm(void *ptr) {
2015-07-06 12:01:55 +01:00
CACHE_REGS
mk(MkIntegerTerm((Int)ptr));
2015-07-06 12:01:55 +01:00
}
2014-06-11 19:27:54 +01:00
2017-12-20 00:29:15 +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));
2017-12-20 00:29:15 +00:00
else {
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), TYPE_ERROR_LIST, to, "");
2018-03-12 15:11:59 +00:00
return TermUnique;
2016-09-21 20:41:23 +01:00
}
}
2017-12-20 00:29:15 +00:00
YAPListTerm::YAPListTerm(YAPTerm ts[], arity_t n) {
2016-07-31 16:22:24 +01:00
CACHE_REGS
BACKUP_H();
if (n == 0)
t = TermNil;
2017-12-20 00:29:15 +00:00
while (HR + n * 2 > ASP - 1024) {
2016-07-31 16:22:24 +01:00
RECOVER_H();
2017-12-20 00:29:15 +00:00
if (!Yap_dogc(0, NULL PASS_REGS)) {
2016-07-31 16:22:24 +01:00
t = TermNil;
}
BACKUP_H();
}
t = AbsPair(HR);
2017-12-20 00:29:15 +00:00
for (arity_t i = 0; i < n; i++) {
2016-07-31 16:22:24 +01:00
HR[2 * i] = ts[i].gt();
HR[2 * i + 1] = AbsPair(HR + (2 * i + 2));
}
}
2017-09-26 15:15:15 +01:00
const char *YAPAtom::getName(void) { return Yap_AtomToUTF8Text(a); }
2016-10-16 23:18:51 +01:00
2017-12-20 00:29:15 +00:00
void YAPQuery::openQuery() {
2016-12-10 07:01:10 +00:00
CACHE_REGS
2018-07-10 23:21:19 +01:00
if (ap == NULL || ap->OpcodeOfPred == UNDEF_OPCODE) {
2017-12-20 00:29:15 +00:00
ap = rewriteUndefQuery();
2016-12-10 07:01:10 +00:00
}
2017-05-19 09:56:37 +01:00
setNext();
2016-12-10 07:01:10 +00:00
}
2015-07-06 12:01:55 +01:00
2017-12-20 00:29:15 +00:00
bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) {
2018-03-26 11:03:08 +01:00
CACHE_REGS
if (ap.ap == NULL)
return false;
BACKUP_MACHINE_REGS();
arity_t arity = ap.getArity();
bool result;
YAP_dogoalinfo q;
2016-11-11 07:23:34 +00:00
2018-03-26 11:03:08 +01:00
q.CurSlot = Yap_StartSlots();
q.p = P;
2018-03-26 11:03:08 +01:00
q.cp = CP;
2019-05-01 01:34:58 +01:00
q.b0 = LCL0-CellPtr(B);
q.env0 = LCL0-ENV;
2018-05-26 00:22:27 +01:00
for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = ts[i].term();
2018-03-26 11:03:08 +01:00
// allow Prolog style exceotion handling
// don't forget, on success these bindings will still be there);
2019-05-01 01:34:58 +01:00
result = YAP_EnterGoal(ap.ap, nullptr, &q);
YAP_LeaveGoal(result, &q);
2018-03-26 11:03:08 +01:00
2018-05-22 21:33:34 +01:00
YAPCatchError();
2018-03-26 11:03:08 +01:00
RECOVER_MACHINE_REGS();
return result;
2017-02-20 14:38:00 +00:00
}
2016-09-27 18:28:54 +01:00
2018-05-25 12:30:31 +01:00
bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
2018-03-19 14:26:29 +00:00
#if YAP_PYTHON
2018-07-21 01:56:48 +01:00
// std::cerr << "mgoal(in) " << YAPTerm(tmod).text() << ":" << YAPTerm(t).text() << "\n";
2018-03-26 11:03:08 +01:00
// PyThreadState *_save;
2018-03-17 10:38:56 +00:00
2018-07-17 11:43:57 +01:00
// std::cerr << "mgoal " << YAPTerm(t).text() << "\n";
2018-03-18 00:44:08 +00:00
// _save = PyEval_SaveThread();
2018-03-19 14:26:29 +00:00
#endif
2018-03-26 11:03:08 +01:00
CACHE_REGS
2019-05-01 01:34:58 +01:00
YAP_dogoalinfo q;
2018-03-26 11:03:08 +01:00
BACKUP_MACHINE_REGS();
2019-05-17 06:08:00 +01:00
2019-04-24 12:10:06 +01:00
Term *ts = nullptr;
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
2019-04-27 17:55:22 +01:00
Int oenv = LCL0-ENV;
Int oB = LCL0-CellPtr(B);
2019-04-24 12:10:06 +01:00
Term omod = CurrentModule;
PredEntry *ap = nullptr;
if (IsStringTerm(tmod))
tmod = MkAtomTerm(Yap_LookupAtom(StringOfTerm(tmod)));
ap = Yap_get_pred(t, tmod, "C++");
if (ap == nullptr ||
ap->OpcodeOfPred == UNDEF_OPCODE) {
ap = rewriteUndefEngineQuery(ap, t, tmod);
}
if (IsApplTerm(t))
ts = RepAppl(t) + 1;
else if (IsPairTerm(t))
ts = RepPair(t);
/* legal ap */
arity_t arity = ap->ArityOfPE;
for (arity_t i = 0; i < arity; i++) {
XREGS[i + 1] = ts[i];
}
ts = nullptr;
bool result;
// allow Prolog style exception handling
// don't forget, on success these guys may create slots
//__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
result = (bool)YAP_EnterGoal(ap, nullptr, &q);
2019-04-27 17:55:22 +01:00
// std::cerr << "mgoal " << YAPTerm(tmod).text() << ":" << YAPTerm(t).text() << "\n
2019-04-24 12:10:06 +01:00
YAP_LeaveGoal(result && !release, &q);
2019-04-27 17:55:22 +01:00
ENV = LCL0-oenv;
B = (choiceptr)(LCL0-oB);
2019-04-24 12:10:06 +01:00
CurrentModule = LOCAL_SourceModule = omod;
// PyEval_RestoreThread(_save);
2019-04-12 17:32:26 +01:00
RECOVER_MACHINE_REGS();
2019-04-24 12:10:06 +01:00
return result;
2014-04-28 11:47:09 +01:00
}
2018-05-25 12:30:31 +01:00
/**
* called when a query must be terminated and its state fully recovered,
* @type {[type]}
*/
2017-12-20 00:29:15 +00:00
void YAPEngine::release() {
2016-10-16 23:18:51 +01:00
BACKUP_MACHINE_REGS();
2019-05-01 01:34:58 +01: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
2017-12-20 00:29:15 +00:00
Term YAPEngine::fun(Term t) {
2016-10-16 23:18:51 +01:00
CACHE_REGS
2018-03-26 11:03:08 +01:00
BACKUP_MACHINE_REGS();
2019-05-01 01:34:58 +01:00
YAP_dogoalinfo q;
2018-07-17 11:43:57 +01:00
Term tmod = Yap_CurrentModule(), *ts = nullptr;
2018-03-26 11:03:08 +01:00
PredEntry *ap;
arity_t arity;
Functor f;
Atom name;
2019-04-09 18:06:27 +01:00
YAP_dogoalinfo backup = q;
2019-05-15 18:48:56 +01:00
CELL *spt;
2019-04-27 17:55:22 +01:00
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
2018-03-26 11:03:08 +01:00
2019-04-27 17:55:22 +01:00
Int oenv = LCL0-ENV;
Int oB = LCL0-CellPtr(B);
2019-05-17 06:08:00 +01:00
2018-03-26 11:03:08 +01:00
if (IsApplTerm(t)) {
ts = RepAppl(t) + 1;
f = (Functor)ts[-1];
name = NameOfFunctor(f);
arity = ArityOfFunctor(f);
for (arity_t i = 0; i < arity; i++)
2019-05-15 18:48:56 +01:00
HR[i + 1] = ts[i];
arity++;
2018-03-26 11:03:08 +01:00
} else if (IsAtomTerm(t)) {
name = AtomOfTerm(t);
f = nullptr;
2019-05-15 18:48:56 +01:00
arity = 1;
2018-03-26 11:03:08 +01:00
} else if (IsPairTerm(t)) {
2019-05-15 18:48:56 +01:00
HR[1] = ts[0];
HR[2] = ts[1];
arity = 3;
2018-03-26 11:03:08 +01:00
name = AtomDot;
f = FunctorDot;
} else {
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
2018-03-26 11:03:08 +01:00
return 0L;
}
2019-04-24 12:10:06 +01:00
Term ot = XREGS[arity + 1] = MkVarTerm();
yhandle_t h = Yap_InitHandle(ot);
arity++;
HR += arity;
f = Yap_MkFunctor(name, arity);
ap = (PredEntry *)(PredPropByFunc(f, tmod));
if (ap == nullptr || ap->OpcodeOfPred == UNDEF_OPCODE) {
Term g = (Yap_MkApplTerm(f, arity, ts));
ap = rewriteUndefEngineQuery(ap, g, (ap->ModuleOfPred));
}
// make sure this is safe
// allow Prolog style exception handling
//__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
bool result = (bool)YAP_EnterGoal(ap, nullptr, &q);
if (result)
ot = Yap_GetFromHandle(h);
else
ot = TermNone;
YAPCatchError();
{
YAP_LeaveGoal(result, &q);
2019-04-27 17:55:22 +01:00
ENV = LCL0-oenv;
B = (choiceptr)(LCL0-oB);
2019-04-24 12:10:06 +01:00
// PyEval_RestoreThread(_save);
RECOVER_MACHINE_REGS();
return ot;
}
2019-05-17 06:08:00 +01:00
}
2016-10-16 23:18:51 +01:00
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
2017-12-20 00:29:15 +00:00
: YAPPredicate(f, mod) {
2017-06-16 11:53:46 +01:00
2014-06-11 19:27:54 +01:00
/* ignore flags for now */
2016-09-21 20:41:23 +01:00
BACKUP_MACHINE_REGS();
2017-09-06 01:16:36 +01:00
Term goal;
2017-05-27 22:54:00 +01:00
2017-12-20 00:29:15 +00:00
if (ts) {
2017-05-27 22:54:00 +01:00
2017-12-20 00:29:15 +00:00
goal = YAPApplTerm(f, ts).term();
nts = RepAppl(goal) + 1;
size_t arity = f.arity();
for (arity_t i = 0; i < arity; i++)
2017-12-20 00:29:15 +00:00
XREGS[i + 1] = nts[i];
} else {
goal = MkVarTerm();
}
openQuery();
2018-06-18 12:16:36 +01:00
names = YAPPairTerm(TermNil);
2017-09-06 01:16:36 +01:00
RECOVER_MACHINE_REGS();
2014-04-29 11:45:19 +01:00
}
2014-04-28 11:47:09 +01:00
2019-04-03 10:39:22 +01:00
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, Term ts[])
: YAPPredicate(f, mod) {
/* ignore flags for now */
BACKUP_MACHINE_REGS();
Term goal;
if (ts) {
size_t arity = f.arity();
goal = Yap_MkApplTerm(Yap_MkFunctor(f.name().asAtom(),arity), arity, ts);
nts = RepAppl(goal) + 1;
for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = ts[i];
} else {
goal = MkVarTerm();
}
openQuery();
names = YAPPairTerm(TermNil);
RECOVER_MACHINE_REGS();
}
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();
2017-05-27 22:54:00 +01:00
if (ts) {
2018-05-30 08:12:51 +01:00
goal = YAPApplTerm(f, nts);
2017-05-27 22:54:00 +01:00
} else {
2017-09-06 01:16:36 +01:00
goal = YAPVarTerm();
2017-05-27 22:54:00 +01:00
nts = nullptr;
}
2017-09-06 01:16:36 +01:00
names = YAPPairTerm( TermNil );
openQuery(term(), nts);
2016-09-21 20:41:23 +01:00
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
2017-05-27 22:54:00 +01:00
YAPQuery::YAPQuery(YAPPredicate p, YAPTerm ts[]) : YAPPredicate(p.ap) {
BACKUP_MACHINE_REGS();
2018-05-24 12:00:10 +01:00
try {
arity_t arity = p.ap->ArityOfPE;
2018-07-10 23:21:19 +01:00
if (arity) {
goal = YAPApplTerm(YAPFunctor(p.ap->FunctorOfPred), ts).term();
for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = ts[i].term();
openQuery();
} else {
goal = MkAtomTerm((Atom)(p.ap->FunctorOfPred));
openQuery();
}
names = TermNil;
} catch (...) {
2017-09-06 01:16:36 +01:00
}
2018-07-10 23:21:19 +01:00
RECOVER_MACHINE_REGS();
2014-04-28 11:47:09 +01:00
}
2017-12-20 00:29:15 +00:00
bool YAPQuery::next() {
2014-06-11 19:27:54 +01:00
CACHE_REGS
2017-12-20 00:29:15 +00:00
bool result = false;
2018-07-10 23:21:19 +01:00
// std::cerr << "next " << YAPTerm(goal).text() << "\n";
2019-04-27 17:55:22 +01:00
q_h.CurSlot = Yap_StartSlots();
q_h.p = P;
q_h.cp = CP;
2018-05-28 09:31:59 +01:00
2017-12-20 00:29:15 +00:00
sigjmp_buf buf, *oldp = LOCAL_RestartEnv;
2018-03-26 11:03:08 +01:00
e = nullptr;
BACKUP_MACHINE_REGS();
if (!q_open)
return false;
LOCAL_RestartEnv = &buf;
// don't forget, on success these guys may create slots
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
2018-03-12 15:11:59 +00:00
2018-03-26 11:03:08 +01:00
if (q_state == 0) {
2018-07-10 23:21:19 +01:00
// Yap_do_low_level_trace = 1;
2018-03-26 11:03:08 +01:00
result = (bool)YAP_EnterGoal(ap, nullptr, &q_h);
} else {
LOCAL_AllowRestart = q_open;
result = (bool)YAP_RetryGoal(&q_h);
}
q_state = 1;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "out %d", result);
2018-07-10 23:21:19 +01:00
if (!result) {
2018-05-24 21:45:38 +01:00
YAP_LeaveGoal(result, &q_h);
2017-09-06 01:16:36 +01:00
q_open = false;
2018-05-29 09:59:28 +01:00
}
2018-07-10 23:21:19 +01:00
YAPCatchError();
2018-03-26 11:03:08 +01:00
RECOVER_MACHINE_REGS();
LOCAL_RestartEnv = oldp;
return result;
2014-04-28 11:47:09 +01:00
}
2017-12-20 00:29:15 +00:00
PredEntry *YAPQuery::rewriteUndefQuery() {
2018-07-10 23:21:19 +01:00
ARG1 = goal = Yap_SaveTerm(Yap_MkApplTerm(FunctorCall, 1, &goal));
2018-01-18 14:47:27 +00:00
return ap = PredCall;
2017-06-05 13:06:12 +01:00
}
2018-03-02 21:18:24 +00:00
PredEntry *YAPEngine::rewriteUndefEngineQuery(PredEntry *a, Term &tgoal,
2017-12-20 00:29:15 +00:00
Term mod) {
2018-06-02 10:36:04 +01:00
tgoal = Yap_MkApplTerm(FunctorCall, 1, &tgoal);
2019-01-09 09:32:09 +00:00
LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
2018-01-18 14:47:27 +00:00
return PredCall;
2018-03-17 10:38:56 +00:00
2017-12-20 00:29:15 +00:00
// return YAPApplTerm(FunctorUndefinedQuery, ts);
2017-06-05 13:06:12 +01:00
}
2017-12-20 00:29:15 +00: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;
2018-05-24 12:00:10 +01:00
YAP_LeaveGoal(true, &q_h);
q_open = false;
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
}
2017-12-20 00:29:15 +00:00
bool YAPQuery::deterministic() {
2016-04-12 16:22:53 +01:00
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;
2019-04-27 17:55:22 +01:00
choiceptr myB = (choiceptr)(LCL0 - q_h.b_entry);
2016-04-12 16:22:53 +01:00
return (B >= myB);
RECOVER_MACHINE_REGS();
}
2016-07-31 16:22:24 +01:00
YAPTerm YAPQuery::getTerm(yhandle_t t) { return YAPTerm(t); }
2017-12-20 00:29:15 +00: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();
Yap_ResetException(worker_id);
2014-06-11 19:27:54 +01:00
/* need to implement backtracking here */
2017-12-20 00:29:15 +00:00
if (q_open != true || q_state == 0) {
RECOVER_MACHINE_REGS();
return;
2014-06-11 19:27:54 +01:00
}
2018-05-24 12:00:10 +01:00
YAP_LeaveGoal(false, &q_h);
2015-04-13 13:28:17 +01:00
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
#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);
2017-12-20 00:29:15 +00:00
JNIEXPORT jint JNICALL JNI_MySQLOnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
2017-12-20 00:29:15 +00:00
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
2017-09-06 01:16:36 +01:00
char *Yap_AndroidBufp;
static size_t Yap_AndroidMax, Yap_AndroidSz;
extern void (*Yap_DisplayWithJava)(int c);
2017-11-21 15:44:43 +00:00
static YAPCallback *cb = new YAPCallback();
2017-12-20 00:29:15 +00:00
void Yap_displayWithJava(int c) {
2017-09-06 01:16:36 +01:00
char *ptr = Yap_AndroidBufp;
if (!ptr)
ptr = Yap_AndroidBufp = (char *)malloc(Yap_AndroidSz);
ptr[Yap_AndroidSz++] = c;
2017-12-20 00:29:15 +00:00
if (Yap_AndroidMax - 1 == Yap_AndroidSz) {
if (Yap_AndroidMax < 32 * 1024) {
2017-09-06 01:16:36 +01:00
Yap_AndroidMax *= 2;
2017-12-20 00:29:15 +00:00
} else {
2017-09-06 01:16:36 +01:00
Yap_AndroidMax += 32 * 1024;
}
Yap_AndroidBufp = (char *)realloc(ptr, Yap_AndroidMax);
}
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
2017-12-20 00:29:15 +00:00
if (c == '\n') {
2017-09-06 01:16:36 +01:00
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
2017-11-21 15:44:43 +00:00
cb->run(Yap_AndroidBufp);
2017-09-06 01:16:36 +01:00
Yap_AndroidSz = 0;
}
}
2016-07-31 16:22:24 +01:00
#endif
2018-02-14 00:13:13 +00:00
void YAPEngine::doInit(YAP_file_type_t BootMode, YAPEngineArgs *engineArgs) {
2018-03-14 00:41:05 +00:00
if (BootMode == YAP_FOUND_BOOT_ERROR) {
2018-03-26 11:03:08 +01:00
std::cerr << "Exception received by " << __func__ << "( "
<< "while booting"
<< ").\n Forwarded...\n\n";
return;
2016-07-31 16:22:24 +01:00
}
2018-03-14 00:41:05 +00:00
YAP_Init(engineArgs);
2017-12-20 00:29:15 +00:00
/* Begin preprocessor code */
/* live */
2018-05-20 00:47:27 +01:00
// yerror = throw YAPError( SOURCE(), );
2017-05-14 11:36:09 +01:00
#if YAP_PYTHON
2017-12-20 00:29:15 +00:00
do_init_python();
2017-05-14 11:36:09 +01:00
#endif
2018-01-27 10:17:27 +00:00
// std::string s = "initialize_prolog";
// YAPPredicate p = YAPPredicate(MkAtomTerm(Yap_LookupAtom(s.c_str())));
// YAPQuery initq = YAPQuery(YAPPredicate(p), nullptr);
// if (initq.next()) {
// initq.cut();
// }
2017-12-20 00:29:15 +00:00
CurrentModule = TermUser;
2019-01-09 09:32:09 +00:00
LOCAL_SourceModule = TermUser;
2015-04-13 13:28:17 +01:00
}
2016-07-31 16:22:24 +01:00
YAPEngine::YAPEngine(int argc, char *argv[],
2017-11-21 15:44:43 +00:00
YAPCallback *cb)
2017-12-20 00:29:15 +00:00
: _callback(0) { // a single engine can be active
2017-05-19 09:56:37 +01:00
2017-12-20 00:29:15 +00:00
YAP_file_type_t BootMode;
engine_args = new YAPEngineArgs();
BootMode = YAP_parse_yap_arguments(argc, argv, engine_args);
// delYAPCallback()b
// if (cb)
// setYAPCallback(cb);
2017-11-21 15:44:43 +00:00
2018-02-14 00:13:13 +00:00
doInit(BootMode, engine_args);
2017-12-20 00:29:15 +00:00
}
2017-11-21 15:44:43 +00:00
2017-12-20 00:29:15 +00:00
YAPPredicate::YAPPredicate(YAPAtom at) {
CACHE_REGS
ap = RepPredProp(PredPropByAtom(at.a, Yap_CurrentModule()));
}
2015-02-09 01:52:10 +00:00
2017-12-20 00:29:15 +00:00
YAPPredicate::YAPPredicate(YAPAtom at, uintptr_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
2017-12-20 00:29:15 +00:00
/// auxiliary routine to find a predicate in the current module.
2018-07-17 11:43:57 +01:00
PredEntry *YAPPredicate::getPred(Term &t, Term &m, CELL *&out) {
2017-12-20 00:29:15 +00:00
CACHE_REGS
t = Yap_StripModule(t, &m);
2018-05-21 14:45:24 +01:00
2017-12-20 00:29:15 +00:00
if (IsVarTerm(t) || IsNumTerm(t)) {
if (IsVarTerm(t))
2018-06-01 08:37:25 +01:00
throw YAPError(SOURCE(), INSTANTIATION_ERROR, t, 0);
2017-12-20 00:29:15 +00:00
else if (IsNumTerm(t))
2018-06-01 08:37:25 +01:00
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
2017-12-20 00:29:15 +00:00
}
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
return ap;
} else if (IsPairTerm(t)) {
2018-07-10 23:21:19 +01:00
Term ts[2], *s = (out ? out : ts);
2017-12-20 00:29:15 +00:00
Functor FunctorConsult = Yap_MkFunctor(Yap_LookupAtom("consult"), 1);
2018-06-01 08:37:25 +01:00
s[1] = t;
s[0] = m;
t = Yap_MkApplTerm(FunctorModule, 2, s);
2017-12-20 00:29:15 +00:00
t = Yap_MkApplTerm(FunctorConsult, 1, &t);
2018-06-01 08:37:25 +01:00
if (!out)
out = RepAppl(t) + 1;
2017-12-20 00:29:15 +00:00
}
Functor f = FunctorOfTerm(t);
if (IsExtensionFunctor(f)) {
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
2017-12-20 00:29:15 +00:00
} else {
ap = RepPredProp(PredPropByFunc(f, m));
2018-06-01 08:37:25 +01:00
if (out)
2018-10-18 22:00:27 +01:00
memmove(out, (const CELL *)RepAppl(t) + 1, ap->ArityOfPE * sizeof(CELL));
2018-06-01 08:37:25 +01:00
else
out = RepAppl(t) + 1;
2017-12-20 00:29:15 +00:00
}
return ap;
}
2015-02-09 01:52:10 +00:00
2017-12-20 00:29:15 +00:00
bool YAPPrologPredicate::assertClause(YAPTerm cl, bool last, YAPTerm source) {
CACHE_REGS
2015-02-09 01:52:10 +00:00
2017-12-20 00:29:15 +00:00
RECOVER_MACHINE_REGS();
Term tt = cl.gt();
Term sourcet;
Term ntt = cl.gt();
if (source.initialized())
sourcet = source.gt();
else
sourcet = TermZERO;
yamop *codeaddr =
Yap_cclause(tt, ap->ArityOfPE, Yap_CurrentModule(),
sourcet); /* vsc: give the number of arguments
to cclause in case there is overflow */
if (LOCAL_ErrorMessage) {
RECOVER_MACHINE_REGS();
return false;
}
Term *tref = &ntt;
if (Yap_addclause(ntt, codeaddr, (last ? TermAssertz : TermAsserta),
Yap_CurrentModule(), tref)) {
RECOVER_MACHINE_REGS();
}
return tref;
}
2016-09-27 18:28:54 +01:00
2017-12-20 00:29:15 +00:00
bool YAPPrologPredicate::assertFact(YAPTerm *cl, bool last) {
CACHE_REGS
arity_t i;
RECOVER_MACHINE_REGS();
Term tt = AbsAppl(HR);
*HR++ = (CELL)(ap->FunctorOfPred);
for (i = 0; i < ap->ArityOfPE; i++, cl++)
*HR++ = cl->gt();
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 false;
}
Term *tref = &tt;
if (Yap_addclause(tt, codeaddr, (last ? TermAssertz : TermAsserta),
Yap_CurrentModule(), tref)) {
RECOVER_MACHINE_REGS();
}
return tref;
}
2016-09-30 23:11:13 +01:00
2017-12-20 00:29:15 +00:00
void *YAPPrologPredicate::retractClause(YAPTerm skeleton, bool all) {
return 0;
}
2016-09-21 20:41:23 +01:00
2017-12-20 00:29:15 +00:00
std::string YAPError::text() {
2018-05-24 21:45:38 +01:00
2018-05-21 14:45:24 +01:00
return "Error";
#if 0
std::stringstream s;
s << "";
2018-05-20 00:47:27 +01:00
if (info->errorNo == YAP_NO_ERROR)
return 0;
2018-05-21 14:45:24 +01:00
if (info->errorFunction) {
2018-05-20 00:47:27 +01:00
s += info->errorFile;
2017-12-20 00:29:15 +00:00
s += ":";
2018-05-20 00:47:27 +01:00
sprintf(buf, "%ld", (long int)info->errorLine);
2017-12-20 00:29:15 +00:00
s += buf;
s += ":0 in C-code";
}
2018-05-21 14:45:24 +01:00
return s;
2018-05-20 00:47:27 +01:00
if (info->prologPredLine) {
2017-12-20 00:29:15 +00:00
s += "\n";
2018-05-20 00:47:27 +01:00
s += info->prologPredFile;
2017-12-20 00:29:15 +00:00
s += ":";
2018-05-21 14:45:24 +01:00
s << info->prologPredLine;
2018-05-20 00:47:27 +01:00
// YAPIntegerTerm(info->prologPredLine).text();
2017-12-20 00:29:15 +00:00
s += ":0 ";
2018-05-20 00:47:27 +01:00
s += info->prologPredModule;
2017-12-20 00:29:15 +00:00
s += ":";
2018-05-20 00:47:27 +01:00
s += (info->prologPredName);
2017-12-20 00:29:15 +00:00
s += "/";
2018-05-21 14:45:24 +01:00
s << info->prologPredArity;
2017-12-20 00:29:15 +00:00
}
s += " error ";
2018-05-20 00:47:27 +01:00
if (info->classAsText == nullptr)
info->classAsText = Yap_errorClassName(info->errorClass);
if (info->classAsText != nullptr)
s += info->classAsText;
2017-12-20 00:29:15 +00:00
s += ".";
2018-05-20 00:47:27 +01:00
if (info->errorAsText == nullptr)
2018-05-21 14:45:24 +01:00
info->errorAsText = Yap_errorName(info->errorNo);
2018-05-20 00:47:27 +01:00
if (info->errorAsText != nullptr)
s += info->errorAsText;
2017-12-20 00:29:15 +00:00
s += ".\n";
// printf("%s\n", s.c_str());
return s.c_str();
2018-05-21 14:45:24 +01:00
#endif
2017-12-20 00:29:15 +00:00
}
2016-10-16 23:18:51 +01:00
2017-12-20 00:29:15 +00:00
void YAPEngine::reSet() {
/* ignore flags for now */
2018-07-10 23:21:19 +01:00
if (B && B->cp_b && B->cp_ap != NOCODE)
2019-05-01 01:34:58 +01:00
// YAP_LeaveGoal(false, &q);
2018-05-27 00:47:03 +01:00
LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
if (LOCAL_CommittedError) {
LOCAL_CommittedError->errorNo = YAP_NO_ERROR;
2018-07-10 23:21:19 +01:00
free(LOCAL_CommittedError);
2018-05-27 00:47:03 +01:00
LOCAL_CommittedError = NULL;
}
2019-05-01 01:34:58 +01:00
pop_text_stack(0);
LOCAL_CurSlot = 0;
2017-12-20 00:29:15 +00:00
}
2017-02-20 14:38:00 +00:00
2017-12-20 00:29:15 +00:00
Term YAPEngine::top_level(std::string s) {
/// parse string s and make term with var names
/// available.
Term tp;
ARG1 = YAP_ReadBuffer(s.data(), &tp);
ARG2 = tp;
ARG3 = MkVarTerm();
2018-01-05 16:57:38 +00:00
if (ARG1 == 0)
2018-05-21 14:45:24 +01:00
throw YAPError(SOURCE(), SYNTAX_ERROR, ARG1, "in input query");
2017-12-20 00:29:15 +00:00
YAPPredicate p = YAPPredicate(YAP_TopGoal());
YAPQuery *Q = new YAPQuery(p, 0);
Term ts[2];
ts[0] = MkAddressTerm(Q);
if (Q->next()) {
ts[1] = ARG3;
} else {
ts[1] = TermNil;
}
return YAP_MkApplTerm(YAP_MkFunctor(YAP_LookupAtom("t"), 2), 2, ts);
}
2017-05-19 09:56:37 +01:00
2017-12-20 00:29:15 +00:00
Term YAPEngine::next_answer(YAPQuery *&Q) {
2017-05-19 09:56:37 +01:00
2017-12-20 00:29:15 +00:00
/// parse string s and make term with var names
/// available.
Term ts[2];
ts[0] = MkAddressTerm(Q);
if (Q->next()) {
ts[1] = ARG3;
} else {
ts[1] = TermNil;
}
return YAP_MkApplTerm(YAP_MkFunctor(YAP_LookupAtom("t"), 2), 2, ts);
}