Introduction to Brain
19 August 2016
This is a short blog post on the Brain language developed by me and other guys from brain-labs, it contains basic information about the language and its compiler.
What is Brain?
Brain\(^1\) is a new esoteric programming language derived from brainfuck, the esoteric part means it is criptic language to humans understand, in fact, Brain was not developed neither to be used by humans nor on a daily-basis work to do some pretty complicated stuff. Just have a look at this example:
++>+**********#
At a glance you can not tell what is going on, Brain was developed to be used within an artificial intelligence and it has a small, but not so small, set of tokens in comparisson with other languages and therefore it is a perfect canditate towards ours - Brain developers - goals.
The compiler
Before we dive in the language itself lets have a quick look at the compiler. The Brain compiler is actually very simple if we put it in perspective with other compilers. Such as the Quack Compiler\(^2\), that has a lexical analyzer\(^3\) and a syntactic analyzer\(^4\) among other things in addition to a parser. The Brain compiler will be separated in modules further on to ease the understanding.
Its compiler process is described in the following graph:
The Parser
The Parser is responsible by reading Brain source code and build the AST (Abstract Syntax Tree), it is an LL(1) \(^3\) parser.
Code Generation
This is the last block of proccess until the code really executes. From the AST each Brain token is transformed to Intermediate Representation, something like asm, but to LLVM understand.
LLVM JIT Compiler
The code execution part, this is where Brain’s code finally is executed, after the AST has been transformed to IR (Intermediate Representation) code the LLVM picks it up and executes it.
The language itself
As you saw in the previous topic Brain can be complicated to human eyes just as bits of code are, but as you understand the concept Brain is simple as bits of code are to the computer. Brain allocates its own stack memory called: brain_cells and uses it to process whatever it’s coded to in a very peculliar way. Internally it looks like this:
Brain uses that memory model to run programs. Programs coded in the Brain language uses arithmetic, conditional and shift operators to change the values inside those cells and transverse the memory as they ran.
Knowing the representation of the memory inside Brain we can move on to its operators, they are 21 so far and are divided in the following sections.
-
Shift operators
>
Shift the position of the index pointer to the next cell.<
Shift the position of the index pointer to the previous cell.^
Jumps to the cell[number], with number being the current cell value.
-
Arithmetic operators
+
Increment the value of the current cell by 1.-
Decrement the value of the current cell by 1.*
Multiply the value of the current with the previous cell and stores the result in the current cell./
Divides the value of the current cell with previous cell and stores the result in the current cell. A note: it’s integer division.%
Gets the remainder between the current and the previous cell, storing the result in the current cell.
-
I/O operators
#
Prints out debug information of the current cell..
Prints out the value of the current cell.,
Accepts input from the stdin and stores it in the current cell.$
Prints the cell value divided by 100. It sort of show floating point numbers.
-
Conditional operators
[]
A while loop.{}
A for loop.?:;
An if-else.
-
Unconditional operators
!
The break operator, want to step out of a loop? This is the way to do it.
-
Non implemented till the date of the publication
@
Not implemented yet.
Brain’s programs run by shifting the index array position forward or backwards and also modifying values on those indexes by using one of the arithmetic operators and finally displaying its output using one of I/O operators.
So what can I do with it?
Pretty much anything, it is turing complete! You can solve any kind of problem with it but it might not be the best choice to some specific problems.
There is an online REPL on which you can see the behavior of Brain programs.