Fish

From Esolang
Jump to: navigation, search
The title of this article is incorrect because of technical limitations. The correct title is ><>.

><> (pronounced /ˈfɪʃ/) is a stack-based, reflective, two-dimensional esoteric programming language. It draws inspiration from among others Befunge. It was invented by Harpyon in 2009.

Concepts[edit]

><> is a two-dimensional language, meaning the code is not necessarily executed in a linear manner. Using various instructions, the direction the code is read can be changed to either up, down, left or right. It is also a stack-based language, so all operations are performed on a stack. You can also store and retrieve values in the codebox, so making a proper compiler is very hard, if not impossible.

What makes ><> unique is the fact that it supports multiple stacks. A program starts off with one stack, where values are pushed and popped. The program can however create a new stack on top of the original one, with a specific amount of values moved over from the original stack. The program can create as many new stacks as it wants to. When a stack is removed, the values residing on it are moved to the top of the underlying stack. Example here. Each stack is effectively a new scope in the program; this allows for writing functions and snippets to drop into code easily.

Code execution[edit]

><> code is layed out in a two-dimensional codebox. The codebox is infinite in all directions, positive and negative. It is interpreted by the instruction pointer, or the IP.

Note: all coordinates for the codebox are given as (character,line).

The instruction pointer starts out at (0,0), i.e. the top-left of the script. It initially moves towards the right, but it can be changed using a variety of instructions. When the IP reaches the end of the code, it will wrap around, or jump to the opposite side. The IP will never reach the negative side of the code; this makes this space useful for storing values. Execution will not stop until a ; instruction is met, or an error occurs.

In the following example, the IP moves downwards, leftwards, wraps around to the right side, goes upwards, leftwards and stops.

v ;<

<  ^

Mirrors[edit]

Mirrors are special kinds of instructions that will change the direction of the IP depending on the direction it already has. For example, if the IP is moving rightwards and meets a \ instruction, it will be "reflected" (unrelated to reflection) downwards. If the IP meets it going downwards, it will be reflected to the right, and so on. Meeting _ horizontally or | vertically equals a NOP, but meeting them vertically and horizontally, respectively, will invert the direction, and # will do so unconditionally.

Stacks[edit]

><> is stack-based, and thus every instruction except movement-related and the end-execution instruction (;) do something to the stack. You can push numbers and input, perform modifications and pop output using various instructions. You can also create new stacks, this using the [ instruction. Creating a new stack pulls a specified number of items from the current stack onto the new one. The new stack is completely isolated from the previous one, and an arbitrary amount of stacks can be created on top of each other. When the ] instruction is met, the current stack is popped and its values are moved back to the underlying stack. If the current stack is the last stack, ] simply empties the stack and registry.

12345 3[  r  ]  rnnnnn;
  ^    ^  ^  ^     ^
  |    |  |  |     | Output: 12543. The three top values were reversed!
  |    |  |  |
  |    |  |  | The second stack is removed, putting its values back to the first stack.
  |    |  |
  |    |  | The second stack is reversed.
  |    |
  |    | A second stack is created, pulling 3 values from the first stack
  |
  | Numbers 1-5 are pushed onto the first stack.

The register[edit]

The & instruction is used to store and retrieve values in a register. If the register is empty, & stores a value in it. When the register holds a value, & pushes it back onto the stack.

When creating new stacks, new registers are also created. However, the values aren't moved around; each stack starts off with an empty register, and when the stack is removed, the register is dropped.

Input/output[edit]

><> has three input/output instructions: i for input and o/n for output. The i instruction simply reads a character from stdin. If no more input is available, i pushes -1. Note that there is no instruction to input a number, you will have to parse the characters yourself.

While parsing numbers is not very hard, it makes for slow and possibly glitchy programs. Most programs requiring number input reads it from the stack at program start. This is done with an interpreter that supports pre-populating the stack with values. Doing so with the fish.py interpreter looks like this:

$ ./fish.py --code "2*n;" --value 10
20

Output is a bit easier, as you have two instructions: o and n. They will pop a value and output it as a character and a number, respectively. The output is naturally written to stdout.

