Difference between revisions of "Environment diagram"

From CS 61A Wiki
Jump to: navigation, search
[unchecked revision][checked revision]
(<code> tags)
(restructure)
Line 1: Line 1:
An '''environment diagram''' is a visualization of the frames of a program and all the existing bindings.
+
An '''environment diagram''' is a visualization of the [[frame]]s of a program and all the existing bindings.
  
== Frame ==
+
== Rules ==
A ''frame'' contains ''bindings'', which map a name/variable to a value. A function call creates a new frame whose parent is the current frame.
+
 
+
The first frame is the ''global frame''.
+
 
+
== Environment ==
+
An ''environment'' consists of a sequence of frames. For example, in the following code:
+
<syntaxhighlight lang="python">
+
x = 1
+
y = 2
+
def outer():
+
    x = 3
+
    y = 4
+
    def inner():
+
        x = 5
+
        y = 6
+
    return inner
+
outer()()
+
</syntaxhighlight>
+
the sequence of frames that makes up the environment of <code>inner</code> is <code>inner</code> → <code>outer</code> → <code>global</code>. Here are the bindings in each frame:
+
{| class="wikitable" style="text-align:center"
+
! frame
+
! x
+
! y
+
|-
+
! <code>global</code>
+
| 1 || 2
+
|-
+
! <code>outer</code>
+
| 3 || 4
+
|-
+
! <code>inner</code>
+
| 5 || 6
+
|}
+
 
+
== Variable lookup ==
+
A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. In other words, to lookup a name in an environment, start looking in the local frame, then in the parent frame (if it exists), until you get to the global frame. For example, in the following code:
+
<syntaxhighlight lang="python">
+
x = 1
+
def outer():
+
    def inner():
+
        return x
+
    return inner
+
outer()()
+
</syntaxhighlight>
+
in the body of <code>inner</code>, to lookup <code>x</code>, look in the local frame <code>inner</code>. Since it is not there, look in the parent frame <code>outer</code>. Since it is not there, look in the parent frame (the global frame) and find <code>x = 1</code>.
+
 
+
== Sources ==
+
* http://inst.eecs.berkeley.edu/~cs61a/sp14/slides/03_6pp.pdf
+
 
+
== Resources ==
+
 
* [http://inst.eecs.berkeley.edu/~cs61a/sp14/pdfs/environment-diagrams.pdf Official guide]
 
* [http://inst.eecs.berkeley.edu/~cs61a/sp14/pdfs/environment-diagrams.pdf Official guide]
 
* [http://albertwu.org/cs61a/notes/environments Albert's guide]
 
* [http://albertwu.org/cs61a/notes/environments Albert's guide]
Line 57: Line 7:
 
* [http://www.michellerubyhwang.com/handouts/sp14/EnvDiagramSp14.pdf Michelle's guide]
 
* [http://www.michellerubyhwang.com/handouts/sp14/EnvDiagramSp14.pdf Michelle's guide]
 
* [http://inst.eecs.berkeley.edu/~cs61a-tz/guerrilla/env.txt Andrew's guide]
 
* [http://inst.eecs.berkeley.edu/~cs61a-tz/guerrilla/env.txt Andrew's guide]
 +
 +
== Sources ==
 +
* http://inst.eecs.berkeley.edu/~cs61a/sp14/slides/03_6pp.pdf
 +
 +
== External links ==
 
* [http://pythontutor.com/visualize.html#mode=edit Python Tutor]
 
* [http://pythontutor.com/visualize.html#mode=edit Python Tutor]

Revision as of 10:55, 27 May 2014

An environment diagram is a visualization of the frames of a program and all the existing bindings.

Rules

Sources

External links