{"id":18550893,"url":"https://github.com/mrjameshamilton/bf","last_synced_at":"2025-04-09T22:31:24.079Z","repository":{"id":54013096,"uuid":"489653524","full_name":"mrjameshamilton/bf","owner":"mrjameshamilton","description":"An optimizing brainf*ck compiler with multiple target backends: JVM, smali, dex, C, LLVM IR, ARM, WASM, JavaScript and Lox.","archived":false,"fork":false,"pushed_at":"2023-09-27T19:34:29.000Z","size":105,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T12:47:33.514Z","etag":null,"topics":["arm","assembly","brainfuck","bytecode","compiler","java","javascript","jvm","jvm-bytecode","jvm-language","kotlin","language","llvm","llvm-ir","lox","smali","wasm"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrjameshamilton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-05-07T11:23:07.000Z","updated_at":"2025-03-22T23:58:54.000Z","dependencies_parsed_at":"2023-09-28T01:26:11.955Z","dependency_job_id":null,"html_url":"https://github.com/mrjameshamilton/bf","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrjameshamilton%2Fbf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrjameshamilton%2Fbf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrjameshamilton%2Fbf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrjameshamilton%2Fbf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrjameshamilton","download_url":"https://codeload.github.com/mrjameshamilton/bf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248123531,"owners_count":21051488,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arm","assembly","brainfuck","bytecode","compiler","java","javascript","jvm","jvm-bytecode","jvm-language","kotlin","language","llvm","llvm-ir","lox","smali","wasm"],"created_at":"2024-11-06T21:06:01.278Z","updated_at":"2025-04-09T22:31:23.610Z","avatar_url":"https://github.com/mrjameshamilton.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# An optimizing Brainf*ck compiler\n\nAn optimizing [brainf*ck](http://brainfuck.org/brainfuck.html) compiler with multiple target backends: \nJVM using [ProGuardCORE](https://github.com/guardsquare/proguard-core) for code generation, \n[smali](https://github.com/JesusFreke/smali), dex using [BAT](https://github.com/netomi/bat),\nC, LLVM IR, ARM assembly, WASM and JavaScript.\n\nSome optimizations are applied before code generation:\n\n* Zero-ing loops (`[+]` / `[-]`) are represented as a single instruction\n* Consecutive zero-ing instructions are merged\n* Consecutive move and add instructions are merged into single instructions with an amount parameter\n* Zero moves/adds are removed\n* Top-level loops before memory updates are removed\n\n## Building\n\n```shell\n./gradlew build\n```\n\nThe build task will execute all tests and create an output jar `lib/bf.jar`.\n\n## Executing\n\nA wrapper script `bin/bf` is provided for convenience in the `bin/` directory:\n\n```shell\n$ bin/bf --help\nUsage: bf options_list\nArguments: \n    script -\u003e brainf*ck script { String }\nOptions: \n    --output, -o -\u003e output { String }\n    --target, -t [JVM] -\u003e target { Value should be one of [jvm, c, llvm, smali] }\n    --debug, -d [false] \n    --help, -h -\u003e Usage info\n```\n\nBy default, `bf` will compile a provided brainf*ck script for the JVM and execute it. The compiler\ncan instead generate a jar file with the `-o` option.\n\n### JVM (default)\n \n```shell\n$ bin/bf examples/helloworld.bf -t jvm -o helloworld.jar\n$ java -jar helloworld.jar\n```\n\n### Smali (Dalvik assembler)\n\n```shell\n$ bin/bf examples/helloworld.bf -t smali -o helloworld.smali\n$ smali a helloworld.smali -o classes.dex\n$ adb push classes.dex /sdcard/Download/classes.dex\n$ adb shell dalvikvm -cp /sdcard/Download/classes.dex Main\n```\n\n### Dex\n\n```shell\n$ bin/bf examples/helloworld.bf -t dex -o helloworld.dex\n$ adb push helloworld.dex /sdcard/Download/helloworld.dex\n$ adb shell dalvikvm -cp /sdcard/Download/helloworld.dex Main\n```\n\n### C\n\n```shell\n$ bin/bf examples/helloworld.bf -t c -o helloworld.c\n$ gcc helloworld.c -o helloworld \u0026\u0026 ./helloworld\n```\n\n### LLVM IR\n\n```shell\n$ bin/bf examples/helloworld.bf -t llvm -o helloworld.ll\n$ lli helloworld.ll\n```\n\n### ARM assembly\n\n```shell\n$ bin/bf examples/helloworld.bf -t arm -o helloworld.s\n$ arm-none-eabi-as helloworld.s -o helloworld.o\n$ arm-none-eabi-ld helloworld.o -o helloworld\n$ qemu-arm ./helloworld\n```\n\n### WASM\n\n```shell\n$ bin/bf examples/helloworld.bf -t wasm -o helloworld.wat\n$ wasmtime helloworld.wat\n```\n\n### JavaScript\n\n```shell\n$ bin/bf examples/helloworld.bf -t js -o helloworld.js\n$ nodejs helloworld.js\n```\n\nInputs for the Javascript version are passed as command-line arguments.\n\n### Lox\n\n[Lox](https://github.com/munificent/craftinginterpreters) doesn't provide any built-in way \nto convert ASCII character codes to characters, and the built-in `print` function always prints newlines, \nbut we can use `awk` to convert ASCII codes to characters while merging all the lines together.\n\n```shell\n$ bin/bf examples/helloworld.bf -t lox -o helloworld.lox\n$ jlox helloworld.lox | awk '{printf(\"%c\", $1)}' ORS=' '\n```\n\nLox also doesn't provide any ability to receive inputs so the inputs to a program\ncan be compiled directly into it by using the `bf` `--input` option.\n\n# Useful brainf*ck resources\n\n* [Brainf*ck language reference](http://brainfuck.org/brainfuck.html)\n* [Sample programs by Daniel B Cristofani](http://brainfuck.org/)\n* [Optimizing brainf*ck programs](http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.html)\n* [Brainf*ck Wikipedia article](https://en.wikipedia.org/wiki/Brainfuck)\n \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrjameshamilton%2Fbf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrjameshamilton%2Fbf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrjameshamilton%2Fbf/lists"}