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

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects.
Chapter 5: Objects and Classes class Point(Object): def __init__(x, y): self.x = initx self.y = inity def move(dx, dy): x += dx; y += dy; p = Point(10,
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Cs784(Prasad)L123Assg1 Assignments. cs784(Prasad)L123Assg2 l-value vs. r-value Pascal/Ada: x := x + 1 C/Java: x = x + 1 l-value = location, address, reference,
EOPL3: Section 3.3 PROC and App B: SLLGEN
1 Programming Languages (CS 550) Lecture 5 Summary Object Oriented Programming and Implementation* Jeremy R. Johnson *Lecture based on notes from SICP.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Environment: Data type associating symbols with values { a: 4s:'foof: x.x } Interface: (empty-env) = [ Ø ] (apply-env [f] s ) = f(s) (extend-env '( s 1...
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
5.4.1: Implementation Method Invocation (define eval-expression (lambda (exp env) (cases expression exp... (method-app-exp (obj-exp method-name rands)
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Operational Semantics of Scheme
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Abstract Syntax cs7100 (Prasad) L7AST.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
Implementing Recursion
Types of Programming Languages
5.4.1: Implementation Method Invocation
Mini Language Interpreter Programming Languages (CS 550)
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
FP Foundations, Scheme In Text: Chapter 14.
Abstract Syntax Prabhaker Mateti 1.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
Procedures App B: SLLGEN 1.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
Lecture 12: Message passing The Environment Model
6.001 SICP Variations on a Scheme
Inheritance in Java CS 3331 Fall 2009.
Assignments cs7100(Prasad) L13Assg.
Assignments and Procs w/Params
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
Presentation transcript:

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

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

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.

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)

( 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

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

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

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

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

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

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)

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

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.

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.

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.

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.

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

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.

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

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

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.

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.

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

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.

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.

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)