Braingolf
One character per command, several stacks of integers, and four modifiers that change what the next command does.
Mayube, 2017 · Turing complete: unknown · ●●○ moderate · esolangs.org
Tested with 7 known programs · Hello World on Try It Online: identical except the line end · Test method
Run it
The idea
Braingolf was made in 2017 by Mayube on the code golf site. The name points at brainfuck, its first inspiration, but the language ended up closer to ><> without the second dimension. The interpreter is a single Python file in the repository dylanrenwick/braingolf, and this implementation follows it.
Read the story
Every character is an operator, a modifier or a literal. A modifier is held until the next command that can use it. ! makes it read without popping, & makes it work on the whole stack, $ keeps it quiet and ~ makes it read from the bottom instead of the top.
The machine
There are three stacks of whole numbers to begin with and one is current. A digit pushes itself, so 10 is written 91+. A string pushes the code of each character and #c pushes one. When the program ends the top of the current stack is printed with a newline.
| Command | Does |
|---|---|
0…9 "…" #c | Push a digit, the codes of a string, one code |
+ - * / % ^ | Arithmetic on the two top items; on one item it is used twice |
_ @ @n | Print a number, a character, the top n characters |
= | Print the stack as a list |
. , < > | Duplicate, swap, rotate either way |
?…:…| | If the popped value is above 0, else |
[…] | Repeat while the bottom item is above 0, taking one off it each round |
{…} | Run once for each item, turning the stack so each comes to the top |
V v R | Make a new stack, go to the next, go back to the first |
i | Read a line of input as codes |
; | Do not print the top at the end |
Some commands have a value for an empty stack. + alone pushes 20 and * alone pushes 1000. Blocks do not nest, and a ] or | inside a string still ends its block, as in the reference.
Example
The string pushes 13 codes and &@ prints them all as characters.
The string pushes 13 codes and &@ prints them all as characters. The stack is then empty, so the print at the end gives only a newline.
[ takes one off the bottom item before the first round and ] before each next one. !~_ prints the bottom item without popping it.
The last one shows the end of a run. @3 is still waiting for a command when the program stops, so it prints the three characters with a newline and keeps them, and then the top, 99, is printed as a number.
This implementation is a subset. The loop (…), A, G and the random r stop the run with a reason, and program arguments are not taken, so input comes only through i.
Patterns it teaches
- Code golf: an empty stack gives
+a useful number instead of an error. - Stack machines: modifiers bend one stack command into four.
Try this
- Print 100 using only digits and arithmetic.
- Read a line and print it backwards.
- Hard: print the numbers 1 to 10, one per line.
Further
See also
Golfing languages
Stack-based
Write your own in Run Convert a program from Braingolf
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: braingolf.
Command line
shib run braingolf -e 'i&@' --input 'a cat
'shib manual braingolfSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/braingolf.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org