/awesome-brainfuck

๐Ÿฆ„ Brainfuck is an esoteric programming language ๐Ÿฆ„

Primary LanguageBrainfuck

brainfuck

Brainfuck is an esoteric programming language

๐Ÿ“š Historical Background

Brainfuck programming language created in 1993 by Urban Mรผller. In 1992, Urban Mรผller, a Swiss physics student, took over a small online archive for Amiga software. The archive grew more popular, and was soon mirrored around the world. Today, it is the world's largest Amiga archive, known as Aminet.

Mรผller designed Brainfuck with the goal of implementing it with the smallest possible compiler, inspired by the 1024-byte compiler for the FALSE programming language. Mรผller's original compiler was implemented in machine language and compiled to a binary with a size of 296 bytes. He uploaded the first Brainfuck compiler to Aminet in 1993. The program came with a "Readme" file, which briefly described the language, and challenged the reader "Who can program anything useful with it? :)". Mรผller also included an interpreter and some quite elaborate examples. A second version of the compiler used only 240 bytes.

As Aminet grew, the compiler became popular among the Amiga community, and in time it was implemented for other platforms. Several brainfuck compilers have been made smaller than 200 bytes, and one is only 100 bytes.

๐Ÿ“š Design Goals

Brainfuck is created for extreme minimalism. The aim of this programming language is to force and entertain programmers rather than being a general language.

๐Ÿ“š Usage Areas

The use of brainfuck is the development of algorithmic approaches of programmers.

๐Ÿ“š Typing Discipline

Typeless ๐Ÿ‘ ๐Ÿ’ฏ

๐Ÿ“š Paradigm

  • Esoteric
  • Imperative
  • Structured

๐Ÿ“š Memory system

tape-based

๐Ÿ“š Dimensions

one-dimensional

๐Ÿ“š Computational class

Turing complete

๐Ÿ“š File extension(s)

  • .b
  • .bf

๐Ÿ“š Self-interpreters

Writing a self-interpreter in brainfuck is not a simple task, yet, several self-interpreters have been written throughout the years.

You can find in self-interpreters directory.

  • by Frans Faase - Perhaps the first one.

  • by Keymaker - Designed in the strictest 8-bit, non-wrapping, EOF = no change (EOF 0 works too) environment. The program emulates unbound cell size for cells (the program +[+] is valid and never ends) -- not really a brainfuck feature but it's there anyway -- and of course all the brainfuck programs written for the 8-bit non-wrapping environment work as supposed to. Supports infinite/unbound number of cells and nested loops.

  • by Daniel B Cristofani - The shortest; see also dbfi

  • by Clive Gifford - The fastest

  • by Adam Domurad - Interprets Brainfuck code from the input until a %, then reads remaining input as input for the interpreted program. Comments are allowed, and up to 256-depth nested loops

    Also you can go deeper with these: Daniel B Cristofani's implementation of a universal Turing machine in brainfuck provides a proof by turing simulation.

๐Ÿ“š Thoughts

  • Great exercice for your brain

    • If you ever succeed to use it at least once, whatever it does, you will never be afraid to learn any other programming language.
  • Very good introduction in geek communities

    • Say you made a Brainfuck program once, and count how many geeks/nerds you will attract.
  • Designed to challenge and amuse programmers

    • Rather than have a practical application, Brainfuck is an esoteric language for the purpose of challenging programmers who want to code in a more unusual language.
  • Esoteric language

    • Brainfuck is by design extremely difficult to use. This makes it un-learnable to beginners.
  • Impractical language, used only as a toy

    • It won't get you a job. It is not used in any practical application.
  • R-rated name

    • It's difficult to talk about in polite company.

๐Ÿ“š Language Design

The language consists of eight commands, listed below. A brainfuck program is a sequence of these commands, possibly interspersed with other characters (which are ignored). The commands are executed sequentially, with some exceptions: an instruction pointer begins at the first command, and each command it points to is executed, after which it normally moves forward to the next command. The program terminates when the instruction pointer moves past the last command.

The brainfuck language uses a simple machine model consisting of the program and instruction pointer, as well as an array of at least 30,000 byte cells initialized to zero; a movable data pointer (initialized to point to the leftmost byte of the array); and two streams of bytes for input and output (most often connected to a keyboard and a monitor respectively, and using the ASCII character encoding).

Character Meaning
> increment the data pointer (to point to the next cell to the right).
< decrement the data pointer (to point to the next cell to the left).
+ increment (increase by one) the byte at the data pointer.
- decrement (decrease by one) the byte at the data pointer.
. output the byte at the data pointer.
, accept one byte of input, storing its value in the byte at the data pointer.
[ if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
] if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.

Brainfuck is an example of a so-called Turing tarpit: It can be used to write any program, but it is not practical to do so, because Brainfuck provides so little abstraction that the programs get very long or complicated.

