StaPLe
The program is a stack too, and any stack can become the program.
BCompton, 2014 · Turing complete: unknown · ●●● hard · esolangs.org
Tested with 5 known programs · Test method
Run it
The idea
BCompton made StaPLe, the Stack Processing Language, in 2014 after Lisp and Joy. There are two stacks and a register. Stack one starts as the code stack and holds the program's tokens, so the last token runs first and a program reads from right to left. Stack two starts as the data stack.
Read the story
Each turn pops the code stack. A command runs, and anything else is pushed as data. warp swaps which stack is the code, and flip swaps which one is current, so data can turn into code and code into data. A program ends when the code stack is empty.
The machine
Tokens end with ;. "text; is a string, #text; a comment, and [...] a stack that can hold commands.
| Command | Does |
|---|---|
+ - * / % ^ | a+b, b-a, a*b, b/a rounded down, b%a, b^a, with a the top |
> < >= <= == | Compare b with a |
! && \|\| | True for 0; and; or |
push pop | Copy the register onto the stack; move the top into the register |
swap dup drop drain | Swap, duplicate, drop the top, empty the stack |
rot | Trade the tops of the two stacks |
flip warp | Make the other stack current; make it the code stack |
<- -> | Push the register into the stack on top; pop from it into the register |
eval unwrap | Put a stack's items on the code stack; on the current stack |
if ife | Pop a and b, eval b if a is true; with c, eval c otherwise |
get getc | Read a line as tokens; as a string |
put putc | Print a value; print a number as its character |
An R in front, as in Rput, takes the first value from the register. R0 puts 0 in the register.
Example
The second Hello World is the one the specification calls more fun.
The second Hello World is the one the specification calls more fun. Read from the right, it leaves warp, the text, [putc] and eval on stack two. Then warp makes stack two the code stack, so that data starts to run. eval unpacks [putc], putc prints the text, and the second warp hands the code back to stack one, where flip and drain empty it.
The factorial keeps the running product in the register. Each round multiplies it by one less, until the count reaches 0 and ife picks the branch that prints the register and drains the code.
The truth machine prints the number it reads, and while it is true it evaluates a stack that sets up the same round again.
The Deadfish interpreter reads one command per line and keeps the accumulator in the register. getc ends the run with an error once the input has run out, after the output below.
The specification leaves a few things open, and this implementation picks the plainest reading. Only pop and -> write the register. A stack that a comparison or logic command takes runs first as code, on the same stacks, and the value it leaves on top is used. put writes no line break.
Patterns it teaches
- Stack machines: with the program on a stack of its own.
- Self-modifying code:
warpruns the data stack as code. - Code as data: stacks hold commands until
evalruns them.
Further
See also
Self-modifying
Stack-based
Write your own in Run Convert a program from StaPLe
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: staple.
Command line
shib run staple -e 'put;"Hello world!;'shib manual stapleSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/staple.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org