Presentation is loading. Please wait.

Presentation is loading. Please wait.

PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.

Similar presentations


Presentation on theme: "PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented."— Presentation transcript:

1 PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented Language

2 PrasadL145OOL2 Interpret = Compile + Run Compiler OOPL programobject code Parser Interpreter Scheme System OOPL program Abstract syntax Abstract machine

3 PrasadL145OOL3 An object is a collection of operations that share state. The object exists at run-time. A class is a textual description of the state variables ( fields ) and the operations ( methods ). A class is an implementation of an ADT. A module is a syntactic mechanism for grouping related elements, and forms the basis for enforcing information hiding.

4 PrasadL145OOL4 Introducing objects and classes into the Language Class definition (via Inheritance) –class variables (** not supported but can be easily incorporated **) –instance variables (state) –assignments (state changes) –method definitions –method invocations –initialization Object creation (instantiation)

5 ( define the-grammar ’( (program ((arbno class-decl) expression) a-program)... (class-decl ("class" identifier "extends" identifier (arbno "field" identifier) (arbno method-decl) ) a-class-decl) (method-decl ("method" identifier "(" (separated-list identifier ",") ")" expression) a-method-decl) (expression ("new" identifier "(" (separated-list expression ",") ")") new-object-exp) (expression ("send" expression identifier "(" (separated-list expression ",") ")") method-app-exp) (expression ("super" identifier "(" (separated-list expression ",") ")") super-call-exp) ) ) PrasadL145OOL5 Additional Syntax

6 PrasadL145OOL6 Example : Object and Class class c1 extends object field i field j method initialize (x) begin set i = x; set j = -(0,x) end method countup (d) begin set i = +(i,d); set j = -(j,d) end method getstate () list(i,j) let t1 = 0 t2 = 0 o1 = new c1(3) in begin send o1 countup (2) set t1 = send o1 getstate () list(t1,t2) end

7 PrasadL145OOL7 Example : Message Passing Style class interior_node extends object field left field right method initialize (l, r) begin set left = l; set right = r end method sum () +(send left sum(), send right sum()) class leaf_node extends object field value method initialize (v) set value = v method sum () value let o1 = new leaf_node(3) o2 = new interior_node( o1, o1) in send o2 sum()

8 PrasadL145OOL8 Example : Inheritance class point extends object field x field y method initialize (ix, iy) begin set x = ix; set y = iy end method move (dx, dy) begin set x = +(x,dx); set y = +(y,dy) end method getlocation () list(x,y) class colorpoint extends point field color method getcolor () color method setcolor ( c ) set color = c let cp = new colorpoint(10,20) in begin send cp move(5,5) send cp getlocation () end

9 PrasadL145OOL9 Example : Shadowing variables class c1 extends object field y method initialize () 1 method sety1 (v) set y = v method gety1 () y class c2 extends c1 field y method sety2 (v) set y = v method gety2 () y let o2 = new c2 () in begin send o2 sety1(5) send o2 sety2(25) send o2 gety1() send o2 gety2() end

10 PrasadL145OOL10 Example : Static vs Dynamic binding class c1 extends object method initialize () 0 method m1 () 1 method m2 () send self m1() class c2 extends c1 method m1 () 2 let o1 = new c1 () o2 = new c2 () in list ( send o1 m1(), send o2 m1(), send o2 m2() )

11 PrasadL145OOL11 Example : Need for Static Dispatch class point extends object field x field y method initialize (ix, iy) begin set x = ix; set y = iy end... class colorpoint extends point field color method initialize (ix, iy, ic) begin set x = ix; set y = iy; set color = ic end... let cp = new colorpoint(10,20,128) in send cp getcolor () super initialize (ix,iy)

12 PrasadL145OOL12 Example : Interaction of self and super class c1 extends object method initialize () 0 method m1 () send self m2() method m2 () 13 class c2 extends c1 method m1 () 22 method m2 () 23 method m3 () super m1 () class c3 extends c2 method m1 () 32 method m2 () 33 let o3 = new c3 () in send o3 m3()

13 PrasadL145OOL13 Modifying the Interpreter ( define (eval-program pgm) (cases program pgm a-program (c-decls exp) (a-program (c-decls exp) (elaborate-class-decls! c-decls) (eval-expression exp (init-env)) ) )) elaborate-class-decls stores information about classes for later use.

