Mid-Term 2 Grades

Similar documents
Outline. More optimizations for our interpreter. Types for objects

Lecture 16: Object Programming Languages

Lecture 12: Conditional Expressions and Local Binding

Principles of Programming Languages

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN

Type Declarations. Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ. {+ e 1 e 2 } : num

Lecture 3: Expressions

Type Declarations. [... <id> τ... ] <id> : τ. Γ <num> : number. Γ true : boolean. Γ false : boolean. Γ e 1 : number.

Functional Programming. Pure Functional Programming

Parsing Scheme (+ (* 2 3) 1) * 1

Comp 311: Sample Midterm Examination

9 Objects and Classes

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Lexical Addresses. let x = 1 y = 2 in let f = proc (x) +(x, y) in (f x) let _ = 1 _ = 2 in let _ = proc (_) +(<0,0>, <1,1>) in (<0,0> <1,0>)

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Typed Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CS61A Midterm 2 Review (v1.1)

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Practice for Chapter 11

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

1. For every evaluation of a variable var, the variable is bound.

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Racket: Modules, Contracts, Languages

Principles of Programming Languages 2017W, Functional Programming

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Rules and syntax for inheritance. The boring stuff

Discussion 12 The MCE (solutions)

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Java and C# in Depth

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

More On inheritance. What you can do in subclass regarding methods:

Lecture 15 CIS 341: COMPILERS

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Object Oriented Programming. Java-Lecture 11 Polymorphism

CMSC 330: Organization of Programming Languages

A Short Summary of Javali

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Lexical Considerations

Part III. Chapter 15: Subtyping

CS115 - Module 10 - General Trees

1 Lexical Considerations

Scheme Quick Reference

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

From FP to OOP. Start with data: class Posn { int x; int y; Posn(int x, int y) { this.x = x; this.y = y; } }

Project 2 Due 11:59:59pm Wed, Mar 7, 2012

First IS-A Relationship: Inheritance

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Inheritance - Assignment5

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Lexical Considerations

CMSC 330: Organization of Programming Languages. Operational Semantics

The syntax of the OUN language

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Class, Variable, Constructor, Object, Method Questions

Programs as data first-order functional language type checking

CSEP505 Programming Languages, Autumn 2016, Final Exam December, Programming Languages for a World of Change

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

More Relationships Between Classes

Chapter 5 Object-Oriented Programming

COS 320. Compiling Techniques

Compilers. Compiler Construction Tutorial The Front-end

Arrays Classes & Methods, Inheritance

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Event Type Polymorphism

SCHEME INTERPRETER GUIDE 4

Principles of Programming Languages

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Visitors. Move functionality

Connecting Definition and Use? Tiger Semantic Analysis. Symbol Tables. Symbol Tables (cont d)

AP CS Unit 6: Inheritance Notes

Scheme Quick Reference

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

C++ Important Questions with Answers

State. Substitution relies on an identifer having a fxed value

Exercise 12 Initialization December 15, 2017

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Example: Count of Points

Pierce Ch. 3, 8, 11, 15. Type Systems

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

Programming Languages: Application and Interpretation

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Java: introduction to object-oriented features

Introduction to Inheritance

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

CS-202 Introduction to Object Oriented Programming

Part III Chapter 15: Subtyping

Java Fundamentals (II)

Transcription:

Mid-Term 2 Grades 100 46 1

HW 9 Homework 9, in untyped class interpreter: Add instanceof Restrict field access to local class Implement overloading (based on argument count) Due date is the same as for HW 10 2

Implementing Type Checking with Classes We used to have two records for each class: Class declarations= abstract syntax Class= run-time class information flattened field and method lists Now we'll have three: Class declarations= abstract syntax Static class= check-time class information flattened lists with types Class= run-time class information flattened lists 3-4

Static Class Elaboration ;; type-of-program : program -> type (define (type-of-program pgm) (cases program pgm (a-program (c-decls exp) (statically-elaborate-class-decls! c-decls) (type-of-expression exp (empty-tenv)))))) 5

Checking Class Declarations Check: Superclass exists, and no cyclic inheritance Methods bodies ok Use host class for type of self Overriding method signatures are the same as in superclass Except for initialize class c2 extends c1 method void m(int x, bool y) if y then +(2, x) else send self w() 6

Checking Class Declarations Cyclic inheritance covered by requirement that classes are ordered (define statically-elaborate-class-decls! (lambda (c-decls) (for-each statically-elaborate-class-decl! c-decls) (for-each check-class-method-bodies! c-decls))) 7

Checking Class Declarations: Methods (define (check-class-method-bodies! c-decl)... (for-each (lambda (m-decl) (typecheck-method-decl! m-decl class-name super-name field-ids field-tys)) m-decls)) 8

