Your SlideShare is downloading. ×
  • Like
Class 32: Interpreters
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Class 32: Interpreters

  • 222 views
Published

Meta-Linguistic Programming …

Meta-Linguistic Programming
Interpreters
History of Object-Oriented Programming
Implementing Application

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
222
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
5
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Class 32: Interpreterscs1120 Fall 2011David Evans7 November 2011
  • 2. PlanImplementing 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 2
  • 3. Building a LanguageDesign the grammar What strings are in the language? Use BNF to describe all the strings in the languageMake up the evaluation rules Describe what every string in the language meansBuild 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 thisas the most fundamental idea inprogramming: The evaluator, which determines the meaning of expressions in the programming language, is just another program.To appreciate this point is to changeour images of ourselves asprogrammers. We come to seeourselves as designers of Abelson andlanguages, rather than only users of Sussman, Structure andlanguages designed by others. Interpretation of Computer Programs (p. 360)
  • 5. Building an EvaluatorTo 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
  • 6. Core of the Evaluator 6
  • 7. 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))
  • 8. Stateful Application RuleTo 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.
  • 9. Eval andApply are Evaldefined interms of eachother. Apply
  • 10. 10
  • 11. Object-Oriented ProgrammingWhirlwind (MIT, 1947-) Sketchpad (Ivan Sutherland, 1963)
  • 12. SimulaConsidered the first “object-oriented” programming languageLanguage 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 displayGraphical User Interface Steve Jobs paid $1M to visit and PARC, and returned to make Apple Lisa/MacEthernetFirst personal computer (Alto)PostScript PrintersObject-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 doesnt violate too many of Newtons Laws!” — Alan Kay, 1971 (Just a mockup – couldn’t really build this in 1972)
  • 15. Dynabook 1972Tablet 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)
  • 16. BYTEMagazine, August 1981
  • 17. SmalltalkEverything is an objectObjects communicate by sending and receiving messagesObjects 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 Smalltalkclass name counter instance variable names count new count <- 0 next count <- count + 1 current ^ count
  • 19. So, who really was the firstobject-oriented programmer? Object-oriented programming is a state of mind where you program thinking about objects that package state and procedures.
  • 20. Implementingthe Interpreter Eval Apply
  • 21. evalApplicationTo 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 ExpressionsUse Python Lists: [+, 2, 3] represents (+ 2 3)How should we represent (+ 2 (+ 3 4)) ? [+, 2, [+, 3, 4]] 22
  • 23. Parsingparse(<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. evalApplicationTo 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.
  • 25. def evalApplication(expr, env): subexprvals = map (lambda sexpr: meval(sexpr, env), expr) return mapply(subexprvals[0], subexprvals[1:]) 25
  • 26. To apply a constructed procedure: How should we1. 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 = parent3. 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): elif1. 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.
  • 30. 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) …
  • 31. So, who really was the firstobject-oriented programmer?
  • 32. Object-orientedprogramming isan exceptionallybad idea whichcould only haveoriginated inCalifornia.Edsger Dijkstra 32
  • 33. I don’t know how many of you haveever 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 atprogramming are the people who refuse toaccept the fact that their brains arent equalto the task. Their egos keep them from beinggreat programmers. The more you learn tocompensate for your small brain, the bettera programmer you’ll be. The more humbleyou 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 meanany process which alters themutual relation of two or morethings, be this relation of whatkind it may. This is the mostgeneral definition, and wouldinclude all subjects in the universe.Again, it might act upon otherthings besides number, wereobjects found whose mutualfundamental relations could be Adaexpressed by those of the abstract Countess of Lovelacescience of operations... around 1843
  • 36. ChargeMeta-linguistic programming is also a state of mind!Read Chapter 11 before WednesdayPS7 Due next Wednesday (delayed from Monday) 36