Date post: | 22-Jun-2015 |
Category: |
Technology |
Upload: | david-evans |
View: | 276 times |
Download: | 0 times |
Class 33:Diving Deep(er) Into TheCharme Evaluator
Guest Lecture byIvan Alagenchev
Recall From Last Lecture
def meval(expr, env): 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))
The Core of Evaluator
public 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 = ""; }
...else {
throw new EvalError("Unknown expression type:" }return returnval;}
Equivalent in Java
Verbose, but not much different!!!
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): 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))
Let's take a look
def isName(expr): return isinstance(expr, str)
public static boolean isName(Object expr) {return (expr instanceof String);
}
Python
Java
An ExampleConsider: (define test 6)
(+ test 1)
Recall:
def evalApplication(expr, env): subexprs = expr subexprvals = map (lambda sexpr: meval(sexpr, env), subexprs) return mapply(subexprvals[0], subexprvals[1:])
['+', 'test', '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
def evalName(expr, env): assert isName(expr) return env.lookupVariable(expr)
What about evalName?
To fully understand this, we need to understand how our Environment works
But first ...
For a Chance To Earn Some Candy
On the first US flag, why were the 13 stars sewn in a circle?
class Environment: def __init__(self, parent): self._parent = parent self._frame = {} def addVariable(self, name, value): self._frame[name] = value def lookupVariable(self, name):
...
Back To Environment
Start Off With Empty Dictionary
The global environment has no parentThe frame contains the places that have been definedPlaces are name-value tuples, but values can change
If the current evaluation environment has no parent, the name is not defined and the expression evaluates to an error.
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.
Let's Translate English to Python
def lookupVariable(self, name):
lookupVariable:
Let's Finish It Together
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);}}
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))
evalDefinition:
Charge
You should have a good understanding of 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.
Don't Forget - PS7 is due Wednesday