Scene 1 First day
LineyHuh? I was just born.And I'm off to work already?
The Life of Code 01
Where does 1 + 2 go?
Someone just wrote one line of code.
But what will happen to it inside the computer?
Let's tag along on Liney's first day at work.
So that's your guess.
Let's see how Snip really cuts it.
LineyHuh? I was just born.And I'm off to work already?
SnipHold on. You can't just walk in.
LineyWhy not? I'm just one little line.
SnipOne line or not, I still need to see what you have inside.
SnipA number, a space, a plus, a space, a number.
LineyAre you taking the spaces along too?
SnipWe'll see. Watch closely which pieces are left.
LineyHuh? I'm five characters long, but only three pieces?
SnipRight. The number of characters and the number of pieces aren't always the same.
LineyTurns out I'm more complicated than I thought.
TwigGot the pieces, thanks.But how am I supposed to put these together?
LineyThere's still more to do?
SnipMy job ends here. Now it's Twig's turn.
Liney's first day at work isn't over yet.
A little experiment
If the spaces change, does it still split the same way?
This time, you pick the expression.
Let's see which pieces Snip cuts out.
Split exactly as Topaz compiler 5.21.1 did
Characters
Pieces
1number+plus sign2number5 characters, 3 pieces
Split exactly as Topaz compiler 5.21.1 did
Characters
Pieces
1number+plus sign2number3 characters, 3 pieces
Split exactly as Topaz compiler 5.21.1 did
Characters
Pieces
1number+plus sign2number9 characters, 3 pieces
Split exactly as Topaz compiler 5.21.1 did
Characters
Pieces
12number+plus sign3number6 characters, 3 pieces
Split exactly as Topaz compiler 5.21.1 did
Characters
Pieces
1number+plus sign2number+plus sign3number9 characters, 5 pieces
First discovery
Compare 1 + 2 with 1+2.
Split exactly as Topaz compiler 5.21.1 did
1 + 25 characters, 3 pieces
1number+plus sign2number1+23 characters, 3 pieces
1number+plus sign2number1 + 29 characters, 3 pieces
1number+plus sign2numberThe number of spaces is different, but the number and plus sign pieces that go on to the next step are the same.
Second discovery
Now pick 12 + 3.
See how 12 didn't split into 1 and 2?
Two digits can make a single piece.
Pieces like the 1, + and 2 that Snip cut out are called tokens.
When a compiler scans the characters of code and picks out pieces like these, it's doing lexical analysis.
A token can carry facts like what kind it is, which characters it came from and where it sat in the source.
Split exactly as Topaz compiler 5.21.1 did
| Piece | Kind | Place in the source |
|---|---|---|
1 | integernumber | cell 1 |
+ | plusplus sign | cell 3 |
2 | integernumber | cell 5 |
The compiler also notes which cells each piece sat in. That way, if something goes wrong, it can point to the exact spot.
Not quite.
In the sum you just saw, adding or removing spaces still gave the same number and plus sign pieces.
But in some code, a single line break can change the meaning.
You'll run into that story again on Liney's journey.
And nobody has even worked out 1 + 2 yet.
Working out how to put the pieces together is the next job, and it's Twig's.
What we found out today
A compiler scans the characters of code and makes pieces called tokens.
So how many pieces is this?
10 + 20Three pieces.
10+2010 and 20 have two characters each, but each one is just one number token.
Now you know a little about Snip's job, right?
Field GuideRevisit these wordsCompilerToken
Next story
Snip cut the pieces just right.
But then Twig found some strange code.
1 + * 2TwigWait. How am I supposed to put this together?
Two symbols sit side by side in this one line. Can Twig put it together?