public string Run(Script script)
            {
                Option <InterpreterSession> result = Brainf_ckInterpreter
                                                     .CreateDebugConfiguration()
                                                     .WithSource(script.Source)
                                                     .WithStdin(script.Stdin)
                                                     .WithMemorySize(script.MemorySize)
                                                     .WithOverflowMode(script.OverflowMode)
                                                     .TryRun();

                using InterpreterSession enumerator = result.Value !;

                enumerator.MoveNext();
                return(enumerator.Current.Stdout);
            }
예제 #2
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));
            }