Brainf*ck to java compiler and a turing complete brainf*ck interpreter. In java!

I was feeling a bit bored, so I wrote turing complete a brainf*ck interpreter, and an optimising brainf*ck-to-java compiler. :smiley:
Unfortunately, the compiler doesn’t produce a turing complete program, as it limits the cell buffer to 30000 entries.

The compiler turns the following brainf*ck:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

into this java code:

/**
 * Compiled by BrainFuckCompiler by Mojang Specifications
 */
public class Foo
{
    public static void main(String[] args) throws java.io.IOException
    {
        StringBuffer sb = new StringBuffer();
        int p = 0;
        byte[] b = new byte[30000];
        
        b[p]+=10;
        while(b[p]!=0)
        {
            b[++p]+=7;
            b[++p]+=10;
            b[++p]+=3;
            ++b[++p];
            --b[p-=4];
        }
        sb.append((char)(b[++p]+=2)).append((char)(++b[++p])).append((char)(b[p]+=7)).append((char)(b[p])).append((char)(b[p]+=3));
        sb.append((char)(b[++p]+=2)).append((char)(b[p-=2]+=15)).append((char)(b[++p])).append((char)(b[p]+=3)).append((char)(b[p]-=6));
        sb.append((char)(b[p]-=8)).append((char)(++b[++p])).append((char)(b[++p]));
        System.out.print(sb.toString());
    }
}

When that java program is run, or when the corresponding brainf*ck code is run through the interpreter, it spits out the following on standard out:

[quote]Hello World!
[/quote]
The compiled version runs about four-five times as fast with the output heavy “hello world” test program.

If the forums didn’t censor the word “brainfuck” even in urls, you’d be able to click these two links to get the source code:
brainfuck compiler source code
brainfuck interpreter source code

… but now you’ll have to manually enter the urls into your browser if you want the source code. :-\

cool! nice job! :smiley: and I thought I had issues with boredom. geez. :stuck_out_tongue:

for those too lazy to replace those *'s:

brainfuck compiler source code
brainfuck interpeter source code

saturday + red bull + supposed to be working on wurm = insane tangents. :wink:

Thanks for the nicer links. :slight_smile:

woah, that brainf*k is the nerdiest thing i’ve ever seen ;D

i’d use something like jflex lexer to do that job with less effort

Of course it could be done easier! That’s pretty much the POINT of brainfuck. :wink:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>>>+++++.<+++++++.++++.-.+++++.----.—.-------.+++++++++.>>.

(on quakenet) :slight_smile:

A pretty funky js bf interpreter (look at the source) can be found here:
http://www.hevanet.com/cristofd/brainfuck/di.html

I prefer Intercal myself (http://www.catb.org/~esr/intercal/)

Ok, this took far too long, but:

Here’s the source code for a java program
Here’s the source code for a brainfuck program

The first file was generated by running the second file.
The second file was generated by running the first file.
(of course, I had to start by manually creating and running a variant of the first program)

It was really tricky to make a .java file contain its own source code, since the copy of the code also has to include itself to be a proper copy. But I did it!

Markus you’re the most leet haxor I’ve ever seen. That is crazy. Now my question is: have you ever encountered the malebolge language?

[quote]Malbolge was so difficult to understand when it arrived that it took two years for the first Malbolge program to appear. The program was not even written by a human being: it was generated by a beam search algorithm designed by Andrew Cooke and implemented in Lisp.
[/quote]

I’ll take that as a compliment. :smiley:

And, whoa… malebolge looks crazy! CRAAAZZZY!!!
starts poking

Malbolge: I was nodding away at the assembler type registers, thinking Ok, it’s an assembler, that can’t be that hard. Then I read what the instructions did. :o I’m going to have nightmares about that! It’s not crazy, its ‘fcarg’, which is a new word I’ve invented, since none of the ones I can think of adequately express my feelings. And no, I’m not about to try to code for it. Life is too short.

I wrote an interpreter, thinking it’d help me understand the language… and… well…
I understand the syntax, and what the different commands do, but I HAVE NO IDEA HOW TO WRITE PROGRAMS IN IT!! Argh!!

* Markus_Persson agrees with Alan_W

Markus, this is truly incredible! Have you considered extending this to the Ook! programming language? http://en.wikipedia.org/wiki/Ook%21_programming_language

It’s basically just brainf*ck, but offers major improvements in accessibility over other languages. Particularly for orangutans.

This post was flagged by the community and is temporarily hidden.