Now that the parser can handle multiple statements and the usage of variables, let’s add the ability to actually declare variables.
build a programming language posts
Subscribe to just build a programming language posts via RSS.
So the parser can handle a single expression, but since we’re not building a Lisp, that’s not enough. It needs to handle multiple statements. For context, an expression is a piece of code that represents a value whereas a statement is a piece of code that can be executed but does not result in a value.
Arithmetic expressions are all well and good, but they don’t really feel much like a programming language. To fix that, let’s start working on variables and function calls.
The code from part 4 that checks whether a pair of binary operators should be grouped to the left or right works, but I’m not particularly happy with it. The issue is that it needs to pattern match on the right node twice: first in the
should_group_left function, and then again in
should_group_left returned true.
Parsing groups is pretty straightforward, with only one minor pain point to keep in mind. I’ll gloss over adding left and right parentheses because it’s super easy—just another single character token.