Sponsored
Enjoy fast, free delivery, exclusive deals, and award-winning movies & TV shows.
Buy New
$39.00
FREE delivery Wednesday, April 29
Ships from: Amazon.com
Sold by: Amazon.com
$39.00
FREE delivery Wednesday, April 29
Or Prime members get FREE delivery Sunday, April 26. Order within 13 hrs 29 mins. Join Prime
In Stock
$$39.00 () Includes selected options. Includes initial monthly payment and selected options. Details
Price
Subtotal
$$39.00
Subtotal
Initial payment breakdown
Shipping cost, delivery date, and order total (including tax) shown at checkout.
Shipper / Seller
Amazon.com
Amazon.com
Shipper / Seller
Amazon.com
Returns
FREE 30-day refund/replacement
FREE 30-day refund/replacement
Quick refund
Usually issued within 24 hours. See exceptions
FREE return
At least one free return option available.
Convenient dropoff
At any of our 50,000 US locations.
See return policy
Gift options
Available at checkout
Available at checkout This item is a gift. Change
At checkout, you can add a custom message, a gift receipt for easy returns and have the item gift-wrapped
Payment
Secure transaction
Your transaction is secure
We work hard to protect your security and privacy. Our payment security system encrypts your information during transmission. We don’t share your credit card details with third-party sellers, and we don’t sell your information to others. Learn more
$26.00
Connecting readers with great books since 1972! Used books may not include companion materials, and may have some shelf wear or limited writing. We ship orders daily and Customer Service is our top priority! Connecting readers with great books since 1972! Used books may not include companion materials, and may have some shelf wear or limited writing. We ship orders daily and Customer Service is our top priority! See less
$4.99 delivery Monday, May 4. Details
Only 1 left in stock - order soon.
$$39.00 () Includes selected options. Includes initial monthly payment and selected options. Details
Price
Subtotal
$$39.00
Subtotal
Initial payment breakdown
Shipping cost, delivery date, and order total (including tax) shown at checkout.
Access codes and supplements are not guaranteed with used items.
Ships from and sold by HPB Inc..
Added to

Sorry, there was a problem.

There was an error retrieving your Wish Lists. Please try again.

Sorry, there was a problem.

List unavailable.
Kindle app logo image

Download the free Kindle app and start reading Kindle books instantly on your smartphone, tablet, or computer - no Kindle device required.

Read instantly on your browser with Kindle for Web.

Using your mobile phone camera - scan the code below and download the Kindle app.

QR code to download the Kindle App

  • Writing A Compiler In Go

Follow the author

Get new release updates & improved recommendations
Something went wrong. Please try your request again later.

Writing A Compiler In Go Paperback – August 10, 2018

4.7 out of 5 stars (105)

{"desktop_buybox_group_1":[{"displayPrice":"$39.00","priceAmount":39.00,"currencySymbol":"$","integerValue":"39","decimalSeparator":".","fractionalValue":"00","symbolPosition":"left","hasSpace":false,"showFractionalPartIfEmpty":true,"offerListingId":"uiF7hBn9iYqHikpvoeXSg1yKfM3JXXAgwUyR2PehwZ2YrHQLgV%2F5JL5rXMijTcDbht02Uo3B%2BMZmAbds85E3k6LDDAtC4PG3Il%2B2mNc2sDRrmx981ovy4BOkMCdy8F%2BrBk2gJwx7JGfK50Qfiteeag%3D%3D","locale":"en-US","buyingOptionType":"NEW","aapiBuyingOptionIndex":0}, {"displayPrice":"$26.00","priceAmount":26.00,"currencySymbol":"$","integerValue":"26","decimalSeparator":".","fractionalValue":"00","symbolPosition":"left","hasSpace":false,"showFractionalPartIfEmpty":true,"offerListingId":"uiF7hBn9iYqHikpvoeXSg1yKfM3JXXAgS%2BI2019FKKRwMNnoI8omu%2BxaEGEA6iADxoI6iB3TxpoXHzUS6bLvJL5urrsIq4sxULLygfogE%2FjTIn%2FQr0d5QkOME9K4zow8sFH26tJ%2Bbp1Qyc7EwQt8SKt2lM%2Bducq%2BPg%2FzCuaKv3MoeDze%2F6DV8A%3D%3D","locale":"en-US","buyingOptionType":"USED","aapiBuyingOptionIndex":1}]}

