![Page 1: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/1.jpg)
1
The Evaluator
![Page 2: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/2.jpg)
2
Compiler vs. Interpreter
CommandProcessing Unit
The Computer
Program in Low Level Machine Language
Program in High Level Language
Transformation
The Programmer
T
![Page 3: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/3.jpg)
3
A Compiler
CPU
Machine Level Program
High Level Program C
Inputs
Outputs
The Compiler turns the high level program instructions toInstructions understood by the machine.
![Page 4: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/4.jpg)
4
An Interpreter
CPU
High Level Program I
Inputs
Outputs
The Interpreter is a machine level program, which interprets and executes the high level program line after line…
![Page 5: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/5.jpg)
5
The Metacircular Evaluator
CPU
SchemeProgram
Inputs
Outputs
The Metacircular Evaluator is an Interpreter for Scheme on a machine whose machine language is Scheme.
ME
![Page 6: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/6.jpg)
Evaluator
6
Packages:
Core (Evaluation Rules)
Abstract Syntax Parser
Data Structures
First, we introduce the environment model for evaluation of expressions!
To be implemented in the Core
![Page 7: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/7.jpg)
7
The Environments Model
Name Value
Environment Table
23score
Substitution model: a single global environment
Environments model: many environments.
Generalizes the substitution model.
![Page 8: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/8.jpg)
8
Frame: a table of bindings• Binding: a pairing of a name and a value
Example: x is bound to 15 in frame A y is bound to (1 2) in frame A the value of the variable x in frame A is 15
21
x: 15
A
y:
![Page 9: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/9.jpg)
9
Environment: a sequence of frames• Environment E1 consists of frames A and B
z: 10
B
E1
E2
x: 15
A
21
y:
this arrow is calledthe enclosing
environment pointer
• Environment E2 consists of frame B only
• A frame may be shared by multiple environments
![Page 10: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/10.jpg)
10
Evaluation in the environment model
• All evaluations occur in an environment
• The current environment changes when theinterpreter applies a procedure
• The top environment is called the global environment (GE)
• Only the GE has no enclosing environment
![Page 11: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/11.jpg)
11
The Environment Model• A precise, completely mechanical, description of:
• name-rule looking up the value of a variable• define-rule creating a new definition of a var• lambda-rule creating a procedure• application rule applying a procedure
• Basis for implementing a scheme interpreter• for now: draw EM state with boxes and pointers• later on: implement with code
• Enables analyzing arbitrary scheme code, including (when we get there) imperative programming
![Page 12: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/12.jpg)
12
Name-rule• A name X evaluated in environment E gives
the value of X in the first frame of E where X is bound
x: 15A
21
z: 10x: 3
B
E1
GE
y:
• In E1, the binding of x in frame A shadows the binding of x in B
•x | GE ==> 3
•z | GE ==> 10 z | E1 ==> 10 x | E1 ==> 15
![Page 13: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/13.jpg)
13
Define-rule• A define special form evaluated in environment E
creates or replaces a binding in the first frame of E
(define z 25) | E1(define z 20) | GE
z: 25
z: 20
x: 15A
21
z: 10x: 3
B
E1
GE
y:
![Page 14: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/14.jpg)
14
Double bubble: how to draw a procedure
(lambda (x) (* x x))eval
lambda-rule
A compound procthat squares its
argument
#[proc-...]pr
int
Environmentpointer
Code pointer
parameters: xbody: (* x x)
![Page 15: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/15.jpg)
15
Lambda-rule• A lambda special form evaluated in environment E
creates a procedure whose environment pointer points to E
x: 15A
z: 10x: 3
B
E1
parameters: xbody: (* x x)
square:
(define square (lambda (x) (* x x))) | E1
environment pointerpoints to frame A
because the lambdawas evaluated in E1
and E1 AEvaluating a lambda actually returns a pointer to the procedure object
![Page 16: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/16.jpg)
16
To apply a compound procedure P to arguments (Application Rule)
1. Create a new frame A
2. Make A into an environment E: A's enclosing environment pointer goes to the same frame as the environment pointer of P
3. In A, bind the parameters of P to the argument values
4. Evaluate the body of P with E as the current environment
We recommend youmemorize these
four steps
![Page 17: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/17.jpg)
17
(square 4) | GE
x: 10GE
parameters: xbody: (* x x)
square:
A
E1 x: 4
(* x x) | E1
*: #[prim]
==> 16
* | E1 ==> x | E1 ==> 4
square | GE ==>
frame becomes inaccessible
![Page 18: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/18.jpg)
18
Example: inc-square
GE
p: xb: (* x x)
square:inc-square:
p: yb: (+ 1 (square y))
(define square (lambda (x) (* x x))) | GE
(define inc-square (lambda (y) (+ 1 (square y))) | GE
![Page 19: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/19.jpg)
19
Example cont'd: (inc-square 4) | GE
GE
p: xb: (* x x)
square:inc-square:
p: yb: (+ 1 (square y))
E1y: 4
(+ 1 (square y)) | E1
+ | E1 ==> #[prim]
inc-square | GE ==>
(square y) | E1
![Page 20: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/20.jpg)
20
Example cont'd: (inc-square 4) | E1
E2x: 4
(* x x) | E2 * | E2 ==> #[prim] x | E2 ==> 4
GE
p: xb: (* x x)
square:inc-square:
p: yb: (+ 1 (square y)) | E1
E1y: 4
==> 16 (+ 1 16) ==> 17
y | E1 ==> 4square | E1 ==>
frames become inaccessible
![Page 21: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/21.jpg)
39
Explaining Nested Definitions
• Nested definitions : block structure
(define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0))
![Page 22: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/22.jpg)
40
The same x in all subprocedures
(sqrt 2) | GE
GE
p: xb:(define good-enou ..) (define improve ..) (define sqrt-iter ..) (sqrt-iter 1.0)
sqrt:
E1 x: 2good-enough:
p: guessb:(< (abs ….)
improve:sqrt-iter:
guess: 1
sqrt-iterguess: 1
good-enou?
![Page 23: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/23.jpg)
41
message passing example
(define (cons x y) (define (dispatch op) (cond ((eq? op 'car) x) ((eq? op 'cdr) y) (else (error "Unknown op -- CONS" op)))) dispatch)
(define (car x) (x 'car))
(define (cdr x) (x 'cdr))
(define a (cons 1 2))
(car a)
![Page 24: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/24.jpg)
42
(define a (cons 1 2)) | GE
GE
p: x yb:(define (dispatch op) ..) dispatch
cons:
p: xb:(x ‘car)
p: xb:(x ‘cdr)
car: cdr:
E1 x: 1y: 2 dispatch:
p: opb:(cond ((eq? op 'car) x) .... )
a:
![Page 25: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/25.jpg)
43
(car a) | GE
GE
p: x yb:(define (dispatch op) ..) dispatch
cons:
p: xb:(x ‘car)
p: xb:(x ‘cdr)
car: cdr:
E1 x: 1y: 2 dispatch:
p: opb:(cond ((eq? op 'car) x) .... )
a:
x:E2
(x ‘car) | E2 op: ‘carE3
(cond ..) | E3 ==> 1
==> 1
![Page 26: 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language](https://reader035.vdocument.in/reader035/viewer/2022062314/56649f225503460f94c3ac4b/html5/thumbnails/26.jpg)
44
The Environment Evaluation Model
1. To evaluate a combination (a compound expression other than a special form), evaluate the subexpressions and then apply the value of the operator subexpression to the values of the operand subexpressions.
2. To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. To construct this environment, extend the environment part of the procedure object by a frame in which the formal parameters of the procedure are bound to the arguments to which the procedure is applied.