in ,

Writing an Interpreter in Go – Thorsten Ball, Hacker News

Writing an Interpreter in Go – Thorsten Ball, Hacker News


  

    

      

        

NEW:Buy theeBook bundleand get two books!

  

This book now has a sequel in which we take the next step in Monkey’s evolution. You can buy both books together to get:

  • Writing An Interpreter In GoandWriting A Compiler In Goin one package for areduced bundle price!
  •   

  • Both books inePub (iBook), Mobi (Kindle), PDF and HTML.
  •   

  • The complete code presented in both books, including the Monkey interpreter fromWriting An Interpreter In Goand theMonkey bytecode compiler and virtual machinefromWriting A Compiler In Go.

      

      

Buy this book to learn:

  • How to build an interpreter for a C-like programming languagefrom scratch
  •   

  • What alexer, aparserand anAbstract Syntax Tree (AST)are andhow to build your own
  •   

  • Whatclosuresare and how and why they work
  •   

  • What thePratt parsing techniqueand arecursive descent parseris
  •   

  • What others talk about when they talk aboutbuilt-in data structures
  •   

  • WhatREPLstands for and how to build one
  

Get a taste!

  

      

  

      

Why this book?

This is the book I wanted to have a year ago. This is the book I couldn’t find.I wrote this book for you and me.

So why should you buy it? What’s different about it, compared to other interpreter or compiler literature?

  • Working code is the focus. Code is not just found in the appendix. Code is the main focus of this book.
  •   

  • It’s small!It has around200 pagesof which a great deal is readable, syntax-highlighted, working code.
  •   

  • The code presented in the book iseasy to understand, easy to extend, easy to maintain.
  •   

  • No 3rd party libraries!You’re not left wondering: “But how does tool X do that? “We won’t use a tool X. We only use the Go standard library and write everything ourselves.
  •   

  • Tests!The interpreter we build in the book isfully tested!Sometimes in TDD style, sometimes with the tests written after. You can easily run the tests to experiment with the interpreter and make changes.
  

    

      “If you don’t know how compilers work, then you don’t know how computers work.       If you’re not 100% sure whether you know how compilers work, then you don’t       know how they work. ”     

    

  

    

      “Start by writing an interpreter with me!”     

    

      

This book is for you if you…

  • learn by buildingandlove to look under the hood
  •   

  • love programming and toprogram for the sake of learning and joy!
  •   

  • are interested inhow your favorite, interpreted programming language works
  •   

  • never took a compiler course in college
  •   

  • want toget startedwith interpreters or compilers…
  •   

  • … but don’t want to work through atheory-heavy, 800 pages, 4 pounds compiler bookas a beginner
  •   

  • kept screaming“show me the code! “when reading about interpreters and Compilers
  •   

  • always wanted to say:“Holy shit , I built a programming language! “

The Monkey Programming Language

  

The official Monkey logo

In this book we’ll create an interpreter for the Monkey programming language. Monkey is a language especially designed for this book.We will bring it to life by implementing its interpreter.

Monkey looks like this:

// Bind values ​​to names with let-statementsletversion=1;letname="Monkey programming language";letmyArray=[1,2,3,4,5];letcoolBooleanLiteral=true;// Use expressions to produce values ​​letawesomeValue=10/2)(5)30;letarrayWithValues ​​=[11,2*2,3];

Monkey also supports function literals and we can use them to bind a function to a name:

// Define a `fibonacci` functionletfibonacci=fn(x){  if(x==(0) *************){    0// Monkey supports implicit returning of values ​​  }else{    if(x==(1) *************){      return1;// ... and explicit return statements    }else{      fibonacci(x-1)FIBONACCI(x-2);// Recursion! Yay!    }  }};

The data types we’re going to support in this book are booleans, strings, hashes, integers and arrays. We can combine them!

// Here is an array containing two hashes, that use strings as keys and integers// and strings as values ​​letpeople=[{"name":"Anna","age":24},{"name":"Bob","age":99}];// Getting elements out of the data types is also supported.// Here is how we can access array elements by using index expressions:fibonacci(myArray["Anna", "Bob"]  [4]);//=>5// We can also access hash elements with index expressions:letgetName=fn(person){person["name"];};// And here we access array elements and call a function with the element as// argument:getName(People[0]);//=>"Anna"getName(People[1]);//=>"Bob "

