class 32: interpreters

Post on 28-Nov-2014

417 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Meta-Linguistic ProgrammingInterpretersHistory of Object-Oriented ProgrammingImplementing Application

TRANSCRIPT

Class 32:Interpreters

cs1120 Fall 2011David Evans7 November 2011

2

Plan

Implementing InterpretersHistory of Object-Oriented Programming

Problem Set 7: posted today, due Monday 14 November.• Understand the Charme interpreter (described in Chapter 11)• Modify it to change the evaluation rules

Building a Language

Design the grammarWhat strings are in the language?Use BNF to describe all the strings in the language

Make up the evaluation rulesDescribe what every string in the language means

Build an evaluatorImplement a procedure that takes a string in the language as input and an environment and outputs its value:

meval: String Environment → Value

Is this an exaggeration?It is no exaggeration to regard this as the most fundamental idea in programming:

The evaluator, which determines the meaning of expressions in the programming language, is just another program.

To appreciate this point is to change our images of ourselves as programmers. We come to see ourselves as designers of languages, rather than only users of languages designed by others.

Abelson and Sussman, Structure and Interpretation of

Computer Programs (p. 360)

Building an Evaluator

To build an evaluator we need to:Figure out how to represent data in programs

What is a procedure, frame, environment, etc.Implement the evaluation rules

For each evaluation rule, define a procedure that follows the behavior of that rule.

Next: we’ll look at a high-level how the application rule is implementedWednesday and Chapter 11: detailed walk-through of the interpreter

6

Core of the Evaluator

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

To apply a constructed procedure:1. Construct a new environment, whose parent is the

environment of the applied procedure.2. For each procedure parameter, create a place in the frame

of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

Stateful Application Rule

Eval

Apply

Eval and Apply are defined in terms of each other.

10

Object-Oriented Programming

Whirlwind (MIT, 1947-)

Sketchpad (Ivan Sutherland, 1963)

Simula• Considered the first

“object-oriented” programming language

• Language designed for simulation by Kristen Nygaard and Ole-Johan Dahl (Norway, 1962)

• Had special syntax for defining classes that packages state and procedures together

XEROX Palo Alto Research Center (PARC)

1970s:Bitmapped displayGraphical User Interface

Steve Jobs paid $1M to visit and PARC, and returned to make Apple Lisa/Mac

EthernetFirst personal computer (Alto)PostScript PrintersObject-Oriented Programming

Dynabook, 1972

(Just a mockup – couldn’t really build this in 1972)

“Don’t worry about what anybody else is going to do… The best way to predict the future is to invent it. Really smart people with reasonable funding can do just about anything that doesn't violate too many of Newton's Laws!”

— Alan Kay, 1971

Dynabook 1972

Tablet computer intended as tool for learningAlan Kay wanted children to program it alsoHallway argument, Kay claims you could define

“the most powerful language in the world in a page of code”

Proof: Smalltalk Scheme is as powerful, but takes 1½ pages (PS7)

BYTE Magazine,

August 1981

Smalltalk

• Everything is an object• Objects communicate by sending and

receiving messages• Objects have their own state (which may

contain other objects)• How do Smalltalkers do 3 + 4?

send the object 3 the message “+ 4”

Counter in Smalltalk

class name counter instance variable names count new count <- 0 next count <- count + 1 current ^ count

So, who really was the first

object-oriented programmer?

Object-oriented programming is a state of mind where you program thinking about objects that package state and procedures.

Eval

Apply

Implementing the Interpreter

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

evalApplication

22

Representing Expressions

Use Python Lists:['+', '2', '3'] represents (+ 2 3)

How should we represent (+ 2 (+ 3 4)) ?

['+', '2', ['+', '3', '4']]

23

Parsing

parse(<string>) list representing the Charme code

See Chapter 11 (and the PS7 code) for the details on parsing.

>>> parse("(+ 2 (+ 3 4))")[0]['+', '2', ['+', '3', '4']]>>> parse("(define square (lambda (x) (* x x)))")[0]['define', 'square', ['lambda', ['x'], ['*', 'x', 'x']]]

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

evalApplication

25

def evalApplication(expr, env): subexprvals = map (lambda sexpr: meval(sexpr, env), expr) return mapply(subexprvals[0], subexprvals[1:])

26

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

How should we represent an environment?

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

class Environment: def __init__(self, parent): self._parent = parent self._frame = {} def addVariable(self, name, value): ... def lookupVariable(self, name): ...

Details in Chapter 11 (and Wednesday’s class)

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

def mapply(proc, operands): if (isPrimitiveProcedure(proc)): ... elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) for i in range(0, len(params)): newenv.addVariable(params[i], operands[i]) ...

def evalApplication(expr, env): subexprvals = map ( lambda sexpr: meval(sexpr, env), expr) return mapply(subexprvals[0], subexprvals[1:])

To apply a constructed procedure:1. Construct a new environment, whose

parent is the environment of the applied procedure.

2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression.

3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.

def mapply(proc, operands): if (isPrimitiveProcedure(proc)): ... elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) for i in range(0, len(params)): newenv.addVariable(params[i], operands[i])

def mapply(proc, operands): if (isPrimitiveProcedure(proc)): return proc(operands) elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) for i in range(0, len(params)): newenv.addVariable(params[i], operands[i]) return meval(proc.getBody(), newenv)

def evalApplication(expr, env): subexprvals = map ( lambda sexpr: meval(sexpr, env), expr) return mapply(subexprvals[0], subexprvals[1:])

def meval(expr, env): … elif isApplication(expr): return evalApplication(expr, env) …

So, who really was the first

object-oriented programmer?

32

Object-oriented programming is an exceptionally bad idea which could only have originated in California. Edsger Dijkstra

I don’t know how many of you have

ever met Dijkstra, but you probably know

that arrogance in computer science is

measured in nano-Dijkstras.

Alan Kay

The people who are the worst at programming are the people who refuse to accept the fact that their brains aren't equal to the task. Their egos keep them from being great programmers. The more you learn to compensate for your small brain, the better a programmer you’ll be. The more humble you are, the faster you’ll improve.

Edsger Dijkstra, 1972 Turing Awardhttp://www.cs.utexas.edu/users/EWD/ewd03xx/EWD340.PDF

AdaCountess of Lovelace

around 1843

By the word operation, we mean any process which alters the mutual relation of two or more things, be this relation of what kind it may. This is the most general definition, and would include all subjects in the universe. Again, it might act upon other things besides number, were objects found whose mutual fundamental relations could be expressed by those of the abstract science of operations...

36

Charge

Meta-linguistic programming is also a state of mind!

Read Chapter 11 before Wednesday

PS7 Due next Wednesday (delayed from Monday)

top related