Reading brainfuck
By the end of this lesson you can read a program somebody else wrote: tell comments from commands, recognise the common pieces, and trace a program by hand.
Comments that bite
This program has a line of explanation above it, and it prints more than the letter H. Press Step a few times and watch which command runs first, then repair the program:
Hint
Press Step and look at the very first command the machine runs.
Hint
The full stops and the comma of the sentence are commands. Take them out.
Notice that the sentence itself ran: its full stops printed and its comma read. The machine skips every character that is not one of the eight symbols, so you can write an explanation anywhere. Such text is called a comment. But a full stop, a comma or a hyphen in a comment is a command, and it runs.
Writers of brainfuck therefore keep these eight out of their comments: . , − + < > [ ]. Some put a comment inside a loop that never runs, at the very start of the program, where the cell is still zero. Inside such a loop anything goes, as long as the brackets match.
Pieces you know
Here are three short pieces of program, each one you have met before. Choose what each of them does:
Notice that you probably did not read [−] as three commands, but as one word: clear
. An experienced reader does the same, and a small stock of such pieces makes up most of every program.
They are called idioms or patterns. Patterns collects them, one page per pattern, each with a machine to step through.
Trace it by hand
Work out on paper, without running anything, what cell 1 holds when the program ++[>+++<−]>+ has ended. Type your answer and press Check. A machine then appears to check it with:
Notice that the machine shows the numbers you could have written down. That is tracing by hand: keep a row of numbers on paper, one per cell, and a mark for the pointer. For a loop, work out one round and count the rounds. Two rounds of three is 6, and the plus after the loop makes 7.
When a program is too long for that, trace it in parts, or let a machine do the stepping while you watch. The Playground has a stepper for programs of any size.
A stranger’s program
Here is a program without a word of explanation, with a box for its input. Try a few short inputs and watch the tape, then say what it does:
Notice that the input came out backwards. >,[>,] lays the input out along the tape, one character per cell, and leaves cell 0 empty. Then <[.<] walks back, printing, until it meets that empty cell.
You found that out the way everybody does: by feeding the program small inputs and watching. Reading and trying go together.
Check yourself
A free exercise
Take a program you wrote in Lesson 9, give every part its own line and a comment, and check that it still does the same. Nothing is checked here:
For when you need them
Four places that help once you write programs of your own:
- PlaygroundAn editor and a machine for programs of any size, to write and step through your own.
- PatternsThe pieces that come back in every program, one page each: clear, move, copy, if, compare, divide, print a number.
- ChallengesTasks from two commands to a few hundred, checked for you case by case, with hints, a worked solution after you solve one, and a score to beat if you like that.
- ReferenceThe exact rules, the character table, and how brainfuck machines differ from each other.
What you can do now
You can tell comments from commands, recognise clear, move and copy at a glance, and trace a short program on paper. These are the pieces to recognise:
| Piece | What it does |
|---|---|
[−] | Clear: empty the cell. |
[−>+<] | Move: add the cell to the cell on its right, and empty it. |
,[.,] | Copy the input to the output. |