Lecture33

438 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
438
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture33

  1. 1. Class 33:Diving Deep(er) Into TheCharme EvaluatorGuest Lecture byIvan Alagenchev
  2. 2. Recall From Last Lecturedef meval(expr, env): The Core of Evaluator if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error (Unknown expression type: + str(expr))
  3. 3. Equivalent in Javapublic static Object meval(Object expr, Environment env) {Object returnval;If ( isPrimitive(expr) ) { returnval = evalPrimitive(expr); }else if ( isIf(expr) ) { returnval = evalIf(expr, env); }else if ( isCond(expr) ) { returnval = evalCond(expr, env); }else if ( isDefinition(expr) ){ evalDefinition(expr, env); returnval = ""; }... Verbose, but not much different!!!else { throw new EvalError("Unknown expression type:" }return returnval;}
  4. 4. The if statement matches the input expression with the corresponding expression type or definition: if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): Lets take a look return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error (Unknown expression type: + str(expr))
  5. 5. def isName(expr): return isinstance(expr, str) Pythonpublic static boolean isName(Object expr) { Java return (expr instanceof String);}
  6. 6. An ExampleConsider: (define test 6) More on this later (+ test 1) [+, test, 1]Recall: def evalApplication(expr, env): subexprs = expr subexprvals = map (lambda sexpr: meval(sexpr, env), subexprs) return mapply(subexprvals[0], subexprvals[1:]) Because of map, meval is called with test sub-expression too ! test is an instance of string object, so isinstance evaluates to true In isName from previous slide
  7. 7. What about evalName? def evalName(expr, env): assert isName(expr) return env.lookupVariable(expr)To fully understand this, we need to understand how our Environment works But first ...
  8. 8. For a Chance To Earn Some Candy On the first US flag, why were the 13 stars sewn in a circle?
  9. 9. Back To Environmentclass Environment: def__init__(self, parent): self._parent = Start Off With Empty Dictionaryparent self._frame = {} defaddVariable(self, name, value): self._frame[name] = The global environment has no parentvalue The frame contains the places that have been defined Places are name-value tuples, but values can change deflookupVariable(self, name): ...
  10. 10. Recall the Lookup Rules First, we search the current environment’s frame for a place with a name that matches the name in the expression. If we have a match, the value in that place is the value of the expression Else we evaluate the name expression in the parent environment. If the current evaluation environment has no parent, the name is not defined and the expression evaluates to an error.
  11. 11. Lets Translate English to Python
  12. 12. lookupVariable:def lookupVariable(self, name): Lets Finish It Together
  13. 13. lookupVariable Cont. def lookupVariable(self, name): if self._frame.has_key(name): return self._frame[name] elif (self._parent): return self._parent.lookupVariable(name) else: evalError(Undefined name: %s % (name))public Object lookupVariable(String name) throws EvalError{if (frame.containsKey(name)) { return frame.get(name);}else if (parent != null) { return parent.lookupVariable(name);}else { throw new EvalError("Undefined name "+name);}}
  14. 14. evalDefinition:def evalDefinition(expr, env): assert isDefinition(expr) if len(expr) != 3: evalError (Bad definition: %s % str(expr)) name = expr[1] if isinstance(name, str): value = meval(expr[2], env) env.addVariable(name, value) else: evalError (Bad definition: %s % str(expr))
  15. 15. ChargeYou should have a good understandingof the Charme interpreter now.We have not explained in detail all primitive operations,parsing and evaluating lambdas.Though they should be easy to understand now. Dont Forget - PS7 is due Wednesday

×