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 */
/// @}

View File

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

View File

@ -6,13 +6,12 @@
*/
#define YAP_CPP_INTERFACE 1
#include <gmpxx.h>
#include <vector>
#include <string>
#include <iostream>
#include <string>
#include <vector>
/*!
*
@ -77,15 +76,20 @@ extern "C" {
// taken from yap_structs.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,
YAP_Term);
X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred,
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
#include <Python.h>

View File

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

View File

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