fixes to C interface

This commit is contained in:
Vítor Santos Costa 2018-05-30 08:12:51 +01:00
parent e9274ef5d3
commit 8cfeb53e77
5 changed files with 52 additions and 73 deletions

View File

@ -1728,6 +1728,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
LOCAL_PrologMode = UserMode; LOCAL_PrologMode = UserMode;
dgi->p = P; dgi->p = P;
dgi->cp = CP; dgi->cp = CP;
dgi->b0 = LCL0 - (CELL *)B;
dgi->CurSlot = LOCAL_CurSlot; dgi->CurSlot = LOCAL_CurSlot;
// ensure our current ENV receives current P. // 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 // __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), // slot=%d", pe, pe->CodeOfPred->opc, FAILCODE, Deref(ARG1), Deref(ARG2),
// LOCAL_CurSlot); // LOCAL_CurSlot);
dgi->b = dgi->b0 = LCL0 - (CELL *)B; dgi->b = LCL0 - (CELL *)B;
dgi->h = HR-H0; dgi->h = HR-H0;
dgi->tr = (CELL*)TR-LCL0; dgi->tr = (CELL*)TR-LCL0;
//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",
@ -1791,62 +1792,41 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) {
X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
CACHE_REGS CACHE_REGS
choiceptr myB; 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->b); myB = (choiceptr)(LCL0 - dgi->b0);
if (B > myB) { handler = B;
/* someone cut us */ while (handler
return false; //&& 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 */ if (LOCAL_PrologMode & AsyncIntMode) {
while (B && B->cp_b && B < myB && B->cp_b < myB) { Yap_signal(YAP_FAIL_SIGNAL);
#ifdef YAPOR }
CUT_prune_to(B); B = handler;
#endif
if (successful) { if (successful) {
B = B->cp_b;
trim_trail();
} else {
P = TRUSTFAILCODE;
Yap_exec_absmi(true, YAP_EXEC_ABSMI);
}
}
/* if backtracking asked for, recover space and bindings */
#if 0
if (0 && !successful) {
/* recover stack space */
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(); Yap_TrimTrail();
}
#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; CP = dgi->cp;
P = dgi->p; P = dgi->p;
} else {
Yap_exec_absmi(true, YAP_EXEC_ABSMI);
LOCAL_CurSlot = dgi->CurSlot; LOCAL_CurSlot = dgi->CurSlot;
ENV = YENV = B->cp_env;
HR = B->cp_h;
TR = B->cp_tr;
// use the current choicepoint
// B=B->cp_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);

View File

@ -1,3 +1,4 @@
/************************************************************************* /*************************************************************************
* * * *
* YAP Prolog * * YAP Prolog *
@ -705,6 +706,7 @@ static void write_list(Term t, int direction, int depth,
while (1) { while (1) {
writeTerm(HeadOfTerm(t), 999, depth + 1, FALSE, writeTerm(HeadOfTerm(t), 999, depth + 1, FALSE,
wglb); wglb);
Yap_DebugPlWriteln(TermNil);
t = Yap_GetFromHandle(h); t = Yap_GetFromHandle(h);
t = TailOfTerm(t); t = TailOfTerm(t);
if (IsVarTerm(t)) if (IsVarTerm(t))
@ -1070,7 +1072,7 @@ void Yap_plwrite(Term t, StreamDesc *mywrite, int max_depth, int flags,
struct write_globs wglb; struct write_globs wglb;
yhandle_t sls = Yap_CurrentSlot(); yhandle_t sls = Yap_CurrentSlot();
int lvl = push_text_stack(); int lvl = push_text_stack();
t = Deref(t);
if (t == 0) if (t == 0)
return; return;
if (!mywrite) { if (!mywrite) {

View File

@ -477,7 +477,7 @@ const char *YAPAtom::getName(void) { return Yap_AtomToUTF8Text(a); }
void YAPQuery::openQuery() { void YAPQuery::openQuery() {
CACHE_REGS CACHE_REGS
lvl = AllocLevel(); int lvl = AllocLevel();
if (ap == NULL || ap->OpcodeOfPred == UNDEF_OPCODE) { if (ap == NULL || ap->OpcodeOfPred == UNDEF_OPCODE) {
ap = rewriteUndefQuery(); ap = rewriteUndefQuery();
@ -509,6 +509,7 @@ bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) {
Yap_CloseHandles(q.CurSlot); Yap_CloseHandles(q.CurSlot);
pop_text_stack(q.lvl+1); pop_text_stack(q.lvl+1);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return result; return result;
} }
@ -1047,25 +1048,14 @@ std::stringstream s;
void YAPEngine::reSet() { void YAPEngine::reSet() {
/* ignore flags for now */ /* ignore flags for now */
BACKUP_MACHINE_REGS(); if (B && B->cp_b && B->cp_ap != NOCODE )
YAP_LeaveGoal(false, &q);
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 );
LOCAL_ActiveError->errorNo = YAP_NO_ERROR; LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
if (LOCAL_CommittedError) { if (LOCAL_CommittedError) {
LOCAL_CommittedError->errorNo = YAP_NO_ERROR; LOCAL_CommittedError->errorNo = YAP_NO_ERROR;
free(LOCAL_CommittedError ); free(LOCAL_CommittedError );
LOCAL_CommittedError = NULL; LOCAL_CommittedError = NULL;
} }
RECOVER_MACHINE_REGS();
} }
Term YAPEngine::top_level(std::string s) { Term YAPEngine::top_level(std::string s) {

View File

@ -69,13 +69,14 @@ argi(N,I,I1) :-
I1 is I+1. I1 is I+1.
python_query( Caller, String ) :- python_query( Caller, String ) :-
writeln(String),
atomic_to_term( String, Goal, VarNames ), atomic_to_term( String, Goal, VarNames ),
query_to_answer( Goal, VarNames, Status, Bindings), query_to_answer( Goal, VarNames, Status, Bindings),
Caller.port := Status, Caller.q.port := Status,
% := print( gc.get_referrers(Caller.port)), % := print( gc.get_referrers(Caller.port)),
write_query_answer( Bindings ), write_query_answer( Bindings ),
nl(user_error), nl(user_error),
Caller.answer := {}, Caller.q.answer := {},
maplist(in_dict(Caller.answer), Bindings). maplist(in_dict(Caller.answer), Bindings).
% := print( "b", gc.get_referrers(Caller.answer)). % := print( "b", gc.get_referrers(Caller.answer)).

View File

@ -73,7 +73,7 @@ class Query:
raise StopIteration() raise StopIteration()
if self.q.next(): if self.q.next():
rc = self.q.answer rc = self.q.answer
if self.q.port == "exit": # if self.q.port == "exit":
return rc return rc
else: else:
if self: if self:
@ -138,7 +138,7 @@ class YAPShell:
# # vs is the list of variables # # vs is the list of variables
# you can print it out, the left-side is the variable name, # you can print it out, the left-side is the variable name,
# the right side wraps a handle to a variable # the right side wraps a handle to a variable
import pdb; pdb.set_trace() #import pdb; pdb.set_trace()
# #pdb.set_trace() # #pdb.set_trace()
# atom match either symbols, or if no symbol exists, sttrings, In this case # atom match either symbols, or if no symbol exists, sttrings, In this case
# variable names should match strings # variable names should match strings
@ -154,7 +154,9 @@ class YAPShell:
g.release() g.release()
g = python_query(self, query) g = python_query(self, query)
self.q = Query( engine, g ) self.q = Query( engine, g )
print(self.q.port)
for bind in self.q: for bind in self.q:
print(bind,self.q.port)
bindings += [bind] bindings += [bind]
if loop: if loop:
continue continue
@ -175,6 +177,7 @@ class YAPShell:
break break
if self.q: if self.q:
self.q.close() self.q.close()
self.q = None
if bindings: if bindings:
return True,bindings return True,bindings
print("No (more) answers") print("No (more) answers")
@ -183,7 +186,9 @@ class YAPShell:
if not self.q: if not self.q:
return False, None return False, None
self.q.close() self.q.close()
print("Exception") self.q = None
print("Exception",e)
e.errorNo = 0
return False, None return False, None
def live(self, engine, **kwargs): def live(self, engine, **kwargs):
@ -193,8 +198,9 @@ class YAPShell:
try: try:
s = input("?- ") s = input("?- ")
if not s: if not s:
loop = False 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))