jupyter
This commit is contained in:
parent
7325407d23
commit
725a0a692a
@ -1721,7 +1721,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
|
|||||||
CACHE_REGS
|
CACHE_REGS
|
||||||
PredEntry *pe = ape;
|
PredEntry *pe = ape;
|
||||||
bool out;
|
bool out;
|
||||||
fprintf(stderr,"EnterGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n",HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
// fprintf(stderr,"EnterGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n",HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
||||||
|
|
||||||
BACKUP_MACHINE_REGS();
|
BACKUP_MACHINE_REGS();
|
||||||
LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
|
LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
|
||||||
@ -1743,7 +1743,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
|
|||||||
//fprintf(stderr,"PrepGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n",
|
//fprintf(stderr,"PrepGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n",
|
||||||
// HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
// HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
||||||
out = Yap_exec_absmi(true, false);
|
out = Yap_exec_absmi(true, false);
|
||||||
fprintf(stderr,"EnterGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", out,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
// fprintf(stderr,"EnterGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", out,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
||||||
dgi->b = LCL0 - (CELL *)B;
|
dgi->b = LCL0 - (CELL *)B;
|
||||||
if (out) {
|
if (out) {
|
||||||
dgi->EndSlot = LOCAL_CurSlot;
|
dgi->EndSlot = LOCAL_CurSlot;
|
||||||
@ -1796,8 +1796,7 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
|
|||||||
choiceptr myB, handler;
|
choiceptr myB, handler;
|
||||||
bool backtrack = false;
|
bool backtrack = false;
|
||||||
|
|
||||||
fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%ld myB=%ld TR=%d P=%p CP=%p Slots=%d\n",
|
// fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%ld myB=%ld TR=%d P=%p CP=%p Slots=%d\n", successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
||||||
successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
|
||||||
BACKUP_MACHINE_REGS();
|
BACKUP_MACHINE_REGS();
|
||||||
myB = (choiceptr)(LCL0 - dgi->b0);
|
myB = (choiceptr)(LCL0 - dgi->b0);
|
||||||
handler = B;
|
handler = B;
|
||||||
@ -1829,8 +1828,7 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
|
|||||||
ASP=(CELL*)B;
|
ASP=(CELL*)B;
|
||||||
}
|
}
|
||||||
RECOVER_MACHINE_REGS();
|
RECOVER_MACHINE_REGS();
|
||||||
fprintf(stderr,"LeftGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n",
|
// fprintf(stderr,"LeftGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
||||||
successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
74
CXX/yapdb.hh
74
CXX/yapdb.hh
@ -90,7 +90,7 @@ protected:
|
|||||||
/// auxiliary routine to find a predicate in the current module.
|
/// auxiliary routine to find a predicate in the current module.
|
||||||
|
|
||||||
/// auxiliary routine to find a predicate in the current module.
|
/// auxiliary routine to find a predicate in the current module.
|
||||||
PredEntry *getPred(YAPTerm &t, CELL *& outp);
|
PredEntry *getPred(Term &t, CELL *& outp);
|
||||||
|
|
||||||
PredEntry *asPred() { return ap; };
|
PredEntry *asPred() { return ap; };
|
||||||
|
|
||||||
@ -99,47 +99,36 @@ protected:
|
|||||||
/// Just do nothing.
|
/// Just do nothing.
|
||||||
inline YAPPredicate() {
|
inline YAPPredicate() {
|
||||||
}
|
}
|
||||||
/// String constructor for predicates
|
|
||||||
///
|
|
||||||
/// It also communicates the array of arguments t[]
|
|
||||||
/// and the array of variables
|
|
||||||
/// back to yapquery
|
|
||||||
YAPPredicate(const char *s0, Term &tout, Term &tnames) {
|
|
||||||
CACHE_REGS
|
|
||||||
Term *modp = NULL;
|
|
||||||
const char *s = (const char *)s0;
|
|
||||||
tnames = MkVarTerm();
|
|
||||||
tout =
|
|
||||||
Yap_BufferToTermWithPrioBindings(s, TermNil, tnames, strlen(s0), 1200);
|
|
||||||
// fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
|
|
||||||
// Yap_DebugPlWrite(out);
|
|
||||||
if (tout == 0L) {
|
|
||||||
Yap_ThrowError(TYPE_ERROR_PREDICATE_INDICATOR, MkStringTerm(s0), "YAPPredicate");
|
|
||||||
}
|
|
||||||
YAPTerm tt = YAPTerm(tout);
|
|
||||||
ap = getPred(tt, modp);
|
|
||||||
tout = Yap_SaveTerm(tout);
|
|
||||||
tnames = Yap_SaveTerm(tnames);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
YAPPredicate(Term &to, Term &tmod, CELL * &ts, const char *pname);
|
YAPPredicate(Term &to, Term &tmod, CELL * &ts, const char *pname);
|
||||||
|
|
||||||
/// Term constructor for predicates
|
/// Term constructor for predicates
|
||||||
///
|
///
|
||||||
/// It is just a call to getPred
|
/// It is just a call to getPred
|
||||||
inline YAPPredicate(Term t) {
|
inline YAPPredicate(Term t, CELL *&v) {
|
||||||
CELL *v = nullptr;
|
if (t) {
|
||||||
YAPTerm tt = YAPTerm(t);
|
ap = getPred(t, v);
|
||||||
ap = getPred(tt, v);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline YAPPredicate(Term t) {
|
||||||
|
if (t) {
|
||||||
|
CELL *v = nullptr;
|
||||||
|
ap = getPred(t, v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Term constructor for predicates
|
/// Term constructor for predicates
|
||||||
///
|
///
|
||||||
/// It is just a call to getPred
|
/// It is just a call to getPred
|
||||||
inline YAPPredicate(YAPTerm t) {
|
inline YAPPredicate(YAPTerm t, CELL *&v) {
|
||||||
CELL *v = nullptr;
|
Term tp = t.term();
|
||||||
ap = getPred(t, v);
|
ap = getPred(tp, v);
|
||||||
|
}
|
||||||
|
inline YAPPredicate(YAPTerm t) {
|
||||||
|
CELL *v = nullptr;
|
||||||
|
Term tp = t.term();
|
||||||
|
ap = getPred(tp, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cast constructor for predicates,
|
/// Cast constructor for predicates,
|
||||||
@ -156,6 +145,28 @@ protected:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
/// String constructor for predicates
|
||||||
|
///
|
||||||
|
/// It also communicates the array of arguments t[]
|
||||||
|
/// and the array of variables
|
||||||
|
/// back to yapquery
|
||||||
|
YAPPredicate(const char *s0, Term &tout, Term &tnames, CELL *&nts) {
|
||||||
|
CACHE_REGS
|
||||||
|
const char *s = (const char *)s0;
|
||||||
|
tnames = MkVarTerm();
|
||||||
|
tout =
|
||||||
|
Yap_BufferToTermWithPrioBindings(s, TermNil, tnames, strlen(s0), 1200);
|
||||||
|
// fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
|
||||||
|
// Yap_DebugPlWrite(out);
|
||||||
|
if (tout == 0L) {
|
||||||
|
throw YAPError();
|
||||||
|
}
|
||||||
|
ap = getPred(tout, nts);
|
||||||
|
tout = Yap_SaveTerm(tout);
|
||||||
|
tnames = Yap_SaveTerm(tnames);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Functor constructor for predicates
|
/// Functor constructor for predicates
|
||||||
///
|
///
|
||||||
/// Asssumes that we use the current module.
|
/// Asssumes that we use the current module.
|
||||||
@ -180,6 +191,7 @@ public:
|
|||||||
///
|
///
|
||||||
YAPPredicate(YAPAtom at);
|
YAPPredicate(YAPAtom at);
|
||||||
|
|
||||||
|
|
||||||
/// Mod:Name/Arity constructor for predicates.
|
/// Mod:Name/Arity constructor for predicates.
|
||||||
///
|
///
|
||||||
inline YAPPredicate(YAPAtom at, uintptr_t arity, YAPModule mod) {
|
inline YAPPredicate(YAPAtom at, uintptr_t arity, YAPModule mod) {
|
||||||
|
57
CXX/yapi.cpp
57
CXX/yapi.cpp
@ -516,7 +516,7 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
|
|||||||
// PyThreadState *_save;
|
// PyThreadState *_save;
|
||||||
|
|
||||||
|
|
||||||
std::cerr << "mgoal " << YAPTerm(t).text() << "\n";
|
//std::cerr << "mgoal " << YAPTerm(t).text() << "\n";
|
||||||
// _save = PyEval_SaveThread();
|
// _save = PyEval_SaveThread();
|
||||||
#endif
|
#endif
|
||||||
CACHE_REGS
|
CACHE_REGS
|
||||||
@ -632,7 +632,6 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
|
|||||||
|
|
||||||
/* ignore flags for now */
|
/* ignore flags for now */
|
||||||
BACKUP_MACHINE_REGS();
|
BACKUP_MACHINE_REGS();
|
||||||
Term *nts;
|
|
||||||
Term goal;
|
Term goal;
|
||||||
|
|
||||||
if (ts) {
|
if (ts) {
|
||||||
@ -654,7 +653,6 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
|
|||||||
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) {
|
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) {
|
||||||
/* ignore flags for now */
|
/* ignore flags for now */
|
||||||
BACKUP_MACHINE_REGS();
|
BACKUP_MACHINE_REGS();
|
||||||
CELL *nts;
|
|
||||||
if (ts) {
|
if (ts) {
|
||||||
goal = YAPApplTerm(f, nts);
|
goal = YAPApplTerm(f, nts);
|
||||||
} else {
|
} else {
|
||||||
@ -667,30 +665,6 @@ goal = YAPApplTerm(f, nts);
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
YAPQuery::YAPQuery(YAPTerm t) : YAPPredicate(t) {
|
|
||||||
BACKUP_MACHINE_REGS();
|
|
||||||
CELL *nts;
|
|
||||||
Term tt = t.term();
|
|
||||||
if (IsPairTerm(tt)) {
|
|
||||||
nts = RepPair(tt);
|
|
||||||
tt = Yap_MkApplTerm(FunctorCsult, 1, nts);
|
|
||||||
}
|
|
||||||
if (IsApplTerm(tt)) {
|
|
||||||
Functor f = FunctorOfTerm(tt);
|
|
||||||
if (!IsExtensionFunctor(f)) {
|
|
||||||
nts = nullptr;
|
|
||||||
arity_t arity = ArityOfFunctor(f);
|
|
||||||
if (arity) {
|
|
||||||
nts = RepAppl(tt) + 1;
|
|
||||||
for (arity_t i = 0; i < arity; i++)
|
|
||||||
XREGS[i + 1] = nts[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
goal =Yap_SaveTerm( tt);
|
|
||||||
openQuery();
|
|
||||||
names = TermNil ;
|
|
||||||
}
|
|
||||||
|
|
||||||
YAPQuery::YAPQuery(YAPPredicate p, YAPTerm ts[]) : YAPPredicate(p.ap) {
|
YAPQuery::YAPQuery(YAPPredicate p, YAPTerm ts[]) : YAPPredicate(p.ap) {
|
||||||
BACKUP_MACHINE_REGS();
|
BACKUP_MACHINE_REGS();
|
||||||
@ -715,7 +689,7 @@ RECOVER_MACHINE_REGS();
|
|||||||
bool YAPQuery::next() {
|
bool YAPQuery::next() {
|
||||||
CACHE_REGS
|
CACHE_REGS
|
||||||
bool result = false;
|
bool result = false;
|
||||||
std::cerr << "next " << YAPTerm(goal).text() << "\n";
|
//std::cerr << "next " << YAPTerm(goal).text() << "\n";
|
||||||
|
|
||||||
sigjmp_buf buf, *oldp = LOCAL_RestartEnv;
|
sigjmp_buf buf, *oldp = LOCAL_RestartEnv;
|
||||||
e = nullptr;
|
e = nullptr;
|
||||||
@ -909,38 +883,39 @@ YAPPredicate::YAPPredicate(YAPAtom at, uintptr_t arity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// auxiliary routine to find a predicate in the current module.
|
/// auxiliary routine to find a predicate in the current module.
|
||||||
PredEntry *YAPPredicate::getPred(YAPTerm &tt, CELL *&outp) {
|
PredEntry *YAPPredicate::getPred(Term &t, CELL *&out) {
|
||||||
CACHE_REGS
|
CACHE_REGS
|
||||||
Term m = Yap_CurrentModule(), t = tt.term();
|
Term m = Yap_CurrentModule();
|
||||||
t = Yap_StripModule(t, &m);
|
t = Yap_StripModule(t, &m);
|
||||||
|
|
||||||
if (IsVarTerm(t) || IsNumTerm(t)) {
|
if (IsVarTerm(t) || IsNumTerm(t)) {
|
||||||
if (IsVarTerm(t))
|
if (IsVarTerm(t))
|
||||||
throw YAPError(SOURCE(), INSTANTIATION_ERROR, tt.term(), 0);
|
throw YAPError(SOURCE(), INSTANTIATION_ERROR, t, 0);
|
||||||
else if (IsNumTerm(t))
|
else if (IsNumTerm(t))
|
||||||
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, tt.term(), 0);
|
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
|
||||||
}
|
}
|
||||||
tt.put(t);
|
|
||||||
if (IsAtomTerm(t)) {
|
if (IsAtomTerm(t)) {
|
||||||
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
||||||
outp = (Term *)NULL;
|
|
||||||
return ap;
|
return ap;
|
||||||
} else if (IsPairTerm(t)) {
|
} else if (IsPairTerm(t)) {
|
||||||
Term ts[2];
|
Term ts[2], *s = ( out ? out : ts );
|
||||||
Functor FunctorConsult = Yap_MkFunctor(Yap_LookupAtom("consult"), 1);
|
Functor FunctorConsult = Yap_MkFunctor(Yap_LookupAtom("consult"), 1);
|
||||||
ts[1] = t;
|
s[1] = t;
|
||||||
ts[0] = m;
|
s[0] = m;
|
||||||
t = Yap_MkApplTerm(FunctorModule, 2, ts);
|
t = Yap_MkApplTerm(FunctorModule, 2, s);
|
||||||
t = Yap_MkApplTerm(FunctorConsult, 1, &t);
|
t = Yap_MkApplTerm(FunctorConsult, 1, &t);
|
||||||
tt.put(t);
|
if (!out)
|
||||||
outp = RepAppl(t) + 1;
|
out = RepAppl(t) + 1;
|
||||||
}
|
}
|
||||||
Functor f = FunctorOfTerm(t);
|
Functor f = FunctorOfTerm(t);
|
||||||
if (IsExtensionFunctor(f)) {
|
if (IsExtensionFunctor(f)) {
|
||||||
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
|
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
|
||||||
} else {
|
} else {
|
||||||
ap = RepPredProp(PredPropByFunc(f, m));
|
ap = RepPredProp(PredPropByFunc(f, m));
|
||||||
outp = RepAppl(t) + 1;
|
if (out)
|
||||||
|
memcpy( out, RepAppl(t) + 1, ap->ArityOfPE*sizeof(CELL) );
|
||||||
|
else
|
||||||
|
out = RepAppl(t) + 1;
|
||||||
}
|
}
|
||||||
return ap;
|
return ap;
|
||||||
}
|
}
|
||||||
|
37
CXX/yapq.hh
37
CXX/yapq.hh
@ -44,8 +44,8 @@ class X_API YAPQuery : public YAPPredicate {
|
|||||||
YAP_dogoalinfo q_h;
|
YAP_dogoalinfo q_h;
|
||||||
Term names;
|
Term names;
|
||||||
Term goal;
|
Term goal;
|
||||||
|
CELL *nts;
|
||||||
// temporaries
|
// temporaries
|
||||||
int lvl;
|
|
||||||
YAPError *e;
|
YAPError *e;
|
||||||
|
|
||||||
inline void setNext() { // oq = LOCAL_execution;
|
inline void setNext() { // oq = LOCAL_execution;
|
||||||
@ -69,6 +69,7 @@ public:
|
|||||||
goal = TermTrue;
|
goal = TermTrue;
|
||||||
openQuery();
|
openQuery();
|
||||||
};
|
};
|
||||||
|
inline ~YAPQuery() { close(); }
|
||||||
/// main constructor, uses a predicate and an array of terms
|
/// 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
|
/// It is given a YAPPredicate _p_ , and an array of terms that must have at
|
||||||
@ -92,32 +93,10 @@ public:
|
|||||||
/// It is given a string, calls the parser and obtains a Prolog term that
|
/// It is given a string, calls the parser and obtains a Prolog term that
|
||||||
/// should be a callable
|
/// should be a callable
|
||||||
/// goal.
|
/// goal.
|
||||||
inline YAPQuery(const char *s) : YAPPredicate(s, goal, names) {
|
inline YAPQuery(const char *s) : YAPPredicate(s, goal, names, (nts = &ARG1)) {
|
||||||
CELL *qt = nullptr;
|
|
||||||
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %d",
|
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %d",
|
||||||
LOCAL_CurSlot);
|
LOCAL_CurSlot);
|
||||||
if (!ap)
|
|
||||||
return;
|
|
||||||
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "%s", names.text());
|
|
||||||
if (IsPairTerm(goal)) {
|
|
||||||
qt = RepPair(goal);
|
|
||||||
goal = Yap_SaveTerm(Yap_MkApplTerm(FunctorCsult, 1, qt));
|
|
||||||
ap = RepPredProp(PredPropByFunc(FunctorCsult, TermProlog));
|
|
||||||
} else {
|
|
||||||
goal = Yap_SaveTerm(goal);
|
|
||||||
}
|
|
||||||
if (IsApplTerm(goal)) {
|
|
||||||
Functor f = FunctorOfTerm(goal);
|
|
||||||
if (!IsExtensionFunctor(f)) {
|
|
||||||
arity_t arity = ap->ArityOfPE;
|
|
||||||
if (arity) {
|
|
||||||
qt = RepAppl(goal) + 1;
|
|
||||||
for (arity_t i = 0; i < arity; i++)
|
|
||||||
XREGS[i + 1] = qt[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
names = Yap_SaveTerm(names);
|
|
||||||
openQuery();
|
openQuery();
|
||||||
};
|
};
|
||||||
// inline YAPQuery() : YAPPredicate(s, tgoal, tnames)
|
// inline YAPQuery() : YAPPredicate(s, tgoal, tnames)
|
||||||
@ -134,7 +113,13 @@ public:
|
|||||||
///
|
///
|
||||||
/// It i;
|
/// It i;
|
||||||
///};
|
///};
|
||||||
YAPQuery(YAPTerm t);
|
/// build a query from a term
|
||||||
|
YAPQuery(YAPTerm t) : YAPPredicate((goal = t.term()),(nts=Yap_XREGS+1)) {
|
||||||
|
BACKUP_MACHINE_REGS();
|
||||||
|
openQuery();
|
||||||
|
names = TermNil ;
|
||||||
|
RECOVER_MACHINE_REGS();
|
||||||
|
}
|
||||||
/// set flags for query execution, currently only for exception handling
|
/// set flags for query execution, currently only for exception handling
|
||||||
void setFlag(int flag) { q_flags |= flag; }
|
void setFlag(int flag) { q_flags |= flag; }
|
||||||
/// reset flags for query execution, currently only for exception handling
|
/// reset flags for query execution, currently only for exception handling
|
||||||
|
@ -13,15 +13,15 @@ static int py_putc(int sno, int ch) {
|
|||||||
// PyObject *pyw_data;
|
// PyObject *pyw_data;
|
||||||
StreamDesc *st = YAP_GetStreamFromId(sno);
|
StreamDesc *st = YAP_GetStreamFromId(sno);
|
||||||
if (st->user_name == TermOutStream) {
|
if (st->user_name == TermOutStream) {
|
||||||
// term_t tg = python_acquire_GIL();
|
term_t tg = python_acquire_GIL();
|
||||||
PySys_WriteStdout("%C", ch);
|
PySys_WriteStdout("%C", ch);
|
||||||
//python_release_GIL(tg);
|
python_release_GIL(tg);
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
if (st->user_name == TermErrStream) {
|
if (st->user_name == TermErrStream) {
|
||||||
//term_t tg = python_acquire_GIL();
|
term_t tg = python_acquire_GIL();
|
||||||
PySys_WriteStderr("%C", ch);
|
PySys_WriteStderr("%C", ch);
|
||||||
//python_release_GIL(tg);
|
python_release_GIL(tg);
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
char s[2];
|
char s[2];
|
||||||
@ -62,8 +62,8 @@ static void *py_open(VFS_t *me, const char *name, const char *io_mode,
|
|||||||
st->user_name = TermOutStream;
|
st->user_name = TermOutStream;
|
||||||
} else if (strcmp(name, "sys.stderr") == 0) {
|
} else if (strcmp(name, "sys.stderr") == 0) {
|
||||||
st->user_name = TermErrStream;
|
st->user_name = TermErrStream;
|
||||||
} else if (strcmp(name, "input") == 0) {
|
// } else if (strcmp(name, "input") == 0) {
|
||||||
pystream = PyObject_Call(pystream, PyTuple_New(0), NULL);
|
//pystream = PyObject_Call(pystream, PyTuple_New(0), NULL);
|
||||||
} else {
|
} else {
|
||||||
st->user_name = YAP_MkAtomTerm(st->name);
|
st->user_name = YAP_MkAtomTerm(st->name);
|
||||||
}
|
}
|
||||||
@ -90,16 +90,16 @@ static bool getLine(int inp) {
|
|||||||
char *myrl_line = NULL;
|
char *myrl_line = NULL;
|
||||||
StreamDesc *rl_instream = YAP_RepStreamFromId(inp);
|
StreamDesc *rl_instream = YAP_RepStreamFromId(inp);
|
||||||
term_t ctk = python_acquire_GIL();
|
term_t ctk = python_acquire_GIL();
|
||||||
fprintf(stderr, "in");
|
Py_ssize_t size;
|
||||||
PyObject *prompt = PyUnicode_FromString("?- "),
|
PyObject *prompt = PyUnicode_FromString("?- "),
|
||||||
*msg = PyUnicode_FromString(" **input** ");
|
*msg = PyUnicode_FromString(" **input** ");
|
||||||
/* window of vulnerability opened */
|
/* window of vulnerability opened */
|
||||||
myrl_line = PyUnicode_AsUTF8(PyObject_CallFunctionObjArgs(
|
myrl_line = PyUnicode_AsUTF8AndSize(PyObject_CallFunctionObjArgs(
|
||||||
rl_instream->u.private_data, msg, prompt, NULL));
|
rl_instream->u.private_data, msg, prompt, NULL), &size);
|
||||||
python_release_GIL(ctk);
|
python_release_GIL(ctk);
|
||||||
rl_instream->u.irl.ptr = rl_instream->u.irl.buf =
|
rl_instream->u.irl.ptr = rl_instream->u.irl.buf =
|
||||||
(const unsigned char *)myrl_line;
|
(const unsigned char *)malloc(size);
|
||||||
myrl_line = NULL;
|
memcpy((void *)rl_instream->u.irl.buf, myrl_line, size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +73,6 @@ python_query( Caller, String ) :-
|
|||||||
query_to_answer( Goal, VarNames, Status, Bindings),
|
query_to_answer( Goal, VarNames, Status, Bindings),
|
||||||
atom_to_string( Status, SStatus ),
|
atom_to_string( Status, SStatus ),
|
||||||
Caller.port := SStatus,
|
Caller.port := SStatus,
|
||||||
start_low_level_trace,
|
|
||||||
write_query_answer( Bindings ),
|
write_query_answer( Bindings ),
|
||||||
nl(user_error),
|
nl(user_error),
|
||||||
Caller.answer := {},
|
Caller.answer := {},
|
||||||
@ -88,4 +87,4 @@ in_dict(Dict, nonvar([V0|Vs],G)) :- !,
|
|||||||
Dict[V0] := G,
|
Dict[V0] := G,
|
||||||
in_dict( Dict, nonvar(Vs, G) ).
|
in_dict( Dict, nonvar(Vs, G) ).
|
||||||
in_dict(_Dict, nonvar([],_G)) :- !.
|
in_dict(_Dict, nonvar([],_G)) :- !.
|
||||||
in_dict(_, _).
|
in_dict(_, _)
|
||||||
|
@ -66,17 +66,10 @@ class Query (YAPQuery):
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
if self.port == "exit" or self.port == "fail":
|
if self.port == "fail":
|
||||||
return
|
raise IndexError()
|
||||||
else:
|
return self.next()
|
||||||
if self.next():
|
|
||||||
return self.port,self.answer
|
|
||||||
else:
|
|
||||||
self.close()
|
|
||||||
|
|
||||||
def close( self ):
|
|
||||||
self.engine.reSet()
|
|
||||||
|
|
||||||
def name( name, arity):
|
def name( name, arity):
|
||||||
try:
|
try:
|
||||||
if arity > 0 and name.isidentifier(): # and not keyword.iskeyword(name):
|
if arity > 0 and name.isidentifier(): # and not keyword.iskeyword(name):
|
||||||
@ -141,13 +134,11 @@ class YAPShell:
|
|||||||
engine = self.engine
|
engine = self.engine
|
||||||
bindings = []
|
bindings = []
|
||||||
loop = False
|
loop = False
|
||||||
if g:
|
|
||||||
g.release()
|
|
||||||
g = python_query(self, query)
|
g = python_query(self, query)
|
||||||
self.q = Query( engine, g )
|
self.q = Query( engine, g )
|
||||||
for port,bind in self.q:
|
while self.q.next():
|
||||||
bindings += [bind]
|
bindings += [self.q.answer]
|
||||||
if port == "exit":
|
if self.q.port == "exit":
|
||||||
break
|
break
|
||||||
if loop:
|
if loop:
|
||||||
continue
|
continue
|
||||||
@ -189,7 +180,6 @@ class YAPShell:
|
|||||||
if not s:
|
if not s:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
print(s)
|
|
||||||
self.query_prolog(s)
|
self.query_prolog(s)
|
||||||
except SyntaxError as err:
|
except SyntaxError as err:
|
||||||
print("Syntax Error error: {0}".format(err))
|
print("Syntax Error error: {0}".format(err))
|
||||||
|
@ -34,8 +34,26 @@ jupyter_cell( _Caller, _, Line ) :-
|
|||||||
jupyter_cell( _Caller, _, [] ) :- !.
|
jupyter_cell( _Caller, _, [] ) :- !.
|
||||||
jupyter_cell( Caller, _, Line ) :-
|
jupyter_cell( Caller, _, Line ) :-
|
||||||
Self := Caller.query,
|
Self := Caller.query,
|
||||||
start_low_level_trace,
|
gated_call( streams(true),
|
||||||
python_query( Self, Line ).
|
python_query(Self,Line),
|
||||||
|
Gate,
|
||||||
|
restreams(Gate)
|
||||||
|
).
|
||||||
|
|
||||||
|
restreams(redo) :-
|
||||||
|
streams(true).
|
||||||
|
restreams(fail) :-
|
||||||
|
streams(false).
|
||||||
|
restreams(answer) :-
|
||||||
|
streams(false).
|
||||||
|
restreams(exit) :-
|
||||||
|
streams(false).
|
||||||
|
restreams(!) :-
|
||||||
|
streams(false).
|
||||||
|
restreams(external_exception(_)) :-
|
||||||
|
streams(false).
|
||||||
|
restreams(exception) :-
|
||||||
|
streams(false).
|
||||||
|
|
||||||
jupyter_consult(Text) :-
|
jupyter_consult(Text) :-
|
||||||
blank( Text ),
|
blank( Text ),
|
||||||
@ -58,24 +76,30 @@ blankc('\t').
|
|||||||
|
|
||||||
streams(false) :-
|
streams(false) :-
|
||||||
nb_setval(jupyter_cell, false),
|
nb_setval(jupyter_cell, false),
|
||||||
fail,
|
|
||||||
flush_output,
|
flush_output,
|
||||||
retract(cell_stream(S)),
|
retract(cell_stream(S)),
|
||||||
close(S),
|
close(S),
|
||||||
fail.
|
fail.
|
||||||
streams(false).
|
streams(false).
|
||||||
streams(true) :-
|
streams(true) :-
|
||||||
|
nb_setval(jupyter_cell, true),
|
||||||
nb_setval(jupyter_cell, true),
|
start_low_level_trace,
|
||||||
fail,
|
\+ current_stream('/python/input',_,_),
|
||||||
% open('/python/input', read, _Input, [alias(user_input),bom(false)]),
|
open('/python/input', read, Input, [alias(user_input),bom(false),script(false)]),
|
||||||
open('/python/sys.stdout', append, Output, [alias(user_output)]),
|
assert( cell_stream( Input) ),
|
||||||
assert( cell_stream( Output) ),
|
set_prolog_flag(user_input,Input),
|
||||||
open('/python/sys.stderr', append, Error, [alias(user_error)]),
|
fail.
|
||||||
|
streams(true) :-
|
||||||
|
\+ current_stream('/python/sys.stdout',_,_),
|
||||||
|
open('/python/sys.stdout', append, Output, [alias(user_output)]),
|
||||||
|
assert( cell_stream( Output) ),
|
||||||
|
fail.
|
||||||
|
streams(true) :-
|
||||||
|
\+ current_stream('/python/sys.stderr',_,_),
|
||||||
|
open('/python/sys.stderr', append, Error, [alias(user_error)]),
|
||||||
assert( cell_stream( Error) ),
|
assert( cell_stream( Error) ),
|
||||||
% set_prolog_flag(user_input,_Input),
|
set_prolog_flag(user_error, Error),
|
||||||
set_prolog_flag(user_output, Output),
|
fail.
|
||||||
set_prolog_flag(user_error, Error).
|
|
||||||
streams(true).
|
streams(true).
|
||||||
|
|
||||||
ready(_Self, Line ) :-
|
ready(_Self, Line ) :-
|
||||||
|
@ -41,6 +41,17 @@ streams = namedtuple('streams', ' text' )
|
|||||||
|
|
||||||
global engine
|
global engine
|
||||||
|
|
||||||
|
def tracefunc(frame, event, arg, indent=[0]):
|
||||||
|
if event == "call":
|
||||||
|
indent[0] += 2
|
||||||
|
print( "-" * indent[0] + "> call function", frame.f_code.co_name )
|
||||||
|
elif event == "return":
|
||||||
|
print( "<" + "-" * indent[0], "exit function", frame.f_code.co_name )
|
||||||
|
indent[0] -= 2
|
||||||
|
return tracefunc
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
class YAPInputSplitter(InputSplitter):
|
class YAPInputSplitter(InputSplitter):
|
||||||
"""An input splitter that recognizes all of iyap's special syntax."""
|
"""An input splitter that recognizes all of iyap's special syntax."""
|
||||||
|
|
||||||
@ -496,10 +507,8 @@ class YAPCompleter(Completer):
|
|||||||
self.matches = []
|
self.matches = []
|
||||||
prolog_res = self.shell.yapeng.mgoal(completions(text, self), "user",True)
|
prolog_res = self.shell.yapeng.mgoal(completions(text, self), "user",True)
|
||||||
if self.matches:
|
if self.matches:
|
||||||
print( text, magic_res )
|
|
||||||
return text, self.matches
|
return text, self.matches
|
||||||
magic_res = self.magic_matches(text)
|
magic_res = self.magic_matches(text)
|
||||||
print( text, magic_res )
|
|
||||||
return text, magic_res
|
return text, magic_res
|
||||||
|
|
||||||
|
|
||||||
@ -537,48 +546,43 @@ class YAPRun:
|
|||||||
# construct a self.queryuery from a one-line string
|
# construct a self.queryuery from a one-line string
|
||||||
# self.query is opaque to Python
|
# self.query is opaque to Python
|
||||||
try:
|
try:
|
||||||
print(query,s, file=sys.stderr)
|
program,squery,stop,howmany = self.prolog_cell(s)
|
||||||
program,query,stop,howmany = self.prolog_cell(s)
|
|
||||||
found = False
|
found = False
|
||||||
if s != self.os:
|
# sys.settrace(tracefunc)
|
||||||
self.os = s
|
if self.query and self.os == squery:
|
||||||
|
howmany += self.iterations
|
||||||
|
else:
|
||||||
|
if self.query:
|
||||||
|
self.query.close()
|
||||||
|
self.os = squery
|
||||||
self.iterations = 0
|
self.iterations = 0
|
||||||
self.bindings = []
|
self.bindings = []
|
||||||
pg = jupyter_query( self, program, query)
|
pg = jupyter_query( self, program, squery)
|
||||||
self.query = self.yapeng.query( pg)
|
self.query = self.yapeng.query(pg)
|
||||||
self.query.port = "call"
|
|
||||||
self.query.answer = {}
|
self.query.answer = {}
|
||||||
print("new", file=sys.stderr)
|
|
||||||
else:
|
|
||||||
self.query.port = "retry"
|
|
||||||
self.os = s
|
|
||||||
howmany += self.iterations
|
|
||||||
print('old', file=sys.stderr)
|
|
||||||
while self.query.next():
|
while self.query.next():
|
||||||
answer = self.query.answer
|
answer = self.query.answer
|
||||||
found = True
|
found = True
|
||||||
self.bindings += [answer]
|
self.bindings += [answer]
|
||||||
self.iterations += 1
|
self.iterations += 1
|
||||||
if stop and howmany == self.iterations:
|
|
||||||
return True, self.bindings
|
|
||||||
if self.query.port == "exit":
|
if self.query.port == "exit":
|
||||||
self.query.close()
|
|
||||||
self.query = None
|
|
||||||
self.os = None
|
self.os = None
|
||||||
sys.stderr.writeln('Done, with', self.bindings)
|
sys.stderr.writeln('Done, with', self.bindings)
|
||||||
return True,self.bindings
|
return True,self.bindings
|
||||||
|
if stop or howmany == self.iterations:
|
||||||
|
return True, self.bindings
|
||||||
if found:
|
if found:
|
||||||
sys.stderr.write('Done, with', self.bindings, '\n')
|
sys.stderr.writeln('Done, with ', self.bindings)
|
||||||
else:
|
else:
|
||||||
|
self.os = None
|
||||||
self.query.close()
|
self.query.close()
|
||||||
self.query = None
|
self.query = None
|
||||||
self.os = None
|
|
||||||
sys.stderr.write('Fail\n')
|
sys.stderr.write('Fail\n')
|
||||||
return True,{}
|
return True,self.bindings
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sys.stderr.write('Exception after', self.bindings, '\n')
|
sys.stderr.write('Exception after', self.bindings, '\n')
|
||||||
has_raised = True
|
has_raised = True
|
||||||
return False,{}
|
return False,[]
|
||||||
|
|
||||||
|
|
||||||
def _yrun_cell(self, raw_cell, store_history=True, silent=False,
|
def _yrun_cell(self, raw_cell, store_history=True, silent=False,
|
||||||
@ -637,10 +641,10 @@ class YAPRun:
|
|||||||
store_history = False
|
store_history = False
|
||||||
|
|
||||||
if store_history:
|
if store_history:
|
||||||
self.result .execution_count = self.shell.execution_count+1
|
self.result.execution_count = self.shell.execution_count+1
|
||||||
|
|
||||||
def error_before_exec(value):
|
def error_before_exec(value):
|
||||||
self.result .error_before_exec = value
|
self.result.error_before_exec = value
|
||||||
self.shell.last_execution_succeeded = False
|
self.shell.last_execution_succeeded = False
|
||||||
return self.result
|
return self.result
|
||||||
|
|
||||||
@ -701,7 +705,6 @@ class YAPRun:
|
|||||||
if linec:
|
if linec:
|
||||||
self.shell.run_line_magic(magic, line)
|
self.shell.run_line_magic(magic, line)
|
||||||
else:
|
else:
|
||||||
print("txt0: ",txt0,"\n", file=sys.stderr)
|
|
||||||
if len(txt0) == 1:
|
if len(txt0) == 1:
|
||||||
cell = ""
|
cell = ""
|
||||||
else:
|
else:
|
||||||
@ -713,35 +716,29 @@ class YAPRun:
|
|||||||
self.shell.displayhook.exec_result = self.result
|
self.shell.displayhook.exec_result = self.result
|
||||||
has_raised = False
|
has_raised = False
|
||||||
try:
|
try:
|
||||||
state = None
|
|
||||||
self.bindings = dicts = []
|
self.bindings = dicts = []
|
||||||
if cell.strip('\n \t'):
|
if cell.strip('\n \t'):
|
||||||
#create a Trace object, telling it what to ignore, and whether to
|
#create a Trace object, telling it what to ignore, and whether to
|
||||||
# do tracing or line-counting or both.
|
# do tracing or line-counting or both.
|
||||||
# tracer = trace.Trace(
|
# tracer = trace.Trace(
|
||||||
# #ignoredirs=[sys.prefix, sys.exec_prefix],
|
# ignoredirs=[sys.prefix, sys.exec_prefix],
|
||||||
# trace=1,
|
# trace=1,
|
||||||
# count=0)
|
# count=0)
|
||||||
#
|
#
|
||||||
# def f(self, cell):
|
|
||||||
# self.jupyter_query( cell )
|
|
||||||
|
|
||||||
# run the new command using the given tracer
|
# def f(self, cell, state):
|
||||||
#
|
# state = self.jupyter_query( cell )
|
||||||
try:
|
|
||||||
self.yapeng.mgoal(streams(True),"user", True)
|
# run the new command using the given tracer
|
||||||
#state = tracer.runfunc(f,self,cell)
|
#
|
||||||
state = self.jupyter_query( cell )
|
# self.yapeng.mgoal(streams(True),"user", True)
|
||||||
self.yapeng.mgoal(streams(False),"user", True)
|
# tracer.runfunc(f,self,cell,state)
|
||||||
except Exception as e:
|
self.jupyter_query( cell )
|
||||||
has_raised = True
|
# state = tracer.runfunc(jupyter_query( self, cell ) )
|
||||||
self.yapeng.mgoal(streams(False),"user")
|
# self.yapeng.mgoal(streams(False),"user", True)
|
||||||
if state:
|
self.shell.last_execution_succeeded = True
|
||||||
self.shell.last_execution_succeeded = True
|
self.result.result = (True, dicts)
|
||||||
self.result.result = (True, dicts)
|
|
||||||
else:
|
|
||||||
self.shell.last_execution_succeeded = True
|
|
||||||
self.result.result = (True, {})
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
has_raised = True
|
has_raised = True
|
||||||
self.result.result = False
|
self.result.result = False
|
||||||
@ -772,23 +769,29 @@ class YAPRun:
|
|||||||
- whether to stop
|
- whether to stop
|
||||||
- when to stop
|
- when to stop
|
||||||
"""
|
"""
|
||||||
i = -1
|
l0 = len(s)
|
||||||
try:
|
i = s.rfind(";")
|
||||||
its = 0
|
if i < 0:
|
||||||
j = 1
|
its = 1
|
||||||
while s[i].isdigit():
|
stop = True
|
||||||
ch = s[i]
|
taken = 0
|
||||||
its += j * (ord(ch) - ord('0'))
|
else:
|
||||||
i-=1
|
taken = l0-(i-1)
|
||||||
j *= 10;
|
n = s[i+1:].strip()
|
||||||
if s[i] == ';':
|
s = s[:i-1]
|
||||||
if j > 1 or its != 0:
|
if n:
|
||||||
return s[:i], 0 - i, True, its
|
its = 0
|
||||||
return s[:i], 0 - i, False, 0
|
j = 0
|
||||||
# one solution, stop
|
for ch in n:
|
||||||
return s, 0, True, 1
|
if not ch.isdigit():
|
||||||
except:
|
raise SyntaxError()
|
||||||
return s,0, False, 0
|
its = its*10+ (ord(ch) - ord('0'))
|
||||||
|
stop = False
|
||||||
|
else:
|
||||||
|
stop = False
|
||||||
|
its = -1
|
||||||
|
# one solution, stop
|
||||||
|
return s, taken, stop, its
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -809,4 +812,5 @@ class YAPRun:
|
|||||||
if query[-1] == '.':
|
if query[-1] == '.':
|
||||||
return s,'',False,0
|
return s,'',False,0
|
||||||
(query, _,loop, sols) = self.clean_end(query)
|
(query, _,loop, sols) = self.clean_end(query)
|
||||||
|
print(program, query, loop, sols)
|
||||||
return (program, query, loop, sols)
|
return (program, query, loop, sols)
|
||||||
|
@ -433,7 +433,7 @@ live :-
|
|||||||
'$process_answer'(Vs, LBlk, NLAnsw),
|
'$process_answer'(Vs, LBlk, NLAnsw),
|
||||||
'$write_vars_and_goals'(NLAnsw, first, FLAnsw).
|
'$write_vars_and_goals'(NLAnsw, first, FLAnsw).
|
||||||
|
|
||||||
% @pred write_query_answer( +Bindings )
|
%% @pred write_query_answer( +Bindings )
|
||||||
%
|
%
|
||||||
% YAP uses this routine to output the answer to a query.
|
% YAP uses this routine to output the answer to a query.
|
||||||
% _Bindings_ are
|
% _Bindings_ are
|
||||||
|
Reference in New Issue
Block a user