• This repository has been archived on 03/Feb/2020
  • Stars
    star
    167
  • Rank 226,635 (Top 5 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

brain

Crates.io Crates.io Build Status Build status Dependency Status Gitter

brain is a strongly-typed, high-level programming language that compiles into brainfuck. Its syntax is based on the Rust programming language (which it is also implemented in). Though many Rust concepts will work in brain, it deviates when necessary in order to better suit the needs of brainfuck programming.

brainfuck is an esoteric programming language with only 8 single-byte instructions: +, -, >, <, ,, ., [, ]. These limited instructions make brainfuck code extremely verbose and difficult to write. It can take a long time to figure out what a brainfuck program is trying to do. brain makes it easier to create brainfuck programs by allowing you to write in a more readable and understandable language.

The type system makes it possible to detect a variety of logical errors when compiling, instead of waiting until runtime. This is an extra layer of convenience that brainfuck does not have. The compiler takes care of generating all the necessary brainfuck code to work with the raw bytes in the brainfuck turing machine.

The brain programming language compiles directly into brainfuck. The generated brainfuck code can be run by a brainfuck interpreter. brain only targets this interpreter which means that its generated programs are only guaranteed to work when run with that. The interpreter implements a brainfuck specification specially designed and written for the brain programming language project.

Optimization Goals

The brain compiler is designed to optimize the generated brainfuck code as much as 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 will become more expressed in the compiled output of the program.

brain syntax

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

cat program (examples/cat.brn)

// cat program
let mut ch: [u8; 1];

while true {
  // stdin.read_exact() panics if EOF is reached
  stdin.read_exact(ch);
  stdout.print(ch);
}

Compile this with brain examples/cat.brn.

Run this with brainfuck 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 many
let mut b: [u8; 5];
stdin.read_exact(b);
stdout.print(b"b = ", b, b"\n");

let mut c: [u8; 1];
stdin.read_exact(c);
stdout.print(b"c = ", c, b"\n");

// You can reuse allocated space again
stdin.read_exact(b);
stdout.print(b"b = ", b, b"\n");

Compile this with brain examples/input.brn.

This compiles into the following brainfuck:

,>,>,>,>,>+++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++.--------------------
-------------------------------------------
---.+++++++++++++++++++++++++++++.---------
--------------------.----------------------
----------<<<<<.>.>.>.>.>++++++++++.-------
---,>++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++.------------------------
-------------------------------------------
.+++++++++++++++++++++++++++++.------------
-----------------.-------------------------
-------<.>++++++++++.----------<<<<<<,>,>,>
,>,>>++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++.-------------------------
-----------------------------------------.+
++++++++++++++++++++++++++++.--------------
---------------.---------------------------
-----<<<<<<.>.>.>.>.>>++++++++++.----------

Run this after compiling with brainfuck input.bf < someinputfile.txt.

Installation

For people just looking to use brain, the easiest way to get brain right now is to first install the Cargo package manager for the Rust programming language.

NOTE: Until this version is released, these instructions will NOT work. Please see the Usage instructions below for how to manually install the compiler from the source code.

Then in your terminal run:

cargo install brain
cargo install brain-brainfuck

If you are upgrading from a previous version, run:

cargo install brain --force
cargo install brain-brainfuck --force

Usage

For anyone just looking to compile with the compiler:

  1. Follow the installation instructions above
  2. Run brain yourfile.brn to compile your brain code
  3. Run brainfuck yourfile.bf to run a brainfuck interpreter which will run your generated brainfuck code

You can also specify an output filename. Run brain --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 have Rust and cargo (comes with Rust) installed.

brain compiler

To compile a brain (.brn) file into brainfuck (.bf)

cargo run filename.brn

where filename.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 running cargo build first to see if the Rust compiler is just taking too long for some reason.

You can also install the compiler from the source code using this command in the repository's root directory:

cargo install --path .

Examples

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

Thanks

This project would not be possible without the brilliant work of the many authors of the Esolang Brainfuck Algorithms page. The entire wiki has been invaluable. That page in particular is the basis for a lot of the code generation in this compiler. I have contributed many novel brainfuck algorithms to that page as I come up with them for use in this compiler.

More Repositories

1

turtle

Create Animated Drawings in Rust
Rust
560
star
2

rust-simple-game-dev-tutorial

Rust game development tutorial with specs ECS and SDL2
Rust
193
star
3

brainfuck

Brainfuck interpreter companion to the brain programming language
Rust
41
star
4

tic-tac-toe

A fully commented Tic-Tac-Toe example written in Rust
Rust
39
star
5

caves

2D Cave Exploration Game with Procedurally Generated Levels
Rust
35
star
6

portrayer

A hierarchical, recursive ray tracer written in Rust
Rust
20
star
7

rust-game-dev-workshop

A very minimal game with a player, enemies, and a goal (that's it!)
Rust
20
star
8

component_group

A Rust crate for working with a group of Components (in the Specs ECS)
Rust
20
star
9

bst

A map and set type based on a binary search tree (BST)
Rust
13
star
10

sudoku

Fast sudoku solver
C
11
star
11

vecbit

A crate for managing memory bit by bit
Rust
9
star
12

gh-pages-es6-starter

Making publishing to GitHub pages with transpiled ES6 code a breeze
JavaScript
9
star
13

lion

A programming language and interactive REPL for performing calculations involving units
Rust
8
star
14

dino

Compiler / PL Experimentation
Rust
8
star
15

chess-old

Rust implementation of the game of chess (work in progress, just for fun)
Rust
7
star
16

wolf-asm

(Work in progress) The Wolf Assembly Language - assembler and interpreter/VM
Rust
6
star
17

tile-universe

Tile Universe Creator
JavaScript
6
star
18

text-to-color

Changes arbitrary text to a CSS color
HTML
5
star
19

robo-quest

Robot game for the GameShell
Rust
5
star
20

reversi

Reversi implemented in Haskell as a terminal game
Haskell
4
star
21

tea

An implementation of the Pie dependent type system from The Little Typer (work-in-progress)
Rust
4
star
22

ci

Continuous integration utilities for Rust
Rust
4
star
23

pea

PL / Compilers / Interpreters Experimentation
Rust
3
star
24

crategrep

Search all the source code on crates.io with ripgrep
Shell
3
star
25

logoc

A compiler for the LOGO programming language
Rust
3
star
26

lox

WIP
Rust
3
star
27

connect4

Terminal implementation of the game Connect 4
Rust
2
star
28

sunjay.github.io

My personal website
HTML
2
star
29

chess

Chess implementation in Rust (just for fun, experimenting with bitboards)
2
star
30

sregex

A simple regex engine made just for fun (work in progress)
Rust
2
star
31

kale

(Work in progress) A 2D graphics library aiming to support the needs of the turtle crate
Rust
2
star
32

advent-of-code-2019

My solutions for Advent of Code 2019
2
star
33

regex-vm

A regex engine that compiles the regex into instructions for a stack-based virtual machine. (work in progress)
Rust
2
star
34

react-redux-gh-pages-starter

(in progress) React Redux GitHub Pages Starter Project
JavaScript
2
star
35

genius-haskell

A Genius Tic-Tac-Toe implementation in Haskell (just for fun, to learn Haskell)
Haskell
2
star
36

board-games

(Work in progress) Implementations of various board games, playable in your terminal
Rust
2
star
37

brain-debug

The brain language debugger and visualizer
JavaScript
2
star
38

htmlgen

Simple HTML templating library (WIP, proof of concept)
Rust
2
star
39

autogamer

(Work in progress) An opinionated, convention over configuration game creation framework designed for use with the Tiled editor and the Python programming language
Rust
2
star
40

peg-solitaire

Peg Solitaire is a fun puzzle game created after I wrote an exam that had a question based on this.
JavaScript
2
star
41

pathgenerator

Proof-of-Concept for an algorithm I designed for my tile-universe project
JavaScript
2
star
42

mgc

The manual garbage collector for when you have complete control
Rust
2
star
43

drawc

(Work in progress) Teaching the basics of compilers by generating programs that draw pictures
Rust
2
star
44

universe-creator

A text adventure creator you can live preview in your browser!
JavaScript
2
star
45

checkers

Implementation of the game checkers with mandatory jumps (work in progress)
Rust
2
star
46

maze-generator

Maze Generation Visualizer
JavaScript
2
star
47

rhino

Rhino Editor - Image Editor for Linux (work in progress)
JavaScript
2
star
48

beaverdb

DB implementation experimentation
Rust
1
star
49

landscapes

2D Landscape Generator
JavaScript
1
star
50

rcov

Rust Code Coverage (initial phase of development)
Rust
1
star
51

elevator

Elevator Controller (in early development)
Rust
1
star
52

plzdoc

Please just generate documentation from JSDoc strings (without all the boilerplate/issues)
1
star
53

advent-of-code-2020

https://adventofcode.com/2020/
Rust
1
star
54

dsarray

A dynamically-sized array in C (work in progress)
C
1
star
55

world-creator

Procedurally generates terrain from a function
JavaScript
1
star
56

voxel-editor

A Voxel Model Editor (still in initial stages of development)
Rust
1
star
57

rust-rfc-data

Downloading and processing data about Rust RFC PRs using the GitHub API
JavaScript
1
star
58

sunjay

1
star
59

snake

Snake Game AI
JavaScript
1
star
60

coherence-in-chalk

Paper about Coherence in Chalk
TeX
1
star
61

spaceship-ai

Models fighting spaceships in a field of obstacles.
Python
1
star
62

poker

Poker hands calculator (in early development)
JavaScript
1
star
63

pixelated

Implementation of the game pixelated
Rust
1
star
64

brightness-toggle

Toggle the screen brightness on/off
JavaScript
1
star
65

balls-game

Rust
1
star
66

sealdb

Database Implementation Experimentation (work in progress)
Rust
1
star
67

brain-turtle

Library for generating turtle graphics commands with brain
1
star
68

dotfiles

My dotfiles and installation script
Shell
1
star
69

rethinkdb-demo

My demo of RethinkDB for my RethinkDB Power Hour Talk (April 28, 2016)
JavaScript
1
star
70

metal-warrior-x

(in progress) Game about the metal warrior who saves us from a post-apocalyptic world dominated by robots
Rust
1
star
71

ray-tracer

My implementation of the book Ray Tracing in a Weekend
C++
1
star
72

upp-7.0.0-travis

A version of the u++-7.0.0 installation compiled on a Travis CI machine.
C++
1
star