This commit is contained in:
Vitor Santos Costa 2017-09-06 01:16:36 +01:00
parent b5bfb638a3
commit 02a2bb0b0b
5 changed files with 743 additions and 762 deletions

View File

@ -121,4 +121,3 @@ public:
#endif /* YAPA_HH */ #endif /* YAPA_HH */
/// @} /// @}

View File

@ -27,8 +27,6 @@ X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred,
} }
static YAPEngine *curren;
YAPAtomTerm::YAPAtomTerm(char *s) YAPAtomTerm::YAPAtomTerm(char *s)
{ // build string { // build string
BACKUP_H(); BACKUP_H();
@ -189,7 +187,6 @@ YAPApplTerm::YAPApplTerm(std::string f, std::vector<YAPTerm> ts)
mk(o); mk(o);
} }
YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm()
{ {
BACKUP_H(); BACKUP_H();
@ -652,19 +649,19 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
/* ignore flags for now */ /* ignore flags for now */
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
Term *nts; Term *nts;
Term tgoal; Term goal;
if ( ts) { if ( ts) {
goal = new YAPApplTerm(f, ts); goal = YAPApplTerm(f, ts).term();
nts = RepAppl(goal->term())+1; nts = RepAppl(goal)+1;
} else { } else {
goal = new YAPVarTerm(); goal = MkVarTerm();
nts = nullptr; nts = nullptr;
} }
openQuery(goal->term(), nts); openQuery( nts);
names = new YAPPairTerm(); names = YAPPairTerm( TermNil );
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -674,14 +671,13 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
CELL *nts; CELL *nts;
if (ts) { if (ts) {
tgoal = YAPApplTerm(f, nts); goal = YAPApplTerm(f, nts);
} else { } else {
tgoal = YAPVarTerm(); goal = YAPVarTerm();
nts = nullptr; nts = nullptr;
} }
*names = new YAPPairTerm(); names = YAPPairTerm( TermNil );
*goal = new YAPTerm(tgoal); openQuery(term(), nts);
openQuery(goal.term(), nts);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
#endif #endif
@ -696,14 +692,14 @@ YAPQuery::YAPQuery(YAPTerm t) : YAPPredicate(t)
Functor f = FunctorOfTerm(tt); Functor f = FunctorOfTerm(tt);
if (IsExtensionFunctor(f)) if (IsExtensionFunctor(f))
nts = nullptr; nts = nullptr;
nts = RepAppl(tt)+1; nts = RepAppl(goal.term())+1;
} else if (IsPairTerm(tt)) { } else if (IsPairTerm(tt)) {
nts = RepPair(tt); nts = RepPair(tt);
} else { } else {
nts = nullptr; nts = nullptr;
} }
names = new YAPPairTerm(); openQuery( nts);
goal = new YAPTerm(t); names = YAPPairTerm( TermNil );
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -712,14 +708,15 @@ YAPQuery::YAPQuery(YAPPredicate p, YAPTerm ts[]) : YAPPredicate(p.ap) {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
arity_t arity = p.ap->ArityOfPE; arity_t arity = p.ap->ArityOfPE;
if (arity) { if (arity) {
goal = new YAPApplTerm(YAPFunctor(p.ap->FunctorOfPred), ts); goal = YAPApplTerm(YAPFunctor(p.ap->FunctorOfPred), ts).term();
for (arity_t i =0; i < arity; i++) { for (arity_t i =0; i < arity; i++)
XREGS[i+1]=ts[i].term(); XREGS[i+1]=ts[i].term();
} openQuery( nullptr);
} else { } else {
goal = new YAPAtomTerm((Atom)(p.ap->FunctorOfPred)); } goal = YAPAtomTerm((Atom)(p.ap->FunctorOfPred));
openQuery(goal->term(), nullptr); openQuery(nullptr);
names = new YAPPairTerm(); }
names = TermNil;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -729,21 +726,13 @@ bool YAPQuery::next()
bool result = false; bool result = false;
sigjmp_buf buf, *oldp = LOCAL_RestartEnv; sigjmp_buf buf, *oldp = LOCAL_RestartEnv;
Term terr; Term terr;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "next %d %ld",
q_state, LOCAL_CurSlot);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "next %d %s %ld",
q_state, names->text(), LOCAL_CurSlot);
if (ap == NULL || ap->OpcodeOfPred == UNDEF_OPCODE) {
ap = rewriteUndefQuery();
}
LOCAL_RestartEnv = &q_env;
try try
{ {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (!q_open) if (!q_open)
return false; return false;
if (false && sigsetjmp(q_env, false)) LOCAL_RestartEnv = &buf;
if (sigsetjmp(*LOCAL_RestartEnv, false))
{ {
//throw YAPError(); //throw YAPError();
return false; return false;
@ -762,8 +751,8 @@ bool YAPQuery::next()
} }
if (result) if (result)
{ {
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "names %d %s %ld", __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "vnames %d %s %ld",
q_state, names->text(), LOCAL_CurSlot); q_state, vnames.text(), LOCAL_CurSlot);
} }
else else
{ {
@ -774,6 +763,7 @@ bool YAPQuery::next()
{ {
if ((terr = Yap_GetException())) if ((terr = Yap_GetException()))
{ {
LOCAL_RestartEnv = &buf;
throw YAPError(); throw YAPError();
} }
} }
@ -802,6 +792,7 @@ bool YAPQuery::next()
Yap_CloseHandles(q_handles); Yap_CloseHandles(q_handles);
q_open = false; q_open = false;
std::cerr << "Exception received by " << __func__ << "( " << YAPTerm(terr).text() << ").\n Forwarded...\n\n"; std::cerr << "Exception received by " << __func__ << "( " << YAPTerm(terr).text() << ").\n Forwarded...\n\n";
LOCAL_RestartEnv = oldp;
throw e; throw e;
} }
} }
@ -810,10 +801,10 @@ PredEntry *
YAPQuery::rewriteUndefQuery() YAPQuery::rewriteUndefQuery()
{ {
Term ts[3]; Term ts[3];
ARG1 = ts[0] = goal->term(); ARG1 = ts[0] = goal.term();
ARG2 = ts[1] = ap->ModuleOfPred; ARG2 = ts[1] = ap->ModuleOfPred;
ARG3 = ts[2] = Yap_cp_as_integer(B PASS_REGS); ARG3 = ts[2] = Yap_cp_as_integer(B PASS_REGS);
goal = new YAPApplTerm(FunctorUndefinedQuery, ts); goal = YAPApplTerm(FunctorUndefinedQuery, ts);
return ap = PredUndefinedQuery; return ap = PredUndefinedQuery;
} }
@ -895,6 +886,39 @@ JNIEXPORT jint JNICALL JNI_MySQLOnLoad(JavaVM *vm, void *reserved)
return JNI_VERSION_1_6; return JNI_VERSION_1_6;
} }
char *Yap_AndroidBufp;
static size_t Yap_AndroidMax, Yap_AndroidSz;
extern void (*Yap_DisplayWithJava)(int c);
void Yap_displayWithJava(int c)
{
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);
}
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
if (c == '\n')
{
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
curren->run(Yap_AndroidBufp);
Yap_AndroidSz = 0;
}
}
#endif #endif
@ -908,7 +932,11 @@ void YAPEngine::doInit(YAP_file_type_t BootMode)
/* Begin preprocessor code */ /* Begin preprocessor code */
/* live */ /* live */
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "initialize_prolog"); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "initialize_prolog");
curren = this; #if __ANDROID__
Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096);
Yap_AndroidBufp[0] = '\0';
Yap_AndroidSz = 0;
#endif
//yerror = YAPError(); //yerror = YAPError();
#if YAP_PYTHON #if YAP_PYTHON
do_init_python(); do_init_python();

