Tetrastack
Four stacks and sixteen hex digits. Every instruction is one pop and one push, and the stack you pop from decides the arithmetic.
GUAqwq, 2023 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
GUAqwq made Tetrastack in 2023, the first language of the TS series. A program is a string of hexadecimal digits. Each digit is two pairs of bits; the upper pair names a stack to pop from, the lower pair a stack to push to. 7 is 01 11, so it moves a number from stack 1 to stack 3.
Read the story
There is no add instruction. Popping stack 0 adds 1 on the way, popping stack 1 takes 1, and stacks 2 and 3 leave the number as it is. Loops hang on stack 3. The specification proves it Turing complete with a table that turns every brainfuck command into Tetrastack, and that table works.
The machine
| Digit | Does |
|---|---|
0 to 9, B | Pop the stack the upper bits name, change the number by that stack's rule, push it to the stack the lower bits name |
F | Push 0 to stack 2 |
C D E | Pop stack 3; on 0 drop it and jump past the matching A, else push it to stack 0, 1 or 2 |
A | Jump back to the matching loop start |
Popping an empty stack ends the run with an error. The input waits on stack 0 with its last character on top and a -1 under its first. When the program ends, stack 3 is printed from its top down.
Example
The cat moves each character to stack 3 through stack 1.
The cat moves each character to stack 3 through stack 1. The loop ends when it pops the -1, which stack 0 has turned into 0.
Five times thirteen. F8 puts a 0 on stack 0 as a total, F800003 puts the counter 5 on stack 3, and each round adds 13 to the total and moves the counter back through stack 1, which takes 1. 17 moves the total to stack 3 unchanged, as stack 0 adds 1 and stack 1 takes it away again.
The Hello World takes 11,220 steps.
The specification leaves two things open, and its own programs settle both. A pops nothing, and a loop start drops the 0 it pops. With either one the other way, the Hello World stops on an empty stack or is still running after ten million steps.
Patterns it teaches
- Stack machines: four stacks, and the arithmetic hides in which one you pop.
- Turing tarpit: sixteen instructions, and brainfuck fits inside.
Further
See also
Stack-based
Write your own in Run Convert a program from Tetrastack
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: tetrastack.
Command line
shib run tetrastack -e '3D73A' --input 'hey'shib manual tetrastackSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/tetrastack.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org