5.4.1: Implementation Method Invocation

Slides:



Advertisements
Similar presentations
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Advertisements

Cs784(Prasad)L10Rec1 Implementing Recursion. cs784(Prasad)L10Rec2 let insufficient for recursion ( (let((fact(lambda (n) (if (zero? n) 1 (* n (fact (-
Closures & Environments CS153: Compilers Greg Morrisett.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let ): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question:
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.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
5.4.1: Implementation Method Invocation (define eval-expression (lambda (exp env) (cases expression exp... (method-app-exp (obj-exp method-name rands)
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:
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.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
Eight languages Eight weeks How I designed and implemented a teaching OO language in 4 days.
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Scheme in Scheme 1.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
6.001 SICP Variations on a Scheme
The interpreter.
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Implementing Recursion
Env. Model Implementation
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
Original material by Eric Grimson
2.3 Representation Strategies for Data Types
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Procedures App B: SLLGEN 1.
3.7 Variable Assignment Recall instance variables in Python:
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
CSE S. Tanimoto Explicit Function Application
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Explicit Application of Procedures, and Explicit Evaluation
Chapter 1 Review: BNF Grammar for lists:
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Chapter 3: Environment-Passing Interpreters
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Common Lisp II.
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Principles of Programming Languages
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

5.4.1: Implementation Method Invocation (define eval-expression (lambda (exp env) (cases expression exp ... (method-app-exp (obj-exp method-name rands) (let ((args (eval-rands rands env) (obj (eval-expression obj-exp env))) (find-method-and-apply method-name (object->class-name obj) ;obj.class-name obj args))) E.g., send circle2 move(-3,10)

super Call (define eval-expression (lambda (exp env) (cases expression exp ... (super-call-exp (method-name rands) (let ((args (eval-rands rands env) (obj (apply-env env 'self))) (find-method-and-apply method-name (apply-env env '%super) ; special name obj args))) Note that identity of super is determined by calling environment, not object! Otherwise...

super Call … otherwise, super wouldn't work as expected: class c1 extends object method initialize() 1 method m () 99 class c2 extends c1 method m () super m (); what happens here? class c3 extends c2 method m () super m () let o = new c3 () in send o m () We'd have an infinite regress at c2.m if we called super on o's parent class (c2)

Object Creation via new (define eval-expression (lambda (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 ; side-effect 'initialize class-name obj args) obj)) ; return obj E.g., new circle(x, y, (sqrt 22))

The Language : (Simple) Implementation Need to write elaborate-class-decls! and find-method-and-apply. For simple implementation, first is easy: (define the-class-env '()) ; why? (define elaborate-class-decls! (lambda (c-decls) (set! the-class-env c-decls))) To understand the rest, we need an example....

class c1 extends object field x field y method initialize() begin set x = 11; set y = 12 end method m1 () ... x ... y ... method m2 () ... send self m3() ... ; why can we do this? class c2 extends c1 method initialize () super initialize(); set y = 22 method m1(u, v) ... x ... y ... method m3() ... class c3 extends c2 field z set x = 31; set z = 32 method m3 () ... x ... y ... z let o3 = new c3() in send o3 m1(7,8)

Can represent o3 as a “class-chain” of parts: a-part a-part c2 a-part c3 c1 31 22 32 11 12 x z y x y

So we need a datatype for parts: (define-dataype part part? (a-part (class-name symbol?) (fields vector?))) Use this to build objects recursively: (define new-object (lambda (class-name) (if (eqv? class-name 'object) '() ; why? (let ((c-decl (lookup-class ; c-decls is class-name))); global (cons (make-first-part c-decl) (new-object ; recur up (class-decl->super-name c-decl)))))))

Piece-by-piece... (define make-first-part (lambda (c-decl) (a-part ; constructor (class-decl->class-name c-decl) (make-vector (length (class-decl->field-ids c-decl).. (define class-decl->class-name (lambda (c-decl) (cases class-decl c-decl (a-class-decl (class-name super-name field-ids m-decls) class-name)))) ; return class_decl.class_name

Now we can write find-method-and-apply (define find-method-and-apply (lambda (m-name host-name self args) (if (eqv? host-name 'object) (eopl:error 'find-method-and-apply ; why? “No method for name ~s” m-name) (let ((m-decl (lookup-method-decl m-name (class-name->method-decls host-name)))) (if (method-decl? m-decl) ; if found (apply-method m-decl host-name self args) (find-method-and-apply m-name (class-name->super-name host-name) self args))))))) ; recur up class hierarchy