Object oriented language
This presentation is the property of its rightful owner.
Sponsored Links
1 / 26

Object-Oriented Language PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

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.

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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


object code





OOPL program

Abstract machine

Abstract syntax


  • 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.


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)


Additional Syntax

(define the-grammar ’(

(program ((arbno class-decl) expression)a-program)

. . .


("class" identifier "extends" identifier

(arbno "field" identifier) (arbno method-decl) )a-class-decl)


("method" identifier

"(" (separated-list identifier ",") ")" expression)a-method-decl)


("new" identifier "(" (separated-list expression ",") ")")new-object-exp)


("send" expression identifier "(" (separated-list expression ",") ")")method-app-exp)


("super" identifier "(" (separated-list expression ",") ")")super-call-exp) )

) )


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 ()




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()


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 ()



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()



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() )


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 ()





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()


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.



(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))


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.



(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)))


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.


  • 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.



(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)))


'initialize class-name obj args)




  • Access class template, allocate storage, run suitable initializer, and finally return the object (in a well-defined state).


Representation of instances

(define-datatype object object?

(an-object (class-name symbol?)

(fields vector?) )


(define (new-object c-name)

(an-object c-name



c-name)) )


  • An instance record contains its class name and a vector of values for instance variables.


Representation of classes

(define-datatype class 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).



(define (elaborate-class-decls! c-decls)



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) . . .)



(define (elaborate-class-decl! c-decl)

(let ((super-name (class-decl->super-name c-decl)))

(let ((field-ids


(class-name->field-ids super-name)

(class-decl->field-ids c-decl))))



(class-decl->class-name c-decl)


(length field-ids)



c-decl super-name field-ids) )



  • Inherited fields precede newly introduced fields.

  • roll-up-method-decls also takes complete list of instance variable names.


Representation of a Method

(define-datatype method 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.


(Forming method environment)

(define (roll-up-method-decls

c-decl super-name field-ids)


(class-name->methods super-name)


(lambda (m-decl)


m-decl super-name field-ids)


(class-decl->method-decls c-decl)




  • Inherited methods precede newly introduced methods. (cf. instance fields)



(define (merge-methods super-methsmeths)

(if (null? super-meths) meths



(method->method-name (car



(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.


(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.


(Changing environment)

(define (apply-method method host-name self args)

(eval-expression (method->body method)


(cons '%super

(cons 'self(method->ids method)))

(cons (method->super-name method)

(cons selfargs))


(method->field-ids method)

(object->fields self)

(empty-env)) ) )


  • Constructs suitable environment for method evaluation.


  • Login