Skip to content

Translating As You Go

Read a line, work out what it means, do it. Then do all of that again the next time the same line comes round.

The first kind of translator is called an interpreter, and it works the way a person would if you handed them a recipe in a language they half knew.

It reads one line. It works out what that line means. It does it. Then it reads the next line and starts again.

The important part is what it does not do. It does not remember having understood the line. The next time that same line comes round, it works the meaning out again from scratch.

Inside a loop

Most of the time that costs nothing you would notice. Then you write a loop:

LET total = 0
FOR i = 1 TO 1000
  LET total = total + 1
END
SHOW total
Output
1000

The line in the middle runs a thousand times. An interpreter does not translate it once and run the translation a thousand times. It translates it a thousand times, because each time round it meets the line fresh.

PassRead the lineWork out what it meansDo ittotal
1LET total = total + 1put total plus one in total0 + 11
2LET total = total + 1put total plus one in total1 + 12
3LET total = total + 1put total plus one in total2 + 13
1000LET total = total + 1put total plus one in total999 + 11000

Look at the middle column. It says the same thing on every row, and the interpreter did that work on every row. Only the last two columns needed doing a thousand times.

Count what happened. A thousand additions, which is the work you asked for. And a thousand translations, which is work you did not ask for and did not know you were paying for.

That translating sits inside the loop whether you wanted it there or not. Nothing in your program mentions it. It is the price of having a translator work as it goes.

Why a game notices

A game has a loop at its heart, and that loop runs every time the screen is drawn: fifty or sixty times a second. Everything inside it gets translated fifty or sixty times a second.

The enemy that decides where to move lives inside that loop. So does the check for whether the player has been hit, and the code that draws every object. An interpreter works all of it out afresh, every frame, before it can do any of it.

On a small machine that is the whole budget. There is a fixed amount of time between one frame and the next, and the translating spends some of it before the game has done anything at all.

Why anyone would build it this way

Because it is kind.

You type a line and it runs. You make a mistake and the interpreter, which is reading your line right now, can tell you where. You can stop, change one line, and carry on. Nothing has to be rebuilt.

That is exactly the experience the BASIC in these machines gave you, and it is why a whole generation learned to program on it. The interpreter is the right tool for finding out what you want to write. It is only the wrong tool once you know, and need it to be fast.

When it’s wrong, see why

  • The loop is slow and the work inside it is tiny. The work is not the cost. The translating is, and it happens every pass however little the line does.
  • You made the loop run fewer times and it got faster. That is the same fact from the other side. Fewer passes, fewer translations.
  • You cannot see where the time goes. You never will by reading the program. The translating is not written down anywhere in it.

What you’ve learnt

  • An interpreter reads a line, works out its meaning, does it, and keeps no memory of having understood it.
  • Inside a loop it translates the same line every pass, which is work you did not ask for.
  • A game loop runs every frame, so everything in it is translated every frame.
  • Interpreters are patient, which is why beginners start on them, and why games do not stay on them.

What’s next

In Unit 3 the translating moves out of the loop entirely. The whole program is turned into numbers once, before it runs, and you build the translator that does it.