private static int CountTo(PlTerm term, PlTerm term2, ref NonDetTest o) { try { var c = o.start; bool succed = term.Unify("callnum" + c); if (!succed) { succed = term2.Unify("callnum" + c); } if (succed) { succed = term2.Unify(term); } if (succed) { return(libpl.PL_succeed); } return(libpl.PL_fail); } finally { o.start++; } }
// foo(X,Y),writeq(f(X,Y)),nl,X=5. public static int Foo(PlTerm t0, PlTerm term2, IntPtr control) { callNum++; if (callNum > 10) { callNum = 0; //return libpl.PL_fail; } var handle = control; FRG fc = (FRG)(libpl.PL_foreign_control(control)); switch (fc) { case FRG.PL_FIRST_CALL: unsafe { ndtp = new PrologCLR.PinnedObject <NonDetTest>(); ndtp.managedObject.start = 1; ndtp.managedObject.stop = 3; //ndtp.managedObject.fid = libpl.PL_open_foreign_frame(); ndtp.Recopy(); IntPtr ctxt = ndtp.Pointer; goto redo; int succeed = CountTo(t0, term2, ref ndtp.managedObject); if (ndtp.managedObject.start <= ndtp.managedObject.stop) { libpl.PL_retry_address(ctxt); } if (succeed == 0) { return(0); } return(3); } break; case FRG.PL_REDO: unsafe { goto redo; NonDetTest *o = (NonDetTest *)0; IntPtr ctxt = libpl.PL_foreign_context_address(control); if (!ctxt.ToString().Equals("0")) { o = (NonDetTest *)ctxt; } else { o = (NonDetTest *)ndtp.Pointer; } int succeed = CountTo(t0, term2, ref *o); NonDetTest managedObject = *o; if (managedObject.start <= managedObject.stop) { libpl.PL_retry_address(ctxt); if (succeed == 0) { return(0); } return(3); } if (managedObject.fid != 0) { libpl.PL_close_foreign_frame(managedObject.fid); managedObject.fid = 0; } if (succeed == 0) { return(0); } return(1); } break; case FRG.PL_CUTTED: unsafe { NonDetTest *o = (NonDetTest *)0; IntPtr ctxt = libpl.PL_foreign_context_address(control); if (!ctxt.ToString().Equals("0")) { o = (NonDetTest *)ctxt; } else { o = (NonDetTest *)ndtp.Pointer; } NonDetTest managedObject = *o; if (managedObject.fid != 0) { libpl.PL_close_foreign_frame(managedObject.fid); managedObject.fid = 0; } return(libpl.PL_succeed); } break; default: { throw new PlException("no frg"); return(libpl.PL_fail); } break; } redo: unsafe { NonDetTest *o = (NonDetTest *)0; IntPtr ctxt = libpl.PL_foreign_context_address(control); var fc0 = libpl.PL_foreign_context(control); if (!ctxt.ToString().Equals("0")) { o = (NonDetTest *)ctxt; } else { o = (NonDetTest *)ndtp.Pointer; } int succeed = CountTo(t0, term2, ref *o); NonDetTest managedObject = *o; if (managedObject.start <= managedObject.stop) { libpl.PL_retry_address(ctxt); if (succeed == 0) { return(0); } return(3); } if (managedObject.fid != 0) { libpl.PL_close_foreign_frame(managedObject.fid); managedObject.fid = 0; } if (succeed == 0) { return(0); } return(1); } }
private static int CountTo(PlTerm term, PlTerm term2, ref NonDetTest o) { try { var c = o.start; bool succed = term.Unify("callnum" + c); if (!succed) { succed = term2.Unify("callnum" + c); } if (succed) { succed = term2.Unify(term); } if (succed) { return libpl.PL_succeed; } return libpl.PL_fail; } finally { o.start++; } }