Implementations of different programming languages use various techniques to execute the code. Let’s figure out the difference between them.
A compiler is a program that translates a high-level language to bytecode or machine code.
At the same time, a compiler itself doesn’t execute the resulting code. The user has to do it instead.
Examples of compilers: GCC, javac, rustc
A transpiler is similar to a compiler, but it translates source code to another high-level language instead of bytecode/machine code.
A transpiler doesn’t execute the resulting code too.
Examples of transpilers: babel, tsc
An interpreter, in turn, takes in the source code and executes it immediately. In other words, it runs it from the source.
Examples of interpreters: Ruby MRI (CRuby), PHP3
There are a lot of programming languages implementations that use compiling and interpreting techniques together. Robert Nystrom captured such implementations for his “Crafting Interpreters” book.
This combination allows getting the best from both worlds.
The execution will be faster because it’s generally easier to optimize bytecode. At the same time, it’s more convenient for the user to run source code with a single command.
For example, CPython works like this.
P.S. Follow me on Twitter for more content like this.
Today I found a very cool blog series. Its author creates a small operation system using Rust. The series is full of valuable insights into how OS works and how to implement one in Rust!
15:59 PM – 22 Feb 2022