Giter Club home page Giter Club logo

bf's Introduction

An optimizing Brainf*ck compiler

An optimizing brainf*ck compiler with multiple target backends: JVM using ProGuardCORE for code generation, smali, dex using BAT, C, LLVM IR, ARM assembly, WASM and JavaScript.

Some optimizations are applied before code generation:

  • Zero-ing loops ([+] / [-]) are represented as a single instruction
  • Consecutive zero-ing instructions are merged
  • Consecutive move and add instructions are merged into single instructions with an amount parameter
  • Zero moves/adds are removed
  • Top-level loops before memory updates are removed

Building

./gradlew build

The build task will execute all tests and create an output jar lib/bf.jar.

Executing

A wrapper script bin/bf is provided for convenience in the bin/ directory:

$ bin/bf --help
Usage: bf options_list
Arguments: 
    script -> brainf*ck script { String }
Options: 
    --output, -o -> output { String }
    --target, -t [JVM] -> target { Value should be one of [jvm, c, llvm, smali] }
    --debug, -d [false] 
    --help, -h -> Usage info

By default, bf will compile a provided brainf*ck script for the JVM and execute it. The compiler can instead generate a jar file with the -o option.

JVM (default)

$ bin/bf examples/helloworld.bf -t jvm -o helloworld.jar
$ java -jar helloworld.jar

Smali (Dalvik assembler)

$ bin/bf examples/helloworld.bf -t smali -o helloworld.smali
$ smali a helloworld.smali -o classes.dex
$ adb push classes.dex /sdcard/Download/classes.dex
$ adb shell dalvikvm -cp /sdcard/Download/classes.dex Main

Dex

$ bin/bf examples/helloworld.bf -t dex -o helloworld.dex
$ adb push helloworld.dex /sdcard/Download/helloworld.dex
$ adb shell dalvikvm -cp /sdcard/Download/helloworld.dex Main

C

$ bin/bf examples/helloworld.bf -t c -o helloworld.c
$ gcc helloworld.c -o helloworld && ./helloworld

LLVM IR

$ bin/bf examples/helloworld.bf -t llvm -o helloworld.ll
$ lli helloworld.ll

ARM assembly

$ bin/bf examples/helloworld.bf -t arm -o helloworld.s
$ arm-none-eabi-as helloworld.s -o helloworld.o
$ arm-none-eabi-ld helloworld.o -o helloworld
$ qemu-arm ./helloworld

WASM

$ bin/bf examples/helloworld.bf -t wasm -o helloworld.wat
$ wasmtime helloworld.wat

JavaScript

$ bin/bf examples/helloworld.bf -t js -o helloworld.js
$ nodejs helloworld.js

Inputs for the Javascript version are passed as command-line arguments.

Lox

Lox doesn't provide any built-in way to convert ASCII character codes to characters, and the built-in print function always prints newlines, but we can use awk to convert ASCII codes to characters while merging all the lines together.

$ bin/bf examples/helloworld.bf -t lox -o helloworld.lox
$ jlox helloworld.lox | awk '{printf("%c", $1)}' ORS=' '

Lox also doesn't provide any ability to receive inputs so the inputs to a program can be compiled directly into it by using the bf --input option.

Useful brainf*ck resources

bf's People

Contributors

mrjameshamilton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.