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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * 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);
                        }
                    }
                }
            }
        }
    }
}
/*
 * 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 algorithms c # code code library github implementation interpreter / compiler programming languages tools / utilities

The pre-compiled .NET 4.0 Console application can be download [here]counter?id=5 BrainFuck Interpreter, C# Console Application algorithms c # code code library github implementation interpreter / compiler programming languages tools / utilities . 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) —

GD Star Rating
loading...
663 words
Last Post: Compute PowerMod a^b%n
Next Post: Codeforces: 237A Free Cash

The Permanent URL is: BrainFuck Interpreter, C# Console Application

Leave a Reply