English subtitles

← Updating Output - Programming Languages

Get Embed Code
3 Languages

Showing Revision 2 created 05/25/2016 by Udacity Robot.

  1. Well, our previous approach only works if we treat calls to "write" specially,
  2. and we're careful to notice every call to "write" and append to this "javascript output"
  3. variable in the global environment.
  4. So let's work it out together.
  5. I'll do the first bit, and you'll finish it off.
  6. We're going to go back to our JavaScript interpreter and finish up our handling
  7. of "write," a special function call.
  8. Well, function calls are expressions, so we're in evaluating expression.
  9. Here's the abstract syntax tree or the parse tree for the expression.
  10. Here's the current environment.
  11. We need to figure out the type of the expression.
  12. We've already seen how to do binops, numbers, that sort of thing.
  13. So the one we really care about this time is function calls.
  14. So this is what we had before.
  15. A function call has a name and some actual arguments.
  16. Looks a bit like this.
  17. If the function call is myfun(a, 3+4), the function name is myfun,
  18. and the arguments are a list of expressions.
  19. We're going to go look up the function name in the environment.
  20. Now "write" is one of those special functions that users get to call,
  21. but they never actually provide a definition for.
  22. So we're not going to find it in the environment.
  23. Our environment lookup returns none if the function is "write,"
  24. but it also returns none if the function is just unknown,
  25. so we'll check for "write" specially.
  26. If so, "write" should have a single argument, so we'll evaluate it.
  27. We'll get the current output that the user has written through previous calls to "write."
  28. I'm doing an environment lookup in the current environment instead of the global environment.
  29. Here's a trick though.
  30. Remember how environment lookups work.
  31. If I don't have it in my pockets, I check my hotel room. I check back home.
  32. I ask my parents. I look around on the web.
  33. I keep going back until eventually I hit the global environment.
  34. Because we've specially picked a variable name with a space in it,
  35. noone else can possibly make this variable.
  36. So even if I'm not in the global environment, I won't have it. My hotel room won't have it.
  37. My house won't have it.
  38. Eventually, I'll get back to the global environment, which we know has it
  39. because we were careful to put it in explicitly starting as the empty string last time.
  40. So the quiz for you is to fill in these 2 blanks.
  41. We figured out the output so far, and now we want to update the environment
  42. so that "javascript output" takes on a new value.
  43. What should go in here to make that work out?