call fixes

This commit is contained in:
Vitor Santos Costa 2019-04-27 11:55:22 -05:00
parent 984cf8a07c
commit 54a222e5df
11 changed files with 251 additions and 185 deletions

View File

@ -1,4 +1,4 @@
/************************************************************************* * /************************************************************************* *
* YAP Prolog * * YAP Prolog *
* Yap Prolog was developed at NCCUP - Universidade do Porto * * Yap Prolog was developed at NCCUP - Universidade do Porto *
* * * *
@ -1749,7 +1749,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
dgi->p = P; dgi->p = P;
dgi->cp = CP; dgi->cp = CP;
dgi->b0 = LCL0 - (CELL *)B; dgi->b0 = LCL0 - (CELL *)B;
dgi->CurSlot = LOCAL_CurSlot; printf("%ld\n", dgi->CurSlot);
// ensure our current ENV receives current P. // ensure our current ENV receives current P.
Yap_PrepGoal(pe->ArityOfPE, nullptr, B PASS_REGS); Yap_PrepGoal(pe->ArityOfPE, nullptr, B PASS_REGS);
@ -1757,7 +1757,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 = LCL0 - (CELL *)B; dgi->b_entry = 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",
@ -1766,11 +1766,12 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
// fprintf(stderr,"EnterGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p // 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, // Slots=%d\n", out,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP,
// LOCAL_CurSlot); // LOCAL_CurSlot);
dgi->b = LCL0 - (CELL *)B; dgi->b_exit = LCL0 - (CELL *)B;
if (out) { if (out) {
dgi->EndSlot = LOCAL_CurSlot; dgi->EndSlot = LOCAL_CurSlot;
Yap_StartSlots(); Yap_StartSlots();
} else { } else {
printf("%ld\n", dgi->CurSlot);
LOCAL_CurSlot = LOCAL_CurSlot =
dgi->CurSlot; // ignore any slots created within the called goal dgi->CurSlot; // ignore any slots created within the called goal
} }
@ -1784,8 +1785,8 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) {
bool out; bool out;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
myB = (choiceptr)(LCL0 - dgi->b); myB = (choiceptr)(LCL0 - dgi->b_exit);
myB0 = (choiceptr)(LCL0 - dgi->b0); myB0 = (choiceptr)(LCL0 - dgi->b_entry);
CP = myB->cp_cp; CP = myB->cp_cp;
/* sanity check */ /* sanity check */
if (B >= myB0) { if (B >= myB0) {
@ -1804,8 +1805,9 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) {
out = Yap_exec_absmi(true, true ); out = Yap_exec_absmi(true, true );
if (out) { if (out) {
dgi->EndSlot = LOCAL_CurSlot; dgi->EndSlot = LOCAL_CurSlot;
dgi->b = LCL0 - (CELL *)B; dgi->b_exit = LCL0 - (CELL *)B;
} else { } else {
printf("F %ld\n", dgi->CurSlot);
LOCAL_CurSlot = LOCAL_CurSlot =
dgi->CurSlot; // ignore any slots created within the called goal dgi->CurSlot; // ignore any slots created within the called goal
} }
@ -1815,39 +1817,28 @@ 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, handler;
// fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%ld myB=%ld TR=%d // fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%ld myB=%ld TR=%d
// P=%p CP=%p Slots=%d\n", // P=%p CP=%p Slots=%d\n",
// successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP, // successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP,
// LOCAL_CurSlot); // LOCAL_CurSlot);
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
myB = (choiceptr)(LCL0 - dgi->b);
if (LOCAL_PrologMode & AsyncIntMode) {
Yap_signal(YAP_FAIL_SIGNAL);
}
handler = B;
while (handler &&
LCL0 - LOCAL_CBorder > (CELL *)handler
//&& handler->cp_ap != NOCODE
&& handler->cp_b != NULL && handler != myB) {
if (handler < myB) {
handler->cp_ap = TRUSTFAILCODE;
}
B = handler;
handler = handler->cp_b;
if (successful) { if (successful) {
choiceptr nB = (choiceptr)(LCL0 - dgi->b_entry);
if (B <= nB) {
B = nB;
}
Yap_TrimTrail(); Yap_TrimTrail();
} else if (!(LOCAL_PrologMode & AsyncIntMode)) { B = B->cp_b;
P = FAILCODE; } else if (LOCAL_PrologMode & AsyncIntMode) {
Yap_exec_absmi(true, YAP_EXEC_ABSMI);
}
}
if (LOCAL_PrologMode & AsyncIntMode) {
Yap_signal(YAP_FAIL_SIGNAL); Yap_signal(YAP_FAIL_SIGNAL);
} }
B = (choiceptr)(LCL0 - dgi->b0);
P = dgi->p; P = dgi->p;
CP = dgi->cp; CP = dgi->cp;
LOCAL_CurSlot =
dgi->CurSlot; // ignore any slots created within the called goal
printf("L %ld\n", dgi->CurSlot);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
// fprintf(stderr,"LeftGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p // 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, // Slots=%d\n", successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P,
@ -1942,7 +1933,7 @@ X_API CELL *YAP_HeapStoreOpaqueTerm(Term t) {
X_API Int YAP_RunGoalOnce(Term t) { X_API Int YAP_RunGoalOnce(Term t) {
CACHE_REGS CACHE_REGS
Term out; Term out;
yamop *old_CP = CP; yamop *old_CP = CP, *old_P = P;
Int oldPrologMode = LOCAL_PrologMode; Int oldPrologMode = LOCAL_PrologMode;
yhandle_t CSlot; yhandle_t CSlot;
@ -1989,10 +1980,10 @@ X_API Int YAP_RunGoalOnce(Term t) {
ASP = B->cp_env; ASP = B->cp_env;
ENV = (CELL *)ASP[E_E]; ENV = (CELL *)ASP[E_E];
B = (choiceptr)ASP[E_CB]; B = (choiceptr)ASP[E_CB];
#ifdef DEPTH_LIMITxs #ifdef DEPTH_LIMIT
DEPTH = ASP[E_DEPTH]; DEPTH = ASP[E_DEPTH];
#endif #endif
P = (yamop *)ASP[E_CP]; P = old_P;
CP = old_CP; CP = old_CP;
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
@ -2078,7 +2069,7 @@ X_API void YAP_PruneGoal(YAP_dogoalinfo *gi) {
CACHE_REGS CACHE_REGS
BACKUP_B(); BACKUP_B();
choiceptr myB = (choiceptr)(LCL0 - gi->b); choiceptr myB = (choiceptr)(LCL0 - gi->b_entry);
while (B != myB) { while (B != myB) {
/* make sure we prune C-choicepoints */ /* make sure we prune C-choicepoints */
if (POP_CHOICE_POINT(B->cp_b)) { if (POP_CHOICE_POINT(B->cp_b)) {

View File

@ -804,11 +804,11 @@ static Int execute_in_mod(USES_REGS1) { /* '$execute'(Goal) */
static void prune_inner_computation(choiceptr parent) { static void prune_inner_computation(choiceptr parent) {
/* code */ /* code */
choiceptr cut_pt; choiceptr cut_pt;
yamop *oP = P, *oCP = CP;
Int oENV = LCL0 - ENV;
cut_pt = B; cut_pt = B;
while (cut_pt && cut_pt->cp_b < parent) { while (cut_pt && cut_pt->cp_b < parent) {
if (cut_pt->cp_ap == NOCODE)
break;
cut_pt = cut_pt->cp_b; cut_pt = cut_pt->cp_b;
} }
if (!cut_pt) if (!cut_pt)
@ -819,9 +819,6 @@ static void prune_inner_computation(choiceptr parent) {
B = cut_pt; B = cut_pt;
Yap_TrimTrail(); Yap_TrimTrail();
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
P = oP;
CP = oCP;
ENV = LCL0 - oENV;
B = parent; B = parent;
} }
@ -1062,8 +1059,6 @@ static Int cleanup_on_exit(USES_REGS1) {
complete_pt[0] = TermExit; complete_pt[0] = TermExit;
} }
Yap_ignore(cleanup, false); Yap_ignore(cleanup, false);
if (B0->cp_ap == NOCODE)
B0->cp_ap = TRUSTFAILCODE;
if (Yap_RaiseException()) { if (Yap_RaiseException()) {
return false; return false;
} }

View File

@ -1,4 +1,4 @@
/************************************************************************** /*************************************************************************
* * * *
* YAP Prolog * * YAP Prolog *
* * * *
@ -4191,7 +4191,7 @@ call_gc(UInt gc_lim, Int predarity, CELL *current_env, yamop *nextop USES_REGS)
int gc_on = FALSE, gc_t = FALSE; int gc_on = FALSE, gc_t = FALSE;
if (Yap_GetValue(AtomGc) != TermNil) if (Yap_GetValue(AtomGc) != TermNil)
gc_on = TRUE; gc_on = false;
if (IsIntegerTerm(Tgc_margin = Yap_GetValue(AtomGcMargin)) && if (IsIntegerTerm(Tgc_margin = Yap_GetValue(AtomGcMargin)) &&
gc_margin > 0) { gc_margin > 0) {
gc_margin = (UInt)IntegerOfTerm(Tgc_margin); gc_margin = (UInt)IntegerOfTerm(Tgc_margin);
@ -4343,3 +4343,4 @@ Yap_inc_mark_variable()
CACHE_REGS CACHE_REGS
LOCAL_total_marked++; LOCAL_total_marked++;
} }

View File

@ -411,6 +411,23 @@ std::vector<Term> YAPPairTerm::listToArray() {
return o; return o;
} }
std::vector<YAPTerm> YAPPairTerm::listToVector() {
Term *tailp;
Term t1 = gt();
Int l = Yap_SkipList(&t1, &tailp);
if (l < 0) {
throw YAPError(SOURCE(), TYPE_ERROR_LIST, (t), nullptr);
}
std::vector<YAPTerm> o = *new std::vector<YAPTerm>(l);
int i = 0;
Term t = gt();
while (t != TermNil) {
o[i++] = YAPTerm(HeadOfTerm(t));
t = TailOfTerm(t);
}
return o;
}
YAP_tag_t YAPTerm::tag() { YAP_tag_t YAPTerm::tag() {
Term tt = gt(); Term tt = gt();
if (IsVarTerm(tt)) { if (IsVarTerm(tt)) {
@ -576,6 +593,8 @@ bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) {
// don't forget, on success these bindings will still be there); // don't forget, on success these bindings will still be there);
result = YAP_LeaveGoal(true, &q); result = YAP_LeaveGoal(true, &q);
Int oenv = LCL0-ENV;
Int oB = LCL0-CellPtr(B);
YAPCatchError(); YAPCatchError();
Yap_CloseHandles(q.CurSlot); Yap_CloseHandles(q.CurSlot);
@ -599,6 +618,8 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
q.CurSlot = Yap_StartSlots(); q.CurSlot = Yap_StartSlots();
q.p = P; q.p = P;
q.cp = CP; q.cp = CP;
Int oenv = LCL0-ENV;
Int oB = LCL0-CellPtr(B);
Term omod = CurrentModule; Term omod = CurrentModule;
PredEntry *ap = nullptr; PredEntry *ap = nullptr;
if (IsStringTerm(tmod)) if (IsStringTerm(tmod))
@ -625,9 +646,10 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
//__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec "); //__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
result = (bool)YAP_EnterGoal(ap, nullptr, &q); result = (bool)YAP_EnterGoal(ap, nullptr, &q);
// std::cerr << "mgoal " << YAPTerm(tmod).text() << ":" << YAPTerm(t).text() << "\n"; // std::cerr << "mgoal " << YAPTerm(tmod).text() << ":" << YAPTerm(t).text() << "\n
YAP_LeaveGoal(result && !release, &q); YAP_LeaveGoal(result && !release, &q);
ENV = LCL0-oenv;
B = (choiceptr)(LCL0-oB);
CurrentModule = LOCAL_SourceModule = omod; CurrentModule = LOCAL_SourceModule = omod;
// PyEval_RestoreThread(_save); // PyEval_RestoreThread(_save);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
@ -652,7 +674,12 @@ Term YAPEngine::fun(Term t) {
arity_t arity; arity_t arity;
Functor f; Functor f;
Atom name; Atom name;
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
Int oenv = LCL0-ENV;
Int oB = LCL0-CellPtr(B);
if (IsApplTerm(t)) { if (IsApplTerm(t)) {
ts = RepAppl(t) + 1; ts = RepAppl(t) + 1;
f = (Functor)ts[-1]; f = (Functor)ts[-1];
@ -684,9 +711,6 @@ Term YAPEngine::fun(Term t) {
Term g = (Yap_MkApplTerm(f, arity, ts)); Term g = (Yap_MkApplTerm(f, arity, ts));
ap = rewriteUndefEngineQuery(ap, g, (ap->ModuleOfPred)); ap = rewriteUndefEngineQuery(ap, g, (ap->ModuleOfPred));
} }
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
// make sure this is safe // make sure this is safe
// allow Prolog style exception handling // allow Prolog style exception handling
//__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec "); //__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
@ -699,6 +723,8 @@ Term YAPEngine::fun(Term t) {
YAPCatchError(); YAPCatchError();
{ {
YAP_LeaveGoal(result, &q); YAP_LeaveGoal(result, &q);
ENV = LCL0-oenv;
B = (choiceptr)(LCL0-oB);
// PyEval_RestoreThread(_save); // PyEval_RestoreThread(_save);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return ot; return ot;
@ -727,6 +753,27 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, Term ts[])
: YAPPredicate(f, mod) {
/* ignore flags for now */
BACKUP_MACHINE_REGS();
Term goal;
if (ts) {
size_t arity = f.arity();
goal = Yap_MkApplTerm(Yap_MkFunctor(f.name().asAtom(),arity), arity, ts);
nts = RepAppl(goal) + 1;
for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = ts[i];
} else {
goal = MkVarTerm();
}
openQuery();
names = YAPPairTerm(TermNil);
RECOVER_MACHINE_REGS();
}
#if 0 #if 0
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) { YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) {
/* ignore flags for now */ /* ignore flags for now */
@ -766,6 +813,9 @@ 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";
q_h.CurSlot = Yap_StartSlots();
q_h.p = P;
q_h.cp = CP;
sigjmp_buf buf, *oldp = LOCAL_RestartEnv; sigjmp_buf buf, *oldp = LOCAL_RestartEnv;
e = nullptr; e = nullptr;
@ -827,7 +877,7 @@ bool YAPQuery::deterministic() {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (!q_open || q_state == 0) if (!q_open || q_state == 0)
return false; return false;
choiceptr myB = (choiceptr)(LCL0 - q_h.b); choiceptr myB = (choiceptr)(LCL0 - q_h.b_entry);
return (B >= myB); return (B >= myB);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -1100,6 +1150,7 @@ void YAPEngine::reSet() {
free(LOCAL_CommittedError); free(LOCAL_CommittedError);
LOCAL_CommittedError = NULL; LOCAL_CommittedError = NULL;
} }
LOCAL_CurSlot = q.CurSlot;
} }
Term YAPEngine::top_level(std::string s) { Term YAPEngine::top_level(std::string s) {

View File

@ -91,7 +91,6 @@ typedef YAP_Term Term;
#define CellSize sizeof(CELL) #define CellSize sizeof(CELL)
#define SmallSize sizeof(SMALLUNSGN) #define SmallSize sizeof(SMALLUNSGN)
typedef YAP_Int Int;
typedef YAP_Float Float; typedef YAP_Float Float;
typedef YAP_handle_t yhandle_t; typedef YAP_handle_t yhandle_t;

View File

@ -138,12 +138,13 @@ typedef enum {
/* this should be opaque to the user */ /* this should be opaque to the user */
typedef struct { typedef struct {
unsigned long b, b0; //> choice-point at entry unsigned long b0, b_entry, b_exit; //> choice-point at entry
YAP_handle_t CurSlot; //> variables at entry YAP_handle_t CurSlot; //> variables at entry
YAP_handle_t EndSlot; //> variables at successful execution YAP_handle_t EndSlot; //> variables at successful execution
struct yami *p; //> Program Counter at entry struct yami *p; //> Program Counter at entry
struct yami *cp; //> Continuation PC at entry struct yami *cp; //> Continuation PC at entry
int lvl; int lvl;
long env;
unsigned long tr, h; unsigned long tr, h;
} YAP_dogoalinfo; } YAP_dogoalinfo;

View File

@ -73,7 +73,8 @@ Yap_Error__(false, __FILE__, __FUNCTION__, __LINE__, id, TermNil, __VA_ARGS__)
{ if ( (TF = Yap_ensure_atom__(__FILE__, __FUNCTION__, __LINE__, T0 ) == 0L ) return false; \ { if ( (TF = Yap_ensure_atom__(__FILE__, __FUNCTION__, __LINE__, T0 ) == 0L ) return false; \
} }
INLINE_ONLY Term Yap_ensure_atom__(const char *fu, const char *fi, int line, //INLINE_ONLY
static Term Yap_ensure_atom__(const char *fu, const char *fi, int line,
Term in) { Term in) {
Term t = Deref(in); Term t = Deref(in);
// Term Context = Deref(ARG2); // Term Context = Deref(ARG2);

View File

@ -76,15 +76,15 @@ static Term python_to_term__(PyObject *pVal) {
t[1] = MkFloatTerm(PyComplex_ImagAsDouble(pVal)); t[1] = MkFloatTerm(PyComplex_ImagAsDouble(pVal));
return Yap_MkApplTerm(FunctorI, 2, t); return Yap_MkApplTerm(FunctorI, 2, t);
} }
else if (PyUnicode_Check(pVal)) { else if (PyUnicode_Check(pVal)) {
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
size_t sz = PyUnicode_GetSize(pVal) + 1; size_t sz = PyUnicode_GetSize(pVal) + 1;
wchar_t *s = malloc(sizeof(wchar_t) * sz); wchar_t *s = malloc(sizeof(wchar_t) * sz);
sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, a, sz - 1); sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, a, sz - 1);
free(ptr); free(ptr);
#else #else
const char *s = PyUnicode_AsUTF8(pVal); const char *s = PyUnicode_AsUTF8(pVal);
#endif #endif
#if 0 #if 0
if (false && Yap_AtomInUse(s)) if (false && Yap_AtomInUse(s))
@ -92,72 +92,72 @@ else if (PyUnicode_Check(pVal)) {
else else
#endif #endif
if (pyStringToString) if (pyStringToString)
return MkStringTerm(s); return MkStringTerm(s);
else else
return MkAtomTerm(Yap_LookupAtom(s)); return MkAtomTerm(Yap_LookupAtom(s));
} }
else if (PyByteArray_Check(pVal)) { else if (PyByteArray_Check(pVal)) {
return MkStringTerm(PyByteArray_AsString(pVal)); return MkStringTerm(PyByteArray_AsString(pVal));
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
} }
else if (PyString_Check(pVal)) { else if (PyString_Check(pVal)) {
return MkStringTerm(PyString_AsString(pVal)); return MkStringTerm(PyString_AsString(pVal));
#endif #endif
} }
else if (PyTuple_Check(pVal)) { else if (PyTuple_Check(pVal)) {
Py_ssize_t sz = PyTuple_Size(pVal); Py_ssize_t sz = PyTuple_Size(pVal);
const char *s; const char *s;
s = Py_TYPE(pVal)->tp_name; s = Py_TYPE(pVal)->tp_name;
if (s == NULL) if (s == NULL)
s = "t"; s = "t";
if (sz == 0) { if (sz == 0) {
return MkAtomTerm(YAP_LookupAtom(Py_TYPE(pVal)->tp_name)); return MkAtomTerm(YAP_LookupAtom(Py_TYPE(pVal)->tp_name));
}
else {
Functor f = Yap_MkFunctor(Yap_LookupAtom(s), sz);
Term t = Yap_MkNewApplTerm(f, sz);
long i;
CELL *ptr = RepAppl(t) + 1;
for (i = 0; i < sz; i++) {
PyObject *p = PyTuple_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
return false;
} }
*ptr++ = python_to_term__(p); else {
Functor f = Yap_MkFunctor(Yap_LookupAtom(s), sz);
Term t = Yap_MkNewApplTerm(f, sz);
long i;
CELL *ptr = RepAppl(t) + 1;
for (i = 0; i < sz; i++) {
PyObject *p = PyTuple_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
return false;
}
*ptr++ = python_to_term__(p);
}
return t;
}
// PL_reset_term_refs(to);
// fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// ||***\n",stderr);
} }
return t; else if (PyList_Check(pVal)) {
} Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
// PL_reset_term_refs(to); if (sz == 0)
// fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" return repr_term(pVal);
// ||***\n",stderr); Term t = TermNil;
} for (i = sz; i > 0; ) {
else if (PyList_Check(pVal)) { -- i;
Py_ssize_t i, sz = PyList_GET_SIZE(pVal); PyObject *p = PyList_GetItem(pVal, i);
if (sz == 0) if (p == NULL) {
return repr_term(pVal); PyErr_Clear();
Term t = TermNil; return false;
for (i = sz; i > 0; ) { }
-- i; if (!python_to_term__(p))
PyObject *p = PyList_GetItem(pVal, i); return false;
if (p == NULL) {
PyErr_Clear();
return false;
}
if (!python_to_term__(p))
return false;
t = MkPairTerm(python_to_term__(p), t); t = MkPairTerm(python_to_term__(p), t);
}
return t;
} }
return t; else if (PyDict_Check(pVal)) {
} Py_ssize_t pos = 0, tot = PyDict_Size(pVal);
else if (PyDict_Check(pVal)) { PyObject *key, *value;
Py_ssize_t pos = 0, tot = PyDict_Size(pVal); Term f, *opt = &f, t, to;
PyObject *key, *value; if (tot == 0)
Term f, *opt = &f, t, to; return MkAtomTerm( Yap_LookupAtom("{}"));
if (tot == 0) while (PyDict_Next(pVal, &pos, &key, &value)) {
return MkAtomTerm( Yap_LookupAtom("{}"));
while (PyDict_Next(pVal, &pos, &key, &value)) {
Term t0[2]; Term t0[2];
t0[0] = python_to_term__(key); t0[0] = python_to_term__(key);
t0[1] = python_to_term__(value); t0[1] = python_to_term__(value);
@ -169,9 +169,9 @@ else if (PyDict_Check(pVal)) {
*opt = t; *opt = t;
opt = pt+1; opt = pt+1;
} else { } else {
if (pos == 0) { if (pos == 0) {
return repr_term(pVal); return repr_term(pVal);
} }
*opt = to; *opt = to;
break; break;
@ -179,7 +179,7 @@ else if (PyDict_Check(pVal)) {
} }
return Yap_MkApplTerm(FunctorBraces, 1, &f); return Yap_MkApplTerm(FunctorBraces, 1, &f);
} }
return repr_term(pVal); return repr_term(pVal);
} }
@ -197,11 +197,13 @@ X_API YAP_Term pythonToYAP(PyObject *pVal) {
/* fputs("***>>\n", stderr); */ /* fputs("***>>\n", stderr); */
if (pVal == NULL) if (pVal == NULL)
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, 0, NULL); Yap_ThrowError(SYSTEM_ERROR_INTERNAL, 0, NULL);
yhandle_t h0 = Yap_CurrentHandle();
Term t = python_to_term__(pVal); Term t = python_to_term__(pVal);
/* fputs("<< *** ", stderr); */ /* fputs("<< *** ", stderr); */
/* Yap_DebugPlWrite(t); */ /* Yap_DebugPlWrite(t); */
/* fputs(" ***\n", stderr); */ /* fputs(" ***\n", stderr); */
// Py_DECREF(pVal); // Py_DECREF(pVal);
Yap_CloseHandles(h0);
return t; return t;
} }
@ -225,13 +227,19 @@ PyObject *py_Local, *py_Global;
*python_assign. *python_assign.
*/ */
bool python_assign(term_t t, PyObject *exp, PyObject *context) { bool python_assign(term_t t, PyObject *exp, PyObject *context) {
PyErr_Print(); bool rc = true;
PyErr_Print();
term_t inp = Yap_CurrentHandle();
Yap_DebugPlWriteln(YAP_GetFromSlot(t));
context = find_obj(context, t, false); context = find_obj(context, t, false);
// Yap_DebugPlWriteln(yt); // Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) { switch (PL_term_type(t)) {
case PL_VARIABLE: { case PL_VARIABLE: {
if (context == NULL) // prevent a.V= N*N[N-1] if (context == NULL) // prevent a.V= N*N[N-1]
return python_to_term(exp, t); PL_reset_term_refs(inp);
rc = python_to_term(exp, t);
break;
} }
case PL_STRING: { case PL_STRING: {
@ -240,24 +248,34 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
PL_get_string_chars(t, &s,&l); PL_get_string_chars(t, &s,&l);
if (!context) if (!context)
context = py_Main; context = py_Main;
if (PyObject_SetAttrString(context, s, exp) == 0) if (PyObject_SetAttrString(context, s, exp) == 0) {
return true; PL_reset_term_refs(inp);
PyErr_Print(); ;
return false; rc = true;
} else {
PyErr_Print();
PL_reset_term_refs(inp);
rc = false;
}
break;
} }
case PL_ATOM: { case PL_ATOM: {
char *s = NULL; char *s = NULL;
PL_get_atom_chars(t, &s); PL_get_atom_chars(t, &s);
if (!context) if (!context)
context = py_Main; context = py_Main;
if (PyObject_SetAttrString(context, s, exp) == 0) if (PyObject_SetAttrString(context, s, exp) == 0){
return true; rc = true;
return false; } else {
rc = false;
}
break;
} }
case PL_INTEGER: case PL_INTEGER:
case PL_FLOAT: case PL_FLOAT:
// domain or type erro? // domain or type erro?
return false; rc = false;
break;
default: { default: {
term_t tail = PL_new_term_ref(), arg = PL_new_term_ref(); term_t tail = PL_new_term_ref(), arg = PL_new_term_ref();
size_t len, i; size_t len, i;
@ -271,90 +289,98 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
PyObject *p; PyObject *p;
if (!PL_get_list(t, arg, t)) { if (!PL_get_list(t, arg, t)) {
PL_reset_term_refs(tail);
p = Py_None; p = Py_None;
} rc = false;
if ((p = PySequence_GetItem(exp, i)) == NULL) } else {
p = Py_None; if ((p = PySequence_GetItem(exp, i)) == NULL)
if (!python_assign(arg, p, context)) { p = Py_None;
PL_reset_term_refs(tail); rc = rc && python_assign(arg, p, context);
} }
} }
} else {
while(PL_is_pair(t)) {
context = find_obj(context, t, false);
}
rc = python_assign(t, exp, context);
} }
} else { } else {
functor_t fun; functor_t fun;
if (!PL_get_functor(t, &fun)) { if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail); rc = false;
return false;
} }
if (fun == FUNCTOR_sqbrackets2) { if (fun == FUNCTOR_sqbrackets2) {
// tail is the object o // tail is the object o
if (!PL_get_arg(2, t, tail)) { if (!PL_get_arg(2, t, tail)) {
PL_reset_term_refs(tail); rc = false;
return false; } else {
} PyObject *o = term_to_python(tail, true, context, false);
PyObject *o = term_to_python(tail, true, context, false); // t now refers to the index
// t now refers to the index if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) ||
if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) || !PL_get_nil(tail)) {
!PL_get_nil(tail)) { PL_reset_term_refs(i);
PL_reset_term_refs(tail); ;
return false; rc = false;
} } else {
PyObject *i = term_to_python(t, true, NULL, false); PyObject *i = term_to_python(t, true, NULL, false);
// check numeric // check numeric
if (PySequence_Check(o) && PyLong_Check(i)) { if (PySequence_Check(o) && PyLong_Check(i)) {
long int j; long int j;
j = PyLong_AsLong(i); j = PyLong_AsLong(i);
return PySequence_SetItem(o, j, exp) == 0; rc = PySequence_SetItem(o, j, exp) == 0;
} }
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
if (PySequence_Check(o) && PyInt_Check(i)) { if (PySequence_Check(o) && PyInt_Check(i)) {
long int j; long int j;
j = PyInt_AsLong(i); j = PyInt_AsLong(i);
return PySequence_SetItem(o, i, exp) == 0; szzb rc = PySequence_SetItem(o, i, exp) == 0;
} } else
#endif #endif
if (PyDict_Check(o)) { if (PyDict_Check(o)) {
if (PyDict_SetItem(o, i, exp) == 0) { if (PyDict_SetItem(o, i, exp) == 0) {
return true; PL_reset_term_refs(inp);
} ;
} rc = true;
if (PyObject_SetAttr(o, i, exp) == 0) { }
return true; }
} if (PyObject_SetAttr(o, i, exp) == 0) {
PL_reset_term_refs(inp);
;
rc = true;
}
}
}
} else { } else {
atom_t s; atom_t s;
int n, i; int n, i;
PL_get_name_arity(t, &s, &n); PL_get_name_arity(t, &s, &n);
PyObject *o = term_to_python(t, true, context, true); PyObject *o = term_to_python(t, true, context, true);
PyErr_Print(); PyErr_Print();
if (PySequence_Check(o) && PySequence_Length(o) == n) { if (PySequence_Check(o) && PySequence_Length(o) == n) {
for (i = 1; i <= n; i++) { for (i = 1; i <= n; i++) {
PyObject *p; PyObject *p;
if (!PL_get_arg(i, t, arg)) { if (!PL_get_arg(i, t, arg)) {
PL_reset_term_refs(tail); o = NULL;
o = false;
p = Py_None; p = Py_None;
} }
if ((p = PySequence_GetItem(exp, i - 1)) == NULL) if ((p = PySequence_GetItem(exp, i - 1)) == NULL)
p = Py_None; p = Py_None;
else if (!python_assign(arg, p, NULL)) { rc = python_assign(arg, p, NULL);
PL_reset_term_refs(tail);
o = NULL;
} else {
PyErr_Print();
}
} }
return true;
} }
} }
} }
PL_reset_term_refs(tail);
} }
PyErr_Print();
return NULL;
} }
PyErr_Print();
PL_reset_term_refs(inp);
return rc;
} }

