+ All Categories
Home > Technology > Lecture33

Lecture33

Date post: 22-Jun-2015
Category:
Upload: david-evans
View: 276 times
Download: 0 times
Share this document with a friend
Popular Tags:
16
Class 33: Diving Deep(er) Into The Charme Evaluator Guest Lecture by Ivan Alagenchev
Transcript
Page 1: Lecture33

Class 33:Diving Deep(er) Into TheCharme Evaluator

Guest Lecture byIvan Alagenchev

Page 2: Lecture33

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

Page 3: Lecture33
Page 4: Lecture33

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!!!

Page 5: Lecture33

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

Page 6: Lecture33

def isName(expr): return isinstance(expr, str)

public static boolean isName(Object expr) {return (expr instanceof String);

}

Python

Java

Page 7: Lecture33

An ExampleConsider: (define test 6)

(+ test 1)

More on this later

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

Page 8: Lecture33

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 ...

Page 9: Lecture33

For a Chance To Earn Some Candy

On the first US flag, why were the 13 stars sewn in a circle?

Page 10: Lecture33

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

Page 11: Lecture33

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.

Page 12: Lecture33

Let's Translate English to Python

Page 13: Lecture33

def lookupVariable(self, name):

lookupVariable:

Let's Finish It Together

Page 14: Lecture33

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);}}

Page 15: Lecture33

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:

Page 16: Lecture33

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


Recommended