Form3

Engineering · Blogs

.tech Podcast - How do interpreters work?

Join host Kevin Holditch for a podcast episode on how interpreters work. Thorsten Ball, author of the fantastic book - Writing An Interpreter In Go - lifts the bonnet on what happens inside the interpreter.

December 15, 20213 min read

To most of us interpreters feel quite magical in that they can take a string and execute that as meaningful code. Thorsten Ball, author of the fantastic book: Writing An Interpreter In Go lifts the bonnet on what happens inside the interpreter on the .tech podcast hosted by Form3’s Kevin Holditch.

Thorsten goes through the stages of interpreting and adds concrete examples of how each stage fits into the next.

Ep 22 .tech - How do interpreters work?

Listen on Form3’s .tech podcast.

LISTEN TO EPISODE

Whistle stop tour

Programming languages can be implemented in one of two ways: by building a compiler or by building an interpreter. An interpreter reads in your program and then executes it by telling the computer/CPU what to do. Examples of interpreted languages are Ruby, Python or Javascript.

Both compilers and intrepreters give you more expressiveness: write less code to do more! Most of us are used to writing in high level languages such as Go, C# etc. More expresiveness and abstraction give you more building blocks to build bigger programs.

The stages of an interpreter are:

  • The lexing/scanner stage: read the input code as a string and split it up into meaningful tokens which are keywords, values and variable names. This creates the token array.
  • The parsing stage: takes in the token array or stream and creates an abstract syntax tree(AST), which is a graph that organises the tokens in a meaningful way. The syntax tree represents conditions and their tokens on their correct tree branches. The parser uses recursion to figure out token precedence.
  • The evaluation stage: this stage takes the syntax tree and executes it, combining the nodes and branches of the AST using their operators and recursion. Easy peasy!

By comparison, compilation results in an artifact that is executed later. An interpreter looks at your source code and then executes it as quickly as possible. The compiler is interested in making the code runtime as optimal as possible, without caring about the time between code writing and code execution.

Interested in being a guest speaker?

If you enjoyed this episode and would like to be part of the podcast, then please fill in this form and we’ll be in touch. ✍️

Written by

Adelina Simion

Adelina Simion

Technology Evangelist

Adelina is a polyglot engineer and developer relations professional, with a decade of technical experience at multiple startups in London. She started her career as a Java backend engineer, converted later to Go, and then transitioned to a full-time developer relations role. She has published multiple online courses about Go on the LinkedIn Learning platform, helping thousands of developers up-skill with Go. She has a passion for public speaking, having presented on cloud architectures at major European conferences. Adelina holds an MSc. Mathematical Modelling and Computing degree.