public bool Equals(RunningScript other) { if (other == null) { return(false); } return(NextScriptPointer.Equals(other.NextScriptPointer) && PrevScriptPointer.Equals(other.PrevScriptPointer) && Id.Equals(other.Id) && Field10h.Equals(other.Field10h) && InstructionPointer.Equals(other.InstructionPointer) && Stack.SequenceEqual(other.Stack) && StackPointer.Equals(other.StackPointer) && Locals.SequenceEqual(other.Locals) && TimerA.Equals(other.TimerA) && TimerB.Equals(other.TimerB) && Field1FCh.Equals(other.Field1FCh) && WakeTime.Equals(other.WakeTime) && Field204h.Equals(other.Field204h) && Field208h.Equals(other.Field208h) && Field20Ch.Equals(other.Field20Ch) && Field20Dh.Equals(other.Field20Dh) && Field20Eh.Equals(other.Field20Eh) && Name.Equals(other.Name) && Field217h.Equals(other.Field217h)); }
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); }
public bool Equals(RunningScript other) { if (other == null) { return(false); } return(NextScriptPointer.Equals(other.NextScriptPointer) && PrevScriptPointer.Equals(other.PrevScriptPointer) && Name.Equals(other.Name) && InstructionPointer.Equals(other.InstructionPointer) && Stack.SequenceEqual(other.Stack) && StackPointer.Equals(other.StackPointer) && Locals.SequenceEqual(other.Locals) && TimerA.Equals(other.TimerA) && TimerB.Equals(other.TimerB) && ConditionResult.Equals(other.ConditionResult) && IsMissionScript.Equals(other.IsMissionScript) && ClearMessages.Equals(other.ClearMessages) && WakeTime.Equals(other.WakeTime) && AndOrState.Equals(other.AndOrState) && NotFlag.Equals(other.NotFlag) && WastedBustedCheckEnabled.Equals(other.WastedBustedCheckEnabled) && WastedBustedCheckResult.Equals(other.WastedBustedCheckResult) && MissionFlag.Equals(other.MissionFlag)); }
ExecutionResults OnPutUnboundVariable(WamInstruction instruction) { SetRegister(instruction.SourceRegister, CreateVariable()); SetRegister(instruction.TargetRegister, GetRegister(instruction.SourceRegister)); InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
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); }
public void Run() { if (_ip == null) { _ip = new InstructionPointer(); } if (_field == null) { _field = new Field(); } while (true) { if (HandleKey()) { break; } } Console.Clear(); Console.SetCursorPosition(0, 0); _field.Print(); Console.ReadLine(); var runner = new CodeRunner(_field); runner.Run(); Console.WriteLine(Environment.NewLine + "Done!"); }
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 OnAllocate(WamInstruction instruction) { Environment = new WamEnvironment(Environment, ReturnInstructionPointer, CutChoicePoint); ReturnInstructionPointer = WamInstructionPointer.Undefined; InstructionPointer = InstructionPointer.GetNext(); 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); }
protected override void Call(MachineOperand op) { Push((uint)InstructionPointer.ToLinear() + (uint)dasm.Current.Length, PrimitiveType.Word32); // Push return value on stack var dest = XferTarget(op); if (envEmulator.InterceptCall(this, (uint)dest.ToLinear())) { return; } InstructionPointer = dest; }
public void SetInstructionStream(WamInstructionStream instructionStream) { if (instructionStream == null) { throw new ArgumentNullException("instructionStream"); } ReturnInstructionPointer = InstructionPointer.GetNext(); CutChoicePoint = ChoicePoint; TemporaryRegisters.Clear(); StackIndex += 1; InstructionPointer = new WamInstructionPointer(instructionStream); }
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 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 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 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 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); }
public string RealExecute(FungeContext fungeContext) { var newThread = new InstructionPointer { Alive = true, DeltaVector = fungeContext.CurrentThreadDeltaVector.Reflect(), CurrentPosition = fungeContext.CurrentThread.CurrentPosition.Copy(), StorageOffset = fungeContext.CurrentThread.StorageOffset.Copy(), Stacks = new Stack <Stack <int> >(), }; newThread.CurrentPosition += newThread.DeltaVector; foreach (var stack in fungeContext.CurrentThread.Stacks.Reverse()) { newThread.Stacks.Push(new Stack <int>()); foreach (var i in stack.Reverse()) { newThread.Stacks.Peek().Push(i); } } fungeContext.SpawnedThreads.Add(newThread); return(null); }
protected override void Ret() { var dst = (ushort)Pop(PrimitiveType.Word16); InstructionPointer = InstructionPointer.NewOffset(dst); }
ExecutionResults OnPutValue(WamInstruction instruction) { SetRegister(instruction.TargetRegister, instruction.ReferenceTarget); InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); }
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); }