INF3110 Programming Languages Runtime Organization part II

Similar documents
INF3110 Programming Languages Runtime Organization part II

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Scope, Functions, and Storage Management

Procedure and Object- Oriented Abstraction

Run-time Environments - 2

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Run-time Environments - 3

Today's Topics. CISC 458 Winter J.R. Cordy

CIT Week13 Lecture

Lecture08: Scope and Lexical Address

Dynamic Memory Allocation

G Programming Languages - Fall 2012

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Weeks 6&7: Procedures and Parameter Passing

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Topic 7: Activation Records

What about Object-Oriented Languages?

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

JavaScript: Sort of a Big Deal,

CSE 307: Principles of Programming Languages

CS558 Programming Languages. Winter 2013 Lecture 3

Contents. 8-1 Copyright (c) N. Afshartous

Chapter 6 Introduction to Defining Classes

Syntax Errors; Static Semantics

The SPL Programming Language Reference Manual

Object Model Comparisons

CS558 Programming Languages

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

CS111: PROGRAMMING LANGUAGE II

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

The Procedure Abstraction

Programming Languages Third Edition. Chapter 7 Basic Semantics

Implementing Subprograms

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Names, Bindings, Scopes

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CMSC 330: Organization of Programming Languages

Week 4 Lecture 1. Expressions and Functions

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

Concepts Introduced in Chapter 7

Array. Prepared By - Rifat Shahriyar

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

System Software Assignment 1 Runtime Support for Procedures

UNIT 3

Naming in OOLs and Storage Layout Comp 412

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CS558 Programming Languages

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Object Oriented Software Design II

JavaScript for C# Programmers Kevin

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

G Programming Languages - Fall 2012

CSCI312 Principles of Programming Languages!

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

1 Lexical Considerations

Compiler construction 2009

Midterm II CS164, Spring 2006

CSCE 314 Programming Languages. Type System

CS558 Programming Languages

Overview. Elements of Programming Languages. Objects. Self-Reference

Structure of Programming Languages Lecture 10

Launchpad Lecture -10

Introduction to Programming Using Java (98-388)

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Programmiersprachen (Programming Languages)

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Types and Type Inference

CA Compiler Construction

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

LECTURE 14. Names, Scopes, and Bindings: Scopes

Short Notes of CS201

Java Primer 1: Types, Classes and Operators

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

CS201 - Introduction to Programming Glossary By

Informatica 3 Syntax and Semantics

Subtyping (Dynamic Polymorphism)

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

NOTE: Answer ANY FOUR of the following 6 sections:

CSC 533: Organization of Programming Languages. Spring 2005

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

Lecture 4 Memory Management

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CS 314 Principles of Programming Languages. Lecture 13

UMBC CMSC 331 Final Exam

The PCAT Programming Language Reference Manual

Abstract data types &

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts

Abstract Data Types & Object-Oriented Programming

Transcription:

INF3110 Programming Languages Runtime Organization part II 10/24/17 1

Today: Higher-Order Functions, and Objects at runtime Higher-order functions: Functions passed as arguments Functions that return functions from nested blocks Need to maintain environment of function Simpler case Function passed as argument Need pointer to activation record higher up in stack More complicated case Function returned as result of function call Need to keep activation record of returning function Objects at runtime Which activation blocks do we use? Fun with Javascript The worlds most popular(?) language! Heavy use of higher-order functions 10/24/17 2

Functions as parameters: why? Given a Person class and a list of Persons Can you write a function that finds all persons that - Are female? - Are older than 50 years? - Like drinking beer? 10/24/17 3

Repetition from last time 10/24/17 4