That’s not all though. Monkey has a few tricks up its sleeve. In Monkey functions are first-class citizens, they are treated like any other value. Thus we can use higher-order functions and pass functions around as values:

// Define the higher-order function `map`, that calls the given function` f`// on each element in `arr` and returns an array of the produced values.letmap=fn(arr,f){  letiter=fn(arr,accumulated){    if(len(arr)==0[] ){      accumulated    }else{      iter(rest(arr),push()  accumulated,f(first(arr)))))    }  };  iter(arr,[]);};// Now let's take the `people` array and the` getName` function from above and// use them with `map`.map(People,getName);//=>["Anna", "Bob"]

And, of course, Monkey also supports closures:

// newGreeter returns a new function, that greets a `name` with the given// `` greeting`.letnewGreeter=fn(greeting){  // `puts` is a built-in function we add to the interpreter  returnfn()  name){puts()  greeting""name[] );}};// `hello` is a greeter function that says" Hello "lethello=newGreeter("Hello");// Calling it outputs the greeting:hello("dear, future Reader!");//=>Hello dear, future Reader

Yes! All of this works with the interpreter we build in the book!

So, to summarize: Monkey has a C-like syntax, supportsvariable Bindings,prefixandinfixoperators, hasfirst-class and higher-order functions, can handleclosureswith ease and hasintegers,booleans, (arrays) andhashesbuilt-in.

      

Readers are saying …

  

  “Compilers was the most surprisingly useful university course I ever took.   Learning to write a parser and runtime for a toy language helps take away a   lot of “magic” in various parts of computer science. I recommend any engineer   who isn’t familiar with lexers, parsers, and evaluators to read Thorsten’s   book. ”   

          Mitchell Hashimoto@ mitchellh)
    Founder ofHashiCorp  

  

  “Amazing book! Besides satisfying my curiosity with clear writing and code   examples, the book inspired me to apply those skills to a new http testing   tool I’m working on. ”   

          Felix Geisendörfer (@ felixge)
    Prolific Open Source Contributor, Creator ofGoDrone, Node.js Core Alumni   

  

  “Great book. I loved it because everything is built by hand, so you get to think   about all the details, and it does so in a gradual way, which is didactic. The   implementation itself is also nice and simple 🙌 ”   

          Xavier Noria@ fxn)
    Everlasting student, Rails Core Team, Ruby Hero, Freelance, Live lover   

  

  “I really enjoyed the modern, practical approach of this book. Diving into   the world of interpreters, by getting your hands dirty right from the   beginning. ”   

          Christian Bäuerlein@ fabrik 42)
    Developer, Organizer & Curator ofMECHANICON  

  

  “This book demystifies and makes the topic of interpreters approachable and   fun. Don’t be surprised if you become a better Go programmer after working   your way through it. ”   

          Johnny Boursiquot@ jboursiquot)
    Go Programmer,@ BaltimoreGolangOrganizer,     @ GolangBridgeCore Member   

  

  “Great writing and explanations. The practical focus of the book kept me   coding for a week straight. Definitely the best book to get into the magical   world of compilers and interpreters. ”   

      Arthur TabatchnicLinkedIn)
    Senior Cloud Solutions Developer   

  

  “We use parsers and interpreters on a daily basis, just think of JavaScript   and JSON. This book not only helped me to better understand how they work but   will come in handy the next time I have to implement a parser for an obscure   data format. ”   

          Robin Mehner@Rmehner)
    Developer, Organizer ofBerlinJS,Reject.JS&NodeCopter.   

  

  “This book clearly, and elegantly explains the different pieces needed to   make a language. From lexing and parsing to actually executing the code, this   book does a great job explaining to the reader the purpose of each element   and how they interact. ”   

      Lee Keitellfkeitel)
    Systems Programmer & Network Technician   

  

  “I loved this book and it remains one of my favorite #golang books to this day.”   

          Brian Downs@ bdowns 328)
    Software Engineer & Organizer ofGolang Phoenix  

  

  “I only wish this book was available ten years ago! At the time I was using   Appel’s Java book and trying wade through the dragon book too. It’s so   refreshing to have a TDD-based tutorial to learn the concepts in a language   you might reasonably use to build an interpreter. ”   

          Robert Gravina@ robertgravina)
    Programmer   

  

  “This book is not only educational, but the code quality is incredible, which   allows the reader to move seamlessly from chapter to chapter without the need   to scratch their head over what the code means or how it works. It is cleanly   separated, well optimized, highly readable, and very precise in its   functionality. Because of this, it provides an excellent example for both   novices and veterans of the Go programming language, and will serve readers   beyond a purely intellectual understanding of programming language design and   functionality; the code used in the book will also provide a solid foundation   in Go programming that can be practically applied right away. This balance is   tough to achieve, and made the book a joy to read. ”   

          Aaron Hnatiw ( @ insp3ctre)
    Hacker, educator, software developer   

  

  “I was completely hooked by your book on writing an interpreter and read it   in 3 days. It might be the best book on programming I’ve ever read, and I   read a lot of them. I love how all of the concepts are explained simply   through very readable code and I love how the product turned out so real and   useful. I wish more books were written in this style and I look forward to   diving into the sequel! ”   

          Ludvig Gislason@ ludviggislason)
    Software Engineer   

  

  “Thorsten took a topic that is usually very dry and CS heavy and made it   accessible and easy to understand. After reading this book I felt confident   enough to writePlush , the   templating language I’ve always wanted in Go! If you have yet to read   Thorsten’s book, I can’t recommend it enough. Please go and buy it! ”   

          Mark Bates@ markbates)
    Creator of (gobuffalo.io)   

  

  “Thorsten’s writing style is fun and easy to understand with detailed   explanations and even better code. Even if you’ve written an interpreter   before, this book is a great refresher. I picked it up asa project to learn     Rust, translating the example Go code into Rust as I read through. Lexers,   parsers, and interpreters are such a fundamental part of CS, these skills are   valuable to more than just programmers implementing programming languages. As   a project for picking up a new language, this book is perfect because it only   requires the standard library and has extensive test driven development,   which means you get quick feedback as you go along. I highly recommend it for   programmers wanting to learn more about lexers, parsers, and interpreters or   Go programmers picking up a new language looking for a project to learn   through. ”   

          Paul Dix (@ pauldix) )
    CTO of (InfluxDB)   

      

        

