Skip to content

deyles-zz/brainjvm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

brainjvm

What is brainjvm?

BrainJVM is a simple Brainf*ck parser and compiler that generates java source code, as well as JVM byte code. The byte code assembler uses the Java 6 JavaCompiler libraries as part of a two stage compilation pipeline (Brainf*ck -> Java -> JVM byte code).

How do I use it?

The package includes output writer classes allowing you to write the resulting compiled code to a file on disk, memory as a byte array, a StringBuffer instance or to STDOUT.

Below is an example compiling Brainf*ck to Java source and writing it to the console:

package com.mypackage;

import brainjvm.compiler.BrainfuckCompiler;
import brainjvm.utils.BrainfuckConsoleOutputWriter;

public class WriteJavaSource {

    public static void main(String[] args) {
        BrainfuckCompiler instance = new BrainfuckCompiler();
        BrainfuckConsoleOutputWriter writer = new BrainfuckConsoleOutputWriter();
        writer.setRaw(false);
        instance.setOutputWriter(writer);
        instance.compile("HelloWorld", "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.\ 
>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
    }

}

Here's an example compiling Brainf*ck to JVM byte code and writing it to a file:

package com.mypackage;

import brainjvm.assembler.BrainfuckAssembler;
import brainjvm.utils.BrainfuckFileOutputWriter;

public class WriteJVMByteCode {

    public static void main(String[] args) {
        BrainfuckFileOutputWriter writer = new BrainfuckFileOutputWriter("HelloWorld", "/tmp/");
        writer.setRaw(true);
        BrainfuckAssembler instance = new BrainfuckAssembler();
        instance.setOutputWriter(writer);
        instance.compile("HelloWorld", "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.\
+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
    }

}

I won't bother writing examples for every possible use case, you can just generate and read the javadoc for the project if you want to know more about the inner workings of the API.

Why?

I dunno, why not? I got interested in screwing around with Brainf*ck while writing brainf*ckjs and wanted to write a simple compiler instead of an interpreter.

Originally I was planning on writing the assembler using Apache BCEL, but after playing around with it for a while I realized I was going to end up ploughing way too many hours into the project and instead chose to use the JavaCompiler package included in Java 6. BCEL is pretty neat, you should definitely check it out, I doubt you'll end up assembling more efficient byte code than the javac compiler though.

If you want to see the actual byte code for the classes generated by brainjvm just use the javap cli utility included in the Java toolchain:

javap -c -private HelloWorld

License yadda yadda yadda

Copyright (c) 2013, Dan Eyles (dan [at] irlgaming [dot] com) All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of IRL Gaming nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IRL Gaming BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

About

BrainJVM - a JVM byte code compiler for Brainf*ck, written using Java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages