Scope and Parameter Passing 1 / 19

Similar documents
Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

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

Recap: Functions as first-class values

Scope, Functions, and Storage Management

CS 381: Programming Language Fundamentals

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

The role of semantic analysis in a compiler

CSCI312 Principles of Programming Languages!

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

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

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

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

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

CMSC 330: Organization of Programming Languages

LECTURE 14. Names, Scopes, and Bindings: Scopes

G Programming Languages - Fall 2012

Variables and Bindings

Variable Definitions and Scope

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Scope & Symbol Table. l Most modern programming languages have some notion of scope.

Syntax Errors; Static Semantics

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

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Lecture08: Scope and Lexical Address

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Procedure and Object- Oriented Abstraction

Java Object Oriented Design. CSC207 Fall 2014

Chapter 1. Fundamentals of Higher Order Programming

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

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

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

Run-time Environments - 2

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

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Lecture 16: Static Semantics Overview 1

G Programming Languages - Fall 2012

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

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

References and Mutable Data Structures

Compiler Theory. (Semantic Analysis and Run-Time Environments)

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Bindings & Substitution

Vectors in Scheme. Data Structures in Scheme

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable

Structure of Programming Languages Lecture 5

CSE 307: Principles of Programming Languages

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

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

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

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

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

Code Generation & Parameter Passing

Memory management COSC346

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

Functions!!! Why functions? Functions provide good way to design systems!

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

Iterative Languages. Scoping

CSCE 314 Programming Languages. Type System

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Programming Languages Fall 2013

CS558 Programming Languages. Winter 2013 Lecture 3

known as non-void functions/methods in C/C++/Java called from within an expression.

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

Lecture 04 FUNCTIONS AND ARRAYS

CSE 341 Section 5. Winter 2018

Run Time Environments

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

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

Types and Type Inference

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CS 320: Concepts of Programming Languages

Functions. Systems Programming Concepts

CSC 2400: Computer Systems. Using the Stack for Function Calls

CS558 Programming Languages

CS422 - Programming Language Design

Programming Languages and Compilers (CS 421)

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Visitors. Move functionality

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

CSC 1052 Algorithms & Data Structures II: Interfaces

CS558 Programming Languages

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Begin at the beginning

Programming Languages

Programmiersprachen (Programming Languages)

CS 415 Midterm Exam Spring 2002

Programming Languages Third Edition. Chapter 7 Basic Semantics

Lecture 5: Methods CS2301

The SPL Programming Language Reference Manual

CSC 533: Organization of Programming Languages. Spring 2005

Names, Scope, and Bindings

COP4020 Fall 2006 Final Exam

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Midterm II CS164, Spring 2006

CS558 Programming Languages

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Transcription:

Scope and Parameter Passing 1 / 19

Outline Overview Naming and scope Function/procedure calls Static vs. dynamic scope Parameter passing schemes 2 / 19

Review of naming Most languages provide a way to name and reuse stuff Naming concepts declaration introduce a new name binding associate a name with a thing reference use the name to stand for the bound thing C/Java variables int x; int y; x = slow(42); y = x + x + x; In Haskell: Local variables let x = slow 42 in x + x + x Type names type Radius = Float data Shape = Circle Radius Function parameters area r = pi * r * r Overview 3 / 19

Scope Every name has a scope The parts of the program where that name can be referenced Block: shared scope of a group of declared names Shadowing: when a declaration in an inner block temporarily hides a name in an outer block C blocks { int x; int y; x = 2; if (x == 3) { int x = 4; int z = 5; y = x; } print(x); } Python locals def demo(): x = 6 if x == 7: x = 8 y = x print x print y Haskell let let x = 9 y = x in let x = 5 z = y in (x,y) Overview 4 / 19

Implementing nested scopes Recall CS 271 approach: local variables are stored in a stack frame enter a block: push a frame exit a block: pop a frame frame 1 frame 2 frame 3. frame N type Frame = [(Var,Val)] type Stack = [Frame] Compare with environments: type Env = [(Var,Val)] Just a flat stack!... data segment Overview 5 / 19

Outline Overview Naming and scope Function/procedure calls Static vs. dynamic scope Parameter passing schemes Overview 6 / 19

