CIT 594 Interpreter clarifications
Spring 2012, David Matuszek
Here are answers to some of the questions I have been asked about the interpreter assignment (plus a couple I made up myself
).
How do I interpret <something>?
The Interpreter is a seriously recursive program. Think recursively! At each node you do something, possibly involving its children, and that's all you do. Recursion takes care of the rest. But you have to do something different at each different kind of node.
To interpret a
program, you interpret itsblock.To interpret a
block, you interpret its children, one after the other, in order.To interpret an
ifstatement, you first evaluate itscondition; iftrue, you interpret its (first) block; iffalseand it has a secondblock(the "else" part), you interpret the secondblock, otherwise you don't do anything more.Et cetera. If the node is a kind of expression, you
evaluate()it; if it's a kind of "statement", youinterpret()it. Depending on the kind of statement, you may interpret or evaluate some or all of its children, and you may do so many times.
Should I modify the Board / Piece / RobotGui classes?
There is no reason to modify Board.java or Piece.java; they already do everything you need.
However, you should modify the RobotGui.initialize() method, to put assorted Pieces on the Board.
What should the Interpreter do when an error occurs?
In many cases you can just do something reasonable, and keep going. If the interpreter tries to get the value of a variable, and the variable hasn't yet been assigned a value, just give it the value zero. If the interpreter encounters a
repeat -1command, that can be treated asrepeat 0. If the interpreter tries to move the robot so far that it would go off the board, the robot should stop at the edge.A more difficult case is
call foo 1 2 3;when thefooprocedure has more or fewer than three parameters. The best thing to do is probably to put up a dialog box giving the user as much information as possible about the nature of the problem; then proceed as if the call never happened.
How should seeing diamond (or any other kind of thing) be evaluated?
Determine the direction that the robot is facing. Then find the first square in front of the robot that contains one or more objects. For example, if the robot is in (8, 3), meaning row 8, column 3, and is facing east (right), then examine (8, 4), then (8, 5), then (8, 6), etc. until you find a location containing one or more objects. Then return
trueifdiamondis one of those objects,falseif it isn't. Do not examine the square that the robot is on (it can't look down), and do not examine any squares past the first one containing objects.If no square in front of the robot contains any objects--that is, if it's looking at the edge of the board--return
false.
How should distance be evaluated?
This is the integer distance to whatever it is that the robot is
seeing. To be "standing on" the nearest <thing>, it should move forward exactly that number of squares. If the robot isn't seeing anything,(distance - 1)is the maximum distance that the robot can move without falling off the edge of the board.
What does the Pause button in the GUI do?
The method
interpreter.setPaused(boolean)should set a boolean variable in the Interpreter. Each timeinterpret()is called (which should happen many times a second), this variable is checked. If it istrue, the robot should pause (not do anything more) until the Run button in the GUI is clicked again. Here's one simple way to do that:while (paused) { try { sleep(100); } catch (InterruptedException e) { } }