Object-Oriented Language
Managing Environments
An Exercise in the Design, Analysis,
Specification, and Implementation of
the Core of an OOP Language.
Prasad
L145OOL
1
Interpret = Compile + Run
OOPL program
Parser
Compiler
Interpreter
object code
Scheme
System
OOPL program
Abstract syntax
Prasad
Abstract machine
L145OOL
2
• 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.
Prasad
L145OOL
3
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)
Prasad
L145OOL
4
Additional Syntax
(define the-grammar ’(
(program ((arbno class-decl) expression)
...
(class-decl
("class" identifier "extends" identifier
(arbno "field" identifier) (arbno method-decl) )
(method-decl
("method" identifier
"(" (separated-list identifier ",") ")" expression)
a-program)
a-class-decl)
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) )
))
Prasad
L145OOL
5
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
Prasad
L145OOL
6
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
Prasad
send o2 sum()
L145OOL
7
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
Prasad
L145OOL
8
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
Prasad
L145OOL
9
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 ()
in
list
o2 = new c2 ()
( send o1 m1(),
send o2 m1(),
send o2 m2() )
Prasad
L145OOL
10
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
super
method initialize (ix, iy, ic)
initialize
begin set x = ix; set y = iy;
(ix,iy)
set color = ic
end
. . .
let cp = new colorpoint(10,20,128)
in
send cp getcolor ()
Prasad
L145OOL
11
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()
Prasad
L145OOL
12
Modifying the Interpreter
(define (eval-program pgm)
(cases program pgm
(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.
Prasad
L145OOL
13
(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.
Prasad
L145OOL
14
(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 self enabling dynamic binding of other
method calls in the super-method body.
Prasad
L145OOL
15
• 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.
Prasad
L145OOL
16
(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).
Prasad
L145OOL
17
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.
Prasad
L145OOL
18
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).
Prasad
L145OOL
19
(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)
. . .)
Prasad
L145OOL
20
(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.
Prasad
L145OOL
21
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.
Prasad
L145OOL
22
(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)
Prasad
L145OOL
23
(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.
Prasad
L145OOL
24
(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.
Prasad
L145OOL
25
(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
self
args))
(extend-env-refs
(method->field-ids method)
(object->fields self)
(empty-env)) ) )
)
• Constructs suitable environment for method evaluation.
Prasad
L145OOL
26
Descargar

Object-Based Languages