CSCI.4430/6969 Programming Languages Fall 2005
Programming Assignment #2

This assignment is to be done either individually or in pairs. Do not show your code to any other group and do not look at any other group's code. Do not put your code in a public directory or otherwise make it public. However, you may get all the help you need from the TAs or the instructor. You are encouraged to use the WebCT Discussions page to post questions so that other students can also answer/see the answers.

MMJ: Multi-methods extension to Java

The goal of this assignment is to implement dynamic method dispatching (aka multi-methods) for the Java language. In Java, which method to invoke is decided based on the declared type of the arguments when multiple methods may apply. For example, given a class C as follows:

public class C {
  public void m(Object o){ System.out.println("m1"); }
  public void m(String s){ System.out.println("m2"); }
}

and given some code calling the method m on an instance of C, as follows:

...
C c = new C();
Object argument = new String("hi");
c.m(argument);

Java will always print m1 while it is more natural to print m2 since the argument's run-time type is a String even though its declared type is an Object.

Your goal is to implement MMJ, a language with the same syntax as Java's, but with dynamic method selection based on the run-time type of the arguments.

Part 1 (50%): Simulating MultiMethods

Since Java does not directly support multiple method dispatching, you are to come up with a strategy to simulate multi-methods using Java's single method dispatching semantics.

Write a few MMJ classes which demonstrate the use of multimethods with inheritance. Be sure to have examples illustrating covariance and contravariance. Determine a generic algorithm for translating MMJ into Java. Provide code for both your example MMJ classes and the Java code they would be changed to after your source-to-source translation algorithm.

Hint: To simulate multimethods, you may use Java's support for reflection or you may use the instanceof construct.

Part 2 (50%): MultiMethods Code Generation

Use JavaCC to create a code generator which converts a .mmj file to a corresponding .java file using your algorithm from Part 1. An .mmj file uses the same grammar as Java, except the expected semantics are multimethods instead of Java's single method invocation semantics. Your code generator should convert .mmj files into .java files that can be executed with the expected semantics of multimethods.

Hint: You can download the Java2JavaParser which takes a Java file as input, converts it to an abstract syntax tree and outputs the same Java code (with improved indentation and no comments). You should be able to solve part 2 with only modification of the SimpleNode.java class. The Java 1.5 grammar might be helpful, in particular the MethodDeclaration and PrimaryExpression non-terminals.

Extra Credit (10% bonus if working individually, required for full credit if working in a pair):

Take into account access control (e.g., public, private, protected method modifiers) in your implementation of multimethods.

References:
MultiJava: Modular Open Classes and Symmetric Multiple Dispatch for Java
Parasitic Methods: An Implementation of Multi-Methods for Java
Maya: Multiple-Dispatch Syntax Extension in Java
The Java 1.5.0 API
The Java Language Specification
The JavaCC FAQ

Due Date:
Received Time Grade Modification
before Monday, October 17th, 11:59PM +10%
Tuesday, October 18th, from 12:00AM to 11:59PM no modification (on time)
Wednesday, October 19th, from 12:00AM to 11:59PM -10%
from Thursday, October 20th, 12:00AM to
Friday, October 21st, 11:59PM
-25%
after Saturday, October 21st, 12:00AM not accepted

The assignment will be graded mostly on correctness, but code clarity / readability will also be a factor (comment, comment, comment!). Be sure your solutions are robust.

Submission Requirements: Your code should consist of a ZIP file containing a README file and two subdirectories part1 and part2. Use your WebCT user name(s) as the filename, either userid1.zip or userid1_userid2.zip. Only submit one assignment per pair (the other does not need to submit anything via WebCT). Please submit your file via WebCT.