예제 #1
0
        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));
        }
예제 #2
0
        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);
        }
예제 #3
0
        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));
        }
예제 #4
0
 ExecutionResults OnPutUnboundVariable(WamInstruction instruction)
 {
     SetRegister(instruction.SourceRegister, CreateVariable());
     SetRegister(instruction.TargetRegister, GetRegister(instruction.SourceRegister));
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
예제 #5
0
        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);
        }
예제 #6
0
        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!");
        }
예제 #7
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);
        }
예제 #8
0
 ExecutionResults OnAllocate(WamInstruction instruction)
 {
     Environment = new WamEnvironment(Environment, ReturnInstructionPointer, CutChoicePoint);
     ReturnInstructionPointer = WamInstructionPointer.Undefined;
     InstructionPointer       = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
예제 #9
0
 ExecutionResults OnCut(WamInstruction instruction)
 {
     while (ChoicePoint != CutChoicePoint)
     {
         ChoicePoint = ChoicePoint.Predecessor;
     }
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
예제 #10
0
 ExecutionResults OnGetBoundVariable(WamInstruction instruction)
 {
     if (!(Unify(GetRegister(instruction.TargetRegister), GetRegister(instruction.SourceRegister))))
     {
         return(ExecutionResults.Backtrack);
     }
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
예제 #11
0
 ExecutionResults OnGetValue(WamInstruction instruction)
 {
     if (!(Unify(instruction.ReferenceTarget, GetRegister(instruction.SourceRegister))))
     {
         return(ExecutionResults.Backtrack);
     }
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
예제 #12
0
 ExecutionResults OnTrustMe(WamInstruction instruction)
 {
     if (ChoicePoint == null)
     {
         throw new InvalidOperationException("Choice point not found.");
     }
     ChoicePoint        = ChoicePoint.Predecessor;
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
예제 #13
0
        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);
        }
예제 #14
0
        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;
        }
예제 #15
0
        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);
        }
예제 #16
0
        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);
        }
예제 #17
0
        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);
        }
예제 #18
0
        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);
        }
예제 #19
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);
        }
예제 #20
0
        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);
        }
예제 #21
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);
        }
예제 #22
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);
        }
예제 #23
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);
            }
        }
예제 #24
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);
        }
예제 #25
0
        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);
        }
예제 #26
0
        protected override void Ret()
        {
            var dst = (ushort)Pop(PrimitiveType.Word16);

            InstructionPointer = InstructionPointer.NewOffset(dst);
        }
예제 #27
0
 ExecutionResults OnPutValue(WamInstruction instruction)
 {
     SetRegister(instruction.TargetRegister, instruction.ReferenceTarget);
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
예제 #28
0
 ExecutionResults OnPutStructure(WamInstruction instruction)
 {
     SetRegister(instruction.TargetRegister, CreateCompoundTerm(instruction.Functor));
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }
예제 #29
0
 ExecutionResults OnNoop(WamInstruction instruction)
 {
     InstructionPointer = InstructionPointer.GetNext();
     return(ExecutionResults.None);
 }