コード例 #1
0
        public Option <InterpreterResult> TryRun()
        {
            Guard.IsNotNull(Source, nameof(Source));

            using MemoryOwner <Brainf_ckOperation>?operations = Brainf_ckParser.TryParse <Brainf_ckOperation>(Source.Value.Span, out SyntaxValidationResult validationResult);

            if (!validationResult.IsSuccess)
            {
                return(Option <InterpreterResult> .From(validationResult));
            }

            if (InitialState is TuringMachineState initialState)
            {
                Guard.IsNull(MemorySize, nameof(MemorySize));
                Guard.IsNull(OverflowMode, nameof(OverflowMode));

                initialState = (TuringMachineState)initialState.Clone();
            }
            else
            {
                int size = MemorySize ?? Specs.DefaultMemorySize;

                Guard.IsBetweenOrEqualTo(size, Specs.MinimumMemorySize, Specs.MaximumMemorySize, nameof(MemorySize));

                initialState = new TuringMachineState(size, OverflowMode ?? Specs.DefaultOverflowMode);
            }

            InterpreterResult result = Brainf_ckInterpreter.Release.Run(
                operations !.Span,
                Stdin.GetValueOrDefault().Span,
                initialState,
                ExecutionToken);

            return(Option <InterpreterResult> .From(validationResult, result));
        }
コード例 #2
0
        public void EmptyStackTrace()
        {
            using MemoryOwner <Brainf_ckOperator>?operators = Brainf_ckParser.TryParse <Brainf_ckOperator>("++[>++>-]>+", out _);

            Assert.IsNotNull(operators);

            using MemoryOwner <StackFrame> stackFrames = MemoryOwner <StackFrame> .Allocate(512);

            stackFrames.DangerousGetReference() = new StackFrame(new Range(0, operators !.Length), 10);

            HaltedExecutionInfo?exceptionInfo = Brainf_ckInterpreter.LoadDebugInfo(operators.Span, stackFrames.Span, -1);

            Assert.IsNull(exceptionInfo);
        }
コード例 #3
0
        public void ValidateReleaseCompression()
        {
            Span <Brainf_ckOperation> operations = stackalloc[]
            {
                new Brainf_ckOperation(Operators.Plus, 5),
                new Brainf_ckOperation(Operators.Minus, 4),
                new Brainf_ckOperation(Operators.ForwardPtr, 7),
                new Brainf_ckOperation(Operators.BackwardPtr, 3),
                new Brainf_ckOperation(Operators.ForwardPtr, 1),
                new Brainf_ckOperation(Operators.BackwardPtr, 1),
                new Brainf_ckOperation(Operators.ForwardPtr, 2),
                new Brainf_ckOperation(Operators.FunctionStart, 1),
                new Brainf_ckOperation(Operators.Plus, 5),
                new Brainf_ckOperation(Operators.FunctionEnd, 1),
                new Brainf_ckOperation(Operators.FunctionCall, 1),
                new Brainf_ckOperation(Operators.FunctionCall, 1),
                new Brainf_ckOperation(Operators.FunctionCall, 1),
                new Brainf_ckOperation(Operators.FunctionCall, 1),
                new Brainf_ckOperation(Operators.FunctionCall, 1),
                new Brainf_ckOperation(Operators.FunctionCall, 1),
                new Brainf_ckOperation(Operators.LoopStart, 1),
                new Brainf_ckOperation(Operators.LoopStart, 1),
                new Brainf_ckOperation(Operators.Plus, 5),
                new Brainf_ckOperation(Operators.LoopEnd, 1),
                new Brainf_ckOperation(Operators.LoopEnd, 1),
                new Brainf_ckOperation(Operators.FunctionCall, 1),
                new Brainf_ckOperation(Operators.Plus, 15),
                new Brainf_ckOperation(Operators.Minus, 15),
                new Brainf_ckOperation(Operators.PrintChar, 1),
                new Brainf_ckOperation(Operators.PrintChar, 1),
                new Brainf_ckOperation(Operators.ReadChar, 1),
                new Brainf_ckOperation(Operators.ReadChar, 1),
            };

            string script = Brainf_ckParser.ExtractSource(operations);

            using MemoryOwner <Brainf_ckOperation>?buffer = Brainf_ckParser.TryParse <Brainf_ckOperation>(script, out SyntaxValidationResult result);

            Assert.IsTrue(result.IsSuccess);
            Assert.AreEqual(result.ErrorType, SyntaxError.None);
            Assert.AreEqual(result.ErrorOffset, -1);
            Assert.AreEqual(result.OperatorsCount, script.Length);

            CollectionAssert.AreEqual(operations.ToArray(), buffer !.Span.ToArray());
        }