Checking Class Declarations: Methods (define (typecheck-method-decl! m-decl self-name super-name field-ids field-types) (cases method-decl m-decl (a-methd-decl (res-texp name id-texps ids body) (let* ((id-tys (expand-ty-exprs id-texps)) (tenv (extend-tenv (cons '%super (cons 'self ids)) (cons (class-type super-name) (cons (class-type self-name) id-tys)) (extend-tenv field-ids field-tys (empty-tenv)))) (body-ty (type-of-expr body tenv))) (check-is-subtype! body-ty (expand-ty-expr res-texp) m-decl))) (an-abstract-method-decl (...) #t))) 9

Checking Object Creation Check: Class exists, and is not abstract Class has an initialize method initialize's argument types match the operand types class c1 extends object method void initialize(int x, bool y)... new c1(1, false) 10

Checking Object Creation (define (type-of-new-obj-exp rand-types) (cases static-class (static-lookup class-name) (a-static-class (...) (cases abstraction-specifier specifier (abstract-specifier () (eopl:error...)) (concrete-specifier () (type-of-method-app-exp #t ;; means from `new' (class-type class-name) 'initialize rand-types) ;; Result: (class-type class-name)))))) 11

Checking Method Calls Check: Receiver expression is an object Method is in the object-type's class Except initialize... Method's argument types match the operand types class c1 extends object method void initialize()... method void m(int x, bool y)... let o1 = new c1() in send o1 m(1, false) 12

Checking Method Calls (define (type-of-method-app-exp for-new? obj-type msg rand-types) (if (and (eq? msg 'initialize) (not for-new?)) (eopl:error...)) (cases type obj-type (class-type (class-name) (type-of-method-app-or-super-call #f class-name msg rand-types)) (else (eopl:error...)))) 13

Checking Super Calls Check: Same as method calls, but simpler: No check for initialize No possibility of a non-object type (define (type-of-super-call-exp super-name msg rand-types) (type-of-method-app-or-super-call #t super-name msg rand-types) 14

Checking Method Application (define (type-of-method-app-or-super-call super-call? host-name msg rand-tys) (let ((method (statically-lookup-method msg (static-class->methods (static-lookup host-name))))) (if (static-method? method) (cases static-method method (a-static-method (method-name spec method-ty super-name) (let ((result-ty (type-of-app method-ty rand-tys))) (if super-call? (cases abstraction-specifier spec (concrete-spec () result-ty) (abstract-spec () (error...))) result-ty)))) (eopl:error...)))) 15

Checking Casts Check: Operand has an object type (for any class) Target class exists Class for operand and target must be comparable Otherwise, cast cannot possibly succeed class c1 extends object... class c2 extends object... cast new c1() c2 16

Checking Casts (define (type-of-cast-exp ty name2 exp) (cases type ty (class-type (name1) (if (or (statically-is-subclass? name1 name2) (statically-is-subclass? name2 name1)) (class-type name2) (eopl:error...))) (else (eopl:error...))))) 17

Checking Other Expressions Other expression forms checked as before check-is-subtype! often used instead of check-equal-type! 18

Compiling with Classes (Optionally) Recall that a compiler takes a program in language A and produces a program in language B To make compilation optional, a common trick is to set B = A, with the expectation that source programs use only a subset of A 19-20

Grammar with Compiler-target Cases <expr> ::= <num> ::= <id> ::= <prim>(<expr>* (,) )... ::= send <expr> <id>(<expr>* (,) )... ::= <<num>,<num>> ::= send <expr> <<num>>(<expr>* (,) ) 21

Grammar with Compiler-target Cases (define the-grammar '((program ((arbno class-decl) expression) a-program) (expression (number) lit-exp) (expression ("true") true-exp)... (expression ("lexvar" number number) lexvar-exp) (expression ("imethod" expression number (separated-list expression ",")) apply-method-indexed-exp))) 22

Interpreter with Compiler-target Cases (define (eval-expression exp env) (cases expression exp (lit-exp (datum) datum) (var-exp (id) (apply-env env id))... (lexvar-exp (depth pos) (apply-env-lexvar env depth pos)) (apply-method-indexed-exp (obj-exp pos rands) (let ((obj (eval-expression obj-exp env)) (args (eval-rands rands env)) (c-name (object->class-name obj))) (apply-method (list-ref (class->methods (lookup-class c-name)) pos)...))))) 23

HW 10 Homework 10: Replace variables with lexical addresses Attach field count to new Index for initialize for new Index for class, instead of finding by name Change super to use class and method index... and more, if you'd like 24-25