Kengo's blog

Technical articles about original projects, JVM, Static Analysis and TypeScript.

released Brainf*ck implementation on Java7

 I've released Brainjack at GutHub. It's a Java7 implementation of the Brainf*ck programming language.

 Sadly, there are no Java7 features except multi-catch. I want to use invokedynamic with ObjectWeb ASM 4.0, but there is no chance because Brainf*ck is too simple. I used only invokespecial, invokevirtual and other 15 classic opcodes.

how about Brainjack works

 Brainjack has 2 modes to run, INTERPRET mode and COMPILE mode.

 What INTERPRET mode does is just executing commands. You can give commands as -c option.

$ java -jar Brainjack.jar interpret -c "+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.------------.<++++++++.--------.+++.------.--------.>+."
Hello, world!

$ echo 'Hello, world!' | java -jar Brainjack.jar interpret -c ">,[>,]<[.<]"
!dlrow ,olleH

 COMPILE mode creates .class file. This .class file has main method to execute given commands, so you can execute your commands by java command.

$ java -jar Brainjack.jar compile -c "+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.------------.<++++++++.--------.+++.------.--------.>+." -d . -n HelloWorld
$ ls *.class
$ java HelloWorld
Hello, world!

$ java -jar Brainjack.jar compile -c ">,[>,]<[.<]" -d . -n Reverse
$ echo 'Hello, world!' | java Reverse
!dlrow ,olleH

 Codes to create class file is simple, methods for each commands are less than 20 lines. I don't use private methods to extract... Brainf*ck specification may be enough simple for JVM opcodes.

 I use args4j to parse command line options. It's very useful because it can handle command line options as String, Number, File and enum etc.


 What I want to fix is logics shared between interpreter and compiler. It's very dirty because I switched it to Visitor pattern from capsuled logics in entity. Refactoring it will make Main class more simple I think.

 And there is another strange code -- an abstract class knows its sub classes very well. I wrote it as enum at first, and I changed it to class. I don't have switched my idea yet.

about next iteration

 I have a schedule to develop next version. I'll try to use CloudBees next week, so Brainjack will have a Maven repository and Jenkins job on cloud. I'll try to use plugins especially for code coverages.

 And I want to use maven-release-plugin, I never used, to release jars to Maven repository and create tags on git. I've checked how to use it, but I cannot test it because there is no Maven repository in my private development environment.

$ mvn release:prepare
$ mvn release:perform -DpushChanges=false