esolang

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

Expected: Hello world!

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.

CommandDoes
+ - * / % ^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 popCopy the register onto the stack; move the top into the register
swap dup drop drainSwap, duplicate, drop the top, empty the stack
rotTrade the tops of the two stacks
flip warpMake the other stack current; make it the code stack
<- ->Push the register into the stack on top; pop from it into the register
eval unwrapPut a stack's items on the code stack; on the current stack
if ifePop a and b, eval b if a is true; with c, eval c otherwise
get getcRead a line as tokens; as a string
put putcPrint 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.
Expected: Hello world!

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.

Expected: 120
Expected: 0

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.

Expected: 4

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

Further

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 staple

Source

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