CS 3520 Homework 10 - Due November 15
Start with this code. Turn in a single interpreter with all of the revisions requested below.Exercise 10.1, An instanceof operator
Extend the grammar to support expressions of the form instanceof(expr, classname)The result of evaluating the expression should be 1 if expr produces an object that is an instance of classname (or one of its subclasses), 0 otherwise.Example use:
class c1 extends object
method initialize () 0
class c2 extends object
method initialize () 0
class c3 extends c1
let o1 = new c3()
in list(instanceof(o1, c1),
instanceof(o1, c2),
instanceof(o1, c3))
=> _(1 0 1)_
[This is EoPL exercise 6.10, page 252]Exercise 10.2, Field bindings
In our language, the envieonmtn for a method includes bindings for all the field variables declared in the host class and its superclasses. Modify the language so that the environment contains bindings only for the field variables of the host class. In other words, make field bindings behave like C++ private fields.Example:
class c1 extends object
field x
method initialize (v) set x = v
class c2 extends c1
method m () x
send new c2(1) m()
=> error, used to be _1_
[This is EoPL exercise 6.11, page 253]Exercise 10.3, Overloading
Object-oriented languages frequently allow overloading of methods. This feature allows a class to have multiple methods with the same name, provided they have different signatures.A method's signature is typically the method name plus the types of the parameters. Since we do not have types in our current language, we might overload based simply on the method name and number of parameters. For example, a class might have two initialize methods, one with no parameters for use when initialization with a default field value is desired, and another with one parameter for use when a particular field value is desried.Extend the interpreter to allow overloading based on the number of method parameters.Example:
class c1 extends object
field x
method initialize () set x = 7
method initialize (v) set x = v
method get_x () x
method get_x (n) +(x, n)
let o1 = new c1 ()
o2 = new c1 (2)
in list(send o1 get_x(3),
send o2 get_x())
=> _(10 2)_
[This is EoPL exercise 6.12, page 253]Exercise 10.4, The Object Class
In our interpreter so far, the class object is a special case. Since it is not explicitly represented in the class environment, and therefore has no initialize method, it cannot be instantiated with new.Place a class whose name is object into the initial class environment. Give the class object an initialize method (which takes no argument) so that it is possible to create an object of class object.Example:
new object()
=> _((a-part object #0()))_
If the initialize method is called directly after the object is created, it can return anything.Hint: %void is not a legal identifier in our interpreter language, but '%void is a legal Scheme symbol.[This is EoPL exercise 6.19, page 254]
Last update: Wednesday, November 15th, 2000mflatt@cs.utah.edu |