.tech Podcast - How do interpreters work?

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.

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

github-icongithub-icongithub-icon
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.

Further resources

Here are some other resources you might find interesting:

.tech Podcast - How and why you should move to Go

Our podcast host tackles how, why and when you should move to Go with the help of Johan Brandhorst & Andy Kuszyk.

.tech Podcast - Making an Open Source Go project a full time job

We are joined by Ashley Jeffs, creator of Benthos. Benthos is an Open Source stream aggregator written in Go, designed to bridge messages between different brokers. Ashley took the step to turn it into his full time work this year and discusses the advantages of using Golang to develop Benthos in.

.tech Podcast - Working in the Go ecosystem

Bartłomiej Klimczak, a GO developer at G2A talks us through what it is like to work in the GO ecosystem. Having developed a love for the programming language, Bart runs the GoKraków meet-ups in Poland.