Betterfunge

From Esolang
Jump to navigation Jump to search

Betterfunge is a 2D esolang. It is based on *><> and Ly.

Storage

Betterfunge uses an array of numeric stacks (with a memory pointer), a stack of functions, 3 global variables and 8 functions.

Commands

Control flow

↑ ↓ ← → Change IP direction to N, S, W and E respectively.

\ Continue at the beginning of the next line ("compressing")

¡ Reverse IP direction.

¤ Random direction.

Trampoline.

§ Goto. Pop v and w, then jump to (v, w) in the codebox without changing the direction.

↘ ↗ Dive and rise. Dive ignores all non-directional instructions until the next rise.

q Pop v, then quit with exit code v.

[code]: Push the function to the function stack. Can be nested.

? Ternary/if-then-else condition. Pop v from the pointed stack then w and x from the function stack. If v is 0 or NaN, execute x, else execute w.

@ While loop. Pop v from the function stack, then execute v until the top of the pointed stack is 0 or NaN (peek).

` Repetition. Pop v from the function stack and w from the pointed stack, then execute v w times.


Literals and operators

Any number Push the (floating-point) number to the current stack. To push multiple numbers, put a space between them. Use ¯ for negative numbers. Use and ¯∞ for infinity, and write NaN in lowercase (nan).

' " String literals. Push the Unicode all characters between (null-terminated, reversed for easy printing)

+ - × ÷ % * & | ^ Add, subtract, multiply, divide, remainder, power, bitand, bitor and bitxor. Pop v and w, push op(w, v).

= < > Equals, greater than and less than. Pop v and w, then push -1 if op(w, v), 0 otherwise. NaN is equal to NaN in Betterfunge.

√ ± ~Square root, negate and bitnot. Pop v, push op(v).

π ε φ н Constants. Push 3.14159..., 2.71828..., 1.61803..., and -1 respectively.

s Split. Pop v, then convert v into a 0gnirts, e.g. 0s pushes 0, then 48.

j Join. Pop a 0gnirts, then convert it into a number then push. NaN if invalid number.

Stack manipulation

# Duplicate the top item on the stack.

_ Pop and discard.

$ Swap the top two items.

r Reverse the stack.

« Move to the previous stack.

» Move to the next stack.

u Move the bottom value up.

dMove the top value down.

Pop v and create a new stack to the right, moving v values from the old one.

¢ Remove the current stack, pushing its values into the one to the left.


Input and output

: Output as a number.

; Output as a character.

Input as a number. NaN on EOF.

Input as a character. -1 on EOF.

f Open file. Pop a 0ginrts from the stack. If a file isn't open, it determines the file name. If a file is open, it is written to the file and the file is closed.

Others

Overloads A-Z given by the fingerprint given by the next 2 chars. NOP if error. (so far, no fingerprints have been implemented)

Unloads A-Z. NOP if error.

A-Z Reference a function. Can be overloaded by .

x-z Reference a variable. All variables' initial values are NaN.

If referencing function, pop v from the function stack and store into referenced function.
If referencing variable, pop v, then set the referernced variable to v.

¥ If referencing function, execute the corresponding function.
If referencing variable, Push the referenced variable's value.

g Pop v and w, then push the charcode at (v, w). Empty cells are equal to 0.

p Pop v, w and x, then change the char at (v, w) to x.

Examples

Never Gonna Give You Up

Prints the lyrics of the whole song.

   [10;]N₩\
   ["We're no strangers to love"[;]@N¥\
   "You know the rules and so do I"[;]@N¥\
   "A full commitment's what I'm thinking of"[;]@N¥\
   "You wouldn't get this from any other guy"[;]@N¥]A₩\
   \
   ["I just wanna tell you how I'm feeling"[;]@N¥\
   "Gotta make you understand"[;]@N¥N¥]X₩\
   \
   ["We've known each other for so long"[;]@N¥\
   "Your heart's been aching but you're too shy to say it"[;]@N¥\
   "Inside we both know what's been going on"[;]@N¥\
   "We know the game and we're gonna play it"[;]@N¥]B₩\
   \
   ["And if you ask me how I'm feeling"[;]@N¥\
   "Don't tell me you're too blind to see"[;]@N¥N¥]Y₩\
   \
   ["Never gonna give you up"[;]@N¥\
   "Never gonna let you down"[;]@N¥\
   "Never gonna run around and desert you"[;]@N¥\
   "Never gonna make you cry"[;]@N¥\
   "Never gonna say goodbye"[;]@N¥]₩\
   \
   ["Never gonna tell a lie and hurt you"[;]@N¥N¥]D\
   \
   ["Never gonna give, never gonna give"[;]@N¥\
   "(Give you up)"[;]@N¥N¥]E₩\
   A¥X¥C¥D¥B¥Y¥C¥D¥C¥D¥E¥B¥Y¥C¥D¥C¥D¥C¥0q

Truth Machine

  ‘[#:]@:q

Calculator

Type 1 to add, 2 to subtract, 3 to multiply, 4 to divide, 5 for power and 6 for square root.

  [‘‘+]A₩[‘‘$-]S₩[‘‘×]M₩[‘‘$÷]D₩[‘‘$*]P₩[‘√]R₩‘1-[A¥:0q][]?1-[S¥:0q][]?1-[M¥:0q][]?1-[D¥:0q][]?1-[P¥:0q][]?1-[R¥:0q][]?"Error: invalid operation"[;]@q