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()
|
||||
|
||||
|
@ -58,13 +58,13 @@ class YAPAtom {
|
||||
YAPAtom( Atom at ) { a = at; }
|
||||
public:
|
||||
/// construct new YAPAtom from UTF-8 string
|
||||
YAPAtom( const char * s) { a = Yap_LookupAtom( s ); }
|
||||
YAPAtom( const char * s) { a = Yap_LookupAtom( s ); }
|
||||
/// construct new YAPAtom from UTF-8 string
|
||||
YAPAtom( const wchar_t * s) { CACHE_REGS a = UTF32ToAtom( s PASS_REGS ); }
|
||||
YAPAtom( const wchar_t * s) { CACHE_REGS a = UTF32ToAtom( s PASS_REGS ); }
|
||||
/// construct new YAPAtom from wide string
|
||||
//YAPAtom( const wchar_t * s) { a = Yap_LookupMaybeWideAtom( s ); }
|
||||
/// construct new YAPAtom from max-length string
|
||||
YAPAtom( const char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); }
|
||||
YAPAtom( const char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); }
|
||||
/// get name of atom
|
||||
const char *getName(void);
|
||||
/// get name of (other way)
|
||||
@ -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 */
|
||||
|
||||
|
75
CXX/yapdb.hh
75
CXX/yapdb.hh
@ -34,14 +34,14 @@ 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){};
|
||||
YAPModule() : YAPAtomTerm(curModule()){};
|
||||
YAPModule(YAPAtom t) : YAPAtomTerm(t){};
|
||||
};
|
||||
|
||||
/**
|
||||
@ -54,12 +54,12 @@ class YAPModuleProp : public YAPProp {
|
||||
ModEntry *m;
|
||||
|
||||
YAPModuleProp(ModEntry *mod) { m = mod; };
|
||||
YAPModuleProp(Term tmod) { m = Yap_GetModuleEntry(tmod); };
|
||||
YAPModuleProp(Term tmod) { m = Yap_GetModuleEntry(tmod); };
|
||||
|
||||
public:
|
||||
YAPModuleProp() { CACHE_REGS m = Yap_GetModuleEntry(Yap_CurrentModule()); };
|
||||
YAPModuleProp(YAPModule tmod);
|
||||
virtual YAPModule module() { return YAPModule(m->AtomOfME); };
|
||||
YAPModuleProp(YAPModule tmod) { m = Yap_GetModuleEntry(tmod.gt()); };
|
||||
YAPModuleProp() { CACHE_REGS m = Yap_GetModuleEntry(Yap_CurrentModule()); };
|
||||
virtual YAPModule module() { return YAPModule(m->AtomOfME); };
|
||||
};
|
||||
|
||||
/**
|
||||
@ -80,14 +80,14 @@ 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); }
|
||||
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) {
|
||||
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
|
||||
@ -96,19 +96,18 @@ public:
|
||||
///
|
||||
/// Note: Python confuses the 3 constructors,
|
||||
/// use YAPFunctorFromWideString
|
||||
inline YAPFunctor(const wchar_t *s, uintptr_t arity) {
|
||||
inline YAPFunctor(const wchar_t *s, uintptr_t arity) {
|
||||
CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity);
|
||||
}
|
||||
~YAPFunctor(){};
|
||||
/// Getter: extract name of functor as an atom
|
||||
///
|
||||
/// this is for external usage.
|
||||
YAPAtom name(void) { return YAPAtom(NameOfFunctor(f)); }
|
||||
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); }
|
||||
uintptr_t arity(void) { return ArityOfFunctor(f); }
|
||||
};
|
||||
|
||||
/**
|
||||
@ -133,7 +132,7 @@ protected:
|
||||
/// It also communicates the array of arguments t[]
|
||||
/// and the array of variables
|
||||
/// back to yapquery
|
||||
YAPPredicate(const char *s0, Term &out, Term &names) {
|
||||
YAPPredicate(const char *s0, Term &out, Term &names) {
|
||||
CACHE_REGS
|
||||
BACKUP_MACHINE_REGS();
|
||||
Term *modp = NULL;
|
||||
@ -155,7 +154,7 @@ names = MkVarTerm ();
|
||||
/// Term constructor for predicates
|
||||
///
|
||||
/// It is just a call to getPred
|
||||
inline YAPPredicate(Term t) {
|
||||
inline YAPPredicate(Term t) {
|
||||
CELL *v = NULL;
|
||||
ap = getPred(t, v);
|
||||
}
|
||||
@ -175,35 +174,34 @@ names = MkVarTerm ();
|
||||
inline YAPPredicate(PredEntry *pe) { ap = pe; }
|
||||
|
||||
public:
|
||||
~YAPPredicate(){};
|
||||
|
||||
/// Functor constructor for predicates
|
||||
///
|
||||
/// Asssumes that we use the current module.
|
||||
YAPPredicate(YAPFunctor f) {
|
||||
YAPPredicate(YAPFunctor f) {
|
||||
CACHE_REGS
|
||||
ap = RepPredProp(PredPropByFunc(f.f, Yap_CurrentModule()));
|
||||
}
|
||||
|
||||
/// Functor constructor for predicates, is given a specific module.
|
||||
///
|
||||
inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
|
||||
inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
|
||||
ap = RepPredProp(PredPropByFunc(f.f, mod.t));
|
||||
}
|
||||
|
||||
/// Name/arity constructor for predicates.
|
||||
///
|
||||
inline YAPPredicate(YAPAtom at, YAPTerm mod) {
|
||||
inline YAPPredicate(YAPAtom at, YAPTerm mod) {
|
||||
ap = RepPredProp(PredPropByAtom(at.a, mod.t));
|
||||
}
|
||||
|
||||
/// Name/0 constructor for predicates.
|
||||
///
|
||||
YAPPredicate(YAPAtom at);
|
||||
YAPPredicate(YAPAtom at);
|
||||
|
||||
/// Mod:Name/Arity constructor for predicates.
|
||||
///
|
||||
inline YAPPredicate(YAPAtom at, uintptr_t arity, YAPModule mod) {
|
||||
inline YAPPredicate(YAPAtom at, uintptr_t arity, YAPModule mod) {
|
||||
if (arity) {
|
||||
Functor f = Yap_MkFunctor(at.a, arity);
|
||||
ap = RepPredProp(PredPropByFunc(f, mod.t()));
|
||||
@ -214,32 +212,32 @@ public:
|
||||
|
||||
/// Atom/Arity constructor for predicates.
|
||||
///
|
||||
YAPPredicate(YAPAtom at, uintptr_t arity);
|
||||
YAPPredicate(YAPAtom at, uintptr_t arity);
|
||||
|
||||
/// char */module constructor for predicates.
|
||||
///
|
||||
inline YAPPredicate(const char *at, uintptr_t arity) {
|
||||
inline YAPPredicate(const char *at, uintptr_t arity) {
|
||||
ap = RepPredProp(PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at), arity),
|
||||
CurrentModule));
|
||||
};
|
||||
|
||||
/// char */module constructor for predicates.
|
||||
///
|
||||
inline YAPPredicate(const char *at, uintptr_t arity, YAPTerm mod) {
|
||||
inline YAPPredicate(const char *at, uintptr_t arity, YAPTerm mod) {
|
||||
ap = RepPredProp(
|
||||
PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at), arity), mod.t));
|
||||
};
|
||||
|
||||
/// char */module constructor for predicates.
|
||||
///
|
||||
inline YAPPredicate(const char *at, YAPTerm mod) {
|
||||
inline YAPPredicate(const char *at, YAPTerm mod) {
|
||||
ap = RepPredProp(PredPropByAtom(Yap_LookupAtom(at), mod.t));
|
||||
}
|
||||
|
||||
/// module of a predicate
|
||||
///
|
||||
/// notice that modules are currently treated as atoms, this should change.
|
||||
YAPModule module() {
|
||||
YAPModule module() {
|
||||
if (ap->ModuleOfPred == PROLOG_MODULE)
|
||||
return YAPModule(AtomProlog);
|
||||
else
|
||||
@ -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.
|
||||
@ -270,15 +277,15 @@ public:
|
||||
*/
|
||||
class YAPPrologPredicate : public YAPPredicate {
|
||||
public:
|
||||
YAPPrologPredicate(YAPTerm t) : YAPPredicate(t){};
|
||||
YAPPrologPredicate(const char *s, arity_t arity) : YAPPredicate(s, arity){};
|
||||
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);
|
||||
void *retractClause(YAPTerm skeleton, bool all = false);
|
||||
/// return the Nth clause (if source is available)
|
||||
// YAPTerm clause(size_t index, YAPPredicate p) { return YAPTerm(); };
|
||||
/// return the Nth clause (if source is available)
|
||||
@ -307,7 +314,7 @@ public:
|
||||
}
|
||||
}
|
||||
};
|
||||
YAPFLIP(const char *name, uintptr_t arity, YAPModule module = YAPModule(),
|
||||
YAPFLIP(const char *name, uintptr_t arity, YAPModule module = YAPModule(),
|
||||
bool backtrackable = false)
|
||||
: YAPPredicate(YAPAtom(name), arity, module) {
|
||||
if (backtrackable) {
|
||||
@ -316,8 +323,8 @@ public:
|
||||
YAP_UserCPredicate(name, 0, arity);
|
||||
}
|
||||
};
|
||||
bool addCall(CPredicate call) { return Yap_AddCallToFli(ap, call); }
|
||||
bool addRetry(CPredicate call) { return Yap_AddRetryToFli(ap, call); }
|
||||
bool addCall(CPredicate call) { return Yap_AddCallToFli(ap, call); }
|
||||
bool addRetry(CPredicate call) { return Yap_AddRetryToFli(ap, call); }
|
||||
bool addCut(CPredicate call) { return Yap_AddCutToFli(ap, call); }
|
||||
};
|
||||
|
||||
|
682
CXX/yapi.cpp
682
CXX/yapi.cpp
File diff suppressed because it is too large
Load Diff
36
CXX/yapi.hh
36
CXX/yapi.hh
@ -5,8 +5,10 @@
|
||||
|
||||
#include <gmpxx.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
//! @{
|
||||
//! @{
|
||||
/**
|
||||
*
|
||||
* @defgroup yap-cplus-interface An object oriented interface for YAP.
|
||||
@ -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)
|
||||
|
15
CXX/yapie.hh
15
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
|
||||
|
46
CXX/yapq.hh
46
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,13 +52,14 @@ 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);
|
||||
if (!ap)
|
||||
return;
|
||||
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", vnames.text());
|
||||
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", vnames.text());
|
||||
openQuery();
|
||||
RECOVER_H();
|
||||
};
|
||||
@ -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();
|
||||
};
|
||||
|
133
CXX/yapt.hh
133
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
|
||||
};
|
||||
Term YAP_ReadBuffer(const char *s, Term *tp);
|
||||
}
|
||||
|
||||
class YAPError;
|
||||
|
||||
@ -33,20 +22,13 @@ 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);
|
||||
// Yap_DebugPlWriteln(LOCAL_HandleBase[t]);
|
||||
return Yap_GetFromSlot(t);
|
||||
// fprintf(stderr,"?%d,%lx,%p\n",t,LOCAL_HandleBase[t], HR);
|
||||
// Yap_DebugPlWriteln(LOCAL_HandleBase[t]);
|
||||
return Yap_GetFromSlot(t);
|
||||
};
|
||||
|
||||
void mk(Term t0) {
|
||||
@ -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) {
|
||||
@ -199,24 +194,24 @@ public:
|
||||
}
|
||||
|
||||
/// 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;
|
||||
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; };
|
||||
@ -260,12 +255,17 @@ public:
|
||||
class YAPApplTerm : public YAPTerm {
|
||||
friend class YAPTerm;
|
||||
YAPApplTerm(Term t0) { mk(t0); }
|
||||
|
||||
|
||||
public:
|
||||
~YAPApplTerm() {}
|
||||
YAPApplTerm(YAPFunctor f, YAPTerm ts[]);
|
||||
//YAPApplTerm(const char *s, std::vector<YAPTerm> ts);
|
||||
//YAPApplTerm(YAPFunctor f);
|
||||
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);
|
||||
YAPFunctor getFunctor();
|
||||
Term getArg(arity_t i) {
|
||||
BACKUP_MACHINE_REGS();
|
||||
@ -327,8 +327,8 @@ public:
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Floating Point Term
|
||||
*/
|
||||
* @brief Floating Point Term
|
||||
*/
|
||||
|
||||
class YAPFloatTerm : public YAPNumberTerm {
|
||||
public:
|
||||
@ -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.
|
||||
///
|
||||
@ -418,7 +417,7 @@ class YAPAtomTerm : public YAPTerm {
|
||||
// Constructor: receives a C-atom;
|
||||
YAPAtomTerm(Atom a) { mk(MkAtomTerm(a)); }
|
||||
YAPAtomTerm(Term t) : YAPTerm(t) { IsAtomTerm(t); }
|
||||
|
||||
|
||||
public:
|
||||
// Constructor: receives an atom;
|
||||
YAPAtomTerm(YAPAtom a) : YAPTerm() { mk(MkAtomTerm(a.a)); }
|
||||
@ -430,15 +429,15 @@ 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
|
||||
virtual bool isGround() { return true; } /// term is ground
|
||||
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())); }
|
||||
|
Reference in New Issue
Block a user