Pop Stack Variables Rule

After a function has finished executing, Python returns to the line where the function was called (call site). The call site context, which was saved in the stack before function execution, is now restored, including the "Variables" map. More formally,

if the following are true:
  • the line at time t = i
  • the tab at time t = j
  • statement at line i, tab j = return rex
  • stack at time t = [ program context with variables: varis and expression state: (expression state with parent stack: parent_stack arguments: arg_stack values: value_stack line: line tab: tab) and control map: control_map, rest ]
  • expression state at time t = "end_expr"

then Variables Map at time (t + 1) = varis

For example, if the following are true:

  • the line at time 26 = 3
  • the tab at time 26 = 1
  • statement at line 3, tab 1 = return z
  • stack at time 26 = [ program context with variables: [ entry a: 5, [ ] ] and expression state: (expression state with parent stack: [ ] arguments: [ ] values: [ ] line: 5 tab: 0) and control map: [ ], [ ] ]
  • expression state at time 26 = "end_expr"

then Variables Map at time (26 + 1) = [ entry a: 5, [ ] ]

Try stepping through the simulator to see LW Python restore the "Variables" map from the stack.

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments