esolang

Fish (><>)

A fish swims through a 2D codebox, bouncing off mirrors, carrying a stack of stacks. One wrong move and something smells fishy.

Harpyon, 2009 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 4 examples and 13 known programs · Hello World on Try It Online: identical · Test method

Run it

Hello World

Expected output

Hello, world!

Output


The idea

<> (say "fish") was made by Harpyon in 2009. It is a close relative of Befunge: code on a

grid, an instruction pointer that moves in four directions and wraps at the edges, and a stack.

What it adds is better plumbing. Diagonal mirrors / and \ bend the path without arrows. Hexadecimal digits push 0–15 in one character. There is a register, a whole stack of stacks, and . to jump anywhere. That makes it a favourite for short, tangled answers on code-golf sites.

It also has one famous error message. Divide by zero, pop an empty stack or hit an unknown character, and the only thing you will ever hear is something smells fishy....

The machine

A codebox (the source, unbounded in both directions), an IP starting top-left moving right, a stack of numbers, and one register per stack. The IP wraps around the edges of the source's bounding box.

CharDoes
> < ^ vSwim right, left, up, down
/ \ \| _ #Mirrors: diagonal, vertical, horizontal, and "reflect from anywhere"
xRandom direction
!Trampoline: skip the next cell
?Pop; skip the next cell if it was 0
.Pop y, x; jump to (x, y)
0-9 a-fPush 0 to 15
+ - * , %Arithmetic (, is division, and it is not integer division)
= ( )Equal, less than, greater than: push 1 or 0
" 'String mode until the matching quote
: ~ $ @Duplicate, drop, swap, rotate the top three
{ } r lShift the stack left or right, reverse it, push its length
[ ]Split off the top n values as a new stack; merge it back
&Register: store the top if empty, else push it back
i o nRead a character (-1 at end), print a character, print a number
g pRead or write the codebox
;Stop

Example

Print Hi!.

Print Hi!. The first line pushes the string and turns down; the second line loops until the stack is empty. l pushes the stack length, and ?!; means "if it is 0, skip the ! and land on ;".

"!iH"v
     >l?!;o

A countdown from 10. a pushes 10; the loop prints the number and a newline (ao), subtracts one, and stops when the top is 0. The second row wraps around onto itself.

av
 >:nao1-:?!;

And a well-known quine. ' pushes the rest of the line, wraps around and ends string mode; r reverses the stack; 3d* pushes 39, the code for '; then >o< bounces, printing until the stack runs out. It ends with the fishy error, which is allowed by most quine rules.

'r3d*>o<

Run them with the subset interpreter (no [ ]):

Checked examples

Hello World

Expected output

Hello, world!

Output


Example

Expected output

abc

Output


Example

Expected output

abc

Output


Quirks

  • A run stops after 3,000,000 steps.

Try this

  1. Write cat: read with i, stop when it gives -1, print with o.
  2. Print the first ten Fibonacci numbers, one per line.
  3. Hard: print the input backwards. Hint: read everything first; the stack already holds it in the right order for printing.

Patterns it teaches

  • Two-dimensional control flow: mirrors make paths that cross themselves, which is how most fish programs stay small.
  • Stack machines: one stack, then a stack of stacks with [ and ].
  • Self-modifying code: p writes to the codebox, including far outside the source, which turns the codebox into unbounded memory.
  • Quine: string mode wrapping around a line gives one of the shortest quines in any language.
  • Code golf: hex digits and mirrors are golf tools.

Further

Self-modifying

Two-dimensional languages

Write your own in Run Convert a program from Fish (><>)

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: fish.

Command line

shib run fish -e '40gn ;'
shib manual fish

Source

Module
langs/grid/index.js
Interpreter
langs/grid/fish.js

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

Source esolangs.org (CC0) · Edit on esolangs.org