Project 2b

Due Date: Mon 21 Nov

Pair Project

Objectives

Application of design patterns

  • Composite pattern
  • Visitor pattern/recursive functions
  • Interpreter pattern

Study of object-oriented language mechanisms

  • Interpreter operating on abstract syntax trees
  • Runtime representation of objects in the presence of inheritance
  • Dynamic binding of methods
  • Reference to current receiver (this) in methods
  • Static binding of instance variables and methods invoked through super

Description

Your job is to extend an interpreter for a simple, dynamically typed Smalltalk-like object-oriented language called miniool. This interpreter is an extension of the interpreter for the simple imperative language with records you have already seen in project 2a. The interpreter skeleton and test cases are available in the course repository.

The language miniool is based on the simple imperative language. It adds the following concepts:
  • classes
  • instances
  • (public) methods
  • (protected) instance variables (a.k.a. fields)
  • l-values (storage locations, including variables and instance variables)
and the following syntactic constructs:
  • statement for new instance creation
  • statement for instance variable selection
  • statement for method invocation (call/message)
  • if statement
Besides the classes for the various constructs, you are given a visitor Execute for evaluating a statement down to an l-value. Your specific assignment is described in the following. It is suggested that you proceed with each portion of the project in the order given here.

Completion of given code

Complete the missing code marked "your job" in execute.scala. Once you have done that, some of the given unit tests should work as expected. Until you complete this part, you will get failures in most of the unit tests!

You must not make any other changes to the given code other than the ones specified below!

MyInt class

Complete the Clazz MyInt in TestMyInt to include the missing methods.

class myInt {
    // ...
    intValue() { /* the value as an Integer object */
    minus(that) { /* binary minus, returning a myInt */ }
    uminus() { /* unary minus, as in -7, returning a myInt */ }
    times(that) { /* multiplication returning a myInt, using itimes */ }
}

Note that the methods init, itimes, and intValue return an Int, while all other methods (plus, minus, uminus, and times) return a new instance of MyInt.

Furthermore, implement the following expression in the Statement that uses the myInt class in TestMyInt.

v = z.minus(10).times(4).uminus().times(3).minus(7).intValue();

Fibonacci class

Complete the (linearly recursive) fib method in the myInt class in FibonacciTest. Observe that fib(0) = 0, fib(1) = 1, fib(n) = fib(n - 1) + fib(n - 2) where n > 1. 

Documentation

It is not necessary to write any additional scaladoc comments for this project since this project description already serves as documentation for the various components. However, you need to generate the scaladoc. Furthermore, you are required to provide inline comments for your code where appropriate similar to those in Execute case Message.

How to submit

Please refer to the online submission procedure.
Comments