Makevisitors: Stack Visitor
At this point in the assignment, makevisitors
can generate a Java class
hierarchy and visitor from the data type definitions in haskell/stack.hs
. Now,
we will implement an evaluator for the stack machine. To do so, we need to
implement a StackInstrVisitor
.
The file java/ExampleStackProgram.java
contains a partial implementation of a
StackInstrVisitor
, along with a program that evaluates a sample list of stack
instructions. Your job is to complete the StackInstrVisitor
implementation.
Your task: complete the StackInstrVisitor
implementation by filling in the
implementation for the visit
methods.
Comments.
- If you understand the visitor pattern well, this should be a straightforward implementation.
- You may want to review the lab, to brush up on how to write new visitors.
- Here are the semantics of the instructions, copied from our assignment earlier in the semester. Keep in mind that these rules are defined using the Haskell data type names.
If the stack looks like | and the operation is | afterwards the stack should be |
---|---|---|
… | Push $r$ |
$r$ … |
$a$ $b$ … | DoOp PlusOp |
$(b+a)$ … |
$a$ $b$ … | DoOp MinusOp |
$(b-a)$ … |
$a$ $b$ … | DoOp TimesOp |
$(b*a)$ … |
$a$ $b$ … | DoOp DivOp |
$(b/a)$ … |
$a$ $b$ … | Swap |
$b$ $a$ … |
-
To check that everything works, you can run the following command from the top-level code directory:
make stack
If you see the word
SUCCESS
, then your Java code compiled and produced the correct answer. You’re all done!