⬆️ ⬇️

JBFD: Brainfuck to Java decompiler

Someone once said: “Take some arbitrary Latin letters, add J before them, and you will get another Java technology.” This article focuses on JBFD technology, which means Java BrainFuck Decompiler. The technology is still quite young (at most 3 hours), so do not judge strictly.



The idea of ​​creating a decompiler was not accidental. All because of a large number of articles on BrainFuck on the Internet in general and on Habre in particular. There are a lot of interpreters of this wonderful language, but I managed to find very few tools for debugging BF code.



But what if you convert the code from BF to your native language and take full advantage of your favorite IDE to detect subtle errors, cover the code with tests, optimize, etc.? All this and much more is possible with JBFD.



For impatient, the link to download the source code of the decompiler is here - JBFD.java . The code works on JDK version 1.5 and higher.

')

After compiling JBFD.java and running the program without arguments, we get:



  Usage: java JBFD << filename >> << options >>
 Options:
	 -p - just print cleaned up source code


The -p option is optional. Maybe it will be useful for someone to validate the code and throw it out of the source of translation of strings and comments. During validation, only extra or missing ']' and '[' characters are found so far. In the case of an extra ']', the line and symbol number in the source file is still indicated.



For example, let's try to decompile a simple echo.bf program:



  , + [-., +]


After executing the 'java JBFD echo.bf' command, the file echo.bf.java will appear in the current folder with the following contents:



class Program { static class FuckedByte { char value; FuckedByte(int value) { this.value = (char) value; } char get() { return value; } void inc(int i) { value += i; value %= 256; } void dec(int i) { value -= i; value %= 256; if (value < 0) value += 256; } static FuckedByte read() { try { return new FuckedByte(System.in.read()); } catch (Exception e) { throw new RuntimeException("Error reading from System.in"); } } void write() { System.out.print(value); } } public static void main(String[] args) { FuckedByte fb0 = new FuckedByte(0); fb0 = FuckedByte.read(); fb0.inc(1); while (fb0.get() != 0) { fb0.dec(1); fb0.write(); fb0 = FuckedByte.read(); fb0.inc(1); } } } 




The contents of the main method are decompiled code with Brainfuck, all the rest is to simplify the work. Now the file can be compiled, debugged, covered with tests, obfuscated, in general, to do with it everything that allows you your imagination.



The decompiler has been tested so far only on the most simple programs like echo, hello world, etc. The decompiler code was written in haste and will be refined. Nevertheless, now I will be grateful for any comments and suggestions for improvement.



Good luck to all.

Source: https://habr.com/ru/post/116160/



All Articles