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;
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,62 +1792,41 @@ 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 (LOCAL_PrologMode & AsyncIntMode) {
Yap_signal(YAP_FAIL_SIGNAL);
}
B = handler;
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();
}
#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;
} else {
Yap_exec_absmi(true, YAP_EXEC_ABSMI);
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();
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);

View File

@ -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) {

View File

@ -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;
}
@ -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) {

View File

@ -69,13 +69,14 @@ argi(N,I,I1) :-
I1 is I+1.
python_query( Caller, String ) :-
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)).

View File

@ -73,7 +73,7 @@ class Query:
raise StopIteration()
if self.q.next():
rc = self.q.answer
if self.q.port == "exit":
# if self.q.port == "exit":
return rc
else:
if self:
@ -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))