Purchase options and add-ons

Writing A Compiler In Go - Version 1.2 - Find out more at https://compilerbook.com

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.

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 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
Sponsored

Frequently bought together

This item: Writing A Compiler In Go
$39.00
Get it as soon as Wednesday, Apr 29
In Stock
Ships from and sold by Amazon.com.
+
$39.00
Get it as soon as Wednesday, Apr 29
In Stock
Ships from and sold by Amazon.com.
+
$43.56
Get it as soon as Wednesday, Apr 29
In Stock
Ships from and sold by Amazon.com.
Total price: $00
To see our price, add these items to your cart.
Details
Added to Cart
Choose items to buy together.

Customers also bought or read

Loading...

Product details

  • Publisher ‏ : ‎ Thorsten Ball
  • Publication date ‏ : ‎ August 10, 2018
  • Language ‏ : ‎ English
  • Print length ‏ : ‎ 352 pages
  • ISBN-10 ‏ : ‎ 398201610X
  • ISBN-13 ‏ : ‎ 978-3982016108
  • Item Weight ‏ : ‎ 1.15 pounds
  • Dimensions ‏ : ‎ 7 x 0.8 x 10 inches
  • Best Sellers Rank: #605,023 in Books (See Top 100 in Books)
  • Customer Reviews:
    4.7 out of 5 stars (105)

About the author

Follow authors to get new release updates, plus improved recommendations.
Thorsten Ball
Brief content visible, double tap to read full content.
Full content visible, double tap to read brief content.

Thorsten Ball is a professional software developer and writer.

His writing is a result of his love for what he calls "recreational programming", where he digs deep into various topics, hoping to come out the other end with a better understanding of what it is that we do when we program. For the last few years, the two topics that kept his attention the most are systems programming and programming languages.

He's also interested in the other side of doing professional software development: software engineering in a team, communicating through code, team and company culture and how to write great code together.

This fascination with programming and what it means to develop software, turned into his two books and various blog posts, podcast appearances and talks.

Sponsored

Customer reviews

4.7 out of 5 stars
105 global ratings
Sponsored
Great coverage of the fundamentals of bytecode generation and execution
5 out of 5 stars
Great coverage of the fundamentals of bytecode generation and execution
The book picks up where the previous book, "Writing an Interpreter in Go", left off. The author takes an AST tree produced by the same parser that serves the Interpreter, and builds a bytecode out of it to later feed into a bytecode execution engine that is built in parallel. The book gives the reader all necessary background about the workings of a stack machine, and goes on to building one. The reader then learns about stack maintenance during normal execution flow, as well as calling functions, where the importance of agreeing on a calling convention (passing arguments, returning a value, allocating local variables) is paramount. Another important concept is delayed patching of the code that's been generated to get the conditional and unconditional jump instructions to have correct operands - those come into play while processing if() conditions. At times, the code seems somewhat hackish, but still illustrates the points author tries to explain. My only problem is with the Kindle edition, as that it appears to have lost some stack diagrams that were supposed to further illustrate the material discussed. I'm attaching a screenshot from the online reader where there's clearly a picture missing from the page. There are a few of such places - not many, maybe 3-4 or so.
Thank you for your feedback
Sorry, there was an error
Sorry we couldn't load the review

