Difference between revisions of "Guides"

From CS 61A Wiki
Jump to: navigation, search
m (undo test)
(moved from Review materials)
Line 1: Line 1:
== Guide 1 ==
+
== Andrew's environment diagram rules ==
...
+
From http://inst.eecs.berkeley.edu/~cs61a-tz/guerrilla/env.txt:
 +
=== Creating a function ===
 +
# Draw the func <name>(<arg1>, <arg2>, ...)
 +
# The parent of the function is wherever the function was defined (the frame we're currently in, since we're creating the function).
 +
# If we used def, make a binding of the name to the value in the current frame.
  
== Guide 2 ===
+
=== Calling user-defined functions ===
 +
# Evaluate the operator and operands.
 +
# Create a new frame; the parent is whatever the operator s parent is. Now this is the current frame.
 +
# Bind the formal parameters to the argument values (the evaluated operands).
 +
# Evaluate the body of the operator in the context of this new frame.
 +
# After evaluating the body, go back to the frame that called the function.
 +
 
 +
=== Assignment ===
 +
# Evaluate the expression to the right of the assignment operator (=).
 +
# If nonlocal, find the frame that has the variable you're looking for, starting in the parent frame and ending just before the global frame (via Lookup rules). Otherwise, use the current frame. Note: If there are multiple frames that have the same variable, pick the frame closest to the current frame.
 +
# Bind the variable name to the value of the expression in the identified
 +
frame. Be sure you override the variable name if it had a previous binding.
 +
 
 +
 
 +
=== Lookup ===
 +
# Start at the current frame. Is the variable in this frame? If yes, that's the answer.
 +
# If it isn't, go to the parent frame and repeat 1.
 +
# If you run out of frames (reach the Global frame and it's not there), complain.
 +
 
 +
=== Tips ===
 +
# You can only bind names to values. No expressions (like 3+4) allowed on environment diagrams!
 +
 
 +
# Frames and Functions both have parents.
 +
 
 +
== Guide 2 ==
 
...
 
...

Revision as of 07:26, 2 June 2014

Andrew's environment diagram rules

From http://inst.eecs.berkeley.edu/~cs61a-tz/guerrilla/env.txt:

Creating a function

  1. Draw the func <name>(<arg1>, <arg2>, ...)
  2. The parent of the function is wherever the function was defined (the frame we're currently in, since we're creating the function).
  3. If we used def, make a binding of the name to the value in the current frame.

Calling user-defined functions

  1. Evaluate the operator and operands.
  2. Create a new frame; the parent is whatever the operator s parent is. Now this is the current frame.
  3. Bind the formal parameters to the argument values (the evaluated operands).
  4. Evaluate the body of the operator in the context of this new frame.
  5. After evaluating the body, go back to the frame that called the function.

Assignment

  1. Evaluate the expression to the right of the assignment operator (=).
  2. If nonlocal, find the frame that has the variable you're looking for, starting in the parent frame and ending just before the global frame (via Lookup rules). Otherwise, use the current frame. Note: If there are multiple frames that have the same variable, pick the frame closest to the current frame.
  3. Bind the variable name to the value of the expression in the identified

frame. Be sure you override the variable name if it had a previous binding.


Lookup

  1. Start at the current frame. Is the variable in this frame? If yes, that's the answer.
  2. If it isn't, go to the parent frame and repeat 1.
  3. If you run out of frames (reach the Global frame and it's not there), complain.

Tips

  1. You can only bind names to values. No expressions (like 3+4) allowed on environment diagrams!
  1. Frames and Functions both have parents.

Guide 2

...