Writing an Interpreter Part 1
The longer I’ve spent coding, the more interested I’ve become in how languages work. Through that research I came across the book Crafting Interpreters by Bob Nystrom. The book walks the reader through creating two language intepreters for a language called Lox. The first interpreter is presented in Java and the second in C. For my next series of blog posts, I’m going to walk through what I’m learning as I follow his book through the first interpreter which I’m writing in Go.
Before I start with the code, I’d like to briefly define compilers and interpreters. One of the differences between languages is if they’re compiled or interpreted. Go, C, and C++ are compiled languages meaning that they use a compiler to convert the program into binary code before the code is executed. Interpreted languages include Ruby and Python, and interpreters execute the code without converting them to machine code first.
The first part of the process is scanning or lexical analysis. This is where the interpreter (or compiler) takes in the source code and looks for tokens. These tokens are chunks that the program has learned to recognize. For the first part of my scanner, I focused on single-character tokens, such as “,”, “.”, and “[”.
In Go, programs are run through the main package, so we’ll start with the set up there:
The main function looks at the supplied argument and if the argument passes the second condition, it runs the runFile function, which will run the run function.
In the run function the source string is used to create a new Scanner struct and after running the source string through the ScanTokens functions, the tokens are printed out.
The real meat of the program so far lies in the scanner package. That’s where the scanner and token structs are defined and where the scanning happens.
In the scanner package, I first declared a new type TokenType and set up some single character tokens as constants.
From here, I set up the Token and Scanner structs:
With that in place, it’s ready for the NewScanner and ScanTokens functions.
The NewScanner function is pretty straightforward. It takes a source and creates a Scanner struct that begins at line 1. While my interpreter doesn’t yet implement the line count part of the Scanner, it will eventually be able to tell us where to find the tokens.
The ScanTokens function has a few associated helper functions: isAtEnd, scanTokens, advance, and addToken. ScanTokens returns an array of tokens for the scanner that called it.
ScanTokens passes the scanner source through scanToken and adds found Tokens to the scanner’s list of Tokens until the scanner’s current is greater than or equal to the length of the source.
The last two helper functions, advance and addToken, are called by scanTokens. Advance moves the scanner’s current attribute along the source and addToken adds a token to the scanner’s list of tokens with the appropriate attributes.
This is the extent of my interpreter so far! I’m learning a lot through this process about how languages are read and about Go, which is a new language for me. I’m still wrapping my head around a lot of it, but re-reading and explaining the code here is helping a lot.
In the next part of my interpreter I will be working with longer tokens, operators, numbers, and reserved words! Thanks for coming along on the journey!
Resources:
Crafting Interpreters
Geeks for Geeks; Compiler vs Interpreter
Feed Image by Opensource.com