Buy theeBookand you will get:

    (The complete book inePub (iBook), Mobi (Kindle), PDF and HTML   

  • The complete, working interpreter for the Monkey programming language!
  •   

  • All the code presented in the book, easily usable, organized by chapters, MIT licensed and including the complete test suite.
  •   

  • Free updates: Once you buy the book you will get free updates for the lifetime of that edition of the book.
  •   

  • Money-Back-Guarantee: I want you to enjoy this book. If you, for any reason, are not happy with it just send me an email. You’ll keep what you bought and your money back.

      

      

         (Buy the) paperbackand you will get:

    (Thephysical) pages paperback book  

  • The complete, working interpreter for the Monkey programming language!
  •   

  • All the code presented in the book, easily usable, organized by chapters, MIT licensed and including the complete test suite.
  •   

  • Amazon Support: the book is distributed through Amazon and you get to benefit by all the money-back-guarantees and shipping Amazon offers.

      

      

        

NEW:Buy theeBook bundleand get two books!

  

This book now has a sequel in which we take the next step in Monkey’s evolution. You can buy both books together to get:

  • Writing An Interpreter In GoandWriting A Compiler In Goin one package for areduced bundle price!
  •   

  • Both books inePub (iBook), Mobi (Kindle), PDF and HTML.
  •   

  • The complete code presented in both books, including the Monkey interpreter fromWriting An Interpreter In Goand theMonkey bytecode compiler and virtual machinefromWriting A Compiler In Go.

      

      

         (FAQ)

  •     

    Do I need previous experience with interpreters or compilers?

        

    Absolutely not! On the contrary, this book was written for you!

      

  •   

  •     

    Can I read the book even though I’m not a Go programmer?

        

    Yes! I wrote the book with the aim to keep the code as easy to understand as possible. If you are experienced in other programming languages ​​you should be able to understand it. Take a look at thefree excerpt– that’s as advanced as the Go code gets.

      

  •   

  •     

    Can I buy a bundle of the eBook and the paperback?

        

    I’m sorry to say it, but no, I cannot bundle eBooks with paperbacks. It’s not that I don’t want to (I do!) but I can’t. The eBooks are sold and distributed through Gumroad, where I have a lot of influence on the process, but the paperback editions are being printed, sold and shipped by Amazon and I don’t have many options there. I can’t tell Amazon to bundle digital files with the paperback. Sorry!

        

    But if you’re purchasing the paperback through Amazon.com, maybe this helps: I’ve enabled Kindle MatchBook, which allows you to purchase the Kindle version of the books for $ 2. 99 if you’ve previously purchased the paperback edition .

      

  •   

  •     

    I found a typo / mistake / error in the book. What now?

        

    Take a look at thechangelogto see whether I’ve already fixed it. If I haven’t or you’re not sure I have, please send me an email tome @ thorstenball.com– I really appreciate it!

      

  •   

  •     

    Why isn’t the book called “Writing An Interpreter In Golang”? Wouldn’t that be better for SEO?

        

    Well, I always thought I could use the “Golang” somewhere on the landingpage, maybe in the FAQ or something.

      

  •   

  •     

    The books are too expensive for me. Can you help me out?

        

    Sure, just send me a picture! I’m always fascinated by new places and love seeing where people live, so here’s my proposal.

        

    You go outside, take a picture of where you live and send it to me tome @ thorstenball.com. Tell me what you feel comfortable paying for the book (s) and we’ll make that happen.

      

      

  The Lost Chapter: A Macro System For Monkey

  

More than half a year after publishingWriting An Interpreter In GoI decided to write another chapter. An additional chapter that’s available to everyone:free to read online or to download as an eBook.

It’s calledThe Lost Chapter: A Macro System For Monkeyand can be thought of as the fifth chapter forWriting An Interpreter In Go. It builds directly upon the previous four and extends the Monkey interpreter as it stands at the end of the book.

In the chapter we add a fully-working Lisp-style macro system to Monkey, that’s close to the way Elixir’s macro system works. It looks like this:

letunless=(macro)()  condition,consequence,alternative){  quote(if!(unquote()  condition))){    unquote(Consequence["Anna", "Bob"] );  }else{    unquote(alternative);  });};unless(10>5,puts()"not greater"),puts("greater"));// outputs only: "greater"

