scope
play

Scope COMP 524: Programming Languages Based in part on slides and - PowerPoint PPT Presentation

Scope COMP 524: Programming Languages Based in part on slides and notes by J.Erickson, S. Krishnan, B. Brandenburg, S. Olivier, A. Block and others. Referencing Environment All currently known names. The set of active bindings. At


  1. Scope COMP 524: Programming Languages Based in part on slides and notes by J.Erickson, S. Krishnan, B. Brandenburg, S. Olivier, A. Block and others.

  2. Referencing Environment “All currently known names.” The set of active bindings. � ➡ At any given point in time during execution. ➡ Can change: names become valid and invalid during execution in most programming languages. ➡ Exception: early versions of Basic had only a single, global, fixed namespace. How is the referencing environment defined? � ➡ Scope rules . ➡ The scope of a binding is its “lifetime.” ➡ I.e., the textual region of the program in which a binding is active. B. Ward — Spring 2014 � 2

  3. Scope of a Binding The (textual) region in which a binding is active. float entity int entity Name time t 0 t 4 t 1 t 2 t 3 void method() { � int name ; � // code executed in [t 1 -t 2 ). � { � float name; � // code executed in [t 2 -t 3 ). � } � // code executed in [t 3 -t 4 ). � } B. Ward — Spring 2014 � 3

  4. Scope of a Binding The (textual) region in which a binding is active. Scope of name -to- int-entity binding. float entity int entity Name time t 0 t 4 t 1 t 2 t 3 void method() { � int name ; � // code executed in [t 1 -t 2 ). � { � float name; � // code executed in [t 2 -t 3 ). � } � // code executed in [t 3 -t 4 ). � } B. Ward — Spring 2014 � 4

  5. Scope of a Binding The (textual) region in which a binding is active. Scope of name -to- float-entity binding. float entity int entity Name time t 0 t 4 t 1 t 2 t 3 void method() { � int name ; � // code executed in [t 1 -t 2 ). � { � float name; � // code executed in [t 2 -t 3 ). � } � // code executed in [t 3 -t 4 ). � } B. Ward — Spring 2014 � 5

  6. Scope of a Binding The (textual) region in which a binding is active. float entity int entity Name Terminology: the name -to- int-entity binding is out of scope in this code fragment. � time t 0 t 4 t 1 t 2 t 3 The scope is said to have a “ hole .” void method() { � int name ; � // code executed in [t 1 -t 2 ). � { � float name; � // code executed in [t 2 -t 3 ). � } � // code executed in [t 3 -t 4 ). � } B. Ward — Spring 2014 � 6

  7. Language Scope Rules a major language design choice void printX() { � printf(“x = ” + x); � } what does x refer to? Dynamically Scoped. � Statically Scoped. � ➡ All bindings determined at ➡ Active bindings depend on compile time . � control flow . ➡ Bindings do not depend on ➡ Bindings are discovered during call history. execution. ➡ Also called lexically scoped . ➡ E.g., meaning of a name depends on call stack. B. Ward — Spring 2014 � 7

  8. Dynamically vs. Statically Scoped Which bindings are active in subroutine body? Dynamically Scoped: Subroutine body is executed in the 
 referencing environment of the subroutine caller . Statically Scoped: Subroutine body is executed in the 
 referencing environment of the subroutine definition . B. Ward — Spring 2014 � 8

  9. Dynamic Scope Example # This is dynamically scoped Perl. � $x = 10; � � sub printX { � � # $x is dynamically scoped. � � $from = $_[0]; � � print "from $from: x = $x \n"; � } � � sub test0 { � � local $x; # binding of $x is shadowed. � � $x = 0; � � printX "test0" � } � � sub test1 { � � local $x; # binding $x is shadowed. � � $x = 1; � � test0; � � printX "test1" � } � � test1; � printX "main"; B. Ward — Spring 2014 � 9

  10. Dynamic Scope Example # This is dynamically scoped Perl. � $x = 10; � � sub printX { � � # $x is dynamically scoped. � � $from = $_[0]; � � print "from $from: x = $x \n"; � } � � New binding created. � sub test0 { � � local $x; # binding of $x is shadowed. � Existing variable is 
 � $x = 0; � � printX "test0" � not overwritten , � } � � rather, the existing � sub test1 { � � local $x; # binding $x is shadowed. � binding (if any) is � $x = 1; � shadowed . � test0; � � printX "test1" � } � � test1; � printX "main"; B. Ward — Spring 2014 � 10

  11. Dynamic Scope Example # This is dynamically scoped Perl. � $x = 10; � � sub printX { � � # $x is dynamically scoped. � � $from = $_[0]; � � print "from $from: x = $x \n"; � } � � sub test0 { � � local $x; # binding of $x is shadowed. � Dynamically scoped : 
 � $x = 0; � � printX "test0" � the current binding of $x is 
 } � the one encountered 
 � sub test1 { � most recently during execution � local $x; # binding $x is shadowed. � (that has not yet been destroyed). � $x = 1; � � test0; � � printX "test1" � } � � test1; � printX "main"; B. Ward — Spring 2014 � 11

  12. Dynamic Scope Example Output: # This is dynamically scoped Perl. � $x = 10; � � from test0: x = 0 � sub printX { � from test1: x = 1 � � # $x is dynamically scoped. � from main: x = 10 � $from = $_[0]; � � print "from $from: x = $x \n"; � } � � sub test0 { � � local $x; # binding of $x is shadowed. � � $x = 0; � � printX "test0" � } � � sub test1 { � � local $x; # binding $x is shadowed. � � $x = 1; � � test0; � � printX "test1" � } � � test1; � printX "main"; B. Ward — Spring 2014 � 12

  13. Dynamic Scope Example Output: # This is dynamically scoped Perl. � $x = 10; � � from test0: x = 0 � sub printX { � from test1: x = 1 � � # $x is dynamically scoped. � from main: x = 10 � $from = $_[0]; � � print "from $from: x = $x \n"; � } � � sub test0 { � � local $x; # binding of $x is shadowed. � � $x = 0; � � printX "test0" � } � � sub test1 { � � local $x; # binding $x is shadowed. � � $x = 1; � � test0; � � printX "test1" � } � � test1; � printX "main"; B. Ward — Spring 2014 � 13

  14. Dynamic Scope Example Output: # This is dynamically scoped Perl. � $x = 10; � � from test0: x = 0 � sub printX { � from test1: x = 1 � � # $x is dynamically scoped. � from main: x = 10 � $from = $_[0]; � � print "from $from: x = $x \n"; � } � � sub test0 { � � local $x; # binding of $x is shadowed. � � $x = 0; � � printX "test0" � } � � sub test1 { � � local $x; # binding $x is shadowed. � � $x = 1; � � test0; � � printX "test1" � } � � test1; � printX "main"; B. Ward — Spring 2014 � 14

  15. Dynamic Scope Example Output: # This is dynamically scoped Perl. � $x = 10; � � from test0: x = 0 � sub printX { � from test1: x = 1 � � # $x is dynamically scoped. � from main: x = 10 � $from = $_[0]; � � print "from $from: x = $x \n"; � } � � sub test0 { � � local $x; # binding of $x is shadowed. � � $x = 0; � � printX "test0" � } � � sub test1 { � � local $x; # binding $x is shadowed. � � $x = 1; � � test0; � � printX "test1" � } � � test1; � printX "main"; B. Ward — Spring 2014 � 15

  16. Dynamic Scope Origin. � ➡ Most early Lisp versions were dynamically scoped. ➡ Scheme is lexically scoped and became highly influential; nowadays, dynamic scoping has fallen out of favor. Possible use. � ➡ Customization of “service routines.” E.g., field width in output. ➡ As output parameters for methods (write to variables of caller). Limitations. � ➡ Hard to reason about program : names could be bound to “anything.” ➡ Accidentally overwrite unrelated common variables (i, j, k, etc.). ➡ Scope management occurs at runtime; this creates overheads and thus limits implementation efficiency. B. Ward — Spring 2014 � 16

  17. Static Scope Example public class Scope { � � static int x = 10 ; � � � � static void printX( String from ) { � � � System . out . println ( "from " + from + � ": x = " + x ); � � } � � � � static void test0() { � � � int x = 0 ; � � � printX ( "test0" ); � � } � � � � static void test1() { � � � int x = 1 ; � � � test0 (); � � � printX ( "test1" ); � � } � � � � public static void main( String ... args ) { � � � test1 (); � � � printX ( "main" ); � � } � } B. Ward — Spring 2014 � 17

  18. Static Scope Example public class Scope { � � static int x = 10 ; � � � � static void printX( String from ) { � � � System . out . println ( "from " + from + � ": x = " + x ); � � } � � � � static void test0() { � New binding created. � � � int x = 0 ; � Existing variable is 
 � � printX ( "test0" ); � � } � not overwritten , � � � � static void test1() { � rather, the existing � � � int x = 1 ; � � � test0 (); � binding (if any) is � � printX ( "test1" ); � � } � shadowed . � � � public static void main( String ... args ) { � � � test1 (); � � � printX ( "main" ); � � } � } B. Ward — Spring 2014 � 18

Recommend


More recommend