コード例 #4
0
        public void RootBreakpoint()
        {
            using MemoryOwner <Brainf_ckOperator>?operators = Brainf_ckParser.TryParse <Brainf_ckOperator>("++[>++>-]>+", out _);

            Assert.IsNotNull(operators);

            using MemoryOwner <StackFrame> stackFrames = MemoryOwner <StackFrame> .Allocate(512);

            stackFrames.DangerousGetReference() = new StackFrame(new Range(0, operators !.Length), 7);

            HaltedExecutionInfo?exceptionInfo = Brainf_ckInterpreter.LoadDebugInfo(operators.Span, stackFrames.Span, 0);

            Assert.IsNotNull(exceptionInfo);
            Assert.AreEqual(exceptionInfo !.StackTrace.Count, 1);
            Assert.AreEqual(exceptionInfo.StackTrace[0], "++[>++>-");
            Assert.AreEqual(exceptionInfo.HaltingOperator, '-');
            Assert.AreEqual(exceptionInfo.HaltingOffset, 7);
        }
コード例 #5
0
        public void TryParseInReleaseMode()
        {
            const string script = "[\n\tTest script\n]\n+++++[\n\t>++ 5 x 2 = 10\n\t<- Loop decrement\n]\n> Move to cell 1";

            using MemoryOwner <Brainf_ckOperation>?operations = Brainf_ckParser.TryParse <Brainf_ckOperation>(script, out SyntaxValidationResult result);

            Assert.IsTrue(result.IsSuccess);
            Assert.AreEqual(result.ErrorType, SyntaxError.None);
            Assert.AreEqual(result.ErrorOffset, -1);
            Assert.AreEqual(result.OperatorsCount, 15);

            Assert.IsNotNull(operations);
            Assert.AreEqual(operations !.Length, 10);

            string source = Brainf_ckParser.ExtractSource(operations.Span);

            Assert.IsNotNull(source);
            Assert.AreEqual(source, "[]+++++[>++<-]>");
        }
コード例 #6
0
        public void FunctionCallBreakpoint()
        {
            using MemoryOwner <Brainf_ckOperator>?operators = Brainf_ckParser.TryParse <Brainf_ckOperator>("(+>):+", out _);

            Assert.IsNotNull(operators);

            using MemoryOwner <StackFrame> stackFrames = MemoryOwner <StackFrame> .Allocate(512);

            stackFrames.Span[0] = new StackFrame(new Range(0, operators !.Length), 5);
            stackFrames.Span[1] = new StackFrame(new Range(1, 3), 2);

            HaltedExecutionInfo?exceptionInfo = Brainf_ckInterpreter.LoadDebugInfo(operators.Span, stackFrames.Span, 1);

            Assert.IsNotNull(exceptionInfo);
            Assert.AreEqual(exceptionInfo !.StackTrace.Count, 2);
            Assert.AreEqual(exceptionInfo.StackTrace[0], "+>");
            Assert.AreEqual(exceptionInfo.StackTrace[1], "(+>):");
            Assert.AreEqual(exceptionInfo.HaltingOperator, '>');
            Assert.AreEqual(exceptionInfo.HaltingOffset, 2);
        }