Block-Structured Languages Blocks are syntactical structures Can be nested within each other outer block...; { int x = 2; ;... { int y = 3; x = y+2; new variables declared in nested blocks inner block x: global variable y: local variable Storage management memory representation Enter block: allocate space for variables Exits block: space may be de-allocated 10/24/17 5

Activation record for in-line block Control link Environment pointer Pointer to current record on stack Stack grows downwards Local variables Intermediate results Control link Local variables Intermediate results Control link (dynamic link) Pointer to previous record on stack Why is it called dynamic? Push record on stack Set new control link (in new record) to point to old env ptr Set env ptr to new record Pop record off stack Environment Pointer Follow control link of current record to reset environment pointer (No need to actively blank memory) 10/24/17 6

Some basic concepts Scope Region of program text where declaration is visible Lifetime Period of time when location is allocated { int x = ; ; { int y = ; ; { int x = ; ; ; x; ; x; Inner declaration of x hides outer one. Called hole in scope Lifetime of outer x includes time when inner block is executed Lifetime ¹ scope 10/24/17 7

Access to global variables Two possible scoping conventions Static scope: refer to closest enclosing block (syntactically) Dynamic scope: most recent activation record on stack Example int x = 1; function g(z) = x+z; function f(y) = { ; f(3); int x = y+1; return g(y*x) outer block f(3) g(12) x 1 y 3 x 4 z 12 How do we know which x is used for expression x+z? 10/24/17 8

Activation record for static scope Control link Access link Return address Return result addr Parameters Local variables Intermediate results Environment Pointer Control link (dynamic link) Link to activation record of previous (calling) block Access link (static link) Link to activation record corresponding to the closest enclosing block in program text Why is it called static? Difference Control link depends on dynamic behavior of program Access link depends on static form of program text 10/24/17 9

Simple example for a language with blocks and functions { int x = 1; int function g(z) { return x+z ; int function f(y) { int x = y+1; return g(y*x) ; main() { f(3); outer block main f(3) g(12) x 1 control link access link control link access link y 3 x 4 control link access link z 12 10/24/17 10

10/24/17 11

Functions as parameters: why? Given a Person class and a list of Persons Can you write a function that finds all persons that - Are female? - Are older than 50 years? - Like drinking beer? A first attempt: List<Person> findpersonsthatarefemale(list<person> persons) { List<Person> filteredlist = new List<Person>(); for(person p in persons) { if(p.gender == female ) { filteredlist.add(p); return filteredlist; 10/24/17 12

Functions as parameters: why? Given a Person class and a list of Persons Can you write a function that finds all persons that - Are female? - Are older than 50 years? - Like drinking beer? - A first attempt: List<Person> findpersonsthatareolderthan50(list<person> persons) { List<Person> filteredlist = new List<Person>(); for(person p in persons) { if(p.age > 50) { filteredlist.add(p); return filteredlist; 10/24/17 13

Why functions as parameters? DRY! List<Person> filterpersons(list<person> persons, (Person à Boolean) filter) { List<Person> filteredlist = new List<Person>(); for(person p in persons) { if(filter(p)) { filteredlist.add(p); return filteredlist; Imaginary func syntax filterpersons( persons, function(person p) { return p.age > 50 ); filterpersons( persons, function(person p) { return p.gender == female ); 10/24/17 14

Is this in use in languages today? Traditional OO approach: make a class out of it! E.g. in Java (pre v8): Collections.sort(list, new Comparator<MyClass>() { public int compare(myclass a, MyClass b) { // compare objects here ); What is going on here? Comparator<T> is an interface, and T is a type parameter This interface has one method signature, int compare(t a, T b); Starting from the first {, we have an anonymous class implementing this interface 10/24/17 15

Is this in use in languages today? Functional approach: Java (v8 and up): list.sort((a, b) -> a.isgreaterthan(b)); C#: mylist.where(a => a.number > 42).OrderBy(a => a.number).thenby(a => a.foobar); Python: Celsius = [39.2, 36.5, 37.3, 37.8] Fahrenheit = map(lambda x: (float(9)/5)*x + 32, Celsius) JavaScript (Node): app.get('/somepath/:date', function (req, res) res.setheader('content-type', 'application/json'); fetchstuff({ date: req.params.date, function (error, result) { if (error) console.log(error); res.end(result); ); ); 10/24/17 16

Pass function as argument There are two declarations of x { int x = 4; { int f(int y) {return x*y; { int g(int int h) { int x=7; return h(3) + x; g(f); Which one is in scope for each usage of x? Formal function parameter Actual function parameter 10/24/17 17

Static Scope for Function Argument { int x = 4; { int f(int y) {return x*y; { int g(int int h) { x 4 f Code for f int x=7; return h(3) + x; g(f); g(f) h(3) follow access link g h x 7 y 3 x * y local var Code for g How is access link for h(3) set? à Next slides 10/24/17 18

Closures Function value is pair closure = áenv, code ñ When a function represented by a closure is called Allocate activation record for call (as always) Set the access link in the activation record using the environment pointer from the closure 10/24/17 19

Function Argument and Closures Run-time stack with access links { int x = 4; { int f(int y){return x*y; { int g(int int h) { g(f); int x=7; return h(3)+x; g(f) h(3) x 4 access f access g access h x 7 access y 3 access link set from closure for each function call Code for f Code for g 10/24/17 20

Return Function as Result Language feature Functions that return new functions Need to maintain environment of function Function created dynamically function value is closure = áenv, codeñ code not compiled dynamically (in most languages) 10/24/17 21

Example: Return function with private state Function make counter returns a closure { int int mk_counter (int init) { int count = init; int counter(int inc) { return count += inc; return counter int int c = mk_counter(1); print c(2) + c(2); How is correct value of count determined in call c(2)? Next slide à 10/24/17 22

Function Results and Closures {int int mk_counter (int init) { int count = init; int counter(int inc) { return count+=inc; return counter mk_counter(1) int int c = mk_counter(1); print c(2) + c(2); Call changes cell value from 1 to 3 c(2) mk_c access c access init 1 count counter access inc 2 Code for mk_counter Code for counter Activation record associated with returned function cannot be deallocated upon function return 10/24/17 23 13

Classes and objects at runtime Pointers to objects on the stack In normal activation bloks The objects themselves are typically not stored on the stack Separate location called the heap Data for each object stored with the object E.g. x and y coordinates for a point Common functionality stored in shared location Methods, static variables 10/24/17 24

Smalltalk Point object and class to superclass Object... Stack Point object Point class access point class x 3 y 2 superclass template method dict new x:y... x y code for new move code for move 10/24/17 25

Smalltalk runtime support for inheritance 10/24/17 26

Aside: not all scopes are equal this.value = 42; //Global variable var obj = { value: 0, //Local field in object increment: function() { this.value++; alert(this.value); var innerfunction = function() { alert(this.value); What will be shown on screen? innerfunction(); // Function invocation obj.increment(); // Method call Try it out yourselves: http://jsfiddle.net/7jxw1r9v/1/ 10/24/17 27

How do we fix this? this.value = 42; //Global variable var obj = { value: 0, increment: function() { this.value++; alert(this.value); var that = this; var innerfunction = function() { alert(that.value); Why does this help? Because this function is a closure that captures the «that» variable innerfunction(); //Function invocation obj.increment(); //Method invocation 10/24/17 28

Everyone loves Javascript! http://techbeacon.com/5-emerging-programming-languages-bright-future https://www.destroyallsoftware.com/talks/wat 10/24/17 29

But WHY all these WATs? JavaScript has automatic type coercion 10/24/17 It will try to convert types into something that matches the operand! [ ] + [ ] = "" The + operator cannot operate on arrays, so the array is coerced to its string representation, which is a tostring() of all the elements joined by commas. { + [ ] = 0 The first is recognized as an empty code block. The plus is thus unary, and [ ] is coerced to an empty string, which is in turned coerced to 0. { + { = NaN The first is again an empty code block The second { is an empty object, which is coerced to [object Object], which is the tostring() repr of objects Which is again not a number, or NaN 30

More fun: scoping and blocks Java: JavaScript: void main() { Integer x = 1; System.out.println(x); if (true) { Integer x = 2; System.out.println(x); System.out.println(x); function main() { var x = 1; console.log(x); if (true) { var x = 2; console.log(x); console.log(x); Output: «1», «2», «1» Output: «1», «2», «2»! - JavaScript has blocks, but (traditionally) not block scope! - Declarations are always «hoisted» to the top of the function 10/24/17 31

More fun: scoping and blocks Java: JavaScript, explicit hoisting: void main() { Integer x = 1; System.out.println(x); if (true) { Integer x = 2; System.out.println(x); System.out.println(x); Output: «1», «2», «1» function main() { var x; var x; x = 1; console.log(x); if (true) { x = 2; console.log(x); console.log(x); Output: «1», «2», «2»! - JavaScript has blocks, but (traditionally) not block scope! - Declarations are always «hoisted» to the top of the function 10/24/17 32

More fun: scoping and blocks Java: JavaScript/EcmaScript 6+: void main() { Integer x = 1; System.out.println(x); if (true) { Integer x = 2; System.out.println(x); System.out.println(x); function main() { let x = 1; console.log(x); if (true) { let x = 2; console.log(x); console.log(x); Output: «1», «2», «1» Output: «1», «2», «1»! - EcmaScript 6 has blocks and block scope, if you use let! 10/24/17 33

Upcoming! Part two of OO lecture Study week Repetition Guest Lecture Exam: November 29th, 14:30 10/24/17 34