Pushy
Two stacks of integers and nothing else. A string is its character codes.
FTcode · Turing complete: yes · ●●○ moderate
Tested with 8 known programs · Hello World on Try It Online: identical except the line end · Test method
Run it
The idea
Pushy is a golfing language by FTcode, written in Python. It only knows integers. Text in backticks pushes the code of each of its characters, and printing turns codes back into characters, so a string is a run of numbers on the stack like everything else.
Read the story
There are two stacks, IN and OUT. The program starts on IN, which already holds the input. I and O switch between them, v and ^ move one item across, and F swaps their whole contents.
The machine
| Command | Does |
|---|---|
0 12 `text` | Push a number, or the codes of each character |
+ - * / % e | Arithmetic with the top on the right; / floors, e is a power |
h t ~ \| f | Add one, take one, negate, absolute value, factorial |
& . @ { } | Duplicate, drop, reverse, rotate either way |
R X | Push 1 to n, or 0 to n−1 |
S P L | Push the sum, the product, the length |
# _ " ' | Print the top, all items with spaces, all items as text, the top as a character |
? : $ [ … ; | If, n times, while the top is not 0, forever |
K / k | Apply the next operations to every item, or only to the top |
A command that needs more items than the stack has does nothing. A failed calculation, such as a division by 0, gives 0. Every print is followed by a delimiter, a newline at first.
Example
: takes 10 off the stack and runs the loop body ten times.
: takes 10 off the stack and runs the loop body ten times. h adds one to the 0 and # prints it.
The input is a Python literal. With K on, 2* doubles every item and _ prints them.
A string input arrives as its codes, @ reverses the stack and " prints it as text.
This implementation follows the reference interpreter, FTcode/Pushy on GitHub. It leaves out the random commands, the sleeps and d, and stops on them.
Patterns it teaches
- Code golf: one character per command, and only one type to think about.
- Stack machines: two stacks, with the input already on one of them.
Try this
- Print the squares of 1 to 10.
- Print the input string upside down, as uppercase letters with
Q. - Hard: print the primes below 100 with
p.
Further
- The reference interpreter, in Python, with a list of commands in its docs
- Related: CJam
See also
Also in Golfing
Write your own in Run Convert a program from Pushy
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: pushy.
Command line
shib run pushy -e 'f#' --input '6'shib manual pushySource
- Module
langs/tio/ports.js- Interpreter
langs/tio/pushy.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language