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 thevarnode should have no children. For consistency, you can relax the requirement that avarstatement have at least one variable; it looks kind of silly to have avarstatement with no variables, but it's harmless.
willy.x?
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"call".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 adostatement, the value is ignored, but the function doesn't know that.
x,
y, and angle?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 avarstatement, with an initial value of0.0, or are passed in to functions as parameters. They are kept in a HashMap. Special variables are not declared in avarstatement 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 thestore(name, value)method.
- The
fetchmethod should get the value of special variables from the instance variables, and get the value of ordinary variables from a HashMap.- The
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
RuntimeException.The
fetchandstoremethods will need to be extended when we deal with functions. Until then, they work as described above.
Color.BLACK.
How do I exit from a loop?All you need is a simple boolean variable (say,Is theboolean exitingLoop), initallyfalse.
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 becomestrue. (Then reset it tofalse.)- When you interpret an
exit ifstatement, set the boolean variable totrue.- When you interpret a list of statements, exit the list when the boolean variable becomes
true.exit ifstatement exits only the immediately enclosing loop.
exit if statement only allowed inside
a loop statement?
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 anexit ifstatement as described above, and theexit ifis not within aloop, it should end interpretation of the entire<program>. I think this is reasonable behavior.