Brainfault

From Esolang
Jump to navigation Jump to search

Brainfault is a cell-based esolang heavily based on brainfuck. It has all of brainfuck's commands as well as a few others for ease of use.

Commands

Command Description
> Move the pointer to the right
< Move the pointer to the left
+ Increment the memory cell at the pointer
- Decrement the memory cell at the pointer
. Output the character signified by the cell at the pointer
: Output the current cell's value as an integer
? Output the current cell's value as binary
, Input a character and store it in the cell at the pointer
[ Jump past the matching ] if the cell at the pointer is 0
] Jump back to the matching [ if the cell at the pointer is nonzero
/ if the input queue is empty
| Jump back to the matching / if input queue is not empty
!int(commands) If the cell at the pointer is not equal to int, jump past the matching )
!~int(commands) If the cell at the pointer is equal to int, jump past the matching )
$subroutine_name{commands} Creates a subroutine named "subroutine_name". Whenever this is executed, everything inside the curly brackets will be executed
*subroutine_name* Executes the subroutine called subroutine_name
#comment# Any characters between pound symbols will be regarded as a comment, regardless of if they contain brainfault code
!int(commands) If the cell at the pointer is not equal to int, jump past the matching )

Things to note about subroutines:

  • All subroutines are global—no defining subroutines inside subroutines
  • All subroutines are hoisted—it doesn't matter where you define them, they can be executed anywhere in the code
  • Subroutine names must consist only of uppercase letters, lowercase letters, and underscores

Just like brainfuck, each cell holds one byte. Incrementing past 255 will wrap to 0 and decrementing below 0 will wrap to 255. Unlike brainfuck, there are (theoretically) infinite cells in either direction.

Example brainfault program:

encode.fault

 $add_twenty{++++++++++++++++++++}
 $add_hundred{*add_twenty**add_twenty**add_twenty**add_twenty**add_twenty*}
 /# while there is input in the queue #
   , # accept one byte of input #
   >[-]< # reset the flag #
   !~32(!~90(!~122(+.>+<))) # if the input is not a space, a capital Z, nor a lowercase z, add one to its ascii value, output it, and set the flag to one #
   !32(>!0(<.>)<) # if the input is a space and the flag is zero, output the space #
   !90(>!0(<[-]*add_twenty**add_twenty**add_twenty*+++++.>)<) # if the input is a capital Z and the flag is zero, ourput a capital A #
   !122(>!0(<*add_hundred*---.>)<) # if the input is a lowercase z and the flag is zero, output a lowercase a #
 |

decode.fault

 $add_twenty{++++++++++++++++++++}
 $add_hundred{*add_twenty**add_twenty**add_twenty**add_twenty**add_twenty*}
 /
 ,>[-]<!~32(!~97(!~65(-.>+<)))!32(>!0(<.>)<)!97(>!0(<[-]*add_hundred**add_twenty*++.>)<)!65(>!0(<*add_hundred*----------.>)<)
 |

div_two.fault

 $div_two{!~0([!1(-)!~0(-->+<)]>[-<+>]<)}$ten{++++++++++}*ten*>,:<.>[*div_two*:<.>][-]<. # continuously (floor) divide the input by two and output it while it is nonzero #