call fixes
This commit is contained in:
		| @@ -1,4 +1,4 @@ | ||||
| /************************************************************************* * | ||||
| /*************************************************************************  * | ||||
|  *	 YAP Prolog 							 * | ||||
|  *	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->cp = CP; | ||||
|   dgi->b0 = LCL0 - (CELL *)B; | ||||
|   dgi->CurSlot = LOCAL_CurSlot; | ||||
|   printf("%ld\n", dgi->CurSlot); | ||||
|   // ensure our current ENV receives current P. | ||||
|  | ||||
|   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 | ||||
|   // slot=%d", pe, pe->CodeOfPred->opc, FAILCODE, Deref(ARG1), Deref(ARG2), | ||||
|   // LOCAL_CurSlot); | ||||
|   dgi->b = LCL0 - (CELL *)B; | ||||
|   dgi->b_entry = 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", | ||||
| @@ -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 | ||||
|   //   Slots=%d\n", out,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, | ||||
|   //   LOCAL_CurSlot); | ||||
|   dgi->b = LCL0 - (CELL *)B; | ||||
|   dgi->b_exit = LCL0 - (CELL *)B; | ||||
|   if (out) { | ||||
|     dgi->EndSlot = LOCAL_CurSlot; | ||||
|     Yap_StartSlots(); | ||||
|   } else { | ||||
|   printf("%ld\n", dgi->CurSlot); | ||||
|     LOCAL_CurSlot = | ||||
|         dgi->CurSlot; // ignore any slots created within the called goal | ||||
|   } | ||||
| @@ -1784,8 +1785,8 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { | ||||
|   bool out; | ||||
|  | ||||
|   BACKUP_MACHINE_REGS(); | ||||
|   myB = (choiceptr)(LCL0 - dgi->b); | ||||
|   myB0 = (choiceptr)(LCL0 - dgi->b0); | ||||
|   myB = (choiceptr)(LCL0 - dgi->b_exit); | ||||
|   myB0 = (choiceptr)(LCL0 - dgi->b_entry); | ||||
|   CP = myB->cp_cp; | ||||
|   /* sanity check */ | ||||
|   if (B >= myB0) { | ||||
| @@ -1804,8 +1805,9 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { | ||||
|   out = Yap_exec_absmi(true, true   ); | ||||
|   if (out) { | ||||
|     dgi->EndSlot = LOCAL_CurSlot; | ||||
|     dgi->b = LCL0 - (CELL *)B; | ||||
|     dgi->b_exit = LCL0 - (CELL *)B; | ||||
|   } else { | ||||
|   printf("F %ld\n", dgi->CurSlot); | ||||
|     LOCAL_CurSlot = | ||||
|         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) { | ||||
|   CACHE_REGS | ||||
|   choiceptr myB, handler; | ||||
|  | ||||
|   //   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 (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) { | ||||
|       choiceptr nB = (choiceptr)(LCL0 - dgi->b_entry); | ||||
|       if (B <= nB) { | ||||
|         B = nB; | ||||
|       } | ||||
|       Yap_TrimTrail(); | ||||
|     } else if (!(LOCAL_PrologMode & AsyncIntMode)) { | ||||
|       P = FAILCODE; | ||||
|       Yap_exec_absmi(true, YAP_EXEC_ABSMI); | ||||
|     } | ||||
|   } | ||||
|   if (LOCAL_PrologMode & AsyncIntMode) { | ||||
|       B = B->cp_b; | ||||
|     } else if (LOCAL_PrologMode & AsyncIntMode) { | ||||
|     Yap_signal(YAP_FAIL_SIGNAL); | ||||
|   } | ||||
|   B = (choiceptr)(LCL0 - dgi->b0); | ||||
|   P = dgi->p; | ||||
|   CP = dgi->cp; | ||||
|     LOCAL_CurSlot = | ||||
|         dgi->CurSlot; // ignore any slots created within the called goal | ||||
|   printf("L %ld\n", 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",    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) { | ||||
|   CACHE_REGS | ||||
|   Term out; | ||||
|   yamop *old_CP = CP; | ||||
|   yamop *old_CP = CP, *old_P = P; | ||||
|   Int oldPrologMode = LOCAL_PrologMode; | ||||
|   yhandle_t CSlot; | ||||
|  | ||||
| @@ -1989,10 +1980,10 @@ X_API Int YAP_RunGoalOnce(Term t) { | ||||
|   ASP = B->cp_env; | ||||
|   ENV = (CELL *)ASP[E_E]; | ||||
|   B = (choiceptr)ASP[E_CB]; | ||||
| #ifdef DEPTH_LIMITxs | ||||
| #ifdef DEPTH_LIMIT | ||||
|   DEPTH = ASP[E_DEPTH]; | ||||
| #endif | ||||
|   P = (yamop *)ASP[E_CP]; | ||||
|  P = old_P; | ||||
|   CP = old_CP; | ||||
|   LOCAL_AllowRestart = FALSE; | ||||
|   RECOVER_MACHINE_REGS(); | ||||
| @@ -2078,7 +2069,7 @@ X_API void YAP_PruneGoal(YAP_dogoalinfo *gi) { | ||||
|   CACHE_REGS | ||||
|   BACKUP_B(); | ||||
|  | ||||
|   choiceptr myB = (choiceptr)(LCL0 - gi->b); | ||||
|   choiceptr myB = (choiceptr)(LCL0 - gi->b_entry); | ||||
|   while (B != myB) { | ||||
|     /* make sure we prune C-choicepoints */ | ||||
|     if (POP_CHOICE_POINT(B->cp_b)) { | ||||
|   | ||||
							
								
								
									
										9
									
								
								C/exec.c
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								C/exec.c
									
									
									
									
									
								
							| @@ -804,11 +804,11 @@ static Int execute_in_mod(USES_REGS1) { /* '$execute'(Goal)	 */ | ||||
| static void prune_inner_computation(choiceptr parent) { | ||||
|   /* code */ | ||||
|   choiceptr cut_pt; | ||||
|   yamop *oP = P, *oCP = CP; | ||||
|   Int oENV = LCL0 - ENV; | ||||
|  | ||||
|   cut_pt = B; | ||||
|   while (cut_pt && cut_pt->cp_b < parent) { | ||||
|     if (cut_pt->cp_ap == NOCODE) | ||||
|       break; | ||||
|     cut_pt = cut_pt->cp_b; | ||||
|   } | ||||
|   if (!cut_pt) | ||||
| @@ -819,9 +819,6 @@ static void prune_inner_computation(choiceptr parent) { | ||||
|   B = cut_pt; | ||||
|   Yap_TrimTrail(); | ||||
|   LOCAL_AllowRestart = FALSE; | ||||
|   P = oP; | ||||
|   CP = oCP; | ||||
|   ENV = LCL0 - oENV; | ||||
|   B = parent; | ||||
| } | ||||
|  | ||||
| @@ -1062,8 +1059,6 @@ static Int cleanup_on_exit(USES_REGS1) { | ||||
|     complete_pt[0] = TermExit; | ||||
|   } | ||||
|   Yap_ignore(cleanup, false); | ||||
|   if (B0->cp_ap == NOCODE) | ||||
|     B0->cp_ap = TRUSTFAILCODE; | ||||
|   if (Yap_RaiseException()) { | ||||
|     return false; | ||||
|   } | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| /************************************************************************** | ||||
| /************************************************************************* | ||||
| *									 * | ||||
| *	 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; | ||||
|  | ||||
|   if (Yap_GetValue(AtomGc) != TermNil) | ||||
|     gc_on = TRUE; | ||||
|     gc_on = false; | ||||
|   if (IsIntegerTerm(Tgc_margin = Yap_GetValue(AtomGcMargin)) && | ||||
|       gc_margin > 0) { | ||||
|     gc_margin = (UInt)IntegerOfTerm(Tgc_margin); | ||||
| @@ -4343,3 +4343,4 @@ Yap_inc_mark_variable() | ||||
|   CACHE_REGS | ||||
|     LOCAL_total_marked++; | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										63
									
								
								CXX/yapi.cpp
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								CXX/yapi.cpp
									
									
									
									
									
								
							| @@ -411,6 +411,23 @@ std::vector<Term> YAPPairTerm::listToArray() { | ||||
|   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() { | ||||
|   Term tt = gt(); | ||||
|   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); | ||||
|   result = YAP_LeaveGoal(true, &q); | ||||
|  | ||||
|  Int oenv = LCL0-ENV; | ||||
|  Int oB = LCL0-CellPtr(B); | ||||
|   YAPCatchError(); | ||||
|  | ||||
|   Yap_CloseHandles(q.CurSlot); | ||||
| @@ -599,6 +618,8 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) { | ||||
|   q.CurSlot = Yap_StartSlots(); | ||||
|   q.p = P; | ||||
|   q.cp = CP; | ||||
|  Int oenv = LCL0-ENV; | ||||
|  Int oB = LCL0-CellPtr(B); | ||||
|   Term omod = CurrentModule; | ||||
|   PredEntry *ap = nullptr; | ||||
|   if (IsStringTerm(tmod)) | ||||
| @@ -625,9 +646,10 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) { | ||||
|   //__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec  "); | ||||
|  | ||||
|    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); | ||||
|  ENV = LCL0-oenv; | ||||
|  B = (choiceptr)(LCL0-oB); | ||||
|   CurrentModule = LOCAL_SourceModule = omod; | ||||
|   //      PyEval_RestoreThread(_save); | ||||
|   RECOVER_MACHINE_REGS(); | ||||
| @@ -652,7 +674,12 @@ Term YAPEngine::fun(Term t) { | ||||
|   arity_t arity; | ||||
|   Functor f; | ||||
|   Atom name; | ||||
|   q.CurSlot = Yap_StartSlots(); | ||||
|   q.p = P; | ||||
|   q.cp = CP; | ||||
|  | ||||
|  Int oenv = LCL0-ENV; | ||||
|  Int oB = LCL0-CellPtr(B); | ||||
|   if (IsApplTerm(t)) { | ||||
|     ts = RepAppl(t) + 1; | ||||
|     f = (Functor)ts[-1]; | ||||
| @@ -684,9 +711,6 @@ Term YAPEngine::fun(Term t) { | ||||
|     Term g = (Yap_MkApplTerm(f, arity, ts)); | ||||
|     ap = rewriteUndefEngineQuery(ap, g, (ap->ModuleOfPred)); | ||||
|   } | ||||
|   q.CurSlot = Yap_StartSlots(); | ||||
|   q.p = P; | ||||
|   q.cp = CP; | ||||
|   // make sure this is safe | ||||
|   // allow Prolog style exception handling | ||||
|   //__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec  "); | ||||
| @@ -699,6 +723,8 @@ Term YAPEngine::fun(Term t) { | ||||
|     YAPCatchError(); | ||||
|   { | ||||
|     YAP_LeaveGoal(result, &q); | ||||
|  ENV = LCL0-oenv; | ||||
|  B = (choiceptr)(LCL0-oB); | ||||
|     //      PyEval_RestoreThread(_save); | ||||
|     RECOVER_MACHINE_REGS(); | ||||
|     return ot; | ||||
| @@ -727,6 +753,27 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[]) | ||||
|   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 | ||||
| YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) { | ||||
|   /* ignore flags for now */ | ||||
| @@ -766,6 +813,9 @@ bool YAPQuery::next() { | ||||
|   CACHE_REGS | ||||
|   bool result = false; | ||||
|   // 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; | ||||
|   e = nullptr; | ||||
| @@ -827,7 +877,7 @@ bool YAPQuery::deterministic() { | ||||
|   BACKUP_MACHINE_REGS(); | ||||
|   if (!q_open || q_state == 0) | ||||
|     return false; | ||||
|   choiceptr myB = (choiceptr)(LCL0 - q_h.b); | ||||
|   choiceptr myB = (choiceptr)(LCL0 - q_h.b_entry); | ||||
|   return (B >= myB); | ||||
|   RECOVER_MACHINE_REGS(); | ||||
| } | ||||
| @@ -1100,6 +1150,7 @@ void YAPEngine::reSet() { | ||||
|     free(LOCAL_CommittedError); | ||||
|     LOCAL_CommittedError = NULL; | ||||
|   } | ||||
|   LOCAL_CurSlot = q.CurSlot; | ||||
| } | ||||
|  | ||||
| Term YAPEngine::top_level(std::string s) { | ||||
|   | ||||
							
								
								
									
										1
									
								
								H/Yap.h
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								H/Yap.h
									
									
									
									
									
								
							| @@ -91,7 +91,6 @@ typedef YAP_Term Term; | ||||
| #define CellSize sizeof(CELL) | ||||
| #define SmallSize sizeof(SMALLUNSGN) | ||||
|  | ||||
| typedef YAP_Int Int; | ||||
| typedef YAP_Float Float; | ||||
| typedef YAP_handle_t yhandle_t; | ||||
|  | ||||
|   | ||||
| @@ -138,12 +138,13 @@ typedef enum { | ||||
|  | ||||
| /* this should be opaque to the user */ | ||||
| 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 EndSlot; //> variables at successful execution | ||||
|   struct yami *p;       //> Program Counter at entry | ||||
|   struct yami *cp;      //> Continuation PC at entry | ||||
|   int lvl; | ||||
|   long env; | ||||
|   unsigned long tr, h; | ||||
| } YAP_dogoalinfo; | ||||
|  | ||||
|   | ||||
| @@ -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; \ | ||||
|   } | ||||
|  | ||||
| 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 t = Deref(in); | ||||
|   // Term Context = Deref(ARG2); | ||||
|   | ||||
| @@ -76,15 +76,15 @@ static Term python_to_term__(PyObject *pVal) { | ||||
|     t[1] = MkFloatTerm(PyComplex_ImagAsDouble(pVal)); | ||||
|     return Yap_MkApplTerm(FunctorI, 2, t); | ||||
|   | ||||
| } | ||||
| else if (PyUnicode_Check(pVal)) { | ||||
|   } | ||||
|   else if (PyUnicode_Check(pVal)) { | ||||
| #if PY_MAJOR_VERSION < 3 | ||||
|   size_t sz = PyUnicode_GetSize(pVal) + 1; | ||||
|   wchar_t *s = malloc(sizeof(wchar_t) * sz); | ||||
|   sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, a, sz - 1); | ||||
|   free(ptr); | ||||
|     size_t sz = PyUnicode_GetSize(pVal) + 1; | ||||
|     wchar_t *s = malloc(sizeof(wchar_t) * sz); | ||||
|     sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, a, sz - 1); | ||||
|     free(ptr); | ||||
| #else | ||||
|   const char *s = PyUnicode_AsUTF8(pVal); | ||||
|     const char *s = PyUnicode_AsUTF8(pVal); | ||||
| #endif | ||||
| #if 0 | ||||
|     if (false && Yap_AtomInUse(s)) | ||||
| @@ -92,72 +92,72 @@ else if (PyUnicode_Check(pVal)) { | ||||
|     else | ||||
| #endif | ||||
|       if  (pyStringToString) | ||||
|   return MkStringTerm(s); | ||||
|         return MkStringTerm(s); | ||||
|       else | ||||
| 	return MkAtomTerm(Yap_LookupAtom(s)); | ||||
| } | ||||
| else if (PyByteArray_Check(pVal)) { | ||||
|   return MkStringTerm(PyByteArray_AsString(pVal)); | ||||
|   } | ||||
|   else if (PyByteArray_Check(pVal)) { | ||||
|     return MkStringTerm(PyByteArray_AsString(pVal)); | ||||
| #if PY_MAJOR_VERSION < 3 | ||||
| } | ||||
| else if (PyString_Check(pVal)) { | ||||
|   return MkStringTerm(PyString_AsString(pVal)); | ||||
|   } | ||||
|   else if (PyString_Check(pVal)) { | ||||
|     return MkStringTerm(PyString_AsString(pVal)); | ||||
| #endif | ||||
| } | ||||
| else if (PyTuple_Check(pVal)) { | ||||
|   Py_ssize_t sz = PyTuple_Size(pVal); | ||||
|   const char *s; | ||||
|   s = Py_TYPE(pVal)->tp_name; | ||||
|   if (s == NULL) | ||||
|     s = "t"; | ||||
|   if (sz == 0) { | ||||
|   } | ||||
|   else if (PyTuple_Check(pVal)) { | ||||
|     Py_ssize_t sz = PyTuple_Size(pVal); | ||||
|     const char *s; | ||||
|     s = Py_TYPE(pVal)->tp_name; | ||||
|     if (s == NULL) | ||||
|       s = "t"; | ||||
|     if (sz == 0) { | ||||
|       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; | ||||
| } | ||||
| // PL_reset_term_refs(to); | ||||
| // fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" | ||||
| // ||***\n",stderr); | ||||
| } | ||||
| else if (PyList_Check(pVal)) { | ||||
|   Py_ssize_t i, sz = PyList_GET_SIZE(pVal); | ||||
|   if (sz == 0) | ||||
|     return repr_term(pVal); | ||||
|   Term t = TermNil; | ||||
|   for (i = sz; i > 0; ) { | ||||
|     -- i; | ||||
|     PyObject *p = PyList_GetItem(pVal, i); | ||||
|     if (p == NULL) { | ||||
|       PyErr_Clear(); | ||||
|       return false; | ||||
|     } | ||||
|     if (!python_to_term__(p)) | ||||
|       return false; | ||||
|   else if (PyList_Check(pVal)) { | ||||
|     Py_ssize_t i, sz = PyList_GET_SIZE(pVal); | ||||
|     if (sz == 0) | ||||
|       return repr_term(pVal); | ||||
|     Term t = TermNil; | ||||
|     for (i = sz; i > 0; ) { | ||||
|       -- i; | ||||
|       PyObject *p = PyList_GetItem(pVal, i); | ||||
|       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); | ||||
|   PyObject *key, *value; | ||||
|   Term f, *opt = &f, t, to; | ||||
|   if (tot == 0) | ||||
|     return MkAtomTerm( Yap_LookupAtom("{}")); | ||||
|   while (PyDict_Next(pVal, &pos, &key, &value)) { | ||||
|   else if (PyDict_Check(pVal)) { | ||||
|     Py_ssize_t pos = 0, tot = PyDict_Size(pVal); | ||||
|     PyObject *key, *value; | ||||
|     Term f, *opt = &f, t, to; | ||||
|     if (tot == 0) | ||||
|       return MkAtomTerm( Yap_LookupAtom("{}")); | ||||
|     while (PyDict_Next(pVal, &pos, &key, &value)) { | ||||
|       Term t0[2]; | ||||
|       t0[0] = python_to_term__(key); | ||||
|       t0[1] = python_to_term__(value); | ||||
| @@ -169,9 +169,9 @@ else if (PyDict_Check(pVal)) { | ||||
|         *opt = t; | ||||
| 	opt = pt+1; | ||||
|       } else { | ||||
| 	  if (pos == 0) { | ||||
| 	    return repr_term(pVal); | ||||
|   }  | ||||
|         if (pos == 0) { | ||||
|           return repr_term(pVal); | ||||
|         }  | ||||
|  | ||||
|         *opt = to; | ||||
| 	break; | ||||
| @@ -179,7 +179,7 @@ else if (PyDict_Check(pVal)) { | ||||
|     } | ||||
|     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); */ | ||||
|   if (pVal == NULL) | ||||
|     Yap_ThrowError(SYSTEM_ERROR_INTERNAL, 0, NULL); | ||||
|   yhandle_t h0 = Yap_CurrentHandle(); | ||||
|   Term t =  python_to_term__(pVal); | ||||
|   /* fputs("<< ***    ", stderr); */ | ||||
|   /* Yap_DebugPlWrite(t); */ | ||||
|   /* fputs(" ***\n", stderr); */ | ||||
|   // Py_DECREF(pVal); | ||||
|   Yap_CloseHandles(h0); | ||||
|   return t; | ||||
| } | ||||
|  | ||||
| @@ -225,13 +227,19 @@ PyObject *py_Local, *py_Global; | ||||
|  *python_assign. | ||||
|  */ | ||||
| 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); | ||||
|   // Yap_DebugPlWriteln(yt); | ||||
|   switch (PL_term_type(t)) { | ||||
|   case PL_VARIABLE: { | ||||
|     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: {              | ||||
| @@ -240,24 +248,34 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) { | ||||
|     PL_get_string_chars(t, &s,&l); | ||||
|     if (!context) | ||||
|       context = py_Main; | ||||
|     if (PyObject_SetAttrString(context, s, exp) == 0) | ||||
|       return true; | ||||
|     PyErr_Print(); | ||||
|     return false; | ||||
|     if (PyObject_SetAttrString(context, s, exp) == 0) { | ||||
|       PL_reset_term_refs(inp); | ||||
|       ; | ||||
|       rc = true; | ||||
|     } else { | ||||
|       PyErr_Print(); | ||||
|       PL_reset_term_refs(inp); | ||||
|       rc = false; | ||||
|     } | ||||
|     break; | ||||
|   } | ||||
|   case PL_ATOM: { | ||||
|     char *s = NULL; | ||||
|     PL_get_atom_chars(t, &s); | ||||
|     if (!context) | ||||
|       context = py_Main; | ||||
|     if (PyObject_SetAttrString(context, s, exp) == 0) | ||||
|       return true; | ||||
|     return false; | ||||
|     if (PyObject_SetAttrString(context, s, exp) == 0){ | ||||
|       rc = true; | ||||
|     } else { | ||||
|       rc = false; | ||||
|     } | ||||
|     break; | ||||
|   } | ||||
|   case PL_INTEGER: | ||||
|   case PL_FLOAT: | ||||
|     // domain or type erro? | ||||
|     return false; | ||||
|     rc = false; | ||||
|     break; | ||||
|   default: { | ||||
|     term_t tail = PL_new_term_ref(), arg = PL_new_term_ref(); | ||||
|     size_t len, i; | ||||
| @@ -271,90 +289,98 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) { | ||||
|           for (i = 0; i < len; i++) { | ||||
|             PyObject *p; | ||||
|             if (!PL_get_list(t, arg, t)) { | ||||
|               PL_reset_term_refs(tail); | ||||
|               p = Py_None; | ||||
|             } | ||||
|             if ((p = PySequence_GetItem(exp, i)) == NULL) | ||||
|               p = Py_None; | ||||
|             if (!python_assign(arg, p, context)) { | ||||
|               PL_reset_term_refs(tail); | ||||
|               rc = false; | ||||
|             } else { | ||||
|               if ((p = PySequence_GetItem(exp, i)) == NULL) | ||||
|                 p = Py_None; | ||||
|               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 { | ||||
|       functor_t fun; | ||||
|  | ||||
|       if (!PL_get_functor(t, &fun)) { | ||||
|         PL_reset_term_refs(tail); | ||||
|         return false; | ||||
|         rc = false; | ||||
|       } | ||||
|  | ||||
|       if (fun == FUNCTOR_sqbrackets2) { | ||||
|         //  tail is the object o | ||||
|         if (!PL_get_arg(2, t, tail)) { | ||||
|           PL_reset_term_refs(tail); | ||||
|           return false; | ||||
|         } | ||||
|         PyObject *o = term_to_python(tail, true, context, false); | ||||
|         // t now refers to the index | ||||
|         if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) || | ||||
|             !PL_get_nil(tail)) { | ||||
|           PL_reset_term_refs(tail); | ||||
|           return false; | ||||
|         } | ||||
|         PyObject *i = term_to_python(t, true, NULL, false); | ||||
|         // check numeric | ||||
|         if (PySequence_Check(o) && PyLong_Check(i)) { | ||||
|           long int j; | ||||
|           j = PyLong_AsLong(i); | ||||
|           return PySequence_SetItem(o, j, exp) == 0; | ||||
|         } | ||||
|           rc =  false; | ||||
|         } else { | ||||
|           PyObject *o = term_to_python(tail, true, context, false); | ||||
|           // t now refers to the index | ||||
|           if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) || | ||||
|               !PL_get_nil(tail)) { | ||||
|             PL_reset_term_refs(i); | ||||
|             ; | ||||
|             rc = false; | ||||
|           } else { | ||||
|             PyObject *i = term_to_python(t, true, NULL, false); | ||||
|             // check numeric | ||||
|             if (PySequence_Check(o) && PyLong_Check(i)) { | ||||
|               long int j; | ||||
|               j = PyLong_AsLong(i); | ||||
|               rc =  PySequence_SetItem(o, j, exp) == 0; | ||||
|             } | ||||
| #if PY_MAJOR_VERSION < 3 | ||||
|         if (PySequence_Check(o) && PyInt_Check(i)) { | ||||
|           long int j; | ||||
|           j = PyInt_AsLong(i); | ||||
|           return PySequence_SetItem(o, i, exp) == 0; | ||||
|         } | ||||
|             if (PySequence_Check(o) && PyInt_Check(i)) { | ||||
|               long int j; | ||||
|               j = PyInt_AsLong(i); | ||||
|               szzb  rc =  PySequence_SetItem(o, i, exp) == 0; | ||||
|             } else | ||||
| #endif | ||||
|         if (PyDict_Check(o)) { | ||||
|           if (PyDict_SetItem(o, i, exp) == 0) { | ||||
|             return true; | ||||
|           } | ||||
|         } | ||||
|         if (PyObject_SetAttr(o, i, exp) == 0) { | ||||
|           return true; | ||||
|         } | ||||
|               if (PyDict_Check(o)) { | ||||
|                 if (PyDict_SetItem(o, i, exp) == 0) { | ||||
|                   PL_reset_term_refs(inp); | ||||
|                   ; | ||||
|                   rc = true; | ||||
|                 } | ||||
|               } | ||||
|             if (PyObject_SetAttr(o, i, exp) == 0) { | ||||
|               PL_reset_term_refs(inp); | ||||
|               ; | ||||
|               rc = true; | ||||
|             } | ||||
|           }  | ||||
|         }  | ||||
|       } else { | ||||
|  | ||||
|         atom_t s; | ||||
|         int n, i; | ||||
|         PL_get_name_arity(t, &s, &n); | ||||
|         PyObject *o = term_to_python(t, true, context, true); | ||||
| 	PyErr_Print(); | ||||
|         PyErr_Print(); | ||||
|         if (PySequence_Check(o) && PySequence_Length(o) == n) { | ||||
|           for (i = 1; i <= n; i++) { | ||||
|             PyObject *p; | ||||
|             if (!PL_get_arg(i, t, arg)) { | ||||
|               PL_reset_term_refs(tail); | ||||
|               o = false; | ||||
|               o = NULL; | ||||
|               p = Py_None; | ||||
|             } | ||||
| 	     | ||||
|             if ((p = PySequence_GetItem(exp, i - 1)) == NULL) | ||||
|               p = Py_None; | ||||
|             else if (!python_assign(arg, p, NULL)) { | ||||
|               PL_reset_term_refs(tail); | ||||
|               o = NULL; | ||||
|             } else { | ||||
|  	      PyErr_Print(); | ||||
| 	    } | ||||
|             rc = python_assign(arg, p, NULL); | ||||
|  | ||||
|           } | ||||
|           return true; | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|     PL_reset_term_refs(tail); | ||||
|   } | ||||
|  	      PyErr_Print(); | ||||
|     return NULL; | ||||
|   } | ||||
|  | ||||
|   PyErr_Print(); | ||||
|   PL_reset_term_refs(inp); | ||||
|    | ||||
|     | ||||
|   return rc; | ||||
| } | ||||
|   | ||||
| @@ -112,12 +112,12 @@ find_obj(PyObject *ob, term_t l, bool eval) { | ||||
|   YAP_Term hd, yt; | ||||
|  | ||||
|   py_Context = NULL; | ||||
|   yt = YAP_GetFromSlot(l); | ||||
|   // Yap_DebugPlWriteln(yt); | ||||
|   while (YAP_IsPairTerm(yt)) { | ||||
|   if (l == 0) | ||||
|     return Py_None; | ||||
|   while (YAP_IsPairTerm((yt = YAP_GetFromSlot(l)))) { | ||||
|     hd = YAP_HeadOfTerm(yt); | ||||
|     yt = YAP_TailOfTerm(yt); | ||||
|     YAP_PutInSlot(l, yt); | ||||
|     Yap_PutInHandle(l, YAP_TailOfTerm(yt)); | ||||
|     ob = yap_to_python(hd, true, ob, false); | ||||
|     if (!ob) { | ||||
|       return Py_None; | ||||
|   | ||||
| @@ -113,7 +113,6 @@ static foreign_t python_is(term_t tobj, term_t tf) { | ||||
|  | ||||
| static foreign_t python_proc(term_t tobj) { | ||||
|   PyObject *o; | ||||
|  | ||||
|   term_t lim = python_acquire_GIL(); | ||||
|  | ||||
|   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) { | ||||
|   printf("A %ld\n", LOCAL_CurSlot); | ||||
|   term_t stackp = python_acquire_GIL(); | ||||
|   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); | ||||
|   } | ||||
|   bool b = python_assign(name, e, NULL); | ||||
|   printf("B %ld\n", LOCAL_CurSlot); | ||||
|   python_release_GIL(stackp); | ||||
|   pyErrorAndReturn(b); | ||||
| } | ||||
|   | ||||
| @@ -133,7 +133,7 @@ undefined_query(G0, M0, Cut) :- | ||||
| %  we found an import, and call again | ||||
| %  we have user code in the unknown_predicate | ||||
| %  we fail, output a message, and just generate an exception. | ||||
| '$undefp'([M0|G0],ok) :- | ||||
| '$undefp'([M0|G0],_) :- | ||||
|     '$search_undef'(M0:G0, M:G), | ||||
| 	'$trace'(M:G). | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user