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
|
||||
PredEntry *pe = ape;
|
||||
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();
|
||||
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",
|
||||
// HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
||||
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;
|
||||
if (out) {
|
||||
dgi->EndSlot = LOCAL_CurSlot;
|
||||
@ -1796,8 +1796,7 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
|
||||
choiceptr myB, handler;
|
||||
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",
|
||||
successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot);
|
||||
// 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);
|
||||
BACKUP_MACHINE_REGS();
|
||||
myB = (choiceptr)(LCL0 - dgi->b0);
|
||||
handler = B;
|
||||
@ -1829,8 +1828,7 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
|
||||
ASP=(CELL*)B;
|
||||
}
|
||||
RECOVER_MACHINE_REGS();
|
||||
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);
|
||||
// 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);
|
||||
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.
|
||||
PredEntry *getPred(YAPTerm &t, CELL *& outp);
|
||||
PredEntry *getPred(Term &t, CELL *& outp);
|
||||
|
||||
PredEntry *asPred() { return ap; };
|
||||
|
||||
@ -99,47 +99,36 @@ protected:
|
||||
/// Just do nothing.
|
||||
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);
|
||||
|
||||
/// Term constructor for predicates
|
||||
///
|
||||
/// It is just a call to getPred
|
||||
inline YAPPredicate(Term t) {
|
||||
CELL *v = nullptr;
|
||||
YAPTerm tt = YAPTerm(t);
|
||||
ap = getPred(tt, v);
|
||||
inline YAPPredicate(Term t, CELL *&v) {
|
||||
if (t) {
|
||||
ap = getPred(t, v);
|
||||
}
|
||||
}
|
||||
|
||||
inline YAPPredicate(Term t) {
|
||||
if (t) {
|
||||
CELL *v = nullptr;
|
||||
ap = getPred(t, v);
|
||||
}
|
||||
}
|
||||
|
||||
/// Term constructor for predicates
|
||||
///
|
||||
/// It is just a call to getPred
|
||||
inline YAPPredicate(YAPTerm t) {
|
||||
CELL *v = nullptr;
|
||||
ap = getPred(t, v);
|
||||
inline YAPPredicate(YAPTerm t, CELL *&v) {
|
||||
Term tp = t.term();
|
||||
ap = getPred(tp, v);
|
||||
}
|
||||
inline YAPPredicate(YAPTerm t) {
|
||||
CELL *v = nullptr;
|
||||
Term tp = t.term();
|
||||
ap = getPred(tp, v);
|
||||
}
|
||||
|
||||
/// Cast constructor for predicates,
|
||||
@ -156,6 +145,28 @@ protected:
|
||||
|
||||
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
|
||||
///
|
||||
/// Asssumes that we use the current module.
|
||||
@ -180,6 +191,7 @@ public:
|
||||
///
|
||||
YAPPredicate(YAPAtom at);
|
||||
|
||||
|
||||
/// Mod:Name/Arity constructor for predicates.
|
||||
///
|
||||
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;
|
||||
|
||||
|
||||
std::cerr << "mgoal " << YAPTerm(t).text() << "\n";
|
||||
//std::cerr << "mgoal " << YAPTerm(t).text() << "\n";
|
||||
// _save = PyEval_SaveThread();
|
||||
#endif
|
||||
CACHE_REGS
|
||||
@ -632,7 +632,6 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
|
||||
|
||||
/* ignore flags for now */
|
||||
BACKUP_MACHINE_REGS();
|
||||
Term *nts;
|
||||
Term goal;
|
||||
|
||||
if (ts) {
|
||||
@ -654,7 +653,6 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
|
||||
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) {
|
||||
/* ignore flags for now */
|
||||
BACKUP_MACHINE_REGS();
|
||||
CELL *nts;
|
||||
if (ts) {
|
||||
goal = YAPApplTerm(f, nts);
|
||||
} else {
|
||||
@ -667,30 +665,6 @@ goal = YAPApplTerm(f, nts);
|
||||
}
|
||||
#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) {
|
||||
BACKUP_MACHINE_REGS();
|
||||
@ -715,7 +689,7 @@ RECOVER_MACHINE_REGS();
|
||||
bool YAPQuery::next() {
|
||||
CACHE_REGS
|
||||
bool result = false;
|
||||
std::cerr << "next " << YAPTerm(goal).text() << "\n";
|
||||
//std::cerr << "next " << YAPTerm(goal).text() << "\n";
|
||||
|
||||
sigjmp_buf buf, *oldp = LOCAL_RestartEnv;
|
||||
e = nullptr;
|
||||
@ -909,38 +883,39 @@ YAPPredicate::YAPPredicate(YAPAtom at, uintptr_t arity) {
|
||||
}
|
||||
|
||||
/// 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
|
||||
Term m = Yap_CurrentModule(), t = tt.term();
|
||||
Term m = Yap_CurrentModule();
|
||||
t = Yap_StripModule(t, &m);
|
||||
|
||||
if (IsVarTerm(t) || IsNumTerm(t)) {
|
||||
if (IsVarTerm(t))
|
||||
throw YAPError(SOURCE(), INSTANTIATION_ERROR, tt.term(), 0);
|
||||
throw YAPError(SOURCE(), INSTANTIATION_ERROR, t, 0);
|
||||
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)) {
|
||||
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
||||
outp = (Term *)NULL;
|
||||
return ap;
|
||||
} else if (IsPairTerm(t)) {
|
||||
Term ts[2];
|
||||
Term ts[2], *s = ( out ? out : ts );
|
||||
Functor FunctorConsult = Yap_MkFunctor(Yap_LookupAtom("consult"), 1);
|
||||
ts[1] = t;
|
||||
ts[0] = m;
|
||||
t = Yap_MkApplTerm(FunctorModule, 2, ts);
|
||||
s[1] = t;
|
||||
s[0] = m;
|
||||
t = Yap_MkApplTerm(FunctorModule, 2, s);
|
||||
t = Yap_MkApplTerm(FunctorConsult, 1, &t);
|
||||
tt.put(t);
|
||||
outp = RepAppl(t) + 1;
|
||||
if (!out)
|
||||
out = RepAppl(t) + 1;
|
||||
}
|
||||
Functor f = FunctorOfTerm(t);
|
||||
if (IsExtensionFunctor(f)) {
|
||||
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
|
37
CXX/yapq.hh
37
CXX/yapq.hh
@ -44,8 +44,8 @@ class X_API YAPQuery : public YAPPredicate {
|
||||
YAP_dogoalinfo q_h;
|
||||
Term names;
|
||||
Term goal;
|
||||
CELL *nts;
|
||||
// temporaries
|
||||
int lvl;
|
||||
YAPError *e;
|
||||
|
||||
inline void setNext() { // oq = LOCAL_execution;
|
||||
@ -69,6 +69,7 @@ public:
|
||||
goal = TermTrue;
|
||||
openQuery();
|
||||
};
|
||||
inline ~YAPQuery() { close(); }
|
||||
/// 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
|
||||
@ -92,32 +93,10 @@ 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) {
|
||||
CELL *qt = nullptr;
|
||||
inline YAPQuery(const char *s) : YAPPredicate(s, goal, names, (nts = &ARG1)) {
|
||||
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "got game %d",
|
||||
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();
|
||||
};
|
||||
// inline YAPQuery() : YAPPredicate(s, tgoal, tnames)
|
||||
@ -134,7 +113,13 @@ public:
|
||||
///
|
||||
/// 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
|
||||
void setFlag(int flag) { q_flags |= flag; }
|
||||
/// 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;
|
||||
StreamDesc *st = YAP_GetStreamFromId(sno);
|
||||
if (st->user_name == TermOutStream) {
|
||||
// term_t tg = python_acquire_GIL();
|
||||
PySys_WriteStdout("%C", ch);
|
||||
//python_release_GIL(tg);
|
||||
term_t tg = python_acquire_GIL();
|
||||
PySys_WriteStdout("%C", ch);
|
||||
python_release_GIL(tg);
|
||||
return ch;
|
||||
}
|
||||
if (st->user_name == TermErrStream) {
|
||||
//term_t tg = python_acquire_GIL();
|
||||
term_t tg = python_acquire_GIL();
|
||||
PySys_WriteStderr("%C", ch);
|
||||
//python_release_GIL(tg);
|
||||
python_release_GIL(tg);
|
||||
return ch;
|
||||
}
|
||||
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;
|
||||
} else if (strcmp(name, "sys.stderr") == 0) {
|
||||
st->user_name = TermErrStream;
|
||||
} else if (strcmp(name, "input") == 0) {
|
||||
pystream = PyObject_Call(pystream, PyTuple_New(0), NULL);
|
||||
// } else if (strcmp(name, "input") == 0) {
|
||||
//pystream = PyObject_Call(pystream, PyTuple_New(0), NULL);
|
||||
} else {
|
||||
st->user_name = YAP_MkAtomTerm(st->name);
|
||||
}
|
||||
@ -90,16 +90,16 @@ static bool getLine(int inp) {
|
||||
char *myrl_line = NULL;
|
||||
StreamDesc *rl_instream = YAP_RepStreamFromId(inp);
|
||||
term_t ctk = python_acquire_GIL();
|
||||
fprintf(stderr, "in");
|
||||
Py_ssize_t size;
|
||||
PyObject *prompt = PyUnicode_FromString("?- "),
|
||||
*msg = PyUnicode_FromString(" **input** ");
|
||||
/* window of vulnerability opened */
|
||||
myrl_line = PyUnicode_AsUTF8(PyObject_CallFunctionObjArgs(
|
||||
rl_instream->u.private_data, msg, prompt, NULL));
|
||||
myrl_line = PyUnicode_AsUTF8AndSize(PyObject_CallFunctionObjArgs(
|
||||
rl_instream->u.private_data, msg, prompt, NULL), &size);
|
||||
python_release_GIL(ctk);
|
||||
rl_instream->u.irl.ptr = rl_instream->u.irl.buf =
|
||||
(const unsigned char *)myrl_line;
|
||||
myrl_line = NULL;
|
||||
(const unsigned char *)malloc(size);
|
||||
memcpy((void *)rl_instream->u.irl.buf, myrl_line, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,6 @@ python_query( Caller, String ) :-
|
||||
query_to_answer( Goal, VarNames, Status, Bindings),
|
||||
atom_to_string( Status, SStatus ),
|
||||
Caller.port := SStatus,
|
||||
start_low_level_trace,
|
||||
write_query_answer( Bindings ),
|
||||
nl(user_error),
|
||||
Caller.answer := {},
|
||||
@ -88,4 +87,4 @@ in_dict(Dict, nonvar([V0|Vs],G)) :- !,
|
||||
Dict[V0] := G,
|
||||
in_dict( Dict, nonvar(Vs, G) ).
|
||||
in_dict(_Dict, nonvar([],_G)) :- !.
|
||||
in_dict(_, _).
|
||||
in_dict(_, _)
|
||||
|
@ -66,17 +66,10 @@ class Query (YAPQuery):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
if self.port == "exit" or self.port == "fail":
|
||||
return
|
||||
else:
|
||||
if self.next():
|
||||
return self.port,self.answer
|
||||
else:
|
||||
self.close()
|
||||
|
||||
def close( self ):
|
||||
self.engine.reSet()
|
||||
|
||||
if self.port == "fail":
|
||||
raise IndexError()
|
||||
return self.next()
|
||||
|
||||
def name( name, arity):
|
||||
try:
|
||||
if arity > 0 and name.isidentifier(): # and not keyword.iskeyword(name):
|
||||
@ -141,13 +134,11 @@ class YAPShell:
|
||||
engine = self.engine
|
||||
bindings = []
|
||||
loop = False
|
||||
if g:
|
||||
g.release()
|
||||
g = python_query(self, query)
|
||||
self.q = Query( engine, g )
|
||||
for port,bind in self.q:
|
||||
bindings += [bind]
|
||||
if port == "exit":
|
||||
while self.q.next():
|
||||
bindings += [self.q.answer]
|
||||
if self.q.port == "exit":
|
||||
break
|
||||
if loop:
|
||||
continue
|
||||
@ -189,7 +180,6 @@ class YAPShell:
|
||||
if not s:
|
||||
continue
|
||||
else:
|
||||
print(s)
|
||||
self.query_prolog(s)
|
||||
except SyntaxError as err:
|
||||
print("Syntax Error error: {0}".format(err))
|
||||
|
@ -34,8 +34,26 @@ jupyter_cell( _Caller, _, Line ) :-
|
||||
jupyter_cell( _Caller, _, [] ) :- !.
|
||||
jupyter_cell( Caller, _, Line ) :-
|
||||
Self := Caller.query,
|
||||
start_low_level_trace,
|
||||
python_query( Self, Line ).
|
||||
gated_call( streams(true),
|
||||
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) :-
|
||||
blank( Text ),
|
||||
@ -58,24 +76,30 @@ blankc('\t').
|
||||
|
||||
streams(false) :-
|
||||
nb_setval(jupyter_cell, false),
|
||||
fail,
|
||||
flush_output,
|
||||
retract(cell_stream(S)),
|
||||
close(S),
|
||||
fail.
|
||||
streams(false).
|
||||
streams(true) :-
|
||||
|
||||
nb_setval(jupyter_cell, true),
|
||||
fail,
|
||||
% open('/python/input', read, _Input, [alias(user_input),bom(false)]),
|
||||
open('/python/sys.stdout', append, Output, [alias(user_output)]),
|
||||
assert( cell_stream( Output) ),
|
||||
open('/python/sys.stderr', append, Error, [alias(user_error)]),
|
||||
nb_setval(jupyter_cell, true),
|
||||
start_low_level_trace,
|
||||
\+ current_stream('/python/input',_,_),
|
||||
open('/python/input', read, Input, [alias(user_input),bom(false),script(false)]),
|
||||
assert( cell_stream( Input) ),
|
||||
set_prolog_flag(user_input,Input),
|
||||
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) ),
|
||||
% set_prolog_flag(user_input,_Input),
|
||||
set_prolog_flag(user_output, Output),
|
||||
set_prolog_flag(user_error, Error).
|
||||
set_prolog_flag(user_error, Error),
|
||||
fail.
|
||||
streams(true).
|
||||
|
||||
ready(_Self, Line ) :-
|
||||
|
@ -41,6 +41,17 @@ streams = namedtuple('streams', ' text' )
|
||||
|
||||
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):
|
||||
"""An input splitter that recognizes all of iyap's special syntax."""
|
||||
|
||||
@ -496,10 +507,8 @@ class YAPCompleter(Completer):
|
||||
self.matches = []
|
||||
prolog_res = self.shell.yapeng.mgoal(completions(text, self), "user",True)
|
||||
if self.matches:
|
||||
print( text, magic_res )
|
||||
return text, self.matches
|
||||
magic_res = self.magic_matches(text)
|
||||
print( text, magic_res )
|
||||
return text, magic_res
|
||||
|
||||
|
||||
@ -537,48 +546,43 @@ class YAPRun:
|
||||
# construct a self.queryuery from a one-line string
|
||||
# self.query is opaque to Python
|
||||
try:
|
||||
print(query,s, file=sys.stderr)
|
||||
program,query,stop,howmany = self.prolog_cell(s)
|
||||
program,squery,stop,howmany = self.prolog_cell(s)
|
||||
found = False
|
||||
if s != self.os:
|
||||
self.os = s
|
||||
# sys.settrace(tracefunc)
|
||||
if self.query and self.os == squery:
|
||||
howmany += self.iterations
|
||||
else:
|
||||
if self.query:
|
||||
self.query.close()
|
||||
self.os = squery
|
||||
self.iterations = 0
|
||||
self.bindings = []
|
||||
pg = jupyter_query( self, program, query)
|
||||
self.query = self.yapeng.query( pg)
|
||||
self.query.port = "call"
|
||||
pg = jupyter_query( self, program, squery)
|
||||
self.query = self.yapeng.query(pg)
|
||||
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():
|
||||
answer = self.query.answer
|
||||
found = True
|
||||
self.bindings += [answer]
|
||||
self.iterations += 1
|
||||
if stop and howmany == self.iterations:
|
||||
return True, self.bindings
|
||||
if self.query.port == "exit":
|
||||
self.query.close()
|
||||
self.query = None
|
||||
self.os = None
|
||||
sys.stderr.writeln('Done, with', self.bindings)
|
||||
return True,self.bindings
|
||||
if stop or howmany == self.iterations:
|
||||
return True, self.bindings
|
||||
if found:
|
||||
sys.stderr.write('Done, with', self.bindings, '\n')
|
||||
sys.stderr.writeln('Done, with ', self.bindings)
|
||||
else:
|
||||
self.os = None
|
||||
self.query.close()
|
||||
self.query = None
|
||||
self.os = None
|
||||
sys.stderr.write('Fail\n')
|
||||
return True,{}
|
||||
return True,self.bindings
|
||||
except Exception as e:
|
||||
sys.stderr.write('Exception after', self.bindings, '\n')
|
||||
has_raised = True
|
||||
return False,{}
|
||||
return False,[]
|
||||
|
||||
|
||||
def _yrun_cell(self, raw_cell, store_history=True, silent=False,
|
||||
@ -637,10 +641,10 @@ class YAPRun:
|
||||
store_history = False
|
||||
|
||||
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):
|
||||
self.result .error_before_exec = value
|
||||
self.result.error_before_exec = value
|
||||
self.shell.last_execution_succeeded = False
|
||||
return self.result
|
||||
|
||||
@ -701,7 +705,6 @@ class YAPRun:
|
||||
if linec:
|
||||
self.shell.run_line_magic(magic, line)
|
||||
else:
|
||||
print("txt0: ",txt0,"\n", file=sys.stderr)
|
||||
if len(txt0) == 1:
|
||||
cell = ""
|
||||
else:
|
||||
@ -713,35 +716,29 @@ class YAPRun:
|
||||
self.shell.displayhook.exec_result = self.result
|
||||
has_raised = False
|
||||
try:
|
||||
state = None
|
||||
self.bindings = dicts = []
|
||||
if cell.strip('\n \t'):
|
||||
#create a Trace object, telling it what to ignore, and whether to
|
||||
# do tracing or line-counting or both.
|
||||
# tracer = trace.Trace(
|
||||
# #ignoredirs=[sys.prefix, sys.exec_prefix],
|
||||
# trace=1,
|
||||
# count=0)
|
||||
#
|
||||
# def f(self, cell):
|
||||
# self.jupyter_query( cell )
|
||||
#create a Trace object, telling it what to ignore, and whether to
|
||||
# do tracing or line-counting or both.
|
||||
# tracer = trace.Trace(
|
||||
# ignoredirs=[sys.prefix, sys.exec_prefix],
|
||||
# trace=1,
|
||||
# count=0)
|
||||
#
|
||||
|
||||
# run the new command using the given tracer
|
||||
#
|
||||
try:
|
||||
self.yapeng.mgoal(streams(True),"user", True)
|
||||
#state = tracer.runfunc(f,self,cell)
|
||||
state = self.jupyter_query( cell )
|
||||
self.yapeng.mgoal(streams(False),"user", True)
|
||||
except Exception as e:
|
||||
has_raised = True
|
||||
self.yapeng.mgoal(streams(False),"user")
|
||||
if state:
|
||||
self.shell.last_execution_succeeded = True
|
||||
self.result.result = (True, dicts)
|
||||
else:
|
||||
self.shell.last_execution_succeeded = True
|
||||
self.result.result = (True, {})
|
||||
# def f(self, cell, state):
|
||||
# state = self.jupyter_query( cell )
|
||||
|
||||
# run the new command using the given tracer
|
||||
#
|
||||
# self.yapeng.mgoal(streams(True),"user", True)
|
||||
# tracer.runfunc(f,self,cell,state)
|
||||
self.jupyter_query( cell )
|
||||
# state = tracer.runfunc(jupyter_query( self, cell ) )
|
||||
# self.yapeng.mgoal(streams(False),"user", True)
|
||||
self.shell.last_execution_succeeded = True
|
||||
self.result.result = (True, dicts)
|
||||
|
||||
except Exception as e:
|
||||
has_raised = True
|
||||
self.result.result = False
|
||||
@ -772,23 +769,29 @@ class YAPRun:
|
||||
- whether to stop
|
||||
- when to stop
|
||||
"""
|
||||
i = -1
|
||||
try:
|
||||
its = 0
|
||||
j = 1
|
||||
while s[i].isdigit():
|
||||
ch = s[i]
|
||||
its += j * (ord(ch) - ord('0'))
|
||||
i-=1
|
||||
j *= 10;
|
||||
if s[i] == ';':
|
||||
if j > 1 or its != 0:
|
||||
return s[:i], 0 - i, True, its
|
||||
return s[:i], 0 - i, False, 0
|
||||
# one solution, stop
|
||||
return s, 0, True, 1
|
||||
except:
|
||||
return s,0, False, 0
|
||||
l0 = len(s)
|
||||
i = s.rfind(";")
|
||||
if i < 0:
|
||||
its = 1
|
||||
stop = True
|
||||
taken = 0
|
||||
else:
|
||||
taken = l0-(i-1)
|
||||
n = s[i+1:].strip()
|
||||
s = s[:i-1]
|
||||
if n:
|
||||
its = 0
|
||||
j = 0
|
||||
for ch in n:
|
||||
if not ch.isdigit():
|
||||
raise SyntaxError()
|
||||
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] == '.':
|
||||
return s,'',False,0
|
||||
(query, _,loop, sols) = self.clean_end(query)
|
||||
print(program, query, loop, sols)
|
||||
return (program, query, loop, sols)
|
||||
|
@ -433,7 +433,7 @@ live :-
|
||||
'$process_answer'(Vs, LBlk, NLAnsw),
|
||||
'$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.
|
||||
% _Bindings_ are
|
||||
|
Reference in New Issue
Block a user