1 / 26

Object-Oriented Language

Object-Oriented Language. Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Interpret = Compile + Run. OOPL program. Compiler. object code. Parser. Interpreter. Scheme System. OOPL program. Abstract machine.

freja
Download Presentation

Object-Oriented Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Interpret = Compile + Run OOPL program Compiler object code Parser Interpreter Scheme System OOPL program Abstract machine Abstract syntax L14-5OOL

  3. 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. L14-5OOL

  4. 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) L14-5OOL

  5. Additional Syntax (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) ) ) ) L14-5OOL

  6. 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 L14-5OOL

  7. 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() L14-5OOL

  8. 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 L14-5OOL

  9. 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 L14-5OOL

  10. 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() ) L14-5OOL

  11. 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) L14-5OOL

  12. 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() L14-5OOL

  13. Modifying the Interpreter (define (eval-program pgm) (cases program pgm (a-program (c-declsexp) (elaborate-class-decls! c-decls) (eval-expression exp (init-env)) ) )) • elaborate-class-declsstores information about classes for later use. L14-5OOL

  14. (cont’d) (define (eval-expression exp env) (cases expression exp . . . (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. L14-5OOL

  15. (cont’d) (define (eval-expression exp env) (cases expression exp . . . (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 selfenabling dynamic binding of other method calls in the super-method body. L14-5OOL

  16. 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. L14-5OOL

  17. (cont’d) (define (eval-expression exp env) (cases expression exp . . . (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). L14-5OOL

  18. 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. L14-5OOL

  19. 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?)) )) • A 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). L14-5OOL

  20. (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) . . .) L14-5OOL

  21. (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. L14-5OOL

  22. 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. L14-5OOL

  23. (Forming method environment) (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) L14-5OOL

  24. (cont’d) (define (merge-methods super-methsmeths) (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-methmeths))) (cons (car super-meths) (merge-methods (cdr super-meths) meths))) ) )) • Overriding method definition replaces the “potentially inheritable” definition. Inherited methods precede newly defined methods. L14-5OOL

  25. (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. L14-5OOL

  26. (Changing environment) (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 selfargs)) (extend-env-refs (method->field-ids method) (object->fields self) (empty-env)) ) ) ) • Constructs suitable environment for method evaluation. L14-5OOL

More Related