Com S 342 --- Principles of Programming Languages EXERCISE 14: REVIEW (File $Date: 2005/04/28 03:41:07 $) (This isn't a real exercise in the usual sense, but something of a record of the programming problems from the discussion sections, after the fact.) The purpose of this exercise is to to learn more about implementation and semantics of object-oriented languages, through the interpreter. The specific objectives are to be able to understand the output of an example of object-oriented code, understand how to traverse the subclass hierarchy, and (a little) understanding how fields are stored in objects. As with all exercises, this is to be done individually. And it is due the day this topic is planned to be discussed in class, unless specified otherwise (see the syllabus at: http://www.cs.iastate.edu/~cs342/syllabus.shtml). We expect this to help you think about the readings (see below). If you don't have an answer or an answer that you think is good, write down what you read, and a question or two that would have helped you make progress. Then you can ask that question in class; there will be other people with the same problem, and everyone can learn by discussing these issues. And you'll most likely see similar things on the homework, so it's best to understand them now. READINGS: Read chapter 5 of "Essentials of Programming Languages" (2nd ed., 2001) by Friedman, Wand, and Haynes. 1. [clone-upto] Here's a suggested practice problem like the one Brian discussed in discussion section. Implement an expression with the syntax: ::= clone upto where an expression of the form clone E upto C evaluates the expression, E, which should yield an object, o. If o's dynamic class, call it D, is a subclass of the class, C, named in the clone-upto expression, then the clone-upto expression itself returns a new object with dynamic class C, and whose fields are initialized to be a copy of the fields of o (that are present in class C). Note that in this case, the initialize method of class C is not run. If o has a dynamic class that is not a subclass of C, then an error message is issued. For example: --> class c1 extends object field x method initialize () set x = 0 method setx(v) set x = v method getx() x let o = new c1() in begin send o setx(3); let o2 = clone o upto c1 in list(send o getx(), send o2 getx()) end (3 3) --> class grandma extends object field x method initialize (v) set x = v method getx() x method whoami() quote grandma class yomama extends grandma field y method initialize (xv, yv) begin super initialize(xv); set y = yv end method gety() y method whoami() quote yomama let orig = new yomama(3, 42) in let copy = clone orig upto grandma in list(send orig whoami(), send copy whoami(), send orig getx(), send orig gety(), send copy getx()) (yomama grandma 3 42 3) 2. [typecase] Here's a suggested practice problem like the one Daniel discussed in discussion section. Implement an expression with the syntax: ::= typecase {when ( ) }* end where an expression of the form typecase E when C1 (f1) E1 ... when Cn (fn) En end evaluates the expression, E, which should yield an object, o. Let D be the dynamic class of o. If D is a subclass of C1, named in the first when clause, then the typecase expression itself returns the value of E1 in an environment where f1 is bound to the object o. Otherwise the same process is tried with all of the when clauses. In general, if i is the index of the first when clause for which Ci is a subclass of D (o's dynamic class), then the value of the typecase expression is the result of evaluating the ith body, Ei, in an environment where fi is bound to the object o. If D is not a subclass of any of the Ci, then the expression gives an error message ("Typecase falls off end!"). For example: --> class c1 extends object method initialize () 1 typecase new c1() when c1 (o1) quote c1 when object (o) quote object end c1 --> class c1 extends object method initialize () 1 typecase new c1() when object (o) quote object when c1 (o1) quote c1 end object --> class grandma extends object method initialize () 1 method whomightibe() quote grandma class yomama extends grandma method initialize () 2 method whoami() quote yomama class kid extends yomama method initialize () 3 method yo() quote kid list(typecase new kid() when kid (k) send k yo() when yomama (ym) send ym whoami() when grandma (gm) send gm whomightibe() end, typecase new yomama() when kid (k) send k yo() when yomama (ym) send ym whoami() when grandma (gm) send gm whomightibe() end, typecase new grandma() when kid (k) send k yo() when yomama (ym) send ym whoami() when grandma (gm) send gm whomightibe() end, typecase new yomama() when grandma (gm) send gm whomightibe() when yomama (ym) send ym whoami() when kid (k) send k yo() end) (kid yomama grandma grandma) --> class c extends object method initialize () 1 typecase new c() end Error reported by eval-expression: Typecase falls off end! WHAT TO HAND IN You should have at the beginning of class, your answers to the above questions (or questions and problems you encountered for each part). Make sure your name is on these. Attach the printouts, if any, requested above.