3x
The only number you can write is 3, and the only sum is (A-B)/C.
Turing complete: unknown · ●●○ moderate · esolangs.org
Tested with 2 known programs · Test method
Run it
The idea
3x is a stack language inspired mainly by 1+, where 1 is the only literal. In 3x the literal is 3, and the one arithmetic command, x, pops A, B and C and pushes (A-B)/C. The stack holds exact rational numbers, so nothing is ever rounded. 333x is (3-3)/3, which is 0, and 3333x3x is 1.
Read the story
Variables are named by numbers, and one that was never set holds 3.
The machine
| Command | Does |
|---|---|
3 | Push 3 |
x | Pop A, B and C, push (A-B)/C |
? | Push a number from the input |
! | Pop and print the top |
v | Pop A and B, store A in variable B |
^ | Pop A, push variable A, or 3 if it was never set |
# | Swap the top two |
(...) | Loop while the top is not 0 |
[...] | Print the text inside |
A is the first item popped, so the stack holds C, B and A from the bottom up. The specification does not say how a fraction prints. This implementation prints it as p/q, with no line break.
Example
The truth machine reads a number and loops while it is not 0.
The truth machine reads a number and loops while it is not 0. On 1 it prints 1 forever. On 0 it skips the loop and prints 0. The last line pushes 3, then 0 and 1, so x gives (1-0)/3.
The specification also has a program that should multiply two numbers, marked as potentially incorrect. This implementation prints 0 for 2 and 3.
Patterns it teaches
- Stack machines: one stack of numbers and a table of variables.
- Turing tarpit: one literal and one sum.
Further
See also
Stack-based
Write your own in Run Convert a program from 3x
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: 3x.
Command line
shib run 3x -e '?([1])[0]' --input '0'shib manual 3xSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/3x.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org