Triple Threat

From Esolang
Jump to navigation Jump to search

Triple Threat (hereafter TT) is a stack-based esolang. Every command modifies and juggles the numbers in one or two of three stacks. Each stack is capable of different actions, so juggling values around the circle is critical to getting anything done.

Memory

TT makes use of three LIFO stacks to handle data. Each stack has an ID number: Stack 1, Stack 2 and Stack 3. At the start of the program, all three stacks are empty. Each stack can hold an arbitrary number of unbounded, signed integers; the only limit on stack depth are those imposed by the hardware used.

Commands

There are a total of 12 valid commands in TT.

  • 11: Push a 0 onto Stack 1.
  • 22: Push a 1 onto Stack 2.
  • 33: Pop the top number of Stack 3 and discard it.
  • 12: Pop the top number of Stack 1 and push it onto Stack 2.
  • 23: Pop the top number of Stack 2 and push it onto Stack 3; if Stack 2 is empty, input an integer and push it onto Stack 3.
  • 31: Pop the top number of Stack 3 and push two copies of it onto Stack 1.
  • 13: Pop the top number of Stack 1 and increase the top number of Stack 3 by the popped number.
  • 21: Pop the top number of Stack 2 and decrease the top number of Stack 1 by the popped number.
  • 32: Pop the top number of Stack 3 and replace the top number of Stack 2 with the popped number; output the number replaced.
  • 10: Pop the top value of Stack 1; if the popped value is 0, skip to after the matching 30.
  • 30: Pop the top value of Stack 3; unless the popped value is 0, skip to after the matching 10.
  • 00: End the program.

Any number of commands can be on one line; each command on the same line must be separated by a dash (-). Everything after the last command is ignored as a comment as is any line that doesn't start with a command. The program terminates if it reached the end of the file or a "00" command.

10 and 30 create matching pairs, like parentheses. There is no maximum recursion depth except hardware limitations.

Handling an empty Stack

If a command (other than 23) tries to pop or manipulate a number from an empty stack, it pushes a 0 onto the empty stack before resolving the command.

Example Code

Cat:

23-31-12-12-23-32-00

Truth machine:

23-31
10-22-22-23-32-23-30
11-22-23-21-32-00

Turing-Completeness

Triple Threat can be shown to be Turing-complete by reduction to brainfuck. First, start the program with "11-11-11-11-11-11-11-11-11-11" to create a 10 cell tape; when more cells are needed, the empty stack handling rules will automatically add them. Then, replace each BF command with the following TT commands:

+: 22-23-13-31-12-23-33
-: 22-21
>: 12-23
<: 31-12-23-33
,: 23-12-23-33-31-12-23-33
.: 12-23-31-12-22-23-32-23-33
[: 12-23-31-10
]: 12-23-31-12-23-30

The top number on Stack 1 is the data pointer. The rest of Stack 1 is to the right of the data pointer and Stack 3 is to the left of the data pointer. Stack 2 is always empty between commands.

Interpreter

Python interpreter.