Names, Scopes, and Bindings
|
Note: These slides cover Chapter 3 of the textbook, except Section 3.6. We encourage you to study Section 3.6 of the textbook, but you are not required to do so. You are not required to study Sections 3.3.3 and 3.3.4. |
Names and Abstractions: What's in a Name?
|
Binding Time
|
Binding Time Examples
|
The Effect of Binding Time
|
Object Lifetime Example
{ SomeClass& myobject = *new SomeClass; ... { OtherClass& myobject = *new OtherClass; ... myobject // is bound to other object ... } ... myobject // is visible again ... delete myobject; } |
Object Storage Management
|
Typical Program and Data Layout in Memory
|
Static Allocation
|
Stack-Based Allocation
|
Stack-Based Allocation Example
|
Example Frame
|
Heap-Based Allocation
|
Heap Allocation Algorithms
|
Garbage Collection
|
Storage Allocation Mechanisms Compared
|
Scope
|
Static Versus Dynamic Scoping
a:integer procedure first a:=1 procedure second a:integer first() procedure main a:=2 second() write_integer(a)
|
Static Scoping
|
Closest Nested Scope Rule
procedure P1(A1:T1) var X:real; ... procedure P2(A2:T2); ... procedure P3(A3:T3); ... begin (* body of P3: P3, A3, P2, A2, X of P1, P1, A1 are visible *) end; ... begin (* body of P2: P3, P2, A2, X of P1, P1, A1 are visible *) end; procedure P4(A4:T4); ... function F1(A5:T5):T6; var X:integer; ... begin (* body of F1: X of F1, F1, A5, P4, A4, P2, P1, A1 are visible *) end; ... begin (* body of P4: F1, P4, A4, P2, X of P1, P1, A1 are visible *) end; ... begin (* body of P1: X of P1, P1, A1 are visible *) end |
Implementation of Static Scope: Static Links
|
Bindings to Non-Local Objects: Static Chains
procedure P1; var X:real; procedure P2; var X:integer begin ... (* X of P1 is hidden *) end; begin ... end
|
Blocks and Local Variable Scope
|
Modules and Object Scope
|
Note: The textbook provides a discussion on modules (pp.122-129) which is neither brief nor comprehensive enough. You are not required to study modules described in this part of the textbook |
Dynamic Scope
|
Dynamic Scope Problem
max_score:integer function scaled_score(raw_score:integer):real return raw_score/max_score*100 ... procedure foo max_score:real:=0 ... foreach student in class student.percent:=scaled_score(student.points) if student.percent>max_score max_score:=student.percent |
Implementation of Dynamic Scope: Binding Stack
|
Note: Textbook Sections 3.3.3 and 3.3.4 deal with the binding stack implementation issues. You are not required to study these sections |
Referencing Environments
|
Deep and Shallow Binding in Dynamically Scoped Languages
thres:integer function older(p:person):boolean return p.age>thres procedure show(p:person, c:function) thres:integer thres:=20 if c(p) write(p) procedure main(p) thres:=35 show(p, older)
|
Implementation of Deep Bindings: Subroutine Closures
|
Deep and Shallow Binding in Statically Scoped Languages
thres:integer function older(p:person):boolean return p.age>thres procedure show(p:person, c:function) thres:integer thres:=20 if c(p) write(p) procedure main(p) thres:=35 show(p, older) |
First-, Second-, and Third-Class Subroutines
|
First-Class Subroutines
function new_int_printer(port:integer):procedure procedure print_int(val:int) begin /*print_int*/ write(port, val) end /*print_int*/ begin /*new_int_printer*/ return print_int end /*new_int_printer*/ procedure main begin /*main*/ myprint:procedure myprint:=new_int_printer(80) myprint(7) end /*main*/ |
First-Class Subroutines (cont'd)
|
Overloading and Bindings
struct complex {...}; enum base {dec, bin, oct, hex}; void print_num(int n) ... void print_num(int n, base b) ... void print_num(struct complex c) ...
|
Polymorphic Subroutines and Templates
length (x:xs) = 1 + length xs length [] = 0 template<class T> class list { T* p; int size; public: int length() { return size; }; }; |