BrainFuck Interpreter, C# Console Application


Brainfuck is a mini programming language that has only 8 keywords, i.e. 8 types of operations expressed by 8 characters. In [here], the Brainfuck is introduced and a python interpreter is presented. Following this link, you will be able to interpret the source code of Brainfuck programming language online.

Today, in order to practice my C# programming, I wrote the following C# Console application (using .NET framework 4.0), which also reviews the Brainfuck programming language.

/*
 * Author: Dr justyy
 * Website: https://HelloACM.com
 * Website: https://rot47.net
 * Version: 0.1
 * BrainFuck Interpreter
 * 
*/
using System;
using System.IO;

namespace BrainFuck
{
    class BrainFuckInterpreter
    {
        private static string VER = "0.0.0.1";
        private static readonly int BUFSIZE = 65535;
        private int[] buf = new int[BUFSIZE];
        private int ptr { get; set; }
        private bool echo { get; set; }

        public BrainFuckInterpreter()
        {
            this.ptr = 0;
            this.Reset();
        }

        public static void PrintHelp()
        {
            Console.WriteLine("BrainFuck Interpreter " + VER);
            Console.WriteLine("https://helloacm.com");
            Console.WriteLine("Parameter: -h: Print Help");
            Console.WriteLine("Parameter: -e: Enable Echo Input Text");
            Console.WriteLine("Parameter: -d: Disable Echo Input Text");
            Console.WriteLine("Parameter: -p: Enable Keyboard Input");
            Console.WriteLine("Parameter: -v: Print Version");
            Console.WriteLine("Parameter: FileName");
        }

        public void Reset()
        {
            Array.Clear(this.buf, 0, this.buf.Length);
        }

        public void Interpret(string s)
        {
            int i = 0;
            int right = s.Length;
            while (i < right)
            {
                switch (s[i])
                {
                    case '>':
                        {
                            this.ptr++;
                            if (this.ptr >= BUFSIZE)
                            {
                                this.ptr = 0;
                            }
                            break;
                        }
                    case '<':
                        {
                            this.ptr--;
                            if (this.ptr < 0)
                            {
                                this.ptr = BUFSIZE - 1;
                            }
                            break;
                        }
                    case '.':
                        {
                            Console.Write((char)this.buf[this.ptr]);
                            break;
                        }
                    case '+':
                        {
                            this.buf[this.ptr]++;
                            break;
                        }
                    case '-':
                        {
                            this.buf[this.ptr]--;
                            break;
                        }
                    case '[':
                        {
                            if (this.buf[this.ptr] == 0)
                            {
                                int loop = 1;
                                while (loop > 0)
                                {
                                    i ++;
                                    char c = s[i];
                                    if (c == '[')
                                    {
                                        loop ++;
                                    }
                                    else
                                    if (c == ']')
                                    {
                                        loop --;
                                    }
                                }
                            }
                            break;
                        }
                    case ']':
                        {
                            int loop = 1;
                            while (loop > 0)
                            {
                                i --;
                                char c = s[i];
                                if (c == '[')
                                {
                                    loop --;
                                }
                                else
                                if (c == ']')
                                {
                                    loop ++;
                                }
                            }
                            i --;
                            break;
                        }
                    case ',':
                        {
                            // read a key
                            ConsoleKeyInfo key = Console.ReadKey(this.echo);
                            this.buf[this.ptr] = (int)key.KeyChar;
                            break;
                        }
                }
                i++; 
            }
        }

        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Try -h, Thanks!");
            }
            else
            {
                BrainFuckInterpreter bf = new BrainFuckInterpreter();
                foreach (string s in args)
                {
                    if (s[0] == '-') // switch options
                    {
                        for (int i = 1; i < s.Length; i++)
                        {
                            switch (s[i])
                            {
                                case 'h':
                                    {
                                        PrintHelp();
                                        break;
                                    }
                                case 'd':
                                    {
                                        bf.echo = false;
                                        break;
                                    }
                                case 'v':
                                    {
                                        Console.WriteLine(VER);
                                        break;
                                    }
                                case 'e':
                                    {
                                        bf.echo = true;
                                        break;
                                    }
                                case 'p':
                                    {
                                        string src = Console.In.ReadToEnd();
                                        bf.Interpret(src);
                                        break;
                                    }
                            }
                        }
                    }
                    else
                    {
                        if (File.Exists(s))
                        {
                            bf.Interpret(File.ReadAllText(s));
                        }
                        else
                        {
                            Console.WriteLine("File Open Error: " + s);
                        }
                    }
                }
            }
        }
    }
}

The above source code can be download at github: and the program looks like following.

bfc BrainFuck Interpreter, C# Console Application

The pre-compiled .NET 4.0 Console application can be download [here]counter?id=5 BrainFuck Interpreter, C# Console Application. The interpreter can take a parameter that specifies the input source file or it can be used in parameter -p to take inputs at runtime from the stdin. This is useful and can be used in pipeline or redirection. e.g. brainfuck.exe < source.

–EOF (The Ultimate Computing & Technology Blog) —

646 words
Last Post: Compute PowerMod a^b%n
Next Post: Codeforces: 237A Free Cash

The Permanent URL is: BrainFuck Interpreter, C# Console Application (AMP Version)

Leave a Reply