View File

@ -112,12 +112,12 @@ find_obj(PyObject *ob, term_t l, bool eval) {
YAP_Term hd, yt; YAP_Term hd, yt;
py_Context = NULL; py_Context = NULL;
yt = YAP_GetFromSlot(l);
// Yap_DebugPlWriteln(yt); // Yap_DebugPlWriteln(yt);
while (YAP_IsPairTerm(yt)) { if (l == 0)
return Py_None;
while (YAP_IsPairTerm((yt = YAP_GetFromSlot(l)))) {
hd = YAP_HeadOfTerm(yt); hd = YAP_HeadOfTerm(yt);
yt = YAP_TailOfTerm(yt); Yap_PutInHandle(l, YAP_TailOfTerm(yt));
YAP_PutInSlot(l, yt);
ob = yap_to_python(hd, true, ob, false); ob = yap_to_python(hd, true, ob, false);
if (!ob) { if (!ob) {
return Py_None; return Py_None;

View File

@ -113,7 +113,6 @@ static foreign_t python_is(term_t tobj, term_t tf) {
static foreign_t python_proc(term_t tobj) { static foreign_t python_proc(term_t tobj) {
PyObject *o; PyObject *o;
term_t lim = python_acquire_GIL(); term_t lim = python_acquire_GIL();
o = term_to_python(tobj, true, NULL, true); o = term_to_python(tobj, true, NULL, true);
@ -237,6 +236,7 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
} }
static foreign_t assign_python(term_t exp, term_t name) { static foreign_t assign_python(term_t exp, term_t name) {
printf("A %ld\n", LOCAL_CurSlot);
term_t stackp = python_acquire_GIL(); term_t stackp = python_acquire_GIL();
PyObject *e = term_to_python(exp, true, NULL, true); PyObject *e = term_to_python(exp, true, NULL, true);
@ -245,6 +245,7 @@ static foreign_t assign_python(term_t exp, term_t name) {
pyErrorAndReturn(false); pyErrorAndReturn(false);
} }
bool b = python_assign(name, e, NULL); bool b = python_assign(name, e, NULL);
printf("B %ld\n", LOCAL_CurSlot);
python_release_GIL(stackp); python_release_GIL(stackp);
pyErrorAndReturn(b); pyErrorAndReturn(b);
} }

View File

@ -133,7 +133,7 @@ undefined_query(G0, M0, Cut) :-
% we found an import, and call again % we found an import, and call again
% we have user code in the unknown_predicate % we have user code in the unknown_predicate
% we fail, output a message, and just generate an exception. % we fail, output a message, and just generate an exception.
'$undefp'([M0|G0],ok) :- '$undefp'([M0|G0],_) :-
'$search_undef'(M0:G0, M:G), '$search_undef'(M0:G0, M:G),
'$trace'(M:G). '$trace'(M:G).