StackStacks
No data at all. Every stack holds stacks, and the number 5 is a stack of five empty stacks.
Oj742, 2014 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 5 known programs · Test method
Run it
The idea
Oj742 made StackStacks in 2014, after Forth. It stores no data except two flags. Every stack holds other stacks, so stacks can nest as deep as you like, the way folders do. A number is the size of a stack, so 3 is a stack holding three empty stacks, and a character is a stack as big as its code. The specification warns that by the end you will be sick of the word stack.
Read the story
There are over sixty instructions, several named as in Forth. A program is a set of functions with C style blocks, and it starts at main.
The machine
| Instruction | Does |
|---|---|
3 push | Push a stack of three empty stacks; push an empty stack |
"text" 'text' | Push one stack of characters; push each character, the first on top |
dup cdup | Push the size of the top; push a full copy of it |
swap rot cycle | Reorder the stacks; rot turns a b c into c a b, cycle sends the top to the bottom |
pack unpack | Put the top stack inside the one under it; take it back out |
inc dec | Add an empty stack inside the top one; take one out |
cat add | Merge the top two stacks |
eq ls not | Compare sizes; the answer is the stack 0 or 1 |
test | Pop a stack and set TEST to whether it holds anything |
?skip ?do | Skip the next instruction or block if TEST is true or false |
?loop ?exit | If TEST is true, start the block again or leave it |
outi outc outs outa | Print sizes as a number or as characters |
geta gets | Push the input |
\down \up | Make the top stack, or the parent, the working stack |
@f | Call the function f |
An instruction that has too few stacks to work on sets the FAIL flag and does nothing, and .fail pushes that flag. The four debug instructions, which show the stacks in a format the specification does not give, do nothing.
Example
The Hello World pushes its characters with the first on top and prints them all with outa.
The Hello World pushes its characters with the first on top and prints them all with outa. A string in double quotes is a single stack, which outs prints just as well.
A number is only a size. inc puts two more empty stacks inside the 3.
The truth machine compares the input with '0'. On 0 it prints the character and leaves main with ?exit; on 1 it loops on 1 outi for ever.
A string in double quotes holds its first character on top, like the single quoted kind, and outs prints from the top. cat puts what the stack pushed first holds above what the one pushed second holds, so the text reads in order.
The quine keeps two lines of itself as strings and prints each twice, once plain and once in quotes. It prints itself only because cat works that way round.
The 99 bottles of beer program, with a function for the bottles and one for taking one down, sings down to no more bottles.
Patterns it teaches
- Stack machines: a stack machine with nothing but stacks in it.
- Quine: two strings, each printed plain and quoted.
- Turing completeness: the specification reduces brainfuck to it, the tape being stacks inside stacks.
Further
See also
Stack-based
Write your own in Run Convert a program from StackStacks
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: stackstacks.
Command line
shib run stackstacks -e 'main
{
geta outa //As with hello world, '\''gets outs'\'' would also work
}' --input 'StackStacks'shib manual stackstacksSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/stackstacks.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org