-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
04 Statements #9
Conversation
Entry pointOur entry point to parsing statements is [eb280b4] |
OUTPUT statement parsingWe are setting up the skeleton here for parsing other kinds of statements too. Each kind of statement will have its own function for consuming the tokens it is looking for. And every statement ends with a line break ( [83ce629] |
More helper functionsWith some foresight, we can also see that some patterns here are going to be repeated a lot. In particular, I notice:
Pattern 1 Pattern 2 These two features are used to check for keyword tokens, which don't form part of the parsed Let's add two more helper functions to our menagerie of parser helpers: [0d0864c] |
BugfixNoticed a bug: https://github.com/nyjc-computing/pseudo/blob/0d0864cf7d756ab271ad4d20b991e8c48a656b6c/parser.py#L106-L108
Testing:
Result:
Oops, |
RefactorOur statement parsers now use the new helper functions to simplify their code: [1fc0ec9] |
The parsing loopWe have We need another parsing loop to keep parsing statements, until we run out of them. The EOF tokenOne problem: our statement-parsing functions also We shall mitigate this by introducing a new type of token: And we define an Introducing the parsing loop: [1e4ea82] |
Testing
Result:
|
Next stepsWe're still missing the last step. This is where we |
Statements
A statement is a declaration, a way of "placing" something on a record ... with words? In programming, we say that statements are a way of changing the state of the computer's internal data. Expressions are evaluated, their value extracted, and then they are forgotten; they came into being, produced a result, but never changed any data.
Statements, on the other hand, almost always change the state of the computer's data in some way, without necessarily returning any value. They are therefore not evaluated, but executed.
In 9608 pseudocode, statements look like the following examples:
DECLARE Pointer : INTEGER
OUTPUT "Customer name is ", "John Goh."
Index <- Index + 1
The general pattern in 9608 pseudocode, as far as I can tell, is that they never start with a value or symbol. They start with one or more keywords, or a name, and end with a line break (
\n
).We have
expression()
for parsing expressions. Here we implementstatement()
for parsing statements. We represented expressions withexpr
, and we'll usestmt
for representing statements. We will continue using dicts to represent them, and decide on the necessary keys when we figure out what we need.