14 PrasadL145OOL14 (cont’d) ( define (eval-expression exp env) (cases expression exp... method-app-exp (obj-exp meth-name rands) (method-app-exp (obj-exp meth-name rands) (let ((args (eval-rands rands env)) (obj (eval-expression obj-exp env)) (find-method-and-apply meth-name ( object->class-name obj) obj args) ) )) Call by value: Operands evaluated in calling context. Dynamic binding: Methods to be searched in the object’s class. Third argument of find-method-and-apply binds to self.

15 PrasadL145OOL15 (cont’d) ( define (eval-expression exp env) (cases expression exp... super-call-exp (meth-name rands) (super-call-exp (meth-name rands) (let ((args (eval-rands rands env)) (obj (apply-env env 'self))) (find-method-and-apply meth-name ( apply-env env '%super) obj args) ))) Precondition : the current object is bound to self and the parent of the class of the call is bound to %super. Method name searched from %super class. Third argument of find-method-and-apply will be associated with self enabling dynamic binding of other method calls in the super-method body.

16 PrasadL145OOL16 Note that due to the preceding clause, a method defined in a (parent) class can be invoked on an instance of its (descendant) subclass. (Coercion) (Polymorphism) Later on observe that the list of instance variables has variables from more specific (sub)-class appearing after those of less specific (super)- class. Later on observe that the vector of values has values for variables due to more specific (sub)- class at a higher index than those due to less specific (super)-classes.

17 PrasadL145OOL17 (cont’d) ( define (eval-expression exp env) (cases expression exp... new-object-exp (class-name rands) (new-object-exp (class-name rands) (let ((args (eval-rands rands env)) (obj (new-object class-name))) (find-method-and-apply 'initialize class-name obj args) obj) ) )) Access class template, allocate storage, run suitable initializer, and finally return the object (in a well-defined state).

18 PrasadL145OOL18 Representation of instances (define-datatype object object? (an-object (class-name symbol?) (fields vector?) ) ) (define (new-object c-name) (an-object c-name (make-vector (class-name->field-length c-name)) ) ) An instance record contains its class name and a vector of values for instance variables.

19 PrasadL145OOL19 Representation of classes (define-datatype class class? (a-class (class-name symbol?) (super-name symbol?) (field-length integer?) (field-ids (list-of symbol?)) (methods (list-of method?)) )) with newly introduced ones appended to those inherited from the parent reflecting method overridingA class record contains class name, parent name, field count, a list of instance variable names (with newly introduced ones appended to those inherited from the parent), and a method environment containing method definitions (reflecting method overriding).

20 PrasadL145OOL20 (cont’d) (define (elaborate-class-decls! c-decls) (initialize-class-env!) (for-each elaborate-class-decl! c-decls)) (define the-class-env '()) (define (initialize-class-env!) (set! the-class-env '())) (define (add-to-class-env! class) (set! the-class-env (cons class the-class-env))) (define (lookup-class name)...)

21 PrasadL145OOL21 (cont’d) (define (elaborate-class-decl! c-decl) (let ((super-name ( class-decl->super-name c-decl))) (let ((field-ids (append (class-name->field-ids super-name) (class-decl->field-ids c-decl)))) (add-to-class-env! (a-class (class-decl->class-name c-decl) super-name (length field-ids) field-ids (roll-up-method-decls c-decl super-name field-ids) ) ) ))) Inherited fields precede newly introduced fields. roll-up-method-decls also takes complete list of instance variable names.

22 PrasadL145OOL22 Representation of a Method (define-datatype method method? (a-method (m-decl method-decl?) (s-name symbol?) (field-ids (list-of symbol?))) The method record stores the method definition, the superclass name, and the list of instance variable names (including inherited fields). The method definition contains method name, list of formals, and body expression.

23 (define (roll-up-method-decls c-decl super-name field-ids) (merge-methods (class-name->methods super-name) (map (lambda (m-decl) (a-method m-decl super-name field-ids) ) (class-decl->method-decls c-decl) ) ) ) Inherited methods precede newly introduced methods. (cf. instance fields) PrasadL145OOL23 (Forming method environment)

24 PrasadL145OOL24 (cont’d) (define (merge-methods super-meths meths) (if (null? super-meths) meths (let((ovrd-meth (lookup-method (method->method-name (car super-meths)) meths))) (if (method? ovrd-meth) (cons ovrd-meth (merge-methods (cdr super-meths) (remove-method ovrd-meth meths))) (cons (car super-meths) (merge-methods (cdr super-meths) meths))) )) Overriding method definition replaces the “potentially inheritable” definition. Inherited methods precede newly defined methods.

25 PrasadL145OOL25 (Method application) (define (find-method-and-apply m-name host-name self args) (let ((method (lookup-method m-name (class-name->methods host-name)) )) (if (method? method) (apply-method method host-name self args) (eopl:error 'find-method-and-apply "No method for name ~s" m-name)) )) apply-method provides environments for variable bindings – the current class, the current object, and the actual arguments. In particular, bindings for instance variables and formal arguments are required.

26 PrasadL145OOL26 (define (apply-method method host-name self args) (eval-expression (method->body method) (extend-env (cons '%super (cons 'self (method->ids method))) (cons (method->super-name method) (cons self args)) (extend-env-refs (method->field-ids method) (object->fields self) (empty-env)) ) ) ) Constructs suitable environment for method evaluation. (Changing environment)


Download ppt "PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented."

Similar presentations


Ads by Google