Mastermind
C-like code whose loops drain and copy cells, compiled to brainfuck.
Heath Manning, 2023 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Heath Manning made Mastermind in 2023 as a tool for writing optimized brainfuck. Its compiler, written in Rust, turns a program into brainfuck for an 8-bit wrapping tape that is infinite both ways. So a cell holds 0 to 255 and every sum wraps.
Read the story
The loops are brainfuck idioms with names. drain x { } repeats its body x times and empties x on the way. copy x { } does the same and leaves x as it was. Both can add 1 to other cells on every pass with into, which is how brainfuck moves or copies one cell into another. Functions are inlined like macros and take their arguments by reference, as there is no value to pass.
The machine
| Code | Does |
|---|---|
cell x = 5; cell a[3] = [1, 2, 3]; cell s[5] = "hello"; | Declare a cell or an array |
x = e; x += e; x -= e; | Assign; an expression only adds and subtracts |
output e; output "hi"; output *a; | Print bytes |
input x; input *a; | Read bytes |
while x { } | Loop while the cell is not 0 |
drain e into y *a { } | Repeat e times, emptying e when it is a cell, and add 1 to y and to each cell of a after each pass |
copy e into y { } | The same, leaving e as it was |
if e { } else { } if not e { } | Branch on e being other than 0 |
fn f(cell x) { } f(y); | Define and inline a function |
This implementation runs Mastermind directly, without the brainfuck in between. It leaves out in-line brainfuck (bf blocks with clobbers and assert) and the @ memory location specifiers, because they depend on where the compiler puts each cell on the tape, and the specification does not say. It leaves out #include too, as a run has no other files. A read past the end of the input gives 0.
Example
The for loop.
The for loop. drain 10 empties a cell of its own ten times, and into i counts i up after each pass.
The function. Its argument is the cell N itself, so the second call sees N after it changed.
The rows and columns. The specification's comment shows six dots, but three rows of six make 18.
A cat. The loop stops at the 0 that a read past the end gives.
Patterns it teaches
- Turing tarpit: loops with names over a tarpit's cells.
Further
See also
Brainfuck
Cell-based
Write your own in Run Convert a program from Mastermind
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: mastermind.
Command line
shib run mastermind -e 'if 13 {
output "13";
}'shib manual mastermindSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/mastermind.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org