1 / 58

The interpreter

The interpreter. Derived Expressions. (define derived? ( lambda (exp) ( or (if? exp) (function-definition? exp) (let? exp )))). Shallow-derive. (define shallow-derive ( lambda (exp) ( cond ((if? exp) (if-> cond exp)) (( function-definition? exp)

lumina
Download Presentation

The interpreter

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The interpreter

  2. Derived Expressions (define derived? (lambda (exp) (or (if? exp) (function-definition? exp) (let? exp))))

  3. Shallow-derive (define shallow-derive (lambda (exp) (cond ((if? exp) (if->cond exp)) ((function-definition? exp) (function-define->define exp)) ((let? exp) (let->combination exp)) (else (error ’shallow-derive "unhandled derivation: ~s" exp)))))

  4. Recursive derivation (define (derive exp) (if (atomic? exp) exp (let ((mapped-derive-exp (map derive exp))) (if (not (derived? exp)) mapped-derive-exp (shallow-derive mapped-derive-exp)))

  5. Concrete derivation

  6. Cont.

  7. let

  8. Function-definition

  9. Reminder

  10. Core (applicative-substitution) • Language expressions • ASP • The global environment • Data structures+core • Values • Data structures+ core

  11. We will start with the data structures, that actually do most of the work • Then the core – evaluation rules will be rather easy

  12. Values The Primitive-procedure ADT: 1. Constructor make-primitive-procedure: Attaches a tag to an implemented code argument. Type: [T -> Primitive-procedure]. 2. Identification predicate primitive-procedure?. Type: [T –> Boolean]. 3. Selector primitive-implementation: It retrieves the implemented code from a primitive procedure value. Type: [Primitive-procedure –> T].

  13. Usage

  14. (User) Procedure • (define (make-procedure pars body) (attach-tag (cons pars body) ‘procedure)) …..

  15. Other values (define (make-value x) (attach-tag (list x) ‘value))

  16. Environment

  17. frame

  18. Initialization with primitives

  19. Before introducing the constructor: A word on mutation • So far we kept our code fully functional • That is, no state required • “define” is somewhat an exception, but mainly for convenience • Recursive function definition is an exception to that, can be done without define, but requires fixpoint operation • For implementing the global environment we want to be able to change bindings • A functional solution is possible but messy • We use an abstraction of mutation through the Box ADT of Dr. Racket • It basically “wrappes” the object and allows (sort of ) a “pointer” to it

  20. Global-env constructor

  21. Defining the global-env (define the-global-environment (make-the-global-environment)) This expression is in the global scope of the interperter…

  22. Selector:lookup

  23. MUTATOR:add-binding!

  24. Binding ADT

  25. Using the environment

  26. Eval-apply loop (applicative-eval exp) (apply-procedure proc args) Mutually recursive (why?)

  27. eval

  28. Eval-atomic

  29. Lambda+definitions

  30. If

  31. application

  32. Apply-primitive

  33. substitute

  34. “main” code of substitute

  35. rename(exp)

  36. “main” code of rename

  37. Environment Model • Formal algorithm • Interpreter

  38. Environment Model (Diagram)

  39. Dynamic env-model

  40. Interpreter for env. model • What should be changed?

  41. List of main changes • ASP stays intact • Data structures: • Support an Environment ADT • Procedure ADT should include an environment pointer • Core: • Every evalfunction gets an environment as an additional parameter • Uses it e.g. for lookup, and passes it on to recursive eval and apply calls • apply of user procedures (closures) change to manage envs.

  42. env

More Related