CS 3520 Homework 9 - Due November 21
Start with this code. Turn in a single interpreter with all of the revisions requested below.Exercise 9.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))
=> (list 1 0 1)
[This is EoPL exercise 5.11, page 199]Exercise 9.2, Field bindings
In our language, the environment 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.Hint: To keep a class's field list in line with the instance vectors, make up an inaccessible name for all of the hidden 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 5.12, page 199]Exercise 9.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())
=> (list 10 2)
[This is EoPL exercise 5.13, page 200]
Last update: Tuesday, November 12th, 2002mflatt@cs.utah.edu |