Earlier quoted context omitted.
The visitor pattern is very common in programming language implementations. I've seen it in the Rust compiler, in the Java Compiler, in the Go compiler and in the Roslyn C# compiler. Also used extensively in JetBrains' IDEs. What do you have against this pattern? Or what is a better alternative?
Visitor is heavy of code pattern that can be replaced by elegant, readable switch with exhaustive check, so all operations available by "Kind" enum are covered.
Crafting Interpreters
41–50 of 62 posts
Re: Crafting Interpreters
#42Earlier quoted context omitted.
The visitor pattern is very common in programming language implementations. I've seen it in the Rust compiler, in the Java Compiler, in the Go compiler and in the Roslyn C# compiler. Also used extensively in JetBrains' IDEs. What do you have against this pattern? Or what is a better alternative?
Visitor is heavy of code pattern that can be replaced by elegant, readable switch with exhaustive check, so all operations available by "Kind" enum are covered.
Re: Crafting Interpreters
#43I hope we get to see "Add a type checker to Lox" sequel
Re: Crafting Interpreters
#44Earlier quoted context omitted.
The bytecode interpreter in the second half of the book doesn't use the visitor pattern.
No, but his first "Tree-walk Interpreter" does - he builds an AST then uses the visitor pattern to interpret it. https://craftinginterpreters.com/representing-code.html#work...
> The style of interpretation it uses—walking the AST directly—is good enough for some real-world uses, but leaves a lot to be desired for a general-purpose scripting language.
Sometimes it's useful to teach progressively, using techniques that were used more often and aren't as much anymore, rather than firehosing a low-level bytecode at people.
[1] https://craftinginterpreters.com/a-bytecode-virtual-machine....
Re: Crafting Interpreters
#45Earlier quoted context omitted.
No, but his first "Tree-walk Interpreter" does - he builds an AST then uses the visitor pattern to interpret it. https://craftinginterpreters.com/representing-code.html#work...
To quote the very first paragraph of the bytecode interpreter section[1]: > The style of interpretation it uses—walking the AST directly—is good enough for some real-world uses, but leaves a lot to be desired for a general-purpose scripting language. Sometimes it's useful to teach progressively, using techniques that were used more often and aren't as much anymore, rather than firehosing a low-level bytecode at peopl…
He's doesn't actually build on this though, but rather goes back to a single pass compiler (no AST, no visitor) for his bytecode compiler.
Re: Crafting Interpreters
#46Re: Crafting Interpreters
#47Re: Crafting Interpreters
#48Earlier quoted context omitted.
Compiler doesn't match the title of the book.
Well, most interpreters use a compiler internally, for example to compile to byte code. The book explains that as well, so I'd recommend just reading it: https://craftinginterpreters.com/a-map-of-the-territory.html...
Re: Crafting Interpreters
#49I love this book! I do wish there was a new edition that updated the version of Java used in the tree-walk interpreter. There's been some additions to the language, like sealed classes and exhaustive switches, that could really benefit the implementation.
It's a fun little exercise left to the reader to upgrade to current Java. It pretty much eliminates the need for his ad-hoc code generation tool.