esolang

Madbrain

The program runs down the lines, but each line runs only one of its characters. Which one is up to a second pointer.

Feuermonster, 2010 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 4 known programs · Test method

Run it

Expected: 0

The idea

Feuermonster made Madbrain in 2010. It is a stack language read from top to bottom, one line a step. A horizontal pointer says which character of the line runs, so a program is a grid in which every column is its own straight piece of code. Jumping changes the line, and the conditionals move the horizontal pointer one column to the left or right. That is how a program branches.

Read the story

The stack is used from both ends. With two arguments, the first comes off the top and the second off the bottom. The specification says it is Turing complete, as it can run tag systems with up to 8 symbols.

The machine

CharDoes
0 to 9Push the digit
* / + -Top op bottom; / makes a float, as in Python
jJump to the line given by the top
g qJump like j and add or subtract the bottom to the pointer
i dAdd or subtract the top to the pointer
> < = #Move the pointer right if top is greater, less, equal, not equal to bottom
^ v _ @The same tests, moving it left
? : . ! ; ,Right or left if the top is above, at or below 0
p c rPrint a number, print a character, read a digit
xHalt

Both pointers start at 0, and the run also ends below the last line. The specification's Python interpreter is the reference; like it, this implementation takes a negative line or pointer from the end and stops with an error on a pointer past the end of a line.

Example

The truth machine.

The truth machine. On 0, : moves the pointer to the second column, which prints the 0 and halts. On 1 the first column prints 1 and 2 j jumps back up to print it again, forever.

The equality check reads two digits. When they are equal, = moves to the second column, which pushes 1 and 0 and jumps to line 0 with j, where the pointer now finds p. Otherwise g jumps there and moves the pointer itself. Either way p then prints the top of the stack, 1 or 0.

Expected: 1

Hello, world! is 13 columns, one for each character. Each column multiplies two digits, adjusts the product, prints it with c and ends in 2 0 g. That jumps back to line 0 and moves the pointer two to the right, past the spaces, to the next column. The last column ends in x.

Expected: Hello, world!

Patterns it teaches

Further

Two-dimensional languages

Stack-based

Write your own in Run Convert a program from Madbrain

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: madbrain.

Command line

shib run madbrain -e 'r
:
10
pp
2x
j' --input '0'
shib manual madbrain

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/madbrain.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org