Live data from Hacker News

Crafting Interpreters: Closures

craftinginterpreters.com

1–10 of 62 posts

Re: Crafting Interpreters: Closures

#2
This is one of my favorite books(-in-progress) to follow along with. If you've ever even had a passing curiosity in how interpreters work, I highly recommend checking it out from the beginning: http://craftinginterpreters.com/welcome.html

The writing is charming and approachable, while still packed with an impressive amount of knowledge.

Re: Crafting Interpreters: Closures

#3
post #2

This is one of my favorite books(-in-progress) to follow along with. If you've ever even had a passing curiosity in how interpreters work, I highly recommend checking it out from the beginning: http://craftinginterpreters.com/welcome.html The writing is charming and approachable, while still packed with an impressive amount of knowledge.

Any more gems like this you would recommend?

Re: Crafting Interpreters: Closures

#4
post #3
post #2

This is one of my favorite books(-in-progress) to follow along with. If you've ever even had a passing curiosity in how interpreters work, I highly recommend checking it out from the beginning: http://craftinginterpreters.com/welcome.html The writing is charming and approachable, while still packed with an impressive amount of knowledge.

Any more gems like this you would recommend?

Bob Nystrom (aka munificent)'s previous book "Game Programming Patterns"[0] is really nice, and there's lots of things in there that work outside of a game context.

[0] gameprogrammingpatterns.com

Re: Crafting Interpreters: Closures

#5
post #3
post #2

This is one of my favorite books(-in-progress) to follow along with. If you've ever even had a passing curiosity in how interpreters work, I highly recommend checking it out from the beginning: http://craftinginterpreters.com/welcome.html The writing is charming and approachable, while still packed with an impressive amount of knowledge.

Any more gems like this you would recommend?

Writing an Interpreter in Go and it's sequel Writing a Compiler in Go by Thorstan Ball are both excellent reads.

Also enjoyed Game programming patterns by Bob again, but that has already been mentioned.

Re: Crafting Interpreters: Closures

#6
The implementation described here is inspired by Lua. Even if you already know what a closure is, it might still be a worthwile read, since it probably implements them very differently from what you are thinking!

Instead of always heap-allocating variables that are used by closures, it starts by stack-allocating them and only moves them to the heap if the closure outlives its parent function. In order for this to work, the inner function always accesses the outer variables through an indirection (upvalue).

The main advantage of this approach is that it is compatible with a single-pass compiler, without sacrificing performance in the common case where closures are not present. Since the generated code for using a variable is the same no matter whether it is used by inner functions or not, the compiler can start emitting code as soon as it sees the variable declaration, without needing to look ahead to find where the variables are going to be used.

Re: Crafting Interpreters: Closures

#7
post #3
post #2

This is one of my favorite books(-in-progress) to follow along with. If you've ever even had a passing curiosity in how interpreters work, I highly recommend checking it out from the beginning: http://craftinginterpreters.com/welcome.html The writing is charming and approachable, while still packed with an impressive amount of knowledge.

Any more gems like this you would recommend?

“Compiler Construction” by Niklaus Wirth (2014) [pdf]

https://www.inf.ethz.ch/personal/wirth/CompilerConstruction/...

Modern Compiler Implementation in Java, C and ML.

https://www.cs.princeton.edu/~appel/modern/

From Nand to Tetris

https://www.nand2tetris.org/

Re: Crafting Interpreters: Closures

#8
post #6

The implementation described here is inspired by Lua. Even if you already know what a closure is, it might still be a worthwile read, since it probably implements them very differently from what you are thinking! Instead of always heap-allocating variables that are used by closures, it starts by stack-allocating them and only moves them to the heap if the closure outlives its parent function. In order for this to wor…

Another approach is to do the opposite - always store all local variables in the heap, and rely on scalar replacement of aggregates to put them back onto the stack where possible. This is what I do in my Ruby interpreter.

That’s definitely not compatible with a single-pass though!

Re: Crafting Interpreters: Closures

#9
post #3
post #2

This is one of my favorite books(-in-progress) to follow along with. If you've ever even had a passing curiosity in how interpreters work, I highly recommend checking it out from the beginning: http://craftinginterpreters.com/welcome.html The writing is charming and approachable, while still packed with an impressive amount of knowledge.

Any more gems like this you would recommend?

This is a great introduction to writing an interpreter if you know, or are learning, Go:

https://interpreterbook.com/

There's a follow-up as well: https://compilerbook.com/

Post reply on HN