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 a var node for its parameters. However, it is possible to declare a function without any parameters, in which case the var node should have no children. For consistency, you can relax the requirement that a var statement have at least one variable; it looks kind of silly to have a var statement with no variables, but it's harmless.
How do we handle dot notation, for example, 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, use "call".

There is no difference between a function call with do and 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 do statement, the value is ignored, but the function doesn't know that.

How do I handle the "special" variables x, y, and angle?

These should be instance variables of your Bug class, and your Java code can access them in the usual way.

Ordinary variables are declared in a var statement, 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 var statement 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 store(name, value) method.

If an ordinary variable is used without first having been declared (and is therefore not in the HashMap), you should throw a RuntimeException.

The fetch and store methods will need to be extended when we deal with functions. Until then, they work as described above.

What should the default (initial) "color" instance variable be set to?
How do I exit from a loop?
All you need is a simple boolean variable (say, boolean exitingLoop), initally false.
Remember that loops can be nested, and be sure to test that an exit if statement exits only the immediately enclosing loop.
Is the 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 an exit if statement as described above, and the exit if is not within a loop, it should end interpretation of the entire <program>. I think this is reasonable behavior.