Brainf*ck .NET Compiler

Overview

The Brainf*ck programming language has only eight commands and is a creation of Urban Müller who originally wrote it for the Amiga. It has been proven that Brainf*ck is Turing complete.

The goal here was to create a Brainf*ck compiler that outputs .NET IL code. The result is Brainf*ck .NET Compiler v1.0. This is probably the simplest .NET compiler around.

Download

The Brainf*ck .NET Compiler is released under the BSD license, and is available for download:

Sample programs

  • hello.b : the obligatory Hello World! example
  • cat.b : reads in a file and prints it out
  • rev.b : reads in a file and prints it out in reversed order
  • prime.b : prints prime numbers
  • pi.b : prints the number Pi
  • fib.b : prints the Fibonacci numbers
  • quine.b : prints itself
  • rot13.b : rot13 encoder
  • bf2ook.b : Brainf*ck to Ook# converter
  • More sources can be found at http://esoteric.sange.fi/brainfuck/.

    Language Specification

    A brainf*ck program uses a data structure that is an array of integer values, initially all set to zero. It also has a pointer which initially points to the beginning of the array. It has eight instructions, each represented as a single character, which are described below:
    >   Increment the pointer
    <   Decrement the pointer
    +   Increment the value at the pointer
    -   Decrement the value at the pointer
    .   Write the value at the pointer to standard output
    ,   Read a value from standard input and store it at the pointer
    [   If the value at the pointer is zero, jump past the matching ]
    ]   Jump to the matching [

    Mono

    The compiler sources have been compiled with csc from ms.net and with mcs from Mono.

    The compilers and the output of these compilers have been tested under ms.net on a win32 system and should also work under Mono using Linux.

    When executing the Brainf*ck program prime.b (or the Ook# equivalant prime.ook) the execution times on the same machine using the same OS for primes up to 200 are on average as follows (per May 26th 2002) :

    Memory usage always stays the same for all environments, about 30KB.

    I have not yet optimized the compilers as at first I'd like to rely on the JIT compilation features of the .NET runtime. Therefor the output of these compilers serve as nice tests for the JIT compiler of Mono. Another nice test program is pi.b which calculates the number Pi.

    Interestingly enough, when the prime.b program is compiled in pure C (see prime.c which was created by compiling bf.c and then running it with the prime.b program), and run on the same machine it takes 20 seconds to compute the primes up to 200. This shows one case where JIT compilation can outperform good-old-fashioned C.
    © 2002 BlueSorcerer.Net
    mailto: Lawrence Pit