Top reviews from the United States

  • Reviewed in the United States on December 2, 2024
    Format: PaperbackVerified Purchase
    Thorsten has done an amazing job at taking a complicated subject, distilling it down into digestible parts and taking the reader along (a fun) journey to implement a compiler in Go.

    All of the concepts are masterfully introduced at the right time and in small steps. I have no background or knowledge of creating programming languages and I was able to follow along from the first page to the last page with relative ease.

    The author's writing style is very fun and engaging which made it very easy to consume the whole book in just a few days.

    My two favorite parts about the author's coding choices are:
    1) test driven design - tests were written to set the expected outcomes before any non-test code was written
    2) zero external dependencies/modules - *everything* was built using the Monkey interpreter (from the previous book) and the Go standard library (stdlib).

    10/10 I would highly recommend this book.
    One person found this helpful
    Report
  • Reviewed in the United States on September 29, 2020
    Format: KindleVerified Purchase
    The book picks up where the previous book, "Writing an Interpreter in Go", left off. The author takes an AST tree produced by the same parser that serves the Interpreter, and builds a bytecode out of it to later feed into a bytecode execution engine that is built in parallel. The book gives the reader all necessary background about the workings of a stack machine, and goes on to building one. The reader then learns about stack maintenance during normal execution flow, as well as calling functions, where the importance of agreeing on a calling convention (passing arguments, returning a value, allocating local variables) is paramount. Another important concept is delayed patching of the code that's been generated to get the conditional and unconditional jump instructions to have correct operands - those come into play while processing if() conditions. At times, the code seems somewhat hackish, but still illustrates the points author tries to explain.

    My only problem is with the Kindle edition, as that it appears to have lost some stack diagrams that were supposed to further illustrate the material discussed. I'm attaching a screenshot from the online reader where there's clearly a picture missing from the page. There are a few of such places - not many, maybe 3-4 or so.
    Customer image
    5.0 out of 5 stars
    Great coverage of the fundamentals of bytecode generation and execution

    Reviewed in the United States on September 29, 2020
    The book picks up where the previous book, "Writing an Interpreter in Go", left off. The author takes an AST tree produced by the same parser that serves the Interpreter, and builds a bytecode out of it to later feed into a bytecode execution engine that is built in parallel. The book gives the reader all necessary background about the workings of a stack machine, and goes on to building one. The reader then learns about stack maintenance during normal execution flow, as well as calling functions, where the importance of agreeing on a calling convention (passing arguments, returning a value, allocating local variables) is paramount. Another important concept is delayed patching of the code that's been generated to get the conditional and unconditional jump instructions to have correct operands - those come into play while processing if() conditions. At times, the code seems somewhat hackish, but still illustrates the points author tries to explain.

    My only problem is with the Kindle edition, as that it appears to have lost some stack diagrams that were supposed to further illustrate the material discussed. I'm attaching a screenshot from the online reader where there's clearly a picture missing from the page. There are a few of such places - not many, maybe 3-4 or so.
    Images in this review
    Customer image
    2 people found this helpful
    Report
  • Reviewed in the United States on April 12, 2020
    Format: PaperbackVerified Purchase
    I have both the waiig and the wacig books. I followed both books and entered the code myself. It took me about 2 months to finish the books and code in sequence. (It's important that you follow them in sequence.)

    The compiler book went much faster because the basics are the same as the interpreter book. Instead of tree-walk to eval, the compiler emits instructions for the vm to execute. Since early in my career I was an assembly language programmer. The vm's stack architecture is quite natural to me so the vm portion, though new, was easy for me to follow as well.

    Both books are wonderfully written. Code in both book work as advertised. I enjoyed reading and following the code immensely.

    Ever since I left school I wanted to someday write a compiler. I did it! :-) . I am thinking about re-implementing the programs in Python. That should be quite fun, I would think.
    6 people found this helpful
    Report
  • Reviewed in the United States on November 16, 2019
    Format: PaperbackVerified Purchase
    This book is incredible. I've studied making programming languages over the years as a hobbyist because I enjoy it and this book has been tremendously helpful in getting my own language up and going.

    It's super easy to read and not weighed down by theory. It's like the practical, up-to-date version of the Dragon book. If I was trapped on an island and forced to build a programming language (hint hint!), this is the only book I would take.
    4 people found this helpful
    Report
  • Reviewed in the United States on January 10, 2020
    Format: PaperbackVerified Purchase
    I absolutely love this book and I love you! The question of interpreters and compilers have puzzled me for a while and I always wanted to know the process of creating my own language. This book provides that instruction and pulls it all into one place so that you do not have to run around the internet trying to make sense of the intricacies related to this often complex study. Thank you for your great work!
    4 people found this helpful
    Report
  • Reviewed in the United States on March 30, 2021
    Format: PaperbackVerified Purchase
    Explains a complex subject very well. It does not focus heavily on theory like the “dragon” book, but provides a pragmatic basis to build on.
    3 people found this helpful
    Report
  • Reviewed in the United States on November 12, 2018
    Format: PaperbackVerified Purchase
    One person found this helpful
    Report
  • Reviewed in the United States on October 12, 2025
    Format: KindleVerified Purchase
    This is an excellent follow-on to the book on building an interpreter. It builds a compiler to a bite code interpreter (virtual machine) step by step. It shows the code with full tests as it is developed. More importantly, it explains why each step is necessary and how it works.

    Review downgraded to three stars because the base language doesn't include loops and imports, so the compiler doesn't, either. They're really important steps to know, and are completely left out of the text and code.