Errors[edit]

The following events will cause an error in ><>:

  • Division by zero
  • Reaching an invalid instruction
  • Trying to pop or modify on the stack when it is empty or has too few values
  • If the interpreter does not support arbitrary-precision numbers, an overflow results in an error

Although there are multiple reasons an error may occur, there is only one error message: something smells fishy...

Instructions[edit]

The following is merely a draft of the instructions in ><>, and is subject to change.

Movement and execution[edit]

 > < ^ v Change the direction of the instruction pointer (right, left, up, down respectively)
 / \ | _ # Mirrors; the IP will change direction depending on what direction it already has.
 x Random direction.
 ! Trampoline - skip the following instruction.
 ? Conditional trampoline - pop one value off the stack. The next instruction is only executed if the popped value is non-zero.
. Jump - pop y and x off the stack, and move the IP to (x,y) in the codebox. The current direction is retained. Note that you have to jump to the cell before the instructions you want to execute, as the IP will move one position next tick before executing.

Literals and operators[edit]

 0-9 a-f Push the corresponding value onto the stack. a = 10, ..., f = 15
 + - * , % Addition, subtraction, multiplication, division and modulo, respectively. Pop x and y off the stack, and push y operator x. Division is float division (meaning 94,n; outputs 2.25 and not 2). Division by 0 raises an error.
 = Equals. Pop x and y off the stack, and push 1 if y = x, and 0 otherwise.
 ) ( Greater than and less than, respectively. Pop x and y off the stack, and push 1 if y operator x, and 0 otherwise.
 ' " Single and double quote - enable string parsing. String parsing pushes every character found to the stack until it finds a closing quote.

Stack manipulation[edit]

 : Duplicate the top value on the stack.
 ~ Remove the top value from the stack.
 $ Swap the top two values on the stack
 @ Swap the top three values on the stack, shifting them rightwards (e.g. if your stack is 1,2,3,4, calling @ results in 1,4,2,3)
 } { Shift the entire stack to the right and left, respectively. (e.g. when having 1,2,3,4, calling } results in 4,1,2,3 while { results in 2,3,4,1)
 r Reverse the stack.
 l Push the length of the stack onto the stack.
 [ Pop x off the stack and create a new stack, moving x values from the old stack onto the new one. See Stacks.
 ] Remove the current stack, moving its values to the top of the underlying stack.

Input/output[edit]

See Input/output.

 o n Pop and output as a character and a number, respectively. Output is written to stdout.
 i Read one character from stdin and push it to the stack. The character should not be shown when read from console. When no more input is available, -1 is pushed.

Reflection/miscellaneous[edit]

 & Pop the top value off the stack and put it in the register. Calling & again will take the value in the register and put it back on the stack. See The register.
 g Pop y and x off the stack, and push the value at x,y in the codebox. Empty cells are equal to 0.
 p Pop y, x, and v off the stack, and change the value at x,y to v. E.g. 123p puts 1 at 2,3 in the codebox.
 ; End execution.

The space character is simply a NOP and is allowed anywhere.

Examples[edit]

All the examples are executed using the fish.py interpreter.

Hello, world![edit]

!v"hello, world"r!
 >l?!;o
$ ./fish.py helloworld.fish 
hello, world

FizzBuzz[edit]

0voa                            ~/?=0:\
 voa            oooo'Buzz'~<     /
 >1+:aa*1+=?;::5%:{3%:@*?\?/'zziF'oooo/
 ^oa                 n:~~/
$ ./fish.py fizzbuzz.fish
1
2
Fizz
...
98
Fizz
Buzz

Fibonacci sequence[edit]

Outputs Fibonacci numbers until stopped. The execution is slowed down using -t 0.01, to avoid being spammed to death with the endless fibonacci sequence.

0:n84*o1:nv
 n:+@:o*48<
$ ./fish.py fibonacci.fish -t 0.01
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 ...

Factorial[edit]

Calculates the factorial of the input number. Assumes the input is a non-negative integer. Notice that the input value, 10, is supplied using the -v switch of the interpreter.

v
\:2(?v
     >1n;      
>:>1-::{*v
n*^!?(2@ <;
$ ./fish.py factorial.fish -v 10
3628800

Quine[edit]

Outputs its own source code.

"r00gol?!;40.
$ ./fish.py quine.fish
"r00gol?!;40.

Square root[edit]

Calculates and outputs the square root of the top of the stack/input number.

  1[:>:r:@@:@,v
;n]~$^!?={:,2+<
$ ./fish.py sqrt.fish -v 64
8

Brainfuck interpreter[edit]

v
1
0

           >$    >$                 \
>:@$:@i:0(?^$:2=?^$:3b*-0=?\v/      >:&$:1=?v>$@p&0(?v$1+
\                  02]p00~~<> :'+'=?^\      $$       >1-0p
\01-\                       /^?='>': <      d+
v   <}            v?= \     > :'<'=?^\      %e
v}]p$p4r}:r:$4[2@:/f1+/     /^?='-': <      \/
>{1+:00g=?\:1g:e-?^~:}0a.   > :'['=?^\
/     -1-:/                 /^?=']': <
>1+:00g=?;:1g0$.            > :'.'=?^\
^             ~<   $\.51$~~ ~^?=',': <
 '['~ 20g:3g  ?^>$4g^

 ']'~ 20g:3g0=?^^
^                 <     \
 '+'~ 20g:3g1+3$@p^
 ','~ 30g:1+30p2g  20g3p/
 '-'~ 20g:3g1-3$@p^
 '.'~ 20g3go      /
 '<'~ 20g1-20p\
^             <
 '>'~ 20g1+20p/
echo '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.' | ./fish.py brainfuck.fish
Hello World!
echo '-,+[-[>>++++[>++++++++<-]<+<-[>+>+>-[>>>]<[[>+<-]>>+>]<<<<<-]]>>>[-]+>--[-[<->+++[-]]]<[++++++++++++<[>-[>+>>]>[+[<+>-]>+>>]<<<<<-]>>[<+>-]>[-[-<<[-]>>]<<[<<->>-]>>]<<[<<+>>-]]<[-]<.[-]<-,+]!Esolangs' | ./fish.py brainfuck.fish
Rfbynatf

Interpreters[edit]

fish.py[edit]

The "official" interpreter, or the one written by the author. The latest version is available here. Requires python versions 2.7 or 3.2 or later. For usage information, run it with the --help switch.

fish.go[edit]

fish.go is written by Corey Thomasson and should follow all specifications. It is also bundled with a bunch of examples. Download at GitHub.

http://fishlanguage.com[edit]

An online interpreter, written in Javascript. Features animation of the instruction pointer moving through the program. The interpreter works well, but the site is work-in-progress, and may be slow.

fish.java[edit]

An interpreter written by Charlie Bouthoorn. It has the same console arguments as the "official" interpreter. Get the source from GitHub here.

Older interpreters[edit]

The following interpreters were written towards an older version of ><> that featured multithreading. The multithreading capabilities were removed and replaced with multiple stacks support, which was deemed more useful (not that esoteric languages are useful in the first place). These interpreters may not work with some updated instructions, like p, [ and ]. You can of course still use these interpreters using the old documentation.

Python[edit]

The old version of the official python interpreter is available through Gist.

Invoke it by first making sure you have installed python, and typing python fish.py <script> [tick] into your system's command line. Script is replaced with a .fish file, and tick is optional and defines a delay in seconds between each instruction, to slow down execution for debugging etc. It should work with both python 2.x and 3.x, as well as both Windows, Linux and Mac.

Delphi[edit]

Another interpreter, written in Delphi, is a result of a so-called 'code-golf challenge'. It interprets every instruction (except the threading related instructions) in about 1290 characters of source code. (A version with threading support is also available on the same site.) Get it here!

Compile the code into a console application with any Unicode-enabled version of Delphi (a port to the FreePascalCompiler FPC is trivial). Invoke the resulting executable and supply the path to a .fish file on the command line. Speed can be limited by increasing the Sleep() time in the code.

More[edit]

There are several interpreters in a multitude of languages available in the StackExchange code-golf challenge.