Brainfuck's formal "parent language" is P'', in addition example P'' program, Program for compute the predecessor (x-1) of an integer x > 0:

R ( R ) L ( r' ( L ( L ) ) r' L ) R r

which translates directly to the equivalent brainfuck program:

[ > ] < [ โˆ’  [ < [ < ] ] โˆ’  < ] > +

๐Ÿ“š Example usages

๐Ÿ”— Hello world

The following program prints "Hello World!" and a newline to the screen:

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

Yet another cool hello world example with brainfuck by Robert de Bath

[]><[][]><[][]><[][]><[][]><[][]><[][]><[][]><[][]><[][]><[]
[]>+>+>++>++<[>[->++++<<+++>]<<]>----.>->+.+++++++..+++.<+[]
[ This is hellbox, a 104 command Hello World               ]
[   >+>+>++>++<[>[->++++<<+++>]<<]>----.>>+.+++++++..+++   ]
[   .>.<<<+++++++++++++++.>>.+++.------.--------.>+.>++.   ]
[ -- ร‡aฤŸatay ร‡alฤฑ -- 2017                                ]
[]>>.<<<+++++++++++++++.>>.+++.------.--------.>+.+>++.<<<[]
[]><[][]><[][]><[][]><[][]><[][]><[][]><[][]><[][]><[][]><[]

example

>+[[->+>+<<]>>[-<<+>>]<<<[->>+>+<<<]>>>[-<<<+>>>]<]

There is no way to generate pseudorandoms without seed

example

>>>++[
    <+++++++++[
        <[<++>-]>>[>>]+>>+[
            -[->>+<<<[<[<<]<+>]>[>[>>]]]
            <[>>[-]]>[>[-<<]>[<+<]]+<<
        ]<[>+<-]>>-
    ]<.[-]>>
]

๐Ÿ”‘ Caesar cipher

example

>>>,[>>,]<++++++++++++[->++>+++++>+++++++>++++++++>++++++++>++++++++++>++++++++++<<<<<<<]>->+++++>++++>----->+>>+++[<]<[>>[>]+[<]<[->+<]>[<+[>]>+<<[<]>>-]++[>]<<[>>>>+>+<<<<<-]>>>>>[-<<<<<+>>>>>]<<[->-[>]<<]<[>>[-]<<+>]>>[-]<<<-[-<[<]>>-[>]]<[<]>>-[-<[->+<]>[<+[>]>>+<<<[<]>>-]++[>]+<<[>>>>>+>+<<<<<<-]>>>>>>[-<<<<<<+>>>>>>]<<[->-[>]<<]<[>>[-]<<+>]>>[-]<<<-[-<<<<<<<[>>>>>>>+>+<<<<<<<<-]>>>>>>>>[-<<<<<<<<+>>>>>>>>][>]<[[<]>-[>]<-]<[<]>>-[>]]<[<]>>-[-<[->+<]>[<+[>]>>+<<<[<]>>-]++[>]<<<[>>>+>>>+<<<<<<-]>>>[<<<+>>>-]+>>[->-[>]<<]<[>>[-]<<+>]>>[-]<<<-[-<[<]>+++>-[>]]<[<]>>-[-<[->+<]>[<+[>]>>+<<<[<]>>-]++[>]<<<<[>>>>+>>>+<<<<<<<-]>>>>[<<<<+>>>>-]+>>[->-[>]<<]<[>>[-]<<+>]>>[-]<<<-[-<[<]>>-[>]]<[<]>>-[-<[->+<]>[<+[>]>>+<<<[<]>>-]++[>]<<<<<[>>>>>+>>>+<<<<<<<<-]>>>>>[<<<<<+>>>>>-]+>>[->-[>]<<]<[>>[-]<<+>]>>[-]<<<-[-<<<<<<<[>>>>>>>+>+<<<<<<<<-]>>>>>>>>[-<<<<<<<<+>>>>>>>>][>]<[[<]>-[>]<-]<[<]>>-[>]]<[<]>>-[-<[->+<]>[<+[>]>>+<<<[<]>>-]++[>]<<<<<<[>>>>>>+>>>+<<<<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]+>>[->-[>]<<]<[>>[-]<<+>]>>[-]<<<-[-<[<]>+++>-[>]]<[<]>>[-]][-]][-]][-]][-]][-]+<<<]>>[.>-[>[-]<]>]

๐Ÿ’ฃ Fork Bomb

+[>+]

๐Ÿบ Bubble Sort

This program sorts the bytes of its input by bubble sort.

๐Ÿบ Insertion Sort

This program sorts bytes of input using insertion sort.

Working demo with backend brainfuck :)

More examples below,

You can try brainfuck online, Brainfuck-visualizer

Sources

Learn brainfuck, be cool.