<commit_before>﻿using System;

namespace BrainTools
{
    /// <summary>
    /// Methods for working with Brainfuck.
    /// </summary>
    public static class Brainfuck
    {
        /// <summary>
        /// Runs the provided Brainfuck code.
        /// </summary>
        /// <param name="code">The code to run.</param>
        public static void Run(string code)
        {
            Tape tape = new Tape();

            char[] program = code.ToCharArray();
            int brackets = 0;
            int codePointer = 0;

            while (codePointer < program.Length)
            {
                switch (program[codePointer])
                {
                    case '+':
                        tape.Cell++;
                        codePointer++;
                        break;
                    case '-':
                        tape.Cell--;
                        codePointer++;
                        break;
                    case '>':
                        tape.Right();
                        codePointer++;
                        break;
                    case '<':
                        tape.Left();
                        codePointer++;
                        break;
                    case '[':
                        if (tape.Cell == 0)
                        {
                            brackets++;
                            while (brackets != 0)
                            {
                                codePointer++;
                                if (program[codePointer] == '[')
                                    brackets++;
                                else if (program[codePointer] == ']')
                                    brackets--;
                            }
                            codePointer++;
                            break;
                        }
                        codePointer++;
                        break;
                    case ']':
                        brackets++;
                        while (brackets != 0)
                        {
                            codePointer--;
                            if (program[codePointer] == ']')
                                brackets++;
                            else if (program[codePointer] == '[')
                                brackets--;
                        }
                        break;
                    case '.':
                        Console.Write((char)tape.Cell);
                        codePointer++;
                        break;
                    case ',':
                        tape.Cell = Console.Read();
                        codePointer++;<commit_msg>BF interpreter now uses raw stdin and stdout for I/O.<commit_after>﻿using System;
using System.IO;

namespace BrainTools
{
    /// <summary>
    /// Methods for working with Brainfuck.
    /// </summary>
    public static class Brainfuck
    {
        /// <summary>
        /// Runs the provided Brainfuck code.
        /// </summary>
        /// <param name="code">The code to run.</param>
        public static void Run(string code)
        {
            Tape tape = new Tape();

            char[] program = code.ToCharArray();
            int brackets = 0;
            int codePointer = 0;

            Stream stdin = Console.OpenStandardInput();
            Stream stdout = Console.OpenStandardOutput();

            while (codePointer < program.Length)
            {
                switch (program[codePointer])
                {
                    case '+':
                        tape.Cell++;
                        codePointer++;
                        break;
                    case '-':
                        tape.Cell--;
                        codePointer++;
                        break;
                    case '>':
                        tape.Right();
                        codePointer++;
                        break;
                    case '<':
                        tape.Left();
                        codePointer++;
                        break;
                    case '[':
                        if (tape.Cell == 0)
                        {
                            brackets++;
                            while (brackets != 0)
                            {
                                codePointer++;
                                if (program[codePointer] == '[')
                                    brackets++;
                                else if (program[codePointer] == ']')
                                    brackets--;
                            }
                            codePointer++;
                            break;
                        }
                        codePointer++;
                        break;
                    case ']':
                        brackets++;
                        while (brackets != 0)
                        {
                            codePointer--;
                            if (program[codePointer] == ']')
                                brackets++;
                            else if (program[codePointer] == '[')
                                brackets--;
                        }
                        break;
                    case '.':
                        stdout.WriteByte((byte)tape.Cell);
                        codePointer++;
                        break;
                    case ',':
                        tape.Cell = stdin.ReadByte();
                        codePointer++;
{'fraction_non_alphanumeric': 0.09365282604719224, 'fraction_numerical': 0.0018291567587342234, 'mean_word_length': 0.6177514792899408, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6914568', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1032, 'n_words': 232}