New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Class 32: Interpreters
1. Class 32:
Interpreters
cs1120 Fall 2011
David Evans
7 November 2011
2. Plan
Implementing Interpreters
History 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
2
3. Building a Language
Design the grammar
What strings are in the language?
Use BNF to describe all the strings in the language
Make up the evaluation rules
Describe what every string in the language means
Build an evaluator
Implement a procedure that takes a string in the
language as input and an environment and outputs its
value:
meval: String Environment → Value
4. 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 Abelson and
languages, rather than only users of Sussman, Structure and
languages designed by others. Interpretation of Computer
Programs (p. 360)
5. 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 implemented
Wednesday and Chapter 11: detailed walk-through of the interpreter
8. Stateful Application Rule
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.
12. 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
13. XEROX Palo Alto Research Center (PARC)
1970s:
Bitmapped display
Graphical User Interface
Steve Jobs paid $1M to visit and
PARC, and returned to make
Apple Lisa/Mac
Ethernet
First personal computer (Alto)
PostScript Printers
Object-Oriented Programming
14. Dynabook, 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
(Just a mockup –
couldn’t really
build this in 1972)
15. Dynabook 1972
Tablet computer intended as tool for learning
Alan Kay wanted children to program it also
Hallway 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)
17. 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”
18. Counter in Smalltalk
class name counter
instance variable names count
new count <- 0
next count <- count + 1
current ^ count
19. 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.
21. evalApplication
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.
22. Representing Expressions
Use Python Lists:
['+', '2', '3'] represents (+ 2 3)
How should we represent (+ 2 (+ 3 4)) ?
['+', '2', ['+', '3', '4']]
22
23. Parsing
parse(<string>)
list representing the Charme code
>>> parse("(+ 2 (+ 3 4))")[0]
['+', '2', ['+', '3', '4']]
>>> parse("(define square (lambda (x) (* x x)))")[0]
['define', 'square', ['lambda', ['x'], ['*', 'x', 'x']]]
See Chapter 11 (and the PS7 code) for the details on parsing.
23
24. evalApplication
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.
26. To apply a constructed procedure: How should we
1. Construct a new environment, whose represent an
parent is the environment of the
applied procedure. environment?
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.
26
27. 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 valueEnvironment:
class in
each place to the value of thedef __init__(self, parent):
corresponding operand expression.
self._parent = parent
3. Evaluate the body of the procedure in
the newly created environment.self._frame = {}
The
resulting value is the value of def addVariable(self, name, value):
the
application.
...
def lookupVariable(self, name):
...
Details in Chapter 11 (and Wednesday’s class)
28. To apply a constructed procedure:
1. Construct a new environment, whose
parent is the environment of the
applied procedure. def evalApplication(expr, env):
2. For each procedure parameter, create subexprvals = map (
a place in the frame of the new lambda sexpr: meval(sexpr, env),
environment with the name of the
expr)
parameter. Evaluate each operand
expression in the environment of the return mapply(subexprvals[0],
application and initialize the value in subexprvals[1:])
each place to the value of the
corresponding operand expression.
3. Evaluate the body of the procedure in
the newly created environment. The
def mapply(proc, operands):
resulting value is the value ofif (isPrimitiveProcedure(proc)): ...
the
application. elif isinstance(proc, Procedure):
params = proc.getParams()
newenv = Environment(proc.getEnvironment())
for i in range(0, len(params)):
newenv.addVariable(params[i], operands[i])
...
29. def mapply(proc, operands):
if (isPrimitiveProcedure(proc)): ...
To apply a constructed procedure:isinstance(proc, Procedure):
elif
1. Construct a new environment, whose= proc.getParams()
params
parent is the environment ofnewenv = Environment(proc.getEnvironment())
the
applied procedure.
for i in range(0, len(params)):
2. For each procedure parameter, create
newenv.addVariable(params[i], operands[i])
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.
33. 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
34. 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 Award
http://www.cs.utexas.edu/users/EWD/ewd03xx/EWD340.PDF
35. 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
Ada
expressed by those of the abstract
Countess of Lovelace
science of operations...
around 1843