esolang

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

Expected: Hello World!

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

InstructionDoes
3 pushPush 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 cdupPush the size of the top; push a full copy of it
swap rot cycleReorder the stacks; rot turns a b c into c a b, cycle sends the top to the bottom
pack unpackPut the top stack inside the one under it; take it back out
inc decAdd an empty stack inside the top one; take one out
cat addMerge the top two stacks
eq ls notCompare sizes; the answer is the stack 0 or 1
testPop a stack and set TEST to whether it holds anything
?skip ?doSkip the next instruction or block if TEST is true or false
?loop ?exitIf TEST is true, start the block again or leave it
outi outc outs outaPrint sizes as a number or as characters
geta getsPush the input
\down \upMake the top stack, or the parent, the working stack
@fCall 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.

Expected: stacks

A number is only a size. inc puts two more empty stacks inside the 3.

Expected: 5

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.

Expected: 0

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.

Expected: abcd

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.

Expected: main { "main" cdup outs endl 123 outc endl 9 outc 34 pack 0 34 pack cat outs endl 1 "cdup outs endl 123 outc endl 9 outc 34 pack 0 34 pack cat outs endl 1" "swap { 9 outc -cycle dup test dec cycle cdup ?do { outs endl 9 outc 34 pack 0 34 pack cat outs } ?skip { 34 pack 0 34 pack cat outs endl 9 outc outs } endl ?loop 125 outc endl }" swap { 9 outc -cycle dup test dec cycle cdup ?do { outs endl 9 outc 34 pack 0 34 pack cat outs } ?skip { 34 pack 0 34 pack cat outs endl 9 outc outs } endl ?loop 125 outc endl } }

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

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 stackstacks

Source

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