Brainfunc

From Esolang
Jump to navigation Jump to search

Brainfunc (not capitalized except at the start of a sentence) is a variation of brainfuck which uses functions instead of loops for flow control.

Memory

Brainfunc uses a tape of wrapping unsigned eight-bit cells, unbounded in both directions. It also uses a cell pointer to keep track of which cell is currently selected. The cell pointed to by the cell pointer is often called the current cell.

Functions

Functions are terminated by right parentheses ()). Execution begins at the character after the rightmost such parenthesis. For example, the program ++)+)%>+ contains the functions ++ and +, and would begin execution at the percentage sign (%); note that the subprogram %>+ is not a function, since it is not terminated by a right parenthesis.

Commands

Command Effect
+ Increment the current cell.
> Increment the cell pointer.
< Decrement the cell pointer.
% If the current cell is less than 32 and not equal to 10, then take the Unicode codepoint of the first character of user input, divide it by 256, and set the current cell to the remainder; if no input is given, then set the current cell to the number 10 instead. Otherwise, output the Unicode character whose codepoint is equal to the current cell.
^ If the current cell is not 0, call the (zero-indexed) Nth function, where N is a base-7 number following this command. If the code after this command does not match the regular expression ^[0-6]+, or if the Nth function does not exist, then throw an error and terminate the program instead.
) Return from the current function.

Implementations can also implement the optional ? command, which outputs the current state of the tape in an implementation-defined manner.

All unrecognised characters are ignored.

Examples

Hello, world!

^2^2^2^2^2^2^2)+++++)^1^1)
++^0%
^2^2^1++++%
^1++%%+++%>
++++^2^2^2^2%>
++^2^2^2%<
^0^1%<
%+++%^0^0^0^2^2^2^2%>
^0^0^0^2^2^1++%>
+%
>+++++^1%

Cat program

%%>+^0)+^0

Truth-machine

^1^1^1^1^1^1^2)++++++++)%^2)%%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++^0

Print all printable characters

%+^0)+++++++)+++^1%+^1^1^1^0

External resources