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 ASCII character corresponding to the value in 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

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.

Examples

encode.fault

This program defines a peculiar cat program which prints the character immediately following the user input in the ASCII repertoire's order.

$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, output 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

This program defines a peculiar cat program which prints the character immediately preceding the user input in the ASCII repertoire's order.

$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

Prints the user input's ASCII code, then halves and prints it repeatedly using the floor operation until it reaches zero.

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

Cat program

A cat program follows which repeats until the user enters no input:

/ , : |

Truth-machine

Utilizing conditionals

This truth-machine implementation employs the conditional facility of brainfault:

,
!48(.)
!49([.])

Utilizing subroutines and recursion

The capability for subroutines and their recursive application is harnessed in the following to implement the truth-machine in an alternative way:

$printRecursively{. !49(*printRecursively*)}
,
*printRecursively*

Interpreter

  • Common Lisp implementation of the brainfault programming language.