Skip to main content
CF

Writing A Compiler In Go

0h 0m 0s
English
Paid

This is the sequel to Writing An Interpreter In Go.

We're picking up right where we left off and write a compiler and a virtual machine for Monkey.

Runnable and tested code front and center, built from the ground up, step by step — just like before.

But this time, we're going to define bytecode, compile Monkey and execute it in our very own virtual machine.

It's the next step in Monkey's evolution.

t's the sequel to … a programming language

Writing A Compiler In Go is the sequel to Writing An Interpreter In Go. It starts right where the first one stopped, with a fully-working, fully-tested Monkey interpreter in hand, connecting both books seamlessly, ready to build a compiler and a virtual machine for Monkey.

In this book, we use the codebase (included in the book!) from the first part and extend it. We take the lexer, the parser, the AST, the REPL and the object system and use them to build a new, faster implementation of Monkey, right next to the tree-walking evaluator we built in the first book.

The approach is unchanged, too. Working, tested code is the focus, we build everything from scratch, do baby steps, write tests firsts, use no 3rd-party-libraries and see and understand how all the pieces fit together.

It's a continuation in prose and in code.

Do you need to read the first part before this one? If you're okay with treating the code from the first book as a black box, then no. But that's not what these books are about; they're about opening up black boxes, looking inside and shining a light. You'll have the best understanding of where we're going in this book, if you know where we started.

Learn how to write a compiler and a virtual machine

Our main goal in in this book is to evolve Monkey. We change its architecture and turn it into a bytecode compiler and virtual machine.

We'll take the lexer, the parser, the AST and the object system we wrote in the first book and use them to build our own Monkey compiler and virtual machine … from scratch! We'll build them side-by-side so that we'll always have a running system we can steadily evolve.

What we end up with is not only much closer to the programming languages we use every day, giving us a better understanding of how they work, but also 3x faster. And that's without explicitly aiming for performance.

Here's what we'll do:

  • We define our own bytecode instructions, specifying their operands and their encoding. Along the way, we also build a mini-disassembler for them.
  • We write a compiler that takes in a Monkey AST and turns it into bytecode by emitting instructions
  • At the same time we build a stack-based virtual machine that executes the bytecode in its main loop

We'll learn a lot about computers, how they work, what machine code and opcodes are, what the stack is and how to work with stack pointers and frame pointers, what it means to define a calling convention, and much more.

We also

  • build a symbol table and a constant pool
  • do stack arithmetic
  • generate jump instructions
  • build frames into our VM to execute functions with local bindings and arguments!
  • add built-in functions to the VM
  • get real closures working in the virtual machine and learn why closure-compilation is so tricky

About the Author: Thorsten Ball

Thorsten Ball thumbnail

Thorsten Ball is a German software engineer and the author of two of the most widely read self-published programming books of the last decade: Writing An Interpreter In Go and Writing A Compiler In Go. The books anchor a generation of developers' first serious exposure to language implementation as a discipline.

His CourseFlix listing carries two Thorsten Ball courses based on those books — covering interpreter construction and compiler construction in Go from first principles. Material is paid and aimed at engineers who want to understand how programming languages actually work by building one.

Books

Read Book Writing A Compiler In Go

#TitleTypeOpen
1writing_a_compiler_in_go_1.2 PDF

Related courses

  • Refactoring With Bill thumbnailUpdated 2y ago

    Refactoring With Bill

    By: Ardan Labs
    Refactoring with Bill presents hour-long videos of Bill working on different projects while providing a stream of consciousness on what he is doing and why.
    7h 49m
  • Angular and Golang: A Rapid Guide - Advanced thumbnailUpdated 3y ago

    Angular and Golang: A Rapid Guide - Advanced

    By: Udemy
    Learn how to create an Ambassador App using Angular and Golang. We will build 3 frontend apps Admin, Ambassador and Checkout and they will consume a big Golang
    9h 40m
  • Redis Internals thumbnailUpdated 9mo ago

    Redis Internals

    By: Arpit Bhayani
    Redis Internals by Arpit Bhayani — self-paced course rebuilding Redis's key features in Go. Master database design, replication, and persistence.
    9h 6m5/5

Frequently asked questions

What is Writing A Compiler In Go about?
This is the sequel to Writing An Interpreter In Go. We're picking up right where we left off and write a compiler and a virtual machine for Monkey. Runnable and tested code front and center, built from the ground up, step by step — just…
Who teaches this course?
It is taught by Thorsten Ball. You can find more courses by this instructor on the corresponding source page.
How long is the course?
It is delivered as a self-paced online course on CourseFlix.
Is it free to watch?
It is part of CourseFlix's premium catalog. A subscription unlocks the full video player; the course description, table of contents, and preview information are available to everyone.
Where can I watch it online?
The course is available to watch online on CourseFlix at https://courseflix.net/course/writing-a-compiler-in-go. The page hosts every lesson with the integrated video player; no download is required.