GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

Brainfuck

Brainfuck is an esoteric programming language created in 1993 by Swiss student . Designed to be extremely minimalistic, the language consists of only eight simple commands, a data pointer, and an instruction pointer. 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 it provides so little abstraction that the programs get very long or complicated. While Brainfuck is fully Turing-complete, it is not intended for practical use but to challenge and amuse programmers. Brainfuck requires one to break down programs into small and simple instructions. The language takes its name from the slang term brainfuck, which... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

Brainfuck is an esoteric programming language created in 1993 by Swiss student . Designed to be extremely minimalistic, the language consists of only eight simple commands, a data pointer, and an instruction pointer. 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 it provides so little abstraction that the programs get very long or complicated. While Brainfuck is fully Turing-complete, it is not intended for practical use but to challenge and amuse programmers. Brainfuck requires one to break down programs into small and simple instructions. The language takes its name from the slang term brainfuck, which refers to things so complicated or unusual that they exceed the limits of one's understanding, as it was not meant or made for designing actual software but to challenge the boundaries of computer programming. Because the language's name contains profanity, many substitute names are used, such as brainf*ck, brainfsck, branflakes, brainoof, brainfrick, BrainF, brainfreeze, and BF. Brainfuck also has many derivatives and equivalents, including !!Fuck, Binary Brainfuck, 100BF, BaguaFuck, and many others ===Notes=== The much older language P′′ already featured three of these commands, >, [, and ], denoting them R, (, and ) instead, while the fourth P′′ command, denoted λ, combined + and [ … ] can be nested arbitrarily and all brackets must be properly matched, making the set of valid programs a context-free language. Many practical implementations allocate a finite array of cells (commonly at least 30 000 bytes) and use 8‑bit cells with wraparound, although neither the exact cell count nor wraparound behavior is required by all interpreters. In implementations with 8-bit unsigned cells and arithmetic wrapping at 256, one of the + or - instructions is redundant. Incrementing a cell by one is equivalent to decrementing it 255 times in succession, and decrementing by one is equivalent to incrementing it 255 times in succession. Accordingly, either the + instruction may be expressed as -^{255} , or the - instruction as +^{255} .{{Efn| H^{k} \; \rightarrow \; \underbrace{H H \dots H}_{k \text{ times}} defines repeated application of an instruction.}} Brainfuck programs are usually difficult to comprehend. This is partly because any mildly complex task requires a long sequence of commands and partly because the program's text gives no direct indications of the program's state. These, as well as Brainfuck's inefficiency and its limited input/output capabilities, are some of the reasons it is not used for serious programming. Nonetheless, like any Turing-complete language, Brainfuck is theoretically capable of computing any computable function or simulating any other computational model if given access to an unlimited amount of memory and time. A variety of Brainfuck programs have been written. Although Brainfuck programs, especially complicated ones, are difficult to write, it is quite trivial to write an interpreter for Brainfuck in a more typical language such as C due to its simplicity. Brainfuck interpreters written in the Brainfuck language itself also exist. [ This program prints "Hello World!" and a newline to the screen; its length is 106 active command characters. [It is not the shortest.] This loop is an "initial comment loop", a simple way of adding a comment to a BF program such that you don't have to worry about any command characters. Any ".", ",", "+", "-", " " characters are simply ignored, the "[" and "]" characters just have to be balanced. This loop and the commands it contains are ignored because the current cell defaults to a value of 0; the 0 value causes this loop to be skipped. ] ++++++++ Set Cell #0 to 8 [ >++++ Add 4 to Cell #1; this will always set Cell #1 to 4 [ as the cell will be cleared by the loop >++ Add 2 to Cell #2 >+++ Add 3 to Cell #3 >+++ Add 3 to Cell #4 >+ Add 1 to Cell #5 + Add 1 to Cell #2 >+ Add 1 to Cell #3 >- Subtract 1 from Cell #4 >>+ Add 1 to Cell #6 [ >. Cell #2 has value 72 which is 'H' >---. Subtract 3 from Cell #3 to get 101 which is 'e' +++++++..+++. Likewise for 'llo' from Cell #3 >>. Cell #5 is 32 for the space >+. Add 1 to Cell #5 gives us an exclamation point >++. And finally a newline from Cell #6 For readability, this code has been spread across many lines, and blanks and comments have been added. Brainfuck ignores all characters except the eight commands +-<>[],. so no special syntax for comments is needed (as long as the comments do not contain the command characters). The code could just as well have been written as: ++++++++[>++++[>++>+++>+++>+ +>+>->>+[ >.>---.+++++++..+++.>>. >+.>++. Another example of 'Hello World' code is presented here. This version highlights the complexity of the language. For example, after printing 'e', it is necessary to increment cell #3 to switch to the letter 'l'. Displaying the second 'l' requires no further modifications, as cell 3 already contains 'l'. [->+ This can be incorporated into a simple addition program as follows: c0_{\text{final}} = \big((2 \bmod 256) + (5 \bmod 256)\big) \bmod 256 = 7 \bmod 256 = 7 ++ Cell c0 = 2 > +++++ Cell c1 = 5 [ Start your loops with your cell pointer on the loop counter (c1 in our case) - Subtract 1 from c1 ] End your loops with the cell pointer on the loop counter At this point our program has added 5 to 2 leaving 7 in c0 and 0 in c1 but we cannot output this value to the terminal since it is not ASCII encoded To display the ASCII character "7" we must add 48 to the value 7 We use a loop to compute 48 = 6 * 8 ++++ ++++ c1 = 8 and this will be our loop counter again [ - Subtract 1 from c1 ] -,+[ Read first character and start outer character reading loop -[ Skip forward if character is 0 >>++++[>++++++++ +>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward + >+>] Special case: move remainder back to divisor and increase quotient >>[-]+ End skip loop; zero former divisor and reuse space for a flag >--[-[ +++[- ] -[>+>>] Reduce divisor; Normal case: increase remainder >[+[ -]>+>>] Special case: increase remainder / move it back to divisor / increase quotient >[ -] Add remainder back to divisor to get a useful 13 >[ Skip forward if quotient was 0 -[ Decrement quotient and skip forward if quotient was 1 - > Zero quotient and divisor if quotient was 2 ] >-]>> Zero divisor and subtract 13 from copy if quotient was 1 ] >-] Zero divisor and add 13 to copy if quotient was 0 ] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3)

References

  1. Brainfuck. https://en.wikipedia.org/w/index.php?oldid=1378762086 · 1795438144.7002156
source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Brainfuck". 1791244182. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1378762086

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·