Function/procedure declarations Function definitions declare names in two scopes 1. the function name: in the file/module 2. the argument names (parameters): in the function body Example: Haskell triple :: Int -> Int triple y = double y + y double :: Int -> Int double x = x + x perimeter :: Int -> Int -> Int perimeter x y = double x + double y Overview 7 / 19

Binding parameters A function definition contains: the declaration of the parameters references to the parameters double :: Int -> Int double x = x + x Q: Where/when are the parameters bound? A: At the call site! GHCi> double 5 10 Overview 8 / 19

References in function definitions Three kinds of variable names parameters local variables external variables Where are bindings for... parameter and local names? in current(ish) stack frame! external names? good question! Haskell area :: Float -> Float area d = let r = d / 2 in pi * r * r C/Java float area(float d) { float r = d / 2; return pi * r * r; } Overview 9 / 19

Outline Overview Naming and scope Function/procedure calls Static vs. dynamic scope Parameter passing schemes Static vs. dynamic scope 10 / 19

Static vs. dynamic scope Static scope: Dynamic scope: external names refer to variables that are visible at definition external names refer to variables that are visible at call site Definition int x = 3;... int baz(int a) { int b = x+a; return b; } Call site int x = 4;... int y = baz(5); Q: What is the value of y? static scope: 8 dynamic scope: 9 Static vs. dynamic scope 11 / 19

Dynamic scope References refer to most recent binding during execution Performing a function call 1. push frame with parameters onto the stack 2. run function body, save return value 3. pop frame from stack and resume executing Tradeoffs: supports ad-hoc extensibility all names are part of the public interface risk of name collision and unintended behavior bad modularity hard to refactor and understand Static vs. dynamic scope 12 / 19

Static scope References refer to most recent binding in the source code Performing a function call 1. save current stack, restore function s stack 2. push frame with parameters onto the stack 3. run function body, save return value 4. restore saved stack and resume executing Tradeoffs: names are not part of the public interface no risk of name collision more predictable behavior improved modularity can change names without breaking clients only supports planned extensibility Static vs. dynamic scope 13 / 19

Closures Closure = function + its environment (stack) Needed to implement static scoping! Static vs. dynamic scope 14 / 19

Outline Overview Naming and scope Function/procedure calls Static vs. dynamic scope Parameter passing schemes Parameter passing schemes 15 / 19

Call-by-value parameter passing Definition def foo(a,b,c): a := b+1 c := a-b return c Call site x := 4 y := foo(3,x,x+1) 1. evaluate argument expressions 2. push frame with argument values Environment: [(Var,Val)] [("a",3), ("b",4), ("c",5)] Parameter passing schemes 16 / 19

Call-by-name parameter passing Definition def foo(a,b,c): if a > 0 then a := a + b else a := a + c return a Call site x := 5 y := 0 foo(x,x+y,x/y) 1. push frame with argument expressions Environment: [(Var,Exp)] [("a", Ref "x"), ("b", Add (Ref "x") (Ref "y")), ("c", Div (Ref "x") (Ref "y"))] This simple approach only works with dynamic scoping why? What happens if an argument has a side effect? Parameter passing schemes 17 / 19

Call-by-need parameter passing (a.k.a. lazy evaluation) Idea: Use call-by-name, but remember the value of any argument we evaluate only evaluate argument if needed, but evaluate each at most once best aspects of call-by-value and call-by-name! Definition def triple(x,y): if x > 0 then z := x + x + x else z := y + y + y return z Call site triple (slow(42), crash()) 1. push frame with argument expressions 2. replace expressions by values as evaluated Environment: [(Var, Either Exp Val)] Parameter passing schemes 18 / 19

Call-by-reference parameter passing Only relevant in languages with assignment use a store to simulate memory type Store = [(Addr,Val)] Definition def foo(a,b,c): a := b+5 c := a-b Call site x := 2 y := 3 z := 4 foo(x,y,z) Note: only plain variable references allowed as arguments! 1. push frame with argument addresses Environment: [(Var,Addr)] [("a",2), ("b",1), ("c",0)] Parameter passing schemes 19 / 19