Extended from original date of Tuesday, 8 November
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.
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
a simple "object-oriented" approach with inheritance as discussed in the Lua Objects slide set
a similar approach susch as the one in the instructor's Prototype Object-Based version of the Arithmetic Expression Tree case study
the instructor's general Class Support Module from the Movable and Named Objects case study
some other similar approach of your choice or design
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.)
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.
Test this thoroughly (e.g., construct a test script similar to what the instructor did for Assignment #3).
Modify the Parser and REPL modules (and run script) to use your new module. Test this new interpreter using Imperative Core programs.
Submit you source code and test scripts to Blackboard. Be sure to indicate how to run your tests and programs.
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.
Background on object-oriented design:
SOLID Design Principles a look at the core SOLID principles through the lens of modularity.
"Uncle" Bob Martin video on "SOLID Principles of Object-Oriented and Agile Design"
Background examples:
Arithmetic Expression Tree examples, object-oriented versions. This skeleton for an assignment in another class is similar to one of the approaches to this assignment.
Natural Numbers example above, especially the object-oriented Java, Scala, and Ruby versions. (The Lua version was not implemented with object-oriented techniques in mind.)
Prototype-based example above (added after the assignment was given). This version does not implement classes or inheritance, but shows how a delegation to a prototpe object or cloning a prototype can accomplish much the same.
Complex Arithmetic, object-oriented version below, which supports both rectangular and polar coordinat systems
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.
class_support
module