support python interface.
This commit is contained in:
parent
5d6ff87d19
commit
b17f4967cb
@ -11,15 +11,18 @@ set (CXX_SOURCES
|
||||
|
||||
list(APPEND LIBYAP_SOURCES ${CXX_SOURCES} PARENT_SCOPE)
|
||||
|
||||
if (ANDROID OR WIN32)
|
||||
add_component (Yap++ ${CXX_SOURCES} )
|
||||
else()
|
||||
add_external (Yap++ ${CXX_SOURCES} )
|
||||
MY_target_link_libraries(Yap++ ${CMAKE_DL_LIBS} libYap)
|
||||
if ( WIN32)
|
||||
add_component (YAP++ ${CXX_SOURCES} )
|
||||
|
||||
MY_install(TARGETS Yap++
|
||||
LIBRARY DESTINATION ${libdir}
|
||||
ARCHIVE DESTINATION ${libdir}
|
||||
set_property( DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "_YAP_NOT_INSTALLED_=1;HAVE_CONFIG_H=1;_GNU_SOURCE;YAP_KERNEL=1" )
|
||||
else()
|
||||
add_external (YAP++ ${CXX_SOURCES} )
|
||||
MY_target_link_libraries(YAP++ ${CMAKE_DL_LIBS} libYap)
|
||||
|
||||
MY_install(TARGETS YAP++
|
||||
LIBRARY DESTINATION ${dlls}
|
||||
RUNTIME DESTINATION ${dlls}
|
||||
ARCHIVE DESTINATION ${dlls}
|
||||
)
|
||||
endif()
|
||||
|
||||
|
@ -89,10 +89,11 @@ class YAPProp {
|
||||
PropTag tag( Prop p ) { return (PropTag)(p->KindOfPE); }
|
||||
public:
|
||||
/// get name of property
|
||||
virtual YAPAtom name() = 0;
|
||||
// virtual YAPAtom name();
|
||||
virtual ~YAPProp() {};
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif /* YAPA_HH */
|
||||
|
||||
|
19
CXX/yapdb.hh
19
CXX/yapdb.hh
@ -34,12 +34,12 @@ class YAPModule;
|
||||
*/
|
||||
class YAPModule : protected YAPAtomTerm {
|
||||
friend class YAPPredicate;
|
||||
friend class YAPModuleProp;
|
||||
YAPModule(Term t) : YAPAtomTerm(t){};
|
||||
Term t() { return gt(); }
|
||||
Term curModule() { CACHE_REGS return Yap_CurrentModule(); }
|
||||
|
||||
public:
|
||||
~YAPModule(){};
|
||||
YAPModule() : YAPAtomTerm(curModule()){};
|
||||
YAPModule(YAPAtom t) : YAPAtomTerm(t){};
|
||||
};
|
||||
@ -57,8 +57,8 @@ class YAPModuleProp : public YAPProp {
|
||||
YAPModuleProp(Term tmod) { m = Yap_GetModuleEntry(tmod); };
|
||||
|
||||
public:
|
||||
YAPModuleProp(YAPModule tmod) { m = Yap_GetModuleEntry(tmod.gt()); };
|
||||
YAPModuleProp() { CACHE_REGS m = Yap_GetModuleEntry(Yap_CurrentModule()); };
|
||||
YAPModuleProp(YAPModule tmod);
|
||||
virtual YAPModule module() { return YAPModule(m->AtomOfME); };
|
||||
};
|
||||
|
||||
@ -99,7 +99,6 @@ public:
|
||||
inline YAPFunctor(const wchar_t *s, uintptr_t arity) {
|
||||
CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity);
|
||||
}
|
||||
~YAPFunctor(){};
|
||||
/// Getter: extract name of functor as an atom
|
||||
///
|
||||
/// this is for external usage.
|
||||
@ -175,7 +174,6 @@ names = MkVarTerm ();
|
||||
inline YAPPredicate(PredEntry *pe) { ap = pe; }
|
||||
|
||||
public:
|
||||
~YAPPredicate(){};
|
||||
|
||||
/// Functor constructor for predicates
|
||||
///
|
||||
@ -256,6 +254,15 @@ public:
|
||||
return YAPAtom(NameOfFunctor(ap->FunctorOfPred));
|
||||
}
|
||||
|
||||
/// functor of predicate
|
||||
///
|
||||
/// onlu defined if arity >= 1
|
||||
YAPFunctor functor() {
|
||||
if (ap->ArityOfPE)
|
||||
return YAPFunctor(ap->FunctorOfPred);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/// arity of predicate
|
||||
///
|
||||
/// we return a positive number.
|
||||
@ -273,10 +280,10 @@ public:
|
||||
YAPPrologPredicate(YAPTerm t) : YAPPredicate(t){};
|
||||
YAPPrologPredicate(const char *s, arity_t arity) : YAPPredicate(s, arity){};
|
||||
/// add a new clause
|
||||
void *assertClause(YAPTerm clause, bool last = true,
|
||||
bool assertClause(YAPTerm clause, bool last = true,
|
||||
YAPTerm source = YAPTerm());
|
||||
/// add a new tuple
|
||||
void *assertFact(YAPTerm *tuple, bool last = true);
|
||||
bool assertFact(YAPTerm *tuple, bool last = true);
|
||||
/// retract at least the first clause matching the predicate.
|
||||
void *retractClause(YAPTerm skeleton, bool all = false);
|
||||
/// return the Nth clause (if source is available)
|
||||
|
518
CXX/yapi.cpp
518
CXX/yapi.cpp
File diff suppressed because it is too large
Load Diff
34
CXX/yapi.hh
34
CXX/yapi.hh
@ -5,6 +5,8 @@
|
||||
|
||||
#include <gmpxx.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
//! @{
|
||||
/**
|
||||
@ -67,38 +69,6 @@ extern "C" {
|
||||
// taken from yap_structs.h
|
||||
#include "iopreds.h"
|
||||
|
||||
#ifdef SWIGPYTHON
|
||||
extern PyObject *yap_to_pythond(YAP_Term t, bool eval);
|
||||
extern PyObject *term_to_python(yhandle_t t, bool eval);
|
||||
extern PyObject *deref_term_to_python(yhandle_t t);
|
||||
X_API bool init_python(void);
|
||||
extern Term pythonToYAP(PyObject *p);
|
||||
|
||||
extern PyObject *py_Main;
|
||||
|
||||
extern inline PyObject *AtomToPy(const char *s) {
|
||||
if (strcmp(s, "true") == 0)
|
||||
return Py_True;
|
||||
if (strcmp(s, "false") == 0)
|
||||
return Py_False;
|
||||
if (strcmp(s, "none") == 0)
|
||||
return Py_None;
|
||||
if (strcmp(s, "[]") == 0)
|
||||
return PyList_New(0);
|
||||
else if (strcmp(s, "{}") == 0)
|
||||
return PyDict_New();
|
||||
/* return __main__,s */
|
||||
else if (PyObject_HasAttrString(py_Main, s)) {
|
||||
return PyObject_GetAttrString(py_Main, s);
|
||||
}
|
||||
// no way to translate
|
||||
return NULL;
|
||||
}
|
||||
|
||||
X_API extern PyObject *yap_to_python(YAP_Term t, bool eval);
|
||||
|
||||
#endif
|
||||
|
||||
X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, YAP_Arity arity);
|
||||
|
||||
/* void UserCPredicateWithArgs(const char *name, int *fn(), unsigned int arity)
|
||||
|
13
CXX/yapie.hh
13
CXX/yapie.hh
@ -7,12 +7,17 @@ class YAPTerm;
|
||||
|
||||
/// take information on a Prolog error:
|
||||
class YAPError {
|
||||
std::string name, errorClass, info;
|
||||
yap_error_number ID;
|
||||
std::string goal, info;
|
||||
int swigcode;
|
||||
|
||||
public:
|
||||
/// error handling when receiving the error term
|
||||
YAPError(){};
|
||||
YAPError(){
|
||||
//ID = LOCAL_ActiveError->errorNo;
|
||||
}
|
||||
/// error handler object with initial data when receiving the error term
|
||||
YAPError(yap_error_number id, YAPTerm culprit, std::string txt);
|
||||
|
||||
/// we just know the error number
|
||||
/// exact error ID
|
||||
yap_error_number getID() { return LOCAL_ActiveError->errorNo; };
|
||||
@ -27,7 +32,7 @@ public:
|
||||
/// the term that caused the bug
|
||||
// YAPTerm getCulprit(LOCAL_ActiveError->errorFile){};
|
||||
/// text describing the Error
|
||||
const char *text();
|
||||
std::string text();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
44
CXX/yapq.hh
44
CXX/yapq.hh
@ -13,12 +13,13 @@ class YAPPredicate;
|
||||
* interface to a YAP Query;
|
||||
* uses an SWI-like status info internally.
|
||||
*/
|
||||
class YAPQuery : public YAPPredicate {
|
||||
class YAPQuery : public YAPPredicate
|
||||
{
|
||||
bool q_open;
|
||||
int q_state;
|
||||
yhandle_t q_g, q_handles;
|
||||
struct yami *q_p, *q_cp;
|
||||
jmp_buf q_env;
|
||||
sigjmp_buf q_env;
|
||||
int q_flags;
|
||||
YAP_dogoalinfo q_h;
|
||||
YAPQuery *oq;
|
||||
@ -51,7 +52,8 @@ 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, goal, names) {
|
||||
inline YAPQuery(const char *s) : YAPPredicate(s, goal, names)
|
||||
{
|
||||
BACKUP_H();
|
||||
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %ld",
|
||||
LOCAL_CurSlot);
|
||||
@ -65,7 +67,8 @@ public:
|
||||
///
|
||||
/// It is given an atom, and a Prolog term that should be a callable
|
||||
/// goal, say `main`, `init`, `live`.
|
||||
inline YAPQuery(YAPAtom g) : YAPPredicate(g) {
|
||||
inline YAPQuery(YAPAtom g) : YAPPredicate(g)
|
||||
{
|
||||
goal = YAPAtomTerm(g).gt();
|
||||
names = TermNil;
|
||||
openQuery();
|
||||
@ -89,10 +92,10 @@ public:
|
||||
const char *text();
|
||||
/// remove alternatives in the current search space, and finish the current
|
||||
/// query
|
||||
void cut();
|
||||
/// finish the current query: undo all bindings.
|
||||
void close();
|
||||
/// query variables.
|
||||
void cut();
|
||||
Term namedVars();
|
||||
/// query variables, but copied out
|
||||
Term namedVarsCopy();
|
||||
@ -101,7 +104,8 @@ public:
|
||||
/// 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;
|
||||
@ -113,7 +117,8 @@ public:
|
||||
/// This class implements a callback Prolog-side. It will be inherited by the
|
||||
/// Java or Python
|
||||
/// class that actually implements the callback.
|
||||
class YAPCallback {
|
||||
class YAPCallback
|
||||
{
|
||||
public:
|
||||
virtual ~YAPCallback() {}
|
||||
virtual void run() { LOG("callback"); }
|
||||
@ -126,7 +131,8 @@ public:
|
||||
*
|
||||
*
|
||||
*/
|
||||
class YAPEngine {
|
||||
class YAPEngine
|
||||
{
|
||||
private:
|
||||
YAPCallback *_callback;
|
||||
YAP_init_args init_args;
|
||||
@ -153,14 +159,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);
|
||||
}
|
||||
@ -178,22 +186,30 @@ public:
|
||||
/// current directory for the engine
|
||||
bool call(YAPPredicate ap, YAPTerm ts[]);
|
||||
/// current directory for the engine
|
||||
bool goalt(YAPTerm t);
|
||||
bool goalt(YAPTerm Yt) { return Yt.term(); };
|
||||
/// current directory for the engine
|
||||
bool goal(Term t);
|
||||
bool mgoal(Term t, Term tmod);
|
||||
/// current directory for the engine
|
||||
|
||||
bool goal(Term t)
|
||||
{
|
||||
return mgoal(t, CurrentModule);
|
||||
}
|
||||
/// reset Prolog state
|
||||
void reSet();
|
||||
/// release: 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();
|
||||
};
|
||||
|
79
CXX/yapt.hh
79
CXX/yapt.hh
@ -2,20 +2,9 @@
|
||||
#ifndef YAPT_HH
|
||||
#define YAPT_HH 1
|
||||
|
||||
#include "config.h"
|
||||
|
||||
extern "C" {
|
||||
Term YAP_ReadBuffer(const char *s, Term *tp);
|
||||
#if defined(SWIGPYTHON) && 0
|
||||
#include <Python.h>
|
||||
extern Term pythonToYAP(PyObject *inp);
|
||||
#define YAPTerm _YAPTERM
|
||||
#elifndef HAVE_PYTHON_H
|
||||
typdef struct { int no_python; } PyObject;
|
||||
#else
|
||||
#include <Python.h>
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
class YAPError;
|
||||
|
||||
@ -33,15 +22,8 @@ class YAPTerm {
|
||||
|
||||
protected:
|
||||
yhandle_t t; /// handle to term, equivalent to term_t
|
||||
public:
|
||||
virtual ~YAPTerm() {
|
||||
// fprintf(stderr,"-%d,%lx,%p ",t,LOCAL_HandleBase[t] ,HR);
|
||||
// Yap_DebugPlWriteln(LOCAL_HandleBase[t]); }
|
||||
// LOCAL_HandleBase[t] = TermFreeTerm;
|
||||
// while ( LOCAL_HandleBase[LOCAL_CurSlot-1] == TermFreeTerm)
|
||||
LOCAL_CurSlot--;
|
||||
};
|
||||
|
||||
public:
|
||||
Term gt() {
|
||||
CACHE_REGS
|
||||
// fprintf(stderr,"?%d,%lx,%p\n",t,LOCAL_HandleBase[t], HR);
|
||||
@ -55,14 +37,12 @@ public:
|
||||
};
|
||||
|
||||
YAPTerm(Term tn) { mk(tn); };
|
||||
YAPTerm(PyObject *inp) {
|
||||
#ifdef SWIGPYTHON
|
||||
Term tinp = pythonToYAP(inp);
|
||||
t = Yap_InitSlot(tinp);
|
||||
#else
|
||||
t = 0;
|
||||
// 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
|
||||
@ -75,6 +55,21 @@ public:
|
||||
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--;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
/// construct a term out of an integer (if you know object type use
|
||||
/// YAPIntegerTerm)
|
||||
/// YAPTerm(long int num) { mk(MkIntegerTerm(num)); }
|
||||
@ -96,7 +91,7 @@ public:
|
||||
inline void bind(YAPTerm *b) { LOCAL_HandleBase[t] = b->term(); }
|
||||
/// from YAPTerm to Term (internal YAP representation)
|
||||
/// fetch a sub-term
|
||||
Term &operator[](size_t n);
|
||||
Term &operator[](arity_t n);
|
||||
// const YAPTerm *vars();
|
||||
/// this term is == to t1
|
||||
virtual bool exactlyEqual(YAPTerm t1) {
|
||||
@ -262,10 +257,15 @@ class YAPApplTerm : public YAPTerm {
|
||||
YAPApplTerm(Term t0) { mk(t0); }
|
||||
|
||||
public:
|
||||
~YAPApplTerm() {}
|
||||
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 char *s, std::vector<YAPTerm> ts);
|
||||
//YAPApplTerm(YAPFunctor f);
|
||||
YAPApplTerm(const std::string s, std::vector<YAPTerm> ts);
|
||||
YAPApplTerm(YAPFunctor f);
|
||||
YAPFunctor getFunctor();
|
||||
Term getArg(arity_t i) {
|
||||
BACKUP_MACHINE_REGS();
|
||||
@ -375,8 +375,7 @@ public:
|
||||
else if (to == TermNil)
|
||||
return TermNil;
|
||||
/* error */
|
||||
Yap_Error(TYPE_ERROR_LIST, t, 0);
|
||||
throw YAPError();
|
||||
throw YAPError(TYPE_ERROR_LIST, YAPTerm(to), "");
|
||||
}
|
||||
/// copy a list.
|
||||
///
|
||||
@ -430,14 +429,14 @@ public:
|
||||
YAPAtomTerm(wchar_t *s);
|
||||
// Constructor: receives a sequence of n wchar_ts, whatever they may be;
|
||||
YAPAtomTerm(wchar_t *s, size_t len);
|
||||
virtual bool isVar() { return false; } /// type check for unbound
|
||||
virtual bool isAtom() { return true; } /// 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 false; } /// is a primitive term
|
||||
virtual bool isAppl() { return false; } /// is a structured term
|
||||
virtual bool isPair() { return false; } /// is a pair term
|
||||
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;
|
||||
|
Reference in New Issue
Block a user