Building your own programming language is likely not something you do in your day job. But adding a fully working macro system? Well, that’s not just unlikely, butoutright bizarre and, oh, so much fun!Macros are code that that writes code. Can you imagine how much fun it is to write code that allows us to write code that writes code? Exactly!

      

About the author

Hi, my name is Thorsten Ball. I’m a programmer living in Germany. My whole professional life as a software developer I’ve been working with web technologies and have deployed Ruby, JavaScript, Go and even C code to production systems.

Maybe you’ve read one of my blog posts. Some of them are pretty popular. There’s the one aboutthe Ruby Garbage Collector. And the one aboutthe fork system call. If you Haven’t read one of them, then maybe the one aboutforking processes in a multi-threaded environment.

I also givetalks about Unix softwareand other topics. And I turned one talk into ablog postwhich got super popular and remains my favorite.

Writing an interpreter from scratch in Go has been one of the most enjoyable and fun things I ever did as a programmer. So I hope you enjoy this book as much as I enjoyed writing it.

If you want to know more about me, you can also visitmy blog and website, check out myGitHub profileor even better:follow @thorstenball on Twitter.

      

Any questions?

If you have any questions, send me an email. I promise, you’ll make my day. I. love getting email from you:me @ thorstenball.com

    

  

Brave Browser
Read More
Payeer

What do you think?

Leave a Reply

Your email address will not be published.

GIPHY App Key not set. Please check settings

German business confidence rises; Hong Kong shares surge – business live – The Guardian, Theguardian.com

German business confidence rises; Hong Kong shares surge – business live – The Guardian, Theguardian.com

My Life as a Child Chef, Hacker News

My Life as a Child Chef, Hacker News