support for init, callbacks
This commit is contained in:
parent
2455a1d83c
commit
0241151599
217
CXX/yapi.cpp
217
CXX/yapi.cpp
@ -2,7 +2,7 @@
|
||||
#define YAP_CPP_INTERFACE 1
|
||||
|
||||
#include "yapi.hh"
|
||||
|
||||
#include "SWI-Stream.h"
|
||||
|
||||
|
||||
YAPAtomTerm::YAPAtomTerm(char *s) { // build string
|
||||
@ -158,6 +158,10 @@ YAPPairTerm::YAPPairTerm() : YAPTerm() {
|
||||
t = Yap_MkNewPairTerm( );
|
||||
}
|
||||
|
||||
void YAPTerm::mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 PASS_REGS); }
|
||||
|
||||
Term YAPTerm::gt() { CACHE_REGS return Yap_GetFromSlot( t PASS_REGS); }
|
||||
|
||||
YAP_tag_t YAPTerm::tag() {
|
||||
Term tt = gt( );
|
||||
if (IsVarTerm(tt)) {
|
||||
@ -264,28 +268,33 @@ intptr_t YAPTerm::hash(size_t sz, size_t depth, bool variant) {
|
||||
}
|
||||
|
||||
char *YAPTerm::text(void) {
|
||||
size_t sze = 4096, sz, length;
|
||||
char *buf = new char[sze], *b;
|
||||
size_t sze = 4096, length;
|
||||
char *os;
|
||||
int enc;
|
||||
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "I LCL0+%p/t=(%d) %x", LCL0, t, LCL0[-15]) ; }
|
||||
BACKUP_MACHINE_REGS();
|
||||
if ((b = Yap_TermToString(gt(), buf, sze, &length, &enc, 0)) != buf) {
|
||||
if (b) free(b);
|
||||
if (!(os = Yap_HandleToString(t, sze, &length, &enc, 0))) {
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "IIa LCL0+t=(%p) %x", LCL0, LCL0[-15]) ; }
|
||||
RECOVER_MACHINE_REGS();
|
||||
return NULL;
|
||||
}
|
||||
sz = length+1;
|
||||
char *os = new char[sz];
|
||||
if (!os) {
|
||||
RECOVER_MACHINE_REGS();
|
||||
return NULL;
|
||||
}
|
||||
memcpy(os, buf, sz);
|
||||
delete buf;
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "II LCL0+t=(%p) %x", LCL0, LCL0[-15]) ; }
|
||||
RECOVER_MACHINE_REGS();
|
||||
return os;
|
||||
}
|
||||
|
||||
bool YAPListTerm::nil() {
|
||||
CACHE_REGS
|
||||
__android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "t=%d LCL0+t=(%p)", t, LCL0+t) ;
|
||||
return gt() == TermNil;
|
||||
}
|
||||
|
||||
|
||||
YAPIntegerTerm::YAPIntegerTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
|
||||
|
||||
|
||||
|
||||
/*
|
||||
YAPTerm *YAPTerm::vars()
|
||||
{
|
||||
@ -297,6 +306,22 @@ YAPTerm *YAPTerm::vars()
|
||||
}
|
||||
*/
|
||||
|
||||
YAPTerm::YAPTerm(void *ptr) { CACHE_REGS mk( MkIntegerTerm( (Int)ptr ) );}
|
||||
|
||||
YAPTerm::YAPTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
|
||||
|
||||
YAPTerm YAPListTerm::car()
|
||||
{
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "t=%d LCL0+t=(%p)", t, LCL0+t) ; }
|
||||
Term to = gt();
|
||||
if (IsPairTerm(to))
|
||||
return YAPTerm(HeadOfTerm(to));
|
||||
else
|
||||
return MkIntTerm(-1);
|
||||
}
|
||||
|
||||
YAPVarTerm::YAPVarTerm() { CACHE_REGS mk( MkVarTerm( ) ); }
|
||||
|
||||
|
||||
char *YAPAtom::name(void) {
|
||||
if (IsWideAtom(a)) {
|
||||
@ -352,21 +377,63 @@ char *YAPAtom::name(void) {
|
||||
YAPPredicate::YAPPredicate(const char *s, Term **outp, term_t &vnames) {
|
||||
CACHE_REGS
|
||||
vnames = Yap_NewSlots(1 PASS_REGS);
|
||||
Term t = Yap_StringToTerm(s, strlen(s)+1, vnames), m;
|
||||
Term t = Yap_StringToTerm(s, strlen(s)+1, vnames);
|
||||
ap = getPred( t, outp );
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "OUT vnames=%d ap=%p LCL0=%p", vnames, ap, LCL0) ; }
|
||||
}
|
||||
|
||||
YAPPredicate::YAPPredicate(YAPAtom at) {
|
||||
CACHE_REGS
|
||||
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
|
||||
}
|
||||
|
||||
YAPPredicate::YAPPredicate(YAPAtom at, arity_t arity) {
|
||||
CACHE_REGS
|
||||
if (arity) {
|
||||
Functor f = Yap_MkFunctor(at.a, arity);
|
||||
ap = RepPredProp(PredPropByFunc(f,CurrentModule));
|
||||
} else {
|
||||
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
|
||||
}
|
||||
}
|
||||
|
||||
/// auxiliary routine to find a predicate in the current module.
|
||||
PredEntry *YAPPredicate::getPred( Term t, Term **outp ) {
|
||||
CACHE_REGS
|
||||
Term m = CurrentModule ;
|
||||
t = Yap_StripModule(t, &m);
|
||||
if (IsVarTerm(t) || IsNumTerm(t))
|
||||
if (IsVarTerm(t) || IsNumTerm(t)) {
|
||||
ap = NULL;
|
||||
if (outp) *outp = NULL;
|
||||
}
|
||||
if (IsAtomTerm(t)) {
|
||||
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
||||
*outp = NULL;
|
||||
} else if (IsApplTerm(t)) {
|
||||
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
|
||||
*outp = RepAppl(t)+1;
|
||||
if (outp) *outp = NULL;
|
||||
} else if (IsPairTerm(t)) {
|
||||
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
|
||||
*outp = RepPair(t);
|
||||
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE));
|
||||
if (outp) *outp = HR;
|
||||
HR[0] = RepPair(t)[0];
|
||||
HR[1] = m;
|
||||
HR+=2;
|
||||
} else {
|
||||
Functor f = FunctorOfTerm(t);
|
||||
if (IsExtensionFunctor(f)) {
|
||||
ap = NULL;
|
||||
if (outp) *outp = NULL;
|
||||
}
|
||||
ap = RepPredProp(PredPropByFunc(f, m));
|
||||
if (outp) *outp = RepAppl(t)+1;
|
||||
}
|
||||
return ap;
|
||||
}
|
||||
|
||||
YAPPredicate::YAPPredicate(YAPFunctor f) {
|
||||
CACHE_REGS
|
||||
ap = RepPredProp(PredPropByFunc(f.f,CurrentModule));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
YAPQuery::initQuery( Term *ts )
|
||||
@ -385,7 +452,7 @@ void
|
||||
YAPQuery::initQuery( YAPTerm t[], arity_t arity )
|
||||
{
|
||||
Term *ts = new Term[arity];
|
||||
for (int i = 0; i < arity; i++)
|
||||
for (arity_t i = 0; i < arity; i++)
|
||||
ts[i] = t[i].term();
|
||||
|
||||
return initQuery( ts );
|
||||
@ -409,25 +476,37 @@ YAPQuery::YAPQuery(YAPPredicate p, YAPTerm t[]): YAPPredicate(p.ap)
|
||||
initQuery( t , p.ap->ArityOfPE);
|
||||
}
|
||||
|
||||
int YAPQuery::next()
|
||||
YAPListTerm YAPQuery::namedVars() {
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "vnames=%d LCL0=%p %x", vnames, LCL0, LCL0[vnames]) ; }
|
||||
CACHE_REGS
|
||||
Term o = Yap_GetFromSlot( vnames PASS_REGS );
|
||||
return YAPListTerm( o );
|
||||
}
|
||||
|
||||
bool YAPQuery::next()
|
||||
{
|
||||
CACHE_REGS
|
||||
int result;
|
||||
if (this->q_open != 1) return 0;
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, " CALL vnames=%d ap=%p LCL0=%p %x", vnames, ap, LCL0, LCL0[vnames]) ; }
|
||||
if (this->q_open != 1) return false;
|
||||
if (setjmp(((YAPQuery *)LOCAL_execution)->q_env))
|
||||
return 0;
|
||||
return false;
|
||||
// don't forget, on success these guys must create slots
|
||||
if (this->q_state == 0) {
|
||||
result = YAP_EnterGoal((YAP_PredEntryPtr)this->ap, this->q_g, &this->q_h);
|
||||
extern void toggle_low_level_trace(void);
|
||||
//toggle_low_level_trace();
|
||||
result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)this->ap, this->q_g, &this->q_h);
|
||||
} else {
|
||||
LOCAL_AllowRestart = this->q_open;
|
||||
result = YAP_RetryGoal(&this->q_h);
|
||||
result = (bool)YAP_RetryGoal(&this->q_h);
|
||||
}
|
||||
this->q_state = 1;
|
||||
if (result == 0) {
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "FAIL vnames=%d %d LCL0=(%p) %x", vnames, result, LCL0, LCL0[vnames]) ; }
|
||||
if (!result) {
|
||||
YAP_LeaveGoal(FALSE, &this->q_h);
|
||||
this->q_open = 0;
|
||||
}
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "SUCCESS vnames=%d ap=%p LCL0=(%p) %x", vnames, ap, LCL0, LCL0[vnames]) ; }
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -466,14 +545,86 @@ int YAPPredicate::call(YAPTerm t[])
|
||||
return ret;
|
||||
}
|
||||
|
||||
YAPEngine::YAPEngine(YAPParams const& params)
|
||||
{ YAP_Init( (YAP_init_args *)¶ms.init_args ); }
|
||||
static YAPEngine *curren;
|
||||
|
||||
#if __ANDROID__
|
||||
|
||||
YAPEngine::YAPEngine()
|
||||
#include <jni.h>
|
||||
#include <string.h>
|
||||
|
||||
extern AAssetManager *assetManager;
|
||||
|
||||
extern char *Yap_AndroidBufp;
|
||||
static size_t Yap_AndroidMax, Yap_AndroidSz;
|
||||
|
||||
extern void(*Yap_DisplayWithJava)(int c);
|
||||
|
||||
static void
|
||||
displayWithJava(int c)
|
||||
{
|
||||
YAPParams *params = new YAPParams();
|
||||
YAP_Init( ¶ms->init_args );
|
||||
char *ptr = Yap_AndroidBufp;
|
||||
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';
|
||||
__android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "after char %c:%s %p",c, Yap_AndroidBufp, curren);
|
||||
curren->display(Yap_AndroidBufp);
|
||||
Yap_AndroidSz = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
YAPEngine::YAPEngine( char *savedState,
|
||||
size_t stackSize,
|
||||
size_t trailSize,
|
||||
size_t maxStackSize,
|
||||
size_t maxTrailSize,
|
||||
char *libDir,
|
||||
char *bootFile,
|
||||
char *goal,
|
||||
char *topLevel,
|
||||
bool script,
|
||||
bool fastBoot,
|
||||
YAPCallback *cb): _callback(0)
|
||||
{ // a single engine can be active
|
||||
#if __ANDROID__
|
||||
if (assetManager == NULL)
|
||||
return;
|
||||
Yap_DisplayWithJava = displayWithJava;
|
||||
Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096);
|
||||
Yap_AndroidBufp[0] = '\0';
|
||||
Yap_AndroidSz = 0;
|
||||
#endif
|
||||
memset((void *)&init_args, 0, sizeof(init_args));
|
||||
init_args.SavedState = savedState;
|
||||
init_args.StackSize = stackSize;
|
||||
init_args.TrailSize = trailSize;
|
||||
init_args.MaxStackSize = maxStackSize;
|
||||
init_args.MaxTrailSize = maxTrailSize;
|
||||
init_args.YapLibDir = libDir;
|
||||
init_args.YapPrologBootFile = bootFile;
|
||||
init_args.YapPrologGoal = goal;
|
||||
init_args.YapPrologTopLevelGoal = topLevel;
|
||||
init_args.HaltAfterConsult = script;
|
||||
init_args.FastBoot = fastBoot;
|
||||
delYAPCallback();
|
||||
if (cb) setYAPCallback(cb);
|
||||
curren = this;
|
||||
YAP_Init( &init_args );
|
||||
}
|
||||
|
||||
YAPQuery *YAPEngine::query( char *s ) {
|
||||
YAPQuery *n = new YAPQuery( s );
|
||||
return n;
|
||||
}
|
||||
|
373
CXX/yapi.hh
373
CXX/yapi.hh
@ -13,16 +13,26 @@
|
||||
* with the swig interface language generator. It uses ideas from the old YAP
|
||||
* interface and from the SWI foreign language interface.
|
||||
*
|
||||
* @{
|
||||
*
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <config.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define old_cplusplus __cplusplus
|
||||
#undef __cplusplus
|
||||
#endif
|
||||
#if USE_GMP
|
||||
#include <gmp.h>
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#ifdef old_cplusplus
|
||||
#define __cplusplus old_cplusplus
|
||||
#undef old_cplusplus
|
||||
#endif
|
||||
|
||||
#include "Yap.h"
|
||||
|
||||
@ -71,30 +81,33 @@ extern Term Yap_StringToTerm(const char *s, size_t len, term_t bindings);
|
||||
|
||||
//#include <vector>
|
||||
|
||||
|
||||
class YAPEngine;
|
||||
class YAPTermHandle;
|
||||
class YAPAtom;
|
||||
class YAPFunctor;
|
||||
class YAPApplTerm;
|
||||
class YAPPairTerm;
|
||||
class YAPQuery;
|
||||
|
||||
class TypeError {};
|
||||
|
||||
/**
|
||||
* @brief Generic Prolog Term
|
||||
*/
|
||||
class YAPTerm {
|
||||
friend class YAPPredicate;
|
||||
friend class YAPTermHandle;
|
||||
friend class YAPApplTerm;
|
||||
friend class YAPPairTerm;
|
||||
friend class YAPListTerm;
|
||||
protected:
|
||||
term_t t;
|
||||
inline void mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 PASS_REGS); }
|
||||
inline Term gt(void) { CACHE_REGS return Yap_GetFromSlot( t PASS_REGS); }
|
||||
handle_t t;
|
||||
void mk(Term t0);
|
||||
Term gt();
|
||||
YAPTerm(Term tn) { mk( tn ); }
|
||||
public:
|
||||
YAPTerm() { mk(TermNil); } // do nothing constructor
|
||||
YAPTerm(void *ptr) { CACHE_REGS mk( MkIntegerTerm( (Int)ptr ) );}
|
||||
YAPTerm(Term tn) { mk( tn ); }
|
||||
YAPTerm(intptr_t i);
|
||||
YAPTerm(void *ptr);
|
||||
YAPTerm(char *s) { Term tp ; mk( YAP_ReadBuffer(s,&tp) ); }
|
||||
/*~YAPTerm(void) {
|
||||
CACHE_REGS
|
||||
@ -138,10 +151,10 @@ public:
|
||||
/**
|
||||
* @brief Variable Term
|
||||
*/
|
||||
class YAPVarTerm: private YAPTerm {
|
||||
class YAPVarTerm: public YAPTerm {
|
||||
YAPVarTerm(Term t) { if (IsVarTerm(t)) mk( t ); }
|
||||
public:
|
||||
YAPVarTerm() { CACHE_REGS mk( MkVarTerm( ) ); }
|
||||
YAPVarTerm();
|
||||
CELL *getVar() { return VarOfTerm( gt() ); }
|
||||
bool unbound() { return IsUnboundVar(VarOfTerm( gt() )); }
|
||||
};
|
||||
@ -149,7 +162,7 @@ public:
|
||||
/**
|
||||
* @brief Compound Term
|
||||
*/
|
||||
class YAPApplTerm: private YAPTerm {
|
||||
class YAPApplTerm: public YAPTerm {
|
||||
friend class YAPTerm;
|
||||
YAPApplTerm(Term t0) { mk(t0); }
|
||||
public:
|
||||
@ -163,7 +176,7 @@ public:
|
||||
/**
|
||||
* @brief List Constructor Term
|
||||
*/
|
||||
class YAPPairTerm: private YAPTerm {
|
||||
class YAPPairTerm: public YAPTerm {
|
||||
friend class YAPTerm;
|
||||
YAPPairTerm(Term t0) { if (IsPairTerm(t0)) mk( t0 ); else mk(0); }
|
||||
public:
|
||||
@ -177,14 +190,14 @@ public:
|
||||
* @brief Integer Term
|
||||
*/
|
||||
|
||||
class YAPIntegerTerm: private YAPTerm {
|
||||
class YAPIntegerTerm: public YAPTerm {
|
||||
public:
|
||||
YAPIntegerTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
|
||||
YAPIntegerTerm(intptr_t i);
|
||||
intptr_t getInteger() { return IntegerOfTerm( gt() ); }
|
||||
bool isTagged() { return IsIntTerm( gt() ); }
|
||||
};
|
||||
|
||||
class YAPListTerm: private YAPTerm {
|
||||
class YAPListTerm: public YAPTerm {
|
||||
public:
|
||||
/// Create a list term out of a standard term. Check if a valid operation.
|
||||
///
|
||||
@ -195,22 +208,30 @@ public:
|
||||
/// @param[in] the array of terms
|
||||
/// @param[in] the length of the array
|
||||
YAPListTerm(YAPTerm ts[], size_t n);
|
||||
/// Create a list term out of an array of Prolog terms.
|
||||
///
|
||||
/// @param[in] the array of terms
|
||||
/// @param[in] the length of the array
|
||||
YAPListTerm(Term ts[], size_t n);
|
||||
*/
|
||||
// YAPListTerm( vector<YAPTerm> v );
|
||||
/// Return the number of elements in a list term.
|
||||
size_t length() { Term *tailp; Term t1 = gt(); return Yap_SkipList(&t1, &tailp); }
|
||||
// vector<YAPTerm> toVector();
|
||||
/// Create an array of term out of a list.
|
||||
/// Extract the first element of a list.
|
||||
///
|
||||
/// @param[in] the list
|
||||
YAPTerm car() { if (IsPairTerm(gt())) return YAPTerm(HeadOfTerm(gt())); else return YAPTerm((term_t)0); }
|
||||
YAPListTerm cdr() { if (IsPairTerm(gt())) return YAPListTerm(TailOfTerm(gt())); else return YAPListTerm((term_t)0); }
|
||||
bool nil() { return gt() == TermNil; }
|
||||
YAPTerm car();
|
||||
/// Extract the tail elements of a list.
|
||||
///
|
||||
/// @param[in] the list
|
||||
YAPListTerm cdr()
|
||||
{
|
||||
Term to = gt();
|
||||
if (IsPairTerm( to ))
|
||||
return YAPListTerm(TailOfTerm( to ));
|
||||
else
|
||||
return MkIntTerm(-1);
|
||||
}
|
||||
|
||||
/// Check if the list is empty.
|
||||
///
|
||||
/// @param[in] the list
|
||||
bool nil();
|
||||
};
|
||||
|
||||
/**
|
||||
@ -233,7 +254,7 @@ public:
|
||||
/**
|
||||
* @brief String Term
|
||||
*/
|
||||
class YAPStringTerm: private YAPTerm {
|
||||
class YAPStringTerm: public YAPTerm {
|
||||
public:
|
||||
YAPStringTerm(char *s) ;
|
||||
YAPStringTerm(char *s, size_t len);
|
||||
@ -245,7 +266,7 @@ public:
|
||||
/**
|
||||
* @brief Atom Term
|
||||
*/
|
||||
class YAPAtomTerm: private YAPTerm {
|
||||
class YAPAtomTerm: public YAPTerm {
|
||||
public:
|
||||
YAPAtomTerm(YAPAtom a): YAPTerm() { mk( MkAtomTerm(a.a) ); }
|
||||
YAPAtomTerm(Atom a): YAPTerm() { mk( MkAtomTerm(a) ); }
|
||||
@ -257,86 +278,105 @@ public:
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Functor
|
||||
* @brief YAPFunctor represents Prolog functors Name/Arity
|
||||
*/
|
||||
class YAPFunctor {
|
||||
friend class YAPApplTerm;
|
||||
friend class YAPPredicate;
|
||||
Functor f;
|
||||
public:
|
||||
YAPFunctor( char * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupAtom( s ), arity ); }
|
||||
YAPFunctor( wchar_t * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupWideAtom( s ), arity ); }
|
||||
YAPFunctor( YAPAtom at, arity_t arity) { f = Yap_MkFunctor( at.a, arity ); }
|
||||
/// Constructor: receives Prolog functor and casts it to YAPFunctor
|
||||
///
|
||||
/// Notice that this is designed for internal use only.
|
||||
YAPFunctor( Functor ff) { f = ff; }
|
||||
public:
|
||||
/// Constructor: receives name as a string plus arity
|
||||
///
|
||||
/// Notice that this is designed for ISO-LATIN-1 right now
|
||||
YAPFunctor( char * s, arity_t arity) { 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
|
||||
YAPFunctor( wchar_t * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupWideAtom( s ), arity ); }
|
||||
/// Constructor: receives name as an atom, plus arity
|
||||
///
|
||||
/// This is the default method, and the most popi;at
|
||||
YAPFunctor( YAPAtom at, arity_t arity) { f = Yap_MkFunctor( at.a, arity ); }
|
||||
|
||||
Atom name(void) {
|
||||
return NameOfFunctor( f );
|
||||
/// 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.
|
||||
arity_t arity(void) {
|
||||
return ArityOfFunctor( f );
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Term Handle
|
||||
*/
|
||||
class YAPTermHandle {
|
||||
long int handle;
|
||||
public:
|
||||
YAPTermHandle(Term t) {
|
||||
CACHE_REGS
|
||||
handle = Yap_InitSlot(t PASS_REGS);
|
||||
}
|
||||
|
||||
void set(YAPTerm t) {
|
||||
CACHE_REGS
|
||||
Yap_PutInSlot(handle, t.t PASS_REGS);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Predicate
|
||||
* @brief Predicates
|
||||
*
|
||||
* This class interfaces with PredEntry in Yatom.g
|
||||
*/
|
||||
class YAPPredicate {
|
||||
friend class YAPQuery;
|
||||
|
||||
private:
|
||||
PredEntry *ap;
|
||||
// trick to communicate t[] back to yapquery
|
||||
YAPPredicate(const char *s, Term **outp, term_t& vnames );
|
||||
|
||||
/// auxiliary routine to find a predicate in the current module.
|
||||
PredEntry *getPred( Term t, Term **outp ) ;
|
||||
|
||||
/// String constructor for predicates
|
||||
///
|
||||
/// It also communicates the array of arguments t[] abd the array of variables
|
||||
/// back to yapquery
|
||||
YAPPredicate(const char *s, Term **outp, handle_t& vnames );
|
||||
|
||||
/// Term constructor for predicates
|
||||
///
|
||||
/// It is just a call to getPred
|
||||
inline YAPPredicate(Term t) {
|
||||
CACHE_REGS
|
||||
Term m = CurrentModule ;
|
||||
t = Yap_StripModule(t, &m);
|
||||
if (IsVarTerm(t) || IsNumTerm(t))
|
||||
ap = NULL;
|
||||
if (IsAtomTerm(t)) {
|
||||
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
||||
} else if (IsApplTerm(t)) {
|
||||
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
|
||||
} else if (IsPairTerm(t)) {
|
||||
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE));
|
||||
} else {
|
||||
ap = NULL;
|
||||
}
|
||||
ap = getPred( t , NULL );
|
||||
}
|
||||
|
||||
/// Cast constructor for predicates,
|
||||
/// if we have the implementation data.
|
||||
///
|
||||
inline YAPPredicate(PredEntry *pe) {
|
||||
ap = pe;
|
||||
}
|
||||
|
||||
public:
|
||||
inline YAPPredicate(YAPFunctor f) {
|
||||
CACHE_REGS
|
||||
ap = RepPredProp(PredPropByFunc(f.f,CurrentModule));
|
||||
}
|
||||
|
||||
/// Functor constructor for predicates
|
||||
///
|
||||
/// Asssumes that we use the current module.
|
||||
YAPPredicate(YAPFunctor f);
|
||||
|
||||
/// Functor constructor for predicates, is given a specific module.
|
||||
///
|
||||
inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
|
||||
ap = RepPredProp(PredPropByFunc(f.f,mod.t));
|
||||
}
|
||||
|
||||
/// Name/arity constructor for predicates.
|
||||
///
|
||||
inline YAPPredicate(YAPAtom at, YAPTerm mod) {
|
||||
ap = RepPredProp(PredPropByAtom(at.a,mod.t));
|
||||
}
|
||||
inline YAPPredicate(YAPAtom at) {
|
||||
CACHE_REGS
|
||||
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
|
||||
}
|
||||
|
||||
|
||||
/// Name/0 constructor for predicates.
|
||||
///
|
||||
YAPPredicate(YAPAtom at);
|
||||
|
||||
/// Mod:Name/Arity constructor for predicates.
|
||||
///
|
||||
inline YAPPredicate(YAPAtom at, arity_t arity, YAPTerm mod) {
|
||||
if (arity) {
|
||||
Functor f = Yap_MkFunctor(at.a, arity);
|
||||
@ -345,55 +385,45 @@ public:
|
||||
ap = RepPredProp(PredPropByAtom(at.a,mod.t));
|
||||
}
|
||||
}
|
||||
inline YAPPredicate(YAPAtom at, arity_t arity) {
|
||||
CACHE_REGS
|
||||
if (arity) {
|
||||
Functor f = Yap_MkFunctor(at.a, arity);
|
||||
ap = RepPredProp(PredPropByFunc(f,CurrentModule));
|
||||
} else {
|
||||
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
|
||||
}
|
||||
}
|
||||
|
||||
/// Atom/Arity constructor for predicates.
|
||||
///
|
||||
YAPPredicate(YAPAtom at, arity_t arity);
|
||||
|
||||
|
||||
/// String constructor for predicates.
|
||||
///
|
||||
/// String is a Prolog term, we extract the main functor after considering the module qualifiers.
|
||||
inline YAPPredicate(char *s) {
|
||||
CACHE_REGS
|
||||
Term t, tp, m = CurrentModule ;
|
||||
Term t, tp;
|
||||
t = YAP_ReadBuffer(s,&tp);
|
||||
t = Yap_StripModule(t, &m);
|
||||
if (IsVarTerm(t) || IsNumTerm(t))
|
||||
ap = NULL;
|
||||
if (IsAtomTerm(t)) {
|
||||
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
||||
} else {
|
||||
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
|
||||
}
|
||||
ap = getPred( t, NULL );
|
||||
}
|
||||
|
||||
|
||||
/// String constructor for predicates, also keeps arguments in tp[]
|
||||
///
|
||||
/// String is a Prolog term, we extract the main functor after considering the module qualifiers.
|
||||
inline YAPPredicate(char *s, Term **outp) {
|
||||
CACHE_REGS
|
||||
Term t, tp, m = CurrentModule ;
|
||||
Term t, tp;
|
||||
t = YAP_ReadBuffer(s,&tp);
|
||||
t = Yap_StripModule(t, &m);
|
||||
if (IsVarTerm(t) || IsNumTerm(t))
|
||||
ap = NULL;
|
||||
if (IsAtomTerm(t)) {
|
||||
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
||||
*outp = NULL;
|
||||
} else if (IsApplTerm(t)) {
|
||||
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
|
||||
*outp = RepAppl(t)+1;
|
||||
} else if (IsPairTerm(t)) {
|
||||
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
|
||||
*outp = RepPair(t);
|
||||
}
|
||||
ap = getPred( t, NULL );
|
||||
}
|
||||
|
||||
/// meta-call this predicate, with arguments ts[]
|
||||
///
|
||||
int call(YAPTerm ts[]);
|
||||
arity_t arity() { return ap->ArityOfPE; }
|
||||
/// module of predicate
|
||||
|
||||
/// module of a predicate
|
||||
///
|
||||
/// notice that modules are currently treated as atoms, this should change.
|
||||
YAPAtom module() { if (ap->ModuleOfPred == PROLOG_MODULE)
|
||||
YAPAtom module() {
|
||||
if (ap->ModuleOfPred == PROLOG_MODULE)
|
||||
return YAPAtom(AtomProlog);
|
||||
else
|
||||
return YAPAtom(AtomOfTerm(ap->ModuleOfPred)); }
|
||||
return YAPAtom(AtomOfTerm(ap->ModuleOfPred));
|
||||
}
|
||||
|
||||
/// name of predicate
|
||||
///
|
||||
/// notice that we return the atom, not a string.
|
||||
@ -402,6 +432,12 @@ public:
|
||||
else
|
||||
return YAPAtom(NameOfFunctor(ap->FunctorOfPred));
|
||||
}
|
||||
|
||||
/// arity of predicate
|
||||
///
|
||||
/// we return a positive number.
|
||||
arity_t getArity() { return ap->ArityOfPE; }
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
@ -410,7 +446,7 @@ public:
|
||||
* interface to a YAP Query;
|
||||
* uses an SWI-like status info internally.
|
||||
*/
|
||||
class YAPQuery: private YAPPredicate {
|
||||
class YAPQuery: public YAPPredicate {
|
||||
int q_open;
|
||||
int q_state;
|
||||
Term *q_g;
|
||||
@ -419,7 +455,7 @@ class YAPQuery: private YAPPredicate {
|
||||
int q_flags;
|
||||
YAP_dogoalinfo q_h;
|
||||
YAPQuery *oq;
|
||||
term_t vnames;
|
||||
handle_t vnames;
|
||||
void initQuery( Term ts[] );
|
||||
void initQuery( YAPTerm t[], arity_t arity );
|
||||
public:
|
||||
@ -451,88 +487,59 @@ public:
|
||||
/// first query
|
||||
///
|
||||
/// actually implemented by calling the next();
|
||||
inline int first() { return next(); }
|
||||
inline bool first() { return next(); }
|
||||
/// ask for the next solution of the current query
|
||||
/// same call for every solution
|
||||
int next();
|
||||
bool next();
|
||||
/// 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.
|
||||
YAPListTerm namedVars() { CACHE_REGS Term o = Yap_GetFromSlot( vnames PASS_REGS ); return YAPListTerm( o ); }
|
||||
YAPListTerm namedVars();
|
||||
};
|
||||
|
||||
// Java support
|
||||
|
||||
class YAPParams;
|
||||
// This class implements a callback Prolog-side
|
||||
class YAPCallback {
|
||||
public:
|
||||
virtual ~YAPCallback() { printf("~YAPCallback\n"); }
|
||||
virtual void run() { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "callback"); }
|
||||
virtual void displayInWindow(char *s) { }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief YAP Constructor
|
||||
* @brief YAP Engine: takes care of constructing an execution environment where we can go executing goals
|
||||
*
|
||||
*/
|
||||
class YAPEngine {
|
||||
public:
|
||||
YAPEngine(YAPParams const& params); /// construct a new engine
|
||||
YAPEngine(); /// construct a new engine, no arguments
|
||||
YAPQuery *query( char *s ) { return new YAPQuery( s ); } /// build a query on the engine
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Parameters for YAP Constructor
|
||||
*
|
||||
*/
|
||||
class YAPParams {
|
||||
friend class YAPEngine;
|
||||
private:
|
||||
YAPCallback *_callback;
|
||||
char **buf;
|
||||
YAP_init_args init_args;
|
||||
public:
|
||||
YAPParams();
|
||||
// sets all the default values for each data member
|
||||
YAPParams& savedState( char * f);
|
||||
YAPParams& stackSize(size_t sz);
|
||||
YAPParams& trailSize(size_t sz);
|
||||
YAPParams& maxStackSize(size_t sz);
|
||||
YAPParams& maxTrailSize(size_t sz);
|
||||
YAPParams& libDir(char *p);
|
||||
YAPParams& bootFile(char *f);
|
||||
YAPParams& goal(char *g);
|
||||
YAPParams& topLevel(char *g);
|
||||
YAPParams& script(bool v);
|
||||
YAPParams& fastBoot(bool v);
|
||||
YAPEngine(char *savedState = NULL,
|
||||
size_t stackSize = 0,
|
||||
size_t trailSize = 0,
|
||||
size_t maxStackSize = 0,
|
||||
size_t maxTrailSize = 0,
|
||||
char *libDir = NULL,
|
||||
char *bootFile = NULL,
|
||||
char *goal = NULL,
|
||||
char *topLevel = NULL,
|
||||
bool script = FALSE,
|
||||
bool fastBoot = FALSE,
|
||||
YAPCallback *callback=NULL); /// construct a new engine, including aaccess to callbacks
|
||||
~YAPEngine() { delYAPCallback(); } /// kill engine
|
||||
void delYAPCallback() { _callback = 0; } /// remove current callback
|
||||
void setYAPCallback(YAPCallback *cb) { delYAPCallback(); _callback = cb; __android_log_print(ANDROID_LOG_INFO, __FILE__, "after loading startup %p",cb); } /// set a new callback
|
||||
void call() { if (_callback) _callback->run(); } /// execute the callback.
|
||||
void display( char *s) { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "bef calling disp %s %p",s, _callback); if (_callback) _callback->displayInWindow(s); } /// execute the callback.
|
||||
YAPQuery *query( char *s ); /// build a query on the engine
|
||||
};
|
||||
|
||||
inline YAPParams::YAPParams()
|
||||
{ Yap_InitDefaults( &init_args, NULL ); }
|
||||
|
||||
inline YAPParams& YAPParams::savedState( char * f)
|
||||
{ init_args.SavedState = f; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::stackSize(size_t sz)
|
||||
{ init_args.StackSize = sz; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::trailSize(size_t sz)
|
||||
{ init_args.TrailSize = sz; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::maxStackSize(size_t sz)
|
||||
{ init_args.MaxStackSize = sz; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::maxTrailSize(size_t sz)
|
||||
{ init_args.MaxTrailSize = sz; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::libDir(char *p)
|
||||
{ init_args.YapLibDir = p; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::bootFile(char *f)
|
||||
{ init_args.YapPrologBootFile = f; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::goal(char *g)
|
||||
{ init_args.YapPrologGoal = g; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::topLevel(char *g)
|
||||
{ init_args.YapPrologTopLevelGoal = g; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::script(bool v)
|
||||
{ init_args.HaltAfterConsult = v; return *this; }
|
||||
|
||||
inline YAPParams& YAPParams::fastBoot(bool v)
|
||||
{ init_args.FastBoot = v; return *this; }
|
||||
|
||||
/*
|
||||
* @}
|
||||
*
|
||||
*/
|
||||
|
Reference in New Issue
Block a user