Assignment 8: Metaprogramming
Overview
In this assignment, you will write a Haskell-to-Java compiler!
In particular, you will translate Haskell data type definitions to the corresponding Java definitions, with support for the visitor pattern.
More than any other assignment we have done before, most of the work for this assignment happens in your head. The challenge is to understand programming languages at many levels—concrete and abstract syntax—and to understand how all the parts of the visitor pattern work together to simulate pattern-matching.
There is quite a bit to read here. Start early. Skim over things so that you know what information is available to you, then dive in and read in more detail.
Materials
Use the assignment workflow and the link provided on Piazza to get access to this week’s files.
Part 1: Understand makevisitors
Your Haskell-to-Java compiler is called makevisitors
.
The makevisitors
program translates Haskell data types to equivalent Java classes.
Furthermore, the Java code supports the visitor pattern, so that people can add new
operations, without modifying the generated Java code.
Your task: Learn about what makevisitors
does and how it
works, so that you will know what we as asking you to implement. Refer back
to this description as you work.
Part 2: Familiarize yourself with the provided code
To implement your code-generator, you will build on the provided code. The most important files and directories to understand are the ones that translate from the Haskell AST to the Java AST and the ones that build the full program:
HaskellAST.hs
: the AST for Haskell declarationsJavaAST.hs
: the AST for Java declarationsTranslate.hs
: transforms Haskell AST to Java ASThaskell
: a directory with some example Haskell declarationsjava
: a directory wheremakevisitor
will place generated Java codeMakefile
: builds themakevisitor
program and runs it on examples
Your task: Read over this short description of the provided code before you start your implementation. Refer back to it as you work.
Part 3: Implement makevisitors
As you work, be sure to use good programming practices: choose descriptive variable names, document your code, define helper functions, and generally aim for readable code. Ten percent of your grade will be based on how well you follow these guidelines for code quality.
Although the Haskell code you write should be as professional as possible, the Java code
that makevisitors
generates is not required to be “industrial strength” Java. In
particular, note that:
-
All generated classes declare the fields to be public (rather than making them private and generating getters and setters).
-
There are few comments.
-
The generated code does not use the
@Override
annotation when overriding a method.
Your task: Implement makevisitors
in the following order, referring to the linked pages for more details:
- Implement pass 1: Generate Java
enum
s from simple Haskelldata
types. - Implement pass 2: Generate the Java visitor
interface
s. - Implement pass 3: Generate the Java
abstract
class
es. - Implement pass 4: Generate the Java concrete
class
es.
Part 4: Bring it all together
Your task: Implement a visitor and use your generated code.