esolang

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

Expected: Hello, World!

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

CommandDoes
0 12 `text` Push a number, or the codes of each character
+ - * / % eArithmetic with the top on the right; / floors, e is a power
h t ~ \| fAdd one, take one, negate, absolute value, factorial
& . @ { }Duplicate, drop, reverse, rotate either way
R XPush 1 to n, or 0 to n−1
S P LPush 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 / kApply 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.
Expected: 1 2 3 4 5 6 7 8 9 10

: takes 10 off the stack and runs the loop body ten times. h adds one to the 0 and # prints it.

Expected: 2 4 6

The input is a Python literal. With K on, 2* doubles every item and _ prints them.

Expected: hello

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

  1. Print the squares of 1 to 10.
  2. Print the input string upside down, as uppercase letters with Q.
  3. Hard: print the primes below 100 with p.

Further

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 pushy

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/pushy.js

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