Assignment #4
Due Thursday, 10 November, 11:59 p.m.

Extended from original date of Tuesday, 8 November

General Instructions

All homework and programming exercises must be prepared in accordance with the instructions given in the Syllabus. Each assignment must be submitted to Blackboard by its stated deadline.

"Object-Oriented" Abstract Syntax Tree

Note: Assignment #5 may overlap with this assignment.

The educational goals of this assignment are for each student to understand the development of prototype-object-based programs in Lua and to be able to compare that approach to the modular approach used in the instructor's interpreter design and the object-oriented approach that might be used in an object-oriented language like Java.

The technical goal of this assignment is to replace both the Abstract Syntax Tree and Evaluator modules of the Imperative Core interpreter with a single new module designed and implemented using the prototype-based "object-oriented" programming approach in Lua.

You may use one of several approaches

Your implementation should be enclosed within a Lua module for use within the interpreter and other purposes.

For example, one design would be to develop an Expression class with methods such as new (or make or init or some other constructor), toString, and eval and implement these appropriately in subclasses. The functionality of the existing constructors (e.g., Add) , exprToString function, and eval function can be absorbed into these methods on classes in the inheritance hierarchy.

You may want to introduce another level into the inheritance hierarchy to be able to reuse more of the code in related structures such as binary operators.

Once you have implemented and tested this new module, modify the Parser and REPL modules to use your new module. (This should be easy to do.)

Tasks

  1. Design and implement a prototype-object-based (i.e., "object-oriented") Lua module to replace the Abstract Syntax and Evaluator modules in the Imperative Core interpreter as described above.

  2. Test this thoroughly (e.g., construct a test script similar to what the instructor did for Assignment #3).

  3. Modify the Parser and REPL modules (and run script) to use your new module. Test this new interpreter using Imperative Core programs.

  4. Submit you source code and test scripts to Blackboard. Be sure to indicate how to run your tests and programs.

Comments (added 27 Oct, Refined 5 Nov)

This assignment expects the solution to use the "object-oriented" or "prototype-based" approach in Lua, as discussed in this course.

That is, each expression "subclass" (e.g., Add) should have an eval method that evaluates that particular type of expression object. The selection of which method to execute should not be determined by an if-then-else construct that does explicit type checks but by the "dynamic binding" of the method call to the appropriate implementation. This is the approach that is considered a "best practice" in an object-oriented language such as Java.

The examples below illustrate this in various languages.

The references below discuss design principles for object-oriented programs. In particualar, the open-closed principle is relevant.

  1. Background on object-oriented design:

  2. Background examples:

    The approach taken in the Complex Arithmetic implementation is a bit different from the the other object-oriented examples. The superclass has a constructor new and two "factory" methods. Each subclass implements the two "factory" methods that create complex numbers depending on the chosen coordinate system. The superclass factory methods call down to the like-named factory methods in the subclasses. This creates an unusual coupling down the class hierarchy that violates the open-closed principle for OO design. This structure should be reexamined in a future design.