Heapfuck
Brainfuck where the tape is a binary heap, and the heap keeps sorting itself under your feet.
K.avi, 2023 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
K.avi made Heapfuck in 2023. The memory is a binary heap instead of a tape. It starts empty, nodes are added one at a time, and after every command the heap restores its order, so a value that grows past its children sinks and a value that shrinks below its parent rises. The pointer moves along the tree, to a child or to the parent, while the values rearrange around it.
Read the story
The specification proves it Turing complete with a heap set up so that it never needs to reorder. The tape cells sit to the left of a spine of -1 values, and the moves ^>< and ^^< walk from one cell to the next and back.
The machine
| Command | Does |
|---|---|
% | Add a node of 0 |
, | Add a node holding the next input character |
< > ^ | Move to the left child, the right child or the parent |
! | Remove the node under the pointer |
+ - | Add or take 1 |
[ ] | Loop while the node is not 0 |
. : | Print the node as a character or as a number |
# | Print the whole heap |
While the heap is empty every command but % and , is passed over. The smallest value sits at the root; the heapfuck program only works that way. The specification leaves the pointer's behaviour open when values move. It keeps its place in the tree, so after a + it may stand on a different value.
Example
The cat reads a character into a node, prints it, removes it and reads the next.
The cat reads a character into a node, prints it, removes it and reads the next. At the end of the input the node is 0 and the loop stops.
The root is counted up three times. Whenever it grows past its child the two swap, so the pointer, which stays at the root, finds 1 there, and the 2 has sunk to the child.
The specification's own program builds eight nodes, fills them from the bottom up with the codes of U P K H F E C A, smallest at the root, and reads them out in the order HEAPFUCK.
Patterns it teaches
- Turing completeness: a brainfuck tape built inside a heap that must not move.
Further
See also
Heapfuck builds on Brainfuck.
Brainfuck derivatives
Cell-based
Write your own in Run Convert a program from Heapfuck
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: heapfuck.
Command line
shib run heapfuck -e ',[.!,]' --input 'abc'shib manual heapfuckSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/heapfuck.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org