Most programming languages have a set of reserved keywords. The compiler or interpreter has to have a way to identify those keywords while the source code is being read. Here is how I do it for Cube.
Getting started on a new programming language and floundering around looking for how to structure the code. Fun, fun fun.
This is the last article in this series. The Lox language is feature complete, though far from actually complete. It is time to move on to something a little more realistic.
Classes were added to the language this time. Almost everything is in place, Class declarations, object initialization, properties, methods, references to 'this', etc. The only thing missing is Inheritance. That is added in the next section.
This section was all about adding a Semantic Analysis step to the project. A Resolver is created to walk the Parse Tree and make note of where and how variables are accessed. That data is then provided to the Interpreter code to prevent a potential bug.
Function declarations and function calls. This looks like a real language now. Of course there is no standard library, no networking library, input / output library, etc. It is apparent to me now that the language definition is relatively easy. It is all the supporting libraries that constitute the bulk of the work.
Branching and looping, two very critical pieces of any Turing complete programming language, are now available. With all of the other work that has been done these features were quite easy to implement as well.
A lot of work went into this section. Statements were added to the grammar, as well as variable declarations, variable assignment and scopes.
The interpreter code is now in place and working. The foundation on which the rest of the Lox language will be created is done.
Getting deep into the Lox language now. The parser is where the syntax of a language lives.
A bit of a side-step here to generate the many classes needed to store the structure of the Lox code to be run. I have Ruby do the hard work for me.
Finally some progress on actual interpreter code. The scanner code is in place and generating tokens. Nothing much happens with them other than being printed to stdout, but it is progress.
I should have spent a bit more time with that interpreter book before committing to use it for this project. I would need much more time to learn Scheme than I want to spend.
This is the second article in the Building an Interpreter series. The project is up and running on Github and some exercises from Chapter One of Essentials of Programming Languages have been written.