Brainfunc

From Esolang
Jump to navigation Jump to search

Brainfunc (not capitalized except at the start of a sentence) is brainfuck but with functions, designed with the goal of having only six commands. It is Turing-complete, as it has unbounded memory and recursion, which can simulate loops in brainfuck ([]).

Memory

Unbounded wrapping 8-bit cells. Uses a cell pointer (CP) to keep track of the current cell.

Functions

Code is implicitly turned into a function whenever a ) is reached outside an if statement, therefore nested functions are impossible as the inner function will not be separated from the outer one in the right way for this (e.g., ++)+) will be parsed as two functions (++ and +) rather than as a function with another function inside it. Functions aren’t necessary for the code to run, as there’s no 'main' function, but you can’t have recursion without them.

Commands

Command Meaning
+ Increment the cell at the CP
> Increment the CP
< Decrement the CP
% If the cell at the CP is less than 32 and not equal to 10 (newline), set the cell at the CP to the ASCII value of the first character of user input, or if no input is given, 10; else, output the ASCII character of the cell at the CP
^ If the cell at the CP is not 0, then call the (0-based) Nth function, where N is a base-7 number following this command (if the code after this command doesn’t follow the following regex: ^[0-6]+, an error is thrown)
) End the current function

Examples

For the sake of any readers, these examples are 'formatted' with spaces and newlines so you can clearly see where functions and if statements begin and end. Functions are indented with two spaces and separated by newlines. No formatting is actually necessary.

One-time cat

%%

Infinite cat

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

Handy subprograms

Written as functions. The same 'formatting' as #Examples applies here.

Decrement the cell at the CP

  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++)

Set the cell at the CP to 0

  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++^0)