Ejemplo n.º 1
0
        ExecutionResults OnLibraryCallBacktrackingPredicate(WamInstruction instruction, BacktrackingPredicate predicate)
        {
            var arguments = new WamReferenceTarget[instruction.Functor.Arity];

            for (var index = 0; index < instruction.Functor.Arity; ++index)
            {
                arguments[index] = ArgumentRegisters[index];
            }

            IEnumerable <bool> enumerable;

            try
            {
                enumerable = predicate.BacktrackingPredicateDelegate(this, arguments);
            }
            catch
            {
                // Backtrack on exception.
                //
                return(ExecutionResults.Backtrack);
            }

            var enumerator = enumerable.GetEnumerator();

            ChoicePoint = new WamChoicePoint(ChoicePoint, Environment, StackIndex, ReturnInstructionPointer, ArgumentRegisters, CutChoicePoint)
            {
                BacktrackInstructionPointer = InstructionPointer.GetNext(),
                PredicateEnumerator         = enumerator
            };
            InstructionPointer  = ChoicePoint.BacktrackInstructionPointer;
            PredicateEnumerator = ChoicePoint.PredicateEnumerator;
            return(ExecutionResults.None);
        }
Ejemplo n.º 2
0
        ExecutionResults OnTryMeElse(WamInstruction instruction)
        {
            var instructionPointer = GetInstructionPointer(instruction.Functor, instruction.Index);

            if (instructionPointer == WamInstructionPointer.Undefined)
            {
                return(ExecutionResults.Failure);
            }

            ChoicePoint = new WamChoicePoint(ChoicePoint, Environment, StackIndex, ReturnInstructionPointer, ArgumentRegisters, CutChoicePoint)
            {
                BacktrackInstructionPointer = instructionPointer
            };
            InstructionPointer = InstructionPointer.GetNext();
            return(ExecutionResults.None);
        }
Ejemplo n.º 3
0
        public WamChoicePoint(WamChoicePoint predecessor, WamEnvironment environment, int stackIndex, WamInstructionPointer returnInstructionPointer, IEnumerable <WamReferenceTarget> argumentRegisters, WamChoicePoint cutChoicePoint)
        {
            Generation = NextGeneration++;

            Predecessor = predecessor;
            Environment = environment;
            StackIndex  = stackIndex;
            ReturnInstructionPointer = returnInstructionPointer;
            ArgumentRegisters        = new WamReferenceTargetList(argumentRegisters);
            CutChoicePoint           = cutChoicePoint;

            BacktrackInstructionPointer = WamInstructionPointer.Undefined;
            PredicateEnumerator         = null;

            Trail = new List <WamVariable>();
        }
Ejemplo n.º 4
0
        public WamChoicePoint(WamChoicePoint predecessor, WamEnvironment environment, int stackIndex, WamInstructionPointer returnInstructionPointer, IEnumerable<WamReferenceTarget> argumentRegisters, WamChoicePoint cutChoicePoint)
        {
            m_generation = s_nextGeneration++;

            m_predecessor = predecessor;
            m_environment = environment;
            m_stackIndex = stackIndex;
            m_returnInstructionPointer = returnInstructionPointer;
            m_argumentRegisters = new WamReferenceTargetList(argumentRegisters);
            m_cutChoicePoint = cutChoicePoint;

            m_backtrackInstructionPointer = WamInstructionPointer.Undefined;
            m_predicateEnumerator = null;

            m_trail = new List<WamVariable>();
        }
Ejemplo n.º 5
0
        public WamChoicePoint(WamChoicePoint predecessor, WamEnvironment environment, int stackIndex, WamInstructionPointer returnInstructionPointer, IEnumerable<WamReferenceTarget> argumentRegisters, WamChoicePoint cutChoicePoint)
        {
            Generation = NextGeneration++;

            Predecessor = predecessor;
            Environment = environment;
            StackIndex = stackIndex;
            ReturnInstructionPointer = returnInstructionPointer;
            ArgumentRegisters = new WamReferenceTargetList(argumentRegisters);
            CutChoicePoint = cutChoicePoint;

            BacktrackInstructionPointer = WamInstructionPointer.Undefined;
            PredicateEnumerator = null;

            Trail = new List<WamVariable>();
        }
Ejemplo n.º 6
0
        public WamChoicePoint(WamChoicePoint predecessor, WamEnvironment environment, int stackIndex, WamInstructionPointer returnInstructionPointer, IEnumerable <WamReferenceTarget> argumentRegisters, WamChoicePoint cutChoicePoint)
        {
            m_generation = s_nextGeneration++;

            m_predecessor = predecessor;
            m_environment = environment;
            m_stackIndex  = stackIndex;
            m_returnInstructionPointer = returnInstructionPointer;
            m_argumentRegisters        = new WamReferenceTargetList(argumentRegisters);
            m_cutChoicePoint           = cutChoicePoint;

            m_backtrackInstructionPointer = WamInstructionPointer.Undefined;
            m_predicateEnumerator         = null;

            m_trail = new List <WamVariable>();
        }
Ejemplo n.º 7
0
        private ExecutionResults OnTryMeElse(WamInstruction instruction)
        {
            WamInstructionPointer instructionPointer = GetInstructionPointer(instruction.Functor, instruction.Index);
            if (instructionPointer == WamInstructionPointer.Undefined)
            {
                return ExecutionResults.Failure;
            }

            ChoicePoint = new WamChoicePoint(ChoicePoint, Environment, StackIndex, ReturnInstructionPointer, ArgumentRegisters, CutChoicePoint);
            ChoicePoint.BacktrackInstructionPointer = instructionPointer;

            InstructionPointer = InstructionPointer.GetNext();

            return ExecutionResults.None;
        }
Ejemplo n.º 8
0
        private ExecutionResults OnLibraryCallBacktrackingPredicate(WamInstruction instruction, BacktrackingPredicate predicate)
        {
            WamReferenceTarget[] arguments = new WamReferenceTarget[instruction.Functor.Arity];
            for (int index = 0; index < instruction.Functor.Arity; ++index)
            {
                arguments[index] = ArgumentRegisters[index];
            }

            IEnumerable<bool> enumerable;
            try
            {
                enumerable = predicate.BacktrackingPredicateDelegate(this, arguments);
            }
            catch
            {
                // Backtrack on exception.
                //
                return ExecutionResults.Backtrack;
            }

            IEnumerator<bool> enumerator = enumerable.GetEnumerator();

            ChoicePoint = new WamChoicePoint(ChoicePoint, Environment, StackIndex, ReturnInstructionPointer, ArgumentRegisters, CutChoicePoint);
            ChoicePoint.BacktrackInstructionPointer = InstructionPointer.GetNext();
            ChoicePoint.PredicateEnumerator = enumerator;

            InstructionPointer = ChoicePoint.BacktrackInstructionPointer;
            PredicateEnumerator = ChoicePoint.PredicateEnumerator;

            return ExecutionResults.None;
        }