Environment Diagrams
Environment Diagrams
What are Environment Diagrams? A visual tool to keep track of bindings state of a computer program In this class,we use Python as our primary language o The diagrams we teach can be applied to similar languages
What are Environment Diagrams? ● A visual tool to keep track of bindings & state of a computer program ● In this class, we use Python as our primary language ○ The diagrams we teach can be applied to similar languages
Why do we use Environment Diagrams? Environment Diagrams are conceptual understand why programs work the way they do confidently predict how a program will behave Environment Diagrams are helpful for debugging When you're really stuck, diagramming code staring at lines of code Environment Diagrams will be used in future courses CS 61C (Machine Structures) CS 164 (Programming Languages and Compilers)
Why do we use Environment Diagrams? • Environment Diagrams are conceptual • understand why programs work the way they do • confidently predict how a program will behave • Environment Diagrams are helpful for debugging • When you're really stuck, diagramming code > staring at lines of code • Environment Diagrams will be used in future courses • CS 61C (Machine Structures) • CS 164 (Programming Languages and Compilers)
What do we've seen so far Assignment Statements Global frame X=1 X 3 X =XX+X Def Statements Global frame function def square(x): square(x)】 square return xx square Call Expressions X 4 square(4) Frame Return 16 value
What do we've seen so far Assignment Statements x = 1 x = x + x + x Def Statements def square(x): return x * x Call Expressions square(4) Frame
Terminology:Frames Global frame function square(x) A frame keeps track of varia square Every call expression has a corr square Global,a.k.a.the global fr 4 arting frame. It doesn't correspond to a speci Return 1. 16 Parent frames value The parent of a function is the frame in which it was defined. If you can't find a variable in the current frame,you check it's parent,and so on.If you can't find the variable,NameError Demo
Terminology: Frames A frame keeps track of variable-to-value bindings. ● Every call expression has a corresponding frame. Global, a.k.a. the global frame, is the starting frame. ● It doesn't correspond to a specific call expression. Parent frames ● The parent of a function is the frame in which it was defined. ● If you can't find a variable in the current frame, you check it's parent, and so on. If you can't find the variable, NameError Demo
Check Your Understanding Draw the environment diagram def square(x): return xX def sum_of_squares(x,y): return square(x)+square(y) sum_of_squares(3,4)
Draw the environment diagram def square(x): return x * x def sum_of_squares(x, y): return square(x) + square(y) sum_of_squares(3, 4) Check Your Understanding
Review:Evaluation Order Demo Remember to evaluate the operator,then the operand(s),then apply the operator onto the operand(s). def add_one(x): Evaluate the operator.A Evaluate the operand.. y=X+1 function value is returned return y Returns 10 Returns 100 def square(x): Evaluate the operator.A Evaluate the operand.Now we have evaluate another return xx function value is returned expression. What will the environment diagram look like?(When are frames created?) The environment diagram should reflect Python's evaluation
square(add_one(9)) Review: Evaluation Order def add_one(x): y = x + 1 return y def square(x): return x * x Remember to evaluate the operator, then the operand(s), then apply the operator onto the operand(s). What will the environment diagram look like? (When are frames created?) The environment diagram should reflect Python’s evaluation. Evaluate the operator. A function value is returned Evaluate the operand. Now we have evaluate another expression. Evaluate the operator. A function value is returned Evaluate the operand.. Returns 10 Returns 100 Demo
Variable Lookup
Variable Lookup
Local Names Variable Lookup: Lookup name in the current frame 。 Lookup name in parent frame,its parent frame,etc.. Stop at the global frame If not found,an error is thrown Global frame >func f(x.y)[parent=Global] An error is thrown func g(z)[parent=Global] g f1:f【parent=Global] def f(x,y): Name"x"is not found,again ×5 y10 return g(x) Name"x"is not found 2 8 parent=Global] z5 def g(z): Important:There was no lookup done in f1 since return z x the parent of f2 was Global What happens here? result f(5,10)
Where do we look next? Local Names Variable Lookup: • Lookup name in the current frame • Lookup name in parent frame, its parent frame, etc.. • Stop at the global frame • If not found, an error is thrown def f(x, y): return g(x) def g(z): return z + x result = f(5, 10) Name “x” is not found Name “x” is not found, again An error is thrown What happens here? Important: There was no lookup done in f1 since the parent of f2 was Global
Demo Evaluation vs Apply def a_plus_bc(a,b,c): I II I >>> a-p1us_bc(2,3,4)#2+3*4 14 Apply operator How many frames are a_plus_bc function to created? n operand 4,3,81. In what order? a_plus_bc(square(2),3,square(square(3))) Apply operator Apply operator square function to Apply operator square function to operand 2. square function to operand 9. operand 3
Evaluation vs Apply def a_plus_bc(a, b, c): """ >>> a_plus_bc(2, 3, 4) # 2 + 3 * 4 14 """ bc = b * c a_plus_bc(square( return a 2+ ), bc 3, square(square(3))) Demo How many frames are created? In what order? Apply operator square function to operand 2. Apply operator square function to operand 3. Apply operator square function to operand 9. Apply operator a_plus_bc function to operand 4, 3, 81