Prepend Return Value Rule

If the state at time t is "return," then prepend the "Return Value" to the first element of the "Values" stack.

if the following are true:
  • expression state at time t = "return"
  • Value Stack at time t = [ v, w ]
  • Return Value at time t = r

then Value Stack at time (t + 1) = [ [ r, v ], w ]

When the state is "return," the return value is simply copied into the values stack. The values are later used to evaluate the parent expression.

For example, if the following are true:

  • expression state at time 5 = "return"
  • Value Stack at time 5 = [ [ ], [ ] ]
  • Return Value at time 5 = 5

then Value Stack at time (5 + 1) = [ [ 5, [ ] ], [ ] ]

Try stepping through the simulator to see the "Return Value" added to the top of the "Values" stack.

Because we are prepending rather than appending to the "Values" list, the list is the reverse of the arguments. For example, after the arguments are processed, we might expect the first element of "Values" to be:

[5, [2, []]]

But we actually see:

[2, [5, []]]

Why don't we just append elements instead of prepending? Appending would certainly reduce confusion, but prepending to a LW list is much simpler than appending. Thus, we prepend values until all the arguments have been processed and reverse the list at the end when needed.

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments