Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Feb 3, 2020. It is now read-only.
/brainPublic archive

A high level programming language that compiles into the brainfuck esoteric programming language

License

NotificationsYou must be signed in to change notification settings

sunjay/brain

Repository files navigation

Crates.ioCrates.ioBuild StatusBuild statusDependency StatusGitter

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.

Optimization Goals

The brain compiler is designed to optimize the generated brainfuck code as muchas possible.

  1. Generate small brainfuck files (use as few instructions as possible)
  2. 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.

brain syntax

For full examples, please see theexamples/ directory. Some examples aren'tfully implemented yet in the compiler.

cat program (examples/cat.brn)

// 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.

Reading Input (examples/input.brn)

// 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.

Installation

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

Usage

For anyone just looking to compile with the compiler:

  1. Follow the installation instructions above
  2. Runbrain yourfile.brn to compile your brain code
  3. Runbrainfuck 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.

brain compiler

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 .

Examples

There are various brain examples in theexamples/ directory which you cancompile into brainfuck using the usage instructions above.

Thanks

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp