From ab4633f6a413d226e270badad6b9405805ecf147 Mon Sep 17 00:00:00 2001 From: Prajit Ramachandran Date: Sat, 31 Aug 2013 18:38:33 -0700 Subject: Tutorial for the Brainfuck programming language. Brainfuck is a minimal programming language that is rather difficult to wrap one's head around. It only uses the characters +-[],.>< --- brainfuck.html.markdown | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 brainfuck.html.markdown (limited to 'brainfuck.html.markdown') diff --git a/brainfuck.html.markdown b/brainfuck.html.markdown new file mode 100644 index 00000000..2b7ce4db --- /dev/null +++ b/brainfuck.html.markdown @@ -0,0 +1,79 @@ +--- +language: brainfuck +contributors: + - ["Prajit Ramachandran", "http://prajitr.github.io"] +--- + +Brainfuck is an extremely minimal programming language (just 8 commands) and +is Turing complete. + +``` +Any character not "><+-.,[]" (excluding quotation marks) is ignored. + +Brainfuck is represented by an array with 30,000 cells initialized to zero +and a data pointer pointing at the current cell. + +There are eight commands: ++ : Increments the value at the current cell by one. +- : Decrements the value at the current cell by one. +> : Moves the data pointer to the next cell (cell on the right). +< : Moves the data pointer to the previous cell (cell on the left). +. : Prints the ASCII value at the current cell (i.e. 65 = 'A'). +, : Reads a single input character into the current cell. +[ : If the value at the current cell is zero, skips to the corresponding ] . + Otherwise, move to the next instruction. +] : If the value at the current cell is zero, move to the next instruction. + Otherwise, move backwards in the instructions to the corresponding [ . + +[ and ] form a while loop. Obviously, they must be balanced. + +Let's look at some basic Brainfuck programs. + +++++++ [ > ++++++++++ < - ] > +++++ . + +This program prints out the letter 'A'. First, it increments cell #1 to 6. +Cell #1 will be used for looping. Then, it enters the loop ([) and moves +to cell #2. It increments cell #2 10 times, moves back to cell #1, and +decrements cell #1. This loop happens 6 times (it takes 6 decrements for +cell #1 to reach 0, at which point it skips to the corresponding ] and +continues on). + +At this point, we're on cell #1, which has a value of 0, while cell #2 has a +value of 60. We move on cell #2, increment 5 times, for a value of 65, and then +print cell #2's value. 65 is 'A' in ASCII, so 'A' is printed to the terminal. + + +, [ > + < - ] > . + +This program reads a character from the user input, copies the character into +another cell, and prints out the same character. + +, reads in a character from the user into cell #1. Then we start a loop. Move +to cell #2, increment the value at cell #2, move back to cell #1, and decrement +the value at cell #1. This continues on until cell #1 is 0, and cell #2 holds +cell #1's old value. Because we're on cell #1 at the end of the loop, move to +cell #2, and then print out the value in ASCII. + +Also keep in mind that the spaces are purely for readibility purposes. You +could just as easily write it as + +,[>+<-]>. + + +Try and figure out what this program does: + +,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >> + +This program takes two numbers for input, and multiplies them. + +The gist is it first reads in two inputs. Then it starts the outer loop, +conditioned on cell #1. Then it moves to cell #2, and starts the inner +loop conditioned on cell #2, incrementing cell #3. However, there comes a +problem: at the end of the inner loop, cell #2 is zero. To solve this problem, +we also increment cell #4, and then recopy cell #4 into cell #2. +``` + +And that's Brainfuck. Not that hard, eh? For fun, you can write your own +Brainfuck programs, or you can write a Brainfuck interpreter in another +language. The interpreter is fairly simple to implement, but if you're a +masochist, trying writing a Brainfuck interpreter... in Brainfuck. -- cgit v1.2.3 From d3ccd75c999d68a68fe82a61d1cb871b4d99a6e3 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Wed, 4 Sep 2013 10:24:15 +0200 Subject: brainfuck: A few corrections --- brainfuck.html.markdown | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'brainfuck.html.markdown') diff --git a/brainfuck.html.markdown b/brainfuck.html.markdown index 2b7ce4db..9282381f 100644 --- a/brainfuck.html.markdown +++ b/brainfuck.html.markdown @@ -1,11 +1,12 @@ --- language: brainfuck contributors: - - ["Prajit Ramachandran", "http://prajitr.github.io"] + - ["Prajit Ramachandran", "http://prajitr.github.io/"] + - ["Mathias Bynens", "http://mathiasbynens.be/"] --- -Brainfuck is an extremely minimal programming language (just 8 commands) and -is Turing complete. +Brainfuck (not capitalized except at the start of a sentence) is an extremely +minimal Turing-complete programming language with just 8 commands. ``` Any character not "><+-.,[]" (excluding quotation marks) is ignored. @@ -27,7 +28,7 @@ There are eight commands: [ and ] form a while loop. Obviously, they must be balanced. -Let's look at some basic Brainfuck programs. +Let's look at some basic brainfuck programs. ++++++ [ > ++++++++++ < - ] > +++++ . @@ -45,21 +46,18 @@ print cell #2's value. 65 is 'A' in ASCII, so 'A' is printed to the terminal. , [ > + < - ] > . -This program reads a character from the user input, copies the character into -another cell, and prints out the same character. - -, reads in a character from the user into cell #1. Then we start a loop. Move -to cell #2, increment the value at cell #2, move back to cell #1, and decrement -the value at cell #1. This continues on until cell #1 is 0, and cell #2 holds -cell #1's old value. Because we're on cell #1 at the end of the loop, move to -cell #2, and then print out the value in ASCII. +This program reads a character from the user input and copies the character into +cell #1. Then we start a loop. Move to cell #2, increment the value at cell #2, +move back to cell #1, and decrement the value at cell #1. This continues on +until cell #1 is 0, and cell #2 holds cell #1's old value. Because we're on +cell #1 at the end of the loop, move to cell #2, and then print out the value +in ASCII. Also keep in mind that the spaces are purely for readibility purposes. You -could just as easily write it as +could just as easily write it as: ,[>+<-]>. - Try and figure out what this program does: ,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >> @@ -73,7 +71,7 @@ problem: at the end of the inner loop, cell #2 is zero. To solve this problem, we also increment cell #4, and then recopy cell #4 into cell #2. ``` -And that's Brainfuck. Not that hard, eh? For fun, you can write your own -Brainfuck programs, or you can write a Brainfuck interpreter in another +And that's brainfuck. Not that hard, eh? For fun, you can write your own +brainfuck programs, or you can write a brainfuck interpreter in another language. The interpreter is fairly simple to implement, but if you're a -masochist, trying writing a Brainfuck interpreter... in Brainfuck. +masochist, try writing a brainfuck interpreter… in brainfuck. -- cgit v1.2.3 From d3c5a723999b57774169e5cce28bebd3fc943cea Mon Sep 17 00:00:00 2001 From: JongChan Choi Date: Sat, 21 Sep 2013 04:17:53 +0900 Subject: fix typo --- brainfuck.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'brainfuck.html.markdown') diff --git a/brainfuck.html.markdown b/brainfuck.html.markdown index 9282381f..9cc7a937 100644 --- a/brainfuck.html.markdown +++ b/brainfuck.html.markdown @@ -53,25 +53,25 @@ until cell #1 is 0, and cell #2 holds cell #1's old value. Because we're on cell #1 at the end of the loop, move to cell #2, and then print out the value in ASCII. -Also keep in mind that the spaces are purely for readibility purposes. You +Also keep in mind that the spaces are purely for readability purposes. You could just as easily write it as: ,[>+<-]>. Try and figure out what this program does: -,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >> +,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >> This program takes two numbers for input, and multiplies them. The gist is it first reads in two inputs. Then it starts the outer loop, conditioned on cell #1. Then it moves to cell #2, and starts the inner -loop conditioned on cell #2, incrementing cell #3. However, there comes a +loop conditioned on cell #2, incrementing cell #3. However, there comes a problem: at the end of the inner loop, cell #2 is zero. To solve this problem, we also increment cell #4, and then recopy cell #4 into cell #2. ``` -And that's brainfuck. Not that hard, eh? For fun, you can write your own -brainfuck programs, or you can write a brainfuck interpreter in another +And that's brainfuck. Not that hard, eh? For fun, you can write your own +brainfuck programs, or you can write a brainfuck interpreter in another language. The interpreter is fairly simple to implement, but if you're a masochist, try writing a brainfuck interpreter… in brainfuck. -- cgit v1.2.3 From 6eb210258d6c21a6634e1a459cd061a404ca78ba Mon Sep 17 00:00:00 2001 From: JongChan Choi Date: Sat, 21 Sep 2013 05:01:47 +0900 Subject: make explanation richer --- brainfuck.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'brainfuck.html.markdown') diff --git a/brainfuck.html.markdown b/brainfuck.html.markdown index 9cc7a937..27ac6921 100644 --- a/brainfuck.html.markdown +++ b/brainfuck.html.markdown @@ -67,8 +67,10 @@ This program takes two numbers for input, and multiplies them. The gist is it first reads in two inputs. Then it starts the outer loop, conditioned on cell #1. Then it moves to cell #2, and starts the inner loop conditioned on cell #2, incrementing cell #3. However, there comes a -problem: at the end of the inner loop, cell #2 is zero. To solve this problem, +problem: At the end of the inner loop, cell #2 is zero. In that case, +inner loop won't work anymore since next time. To solve this problem, we also increment cell #4, and then recopy cell #4 into cell #2. +Then cell #3 is the result. ``` And that's brainfuck. Not that hard, eh? For fun, you can write your own -- cgit v1.2.3 From 20d612ce5c8e1fe8ea23c15ff937142ef53f5034 Mon Sep 17 00:00:00 2001 From: Fatih Erikli Date: Tue, 24 Mar 2015 11:11:14 +0200 Subject: Add brainfuck-visualizer link --- brainfuck.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'brainfuck.html.markdown') diff --git a/brainfuck.html.markdown b/brainfuck.html.markdown index 27ac6921..aa1fcc40 100644 --- a/brainfuck.html.markdown +++ b/brainfuck.html.markdown @@ -8,6 +8,8 @@ contributors: Brainfuck (not capitalized except at the start of a sentence) is an extremely minimal Turing-complete programming language with just 8 commands. +You can try brainfuck on your browser with brainfuck-visualizer. + ``` Any character not "><+-.,[]" (excluding quotation marks) is ignored. -- cgit v1.2.3 From c7a9731b07071c2380a5383ab976592df970aa50 Mon Sep 17 00:00:00 2001 From: Fatih Erikli Date: Tue, 24 Mar 2015 22:24:49 +0200 Subject: Change link format --- brainfuck.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'brainfuck.html.markdown') diff --git a/brainfuck.html.markdown b/brainfuck.html.markdown index aa1fcc40..a76169c8 100644 --- a/brainfuck.html.markdown +++ b/brainfuck.html.markdown @@ -8,7 +8,7 @@ contributors: Brainfuck (not capitalized except at the start of a sentence) is an extremely minimal Turing-complete programming language with just 8 commands. -You can try brainfuck on your browser with brainfuck-visualizer. +You can try brainfuck on your browser with [brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/). ``` Any character not "><+-.,[]" (excluding quotation marks) is ignored. -- cgit v1.2.3