Hello! This is a guide on making environment diagrams for 61A. If you have this playbook open, you should be able to navigate most 61A diagramming questions.
Steps to Diagramming — The Big Picture
- Draw the current frame. We start in the very beginning by drawing the global frame. If current frame is not the global frame, make sure to add the function's arguments to the frame.
- Start assigning values to variables.
- Re-assign variables if they already exist in the current frame and they are getting overwritten.
- When looking up values of a variable, look in the current frame. If they aren't in the current frame, look in the parent frame. Keep going until variable is found. If not found, Error.
- If there is a function call, find the function being called and evaluate the arguments. Then open a new frame, and start back at step 1 in this new frame.
- If you reach a return statement, return the value from the frame and close the frame. Go back to where you stopped in the frame that called this function, and continue from step 2.
- If you reach the end without a return statement, simply close the frame and return to where you stopped in the frame that called this function. If you are in the global frame, you're done — flip to the next question.
The Environment Diagram Commandments
Things to keep in mind!
- The parent of a function is the frame in which it was created.
- We cannot have two variables with the same name in the same frame.
- You can read the values in your parent frame, as well as your grandparent frame, and so on...
- You cannot change any values in your parent frame.