Pop Stack Line Rule

After a function has finished executing, Python returns to the line where the function was called (call site). The line number was saved in the stack before function execution and is now restored. 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 the line at time (t + 1) = line

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 the line at time (26 + 1) = 5

Try stepping through the simulator to see LW Python restore the line number from the stack.

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments