Tru
A two-stack machine written only in brackets. No spaces are needed, because no instruction is the start of another.
Swc, 2005 · Turing complete: unknown · ●○○ easy · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Tru is by the user Swc. Every instruction is a short run of < and >, plus (, ), [ and ]. >><< duplicates, <<< adds, <><>><< prints a character.
Read the story
The trick is a prefix-free code. No instruction is the beginning of a longer one, so a reader always knows where one ends and the next begins. You can write the whole program as one unbroken line of brackets, as with Huffman codes or phone numbers.
There are two stacks, and you choose which one is current. The specification even has a quine in it.
The machine
Two integer stacks, numbered 0 and 1; stack 0 is current at the start. Popping an empty stack gives 0.
| Code | Does |
|---|---|
(n) | Push the number n |
<><>><< <><>><> | Pop and print as a character, as a number |
<><>>> <><>< | Read a number, a character (0 at end of input) |
>><< >><> <><< | Dup, swap, discard |
<<< >>>> | Add; subtract (top from the one below) |
<<> <>> ><< | Greater than, equals, not (1 or 0) |
>>><> | Move the top to the other stack |
><> | Pop; 0 makes stack 0 current, anything else stack 1 |
[ ] | Pop; skip past ] on 0, jump back to [ on nonzero |
>>><< | End |
# | Comment to the end of the line |
Spaces and line breaks are ignored between instructions.
Example
The Hello World pushes the text backwards, ending with a 0.
The Hello World pushes the text backwards, ending with a 0. >><< duplicates H, the loop [ ... ] prints a character and duplicates the next one, and it stops when it reaches the 0. The output ends with a newline.
Patterns it teaches
- Stack machines: two stacks and a handful of operations.
- Encoding tricks: a prefix-free code makes separators unneeded.
- Quine: the specification has a Tru program that prints itself.
Try this
- Print
42as a number. - Read two numbers and print their sum.
- Hard: use the second stack to print the input reversed.
Further
See also
Stack-based
Write your own in Run Convert a program from Tru
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: tru.
Command line
shib run tru -e '(0)(10)(33)(100)(108)(114)(111)(119)(32)(44)(111)(108)(108)(101)(72)>><<[<><>><<>><<]>>><<'shib manual truSource
- Module
langs/stack/ports.js- Interpreter
langs/stack/tru.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org