Exemplo n.º 1
0
        // 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);
            }
        }
Exemplo n.º 2
0
        // 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;
            }
        }