munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.75k stars 1.03k forks source link

Challenge 1 of chapter 2.6 #970

Closed Ach113 closed 3 years ago

Ach113 commented 3 years ago

First of all I want to say that the book is amazing. I am implementing my version of the language in Rust, and trying to mimic Java implementation has been quite fun.

I have been thinking how to solve challenge 1 of chapter 2.6 (statements and state). After implementing statements, REPL is unable to interpret expressions and return (or print) their values (as interpreter takes list of statements as an argument). In fact, it is a syntax error to enter an expression into a REPL now (since parser thinks expressions are part of expression statements and need to have semicolon at the end).

The only solution I could come up with for now is to merge expressions and statements (where statements are expressions which return nil), would this be the correct approach? Could someone nudge me into a correct direction?

munificent commented 3 years ago

If it helps, you can see my suggested answer here: https://github.com/munificent/craftinginterpreters/blob/master/note/answers/chapter08_statements.md