View File

@ -6,13 +6,12 @@
*/ */
#define YAP_CPP_INTERFACE 1 #define YAP_CPP_INTERFACE 1
#include <gmpxx.h> #include <gmpxx.h>
#include <vector>
#include <string>
#include <iostream> #include <iostream>
#include <string>
#include <vector>
/*! /*!
* *
@ -77,15 +76,20 @@ extern "C" {
// taken from yap_structs.h // taken from yap_structs.h
#include "iopreds.h" #include "iopreds.h"
X_API extern void YAP_UserCPredicate(const char *, YAP_UserCPred, YAP_Arity arity); X_API extern void YAP_UserCPredicate(const char *, YAP_UserCPred,
YAP_Arity arity);
/* extern void UserCPredicateWithArgs(const char *name, int *fn(), unsigned int arity) /* extern void UserCPredicateWithArgs(const char *name, int *fn(), unsigned int
* arity)
*/ */
X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, YAP_Arity, X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred,
YAP_Term); YAP_Arity, YAP_Term);
X_API extern void UserBackCPredicate(const char *name, int *init(), int *cont(),
int arity, int extra);
X_API extern YAP_Term YAP_ReadBuffer(const char *s, YAP_Term *tp);
X_API extern void UserBackCPredicate(const char *name, int *init(), int *cont(), int
arity, int extra);
#if YAP_PYTHON #if YAP_PYTHON
#include <Python.h> #include <Python.h>

View File

