Introduction

Brainf*ck is a simple Turing-complete language which only has eight commands. Structure of brainf*ck program is simple, it consists of certain number of cells for storing program data and data pointer (dataptr) that is used for referencing data by program's commands. Size of a cell is not defined but it is usually a single byte or a word for easier handling of EOF marker when dealing with I/O commands.

brainf*ck commands:

>

moves dataptr to the next cell

<

moves dataptr to the previous cell

+

increments cell referenced by dataptr

-

decrements cell referenced by dataptr

[

test value of cell referenced by dataptr if it is equal to 0, command moves execution forward to the command after the matching ] command.

]

moves execution backward to the matching [ command

.

writes single byte stored in a cell referenced by the data pointer to the standard output

,

reads single byte from the standard input and stores it in a cell referenced by the data pointer

Converting a brainf*ck source code to C# equivalent is an easy task using the following mappings:

>

dataptr = dataptr + 1;

<

dataptr = dataptr - 1;

+

cells[dataptr] = cells[dataptr] + 1;

-

cells[dataptr] = cells[dataptr] - 1;

[

for(; cells[dataptr] != 0;) {

]

}

.

Console.Write(cells[dataptr]);

,

cells[dataptr] = Console.Read();

Once the brainf*ck program is converted to C# code, C# compiler can be invoked dynamically to produce executable file.

Generating C# Code using CodeDOM

CodeDOM is part of .NET Framework and allows programmers to build source code tree dynamically. Unlike Expression Trees and Lambda Expressions, code models built using CodeDOM can be used to feed compiler and produce assembly files at run-time.

Comments and Discussions

Awesome article and program example! I really would have liked to see a line by line breakdown before compiling this way if the result has malicious source I could possibly review it... It would also be nice to attempt to show what the cell values were at each point... Otherwise great job!

The language (and it isn't just something the author made up[^]) is called Brainfuck. Get over it. The name accurately describes the language. This latter point is intentional: it is one of a series of esoteric languages designed to amusement with exactly how difficult/abtruse it is.
Sadly I doubt you'll ever understand exactly how good Brainfuck is at achieving its designer's goals.

I absolutely dislike the language's name. Still this has nothing to do with the current article and the author. I have read it and it was a good and enlightening experience to me. I think the purpose of this site's ability to rate stuff is to show whether it is useful and well written. Judging on things you do not know just on the way you perceive them is a bit too impulsive, and does not look good from a different angle. At least it is not appropriate for this particular article.