This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/CXX/yapi.cpp

630 lines
14 KiB
C++
Raw Normal View History

2014-04-28 11:47:09 +01:00
#define YAP_CPP_INTERFACE 1
#include "yapi.hh"
2014-06-11 19:27:54 +01:00
#include "SWI-Stream.h"
2014-06-19 15:06:15 +01:00
YAPAtomTerm::YAPAtomTerm(char *s) { // build string
2014-06-11 19:27:54 +01:00
BACKUP_H();
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_ATOM;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2014-06-19 15:06:15 +01:00
YAPAtomTerm::YAPAtomTerm(char *s, size_t len) { // build string
2014-06-11 19:27:54 +01:00
BACKUP_H();
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2014-06-19 15:06:15 +01:00
YAPAtomTerm::YAPAtomTerm(wchar_t *s): YAPTerm() { // build string
2014-06-11 19:27:54 +01:00
BACKUP_H();
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_ATOM;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2014-06-19 15:06:15 +01:00
YAPAtomTerm::YAPAtomTerm(wchar_t *s, size_t len) : YAPTerm() { // build string
2014-06-11 19:27:54 +01:00
BACKUP_H();
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2014-06-19 15:06:15 +01:00
YAPStringTerm::YAPStringTerm(char *s) { // build string
2014-06-11 19:27:54 +01:00
BACKUP_H();
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_STRING;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2014-06-19 15:06:15 +01:00
YAPStringTerm::YAPStringTerm(char *s, size_t len) { // build string
2014-06-11 19:27:54 +01:00
BACKUP_H();
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2014-06-19 15:06:15 +01:00
YAPStringTerm::YAPStringTerm(wchar_t *s): YAPTerm() { // build string
2014-06-11 19:27:54 +01:00
BACKUP_H();
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_STRING;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
2014-06-19 15:06:15 +01:00
YAPStringTerm::YAPStringTerm(wchar_t *s, size_t len) : YAPTerm() { // build string
2014-06-11 19:27:54 +01:00
BACKUP_H();
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
2014-04-28 11:47:09 +01:00
}
YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) : YAPTerm() {
2014-06-11 19:27:54 +01:00
UInt arity = ArityOfFunctor(f.f);
mk ( Yap_MkApplTerm( f.f, arity, (Term *)ts) );
2014-04-28 11:47:09 +01:00
}
YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() {
2014-06-11 19:27:54 +01:00
UInt arity = ArityOfFunctor(f.f);
mk ( Yap_MkNewApplTerm( f.f, arity) );
2014-04-28 11:47:09 +01:00
}
YAPTerm YAPApplTerm::getArg(int arg) {
2014-06-11 19:27:54 +01:00
return YAPTerm( ArgOfTerm(arg, gt() ) );
2014-04-29 11:45:19 +01:00
}
YAPFunctor YAPApplTerm::getFunctor() {
2014-06-11 19:27:54 +01:00
return YAPFunctor( FunctorOfTerm( gt( )) );
2014-04-29 11:45:19 +01:00
}
2014-04-28 11:47:09 +01:00
YAPPairTerm::YAPPairTerm(YAPTerm th, YAPTerm tl) : YAPTerm() {
2014-06-11 19:27:54 +01:00
CACHE_REGS
mk ( MkPairTerm( th.term(), tl.term() ) );
2014-04-28 11:47:09 +01:00
}
YAPPairTerm::YAPPairTerm() : YAPTerm() {
2014-06-11 19:27:54 +01:00
t = Yap_MkNewPairTerm( );
2014-04-28 11:47:09 +01:00
}
2014-06-11 19:27:54 +01:00
void YAPTerm::mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 PASS_REGS); }
Term YAPTerm::gt() { CACHE_REGS return Yap_GetFromSlot( t PASS_REGS); }
2014-04-28 11:47:09 +01:00
YAP_tag_t YAPTerm::tag() {
Term tt = gt( );
2014-06-11 19:27:54 +01:00
if (IsVarTerm(tt)) {
CELL *pt = VarOfTerm(tt);
if (IsUnboundVar(pt)) {
CACHE_REGS
if (IsAttVar(pt))
return YAP_TAG_ATT;
return YAP_TAG_UNBOUND;
}
return YAP_TAG_REF;
}
if (IsPairTerm(tt))
return YAP_TAG_PAIR;
if (IsAtomOrIntTerm(tt)) {
if (IsAtomTerm(tt))
return YAP_TAG_ATOM;
return YAP_TAG_INT;
} else {
Functor f = FunctorOfTerm(tt);
if (IsExtensionFunctor(f)) {
if (f == FunctorDBRef) {
return YAP_TAG_DBREF;
}
if (f == FunctorLongInt) {
return YAP_TAG_LONG_INT;
}
if (f == FunctorBigInt) {
big_blob_type bt = (big_blob_type)RepAppl(tt)[1];
switch (bt) {
case BIG_INT:
return YAP_TAG_BIG_INT;
case BIG_RATIONAL:
return YAP_TAG_RATIONAL;
default:
return YAP_TAG_OPAQUE;
}
}
}
return YAP_TAG_APPL;
}
2014-04-28 11:47:09 +01:00
}
YAPTerm YAPTerm::deepCopy() {
2014-06-11 19:27:54 +01:00
Term tn;
BACKUP_MACHINE_REGS();
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
tn = Yap_CopyTerm( gt() );
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
RECOVER_MACHINE_REGS();
return new YAPTerm( tn );
2014-04-28 11:47:09 +01:00
}
bool YAPTerm::exactlyEqual(YAPTerm t1) {
2014-06-11 19:27:54 +01:00
int out;
BACKUP_MACHINE_REGS();
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
out = Yap_eq(gt(), t1.term());
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
RECOVER_MACHINE_REGS();
return out;
2014-04-28 11:47:09 +01:00
}
2014-05-04 22:31:22 +01:00
2014-04-28 11:47:09 +01:00
bool YAPTerm::unify(YAPTerm t1) {
2014-06-11 19:27:54 +01:00
int out;
BACKUP_MACHINE_REGS();
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
out = Yap_unify(gt(), t1.term());
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
RECOVER_MACHINE_REGS();
return out;
2014-04-28 11:47:09 +01:00
}
bool YAPTerm::unifiable(YAPTerm t1) {
2014-06-11 19:27:54 +01:00
int out;
BACKUP_MACHINE_REGS();
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
out = Yap_Unifiable(gt(), t1.term());
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
RECOVER_MACHINE_REGS();
return out;
2014-04-28 11:47:09 +01:00
}
bool YAPTerm::variant(YAPTerm t1) {
2014-06-11 19:27:54 +01:00
int out;
BACKUP_MACHINE_REGS();
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
out = Yap_Variant(gt(), t1.term());
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
RECOVER_MACHINE_REGS();
return out;
2014-04-28 11:47:09 +01:00
}
2014-06-22 17:35:05 +01:00
intptr_t YAPTerm::hashTerm(size_t sz, size_t depth, bool variant) {
intptr_t out;
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
BACKUP_MACHINE_REGS();
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
out = Yap_TermHash(gt(), sz, depth, variant) ;
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
RECOVER_MACHINE_REGS();
return out;
2014-04-28 11:47:09 +01:00
}
char *YAPTerm::text(void) {
2014-06-11 19:27:54 +01:00
size_t sze = 4096, length;
char *os;
int enc;
2014-06-11 19:27:54 +01:00
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "I LCL0+%p/t=(%d) %x", LCL0, t, LCL0[-15]) ; }
BACKUP_MACHINE_REGS();
2014-06-11 19:27:54 +01:00
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();
2014-06-12 01:25:50 +01:00
return (char *)NULL;
}
2014-06-11 19:27:54 +01:00
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "II LCL0+t=(%p) %x", LCL0, LCL0[-15]) ; }
RECOVER_MACHINE_REGS();
return os;
}
2014-06-11 19:27:54 +01:00
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()
{
BACKUP_MACHINE_REGS();
CACHE_REGS
YAPPairTerm lv = YAPPairTerm(Yap_TermVariables(gt(), 0 PASS_REGS));
RECOVER_MACHINE_REGS();
return lv;
}
2014-06-11 19:27:54 +01:00
*/
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( ) ); }
2014-04-28 11:47:09 +01:00
2014-06-19 15:06:15 +01:00
char *YAPAtom::getName(void) {
2014-06-11 19:27:54 +01:00
if (IsWideAtom(a)) {
// return an UTF-8 version
size_t sz = 512;
wchar_t * ptr = a->WStrOfAE;
int ch = -1;
char *s = new char[sz], *op = s;
while (ch) {
ch = *ptr++;
utf8_put_char( op, ch );
}
sz = strlen(s)+1;
char *os = new char[sz];
memcpy(os, s, sz);
delete[] s;
2014-06-11 19:27:54 +01:00
return os;
} else if (IsBlob(a)) {
PL_blob_t *type = RepBlobProp(a->PropsOfAE)->blob_t;
size_t sz = 512;
if (type->write) {
char *s = new char[sz];
IOSTREAM *stream = Sopenmem(&s, &sz, "w");
stream->encoding = ENC_UTF8;
atom_t at = YAP_SWIAtomFromAtom(AbsAtom(a));
type->write(stream, at, 0);
Sclose(stream);
popOutputContext();
sz = strlen(s)+1;
char *os = new char[sz];
memcpy(os, s, sz);
delete s;
return os;
} else {
char *s = new char[sz];
2014-04-28 11:47:09 +01:00
#if defined(__linux__) || defined(__APPLE__)
2014-06-11 19:27:54 +01:00
snprintf(s, sz, "'%s'(%p)", AtomSWIStream->StrOfAE, a);
2014-04-28 11:47:09 +01:00
#else
2014-06-11 19:27:54 +01:00
snprintf(s, sz, "'%s'(0x%p)", AtomSWIStream->StrOfAE, a);
2014-04-28 11:47:09 +01:00
#endif
2014-06-11 19:27:54 +01:00
char *os = new char[sz];
memcpy(os, s, sz);
delete[] s;
2014-06-11 19:27:54 +01:00
return os;
}
} else {
return a->StrOfAE;
}
2014-04-28 11:47:09 +01:00
}
YAPPredicate::YAPPredicate(const char *s, Term **outp, YAPTerm &vnames) throw (int) {
CACHE_REGS
vnames = Yap_NewSlots(1 PASS_REGS);
Term t = Yap_StringToTerm(s, strlen(s)+1, Yap_GetFromSlot( vnames.t PASS_REGS));
2014-06-22 17:35:05 +01:00
if (t == 0L)
2014-07-14 05:40:39 +01:00
throw YAPError::YAP_SYNTAX_ERROR;
2014-06-11 19:27:54 +01:00
ap = getPred( t, outp );
2014-06-22 17:35:05 +01:00
//{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "OUT vnames=%d ap=%p LCL0=%p", vnames, ap, LCL0) ; }
2014-06-11 19:27:54 +01:00
}
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);
2014-06-11 19:27:54 +01:00
if (IsVarTerm(t) || IsNumTerm(t)) {
2014-06-12 01:25:50 +01:00
ap = (PredEntry *)NULL;
if (outp) *outp = (Term *)NULL;
2014-06-11 19:27:54 +01:00
}
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
2014-06-12 01:25:50 +01:00
if (outp) *outp = (Term *)NULL;
2014-06-11 19:27:54 +01:00
} else if (IsPairTerm(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)) {
2014-06-12 01:25:50 +01:00
ap = (PredEntry *)NULL;
if (outp) *outp = (Term *)NULL;
2014-06-11 19:27:54 +01:00
}
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));
}
2014-06-11 19:27:54 +01:00
2014-04-29 11:45:19 +01:00
void
YAPQuery::initQuery( Term *ts )
2014-04-28 11:47:09 +01:00
{
2014-06-11 19:27:54 +01:00
CACHE_REGS
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
this->oq = (YAPQuery *)LOCAL_execution;
LOCAL_execution = (struct open_query_struct *)this;
this->q_open=1;
this->q_state=0;
2014-07-10 15:07:55 +01:00
this->q_flags = PL_Q_PASS_EXCEPTION;
2014-06-11 19:27:54 +01:00
this->q_g = ts;
}
void
YAPQuery::initQuery( YAPTerm t[], arity_t arity )
{
2014-06-11 19:27:54 +01:00
Term *ts = new Term[arity];
for (arity_t i = 0; i < arity; i++)
ts[i] = t[i].term();
2014-06-11 19:27:54 +01:00
return initQuery( ts );
2014-04-28 11:47:09 +01:00
}
2014-04-29 11:45:19 +01:00
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]): YAPPredicate(f, mod)
2014-04-28 11:47:09 +01:00
{
2014-06-11 19:27:54 +01:00
/* ignore flags for now */
initQuery( t , f.arity());
2014-04-29 11:45:19 +01:00
}
2014-04-28 11:47:09 +01:00
2014-04-29 11:45:19 +01:00
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm t[]): YAPPredicate(f)
{
2014-06-11 19:27:54 +01:00
/* ignore flags for now */
initQuery( t , f.arity());
2014-04-28 11:47:09 +01:00
}
YAPQuery::YAPQuery(YAPPredicate p, YAPTerm t[]): YAPPredicate(p.ap)
{
2014-06-11 19:27:54 +01:00
initQuery( t , p.ap->ArityOfPE);
2014-04-28 11:47:09 +01:00
}
2014-06-11 19:27:54 +01:00
YAPListTerm YAPQuery::namedVars() {
CACHE_REGS
Term o = Yap_GetFromSlot( this->vnames.t PASS_REGS );
2014-06-11 19:27:54 +01:00
return YAPListTerm( o );
}
bool YAPQuery::next()
2014-04-28 11:47:09 +01:00
{
2014-06-11 19:27:54 +01:00
CACHE_REGS
int result;
if (this->q_open != 1) return false;
if (setjmp(((YAPQuery *)LOCAL_execution)->q_env))
return false;
// don't forget, on success these guys must create slots
if (this->q_state == 0) {
2014-07-10 15:07:55 +01:00
// extern void toggle_low_level_trace(void);
2014-06-11 19:27:54 +01:00
//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 = (bool)YAP_RetryGoal(&this->q_h);
}
this->q_state = 1;
if (!result) {
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
}
return result;
2014-04-28 11:47:09 +01:00
}
void YAPQuery::cut()
{
2014-06-11 19:27:54 +01:00
CACHE_REGS
2014-04-28 11:47:09 +01:00
2014-06-11 19:27:54 +01:00
if (this->q_open != 1 || this->q_state == 0) return;
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
LOCAL_execution = (struct open_query_struct *)this->oq;
2014-04-28 11:47:09 +01:00
}
void YAPQuery::close()
{
2014-06-11 19:27:54 +01:00
CACHE_REGS
2014-05-04 22:31:22 +01:00
2014-06-11 19:27:54 +01:00
if (EX && !(this->q_flags & (PL_Q_CATCH_EXCEPTION))) {
2014-06-12 01:25:50 +01:00
EX = (struct DB_TERM *)NULL;
2014-06-11 19:27:54 +01:00
}
/* need to implement backtracking here */
if (this->q_open != 1 || this->q_state == 0) {
return;
}
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
LOCAL_execution = (struct open_query_struct *)this->oq;
2014-04-28 11:47:09 +01:00
}
int YAPPredicate::call(YAPTerm t[])
{
2014-06-11 19:27:54 +01:00
YAPQuery q = YAPQuery(*this, t);
2014-07-10 15:07:55 +01:00
int ret;
ret = q.next();
2014-06-11 19:27:54 +01:00
q.cut();
q.close();
return ret;
2014-04-28 11:47:09 +01:00
}
2014-04-29 11:45:19 +01:00
2014-06-11 19:27:54 +01:00
static YAPEngine *curren;
#if __ANDROID__
2014-04-29 11:45:19 +01:00
2014-06-11 19:27:54 +01:00
#include <jni.h>
#include <string.h>
2014-04-29 11:45:19 +01:00
2014-06-11 19:27:54 +01:00
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)
{
2014-06-11 19:27:54 +01:00
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';
2014-06-19 15:06:15 +01:00
curren->run(Yap_AndroidBufp);
2014-06-11 19:27:54 +01:00
Yap_AndroidSz = 0;
}
}
2014-06-11 19:27:54 +01:00
#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__
2014-06-22 17:35:05 +01:00
if (GLOBAL_assetManager == (AAssetManager *)NULL)
2014-06-11 19:27:54 +01:00
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;
}