CIT 594 Interpreter FAQ
Spring 2015, David Matuszek
This is a handful of notes that did not seem to fit easily in the Interpreter assignment. It may be updated from time to time.
How is it that a
<var declaration> requires at least one variable, but a
var node may have zero children?
A function declaration requires aHow do we handle dot notation, for example,
varnode for its parameters. However, it is possible to declare a function without any parameters, in which case the
varnode should have no children. For consistency, you can relax the requirement that a
varstatement have at least one variable; it looks kind of silly to have a
varstatement with no variables, but it's harmless.
Ignore it for Part 1. We'll deal with that in Part 2.I used
"do"as the string value in the root of the AST for
<do statement>. Is that correct?
No, useHow do I handle the "special" variables
There is no difference between a function call with
doand a function call as part of an expression, so there is no reason that the trees should be different. A function always returns a value. When a function is called from within an expression, the value is used in the expression. When the function is called from a
dostatement, the value is ignored, but the function doesn't know that.
What should the default (initial) "color" instance variable be set to?
These should be instance variables of your
Bugclass, and your Java code can access them in the usual way.
Ordinary variables are declared in a
varstatement, with an initial value of
0.0, or are passed in to functions as parameters. They are kept in a HashMap. Special variables are not declared in a
varstatement and cannot be used as formal parameters.
There are only two things you can do with a variable: fetch it (get its value) or store it (give it a new value). Every variable, special or not, that is used in a Bugs language program should be accessed via either the
fetch(name)method or the
fetchmethod should get the value of special variables from the instance variables, and get the value of ordinary variables from a HashMap.
storemethod should save the value of special variables into the instance variables, and put the value of ordinary variables into a HashMap.
If an ordinary variable is used without first having been declared (and is therefore not in the HashMap), you should throw a
storemethods will need to be extended when we deal with functions. Until then, they work as described above.
How do I exit from a loop?
All you need is a simple boolean variable (say,Is the
boolean exitingLoop), initally
Remember that loops can be nested, and be sure to test that an
- When you interpret a
loopstatement, exit the loop when this boolean variable becomes
true. (Then reset it to
- When you interpret an
exit ifstatement, set the boolean variable to
- When you interpret a list of statements, exit the list when the boolean variable becomes
exit ifstatement exits only the immediately enclosing loop.
exit ifstatement only allowed inside a
Ideally, yes, but that is impossible to require in the BNF, difficult to require in the Parser, and not particularly easy in the Interpreter, either.
When you interpret an
exit ifstatement as described above, and the
exit ifis not within a
loop, it should end interpretation of the entire
<program>. I think this is reasonable behavior.