Top reviews from other countries

Translate all reviews to English
  • Sam Brown
    5.0 out of 5 stars So THAT's how compilers work
    Reviewed in the United Kingdom on September 12, 2020
    Format: KindleVerified Purchase
    This is a great book for understanding the inner parts of a compiler, especially if:
    1. You are already a programmer
    2. You code along in Go (no previous Go knowledge needed)
    3. You have some interest in compilers
    It is very hands on, and you need to work from beginning to end of the book, which is not how technical books on computers usually work. I found it rewarding getting feedback when the code I had copied actually worked as I went along.
    It is not a comprehensive book about all the various ways a compiler can be written.
  • Sören Schellhoff
    5.0 out of 5 stars Sehr guter Einstieg
    Reviewed in Germany on November 6, 2019
    Format: KindleVerified Purchase
    Das Buch ist ein guter Einstieg in die Thematik und verfolgt dabei, wie bereits der erste Teil, einen sehr praktischen Ansatz. Ich kannte Go vorher nur sporadisch und konnte den Code dennoch gut verstehen. Für die die sich weitergehende für das Thema interessieren empfehle ich auch einen Blick auf Crafting Interpreters von Bob Nystrom zu werfen. Hier wird ein ähnlicher Ansatz verfolgt, wobei sich das Buch in vielen Kleinigkeiten unterscheidet. In Kombination mit diesem Buch erhält man so noch ein paar mehr Ideen zur praktischen Umsetzung von Interpretern und VM Compilern.
    Report
  • Franck
    5.0 out of 5 stars Coup de maitre
    Reviewed in France on September 14, 2018
    Format: PaperbackVerified Purchase
    « Writing a compiler in Go » est le digne successeur de “Writing an interpreter in Go” et le talent de l’auteur pour rendre simple des choses complexes ne se dément pas.
    Comme pour le premier opus, le cheminement est extrêmement progressif (sans pour autant prendre de raccourcis) et le style très agréable.
    A la fin du livre vous aurez réellement écris 100% d’un compilateur (byte code et machine virtuelle) sans utiliser aucun outil externe et aucune librairie autre qu’une toute petite portion de la librairie standard Go.

    Même si vous ne pratiquez pas ou n’aimez pas Go (ce qui serait vraiment dommage), vous pouvez faire abstraction du langage utilisé, ce livre reste un des rares ouvrages accessible sur le sujet et rien ne vous empêche de porter le code dans votre langage préféré (quelqu’un la fait pour Rust sur le premier volume).

    J’ai vraiment hâte de découvrir ce que sera son prochain livre. Suggestions (improbables) : « Writing a micro-processor in VHDL » ?!? « Writing an operating system in Monkey » ?!?
    One person found this helpful
    Report