Example #1
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);
        }
Example #2
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);
        }
Example #3
0
            /// <summary>
            /// Creates a new Brainf*ck/PBrain session with the given parameters
            /// </summary>
            /// <param name="source">The source code to parse and execute</param>
            /// <param name="breakpoints">The sequence of indices for the breakpoints to apply to the script</param>
            /// <param name="stdin">The input buffer to read data from</param>
            /// <param name="machineState">The target machine state to use to run the script</param>
            /// <param name="executionToken">A <see cref="CancellationToken"/> that can be used to halt the execution</param>
            /// <param name="debugToken">A <see cref="CancellationToken"/> that is used to ignore/respect existing breakpoints</param>
            /// <returns>An <see cref="Option{T}"/> of <see cref="InterpreterSession"/> instance with the results of the execution</returns>
            public static Option <InterpreterSession> TryCreateSession(
                ReadOnlySpan <char> source,
                ReadOnlySpan <int> breakpoints,
                ReadOnlyMemory <char> stdin,
                TuringMachineState machineState,
                CancellationToken executionToken,
                CancellationToken debugToken)
            {
                MemoryOwner <Brainf_ckOperator> opcodes = Brainf_ckParser.TryParse <Brainf_ckOperator>(source, out SyntaxValidationResult validationResult) !;

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

                // Initialize the temporary buffers
                MemoryOwner <bool>  breakpointsTable = LoadBreakpointsTable(source, validationResult.OperatorsCount, breakpoints);
                MemoryOwner <int>   jumpTable        = LoadJumpTable(opcodes.Span, out int functionsCount);
                MemoryOwner <Range> functions        = MemoryOwner <Range> .Allocate(ushort.MaxValue, AllocationMode.Clear);

                MemoryOwner <ushort>     definitions = LoadDefinitionsTable(functionsCount);
                MemoryOwner <StackFrame> stackFrames = MemoryOwner <StackFrame> .Allocate(Specs.MaximumStackSize);

                // Initialize the root stack frame
                stackFrames.DangerousGetReference() = new StackFrame(new Range(0, opcodes.Length), 0);

                // Create the interpreter session
                InterpreterSession session = new InterpreterSession(
                    opcodes,
                    breakpointsTable,
                    jumpTable,
                    functions,
                    definitions,
                    stackFrames,
                    stdin,
                    machineState,
                    executionToken,
                    debugToken);

                return(Option <InterpreterSession> .From(validationResult, session));
            }