Brainfuck compiler that executes Brainfuck code in Java and transpiles it to JavaScript.
The following phases of the compiler are supported:
- Lexical Analysis (Tokenization). The text of the program is broken up into
Token
s and kept inside of a computer’s memory; - Parsing. A sequence of
Token
s is being analyzed, and a parse model consisting ofStatement
s andExpression
s is constructed showing syntactic relation between commands; - Code Optimization. Subsequent increment, decrement, and shift commands are replaced by a single aggregate operation;
- Code Generation or Execution:
- Code Generation. Brainfuck program is transpiled into JavaScript code;
- Code Execution. Brainfuck program is being interpreted in Java.
Use BrainfuckCompiler
command-line utility to try out how the transpiler works.
To execute a classic "Hello World!", create a file with Brainfuck code:
echo "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++." > /tmp/HelloWorld.b
Then, run the compiler with ./gradlew run
passing arguments for input Brainfuck and output JS files.
./gradlew run --args="-i /tmp/HelloWorld.b -o /tmp/HelloWorld.js"
First, BrainfuckCompiler
executes Brainfuck code in Java, then transpiles it to JavaScript, and evaluates generated script inside the Nashorn JS engine.