-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4-16.scm
45 lines (42 loc) · 1.46 KB
/
4-16.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(load "meta-evaluator.scm")
;; a
(define (lookup-variable-value var env)
(define (env-loop env)
(define (scan vars vals)
(cond ((null? vars) (env-loop (enclosing-environment env)))
((eq? var (car vars))
(if (eq? (car vals) '*unassigned*)
(error "Unassigned variable -- LOOKUP" var)
(car vals)))
(else (scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable -- LOOKUP" var)
(let ((frame (first-frame env)))
(scan (frame-variables frame) (frame-values frame)))))
(env-loop env))
;; b
(define (scan-out-defines body)
(let ((vars '())
(vals '())
(exps '()))
(define (scan seq)
(cond ((null? seq) 'done)
((definition? (car seq))
(set! vars (cons (definition-variable (car seq)) vars))
(set! vals (cons (definition-value (car seq)) vals))
(scan (cdr seq)))
(else
(set! exps (cons (car seq) exps))
(scan (cdr seq)))))
(scan body)
(if (null? vars)
body
(make-let
(map (lambda (x) (list x '*unassigned*)) vars)
(append (map (lambda (x y) (make-set! x y)) vars vals) exps)))))
(define (make-set! x y)
(list 'set! x y))
(define (make-let vars exps)
(cons 'let (cons vars exps)))
;; c
(define (procedure-body p) (scan-out-defines (list-ref p 2) ))