Interpreter

An interpreter is a program that reads source code and runs it directly, one statement at a time.

A computer program is written in a language people can read, but a computer only understands binary. Something has to translate between the two. An interpreter is one kind of translator: instead of converting the whole program ahead of time, it works through the code as it runs, reading each statement, translating it, and carrying it out before moving to the next.

How an Interpreter Runs Code#

An interpreter takes the source code and executes it line by line. There is no separate build step and no standalone file produced at the end. The interpreter itself must be present every time the program runs, because it is the interpreter, not the code, that talks to the machine.

source code  →  interpreter  →  runs each statement in turn

This is why an interpreted program can start running immediately after it is written or changed. There is nothing to build first.

Interpreter or Compiler#

An interpreter is one of two common kinds of code translator. The other is a compiler, which translates the whole program ahead of time into a separate file, before any of it runs.

The trade-off is speed against immediacy. A compiler does its work up front, so the finished program tends to run faster, but it must be rebuilt after every change. An interpreter skips the build step, so code can be changed and run straight away, at the cost of translating again on every run.

Languages are often described as “interpreted” or “compiled”, but this describes how a language is usually run, not a fixed rule. Some languages use a mix of both.

⚠️ Mistakes

Thinking the interpreter is the program

The interpreter is not the code, and it is not part of the program being run. It is a separate tool that must be installed on the machine. Without it, the source code is just text the computer cannot act on.

Expecting an interpreted program to run without the interpreter

A compiled program produces a standalone file that can run on its own. An interpreted program does not. It needs the interpreter present every time it runs, because the interpreter is what actually carries out the code.

📚 Resources