- Notifications
You must be signed in to change notification settings - Fork11
A high level programming language that compiles into the brainfuck esoteric programming language
License
sunjay/brain
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
brain is a strongly-typed, high-level programming language that compiles intobrainfuck. Its syntax is based on theRust programming language (whichit is also implemented in). Though many Rust concepts will work in brain, itdeviates when necessary in order to better suit the needs of brainfuckprogramming.
brainfuck is an esoteric programming language with only 8 single-byteinstructions:+
,-
,>
,<
,,
,.
,[
,]
. These limited instructionsmake brainfuck code extremely verbose and difficult to write. It can take a longtime to figure out what a brainfuck program is trying to do. brain makes iteasier to create brainfuck programs by allowing you to write in a more readableand understandable language.
The type system makes it possible to detect a variety of logical errors whencompiling, instead of waiting until runtime. This is an extra layer ofconvenience that brainfuck does not have. The compiler takes care of generatingall the necessary brainfuck code to work with the raw bytes in the brainfuckturing machine.
The brain programming language compiles directly into brainfuck. The generatedbrainfuck code can be run by abrainfuck interpreter.brain only targets this interpreter which means that its generated programs areonly guaranteed to work when run with that. The interpreter implements abrainfuck specification specially designed and written for the brainprogramming language project.
The brain compiler is designed to optimize the generated brainfuck code as muchas possible.
- Generate small brainfuck files (use as few instructions as possible)
- Generate memory efficient code (use as few brainfuck cells as possible)
Optimization is an ongoing effort. As the project matures, these goals willbecome more expressed in the compiled output of the program.
For full examples, please see theexamples/
directory. Some examples aren'tfully implemented yet in the compiler.
// cat programletmut ch:[u8;1];whiletrue{// stdin.read_exact() panics if EOF is reached stdin.read_exact(ch); stdout.print(ch);}
Compile this withbrain examples/cat.brn
.
Run this withbrainfuck cat.bf < someinputfile.txt
.
// input requires explicit sizing// always reads exactly this many characters or panics if EOF is reached before then// if this many characters aren't available yet, it waits for you to send that manyletmut b:[u8;5];stdin.read_exact(b);stdout.print(b"b = ", b,b"\n");letmut c:[u8;1];stdin.read_exact(c);stdout.print(b"c = ", c,b"\n");// You can reuse allocated space againstdin.read_exact(b);stdout.print(b"b = ", b,b"\n");
Compile this withbrain examples/input.brn
.
This compiles into the following brainfuck:
,>,>,>,>,>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------------------------------------------------------------.+++++++++++++++++++++++++++++.-----------------------------.--------------------------------<<<<<.>.>.>.>.>++++++++++.----------,>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-------------------------------------------------------------------.+++++++++++++++++++++++++++++.-----------------------------.--------------------------------<.>++++++++++.----------<<<<<<,>,>,>,>,>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------------------------------------------------------------.+++++++++++++++++++++++++++++.-----------------------------.--------------------------------<<<<<<.>.>.>.>.>>++++++++++.----------
Run this after compiling withbrainfuck input.bf < someinputfile.txt
.
For people just looking to use brain, the easiest way to get brain right nowis to first install theCargo package manager for theRust programming language.
NOTE: Until this version is released, these instructions will NOT work. Pleasesee the Usage instructions below for how to manually install the compiler from thesource code.
Then in your terminal run:
cargo install braincargo install brain-brainfuck
If you are upgrading from a previous version, run:
cargo install brain --forcecargo install brain-brainfuck --force
For anyone just looking to compile with the compiler:
- Follow the installation instructions above
- Run
brain yourfile.brn
to compile your brain code - Run
brainfuck yourfile.bf
to run a brainfuck interpreter which willrun your generated brainfuck code
You can also specify an output filename. Runbrain --help
for more information.
For anyone looking to build the source code:
This project contains both the brain compiler and a basic brainfuck interpreter.
Make sure you haveRust and cargo (comes with Rust) installed.
To compile a brain (.brn) file into brainfuck (.bf)
cargo run filename.brn
wherefilename.brn
is the brain program you want to compile
Use--help
to see further options and additional information
cargo run -- --help
If the brain compiler seems to be taking too long or "hanging", try runningcargo build
first to see if the Rust compiler is just taking too long forsome reason.
You can also install the compiler from the source code using this command in therepository's root directory:
cargo install --path .
There are various brain examples in theexamples/
directory which you cancompile into brainfuck using the usage instructions above.
This project would not be possible without the brilliant work of the manyauthors of theEsolang Brainfuck Algorithms page. The entirewiki has been invaluable. That page in particular is the basis for a lot of thecode generation in this compiler. I have contributed many novel brainfuckalgorithms to that page as I come up with them for use in this compiler.
About
A high level programming language that compiles into the brainfuck esoteric programming language