Makevisitors: Pass 2
Generate the Java visitor interface
s
If a Haskell data type is not simple, then we will call it complex. A complex Haskell data type has at least one tag that has associated values. For a complex data type, we will generate a class hierarchy and a visitor interface. In this pass, we’ll focus on the visitor interface.
Here is an example of a complex Haskell data type, and its corresponding abstract syntax:
-- concrete Haskell declaration
data Expr = Value {value :: Integer}
| BinOp {operator :: Op, leftOperand :: Expr, rightOperand :: Expr}
-- abstract syntax (an instance of TyDefs)
[Datatype "Expr" [ Record "Value" [ ("value", Base "Integer") ]
, Record "BinOp" [ ("operator", Base "Op")
, ("leftOperand", Base "Expr")
, ("rightOperand", Base "Expr") ]]
And here is a Java visitor for this datatype, along with its corresponding abstract syntax:
// concrete Java declaration
public interface ExprVisitor {
public void visitValue(Value valueVal);
public void visitBinOp(BinOp binOpVal);
}
-- abstract syntax (an instance of JDeclaration)
InterfaceDef "ExprVisitor" []
[ MethodDef [Public] (Typename "void") "visitValue" [(Typename "Value","valueVal")] NoBody
, MethodDef [Public] (Typename "void") "visitBinOp" [(Typename "BinOp","binOpVal")] NoBody ]
Your task: In Translate.hs
, complete the implementation of the second pass.
After you complete this pass, you should be able to generate Visitor
classes
for any complex data type. The Java code likely won’t compile because you
haven’t yet implemented the part that generates classes for that type.
Testing. It is largely on you to test your code. We suggest:
- In
makevisitors.hs
, comment out the calls for the unimplemented passes (just make sure to uncomment them as you implement them):genAbstractSuperclasses
genConcreteSubclasses
- Write a sample file that contains only complex data types and check that the generated visitors matches your expectations.
- Run
make test
, which will generate code for lots of different examples. Check that the visitors are generated as you expected. You can compare your code against the code generated by the reference implementation, specifically:exprTest/ExprVisitor.java
jsonTest/JValueVisitor.java
lambdaTest/ExprVisitor.java
lambdaTest/ValueVisitor.java
randomTest/FobleVisitor.java
randomTest/WidgetVisitor.java
stack/StackInstrVisitor.java
These files should match yours. (The other files were generated by a complete implementation, whereas you currently have a partial implementation.)