5 ms·
Crafting a Lox Interpreter in Rust, Part 1
- ibraheemdev 5y agoOne cool pattern for tokens is a T! macro: macro_rules! T { (+) => { Token::Add }, (-) => { Token::Sub }, // ... } This is really nice because it lets you match against tokens with `T![+]` instead of `Token::Add`.
- n8ta 5y agoInterested to see how you handle the nested compilers in the bytecode compiler once you hit functions. I still haven't gotten my rust-lox compiler handling this in an elegant way.
- chrisseaton 5y agoWhat's the issue with nested compilers in Lox?
- n8ta 5y agoThe implementation recommended in the book involves allocating a new compiler struct for each function with a pointer to the enclosing compiler. I found this very awkward in rust. Additionally the nested compiler needs to share the lexer (which is lazy thus mutable).
- pests 5y agoYou don't have to consume the " token outside of the string function. In the scanner you could have peeked the " (and certain other things) before calling advance() or added a backwards() opposing your advance() you'd call before string() and it's ilk.