ExecutionResults OnLibraryCallPredicate(WamInstruction instruction, Predicate predicate) { var arguments = new WamReferenceTarget[instruction.Functor.Arity]; for (var index = 0; index < instruction.Functor.Arity; ++index) { arguments[index] = ArgumentRegisters[index]; } bool result; try { result = predicate.PredicateDelegate(this, arguments); } catch { // Backtrack on exception. // return(ExecutionResults.Backtrack); } if (result == false) { return(ExecutionResults.Backtrack); } InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnPutUnboundVariable(WamInstruction instruction) { SetRegister(instruction.SourceRegister, CreateVariable()); SetRegister(instruction.TargetRegister, GetRegister(instruction.SourceRegister)); InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnGetStructure(WamInstruction instruction) { var sourceReference = GetRegister(instruction.SourceRegister).Dereference(); var sourceVariable = sourceReference as WamVariable; var sourceCompoundTerm = sourceReference as WamCompoundTerm; // Ensure target is either a variable or compound term. // Debug.Assert(sourceVariable != null || sourceCompoundTerm != null); if (sourceVariable != null) { var compoundTerm = WamCompoundTerm.Create(instruction.Functor); Bind(sourceVariable, compoundTerm); CurrentStructure = compoundTerm; CurrentStructureIndex = -1; CurrentUnifyMode = UnifyModes.Write; } else // targetCompoundTerm != null { if (sourceCompoundTerm.Functor != instruction.Functor) { return(ExecutionResults.Backtrack); } CurrentStructure = sourceCompoundTerm; CurrentStructureIndex = -1; CurrentUnifyMode = UnifyModes.Read; } InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnAllocate(WamInstruction instruction) { Environment = new WamEnvironment(Environment, ReturnInstructionPointer, CutChoicePoint); ReturnInstructionPointer = WamInstructionPointer.Undefined; InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
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); }
ExecutionResults OnProceed(WamInstruction instruction) { StackIndex -= 1; InstructionPointer = ReturnInstructionPointer; ReturnInstructionPointer = WamInstructionPointer.Undefined; TemporaryRegisters.Clear(); return(ExecutionResults.None); }
ExecutionResults OnCut(WamInstruction instruction) { while (ChoicePoint != CutChoicePoint) { ChoicePoint = ChoicePoint.Predecessor; } InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnGetBoundVariable(WamInstruction instruction) { if (!(Unify(GetRegister(instruction.TargetRegister), GetRegister(instruction.SourceRegister)))) { return(ExecutionResults.Backtrack); } InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnGetValue(WamInstruction instruction) { if (!(Unify(instruction.ReferenceTarget, GetRegister(instruction.SourceRegister)))) { return(ExecutionResults.Backtrack); } InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnTrustMe(WamInstruction instruction) { if (ChoicePoint == null) { throw new InvalidOperationException("Choice point not found."); } ChoicePoint = ChoicePoint.Predecessor; InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnRetryMeElse(WamInstruction instruction) { var instructionPointer = GetInstructionPointer(instruction.Functor, instruction.Index); if (instructionPointer == WamInstructionPointer.Undefined) { return(ExecutionResults.Failure); } ChoicePoint.BacktrackInstructionPointer = instructionPointer; InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnDeallocate(WamInstruction instruction) { if (Environment == null) { throw new InvalidOperationException("Environment not found."); } ReturnInstructionPointer = Environment.ReturnInstructionPointer; Environment = Environment.Predecessor; CutChoicePoint = Environment.CutChoicePoint; InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnUnifyUnboundVariable(WamInstruction instruction) { CurrentStructureIndex += 1; if (CurrentUnifyMode == UnifyModes.Read) { SetRegister(instruction.TargetRegister, CurrentStructure.Children[CurrentStructureIndex]); } else // write { SetRegister(instruction.TargetRegister, CreateVariable()); CurrentStructure.Children[CurrentStructureIndex] = GetRegister(instruction.TargetRegister); } InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnSetValue(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."); } CurrentStructure.Children[CurrentStructureIndex] = instruction.ReferenceTarget; InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
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); }
ExecutionResults OnUnifyValue(WamInstruction instruction) { CurrentStructureIndex += 1; if (CurrentUnifyMode == UnifyModes.Read) { if (!Unify(instruction.ReferenceTarget, CurrentStructure.Children[CurrentStructureIndex])) { return(ExecutionResults.Backtrack); } } else // write { CurrentStructure.Children[CurrentStructureIndex] = instruction.ReferenceTarget; } 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 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 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 OnExecute(WamInstruction instruction) { var instructionPointer = GetInstructionPointer(instruction.Functor, 0); if (instructionPointer == WamInstructionPointer.Undefined) { return(ExecutionResults.Failure); } if (StackIndex >= StackSizeLimit) { return(ExecutionResults.Failure); } CutChoicePoint = ChoicePoint; TemporaryRegisters.Clear(); InstructionPointer = instructionPointer; return(ExecutionResults.None); }
ExecutionResults OnLibraryCallCodePredicate(WamInstruction instruction, CodePredicate predicate) { var arguments = new WamReferenceTarget[instruction.Functor.Arity]; for (var index = 0; index < instruction.Functor.Arity; ++index) { arguments[index] = ArgumentRegisters[index]; } try { predicate.CodePredicateDelegate(this, arguments); } catch { // Backtrack on exception. // return(ExecutionResults.Backtrack); } return(ExecutionResults.None); }
ExecutionResults OnLibraryCall(WamInstruction instruction) { var method = Program.Libraries[instruction.Functor]; var function = method as Function; if (function != null) { return(OnLibraryCallFunction(instruction, function)); } var predicate = method as Predicate; if (predicate != null) { return(OnLibraryCallPredicate(instruction, predicate)); } var backtrackingPredicate = method as BacktrackingPredicate; if (backtrackingPredicate != null) { return(OnLibraryCallBacktrackingPredicate(instruction, backtrackingPredicate)); } var codePredicate = method as CodePredicate; if (codePredicate != null) { return(OnLibraryCallCodePredicate(instruction, codePredicate)); } // Library method not found. // return(ExecutionResults.Failure); }
private ExecutionResults OnUnifyValue(WamInstruction instruction) { CurrentStructureIndex += 1; if (CurrentUnifyMode == UnifyModes.Read) { if (!Unify(instruction.ReferenceTarget, CurrentStructure.Children[CurrentStructureIndex])) { return ExecutionResults.Backtrack; } } else // write { CurrentStructure.Children[CurrentStructureIndex] = instruction.ReferenceTarget; } InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnGetUnboundVariable(WamInstruction instruction) { SetRegister(instruction.TargetRegister, GetRegister(instruction.SourceRegister)); InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnGetStructure(WamInstruction instruction) { WamReferenceTarget sourceReference = GetRegister(instruction.SourceRegister).Dereference(); WamVariable sourceVariable = sourceReference as WamVariable; WamCompoundTerm sourceCompoundTerm = sourceReference as WamCompoundTerm; // Ensure target is either a variable or compound term. // Debug.Assert(sourceVariable != null || sourceCompoundTerm != null); if (sourceVariable != null) { WamCompoundTerm compoundTerm = WamCompoundTerm.Create(instruction.Functor); Bind(sourceVariable, compoundTerm); CurrentStructure = compoundTerm; CurrentStructureIndex = -1; CurrentUnifyMode = UnifyModes.Write; } else // targetCompoundTerm != null { if (sourceCompoundTerm.Functor != instruction.Functor) { return ExecutionResults.Backtrack; } CurrentStructure = sourceCompoundTerm; CurrentStructureIndex = -1; CurrentUnifyMode = UnifyModes.Read; } InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnSetValue(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."); } CurrentStructure.Children[CurrentStructureIndex] = instruction.ReferenceTarget; InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnPutStructure(WamInstruction instruction) { SetRegister(instruction.TargetRegister, CreateCompoundTerm(instruction.Functor)); InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
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; }
private ExecutionResults OnFailure(WamInstruction instruction) { return ExecutionResults.Failure; }
private ExecutionResults OnDeallocate(WamInstruction instruction) { if (Environment == null) { throw new InvalidOperationException("Environment not found."); } ReturnInstructionPointer = Environment.ReturnInstructionPointer; Environment = Environment.Predecessor; CutChoicePoint = Environment.CutChoicePoint; InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnCut(WamInstruction instruction) { while (ChoicePoint != CutChoicePoint) { ChoicePoint = ChoicePoint.Predecessor; } InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
ExecutionResults OnSuccess(WamInstruction instruction) { return(ExecutionResults.Success); }
ExecutionResults OnFailure(WamInstruction instruction) { return(ExecutionResults.Failure); }
ExecutionResults OnPutValue(WamInstruction instruction) { SetRegister(instruction.TargetRegister, instruction.ReferenceTarget); InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
private ExecutionResults Execute(WamInstruction instruction) { PerformanceStatistics.IncrementInstructionCount(); switch (instruction.OpCode) { // Control Flow // case WamInstructionOpCodes.Allocate: return OnAllocate(instruction); case WamInstructionOpCodes.Call: return OnCall(instruction); case WamInstructionOpCodes.Cut: return OnCut(instruction); case WamInstructionOpCodes.Execute: return OnExecute(instruction); case WamInstructionOpCodes.Deallocate: return OnDeallocate(instruction); case WamInstructionOpCodes.LibraryCall: return OnLibraryCall(instruction); case WamInstructionOpCodes.Noop: return OnNoop(instruction); case WamInstructionOpCodes.Proceed: return OnProceed(instruction); case WamInstructionOpCodes.RetryMeElse: return OnRetryMeElse(instruction); case WamInstructionOpCodes.Success: return OnSuccess(instruction); case WamInstructionOpCodes.Failure: return OnFailure(instruction); case WamInstructionOpCodes.TrustMe: return OnTrustMe(instruction); case WamInstructionOpCodes.TryMeElse: return OnTryMeElse(instruction); // Put/Set // case WamInstructionOpCodes.PutBoundVariable: return OnPutBoundVariable(instruction); case WamInstructionOpCodes.PutStructure: return OnPutStructure(instruction); case WamInstructionOpCodes.PutUnboundVariable: return OnPutUnboundVariable(instruction); case WamInstructionOpCodes.PutValue: return OnPutValue(instruction); case WamInstructionOpCodes.SetBoundVariable: return OnSetBoundVariable(instruction); case WamInstructionOpCodes.SetUnboundVariable: return OnSetUnboundVariable(instruction); case WamInstructionOpCodes.SetValue: return OnSetValue(instruction); // Get/Unify // case WamInstructionOpCodes.GetBoundVariable: return OnGetBoundVariable(instruction); case WamInstructionOpCodes.GetStructure: return OnGetStructure(instruction); case WamInstructionOpCodes.GetUnboundVariable: return OnGetUnboundVariable(instruction); case WamInstructionOpCodes.GetValue: return OnGetValue(instruction); case WamInstructionOpCodes.UnifyBoundVariable: return OnUnifyBoundVariable(instruction); case WamInstructionOpCodes.UnifyUnboundVariable: return OnUnifyUnboundVariable(instruction); case WamInstructionOpCodes.UnifyValue: return OnUnifyValue(instruction); default: throw new InvalidOperationException(string.Format("Unknown opcode {0}.", instruction.OpCode)); } }
public void Write(WamInstruction instruction) { m_instructions.Add(instruction); }
ExecutionResults OnPutStructure(WamInstruction instruction) { SetRegister(instruction.TargetRegister, CreateCompoundTerm(instruction.Functor)); InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
ExecutionResults OnNoop(WamInstruction instruction) { InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
private ExecutionResults OnTrustMe(WamInstruction instruction) { if (ChoicePoint == null) { throw new InvalidOperationException("Choice point not found."); } ChoicePoint = ChoicePoint.Predecessor; InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnLibraryCallCodePredicate(WamInstruction instruction, CodePredicate predicate) { WamReferenceTarget[] arguments = new WamReferenceTarget[instruction.Functor.Arity]; for (int index = 0; index < instruction.Functor.Arity; ++index) { arguments[index] = ArgumentRegisters[index]; } try { predicate.CodePredicateDelegate(this, arguments); } catch { // Backtrack on exception. // return ExecutionResults.Backtrack; } return ExecutionResults.None; }
private ExecutionResults OnPutValue(WamInstruction instruction) { SetRegister(instruction.TargetRegister, instruction.ReferenceTarget); InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
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; }
private 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; }
private ExecutionResults OnLibraryCallFunction(WamInstruction instruction, Function function) { CodeTerm[] 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 { CodeValue 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; } }
private ExecutionResults OnGetValue(WamInstruction instruction) { if (!(Unify(instruction.ReferenceTarget, GetRegister(instruction.SourceRegister)))) { return ExecutionResults.Backtrack; } InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnNoop(WamInstruction instruction) { InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnGetBoundVariable(WamInstruction instruction) { if (!(Unify(GetRegister(instruction.TargetRegister), GetRegister(instruction.SourceRegister)))) { return ExecutionResults.Backtrack; } InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnProceed(WamInstruction instruction) { StackIndex -= 1; InstructionPointer = ReturnInstructionPointer; ReturnInstructionPointer = WamInstructionPointer.Undefined; TemporaryRegisters.Clear(); return ExecutionResults.None; }
private 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; }
private ExecutionResults OnRetryMeElse(WamInstruction instruction) { WamInstructionPointer instructionPointer = GetInstructionPointer(instruction.Functor, instruction.Index); if (instructionPointer == WamInstructionPointer.Undefined) { return ExecutionResults.Failure; } ChoicePoint.BacktrackInstructionPointer = instructionPointer; InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnUnifyUnboundVariable(WamInstruction instruction) { CurrentStructureIndex += 1; if (CurrentUnifyMode == UnifyModes.Read) { SetRegister(instruction.TargetRegister, CurrentStructure.Children[CurrentStructureIndex]); } else // write { SetRegister(instruction.TargetRegister, CreateVariable()); CurrentStructure.Children[CurrentStructureIndex] = GetRegister(instruction.TargetRegister); } InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
private ExecutionResults OnSuccess(WamInstruction instruction) { return ExecutionResults.Success; }
private ExecutionResults OnAllocate(WamInstruction instruction) { Environment = new WamEnvironment(Environment, ReturnInstructionPointer, CutChoicePoint); ReturnInstructionPointer = WamInstructionPointer.Undefined; InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
ExecutionResults Execute(WamInstruction instruction) { PerformanceStatistics.IncrementInstructionCount(); switch (instruction.OpCode) { // Control Flow // case WamInstructionOpCodes.Allocate: return(OnAllocate(instruction)); case WamInstructionOpCodes.Call: return(OnCall(instruction)); case WamInstructionOpCodes.Cut: return(OnCut(instruction)); case WamInstructionOpCodes.Execute: return(OnExecute(instruction)); case WamInstructionOpCodes.Deallocate: return(OnDeallocate(instruction)); case WamInstructionOpCodes.LibraryCall: return(OnLibraryCall(instruction)); case WamInstructionOpCodes.Noop: return(OnNoop(instruction)); case WamInstructionOpCodes.Proceed: return(OnProceed(instruction)); case WamInstructionOpCodes.RetryMeElse: return(OnRetryMeElse(instruction)); case WamInstructionOpCodes.Success: return(OnSuccess(instruction)); case WamInstructionOpCodes.Failure: return(OnFailure(instruction)); case WamInstructionOpCodes.TrustMe: return(OnTrustMe(instruction)); case WamInstructionOpCodes.TryMeElse: return(OnTryMeElse(instruction)); // Put/Set // case WamInstructionOpCodes.PutBoundVariable: return(OnPutBoundVariable(instruction)); case WamInstructionOpCodes.PutStructure: return(OnPutStructure(instruction)); case WamInstructionOpCodes.PutUnboundVariable: return(OnPutUnboundVariable(instruction)); case WamInstructionOpCodes.PutValue: return(OnPutValue(instruction)); case WamInstructionOpCodes.SetBoundVariable: return(OnSetBoundVariable(instruction)); case WamInstructionOpCodes.SetUnboundVariable: return(OnSetUnboundVariable(instruction)); case WamInstructionOpCodes.SetValue: return(OnSetValue(instruction)); // Get/Unify // case WamInstructionOpCodes.GetBoundVariable: return(OnGetBoundVariable(instruction)); case WamInstructionOpCodes.GetStructure: return(OnGetStructure(instruction)); case WamInstructionOpCodes.GetUnboundVariable: return(OnGetUnboundVariable(instruction)); case WamInstructionOpCodes.GetValue: return(OnGetValue(instruction)); case WamInstructionOpCodes.UnifyBoundVariable: return(OnUnifyBoundVariable(instruction)); case WamInstructionOpCodes.UnifyUnboundVariable: return(OnUnifyUnboundVariable(instruction)); case WamInstructionOpCodes.UnifyValue: return(OnUnifyValue(instruction)); default: throw new InvalidOperationException(string.Format("Unknown opcode {0}.", instruction.OpCode)); } }
private ExecutionResults OnExecute(WamInstruction instruction) { WamInstructionPointer instructionPointer = GetInstructionPointer(instruction.Functor, 0); if (instructionPointer == WamInstructionPointer.Undefined) { return ExecutionResults.Failure; } if (StackIndex >= StackSizeLimit) { return ExecutionResults.Failure; } CutChoicePoint = ChoicePoint; TemporaryRegisters.Clear(); InstructionPointer = instructionPointer; return ExecutionResults.None; }
public void Write(WamInstruction instruction) { _instructions.Add(instruction); }
private ExecutionResults OnLibraryCall(WamInstruction instruction) { LibraryMethod method = Program.Libraries[instruction.Functor]; Function function = method as Function; if (function != null) { return OnLibraryCallFunction(instruction, function); } Predicate predicate = method as Predicate; if (predicate != null) { return OnLibraryCallPredicate(instruction, predicate); } BacktrackingPredicate backtrackingPredicate = method as BacktrackingPredicate; if (backtrackingPredicate != null) { return OnLibraryCallBacktrackingPredicate(instruction, backtrackingPredicate); } CodePredicate codePredicate = method as CodePredicate; if (codePredicate != null) { return OnLibraryCallCodePredicate(instruction, codePredicate); } // Library method not found. // return ExecutionResults.Failure; }
private ExecutionResults OnLibraryCallPredicate(WamInstruction instruction, Predicate predicate) { WamReferenceTarget[] arguments = new WamReferenceTarget[instruction.Functor.Arity]; for (int index = 0; index < instruction.Functor.Arity; ++index) { arguments[index] = ArgumentRegisters[index]; } bool result; try { result = predicate.PredicateDelegate(this, arguments); } catch { // Backtrack on exception. // return ExecutionResults.Backtrack; } if (result == false) { return ExecutionResults.Backtrack; } InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; }
public override string ToString() { return(WamInstruction.ToString()); }
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; }