Created
April 30, 2015 20:59
-
-
Save robbielynch/e611442ca2d056f3b78f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<img src=\"http://robl.co/wp-content/uploads/2014/09/apple-galaxy-wallpaper.jpg\" />" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Welcome to IBrainfuck - The Brainfuck Language Kernel for IPython\n", | |
"\n", | |
"Hi, I'm [Robbie](https://twitter.com/lynchrobbie). I've created a brainfuck language kernel for IPython using IPython's [Python Wrapper Kernel Example](https://ipython.org/ipython-doc/dev/development/wrapperkernels.html).\n", | |
"\n", | |
"### Why?\n", | |
"I have an interest in IPython, I created the [Erlang Language Kernel - IErlang](https://github.com/robbielynch/ierlang) about a year ago and I've also [blogged about brainfuck](http://robl.co/brief-look-at-brainfuck/) in the past, and obviously love it. As a true example/demo of IBrainfuck, I've decided to port that blog post to an IBrainfuck Notebook for your viewing pleasure.\n", | |
"\n", | |
"---" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# A Brief Look at brainfuck\n", | |
"\n", | |
"I know. You’re thinking – “This guy’s got a mouth on him”. Well, in this case, I do. I’ve been looking into the brainfuck programming language, and I said I’d like to give it a go.\n", | |
"\n", | |
"## This is My Experience of Using brainfuck for 30 Minutes\n", | |
"\n", | |
"### A Quick Introduction\n", | |
"According to wikipedia:\n", | |
"\n", | |
">“Brainfuck is an esoteric programming language noted for its extreme minimalism. The language consists of only eight simple commands and an instruction pointer. It is designed to challenge and amuse programmers, and was not made to be suitable for practical use. It was created in 1993 by Urban Müller.”" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 3 Reasons to Learn brainfuck\n", | |
"\n", | |
"- You’re bored\n", | |
"- You want to show off to your mates\n", | |
"- You want hot girls to throw themselves at you\n", | |
"\n", | |
"### Commands in brainfuck\n", | |
"\n", | |
"There is a total of **8 commands** in brainfuck. Each command consists of a single character and every character that is not a command is ignored by the interpreter.\n", | |
"\n", | |
">Brainfuck is represented by an **array with 30,000 cells** initialized to zero and a data pointer pointing at the current cell (Starting at cell 0).\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The 8 commands are:\n", | |
"\n", | |
"|Command |\tExplanation|\n", | |
"|--------|-------------|\n", | |
"|+|\tIncrements the value at the current cell by one.|\n", | |
"|-|\tDecrements the value at the current cell by one.|\n", | |
"|>|\tMoves the data pointer to the next cell (cell on the right).|\n", | |
"|<|\tMoves the data pointer to the previous cell (cell on the left).|\n", | |
"|.|\tPrints the ASCII value at the current cell (i.e. 65 = ‘A’).|\n", | |
"|,|\tReads a single input character into the current cell.|\n", | |
"|[|\tIf the value at the current cell is zero, skips to the corresponding ] . Otherwise, move to the next instruction.|\n", | |
"|]|\tIf the value at the current cell is zero, move to the next instruction. Otherwise, move backwards in the instructions to the corresponding [ |" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"---\n", | |
"\n", | |
"## My First Ever brainfuck Program\n", | |
"\n", | |
"This took me 15 to 20 minutes to create, which includes the time I spent learning the language. To execute it, I am using an online brainfuck interpreter, which is written in javascript and it works pretty well. Check it out HERE My first program is pretty simple, it just prints the word **“ROBBIE”** to the screen. Here it is:\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"ROBBIE" | |
] | |
} | |
], | |
"source": [ | |
"++++++++[>++++++++++<-]>++.>++++++++[>++++++++++<-]>-.>++++++\n", | |
"[>++++++++++<-]>++++++.>++++++[>++++++++++<-]>++++++.[>+<-]>+++++++.[>+<-]>----." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Explanation\n", | |
"\n", | |
"To explain what’s going on, I’ll break the program down and explain every single command to help you understand what exactly is going on. Also, remember **that any character that is not a command is ignored by the interpreter** i.e. every alpha-numeric character and white space.\n", | |
"\n", | |
">Tip: If you have a pencil and paper, it might be useful to draw an array to keep track of what’s in each cell.\n", | |
"\n", | |
"How the original code prints each letter:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"ROBBIE" | |
] | |
} | |
], | |
"source": [ | |
"Print R\n", | |
"++++++++\n", | |
"[ > ++++++++++ < - ]\n", | |
"> ++ .\n", | |
" \n", | |
"Print O\n", | |
"> ++++++++ \n", | |
"[ > ++++++++++ < - ] \n", | |
"> - . \n", | |
" \n", | |
"Print BB\n", | |
"> ++++++ [ > ++++++++++ < - ] > ++++++ .\n", | |
"> ++++++ [ > ++++++++++ < - ] > ++++++ .\n", | |
" \n", | |
"Print I\n", | |
"[ > + < - ] > +++++++ .\n", | |
" \n", | |
"Print E\n", | |
"[ > + < - ] > ---- ." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Printing the letter 'R'\n", | |
"\n", | |
"We want to print R, that means we’ve got to set a cell in the array to 82 (the ASCII number for R). Sounds easy, right? You could simply insert `82 + commands` and output it using the `. command`\n", | |
"\n", | |
"It’s correct, but there’s a cleaner and a more clever way to do it using loops.\n", | |
"\n", | |
"We are going to use cell 0 as the loop counter. When the loop counter gets to 0, the loop will end and we’ll move on to the next instruction after the ] command.\n", | |
"\n", | |
"I’ll give you the algorithm for printing the letter R using loops:\n", | |
"\n", | |
"- `Initialise cell 0 to 8`\n", | |
"- `While cell 0 != 0`\n", | |
"- `move to the next cell (cell 1)`\n", | |
"- `increment cell 1 by 10`\n", | |
"- `move back to the previous cell (cell 0)`\n", | |
"- `decrement cell 0 by 1`\n", | |
"\n", | |
"##### Code to print 'R'\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"R" | |
] | |
} | |
], | |
"source": [ | |
"++++++++ [ > ++++++++++ < - ] > ++ ." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Command by command instructions:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"R" | |
] | |
} | |
], | |
"source": [ | |
"++++++++ initialise cell 0 to 8\n", | |
"[ Start loop if current cell value is not equal to 0\n", | |
" > move to cell 1\n", | |
" ++++++++++ increment cell 1 by 10\n", | |
" < move back to the cell 0\n", | |
" - decrement cell 0 by 1\n", | |
"] if the current value of cell 0 is 0 move to next instruction: otherwise go back to the opening square bracket\n", | |
"> move to cell 1\n", | |
"++ increment cell 1 by 2: making the final value of cell 1 equal to 82\n", | |
". print the ASCII value of cell 1 (R)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#####Printing the Letters 'OBB'\n", | |
"\n", | |
"This is very similar to Printing R. If you understand that, then you should have no problem understanding how to print OBB.\n", | |
"\n", | |
"##### Printing I\n", | |
"\n", | |
"This is a different approach to printing a letter. Here, we will use the value of the previously assigned cell (the letter B in this case).\n", | |
"\n", | |
"We are going to move the value from cell 3 (66 i.e. the letter B ) into cell 4, and then manipulate the value of cell 4 to make it equal to 73 (i.e. the letter I).\n", | |
"\n", | |
"To do this we need to move 66 (B) to the next cell and add 7 to turn it into an I.\n", | |
"\n", | |
"The code to move the current cell into the next cell:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\b" | |
] | |
} | |
], | |
"source": [ | |
"[ > + < - ] > +++++++ . //This code has no context so it won't execute here" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Code explained command by command: (Again, the code won't work here because it's only a snippet)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\b" | |
] | |
} | |
], | |
"source": [ | |
"[ Start loop if current cell (cell 3) value is not equal to 0\n", | |
" > move to next cell (cell 4)\n", | |
" + increment cell 4 by 1\n", | |
" < go back to cell 3\n", | |
" - decrement cell 3 by 1\n", | |
"] if the current value of cell 3 is 0 move to next instruction: otherwise go back to the opening square bracket\n", | |
"> move to next cell (cell 4)\n", | |
"+++++++ increment cell 4 by 7\n", | |
". print cell 4 (I)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"##### Printing the Letter E\n", | |
"\n", | |
"You’ve just printed the letter I. Printing E uses the same concept as printing I (moving the value (I) to the next cell and manipulating it to change it to an E)\n", | |
"\n", | |
"And now you're finished!!! :)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"--- \n", | |
"\n", | |
"## Basic Algorithms\n", | |
"\n", | |
"Now that you’ve conquered the basics of looping, here is a list of useful brainfuck algorithms:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\u0001" | |
] | |
} | |
], | |
"source": [ | |
"Clearing a Value: {x} → {0} \n", | |
"[-]\n", | |
" \n", | |
"Moving a Value: {x, 0} → {0, x} (We used this algorithm earlier)\n", | |
"[->+<]\n", | |
" \n", | |
"Copying a Value: {x, 0, 0} → {x, x, 0} \n", | |
"[->+>+<<]>>[-<<+>>]<<\n", | |
" \n", | |
"Addition: {x, y} → {0, x+y} \n", | |
"[->+<]\n", | |
" \n", | |
"Subtraction: {x, y} → {x-y, 0} \n", | |
">[-<->]<\n", | |
" \n", | |
"Multiplication: {x, y, 0, 0} → {0, y, x*y, 0} \n", | |
"[->[->+>+<<]>>[-<<+>>]<<<]\n", | |
" \n", | |
"Exponentiation: {x, y, 0, 0, 0} → {x, 0, pow(x, y), 0, 0} \n", | |
">>+<[->[-<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<]>[-<+>]<<]<\n", | |
" \n", | |
"Division: {x, y, 0, 0, 0, 0} → {x/y, x%y, 0, 0, 0, 0} \n", | |
"[>[->+>+<<]>[-<<-[>]>>>[<[-<->]<[>]>>[[-]>>+<]>-<]<<]>>>+<<[-<<+>>]<<<] \n", | |
">>>>>[-<<<<<+>>>>>]<<<<< \n", | |
"Warning: When y=0, an infinite loop will result." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"There you have it. You’ve created your first brainfuck program, learned about loops and moving a value from one cell to the next, and you’ve got a list of the basic algorithms that you can play around with.\n", | |
"\n", | |
"Created by: [Robbie Lynch](http://robl.co)\n", | |
"\n", | |
"Twitter: [Robbie Lynch](https://twitter.com/lynchrobbie)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Brainfuck", | |
"language": "", | |
"name": "brainfuck" | |
}, | |
"language_info": { | |
"mimetype": "text/plain", | |
"name": "brainfuck" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment