Yeefunge
Yeefunge is a Befunge-inspired programming language made by me, User:LEOMOK. It is inspired by +Output, but includes more features.
The Basics
Like +Output, Yeefunge is two-dimensional. The arrows <
, >
, ^
and v
rotate the instruction pointer (IP) left, right, up and down respectively.
The program ends once a semicolon ;
is reached.
Yeefunge operates on a single stack of numbers, strings and arrays.
Numbers
To push a number, you simply write its decimal representation, as a run of digits with optionally a decimal point at the start or middle. They may also contain a minus sign at the start. They must be written in the same direction as the IP.
For example:
> 123 0.45 -1 v - 9 7 ; 123234 227. <
This pushes the numbers 123, 0.45, -1, -97, 0.722 and 432321 to the stack.
Now you might be thinking: "Hang on, isn't the minus sign also used for subtraction? How will the interpreter tell which to use?"
The answer is really simple. The interpreter checks if the character after the minus sign (in the current direction) is a digit. If not, the minus sign does subtraction.
Strings
A double quote "
will push all characters up to the next double quote as a string. Like numbers, strings are pushed in the same direction as the IP. For example:
> v ;"Hello, world!"<
will push "!dlrow ,olleH"
, not "Hello, world!"
, as the IP is going left when the string is pushed.
You can also push a single character with a single quote '
, for example 'a
or '\x86
.
Unlike in +Output, strings may contain these escape codes:
\"
- a double quote\\
- a backslash\n
- a newline\t
- a tab\x00
- a one-byte Unicode character
(hex digits can be uppercase or lowercase)\u0000
- a two-byte Unicode character\U000000
- a three-byte Unicode character
When a backslash is encountered, the IP will take the next 1-7 characters (depending on the first character after the backslash) and treat them specially.
Arrays
Arrays are created using square brackets [
and ]
. When a [
is encountered, the current size of the stack is marked, and ]
will merge all values from the last marked point into an array.
Unlike in traditional programming languages, an unmatched ]
does not throw an error. Instead, it groups the whole stack into an array.
Putting it all together
With all three data types, it is possible to create complex structures like these:
>[1 [2 "text\n" v ; - \ 5 \ 7 ' ' 9 a . ] ^["cdefgh" 56.7-<
This will push the array [1, [2, "text\n", -57, "a"], -7.65, "hgfedc"]
, the number 0.9, and a string containing only a backslash.
Instructions
All indexing starts from 0.
Stack operations
Instruction | Popped values | What it does |
---|---|---|
: |
Duplicate the top stack value. | |
$ |
Remove the top stack value. | |
\ |
Swap the top two stack values. | |
@ |
Rotate the top three stack values clockwise. | |
|
Example | Example |
Example | Example | Example |
Example | Example | Example |
General operations
Instruction | Popped values | What it does |
---|---|---|
! |
x | Push 1 if x is zero, empty string or empty array, else 0. |
+ |
x, y | Push y + x. If both are strings or arrays, concatenate. |
- |
x, y | Push y - x. If followed by a digit, included as part of the number. |
* |
x, y | Push y * x. If one is a number and one is a string/array, repeat the string/array that many times. |
/ |
x, y | Push y / x. If a is 0, throw an exception. |
% |
x, y | Push y mod x, with the sign of a. If a is 0, throw an exception. |
~ |
x, y | Push y to the power of x. 0 to the power of 0 is 1. |
= |
x, y | Push 1 if y has the same type and value as x, else 0. Arrays are not compared by reference. |
( |
x, y | Push 1 if y is less than x, else 0. If both are strings, compare lexicographically. |
) |
x, y | Push 1 if y is greater than x, else 0. If both are strings, compare lexicographically. |
List and string operations
Instruction | Popped values | What it does |
---|---|---|
& |
x, y | Push y[x]. x can be a single index or a list of indices. |
a |
x, y | Append y to x. |
c |
x, y | Split y by x into an array. If x is a number, split at every n characters. |
h |
x, y | Take the first y values of x. |
j |
x, y | Join y by x. |
l |
x | Push length of x. |
r |
x, y | Push range of numbers x (inclusive) to y (exclusive). |
t |
x, y | Drop the first y values of x. |
x |
x, y | Push index of first occurrence of y in x. -1 if not found. |
y |
x, y, z | Insert z at position y of x. |
z |
x, y | Delete element y of x. |
R |
x | Push a random value in x. |
S |
x | Push x sorted. |
X |
x, y, z | If y is a number, set z[y] to x. Else, replace all occurences of z with y in x. |
Z |
x | Dump the values of x onto the stack. |
Conversions
Instruction | Popped values | What it does |
---|---|---|
n |
x | Convert x to number. If x is non-numeric, push x back. |
s |
x | Convert to string. |
u |
x | If x is a number, convert to Unicode character. If x is a list of numbers, convert to Unicode string. If x is a string, convert to list of Unicode codepoints. |
b |
x, y | If x is a number, push base-x digits of y as an array. If x is an array, interpret it as a base-y number. |