Makevisitors: Provided code

Overview

  • In the top level of the provided directory, you’ll notice several Haskell files.
    • The only Haskell file you need to modify is Translate.hs.
    • You will want to be familiar with the contents of HaskellAST.hs and JavaAST.hs (more details below).
  • The Makefile in the top-level directory is to help you build your makevistors executable and run it on various examples.
  • As in the lab, there are also two subdirectories:
    • The haskell subdirectory contains sample files with data types that can be translated to Java.
    • The java subdirectory contains necessary library code and an example program that uses the results makevistors. It is also where the generated code will go.

HaskellAST.hs: Representing Haskel Data Types

The input to makevisitors is a collection of Haskell data type declarations (to be converted to Java visitor-pattern code); this input needs to be parsed and represented in Haskell.

In HaskellAST.hs you will find abstract syntax for (a subset of) Haskell type declarations. Read over HaskellAST.hs now!

Representing Haskell records

The abstract syntax supports one feature of Haskell that is likely new to you: records.

For example, we define an Expr data type where values are stored in records with named fields:

    data Expr = Value {value :: Integer}
              | BinOp {operator :: Op, leftOperand :: Expr, rightOperand :: Expr}

    data Op = Plus | Minus | Times | Divide

If you use makevisitor’s parser to parse these Haskell declarations, it will produce the following Haskell AST:

    [   Datatype "Expr" 
                 [  Record "Value" [  ("value", Base "Integer")  ]
                  , Record "BinOp" [  ("operator", Base "Op")
                                    , ("leftOperand",  Base "Expr")
                                    , ("rightOperand", Base "Expr")  ]]
     , Datatype "Op" 
                [  Plain "Plus"   []
                 , Plain "Minus"  []
                 , Plain "Times"  []
                 , Plain "Divide" [] ]                                            
    ]
                                         

For this assignment, we prefer specifications that use records because they provide names for all the data values, which turns out to be very handy when generating code to declare Java classes and fields.

JavaAST.hs: Representing the Output

The output from makevisitors is supposed to be human-readable Java files. The contents of these files need to be represented and pretty-printed.

The file JavaAST.hs contains abstract syntax for a very small subset of Java. The goal is for it to be able to express, in a meaningful way, the code for the Java declarations that we generate (i.e., the representation of the output for makevisitors). This file is complete enough to do the assignment.

Translate.hs: Generating Java code from Haskell code

Translate.hs is the file that you will modify for this assignment. It provides a way to take Haskell AST as input and generate the corresponding Java AST, with support for the visitor pattern.

Read over Translate.hs now, paying particular attention to the helper functions that appear before the passes.

Makefile: Building the program, running it on examples

The Makefile helps you build all the pieces of your code.

Try running the following commands in your top-level directory (i.e., the directory where Makefile appears):

  • make which builds your makevisitors executable.
  • make expr-test which runs the executable on haskell/expr-test.hs.

Skim through the Makefile to see what other things you can build.