Brain

From Esolang
Jump to: navigation, search

An esoteric programming language based on Brainfuck.

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

Tools

  • Brain Visualizer - an Online Javascript Interpreter for the Brain Language.
  • Brainduino - a Brain interpreter for Arduino.
  • BrainStation - A small video game console for Arduino using the Brainduino interpreter.

Install

Arch Linux via AUR

yaourt -S brain

Docker

docker pull luizperes/brain:1.0

docker run -it luizperes/brain:1.0

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.

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

Command Description
> Move the pointer to the right
< Move the pointer to the left
+ Increment the memory cell under the pointer
- Decrement the memory cell under the pointer
. Output the character signified by the cell at the pointer
, Input a character and store it in the cell at the pointer
[ Jump past the matching ] if the cell under the pointer is 0
] Jump back to the matching [ if the cell under the pointer is nonzero
* multiply *ptr with *(ptr-1)<code>. Store result in <code>*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.

Not Implemented

Command Description
@ calls a function according to the value at the data pointer.
^ move the data pointer (jump) on the tape. Ex.: ++++++++++^ //the data pointer will jump to cell 10.

Examples

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

Compiler Options

  • --version Shows the current version of Brain
  • --size=<number> Sets the number of cells used by the interpreter
  • -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
  • -v Uses verbose mode for the output
  • -O0 Generates output code with no optmizations
  • -O1 Optimizes Brain generated output code (Default)

Real-life Applications

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

See Also

External Resources