Let’s Build An Interpreter In Python From Scratch
Part 5: Creating a Parser for Looping and Branching statement
This article is in continuation from: Part 4: Creating Parser for Variable Assignment and Printing
Till now we did the bare minimum for calculator with variable assignment. We have understood most of the basic concept in parsing let statement and printing statement. The last article should be enough to start parsing most of the statements. Addition of if and while statement is just for a quick revision to boost confidence.
While statement
The generic syntax of While Statement is:
while (condition) {
statememt1; # blocks
statement2;
}
We can have list of statement inside the while. We call it block. it is quite common to see block of code in looping and branching statements or functions. We write a code for it first so we don’t have to rewrite it every time we define a new segment. Lets create a node with list of statement.
The eval
for Block statement is same as interpreter. We go through each statement one by one and execute the code. Now lets create a parser for block statement. It is a…