{"id":13545773,"url":"https://github.com/pablojorge/brainfuck","last_synced_at":"2025-04-10T01:10:36.156Z","repository":{"id":2678532,"uuid":"3670680","full_name":"pablojorge/brainfuck","owner":"pablojorge","description":"Collection of BF interpreters/translators in C/C++/ASM/JS/Python/Rust + others","archived":false,"fork":false,"pushed_at":"2022-02-23T11:01:29.000Z","size":338,"stargazers_count":252,"open_issues_count":2,"forks_count":35,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-10T01:10:30.455Z","etag":null,"topics":["assembler","brainfuck","brainfuck-interpreter","brainfuck-language","c","esoteric-language","golang","haskell","javascript","lua","python","rust","wasm"],"latest_commit_sha":null,"homepage":"http://pablojorge.github.io/brainfuck","language":"Rust","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/pablojorge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-03-09T13:03:40.000Z","updated_at":"2025-03-28T15:45:06.000Z","dependencies_parsed_at":"2022-07-08T19:50:21.865Z","dependency_job_id":null,"html_url":"https://github.com/pablojorge/brainfuck","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/pablojorge%2Fbrainfuck","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablojorge%2Fbrainfuck/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablojorge%2Fbrainfuck/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablojorge%2Fbrainfuck/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pablojorge","download_url":"https://codeload.github.com/pablojorge/brainfuck/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137886,"owners_count":21053775,"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":["assembler","brainfuck","brainfuck-interpreter","brainfuck-language","c","esoteric-language","golang","haskell","javascript","lua","python","rust","wasm"],"created_at":"2024-08-01T12:00:16.811Z","updated_at":"2025-04-10T01:10:36.130Z","avatar_url":"https://github.com/pablojorge.png","language":"Rust","funding_links":[],"categories":[":books: Example usages","Rust"],"sub_categories":[":link: [Hello world](en.wikipedia.org/wiki/Brainfuck)"],"readme":"# Brainf\\*ck\n\nA collection of [brainfuck](http://www.muppetlabs.com/~breadbox/bf/) interpreters/translators in [C](#c)/[C++](#c-1)/[asm](#asm)/[Javascript](#javascript)/[Python](#python)/[Rust](#rust) + others. The JS version includes an interactive [\"IDE\"](http://pablojorge.github.io/brainfuck/) that lets you choose between a pure JS or a [Wasm](./pablojorge/brainfuck/blob/master/wasm) engine. There are two JIT implementations: in [C++ ](https://pablojorge.github.io/blog/2020/07/27/bf-jit-compiler-in-cpp.html) and [Rust](https://pablojorge.github.io/blog/2020/09/13/bf-jit-compiler-in-rust.html).\n\n## Contents\n\n- [Intro](#intro)\n- [Programs](#programs)\n- [Languages](#languages)\n    - [asm](#asm)\n    - [C](#c)\n    - [C++](#c-1)\n    - [Golang](#golang) (contributed by [Philip K.](https://github.com/phikal))\n    - [Haskell](#haskell)\n    - [Javascript](#javascript)\n    - [Lua](#lua) (contributed by [François Perrad](https://github.com/fperrad))\n    - [Python](#python)\n    - [Rust](#rust)\n    - [WASM](#wasm)\n- [Benchmark](#benchmark)\n- [See also](#see-also)\n\n## Intro\n\nThis project is just really a playground to try different languages. A nice excuse to do something a bit more complex than a \"hello world\". New BF programs or interpreters in other languages are absolutely welcome!\n\n## Programs\n\nThe `programs` directory contains a gallery of BF programs, most of them taken from other sites dedicated to BF (check [See Also](#See_Also)). Notable mentions:\n\n * [dbf2c.bf](programs/dbf2c.bf): A Brainfuck to C translator.\n * [dbfi.bf](programs/dbfi.bf): A Brainfuck interpreter in Brainfuck.\n * [fibonacci.bf](programs/fibonacci.bf): Fibonacci number generator.\n * [mandelbrot.bf](programs/mandelbrot.bf): Mandelbrot set generator.\n * [numwarp.bf](programs/numwarp.bf): Displays numbers from stdin vertically.\n * [primes.bf](programs/primes.bf): Prime numbers generator.\n * [sierpinski.bf](programs/sierpinski.bf): \"Sierpinsky Triangle\" generator.\n\n## Languages\n\n### asm\n\nSimple interpreter written in x86_64 asm, using Mac OS syscall conventions.\n\nSource: [asm/brainfuck.s](asm/brainfuck.s)\n\n    $ cd asm\n    $ make\n    as -arch x86_64 brainfuck.s -o brainfuck.o\n    ld -e _main -arch x86_64 -lc brainfuck.o -o brainfuck \n    ld: warning: -macosx_version_min not specified, assuming 10.6\n    rm brainfuck.o\n    $ ./brainfuck ../programs/primes.bf\n    Primes up to: 50\n    2 3 5 7 11 13 17 19 23 29 31 37 41 43 47  \n\n### C\n\nSimple implementation, with precalculated jumps.\n\nSource: [c/brainfuck.c](c/brainfuck.c)\n\n    $ cd c\n    $ make brainfuck\n    cc brainfuck.c -o brainfuck\n    $ ./brainfuck ../programs/hello.bf\n    Hello World!\n\n\n### C++\n\nThere are currently two interpreters in different styles, and a JIT compiler (see \u003chttps://pablojorge.github.io/blog/2020/07/27/bf-jit-compiler-in-cpp.html\u003e)\n\nAll tested with Clang 11.0.3 and GCC 10.1.0.\n\nSee more in [cpp](cpp).\n\nQuick example:\n\n    $ cd cpp\n    $ make brainfuck-jit\n    c++ -std=c++14 -g -O3 brainfuck-jit.cpp -o brainfuck-jit\n    $ ./brainfuck-jit ../programs/hello.bf\n    Hello World!\n\n### Golang\n\nSource: [go/brainfuck.go](go/brainfuck.go)\n\nRunning the number warper with the go interpreter:\n\n    $ cd go\n    $ GOPATH=$PWD go run brainfuck.go ../programs/numwarp.bf\n    32\n      /\\\n       / \n    /\\ \\/\n     /\\\n      /\n\nThanks [Philip K.](https://github.com/phikal) for the contribution!\n\n### Haskell\n\nInterpreter source: [haskell/brainfuck.hs](haskell/brainfuck.hs)\n\nTranslator to C: [haskell/bf2c.hs](haskell/bf2c.hs)\n\nTo run the interpreter:\n\n    $ cd haskell\n    $ runhaskell brainfuck.hs ../programs/hello.bf\n    Hello World!\n\nTo translate any BF program to C:\n\n    $ cd haskell\n    $ make ../programs/sierpinski.c\n    runhaskell bf2c.hs \u003c ../programs/sierpinski.bf | indent \u003e sierpinski.c\n    $ make sierpinski\n    cc sierpinski.c -o sierpinski\n    $ ./sierpinski\n    [...]\n\n### Javascript\n\nSource: [javascript/brainfuck.js](javascript/brainfuck.js)\n\nSimple debugger-like UI, using Bootstrap and jQuery (yes, I'm old school) with support for:\n\n  * Memory inspection\n  * Step-by-step execution\n  * Configurable speed (instructions per cycle/delay between cycles)\n  * Gallery of sample programs, loaded on the fly\n  * Choice of pure JS or [WASM](#WASM) engine\n\n![JS UI Screenshot](./javascript/screenshot.png)\n\nThe JS version can run in stand-alone mode, but to enable the WASM engine a minimal HTTP server is needed. In the `./javascript` directory, run:\n\n```\n$ cd javascript\n$ python -m SimpleHTTPServer\nServing HTTP on 0.0.0.0 port 8000 ...\n```\n\nThen connect to \u003chttp://localhost:8000\u003e\n\nAlternatively, use the online version available at \u003chttp://pablojorge.github.io/brainfuck\u003e.\n\n### Lua\n\nInterpreter source: [lua/brainfuck.lua](lua/brainfuck.lua)\n\nTranslator source: [lua/bf2lua.lua](lua/bf2lua.lua)\n\nThe interpreter is compatible with Lua 5.1, 5.2 and 5.3, and runs with [LuaJIT](http://luajit.org/). To run it:\n\n    $ cd lua\n    $ lua brainfuck.lua ../programs/hello.bf\n    Hello World!\n\nRunning the same program, but the version translated to Lua:\n\n    $ cd lua\n    $ lua bf2lua.lua ../programs/hello.bf | lua -\n    Hello World!\n\nThanks [François Perrad](https://github.com/fperrad) for the contribution!\n\n### Python\n\nOriginal, unoptimized, verbose interpreter: [python/brainfuck.py](python/brainfuck.py).\n\nOptimized (no method lookups, pre-computed jumps over minified source): [python/brainfuck-simple.py](python/brainfuck-simple.py).\n\nSlightly modified version of the optimized interpreter so it can be translated to C, using RPython: [python/brainfuck-rpython.py](python/brainfuck-rpython.py)\n\nJIT-enabled version: [python/brainfuck-rpython-jit.py](python/brainfuck-rpython-jit.py)\n\nUsing the plain python interpreter to run a \"helloworld\" program:\n\n    $ cd python\n    $ cat \u003c\u003c EOF | python brainfuck.py \n    \u003e ++++++++++[\u003e+++++++\u003e++++++++++\u003e+++\u003e+\u003c\u003c\u003c\u003c-]\u003e++.\u003e+.+++++++..+++.\u003e++.\u003c\u003c+++++++++++++++.\u003e.+++.------.--------.\u003e+.\u003e.\n    \u003e EOF\n    Hello World!\n\nTo use the RPython version: \n\n    $ cd \u003cpypy-source\u003e\n    $ python rpython/translator/goal/translate.py ~/Projects/github/brainfuck/python/brainfuck-rpython.py\n    $ time ./brainfuck-rpython-c ~/Projects/github/brainfuck/programs/mandelbrot.bf\n    [...]\n    real  0m29.978s\n    user  0m29.796s\n    sys  0m0.110s\n\nTo use the JIT-enabled version: (thanks [Gsam](https://github.com/GSam) for the suggestion)\n\n    $ python rpython/translator/goal/translate.py --opt=jit ~/Projects/github/brainfuck/python/brainfuck-rpython-jit.py\n    $ time ./brainfuck-rpython-jit-c ~/Projects/github/brainfuck/programs/mandelbrot.bf\n    [...]\n    real  0m4.943s\n    user  0m4.867s\n    sys  0m0.043s\n\n### Rust\n\nInterpreter source: [rust/src/lib.rs](rust/src/lib.rs). Includes improved optimizations (compressing identical contiguous operations)\n\nTranslator source: [rust/src/bin/translate.rs](rust/src/bin/translate.rs). Supports translating to Rust and C.\n\nJIT version: [rust/src/bin/jit.rs](rust/src/bin/jit.rs). See \u003chttps://pablojorge.github.io/blog/2020/09/13/bf-jit-compiler-in-rust.html\u003e.\n\nTo run the interpreter:\n\n```\n$ cd rust\n$ cargo run ../programs/primes.bf\n    Finished dev [optimized + debuginfo] target(s) in 0.01s\n     Running `target/debug/main ../programs/primes.bf`\nPrimes up to: 50\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47\n```\n\nTo translate to Rust:\n\n```\n$ ./translate.sh ../programs/mandelbrot.bf rs\n    Finished dev [optimized + debuginfo] target(s) in 0.02s\n     Running `target/debug/translate rs`\n   Compiling brainfuck v0.1.0 (/Users/pablo/Projects/github/pablojorge/brainfuck/rust)\n    Finished dev [optimized + debuginfo] target(s) in 0.38s\n\nreal\t0m0.419s\nuser\t0m0.299s\nsys\t0m0.134s\n    Finished dev [optimized + debuginfo] target(s) in 0.01s\n     Running `target/debug/mandelbrot`\n[...]\n\nreal\t0m2.049s\nuser\t0m1.976s\nsys\t0m0.047s\n```\n\nAnd to C:\n\n```\n$ ./translate.sh ../programs/mandelbrot.bf c\n    Finished dev [optimized + debuginfo] target(s) in 0.02s\n     Running `target/debug/translate c`\ncc -O1 mandelbrot.c -o mandelbrot\n\nreal\t0m1.603s\nuser\t0m1.448s\nsys\t0m0.114s\n[...]\n\nreal\t0m1.006s\nuser\t0m0.985s\nsys\t0m0.009s\n```\n\n### WASM\n\nRust interpreter, prepared to be compiled to WASM: [wasm/src/lib.rs](wasm/src/lib.rs)\n\nTo generate the WASM binary and JS glue:\n\n    $ cd rust\n    $ wasm-pack build --target web\n\nThat will generate the target libs in `./pkg`. To try in the browser:\n\n    $ python -m SimpleHTTPServer\n\nAnd then connect to \u003chttp://localhost:8000\u003e. Modify the `program` and `input` vars in `index.html` to try other programs.\n\n## Benchmark\n\n|                                        | Mandelbrot  | Primes up to 100 |\n|                                   ---: | :---:       | :---:            |\n| Non-optimized python version (CPython) | 991m45.631s | 19m34.163s       |\n|    Non-optimized python version (PyPy) | 24m59.928s  | 0m28.210s        |\n|    Simplified python version (CPython) | 67m39.287s  | 1m16.431s        |\n|       Simplified python version (PyPy) | 1m35.345s   | 0m2.144s         |\n|      Improved python version (RPython) | 0m29.796s   | 0m0.486s         |\n|      JIT-enabled version (RPython-JIT) | 0m4.867s    | 0m0.107s         |\n|                              Assembler | 1m7.288s    | 0m1.501s         |\n|                    C Interpreter (-O0) | 2m7.036s    | 0m2.012s         |\n|                    C Interpreter (-O1) | 1m7.504s    | 0m1.005s         |\n|                  Translated to C (-O0) | 0m19.674s   | 0m0.243s         |\n|                  Translated to C (-O1) | 0m1.360s    | 0m0.012s         |\n\nNotes:\n\n  * The _same_ code is 40 times slower in CPython vs PyPy. You can have a really big gain for \"free\" (almost), by just using the PyPy interpreter.\n  * Adapting the source to RPython is not free of course, and the gain is not as big, BUT if we explictly add support for the JIT, the gain makes it totally worthy. In fact the gain of this version compared with running the simplified version (unmodified) with PyPy, is comparable to the gain obtained by running the unmodified version with PyPy vs running it with CPython.\n  * The other performance differences are totally expected (C interpreter compiled with optimisations has an equivalent performance to the assembler interpreter, the translated to C version is almost two orders of magnitude faster than the interpreted version, etc.).\n  * The JIT-enabled Python version runs _faster_ than the translated-to-C version (compiled without optimizations).\n\n## See Also\n\n  * \u003chttp://en.wikipedia.org/wiki/Brainfuck\u003e\n  * \u003chttp://esoteric.sange.fi/brainfuck/bf-source/prog/\u003e\n  * [El Brainfuck](http://copy.sh/brainfuck/) by Fabian Hemmer (\u003chttp://copy.sh/\u003e)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpablojorge%2Fbrainfuck","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpablojorge%2Fbrainfuck","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpablojorge%2Fbrainfuck/lists"}