Brainfuck!

Brainfuck!

What Is It?

An Esoteric, Turing-Complete

Programming Language

An Esoteric, Turing-Complete

Programming Language

"...designed to test the boundaries of computer programming language design, as a proof of concept, as software art, or as a joke."

"Theoretically capable of computing any computable function or simulating any other computational model, if given access to an unlimited amount of memory."

An Esoteric, Turing-Complete

Programming Language

Why Is It Called Brainfuck?


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

The 8 Language Commands


irb > array = (0..Float::INFINITY).lazy.collect { |x| 0 }
 => #<Enumerator::Lazy: #<Enumerator::Lazy: 0..Infinity>:collect>

irb > _.first 10
 => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

irb > pointer = _[0]
 => 0

(Program Start)

(translated into Ruby)

#     _
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

irb > pointer += 1
=> 1

>

The 8 Language Commands

<

The 8 Language Commands

#  _
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

irb > pointer -= 1
=> 0

+

The 8 Language Commands

#  _  
# [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

irb > array[pointer] += 1
=> 1

-

The 8 Language Commands

#  _  
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

irb > array[pointer] -= 1
=> 0
#       __
# [..., 65, 0, 23, ...]

irb > puts pointer.chr
=> "A"

.

The 8 Language Commands


 irb > gets[0].ord
 Dan
 => 68

,

The 8 Language Commands

irb > pointer = 10
irb > while pointer != 0 do
irb >   # pointer += 1          # [10, 0, ...]
irb >   # array[pointer] += 1   # [10, 1, ...]
irb >   # pointer -= 1          # [10, 1, ...]
irb >   # array[pointer] -= 1   # [9, 1, ...]
irb >   # and so forth...       # [0, 10, ...] 
                                # the values are 
                                # copied across

[

The 8 Language Commands


irb >     ...
irb > end

]

The 8 Language Commands

What Can You Do With It?


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

> brainfuck -e '
+++++++++++[>++++++<-]>.<+++++++++++
[>>++++++++++<<-]>>++++.<<++++++++++
+[>>>+++++++++<<<-]>>>--.++++++++.++
+++.--------.<+++.>---.++++++++.<<<+
++++++++++++++++++++++++++++++++.'

Brainfuck!%

> brainfuck -e '
+++++++++++[>++++++<-]>.<+++++++++++
[>>++++++++++<<-]>>++++.<<++++++++++
+[>>>+++++++++<<<-]>>>--.++++++++.++
+++.--------.<+++.>---.++++++++.<<<+
++++++++++++++++++++++++++++++++.'

Brainfuck!%

# p0:0/11 p1:0
+++++++++++

# while p0 > 0 
[

  # p1:/6 => p0:11 p1:6
  >++++++

  # p0:\1 => p0:10 p1:6
  <-

# and so forth until p0:0 and p1:66
]

# go to p1:66 
>

# print 'B'
.
# go to p0 and increment by 11
<+++++++++++
[
  # go to p2 and increment by 10, decrement p0
  >>++++++++++<<-
  # until p2:110 and p0:0
]
# go to p2 and increment by 4 
>>++++ 
# print 'r'
.

<<+++++++++++[>>>+++++++++<<<-]>>>--. # p0:11\0 p3:11/97 a
++++++++.                             # p3:97/105        i
+++++.                                # p3:105/110       n
--------.                             # p3:110\102       f
<+++.                                 # p2:114/117       u
>---.                                 # p3:102\99        c
++++++++.                             # p3:99/107        k
<<<+++++++++++++++++++++++++++++++++. # p0:0/33          !

Brainfuck!%
[-]           # p0:0
 >            # p1
 [            # enter loop if p1:1
  >           # p2
  [           # enter loop if p2:1
   <<+>>      # p0:1
   [-]        # empty p2
  ]
  <           # empty p1
  [-]
 ]
 <            # p0

=> p0:1 only if p1:!0 AND p2:!0, else p0:0

Logical AND

 [-]            # p0:0
 >              # p1
 [>>+<<[-]]     # p3:/1 if p1:!0
 >              # p2
 [>+<[-]]       # p3:/1 if p2:!0
 >              # p3
 [<<<+>>>[-]]   # p0:1 if p3:!0

 => p0:1 only if p1:!0 OR p2:!0, else p0:0. 
    The last line is necessary to make sure 
    p0:1 and not p0:2 (if both inputs are true).

Logical OR

 [-]           # p0:0
 >>>[-]<<      # p3:0 p1
 [>>+<<[-]]    # p3:/1
 >             # p2
 [>+<[-]]      # p3:/1
 >-            # p3:\1 p3:0 if XOR is true
 <<<+>>>       # p0:1 p3
 [<<<->>>[-]]  # p0:\1
 <<<           # p0

 => p0:1 only if p1:!0 XOR p2:!0, else p0:0

Logical XOR

 [-]+         # p0:1
 >[<->[-]]<   # p0:0 if p1:!0

 => p0:1 only if p1:!0

Logical NOT

[                   # enter loop if p1 has non-zero value
 >++++++++          # p1:8
  [>+++++++++<-]    # p2:72
 >.                 # display p2
 <<                 # p0
 [-]                # p0:0 so that the loop only gets executed once
]

=> only IF p0:!0 will the loop get executed.

If Then

,                   # read input
----------------    # p0:\64 you can change this value to anything
----------------    #        you want; 64 is the value you compare
----------------    #        p0 with
----------------           
>[-]+<              # p1:1 p0
[                   # enter loop if p0:!0
 >                  # p1
  [-]               # p1:0
 <                  # p0
 [-]                # p0:0 so you can exit the loop
]
>                   # p1
[                   # enter loop if p0:0 (64 was entered)
 >++++++++          # p2:8
 [>++++++++<-]      # p4:64
 >.                 # display '@'
 <<                 # p1
 [-]                # p1:0 so the loop gets executed only once
]

If Then

,                   # read input
----------------
----------------    # p0:\64
----------------
----------------
[                   # enter loop if p0:!64
 >++++++++          # p1:8
 [>++++++++<-]      # p2:64
 >.                 # display '@'
 <<                 # p0
 [-]                # p0:0 so you can exit the loop
]

If Not

,                       # read input
----------------
----------------        # p0:\64
----------------
----------------
>>>+<<<                 # p3:1 since p0..p2 are used
[                       # enter loop if p0:!64          \
 >+++++++++++++         # p1:13                         |
 [>++++++<-]            # p2:78                         |
 >.                     # display 'N'                   | ELSE part
 >-                     # p3:0                          |
 <<<                    # p0                            |
 [-]                    # p0:0 so you can exit the loop /
]
>>>                     # p3
[                       # enter loop if p3:1 and p0:64  \
 <<                     # p1                            |
 ++++++++               # p1:8                          |
 [>++++++++<-]          # p2:64                         | IF part
 >.                     # display '@'                   |
 >                      # p3                            |
 [-]                    # p3:0 so you can exit the loop /
]

If Then Else

                 
<<<                 # rewind, p0:0 or 1
+++++++++
+++++++++
+++++++++
+++++++++
+++++++++
+++++++++.          # p0:/48 to print ASCII 0 or 1

Print The Value

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

Enter input: 3
3
3's value is odd.% 

Enter input: 2
2
2's value is even.% 
REALLY COOOOOOOOL

References

Slides

Questions?

Brainfuck!

By DRJ

Brainfuck!

A lightning talk on the brainfuck language

  • 296