Makevisitors: Pass 1
Pass 1: Generate Java enum
s from simple Haskell data
types
We define a simple Haskell data type to be a data type where all of the cases are just tags. In other words, none of the tags for the data type have associated values.
An example of a simple Haskell data type is Op
, from haskell/expr-test.hs
:
data Op = Plus | Minus | Times | Divide
Simple data types don’t require the complex Java code we will generate for Haskell
constructors that take arguments. Instead, we can just produce
a Java enum
like so:
public enum Op {PLUS, MINUS, TIMES, DIVIDE};
To transform a simple Haskell data
type to a Java enum
, we need to write a
function that will take something such as this Haskell AST:
Datatype "Op" [ Plain "Plus" []
, Plain "Minus" []
, Plain "Times" []
, Plain "Divide" [] ]
and transform it to something this Java AST:
EnumDecl "Op" ["PLUS", "MINUS", "TIMES", "DIVIDE"]
Your task: In Translate.hs
, complete the implementation of the first pass.
- Understand what the
genEnums
function and how it does it. - Complete the implementation of
genEnum
.
After you complete this pass, you should be able to generate enum
s for a
Haskell file that contains only simple datatypes.
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):genVisitorInterfaces
genAbstractSuperclasses
genConcreteSubclasses
- Write a sample file that contains only simple data types and check that the generated code matches your expectations.
- Run
make test
, which will generate code for lots of different examples. Check that the simple data types are translated as you expected. You can compare your code against the code generated by the reference implementation, specifically:exprTest/Op.java
lambdaTest/Op.java
randomTest/Footware.java
randomTest/Suit.java
stack/Op.java
These files should match yours. (The other files were generated by a complete implementation, whereas you currently have a partial implementation.)