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
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
| Char | Does |
|---|---|
0 to 9 | Push the digit |
* / + - | Top op bottom; / makes a float, as in Python |
j | Jump to the line given by the top |
g q | Jump like j and add or subtract the bottom to the pointer |
i d | Add 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 r | Print a number, print a character, read a digit |
x | Halt |
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.
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.
Patterns it teaches
- Stack machines: with a stack read from both ends.
- Two-dimensional control flow: rows are time, columns are branches.
Further
See also
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 madbrainSource
- 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