-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocedure.scm
More file actions
47 lines (37 loc) · 1.28 KB
/
procedure.scm
File metadata and controls
47 lines (37 loc) · 1.28 KB
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
46
47
;; procedure.scm
;;
;; Interface:
;; (lookup-variable-value var env)
;; Procedure structure:
;; ('procedure paras body env)
;; Requires "environment.scm", and we load it in main.scm.
(define (make-procedure parameters body env)
(list 'procedure parameters body env))
;; 'compound-procedure?' is already used in Scheme.
(define (compound-procedure? p) (tagged-list? p 'procedure))
(define (procedure-parameters p) (cadr p))
(define (procedure-body p) (caddr p))
(define (procedure-environment p) (cadddr p))
(define apply-in-underlying-scheme apply)
;; lookup-variable-value
;; Searches given variable start from first frame.
(define (lookup-variable-value var env)
(define (env-loop env)
(define (scan vars vals)
(cond ((null? vars)
(env-loop (enclosing-environment env)))
((eq? (car vars) var)
;; (car vals))
(if (eq? (car vals) '*unassigned*)
(error "Variable unassigned --lookup-variable-value")
(car vals)))
(else
(scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable -- lookup-variable-value: " var)
(let ((frame (first-frame env)))
(scan (frame-variables frame) (frame-values frame)))))
;; (newline)
;; (display "lookup-variable-value: ")
;; (user-print var)
(env-loop env))