top | item 46630417

(no title)

kevthecoder | 1 month ago

The bytecode interpreter in the second half of the book doesn't use the visitor pattern.

discuss

order

HarHarVeryFunny|1 month ago

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

etyp|1 month ago

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 people.

[1] https://craftinginterpreters.com/a-bytecode-virtual-machine....

jokoon|1 month ago

the parser does

ceronman|1 month ago

The parsers in crafting interpreters do not use the visitor pattern. The visitor pattern is used when you already have a tree structure or similar. The parser is what gives you such tree structure, the AST. When you have this structure, you typically use the visitor pattern to process it for semantic analysis, code generation, etc.

tonyedgecombe|1 month ago

I’ve only glanced at the second part but I don’t remember that being the case.