Interpreter Project: Parser

Published on May 8, 2018

This step has gone quite smoothly. A lot of the details involved in parsing were taken care of in the last section. The AST Node classes that hold the parsed info are already in place. All that was needed now was to iterate over the tokens provided by the scanner, figure out what kind of 'sentence' the tokens correspond to in the Lox language and then build the correct AST Node from the results.

The code so far can be found here. It also contains the code for the first two challenges, the comma and ternary operators.

Possible Refactoring

I'm wondering if I should split up the parser. Much like the Ringo::LoxScanner, the Ringo::LoxParser really only has one public method, parse. All of the parsing functionality is in private methods.

Those private methods can be split into two distinct types. There are support methods, such as match?, advance, at_end? and so on. Then there are the methods that implement the parser grammar, like expression, multiplication and unary.

I'm considering moving the support methods to a module that can be included in the Ringo::LoxParser. It would clean up the code a bit and reduce the size of the file. There are going to many more grammar expression methods that will be added as the project progresses, so maintaining a single focus in the Ringo::LoxParser might be worthwhile.

Then again, this is just a learning experience for me. I don't intend to keep working on this little language. At least not in Ruby. The second section of the Crafting Interpreters book rebuilds the Lox language in C with a virtual machine. I believe it will be a much more valuable use of my time to focus on, add to, and improve the C version of this project.

Moving Forward

The next section adds evaluation. The interpreter will finally start to give answers for the expressions it parses. This should also be a very interesting step. I'm looking forward to it.