From 8cfeb53e77be9a0d3ea36cd3633d19ffc7eebd3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Wed, 30 May 2018 08:12:51 +0100 Subject: [PATCH] fixes to C interface --- C/c_interface.c | 78 +++++++++++----------------- C/write.c | 4 +- CXX/yapi.cpp | 20 ++----- packages/python/swig/prolog/yapi.yap | 7 +-- packages/python/swig/yap4py/yapi.py | 16 ++++-- 5 files changed, 52 insertions(+), 73 deletions(-) diff --git a/C/c_interface.c b/C/c_interface.c index 4a845d653..c49de58d9 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -1726,8 +1726,9 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) { BACKUP_MACHINE_REGS(); LOCAL_ActiveError->errorNo = YAP_NO_ERROR; LOCAL_PrologMode = UserMode; - dgi->p = P; + dgi->p = P; dgi->cp = CP; + dgi->b0 = LCL0 - (CELL *)B; dgi->CurSlot = LOCAL_CurSlot; // ensure our current ENV receives current P. @@ -1736,7 +1737,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) { // __android_log_print(ANDROID_LOG_INFO, "YAP ", "ap=%p %d %x %x args=%x,%x // slot=%d", pe, pe->CodeOfPred->opc, FAILCODE, Deref(ARG1), Deref(ARG2), // LOCAL_CurSlot); - dgi->b = dgi->b0 = LCL0 - (CELL *)B; + dgi->b = LCL0 - (CELL *)B; dgi->h = HR-H0; dgi->tr = (CELL*)TR-LCL0; //fprintf(stderr,"PrepGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", @@ -1791,64 +1792,43 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { CACHE_REGS - choiceptr myB; + 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); BACKUP_MACHINE_REGS(); - myB = (choiceptr)(LCL0 - dgi->b); - if (B > myB) { - /* someone cut us */ - return false; + myB = (choiceptr)(LCL0 - dgi->b0); + handler = B; + while (handler + //&& LOCAL_CBorder > LCL0 - (CELL *)handler + //&& handler->cp_ap != NOCODE + && handler->cp_b != NULL + && handler != myB + ) { + handler->cp_ap = TRUSTFAILCODE; + handler = handler->cp_b; } - /* prune away choicepoints */ - while (B && B->cp_b && B < myB && B->cp_b < myB) { -#ifdef YAPOR - CUT_prune_to(B); -#endif - if (successful) { - B = B->cp_b; - trim_trail(); - } else { - P = TRUSTFAILCODE; + if (LOCAL_PrologMode & AsyncIntMode) { + Yap_signal(YAP_FAIL_SIGNAL); + } + B = handler; + if (successful) { + Yap_TrimTrail(); + CP = dgi->cp; + P = dgi->p; + } else { Yap_exec_absmi(true, YAP_EXEC_ABSMI); - } - } - /* if backtracking asked for, recover space and bindings */ -#if 0 - if (0 && !successful) { - /* recover stack space */ + LOCAL_CurSlot = dgi->CurSlot; + ENV = YENV = B->cp_env; HR = B->cp_h; TR = B->cp_tr; -#ifdef DEPTH_LIMIT - DEPTH = B->cp_depth; -#endif /* DEPTH_LIMIT */ - - YENV = ENV = B->cp_env; - } else { - Yap_TrimTrail(); + // use the current choicepoint + // B=B->cp_b; + ASP=(CELL*)B; } - #endif -/* recover local stack */ -#ifdef DEPTH_LIMIT - DEPTH = ENV[E_DEPTH]; -#endif - /* ASP should be set to the top of the local stack when we - did the call */ - ASP = B->cp_env; - /* YENV should be set to the current environment */ - YENV = ENV = (CELL *)((B->cp_env)[E_E]); - B = B->cp_b; - // SET_BB(B); - if (B) { - HB = PROTECT_FROZEN_H(B); - } - CP = dgi->cp; - P = dgi->p; - LOCAL_CurSlot = dgi->CurSlot; 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); return TRUE; } diff --git a/C/write.c b/C/write.c index 45666357f..0c7c87795 100644 --- a/C/write.c +++ b/C/write.c @@ -1,3 +1,4 @@ + /************************************************************************* * * * YAP Prolog * @@ -705,6 +706,7 @@ static void write_list(Term t, int direction, int depth, while (1) { writeTerm(HeadOfTerm(t), 999, depth + 1, FALSE, wglb); + Yap_DebugPlWriteln(TermNil); t = Yap_GetFromHandle(h); t = TailOfTerm(t); if (IsVarTerm(t)) @@ -1070,7 +1072,7 @@ void Yap_plwrite(Term t, StreamDesc *mywrite, int max_depth, int flags, struct write_globs wglb; yhandle_t sls = Yap_CurrentSlot(); int lvl = push_text_stack(); - t = Deref(t); + if (t == 0) return; if (!mywrite) { diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index b63a75cee..a8aeae14c 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -477,7 +477,7 @@ const char *YAPAtom::getName(void) { return Yap_AtomToUTF8Text(a); } void YAPQuery::openQuery() { CACHE_REGS - lvl = AllocLevel(); + int lvl = AllocLevel(); if (ap == NULL || ap->OpcodeOfPred == UNDEF_OPCODE) { ap = rewriteUndefQuery(); @@ -509,6 +509,7 @@ bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) { Yap_CloseHandles(q.CurSlot); pop_text_stack(q.lvl+1); + RECOVER_MACHINE_REGS(); return result; } @@ -658,7 +659,7 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) { BACKUP_MACHINE_REGS(); CELL *nts; if (ts) { - goal = YAPApplTerm(f, nts); +goal = YAPApplTerm(f, nts); } else { goal = YAPVarTerm(); nts = nullptr; @@ -1047,25 +1048,14 @@ std::stringstream s; void YAPEngine::reSet() { /* ignore flags for now */ - BACKUP_MACHINE_REGS(); - - choiceptr b = (choiceptr)(LCL0-q.b); - if (b > B) B = b; - P = FAILCODE; - Yap_exec_absmi(true, YAP_EXEC_ABSMI); - /* recover stack space */ - if (H0+q.h < HR) - HR = H0+q.h; - if (LCL0+q.tr < (CELL*)TR) - TR = (tr_fr_ptr)(LCL0+q.tr); - Yap_CloseHandles( q.CurSlot ); + if (B && B->cp_b && B->cp_ap != NOCODE ) + YAP_LeaveGoal(false, &q); LOCAL_ActiveError->errorNo = YAP_NO_ERROR; if (LOCAL_CommittedError) { LOCAL_CommittedError->errorNo = YAP_NO_ERROR; free(LOCAL_CommittedError ); LOCAL_CommittedError = NULL; } -RECOVER_MACHINE_REGS(); } Term YAPEngine::top_level(std::string s) { diff --git a/packages/python/swig/prolog/yapi.yap b/packages/python/swig/prolog/yapi.yap index cd71eb323..66f153ea6 100644 --- a/packages/python/swig/prolog/yapi.yap +++ b/packages/python/swig/prolog/yapi.yap @@ -69,13 +69,14 @@ argi(N,I,I1) :- I1 is I+1. python_query( Caller, String ) :- - atomic_to_term( String, Goal, VarNames ), +writeln(String), +atomic_to_term( String, Goal, VarNames ), query_to_answer( Goal, VarNames, Status, Bindings), - Caller.port := Status, + Caller.q.port := Status, % := print( gc.get_referrers(Caller.port)), write_query_answer( Bindings ), nl(user_error), - Caller.answer := {}, + Caller.q.answer := {}, maplist(in_dict(Caller.answer), Bindings). % := print( "b", gc.get_referrers(Caller.answer)). diff --git a/packages/python/swig/yap4py/yapi.py b/packages/python/swig/yap4py/yapi.py index 056d8691d..db60d0db5 100644 --- a/packages/python/swig/yap4py/yapi.py +++ b/packages/python/swig/yap4py/yapi.py @@ -73,8 +73,8 @@ class Query: raise StopIteration() if self.q.next(): rc = self.q.answer - if self.q.port == "exit": - return rc + # if self.q.port == "exit": + return rc else: if self: self.close() @@ -138,7 +138,7 @@ class YAPShell: # # vs is the list of variables # you can print it out, the left-side is the variable name, # the right side wraps a handle to a variable - import pdb; pdb.set_trace() + #import pdb; pdb.set_trace() # #pdb.set_trace() # atom match either symbols, or if no symbol exists, sttrings, In this case # variable names should match strings @@ -154,7 +154,9 @@ class YAPShell: g.release() g = python_query(self, query) self.q = Query( engine, g ) + print(self.q.port) for bind in self.q: + print(bind,self.q.port) bindings += [bind] if loop: continue @@ -175,6 +177,7 @@ class YAPShell: break if self.q: self.q.close() + self.q = None if bindings: return True,bindings print("No (more) answers") @@ -183,7 +186,9 @@ class YAPShell: if not self.q: return False, None self.q.close() - print("Exception") + self.q = None + print("Exception",e) + e.errorNo = 0 return False, None def live(self, engine, **kwargs): @@ -193,8 +198,9 @@ class YAPShell: try: s = input("?- ") if not s: - loop = False + continue else: + print(s) self.query_prolog(s) except SyntaxError as err: print("Syntax Error error: {0}".format(err))