@ -9,8 +9,8 @@
* *
* @{ * @{
* *
* These classes wrap engine and query. An engine is an environment where we * These classes wrap engine and query. An engine is an environment where we can rum
* can rum Prolog, that is, where we can run queries. * Prolog, that is, where we can run queries.
* *
* Also, supports callbacks and engine configuration. * Also, supports callbacks and engine configuration.
* *
@ -31,7 +31,8 @@ class X_API YAPPredicate;
* interface to a YAP Query; * interface to a YAP Query;
* uses an SWI-like status info internally. * uses an SWI-like status info internally.
*/ */
class X_API YAPQuery : public YAPPredicate { class X_API YAPQuery : public YAPPredicate
{
bool q_open; bool q_open;
int q_state; int q_state;
yhandle_t q_g, q_handles; yhandle_t q_g, q_handles;
@ -40,8 +41,8 @@ class X_API YAPQuery : public YAPPredicate {
int q_flags; int q_flags;
YAP_dogoalinfo q_h; YAP_dogoalinfo q_h;
YAPQuery *oq; YAPQuery *oq;
YAPPairTerm *names; YAPPairTerm names;
YAPTerm *goal; YAPTerm goal;
// temporaries // temporaries
Term tnames, tgoal ; Term tnames, tgoal ;
@ -54,11 +55,10 @@ class X_API YAPQuery : public YAPPredicate {
q_p = P; q_p = P;
q_cp = CP; q_cp = CP;
// make sure this is safe // make sure this is safe
names = new YAPPairTerm();
goal = new YAPTerm();
q_handles = LOCAL_CurSlot; q_handles = LOCAL_CurSlot;
}; };
void openQuery( Term *ts); void openQuery( Term *ts);
PredEntry *rewriteUndefQuery(); PredEntry *rewriteUndefQuery();
@ -91,23 +91,26 @@ public:
/// It is given a string, calls the parser and obtains a Prolog term that /// It is given a string, calls the parser and obtains a Prolog term that
/// should be a callable /// should be a callable
/// goal. /// goal.
inline YAPQuery(const char *s) : YAPPredicate(s, tgoal, tnames) { inline YAPQuery(const char *s) : YAPPredicate(s, tgoal, tnames)
{
CELL *qt = nullptr; CELL *qt = nullptr;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %ld", __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %ld",
LOCAL_CurSlot); LOCAL_CurSlot);
if (!ap) if (!ap)
return; return;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "ŸAPQuery"); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", vnames.text());
goal = YAPTerm(tgoal);
if (IsPairTerm(tgoal)) { if (IsPairTerm(tgoal)) {
qt = RepPair(tgoal); qt = RepPair(tgoal);
tgoal = tgoal = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("consult"), 1),1,qt);
Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("consult"), 1), 1, qt);
} else if (IsApplTerm(tgoal)) { } else if (IsApplTerm(tgoal)) {
Functor f = FunctorOfTerm(tgoal); Functor f = FunctorOfTerm(tgoal);
if (!IsExtensionFunctor(f)) { if (!IsExtensionFunctor(f)) {
qt = RepAppl(tgoal)+1; qt = RepAppl(tgoal)+1;
} }
} }
names = YAPPairTerm(tnames);
openQuery(qt);
}; };
// inline YAPQuery() : YAPPredicate(s, tgoal, tnames) // inline YAPQuery() : YAPPredicate(s, tgoal, tnames)
// { // {
@ -115,7 +118,7 @@ public:
// LOCAL_CurSlot); // LOCAL_CurSlot);
// if (!ap) // if (!ap)
// return; // return;
// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", names->text()); // __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", vnames.text());
// goal = YAPTerm(tgoal); // goal = YAPTerm(tgoal);
// names = YAPPairTerm(tnames); // names = YAPPairTerm(tnames);
// openQuery(tgoal); // openQuery(tgoal);
@ -141,20 +144,23 @@ public:
bool deterministic(); bool deterministic();
/// represent the top-goal /// represent the top-goal
const char *text(); const char *text();
/// remove alternatives in the current search space, and finish tnamedyaphe /// remove alternatives in the current search space, and finish the current
/// current query finish the current query: undo all bindings. /// query
/// finish the current query: undo all bindings.
void close(); void close();
/// query variables. /// query variables.
void cut(); void cut();
Term namedVars() {return names.term(); };
Term namedVars() { return names->term(); };
YAPPairTerm *namedYAPVars() { return names; };
/// query variables, but copied out /// query variables, but copied out
std::vector<Term> namedVarsVector() {
return names.listToArray(); };
/// convert a ref to a binding.
YAPTerm getTerm(yhandle_t t); YAPTerm getTerm(yhandle_t t);
/// simple YAP Query; /// simple YAP Query;
/// just calls YAP and reports success or failure, Useful when we just /// just calls YAP and reports success or failure, Useful when we just
/// want things done, eg YAPCommand("load_files(library(lists), )") /// want things done, eg YAPCommand("load_files(library(lists), )")
inline bool command() { inline bool command()
{
bool rc = next(); bool rc = next();
close(); close();
return rc; return rc;
@ -166,96 +172,170 @@ public:
/// This class implements a callback Prolog-side. It will be inherited by the /// This class implements a callback Prolog-side. It will be inherited by the
/// Java or Python /// Java or Python
/// class that actually implements the callback. /// class that actually implements the callback.
class X_API YAPCallback { class X_API YAPCallback
{
public: public:
virtual ~YAPCallback() {} virtual ~YAPCallback() {}
virtual void run() { LOG("callback"); } virtual void run() { LOG("callback"); }
virtual void run(char *s) {} virtual void run(char *s) {}
}; };
/// @brief Setup all arguments to a new engine /// @brief Setup all arguments to a new engine
class X_API YAPEngineArgs { class X_API YAPEngineArgs {
public: public:
YAP_init_args init_args; YAP_init_args init_args;
inline void setEmbedded(bool fl) { init_args.Embedded = fl; }; inline void setEmbedded( bool fl )
{
init_args.Embedded = fl;
};
inline bool getEmbedded() { return init_args.Embedded; }; inline bool getEmbedded( )
{
return init_args.Embedded;
};
inline void setStackSize(bool fl) { init_args.StackSize = fl; }; inline void setStackSize( bool fl )
{
init_args.StackSize = fl;
};
inline bool getStackSize() { return init_args.StackSize; }; inline bool getStackSize( )
{
return init_args.StackSize;
};
inline void setTrailSize(bool fl) { init_args.TrailSize = fl; }; inline void setTrailSize( bool fl )
{
init_args.TrailSize = fl;
};
inline bool getTrailSize() { return init_args.TrailSize; }; inline bool getTrailSize( )
{
return init_args.TrailSize;
};
inline bool getMStackSize() { return init_args.StackSize; }; inline bool getMStackSize( )
{
return init_args.StackSize;
};
inline void setMaxTrailSize(bool fl) { init_args.MaxTrailSize = fl; }; inline void setMaxTrailSize( bool fl )
{
init_args.MaxTrailSize = fl;
};
inline bool getMaxTrailSize() { return init_args.MaxTrailSize; }; inline bool getMaxTrailSize( )
{
return init_args.MaxTrailSize;
};
inline void setYapLibDir(const char *fl) { inline void setYapLibDir( const char * fl )
{
init_args.YapLibDir = (const char *)malloc(strlen(fl)+1); init_args.YapLibDir = (const char *)malloc(strlen(fl)+1);
strcpy((char *)init_args.YapLibDir, fl); strcpy((char *)init_args.YapLibDir, fl);
}; };
inline const char *getYapLibDir() { return init_args.YapLibDir; }; inline const char * getYapLibDir( )
{
return init_args.YapLibDir;
};
inline void setYapShareDir(const char *fl) { inline void setYapShareDir( const char * fl )
{
init_args.YapShareDir = (const char *)malloc(strlen(fl)+1); init_args.YapShareDir = (const char *)malloc(strlen(fl)+1);
strcpy((char *)init_args.YapShareDir, fl); strcpy((char *)init_args.YapShareDir, fl);
}; };
inline const char *getYapShareDir() { return init_args.YapShareDir; }; inline const char * getYapShareDir( )
{
return init_args.YapShareDir;
};
inline void setSavedState(const char *fl) { inline void setSavedState( const char * fl )
{
init_args.SavedState = (const char *)malloc(strlen(fl)+1); init_args.SavedState = (const char *)malloc(strlen(fl)+1);
strcpy((char *)init_args.SavedState, fl); strcpy((char *)init_args.SavedState, fl);
}; };
inline const char *getSavedState() { return init_args.SavedState; }; inline const char * getSavedState( )
{
return init_args.SavedState;
};
inline void setYapPrologBootFile(const char *fl) { inline void setYapPrologBootFile( const char * fl )
{
init_args.YapPrologBootFile = (const char *)malloc(strlen(fl)+1); init_args.YapPrologBootFile = (const char *)malloc(strlen(fl)+1);
strcpy((char *)init_args.YapPrologBootFile, fl); strcpy((char *)init_args.YapPrologBootFile, fl);
}; };
inline const char *getYapPrologBootFile() { inline const char * getYapPrologBootFile( )
{
return init_args.YapPrologBootFile; return init_args.YapPrologBootFile;
}; };
inline void setYapPrologGoal(const char *fl) { inline void setYapPrologGoal( const char * fl )
{
init_args.YapPrologGoal = fl; init_args.YapPrologGoal = fl;
}; };
inline const char *getYapPrologGoal() { return init_args.YapPrologGoal; }; inline const char * getYapPrologGoal( )
{
return init_args.YapPrologGoal;
};
inline void setYapPrologTopLevelGoal(const char *fl) { inline void setYapPrologTopLevelGoal( const char * fl )
{
init_args.YapPrologTopLevelGoal = fl; init_args.YapPrologTopLevelGoal = fl;
}; };
inline const char *getYapPrologTopLevelGoal() { inline const char * getYapPrologTopLevelGoal( )
{
return init_args.YapPrologTopLevelGoal; return init_args.YapPrologTopLevelGoal;
}; };
inline void setHaltAfterConsult(bool fl) { init_args.HaltAfterConsult = fl; }; inline void setHaltAfterConsult( bool fl )
{
init_args.HaltAfterConsult = fl;
};
inline bool getHaltAfterConsult() { return init_args.HaltAfterConsult; }; inline bool getHaltAfterConsult( )
{
return init_args.HaltAfterConsult;
};
inline void setFastBoot(bool fl) { init_args.FastBoot = fl; }; inline void setFastBoot( bool fl )
{
init_args.FastBoot = fl;
};
inline bool getFastBoot() { return init_args.FastBoot; }; inline bool getFastBoot( )
{
return init_args.FastBoot;
};
inline void setArgc(int fl) { init_args.Argc = fl; }; inline void setArgc( int fl )
{
init_args.Argc = fl;
};
inline int getArgc() { return init_args.Argc; }; inline int getArgc( )
{
return init_args.Argc;
};
inline void setArgv(char **fl) { init_args.Argv = fl; }; inline void setArgv( char ** fl )
{
init_args.Argv = fl;
};
inline char **getArgv() { return init_args.Argv; }; inline char ** getArgv( )
{
return init_args.Argv;
};
YAPEngineArgs() { YAPEngineArgs() {
Yap_InitDefaults(&init_args, NULL, 0, NULL); Yap_InitDefaults(&init_args, NULL, 0, NULL);
@ -266,13 +346,15 @@ public:
}; };
}; };
/** /**
* @brief YAP Engine: takes care of the execution environment * @brief YAP Engine: takes care of the execution environment
where we can go executing goals. where we can go executing goals.
* *
* *
*/ */
class YAPEngine { class YAPEngine
{
private: private:
YAPEngineArgs *engine_args; YAPEngineArgs *engine_args;
YAPCallback *_callback; YAPCallback *_callback;
@ -283,7 +365,8 @@ private:
public : public :
/// construct a new engine; may use a variable number of arguments /// construct a new engine; may use a variable number of arguments
YAPEngine(YAPEngineArgs *cargs) { YAPEngine(YAPEngineArgs *cargs)
{
engine_args = cargs; engine_args = cargs;
//doInit(cargs->init_args.boot_file_type); //doInit(cargs->init_args.boot_file_type);
doInit(YAP_QLY); doInit(YAP_QLY);
@ -296,14 +379,16 @@ public:
/// remove current callback /// remove current callback
void delYAPCallback() { _callback = 0; }; void delYAPCallback() { _callback = 0; };
/// set a new callback /// set a new callback
void setYAPCallback(YAPCallback *cb) { void setYAPCallback(YAPCallback *cb)
{
delYAPCallback(); delYAPCallback();
_callback = cb; _callback = cb;
}; };
/// execute the callback. /// execute the callback.
////void run() { if (_callback) _callback.run(); } ////void run() { if (_callback) _callback.run(); }
/// execute the callback with a text argument. /// execute the callback with a text argument.
void run(char *s) { void run(char *s)
{
if (_callback) if (_callback)
_callback->run(s); _callback->run(s);
} }
@ -330,20 +415,25 @@ public:
bool mgoal(Term t, Term tmod); bool mgoal(Term t, Term tmod);
/// current directory for the engine /// current directory for the engine
bool goal(Term t) { return mgoal(t, CurrentModule); } bool goal(Term t)
{
return mgoal(t, CurrentModule);
}
/// reset Prolog state /// reset Prolog state
void reSet(); void reSet();
/// assune that there are no stack pointers, just release memory /// assune that there are no stack pointers, just release memory
// for last execution // for last execution
void release(); void release();
const char *currentDir() { const char *currentDir()
{
char dir[1024]; char dir[1024];
std::string s = Yap_getcwd(dir, 1024 - 1); std::string s = Yap_getcwd(dir, 1024 - 1);
return s.c_str(); return s.c_str();
}; };
/// report YAP version as a string /// report YAP version as a string
const char *version() { const char *version()
{
std::string s = Yap_version(); std::string s = Yap_version();
return s.c_str(); return s.c_str();
}; };
@ -355,13 +445,14 @@ public:
Term fun(YAPTerm t) { return fun(t.term()); }; Term fun(YAPTerm t) { return fun(t.term()); };
//> set a StringFlag, usually a path //> set a StringFlag, usually a path
//> //>
bool setStringFlag(std::string arg, std::string path) { bool setStringFlag(std::string arg, std::string path)
return setYapFlag(MkAtomTerm(Yap_LookupAtom(arg.data())), {
MkAtomTerm(Yap_LookupAtom(path.data()))); return setYapFlag(MkAtomTerm(Yap_LookupAtom(arg.data())), MkAtomTerm(Yap_LookupAtom(path.data())));
}; };
Term top_level( std::string s); Term top_level( std::string s);
Term next_answer(YAPQuery * &Q); Term next_answer(YAPQuery * &Q);
}; };
#endif /* YAPQ_HH */ #endif /* YAPQ_HH */

View File

@ -20,17 +20,11 @@
* *
*/ */
#include <vector> #include <vector>
#ifndef YAPT_HH #ifndef YAPT_HH
#define YAPT_HH 1 #define YAPT_HH 1
extern "C" {
X_API Term YAP_ReadBuffer(const char *s, Term *tp);
}
class YAPError; class YAPError;
/** /**
@ -38,17 +32,11 @@ class YAPError;
*/ */
class X_API YAPTerm { class X_API YAPTerm {
friend class YAPPredicate; friend class YAPPredicate;
friend class YAPPrologPredicate; friend class YAPPrologPredicate;
friend class YAPQuery; friend class YAPQuery;
friend class YAPModule; friend class YAPModule;
friend class YAPModuleProp; friend class YAPModuleProp;
friend class YAPApplTerm; friend class YAPApplTerm;
friend class YAPListTerm; friend class YAPListTerm;
protected: protected:
@ -79,16 +67,13 @@ public:
// t = Yap_InitSlot(tinp); // t = Yap_InitSlot(tinp);
//} //}
#endif #endif
/// private method to convert from Term (internal YAP representation) to /// private method to convert from Term (internal YAP representation) to
/// YAPTerm /// YAPTerm
// do nothing constructor // do nothing constructor
YAPTerm() { t = 0; }; YAPTerm() { t = 0; };
// YAPTerm(yhandle_t i) { t = i; }; // YAPTerm(yhandle_t i) { t = i; };
/// pointer to term /// pointer to term
YAPTerm(void *ptr); YAPTerm(void *ptr);
/// parse string s and construct a term. /// parse string s and construct a term.
YAPTerm(char *s) { YAPTerm(char *s) {
Term tp; Term tp;
@ -96,7 +81,6 @@ public:
} }
#if 1 #if 1
/// Term destructor, tries to recover slot /// Term destructor, tries to recover slot
virtual ~YAPTerm(){ virtual ~YAPTerm(){
// fprintf(stderr,"-%d,%lx,%p ",t,LOCAL_HandleBase[t] ,HR); // fprintf(stderr,"-%d,%lx,%p ",t,LOCAL_HandleBase[t] ,HR);
@ -121,24 +105,18 @@ public:
/// YAPTerm(YAPFunctor f, YAPTerm ts[]); /// YAPTerm(YAPFunctor f, YAPTerm ts[]);
/// extract the tag of a term, after dereferencing. /// extract the tag of a term, after dereferencing.
YAP_tag_t tag(); YAP_tag_t tag();
/// copy the term ( term copy ) /// copy the term ( term copy )
Term deepCopy(); Term deepCopy();
/// numbervars ( int start, bool process=false ) /// numbervars ( int start, bool process=false )
intptr_t numberVars(intptr_t start, bool skip_singletons = false); intptr_t numberVars(intptr_t start, bool skip_singletons = false);
inline Term term() { inline Term term() {
return gt(); return gt();
} /// from YAPTerm to Term (internal YAP representation) } /// from YAPTerm to Term (internal YAP representation)
inline void bind(Term b) { LOCAL_HandleBase[t] = b; } inline void bind(Term b) { LOCAL_HandleBase[t] = b; }
inline void bind(YAPTerm *b) { LOCAL_HandleBase[t] = b->term(); } inline void bind(YAPTerm *b) { LOCAL_HandleBase[t] = b->term(); }
/// from YAPTerm to Term (internal YAP representation) /// from YAPTerm to Term (internal YAP representation)
/// fetch a sub-term /// fetch a sub-term
Term &operator[](arity_t n); Term &operator[](arity_t n);
// const YAPTerm *vars(); // const YAPTerm *vars();
/// this term is == to t1 /// this term is == to t1
virtual bool exactlyEqual(YAPTerm t1) { virtual bool exactlyEqual(YAPTerm t1) {
@ -185,7 +163,6 @@ public:
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
}; };
/// term hash, /// term hash,
virtual bool isVar() { return IsVarTerm(gt()); } /// type check for unound virtual bool isVar() { return IsVarTerm(gt()); } /// type check for unound
virtual bool isAtom() { return IsAtomTerm(gt()); } /// type check for atom virtual bool isAtom() { return IsAtomTerm(gt()); } /// type check for atom
@ -206,52 +183,6 @@ public:
virtual bool isGround() { return Yap_IsGroundTerm(gt()); } /// term is ground virtual bool isGround() { return Yap_IsGroundTerm(gt()); } /// term is ground
virtual bool isList() { return Yap_IsListTerm(gt()); } /// term is a list virtual bool isList() { return Yap_IsListTerm(gt()); } /// term is a list
std::vector<YAPTerm> YAPTermToJavaArray()
{
BACKUP_H();
Term pt = gt();
if (IsApplTerm(t)) {
arity_t arity = ArityOfFunctor(FunctorOfTerm(pt));
std::vector<YAPTerm> o = std::vector<YAPTerm>(arity);
for (arity_t i=0; i < arity; i++) {
o[i] = YAPTerm( ArgOfTerm(i+1, pt) );
}
return o;
}
if (IsPairTerm(t)) {
std::vector<YAPTerm> o = std::vector<YAPTerm>(2);
o[0] = YAPTerm( HeadOfTerm(pt) );
o[1] = YAPTerm( TailOfTerm(pt) );
return o;
}
return std::vector<YAPTerm>(0);
}
std::vector<Term> YAPTermToVector()
{
BACKUP_H();
Term pt = gt();
if (IsApplTerm(t)) {
arity_t arity = ArityOfFunctor(FunctorOfTerm(pt));
std::vector<Term> o = std::vector<Term>(arity);
for (arity_t i=0; i < arity; i++) {
o[i] = ( ArgOfTerm(i+1, pt) );
}
return o;
}
if (IsPairTerm(t)) {
std::vector<Term> o = std::vector<Term>(2);
o[0] = ( HeadOfTerm(pt) );
o[1] = ( TailOfTerm(pt) );
return o;
}
return std::vector<Term>(0);
}
/// extract the argument i of the term, where i in 1...arity /// extract the argument i of the term, where i in 1...arity
virtual Term getArg(arity_t i) { virtual Term getArg(arity_t i) {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
@ -275,9 +206,7 @@ public:
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return tf; return tf;
} }
virtual YAPTerm getYAPArg(int i) {
return YAPTerm(getArg((arity_t)i));
}
/// extract the arity of the term /// extract the arity of the term
/// variables have arity 0 /// variables have arity 0
virtual inline arity_t arity() { virtual inline arity_t arity() {
@ -326,15 +255,10 @@ public:
*/ */
class X_API YAPFunctor : public YAPProp { class X_API YAPFunctor : public YAPProp {
friend class YAPApplTerm; friend class YAPApplTerm;
friend class YAPTerm; friend class YAPTerm;
friend class YAPPredicate; friend class YAPPredicate;
friend class YAPQuery; friend class YAPQuery;
Functor f; Functor f;
/// Constructor: receives Prolog functor and casts it to YAPFunctor /// Constructor: receives Prolog functor and casts it to YAPFunctor
/// ///
/// Notice that this is designed for internal use only. /// Notice that this is designed for internal use only.
@ -354,7 +278,6 @@ public:
inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) { inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) {
f = Yap_MkFunctor(Yap_LookupAtom(s), arity); f = Yap_MkFunctor(Yap_LookupAtom(s), arity);
} }
/// Constructor: receives name as a wide string plus arity /// Constructor: receives name as a wide string plus arity
/// ///
/// Notice that this is designed for UNICODE right now /// Notice that this is designed for UNICODE right now
@ -364,7 +287,6 @@ public:
inline YAPFunctor(const wchar_t *s, uintptr_t arity) { inline YAPFunctor(const wchar_t *s, uintptr_t arity) {
CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity); CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity);
} }
/// Getter: extract name of functor as an atom /// Getter: extract name of functor as an atom
/// ///
/// this is for external usage. /// this is for external usage.
@ -384,22 +306,16 @@ class X_API YAPApplTerm : public YAPTerm {
public: public:
YAPApplTerm(Term t0) { mk(t0); } YAPApplTerm(Term t0) { mk(t0); }
YAPApplTerm(Functor f, Term ts[]) { YAPApplTerm(Functor f, Term ts[]) {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
Term t0 = Yap_MkApplTerm(f, f->ArityOfFE, ts); Term t0 = Yap_MkApplTerm(f, f->ArityOfFE, ts);
mk(t0); mk(t0);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
}; };
YAPApplTerm(YAPFunctor f, YAPTerm ts[]); YAPApplTerm(YAPFunctor f, YAPTerm ts[]);
YAPApplTerm(const std::string s, std::vector<YAPTerm> ts); YAPApplTerm(const std::string s, std::vector<YAPTerm> ts);
YAPApplTerm(YAPFunctor f); YAPApplTerm(YAPFunctor f);
inline Functor functor() { return FunctorOfTerm(gt()); } inline Functor functor() { return FunctorOfTerm(gt()); }
inline YAPFunctor getFunctor() { return YAPFunctor(FunctorOfTerm(gt())); } inline YAPFunctor getFunctor() { return YAPFunctor(FunctorOfTerm(gt())); }
Term getArg(arity_t i) { Term getArg(arity_t i) {
@ -410,7 +326,6 @@ public:
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return tf; return tf;
}; };
virtual bool isVar() { return false; } /// type check for unbound virtual bool isVar() { return false; } /// type check for unbound
virtual bool isAtom() { return false; } /// type check for atom virtual bool isAtom() { return false; } /// type check for atom
virtual bool isInteger() { return false; } /// type check for integer virtual bool isInteger() { return false; } /// type check for integer
@ -429,7 +344,6 @@ public:
class X_API YAPPairTerm : public YAPTerm { class X_API YAPPairTerm : public YAPTerm {
friend class YAPTerm; friend class YAPTerm;
public: public:
YAPPairTerm(Term t0) { YAPPairTerm(Term t0) {
t0 = Deref(t0); t0 = Deref(t0);
@ -438,23 +352,10 @@ public:
else else
Yap_ThrowError(TYPE_ERROR_LIST, t0, "YAPPairTerms"); Yap_ThrowError(TYPE_ERROR_LIST, t0, "YAPPairTerms");
} }
/// cons
YAPPairTerm(YAPTerm hd, YAPTerm tl); YAPPairTerm(YAPTerm hd, YAPTerm tl);
/// null constructor, or empty list
YAPPairTerm(); YAPPairTerm();
bool empty() { return gt() == TermNil; }
Term getHead() { return (HeadOfTerm(gt())); } Term getHead() { return (HeadOfTerm(gt())); }
Term getTail() { return (TailOfTerm(gt())); } Term getTail() { return (TailOfTerm(gt())); }
YAPTerm car() { return YAPTerm(HeadOfTerm(gt())); }
YAPPairTerm cdr() { return YAPPairTerm(TailOfTerm(gt())); }
std::vector<Term> listToArray() { std::vector<Term> listToArray() {
Term *tailp; Term *tailp;
Term t1 = gt(); Term t1 = gt();
@ -471,23 +372,6 @@ public:
} }
return o; return o;
} }
std::vector<YAPTerm> listToYAPArray() {
Term *tailp;
Term t1 = gt();
Int l = Yap_SkipList(&t1, &tailp);
if (l < 0) {
throw YAPError(TYPE_ERROR_LIST, YAPTerm(t), "");
}
std::vector<YAPTerm> o = std::vector<YAPTerm>(l);
int i = 0;
Term t = gt();
while (t != TermNil) {
o[i++] = YAPTerm(HeadOfTerm(t));
t = TailOfTerm(t);
}
return o;
}
}; };
/** /**
@ -497,7 +381,6 @@ public:
class X_API YAPNumberTerm : public YAPTerm { class X_API YAPNumberTerm : public YAPTerm {
public: public:
YAPNumberTerm(){}; YAPNumberTerm(){};
bool isTagged() { return IsIntTerm(gt()); } bool isTagged() { return IsIntTerm(gt()); }
}; };
@ -508,7 +391,6 @@ public:
class X_API YAPIntegerTerm : public YAPNumberTerm { class X_API YAPIntegerTerm : public YAPNumberTerm {
public: public:
YAPIntegerTerm(intptr_t i); YAPIntegerTerm(intptr_t i);
intptr_t getInteger() { return IntegerOfTerm(gt()); }; intptr_t getInteger() { return IntegerOfTerm(gt()); };
}; };
@ -529,18 +411,15 @@ public:
/// ///
/// @param[in] the term /// @param[in] the term
YAPListTerm() { mk(TermNil); /* else type_error */ } YAPListTerm() { mk(TermNil); /* else type_error */ }
/// Create an empty list term. /// Create an empty list term.
/// ///
/// @param[in] the term /// @param[in] the term
YAPListTerm(Term t0) { mk(t0); /* else type_error */ } YAPListTerm(Term t0) { mk(t0); /* else type_error */ }
/// Create a list term out of an array of terms. /// Create a list term out of an array of terms.
/// ///
/// @param[in] the array of terms /// @param[in] the array of terms
/// @param[in] the length of the array /// @param[in] the length of the array
YAPListTerm(YAPTerm ts[], size_t n); YAPListTerm(YAPTerm ts[], size_t n);
// YAPListTerm( vector<YAPTerm> v ); // YAPListTerm( vector<YAPTerm> v );
/// Return the number of elements in a list term. /// Return the number of elements in a list term.
size_t length() { size_t length() {
@ -548,15 +427,12 @@ public:
Term t1 = gt(); Term t1 = gt();
return Yap_SkipList(&t1, &tailp); return Yap_SkipList(&t1, &tailp);
} }
/// Extract the nth element. /// Extract the nth element.
Term &operator[](size_t n); Term &operator[](size_t n);
/// Extract the first element of a list. /// Extract the first element of a list.
/// ///
/// @param[in] the list /// @param[in] the list
Term car(); Term car();
/// Extract the tail elements of a list. /// Extract the tail elements of a list.
/// ///
/// @param[in] the list /// @param[in] the list
@ -569,7 +445,6 @@ public:
/* error */ /* error */
throw YAPError(TYPE_ERROR_LIST, YAPTerm(to), ""); throw YAPError(TYPE_ERROR_LIST, YAPTerm(to), "");
} }
/// copy a list. /// copy a list.
/// ///
/// @param[in] the list /// @param[in] the list
@ -580,7 +455,9 @@ public:
/// @param[in] the list /// @param[in] the list
inline bool nil() { inline bool nil() {
return gt() == TermNil; return gt() == TermNil;
}; }
;
}; };
/** /**
@ -590,16 +467,12 @@ class X_API YAPStringTerm : public YAPTerm {
public: public:
/// your standard constructor /// your standard constructor
YAPStringTerm(char *s); YAPStringTerm(char *s);
/// use this one to construct length limited strings /// use this one to construct length limited strings
YAPStringTerm(char *s, size_t len); YAPStringTerm(char *s, size_t len);
/// construct using wide chars /// construct using wide chars
YAPStringTerm(wchar_t *s); YAPStringTerm(wchar_t *s);
/// construct using length-limited wide chars /// construct using length-limited wide chars
YAPStringTerm(wchar_t *s, size_t len); YAPStringTerm(wchar_t *s, size_t len);
const char *getString() { return StringOfTerm(gt()); } const char *getString() { return StringOfTerm(gt()); }
}; };
@ -609,28 +482,21 @@ public:
*/ */
class X_API YAPAtomTerm : public YAPTerm { class X_API YAPAtomTerm : public YAPTerm {
friend class YAPModule; friend class YAPModule;
// Constructor: receives a C-atom; // Constructor: receives a C-atom;
YAPAtomTerm(Term t) : YAPTerm(t) { IsAtomTerm(t); } YAPAtomTerm(Term t) : YAPTerm(t) { IsAtomTerm(t); }
public: public:
YAPAtomTerm(Atom a) { mk(MkAtomTerm(a)); } YAPAtomTerm(Atom a) { mk(MkAtomTerm(a)); }
// Constructor: receives an atom; // Constructor: receives an atom;
YAPAtomTerm(YAPAtom a) : YAPTerm() { mk(MkAtomTerm(a.a)); } YAPAtomTerm(YAPAtom a) : YAPTerm() { mk(MkAtomTerm(a.a)); }
// Constructor: receives a sequence of ISO-LATIN1 codes; // Constructor: receives a sequence of ISO-LATIN1 codes;
YAPAtomTerm(char s[]); YAPAtomTerm(char s[]);
// Constructor: receives a sequence of up to n ISO-LATIN1 codes; // Constructor: receives a sequence of up to n ISO-LATIN1 codes;
YAPAtomTerm(char *s, size_t len); YAPAtomTerm(char *s, size_t len);
// Constructor: receives a sequence of wchar_ts, whatever they may be; // Constructor: receives a sequence of wchar_ts, whatever they may be;
YAPAtomTerm(wchar_t *s); YAPAtomTerm(wchar_t *s);
// Constructor: receives a sequence of n wchar_ts, whatever they may be; // Constructor: receives a sequence of n wchar_ts, whatever they may be;
YAPAtomTerm(wchar_t *s, size_t len); YAPAtomTerm(wchar_t *s, size_t len);
bool isVar() { return false; } /// type check for unbound bool isVar() { return false; } /// type check for unbound
bool isAtom() { return true; } /// type check for atom bool isAtom() { return true; } /// type check for atom
bool isInteger() { return false; } /// type check for integer bool isInteger() { return false; } /// type check for integer
@ -643,14 +509,11 @@ public:
virtual bool isList() { return gt() == TermNil; } /// [] is a list virtual bool isList() { return gt() == TermNil; } /// [] is a list
// Getter: outputs the atom; // Getter: outputs the atom;
YAPAtom getAtom() { return YAPAtom(AtomOfTerm(gt())); } YAPAtom getAtom() { return YAPAtom(AtomOfTerm(gt())); }
// Getter: outputs the name as a sequence of ISO-LATIN1 codes; // Getter: outputs the name as a sequence of ISO-LATIN1 codes;
const char *text() { return (const char *)AtomOfTerm(gt())->StrOfAE; } const char *text() { return (const char *)AtomOfTerm(gt())->StrOfAE; }
}; };
#endif /* YAPT_HH */ #endif /* YAPT_HH */
/** /**
* @brief Variable Term * @brief Variable Term
*/ */
@ -660,19 +523,15 @@ class X_API YAPVarTerm : public YAPTerm {
public: public:
/// constructor /// constructor
YAPVarTerm() { mk(MkVarTerm()); }; YAPVarTerm() { mk(MkVarTerm()); };
/// get the internal representation /// get the internal representation
CELL *getVar() { return VarOfTerm(gt()); } CELL *getVar() { return VarOfTerm(gt()); }
/// is the variable bound to another one /// is the variable bound to another one
YAPVarTerm(Term t) { YAPVarTerm(Term t) {
if (IsVarTerm(t)) { if (IsVarTerm(t)) {
mk(t); mk(t);
} }
} }
bool unbound() { return IsUnboundVar(VarOfTerm(gt())); } bool unbound() { return IsUnboundVar(VarOfTerm(gt())); }
inline bool isVar() { return true; } /// type check for unbound inline bool isVar() { return true; } /// type check for unbound
inline bool isAtom() { return false; } /// type check for atom inline bool isAtom() { return false; } /// type check for atom
inline bool isInteger() { return false; } /// type check for integer inline bool isInteger() { return false; } /// type check for integer