示例#1
0
        ExecutionResults OnSetUnboundVariable(WamInstruction instruction)
        {
            if (CurrentStructure == null)
            {
                throw new InvalidOperationException("No current structure.");
            }

            CurrentStructureIndex += 1;
            if (CurrentStructureIndex >= CurrentStructure.Functor.Arity)
            {
                throw new InvalidOperationException("Current structure arity exceeded.");
            }

            SetRegister(instruction.TargetRegister, CreateVariable());
            CurrentStructure.Children[CurrentStructureIndex] = GetRegister(instruction.TargetRegister);
            InstructionPointer = InstructionPointer.GetNext();
            return(ExecutionResults.None);
        }
示例#2
0
        ExecutionResults OnUnifyBoundVariable(WamInstruction instruction)
        {
            CurrentStructureIndex += 1;

            if (CurrentUnifyMode == UnifyModes.Read)
            {
                if (!Unify(GetRegister(instruction.TargetRegister), CurrentStructure.Children[CurrentStructureIndex]))
                {
                    return(ExecutionResults.Backtrack);
                }
            }
            else // write
            {
                CurrentStructure.Children[CurrentStructureIndex] = GetRegister(instruction.TargetRegister);
            }
            InstructionPointer = InstructionPointer.GetNext();
            return(ExecutionResults.None);
        }
示例#3
0
        ExecutionResults OnLibraryCallFunction(WamInstruction instruction, Function function)
        {
            var functionArguments = new CodeTerm[instruction.Functor.Arity];

            for (int index = 0; index < instruction.Functor.Arity; ++index)
            {
                functionArguments[index] = Evaluate(ArgumentRegisters[index]).GetCodeTerm();
            }

            CodeTerm functionResult;

            try
            {
                functionResult = function.FunctionDelegate(functionArguments);
            }
            catch
            {
                // Backtrack on exception.
                //
                return(ExecutionResults.Backtrack);
            }

            try
            {
                var functionResultValue = (CodeValue)functionResult;
                if (Convert.ToBoolean(functionResultValue.Object))
                {
                    InstructionPointer = InstructionPointer.GetNext();
                    return(ExecutionResults.None);
                }
                else
                {
                    // Result converts to false.
                    //
                    return(ExecutionResults.Backtrack);
                }
            }
            catch
            {
                // Result cannot be converted to a boolean.
                //
                return(ExecutionResults.Backtrack);
            }
        }
示例#4
0
        ExecutionResults OnCall(WamInstruction instruction)
        {
            var instructionPointer = GetInstructionPointer(instruction.Functor, 0);

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

            if (StackIndex >= StackSizeLimit)
            {
                return(ExecutionResults.Failure);
            }

            ReturnInstructionPointer = InstructionPointer.GetNext();
            CutChoicePoint           = ChoicePoint;
            TemporaryRegisters.Clear();

            StackIndex        += 1;
            InstructionPointer = instructionPointer;

            return(ExecutionResults.None);
        }
示例#5
0
 ExecutionResults OnGetUnboundVariable(WamInstruction instruction)
 {
     SetRegister(instruction.TargetRegister, GetRegister(instruction.SourceRegister));
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
示例#6
0
 ExecutionResults OnPutStructure(WamInstruction instruction)
 {
     SetRegister(instruction.TargetRegister, CreateCompoundTerm(instruction.Functor));
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
示例#7
0
 ExecutionResults OnPutValue(WamInstruction instruction)
 {
     SetRegister(instruction.TargetRegister, instruction.ReferenceTarget);
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
示例#8
0
 ExecutionResults OnNoop(WamInstruction instruction)
 {
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }