diff --git a/CXX/yapa.hh b/CXX/yapa.hh index 18f887320..8d0454326 100644 --- a/CXX/yapa.hh +++ b/CXX/yapa.hh @@ -24,7 +24,7 @@ * * In a nutshell: * - YAPAtom serves as the gateway to the data-base; - * + * * - YAProp abstracts most data-base objects. * * - PropTag allows distinguishing the different classes of YAPProp. @@ -41,12 +41,12 @@ enum PropTag { ARITHMETIC_PROPERTY_TAG = ExpProperty, // 0xFFE0, /// map the atom to an integer TRANSLATION_TAG = TranslationProperty, // 0xFFF4, - /// ensure the atom may not be garbafe colected - HOLD_TAG = HoldProperty, // 0xFFF6 + /// ensure the atom may not be garbafe colected + HOLD_TAG = HoldProperty, // 0xFFF6 /// named mutEX MUTEX_TAG = MutexProperty, // 0xFFF6, /// A typed array, may be in-db or in-stack deped - ARRAY_TAG = ArrayProperty, // 0xFFF7, + ARRAY_TAG = ArrayProperty, // 0xFFF7, /// module MODULE_TAG = ModProperty, // 0xFFFA, /// the original SICStus blackboard @@ -57,8 +57,8 @@ enum PropTag { GLOBAL_VAR_TAG = GlobalProperty, // 0xFFFD /// SWI-STYLE ATOM Extension BLOB_TAG = BlobProperty, // 0xFFFE, - /// Prolog operator, - OPERATOR_TAG = OpProperty, // 0xFFFF, + /// Prolog operator, + OPERATOR_TAG = OpProperty, // 0xFFFF, }; /** @@ -94,7 +94,7 @@ public: /// get name of (other way) inline const char *text(void) { return getName(); } ; /// get prop of type - Prop getProp( PropTag tag ) { return Yap_GetAProp( a , (PropFlags)tag ); } + Prop getProp( PropTag tag ) { return Yap_GetAProp( a , (PropFlags)tag ); } }; /** @@ -115,10 +115,9 @@ public: /// get name of property // virtual YAPAtom name(); virtual ~YAPProp() {}; - + }; -#endif /* YAPA_HH */ +#endif /* YAPA_HH */ /// @} - diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index 49385fa2f..ab3aedbb0 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -27,14 +27,12 @@ X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred, } -static YAPEngine *curren; - YAPAtomTerm::YAPAtomTerm(char *s) { // build string BACKUP_H(); CACHE_REGS - seq_tv_t inp, out; + seq_tv_t inp, out; inp.val.c = s; inp.type = YAP_STRING_CHARS; out.type = YAP_STRING_ATOM; @@ -189,7 +187,6 @@ YAPApplTerm::YAPApplTerm(std::string f, std::vector ts) mk(o); } - YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() { BACKUP_H(); @@ -652,20 +649,20 @@ 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(); - RECOVER_MACHINE_REGS(); + openQuery( nts); + names = YAPPairTerm( TermNil ); + RECOVER_MACHINE_REGS(); } #if 0 @@ -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 @@ -695,15 +691,15 @@ YAPQuery::YAPQuery(YAPTerm t) : YAPPredicate(t) if (IsApplTerm(tt)) { Functor f = FunctorOfTerm(tt); if (IsExtensionFunctor(f)) - nts = nullptr; - nts = RepAppl(tt)+1; + nts = nullptr; + 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++) { - XREGS[i + 1] = ts[i].term(); - } + 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 + 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,58 +751,60 @@ 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 - { - __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "fail"); - } + { + __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "fail"); + } q_state = 1; if ((terr = Yap_GetException())) + { + if ((terr = Yap_GetException())) { - if ((terr = Yap_GetException())) - { - throw YAPError(); - } + LOCAL_RestartEnv = &buf; + throw YAPError(); } + } __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "out %d", result); if (!result) - { - YAP_LeaveGoal(false, &q_h); - Yap_CloseHandles(q_handles); - q_open = false; - } + { + YAP_LeaveGoal(false, &q_h); + Yap_CloseHandles(q_handles); + q_open = false; + } else - { - q_handles = Yap_StartSlots(); - } + { + q_handles = Yap_StartSlots(); + } RECOVER_MACHINE_REGS(); LOCAL_RestartEnv = oldp; return result; } catch (YAPError e) - { - q_open = false; - Yap_PopTermFromDB(LOCAL_ActiveError->errorTerm); - memset(LOCAL_ActiveError, 0, sizeof(*LOCAL_ActiveError)); - YAP_LeaveGoal(false, &q_h); - Yap_CloseHandles(q_handles); - q_open = false; - std::cerr << "Exception received by " << __func__ << "( " << YAPTerm(terr).text() << ").\n Forwarded...\n\n"; - throw e; - } + { + q_open = false; + Yap_PopTermFromDB(LOCAL_ActiveError->errorTerm); + memset(LOCAL_ActiveError, 0, sizeof(*LOCAL_ActiveError)); + YAP_LeaveGoal(false, &q_h); + 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; + } } 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,18 +932,22 @@ 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(); + do_init_python(); #endif - YAP_PredEntryPtr p = YAP_AtomToPred( YAP_LookupAtom("initialize_prolog") ); - YAPQuery initq = YAPQuery(YAPPredicate(p), nullptr); - if (initq.next()) - { - initq.cut(); - } - CurrentModule = TermUser; + YAP_PredEntryPtr p = YAP_AtomToPred( YAP_LookupAtom("initialize_prolog") ); + YAPQuery initq = YAPQuery(YAPPredicate(p), nullptr); + if (initq.next()) + { + initq.cut(); + } + CurrentModule = TermUser; } diff --git a/CXX/yapi.hh b/CXX/yapi.hh index 48ee5d04e..d16c219c0 100644 --- a/CXX/yapi.hh +++ b/CXX/yapi.hh @@ -6,13 +6,12 @@ */ - #define YAP_CPP_INTERFACE 1 #include -#include -#include #include +#include +#include /*! * @@ -71,26 +70,31 @@ extern "C" { #endif #if _MSC_VER || defined(__MINGW32__) - //#include +//#include #endif - // taken from yap_structs.h +// 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) - */ - X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, YAP_Arity, - YAP_Term); +/* 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 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 - extern bool python_in_python; +extern bool python_in_python; #endif } diff --git a/CXX/yapq.hh b/CXX/yapq.hh index 54d3ba89c..6b90e6646 100644 --- a/CXX/yapq.hh +++ b/CXX/yapq.hh @@ -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,34 +41,33 @@ 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; + Term tnames, tgoal ; - inline void setNext() { // oq = LOCAL_execution; - // LOCAL_execution = this; - q_open = true; - q_state = 0; - q_flags = true; // PL_Q_PASS_EXCEPTION; + inline void setNext() { // oq = LOCAL_execution; + // LOCAL_execution = this; + q_open = true; + q_state = 0; + q_flags = true; // PL_Q_PASS_EXCEPTION; - q_p = P; - q_cp = CP; - // make sure this is safe - names = new YAPPairTerm(); - goal = new YAPTerm(); - q_handles = LOCAL_CurSlot; - }; + q_p = P; + q_cp = CP; + // make sure this is safe + q_handles = LOCAL_CurSlot; +}; - void openQuery(Term *ts); - PredEntry *rewriteUndefQuery(); + void openQuery( Term *ts); + +PredEntry *rewriteUndefQuery(); public: - YAPQuery() { - goal = TermTrue; - openQuery(nullptr); - }; +YAPQuery() { + goal = TermTrue; + openQuery( nullptr); +}; /// main constructor, uses a predicate and an array of terms /// /// It is given a YAPPredicate _p_ , and an array of terms that must have at @@ -85,29 +85,32 @@ public: /// /// It is given a functor, and an array of terms that must have at least /// the same arity as the functor. Works within the current module. - // YAPQuery(YAPFunctor f, YAPTerm t[]); + //YAPQuery(YAPFunctor f, YAPTerm t[]); /// string constructor without varnames /// /// 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"); - if (IsPairTerm(tgoal)) { - qt = RepPair(tgoal); - 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; + __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %ld", + LOCAL_CurSlot); + if (!ap) + return; + __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); + } 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); @@ -128,37 +131,40 @@ public: /// set flags for query execution, currently only for exception handling void setFlag(int flag) { q_flags |= flag; } /// reset flags for query execution, currently only for exception handling - void resetFlag(int flag) { q_flags &= ~flag; } - /// first query - /// - /// actually implemented by calling the next(); - inline bool first() { return next(); } - /// ask for the next solution of the current query - /// same call for every solution - bool next(); - /// does this query have open choice-points? - /// or is it deterministic? - 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. - void close(); - /// query variables. - void cut(); - - Term namedVars() { return names->term(); }; - YAPPairTerm *namedYAPVars() { return names; }; - /// query variables, but copied out - 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() { - bool rc = next(); - close(); - return rc; - }; +void resetFlag(int flag) { q_flags &= ~flag; } +/// first query +/// +/// actually implemented by calling the next(); +inline bool first() { return next(); } +/// ask for the next solution of the current query +/// same call for every solution +bool next(); +/// does this query have open choice-points? +/// or is it deterministic? +bool deterministic(); +/// represent the top-goal +const char *text(); +/// 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(); }; +/// query variables, but copied out +std::vector 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() +{ + bool rc = next(); + close(); + return rc; +}; }; // Java support @@ -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) { - init_args.YapLibDir = (const char *)malloc(strlen(fl) + 1); + 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) { - init_args.YapShareDir = (const char *)malloc(strlen(fl) + 1); + 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) { - init_args.SavedState = (const char *)malloc(strlen(fl) + 1); + 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 void setYapPrologBootFile(const char *fl) { - init_args.YapPrologBootFile = (const char *)malloc(strlen(fl) + 1); - strcpy((char *)init_args.YapPrologBootFile, fl); + inline const char * getSavedState( ) + { + return init_args.SavedState; }; - inline const char *getYapPrologBootFile() { + 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( ) + { 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; @@ -281,29 +363,32 @@ private: YAP_dogoalinfo q; PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term t, Term tmod); -public: - /// construct a new engine; may use a variable number of arguments - YAPEngine(YAPEngineArgs *cargs) { + public : + /// construct a new engine; may use a variable number of arguments + YAPEngine(YAPEngineArgs *cargs) + { engine_args = cargs; - // doInit(cargs->init_args.boot_file_type); + //doInit(cargs->init_args.boot_file_type); doInit(YAP_QLY); }; /// construct a new engine, including aaccess to callbacks /// construct a new engine using argc/argv list of arguments YAPEngine(int argc, char *argv[], - YAPCallback *callback = (YAPCallback *)NULL); + YAPCallback *callback = (YAPCallback *)NULL); /// kill engine ~YAPEngine() { delYAPCallback(); }; /// 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,15 +445,16 @@ 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); -}; + Term top_level( std::string s); + Term next_answer(YAPQuery * &Q); + + }; #endif /* YAPQ_HH */ -/// @} + /// @} diff --git a/CXX/yapt.hh b/CXX/yapt.hh index 68d4a7182..520270166 100644 --- a/CXX/yapt.hh +++ b/CXX/yapt.hh @@ -20,474 +20,358 @@ * */ - #include #ifndef YAPT_HH #define YAPT_HH 1 -extern "C" { - -X_API Term YAP_ReadBuffer(const char *s, Term *tp); -} - class YAPError; /** * @brief Generic Prolog Term */ 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; + friend class YAPPredicate; + friend class YAPPrologPredicate; + friend class YAPQuery; + friend class YAPModule; + friend class YAPModuleProp; + friend class YAPApplTerm; + friend class YAPListTerm; protected: - yhandle_t t; /// handle to term, equivalent to term_t + yhandle_t t; /// handle to term, equivalent to term_t public: - Term gt() { - CACHE_REGS - // fprintf(stderr,"?%d,%lx,%p\n",t,LOCAL_HandleBase[t], HR); - // Yap_DebugPlWriteln(LOCAL_HandleBase[t]); - return Yap_GetFromSlot(t); - }; + Term gt() { + CACHE_REGS + // fprintf(stderr,"?%d,%lx,%p\n",t,LOCAL_HandleBase[t], HR); + // Yap_DebugPlWriteln(LOCAL_HandleBase[t]); + return Yap_GetFromSlot(t); + }; - void mk(Term t0) { - CACHE_REGS t = Yap_InitSlot(t0); - // fprintf(stderr,"+%d,%lx,%p,%p",t,t0,HR,ASP); Yap_DebugPlWriteln(t0); - }; + void mk(Term t0) { + CACHE_REGS t = Yap_InitSlot(t0); + // fprintf(stderr,"+%d,%lx,%p,%p",t,t0,HR,ASP); Yap_DebugPlWriteln(t0); + }; - void put(Term t0) { - Yap_PutInHandle(t, t0); - // fprintf(stderr,"+%d,%lx,%p,%p",t,t0,HR,ASP); Yap_DebugPlWriteln(t0); - }; + void put(Term t0) { + Yap_PutInHandle(t, t0); + // fprintf(stderr,"+%d,%lx,%p,%p",t,t0,HR,ASP); Yap_DebugPlWriteln(t0); + }; - YAPTerm(Term tn) { mk(tn); }; + YAPTerm(Term tn) { mk(tn); }; #ifdef SWIGPYTHON - // YAPTerm(struct _object *inp) { - // Term tinp = pythonToYAP(inp); - // t = Yap_InitSlot(tinp); - //} +// YAPTerm(struct _object *inp) { +// Term tinp = pythonToYAP(inp); +// 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; - mk(YAP_ReadBuffer(s, &tp)); - } + /// 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; + mk(YAP_ReadBuffer(s, &tp)); + } #if 1 - - /// Term destructor, tries to recover slot - virtual ~YAPTerm() { - // fprintf(stderr,"-%d,%lx,%p ",t,LOCAL_HandleBase[t] ,HR); -/* if (!t) - return; - Yap_DebugPlWriteln(LOCAL_HandleBase[t]); - LOCAL_HandleBase[t] = TermFreeTerm; - while (LOCAL_HandleBase[LOCAL_CurSlot - 1] == TermFreeTerm) { - LOCAL_CurSlot--; - } - */ - }; + /// Term destructor, tries to recover slot + virtual ~YAPTerm(){ + // fprintf(stderr,"-%d,%lx,%p ",t,LOCAL_HandleBase[t] ,HR); + /* if (!t) + return; + Yap_DebugPlWriteln(LOCAL_HandleBase[t]); + LOCAL_HandleBase[t] = TermFreeTerm; + while (LOCAL_HandleBase[LOCAL_CurSlot - 1] == TermFreeTerm) { + LOCAL_CurSlot--; + } + */ + }; #endif - /// construct a term out of an integer (if you know object type use - /// YAPIntegerTerm) - /// YAPTerm(long int num) { mk(MkIntegerTerm(num)); } - /// construct a term out of an integer (if you know object type use - /// YAPIntegerTerm) - /// YAPTerm(double num) { mk(MkFloatTerm(num)); } - /// parse string s and construct a term. - /// YAPTerm(YAPFunctor f, YAPTerm ts[]); - /// extract the tag of a term, after dereferencing. - YAP_tag_t tag(); + /// construct a term out of an integer (if you know object type use + /// YAPIntegerTerm) + /// YAPTerm(long int num) { mk(MkIntegerTerm(num)); } + /// construct a term out of an integer (if you know object type use + /// YAPIntegerTerm) + /// YAPTerm(double num) { mk(MkFloatTerm(num)); } + /// parse string s and construct a term. + /// 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) { + bool out; + BACKUP_MACHINE_REGS(); + out = Yap_eq(gt(), t1.term()); + RECOVER_MACHINE_REGS(); + return out; + }; - /// copy the term ( term copy ) - Term deepCopy(); + /// t = t1 + virtual bool unify(YAPTerm t1) { + intptr_t out; + BACKUP_MACHINE_REGS(); + out = Yap_unify(gt(), t1.term()); + RECOVER_MACHINE_REGS(); + return out; + }; - /// numbervars ( int start, bool process=false ) - intptr_t numberVars(intptr_t start, bool skip_singletons = false); + /// we can unify t and t1 + virtual bool unifiable(YAPTerm t1) { + bool out; + BACKUP_MACHINE_REGS(); + out = Yap_eq(gt(), t1.term()); + RECOVER_MACHINE_REGS(); + return out; + }; - inline Term term() { - return gt(); - } /// from YAPTerm to Term (internal YAP representation) - inline void bind(Term b) { LOCAL_HandleBase[t] = b; } + /// t =@= t1, the two terms are equal up to variable renamingvirtual bool + /// variant( + inline virtual YAP_Term variant(YAPTerm t1) { + intptr_t out; + BACKUP_MACHINE_REGS(); + out = Yap_Variant(gt(), t1.term()); + RECOVER_MACHINE_REGS(); + return out; + }; - inline void bind(YAPTerm *b) { LOCAL_HandleBase[t] = b->term(); } + virtual intptr_t hashTerm(size_t sz, size_t depth, bool variant) { + intptr_t out; - /// from YAPTerm to Term (internal YAP representation) - /// fetch a sub-term - Term &operator[](arity_t n); + BACKUP_MACHINE_REGS(); + out = Yap_TermHash(gt(), sz, depth, variant); + 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 + virtual bool isInteger() { + return IsIntegerTerm(gt()); + } /// type check for integer + virtual bool isFloat() { + return IsFloatTerm(gt()); + } /// type check for floating-point + virtual bool isString() { + return IsStringTerm(gt()); + } /// type check for a string " ... " + virtual bool isCompound() { + return !(IsVarTerm(gt()) || IsNumTerm(gt())); + } /// is a primitive term + virtual bool isAppl() { return IsApplTerm(gt()); } /// is a structured term + virtual bool isPair() { return IsPairTerm(gt()); } /// is a pair term + virtual bool isGround() { return Yap_IsGroundTerm(gt()); } /// term is ground + virtual bool isList() { return Yap_IsListTerm(gt()); } /// term is a list - // const YAPTerm *vars(); - /// this term is == to t1 - virtual bool exactlyEqual(YAPTerm t1) { - bool out; - BACKUP_MACHINE_REGS(); - out = Yap_eq(gt(), t1.term()); - RECOVER_MACHINE_REGS(); - return out; - }; + /// extract the argument i of the term, where i in 1...arity + virtual Term getArg(arity_t i) { + BACKUP_MACHINE_REGS(); + Term tf = 0; + Term t0 = gt(); - /// t = t1 - virtual bool unify(YAPTerm t1) { - intptr_t out; - BACKUP_MACHINE_REGS(); - out = Yap_unify(gt(), t1.term()); - RECOVER_MACHINE_REGS(); - return out; - }; - - /// we can unify t and t1 - virtual bool unifiable(YAPTerm t1) { - bool out; - BACKUP_MACHINE_REGS(); - out = Yap_eq(gt(), t1.term()); - RECOVER_MACHINE_REGS(); - return out; - }; - - /// t =@= t1, the two terms are equal up to variable renamingvirtual bool - /// variant( - inline virtual YAP_Term variant(YAPTerm t1) { - intptr_t out; - BACKUP_MACHINE_REGS(); - out = Yap_Variant(gt(), t1.term()); - RECOVER_MACHINE_REGS(); - return out; - }; - - virtual intptr_t hashTerm(size_t sz, size_t depth, bool variant) { - intptr_t out; - - BACKUP_MACHINE_REGS(); - out = Yap_TermHash(gt(), sz, depth, variant); - 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 - virtual bool isInteger() { - return IsIntegerTerm(gt()); - } /// type check for integer - virtual bool isFloat() { - return IsFloatTerm(gt()); - } /// type check for floating-point - virtual bool isString() { - return IsStringTerm(gt()); - } /// type check for a string " ... " - virtual bool isCompound() { - return !(IsVarTerm(gt()) || IsNumTerm(gt())); - } /// is a primitive term - virtual bool isAppl() { return IsApplTerm(gt()); } /// is a structured term - virtual bool isPair() { return IsPairTerm(gt()); } /// is a pair term - virtual bool isGround() { return Yap_IsGroundTerm(gt()); } /// term is ground - virtual bool isList() { return Yap_IsListTerm(gt()); } /// term is a list - - - std::vector YAPTermToJavaArray() - { - BACKUP_H(); - Term pt = gt(); - if (IsApplTerm(t)) { - arity_t arity = ArityOfFunctor(FunctorOfTerm(pt)); - std::vector o = std::vector(arity); - for (arity_t i=0; i < arity; i++) { - o[i] = YAPTerm( ArgOfTerm(i+1, pt) ); - } - return o; - - } - if (IsPairTerm(t)) { - std::vector o = std::vector(2); - o[0] = YAPTerm( HeadOfTerm(pt) ); - o[1] = YAPTerm( TailOfTerm(pt) ); - return o; - - } - return std::vector(0); + if (IsApplTerm(t0)) { + if (i > ArityOfFunctor(FunctorOfTerm(t0))) + YAPError(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 + YAPError(DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()"); + } else { + YAPError(TYPE_ERROR_COMPOUND, t0, "t0.getArg()"); } + RECOVER_MACHINE_REGS(); + return tf; + } - std::vector YAPTermToVector() - { - BACKUP_H(); - Term pt = gt(); - if (IsApplTerm(t)) { - arity_t arity = ArityOfFunctor(FunctorOfTerm(pt)); - std::vector o = std::vector(arity); - for (arity_t i=0; i < arity; i++) { - o[i] = ( ArgOfTerm(i+1, pt) ); - } - return o; + /// extract the arity of the term + /// variables have arity 0 + virtual inline arity_t arity() { + Term t0 = gt(); - } - if (IsPairTerm(t)) { - std::vector o = std::vector(2); - o[0] = ( HeadOfTerm(pt) ); - o[1] = ( TailOfTerm(pt) ); - return o; - - } - return std::vector(0); - } - /// extract the argument i of the term, where i in 1...arity - virtual Term getArg(arity_t i) { - BACKUP_MACHINE_REGS(); - Term tf = 0; - Term t0 = gt(); - - if (IsApplTerm(t0)) { - if (i > ArityOfFunctor(FunctorOfTerm(t0))) - YAPError(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 - YAPError(DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()"); - } else { - YAPError(TYPE_ERROR_COMPOUND, t0, "t0.getArg()"); - } - 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() { - Term t0 = gt(); - - if (IsApplTerm(t0)) { - Functor f = FunctorOfTerm(t0); - if (IsExtensionFunctor(f)) - return 0; - return ArityOfFunctor(f); - } else if (IsPairTerm(t0)) { - return 2; - } + if (IsApplTerm(t0)) { + Functor f = FunctorOfTerm(t0); + if (IsExtensionFunctor(f)) return 0; + return ArityOfFunctor(f); + } else if (IsPairTerm(t0)) { + return 2; } + return 0; + } - /// return a string with a textual representation of the term - virtual const char *text() { - CACHE_REGS - size_t length = 0; - encoding_t enc = LOCAL_encoding; - char *os; + /// return a string with a textual representation of the term + virtual const char *text() { + CACHE_REGS + size_t length = 0; + encoding_t enc = LOCAL_encoding; + char *os; - BACKUP_MACHINE_REGS(); - if (!(os = Yap_TermToString(Yap_GetFromSlot(t), &length, enc, - Handle_vars_f))) { - RECOVER_MACHINE_REGS(); - return 0; - } - RECOVER_MACHINE_REGS(); - length = strlen(os) + 1; - char *sm = (char *) malloc(length + 1); - strcpy(sm, os); - return sm; - }; + BACKUP_MACHINE_REGS(); + if (!(os = Yap_TermToString(Yap_GetFromSlot(t), &length, enc, + Handle_vars_f))) { + RECOVER_MACHINE_REGS(); + return 0; + } + RECOVER_MACHINE_REGS(); + length = strlen(os) + 1; + char *sm = (char *)malloc(length + 1); + strcpy(sm, os); + return sm; + }; - /// return a handle to the term - inline yhandle_t handle() { return t; }; + /// return a handle to the term + inline yhandle_t handle() { return t; }; - /// whether the term actually refers to a live object - inline bool initialized() { return t != 0; }; + /// whether the term actually refers to a live object + inline bool initialized() { return t != 0; }; }; /** * @brief YAPFunctor represents Prolog functors Name/Arity */ 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. - inline YAPFunctor(Functor ff) { f = ff; } + 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. + inline YAPFunctor(Functor ff) { f = ff; } public: - /// Constructor: receives name as an atom, plus arity - /// - /// This is the default method, and the most popular - YAPFunctor(YAPAtom at, uintptr_t arity) { f = Yap_MkFunctor(at.a, arity); } + /// Constructor: receives name as an atom, plus arity + /// + /// This is the default method, and the most popular + YAPFunctor(YAPAtom at, uintptr_t arity) { f = Yap_MkFunctor(at.a, arity); } - /// Constructor: receives name as a string plus arity - /// - /// Notice that this is designed for ISO-LATIN-1 right now - /// Note: Python confuses the 3 constructors, - /// use YAPFunctorFromString - inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) { - f = Yap_MkFunctor(Yap_LookupAtom(s), arity); - } + /// Constructor: receives name as a string plus arity + /// + /// Notice that this is designed for ISO-LATIN-1 right now + /// Note: Python confuses the 3 constructors, + /// use YAPFunctorFromString + 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 + /// + /// Note: Python confuses the 3 constructors, + /// use YAPFunctorFromWideString + 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. + YAPAtom name(void) { return YAPAtom(NameOfFunctor(f)); } - /// Constructor: receives name as a wide string plus arity - /// - /// Notice that this is designed for UNICODE right now - /// - /// Note: Python confuses the 3 constructors, - /// use YAPFunctorFromWideString - 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. - YAPAtom name(void) { return YAPAtom(NameOfFunctor(f)); } - - /// Getter: extract arity of functor as an unsigned integer - /// - /// this is for external usage. - uintptr_t arity(void) { return ArityOfFunctor(f); } + /// Getter: extract arity of functor as an unsigned integer + /// + /// this is for external usage. + uintptr_t arity(void) { return ArityOfFunctor(f); } }; /** * @brief Compound Term */ class X_API YAPApplTerm : public YAPTerm { - friend class YAPTerm; + friend class YAPTerm; public: - YAPApplTerm(Term t0) { mk(t0); } + 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 ts); + YAPApplTerm(YAPFunctor f); + inline Functor functor() { return FunctorOfTerm(gt()); } + inline YAPFunctor getFunctor() { return YAPFunctor(FunctorOfTerm(gt())); } - 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 ts); - - YAPApplTerm(YAPFunctor f); - - inline Functor functor() { return FunctorOfTerm(gt()); } - - inline YAPFunctor getFunctor() { return YAPFunctor(FunctorOfTerm(gt())); } - - Term getArg(arity_t i) { - BACKUP_MACHINE_REGS(); - Term t0 = gt(); - Term tf; - tf = ArgOfTerm(i, t0); - 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 - virtual bool isFloat() { return false; } /// type check for floating-point - virtual bool isString() { return false; } /// type check for a string " ... " - virtual bool isCompound() { return true; } /// is a primitive term - virtual bool isAppl() { return true; } /// is a structured term - virtual bool isPair() { return false; } /// is a pair term - virtual bool isGround() { return true; } /// term is ground - virtual bool isList() { return false; } /// [] is a list + Term getArg(arity_t i) { + BACKUP_MACHINE_REGS(); + Term t0 = gt(); + Term tf; + tf = ArgOfTerm(i, t0); + 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 + virtual bool isFloat() { return false; } /// type check for floating-point + virtual bool isString() { return false; } /// type check for a string " ... " + virtual bool isCompound() { return true; } /// is a primitive term + virtual bool isAppl() { return true; } /// is a structured term + virtual bool isPair() { return false; } /// is a pair term + virtual bool isGround() { return true; } /// term is ground + virtual bool isList() { return false; } /// [] is a list }; /** * @brief List Constructor Term */ class X_API YAPPairTerm : public YAPTerm { - friend class YAPTerm; - + friend class YAPTerm; public: - YAPPairTerm(Term t0) { - t0 = Deref(t0); - if (IsPairTerm(t0) || t0 == TermNil) - mk(t0); - else - Yap_ThrowError(TYPE_ERROR_LIST, t0, "YAPPairTerms"); + YAPPairTerm(Term t0) { + t0 = Deref(t0); + if (IsPairTerm(t0) || t0 == TermNil) + mk(t0); + else + Yap_ThrowError(TYPE_ERROR_LIST, t0, "YAPPairTerms"); + } + YAPPairTerm(YAPTerm hd, YAPTerm tl); + YAPPairTerm(); + Term getHead() { return (HeadOfTerm(gt())); } + Term getTail() { return (TailOfTerm(gt())); } + std::vector listToArray() { + Term *tailp; + Term t1 = gt(); + Int l = Yap_SkipList(&t1, &tailp); + if (l < 0) { + throw YAPError(TYPE_ERROR_LIST, YAPTerm(t), ""); } - - /// 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 listToArray() { - Term *tailp; - Term t1 = gt(); - Int l = Yap_SkipList(&t1, &tailp); - if (l < 0) { - throw YAPError(TYPE_ERROR_LIST, YAPTerm(t), ""); - } - std::vector o = std::vector(l); - int i = 0; - Term t = gt(); - while (t != TermNil) { - o[i++] = HeadOfTerm(t); - t = TailOfTerm(t); - } - return o; - } - - std::vector listToYAPArray() { - Term *tailp; - Term t1 = gt(); - Int l = Yap_SkipList(&t1, &tailp); - if (l < 0) { - throw YAPError(TYPE_ERROR_LIST, YAPTerm(t), ""); - } - std::vector o = std::vector(l); - int i = 0; - Term t = gt(); - while (t != TermNil) { - o[i++] = YAPTerm(HeadOfTerm(t)); - t = TailOfTerm(t); - } - return o; + std::vector o = std::vector(l); + int i = 0; + Term t = gt(); + while (t != TermNil) { + o[i++] = HeadOfTerm(t); + t = TailOfTerm(t); } + return o; + } }; /** @@ -496,9 +380,8 @@ public: class X_API YAPNumberTerm : public YAPTerm { public: - YAPNumberTerm() {}; - - bool isTagged() { return IsIntTerm(gt()); } + YAPNumberTerm(){}; + bool isTagged() { return IsIntTerm(gt()); } }; /** @@ -507,9 +390,8 @@ public: class X_API YAPIntegerTerm : public YAPNumberTerm { public: - YAPIntegerTerm(intptr_t i); - - intptr_t getInteger() { return IntegerOfTerm(gt()); }; + YAPIntegerTerm(intptr_t i); + intptr_t getInteger() { return IntegerOfTerm(gt()); }; }; /** @@ -518,69 +400,64 @@ public: class X_API YAPFloatTerm : public YAPNumberTerm { public: - YAPFloatTerm(double dbl) { mk(MkFloatTerm(dbl)); }; + YAPFloatTerm(double dbl) { mk(MkFloatTerm(dbl)); }; - double getFl() { return FloatOfTerm(gt()); }; + double getFl() { return FloatOfTerm(gt()); }; }; class X_API YAPListTerm : public YAPTerm { public: - /// Create a list term out of a standard term. Check if a valid operation. - /// - /// @param[in] the term - YAPListTerm() { mk(TermNil); /* else type_error */ } + /// Create a list term out of a standard term. Check if a valid operation. + /// + /// @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 v ); + /// Return the number of elements in a list term. + size_t length() { + Term *tailp; + 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 + Term cdr() { + Term to = gt(); + if (IsPairTerm(to)) + return (TailOfTerm(to)); + else if (to == TermNil) + return TermNil; + /* error */ + throw YAPError(TYPE_ERROR_LIST, YAPTerm(to), ""); + } + /// copy a list. + /// + /// @param[in] the list + Term dup(); - /// Create an empty list term. - /// - /// @param[in] the term - YAPListTerm(Term t0) { mk(t0); /* else type_error */ } + /// Check if the list is empty. + /// + /// @param[in] the list + inline bool nil() { + return gt() == TermNil; + } - /// 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 v ); - /// Return the number of elements in a list term. - size_t length() { - Term *tailp; - 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 - Term cdr() { - Term to = gt(); - if (IsPairTerm(to)) - return (TailOfTerm(to)); - else if (to == TermNil) - return TermNil; - /* error */ - throw YAPError(TYPE_ERROR_LIST, YAPTerm(to), ""); - } - - /// copy a list. - /// - /// @param[in] the list - Term dup(); - - /// Check if the list is empty. - /// - /// @param[in] the list - inline bool nil() { - return gt() == TermNil; - }; + ; }; /** @@ -588,19 +465,15 @@ public: */ 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()); } + /// 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()); } }; /** @@ -608,81 +481,67 @@ public: * Term Representation of an Atom */ class X_API YAPAtomTerm : public YAPTerm { - friend class YAPModule; - - // Constructor: receives a C-atom; - YAPAtomTerm(Term t) : YAPTerm(t) { IsAtomTerm(t); } + 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 - bool isFloat() { return false; } /// type check for floating-point - bool isString() { return false; } /// type check for a string " ... " - bool isCompound() { return false; } /// is a primitive term - bool isAppl() { return false; } /// is a structured term - bool isPair() { return false; } /// is a pair term - virtual bool isGround() { return true; } /// term is ground - 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; } + 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 + bool isFloat() { return false; } /// type check for floating-point + bool isString() { return false; } /// type check for a string " ... " + bool isCompound() { return false; } /// is a primitive term + bool isAppl() { return false; } /// is a structured term + bool isPair() { return false; } /// is a pair term + virtual bool isGround() { return true; } /// term is ground + 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 */ class X_API YAPVarTerm : public YAPTerm { - friend class YAPTerm; + friend class 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); - } + /// 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 - inline bool isFloat() { return false; } /// type check for floating-point - inline bool isString() { return false; } /// type check for a string " ... " - inline bool isCompound() { return false; } /// is a primitive term - inline bool isAppl() { return false; } /// is a structured term - inline bool isPair() { return false; } /// is a pair term - inline bool isGround() { return false; } /// term is ground - inline bool isList() { return false; } /// term is a list + } + 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 + inline bool isFloat() { return false; } /// type check for floating-point + inline bool isString() { return false; } /// type check for a string " ... " + inline bool isCompound() { return false; } /// is a primitive term + inline bool isAppl() { return false; } /// is a structured term + inline bool isPair() { return false; } /// is a pair term + inline bool isGround() { return false; } /// term is ground + inline bool isList() { return false; } /// term is a list }; /// @}