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); }
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); }
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); } }
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); }
ExecutionResults OnGetUnboundVariable(WamInstruction instruction) { SetRegister(instruction.TargetRegister, GetRegister(instruction.SourceRegister)); InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnPutStructure(WamInstruction instruction) { SetRegister(instruction.TargetRegister, CreateCompoundTerm(instruction.Functor)); InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnPutValue(WamInstruction instruction) { SetRegister(instruction.TargetRegister, instruction.ReferenceTarget); InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnNoop(WamInstruction instruction) { InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }