You have a working interpreter. It’s small and slow and it’s missing things a real language needs, all of which was on purpose. This chapter is a list of changes to make if you want to keep going. None of this is required reading.
Exercises #
If you want to expand your knowledge on this book, here are a set of changes you can make to the existing interpreter. Every one of them has a test you can write before you start.
- Make
letsilent in the REPL instead of printingnull. - Add
<=and>=. Lexer (two-character lookahead, same as==), precedence table, infix loop, integer infix evaluation. - Add
%. Same path as above, and decide what5 % 0should do. - Add comments.
//to end of line, handled entirely in the lexer’sskipWhitespace. - Add string escapes so
"a\nb"contains a newline. Lexer only, but nowreadStringhas to allocate. - Add line numbers to tokens and error messages.
- Add
&&and||with short-circuit evaluation. The interesting part is that the right side must not be evaluated if the left side decides the answer, which means these can’t go throughevalInfixExpressionas written. - Add a
whileloop. You’ll need to decide whether it’s a statement or an expression, and what it evaluates to. - Add assignment. Then go back to
pushand ask yourself whether it should still copy. - Fix the string hash key collision by storing the original string in the key and comparing on lookup.
- Make
putswrite to real stdout instead of stderr. - Write a mark-and-sweep garbage collector.
Thanks for reading. If you find bugs or have a better way to explain something, my socials are on the front page. Fail a lot, and learn.