Skip to content

brain-labs/brain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo

Brain

Build Status

A computer language based on Brainfuck.

Table of Contents

About

Brain wants to improve the performance of the Brainfuck programming language and extend it as well, as Brainfuck itself has a lack of flexibility and does not perform great control over complex computations. Brain is open to new model represetantion and allows programmers to extend its capability by attaching LLVM IR to its code.

One of the main ideas of Brain is saving some operations in machine language, creating an instruction optmizer due to the excess of instructions that Brainfuck would generate. Brain aims to implement it by using current technology (LLVM).

In spite of implementing new commands and features, Brain is completely compatible with Brainfuck (but the opposite is not true).

Install

Arch Linux via AUR

yaourt -S brain

Docker

docker pull luizperes/brain:1.0
docker run -it luizperes/brain:1.0

Documentation

You can check this repository documentation on https://brain-labs.github.io/brain/docs/html/index.html

Current Status

Brain is stable on its tag version 1.0

Obs.: To use Project Status (the "Kanban" below), please visit:https://github.com/luizperes/status-projects/blob/master/README.md and https://luizperes.github.io/status-projects/

Project Name Status Technology Priority Deadline
Brain 1.0 Progress C/C++/LLVM Low
Brain 1.1 Progress C/C++/LLVM Low
To Do In Progress Done
PostIt PostIt PostIt PostIt
PostItPostIt
PostItPostIt
PostItPostIt
PostIt

TODO List

If you want to check the micro TODO list, please see this issue.

How to build LLVM

Brain runs on the top of LLVM, thus, you are required to install the lastest versions of LLVM. You can do that by following this https://llvm.org/releases/download.html

How to install pre-commit

This project uses pre-commit to help us to check our commits in order to minimize bugs and other problems on the project, therefore is strongly recommended that you use it, if you are intending to contribute to the project. For that, you can install by:

if you have pip installed:

Mac

brew install pre-commit

Linux

sudo pip install pre-commit

After that, go to where Brain lives:

$ cd /path/to/brain
$ pre-commit install

More information about that here

How to build Brain and run files

To build it, after installing LLVM, execute:

$ cd /path/to/brain/src
$ make
$ make install

Brain will try to run on clang and clang++ automatically. However you can change your CC with the commands:

  • make build-3.8 or make build-3.9 for LLVM 3.8 and 3.9
  • make CC=clang++-3.7 LLVM_CONFIG=clang++-3.7 for older versions (3.7 in this case)

And you can do the same for installing it and running tests:

  • make install C=clang-3.7
  • make tests C=clang-3.7

After running make and make install on it, you can execute:./brain your_brain_file.b. Please check the current status of the project.

How it has been built

Brain is based on previous work https://github.com/luizperes/BrainfuckInterpreter and https://github.com/Lisapple/BF-Compiler-Tutorial-with-LLVM, now trying to make something more serious: Turing Complete, faster, more features/commands and different types.

Technical Information

Brain is now a Turing Complete language. You can now extend the tape size by using the flag --size=<tape size>.

Commands

Implemented

  • > increment the data pointer (to point to the next cell to the righ.
  • < decrement the data pointer (to point to the next cell to the left).
  • + increment (increase by one) the value at the data pointer.
  • - decrement (decrease by one) the value at the data pointer.
  • . output the value at the data pointer.
  • , accept one value of input, storing its value in the value at the data pointer.
  • [ if the value at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
  • ] jump to its correspondent [ .
  • * multiply *ptr with *(ptr-1). Store result in *ptr // format: 2 3 *
  • / divide *ptr with *(ptr-1). Store the result in *ptr // format: 2 3 /
  • % divide *ptr with *(ptr-1). Store the remainder in *ptr // format: 2 3 %
  • # prints out the current debug information.
  • { (for loop) iterates 'value-at-the-data-pointer' times and needs to be closed with a matching } command. It does not decrease the value at the data pointer. It will only work for positive values.
  • } jump to its correspondent { .
  • ! (break) jumps to the end of a loop ([ ] or { })
  • ? if the value at the data pointer is zero , jumps to the block with : or ; and executes the commands one by one up to its correlative ;, otherwise, it executes the code until it finds a : or ;.
  • : it works as an otherwise (or else) for ?.
  • ; ends a statement.
  • $ prints out the value at the data pointer divided by 100.
  • ^ move the data pointer (jump) on the tape. Ex.: ++++++++++^ // the data pointer will jump to cell 10.
  • & stores the value of *(ptr-1) in the *ptr-th cell // format: 10 500 & (stores 10 at cell number 500.)

Not Implemented

  • @ calls a function according to the value at the data pointer.

Example of the instructions above:

  • if-then: ? +++ ; // if (*ptr) { *ptr += 3; }
  • if-else: ? +++ : --- ; // if (*ptr) { *ptr += 3; } else { *ptr -= 3; }
  • for: ++++ { commands } // makes four iterations 4 through 0 (excluded)
  • float numbers: ++>+********$ cell 0[2] cell 1[256] // '$' prints out 256 / 100
  • break loop: +[+++++!] // *ptr = 1; while(*ptr) { *ptr += 5; break; }

Compiler Options

  • --code=<"inline code"> Sets inline brain code
  • --io=repl Sets the IO module to REPLs style
  • --out=<filename> Sets the output filename
  • --size=<number> Sets the number of cells used by the interpreter
  • --size-cell=<number> Sets the cell width (8, 16, 32, 64 bits) of each cell
  • --version Shows the current version of Brain
  • -emit-llvm Emits LLVM IR code for the given input
  • -emit-ast Emits the AST for the given input
  • -emit-code Emits an optimized code for the given input
  • -c Generates object file
  • -S Generates assembly file
  • -v Uses verbose mode for the output
  • -O0 Generates output code with no optmizations
  • -O1 Optimizes Brain generated output code (Default)

Applications on real life

  • Artificial Intelligence/ Machine Learning
  • Send commands to Arduino
  • Easy support to primitive processors

Help

Feel free to send your pull requests. :)

LICENSE

This project extends GNU GPL v. 3, so be aware of that, regarding copying, modifying and (re)destributing.