170 likes | 263 Views
A Model For The Python Interpreter. What happens when you…. Type in an expression (anything other than assignment statement…for now)? Answer: the expression is evaluated (and its textual representation is printed if you are in the interactive environment of IDLE)
E N D
What happens when you… • Type in an expression (anything other than assignment statement…for now)? • Answer: the expression is evaluated (and its textual representation is printed if you are in the interactive environment of IDLE) • Rule #1: Basic expressions get evaluated to themselves • Rule #2: Compound expressions get evaluated in meaningful ways ( exp1 + exp2 => value of exp1 + value of exp2) • Rule #3: Names get evaluated to the values assigned to them before (more on this later) • Rule #4: Function invocations? Hmmm…
What happens when you… • Type in an assignment statement? [a = 5] • Answer: Several things happen. • Python creates a table before everything runs • The right-hand-side of the equal sign is evaluated (following the rules in the last slide) • If the left-hand-side is a name, look it up in the table. If found, change its value; otherwise create a new entry with the name, and the value from step 2. • (Provisional) If the left-hand-side is a location, e.g.,myName[4], then (a) look up the name (or fail) (b) change the "thing" at that location to the resulted value
Rules For Interpreting Definition StatementHow To Put A Spell Into Spellbook • Select an available slot in the heap (backpack), and associate that slot with your function name in the function table (spellbook) • In that slot, record the name of the argument (reagent), by which you refer to it within the body of function (incantation) • Also record the body of the function (incantation)
Function Application ExpressionSpell Invocation • Examples • f(4) • myFunc(3, 5) • anyFunc() • … • General form: • expr1() or expr1(expr2) or expr1(expr2, expr3) or … • If expr1 evaluates to a Function, it's a function application expression • For now, expr1 will always be a name
Function Application Rules • Evaluate the argument, using the current variable table • Create a new blank variable table on top of the original variable table (enter a dungeon), and the name of the argument (according to the definition) gets the value from step 1. • Interpret the body of the function as if it is a separate program (the content of the original variable table is not available to you) • Upon return (or reaching the end of the function), throw away the new variable table (dungeon map) and go back to the original (leave the dungeon). The function application expression is evaluated to the value of the return expression
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 t = t + y return t y = addOne(x)
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 t = t + y return y y = addOne(x)
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)
Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)
To Avoid Insanity • Whenever you use a name within a function definition, make sure it's either one of the argument(s) or has been assigned a value before in the same definition • Try not to modify the argument within the body of the function • Never declare functions within other functions • There are legitimate programs (in fact, almost all) that violate the above rules, but don't do that yet.