Writing A Compiler In Go
https://gyazo.com/7ad16e0ac416a3e92c9c658ec84e2edb
2022/7/2
2022/7/9
Thorsten Ball『Writing A Compiler In Go』読了。前書ほどの軽快さはなかったが、コンパイラにおける考え方やVMの動作原理について丁寧に解説されていた。そして今回もほとんどつまずくことなく一週間で完走できた。資産となる経験を積めた
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.
It's the sequel to … a programming language
Cover of Writing An Interpreter In Go, the first part
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.
Acknowledgments
Introduction
Evolving Monkey
The Past and Present
The Future
Use This Book
Compilers & Virtual Machines
Compilers
Virtual and Real Machines
Real Machines
What Is a Virtual Machine?
Why Build One?
Bytecode
What We’re Going to Do, or: the Duality of VM and Compiler
Hello Bytecode!
First Instructions
Starting With Bytes
The Smallest Compiler
Bytecode, Disassemble!
Back to the Task at Hand
Powering On the Machine
Adding on the Stack
Hooking up the REPL
Compiling Expressions
Cleaning Up the Stack
Infix Expressions
Booleans
Comparison Operators
Prefix Expressions
Conditionals
Jumps
Compiling Conditionals
Executing Jumps
Welcome Back, Null!
Keeping Track of Names
The Plan
Compiling Bindings
Introducing: the Symbol Table
Using Symbols in the Compiler
Adding Globals to the VM
String, Array and Hash
String
Array
Hash
Adding the index operator
Functions
Dipping Our Toes: a Simple Function
Representing Functions
Opcodes to Execute Functions
Compiling Function Literals
Compiling Function Calls
Functions in the VM
A Little Bonus
Local Bindings
Opcodes for Local Bindings
Compiling Locals
Implementing Local Bindings in the VM
Arguments
Compiling Calls With Arguments
Resolving References to Arguments
Arguments in the VM
Built-in Functions
Making the Change Easy
Making the Change: the Plan
A New Scope for Built-in Functions
Executing built-in functions
Closures
The Problem
The Plan
Everything’s a closure
Compiling and resolving free variables
Creating real closures at run time
Recursive Closures
Taking Time
Resources
Feedback
Changelog