Example #1
0
 /// <summary>
 /// Creates a new <see cref="InterpreterSession"/> with the specified parameters
 /// </summary>
 /// <param name="opcodes">The sequence of parsed opcodes to execute</param>
 /// <param name="breakpoints">The table of breakpoints for the current executable</param>
 /// <param name="jumpTable">The jump table for loops and function declarations</param>
 /// <param name="functions">The mapping of functions for the current execution</param>
 /// <param name="definitions">The lookup table to check which functions are defined</param>
 /// <param name="stackFrames">The sequence of stack frames for the current execution</param>
 /// <param name="stdin">The input <see cref="ReadOnlyMemory{T}"/> to read characters 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>
 internal InterpreterSession(
     MemoryOwner <Brainf_ckOperator> opcodes,
     MemoryOwner <bool> breakpoints,
     MemoryOwner <int> jumpTable,
     MemoryOwner <Range> functions,
     MemoryOwner <ushort> definitions,
     MemoryOwner <StackFrame> stackFrames,
     ReadOnlyMemory <char> stdin,
     TuringMachineState machineState,
     CancellationToken executionToken,
     CancellationToken debugToken)
 {
     Opcodes        = opcodes;
     Breakpoints    = breakpoints;
     JumpTable      = jumpTable;
     Functions      = functions;
     Definitions    = definitions;
     StackFrames    = stackFrames;
     MachineState   = machineState;
     StdinBuffer    = new StdinBuffer(stdin);
     StdoutBuffer   = StdoutBuffer.Allocate();
     ExecutionToken = executionToken;
     DebugToken     = debugToken;
     Stopwatch      = new Stopwatch();
     SourceCode     = Brainf_ckParser.ExtractSource(opcodes.Span);
 }
Example #2
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());
        }
Example #3
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, "[]+++++[>++<-]>");
        }
Example #4
0
        public void ExtractSource()
        {
            Span <Brainf_ckOperator> operators = stackalloc Brainf_ckOperator[]
            {
                Operators.Plus,
                Operators.Minus,
                Operators.ForwardPtr,
                Operators.BackwardPtr,
                Operators.PrintChar,
                Operators.ReadChar,
                Operators.LoopStart,
                Operators.LoopEnd,
                Operators.FunctionStart,
                Operators.FunctionEnd,
                Operators.FunctionCall
            };

            string source = Brainf_ckParser.ExtractSource(operators);

            Assert.IsNotNull(source);
            Assert.AreEqual(source, "+-><.,[]():");
        }