Begin Expression Values Rule

If LW Python is in the "begin_expr" state and "Expression" is a function call, then prepend an empty list to the value stack. More formally:

if the following are true:
  • expression state at time t = "begin_expr"
  • the expression at time t = function call with name: name and arguments: args

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

For example, if the following are true:
  • expression state at time 1 = "begin_expr"
  • the expression at time 1 = shift(a)

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

LW Python evaluates expressions and stores the results in the value stack.

The value stack is a list whose elements are also lists. Initially, the stack is an empty list. When LW Python begins to execute an expression, it prepends an empty child list to the stack. As the arguments of the function call are evaluated, the results will be added to this child list.

Why is this child list necessary? Why don't we just add results directly to the stack? If a function call argument (child) is also a function call, then when that child is evaluated, Python creates a new list to store the grandchildren values. This is to separate the grandchildren values from the children values. Thus, there can be many lists in the stack, one for each descendent "level."

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments