260 likes | 388 Views
The Evaluator. Compiler vs. Interpreter. The Programmer. The Computer. Transformation. Command Processing Unit. T. Program in High Level Language. Program in Low Level Machine Language. A Compiler. Inputs. C. High Level Program. Machine Level Program.
E N D
Compiler vs. Interpreter The Programmer The Computer Transformation Command Processing Unit T Program in High Level Language Program in Low Level Machine Language
A Compiler Inputs C High Level Program Machine Level Program The Compiler turns the high level program instructions to Instructions understood by the machine. CPU Outputs
An Interpreter Inputs I High Level Program CPU Outputs The Interpreter is a machine level program, which interprets and executes the high level program line after line…
The Metacircular Evaluator Inputs ME Scheme Program CPU Outputs The Metacircular Evaluator is an Interpreter for Scheme on a machine whose machine language is Scheme.
Evaluator 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
Substitution model: a single global environment Environment Table Name Value score 23 The Environments Model Environments model: many environments. Generalizes the substitution model.
Example: x is bound to 15 in frame Ay is bound to (1 2) in frame A the value of the variable x in frame A is 15 A x: 15 y: 1 2 Frame: a table of bindings • Binding: a pairing of a name and a value
B z: 10 E2 A x: 15 E1 y: 1 2 Environment: a sequence of frames • Environment E1 consists of frames A and B • Environment E2 consists of frame B only • A frame may be shared by multiple environments this arrow is calledthe enclosingenvironment pointer
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
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 • Enables analyzing arbitrary scheme code, including (when we get there) imperative programming • Basis for implementing a scheme interpreter • for now: draw EM state with boxes and pointers • later on: implement with code
In E1, the binding of x in frame A shadows the binding of x in B B • x | GE ==> 3 z: 10x: 3 GE A x: 15 E1 y: 1 2 Name-rule • A name X evaluated in environment E givesthe value of X in the first frame of E where X is bound • z | GE ==> 10 z | E1 ==> 10 x | E1 ==> 15
B z: 10 x: 3 GE z: 20 A x: 15 y: E1 1 2 Define-rule • A define special form evaluated in environment Ecreates or replaces a binding in the first frame of E (define z 25) | E1 (define z 20) | GE z: 25
(lambda (x) (* x x)) print Environmentpointer #[proc-...] evallambda-rule A compound procthat squares itsargument Code pointer parameters: xbody: (* x x) Double bubble: how to draw a procedure
B z: 10x: 3 A x: 15 E1 square: Evaluating a lambda actually returns a pointer to the procedure object parameters: xbody: (* x x) Lambda-rule • A lambda special form evaluated in environment Ecreates a procedure whose environment pointer points to E (define square (lambda (x) (* x x)))| E1 environment pointerpoints to frame Abecause the lambdawas evaluated in E1and E1 A
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 thesefour steps
A E1 frame becomes inaccessible (square 4) | GE (* x x) | E1 x: 10 *: #[prim] GE square: parameters: xbody: (* x x) x: 4 square | GE ==> ==> 16 x | E1 ==> 4 * |E1 ==>
inc-square: square: p: xb: (* x x) p: yb: (+ 1 (square y)) Example: inc-square (define square (lambda (x) (* x x))) | GE GE (define inc-square (lambda (y) (+ 1 (square y))) | GE
inc-square: GE square: E1 p: xb: (* x x) p: yb: (+ 1 (square y)) Example cont'd: (inc-square 4) | GE y: 4 inc-square | GE ==> (+ 1 (square y)) | E1 + |E1 ==> #[prim] (square y) | E1
E2 E1 frames become inaccessible Example cont'd: (inc-square 4) | E1 inc-square: GE square: y: 4 x: 4 p: xb: (* x x) p: yb: (+ 1 (square y)) | E1 square | E1 ==> y | E1 ==> 4 (* x x) | E2 (+ 1 16) ==> 17 ==> 16 * |E2 ==> #[prim] x | E2 ==> 4
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))
sqrt: GE E1 x: 2 good-enough: improve: sqrt-iter: p: xb:(define good-enou ..) (define improve ..) (define sqrt-iter ..) (sqrt-iter 1.0) p: guessb:(< (abs ….) The same x in all subprocedures (sqrt 2) | GE guess: 1 sqrt-iter guess: 1 good-enou?
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)
cons: GE E1 x: 1 y: 2 p: x yb:(define (dispatch op) ..) dispatch dispatch: p: op b:(cond ((eq? op 'car) x) .... ) (define a (cons 1 2)) | GE car: cdr: a: p: xb:(x ‘cdr) p: xb:(x ‘car)
cons: GE p: x yb:(define (dispatch op) ..) dispatch E2 x: p: op b:(cond ((eq? op 'car) x) .... ) op: ‘car E3 ==> 1 (car a) | GE car: cdr: a: E1 x: 1 y: 2 p: xb:(x ‘cdr) p: xb:(x ‘car) dispatch: (x ‘car) | E2 (cond ..) | E3 ==> 1
The Environment Evaluation Model • To evaluatea 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. • 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.