Skip to content

annikulin/brainfuck-compiler

Repository files navigation

Brainfuck to JavaScript compiler

Actions Status

Overview

Brainfuck compiler that executes Brainfuck code in Java and transpiles it to JavaScript.

The following phases of the compiler are supported:

  1. Lexical Analysis (Tokenization). The text of the program is broken up into Tokens and kept inside of a computer’s memory;
  2. Parsing. A sequence of Tokens is being analyzed, and a parse model consisting of Statements and Expressions is constructed showing syntactic relation between commands;
  3. Code Optimization. Subsequent increment, decrement, and shift commands are replaced by a single aggregate operation;
  4. Code Generation or Execution:
    • Code Generation. Brainfuck program is transpiled into JavaScript code;
    • Code Execution. Brainfuck program is being interpreted in Java.

Quickstart

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.
Hello World in Brainfuck

Releases

No releases published

Packages

No packages published

Languages