esolang

Triple Threat

Three stacks, and each one can do something the other two cannot. Every number has to be passed around the circle.

2017 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 4 known programs · Hello World on Try It Online: identical except the line end · Test method

Run it

Expected: 42

The idea

Triple Threat is from 2017. It has three stacks of integers and twelve commands, each written as two digits. A command touches one or two stacks, and each stack has its own skills: only stack 1 can be counted down, only stack 3 can be added to, and only a value on stack 2 can be printed. So a program spends most of its time moving numbers from one stack to the next.

The machine

Commands are joined by dashes, any number on a line. A line that does not start with a command is a comment, and so is the rest of a line after its last command. A stack that a command needs while it is empty gets a 0 first.

CommandDoes
11Push 0 on stack 1
22Push 1 on stack 2
33Drop the top of stack 3
12Move the top of stack 1 to stack 2
23Move the top of stack 2 to stack 3; if stack 2 is empty, read an integer onto stack 3
31Pop stack 3 and push the value twice on stack 1
13Pop stack 1 and add it to the top of stack 3
21Pop stack 2 and subtract it from the top of stack 1
32Pop stack 3 into the top of stack 2, and print the number it replaces
10Pop stack 1; if it was 0, jump past the matching 30
30Pop stack 3; unless it was 0, jump back past the matching 10
00Stop

Numbers are printed in decimal with a newline, and input is one integer per line.

Example

The cat program.

The cat program. 23 reads 42 onto stack 3, 31 puts two copies on stack 1, and the two 12s move them to stack 2. 23 moves one back to stack 3, and 32 swaps it in for the other, printing the 42 it replaces.

Expected: 0

A truth machine. With 0 the 10 skips the loop and the last line prints 0. With 1 the loop prints 1 forever.

Patterns it teaches

  • Stack machines: three stacks, but no stack has every operation.
  • Turing completeness: each brainfuck command has a fixed translation. Stack 1 holds the cell under the pointer and everything to its right, stack 3 the cells to its left.
  • Turing tarpit: adding 1 to a number takes seven commands.

Try this

  1. Read two numbers and print their sum.
  2. Print the numbers 3, 2, 1.
  3. Hard: print 72, then use it as a counter to print 72 ones.

Further

Stack-based

Write your own in Run Convert a program from Triple Threat

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: triple-threat.

Command line

shib run triple-threat -e '23-31-12-12-23-32-00' --input '42
'
shib manual triple-threat

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/triple-threat.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org