-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.scm
67 lines (51 loc) · 2 KB
/
eval.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
;; eval.scm
(define (first-exp seq) (car seq))
(define (println data)
(newline)
(display data)
(newline))
(define (eval exp env)
(cond ((self-evaluating? exp) exp)
((name? exp) (lookup-variable exp env))
((quote? exp) (quote-content exp))
((assign? exp) (eval-assign exp env))
((func? exp) (eval-func exp env)) ; TODO
((if? exp) (eval-if exp env))
((make-proc? exp) (build-procedure (make-proc-params exp)
(make-proc-body exp)
env))
((begin? exp) (eval-sequence (begin-exps exp) env)) ; TODO
((cond? exp) (eval (cond->if exp) env))
((application? exp)
(apply (eval (operator exp) env)
(list-of-values (operands exp) env)))
(else (error "Unknown expression -- EVAL" exp))))
(define (list-of-values exps env)
(if (no-operands? exps)
'()
(cons (eval (first-operand exps) env)
(list-of-values (rest-operand exp) env))))
(define (eval-if exp env)
(if (true? (eval (if-pred exp) env))
(eval (if-conseq exp) env)
(eval (if-altern exp) env)))
(define (eval-sequence exps env)
(cond ((last-exp? exps) (eval (first-exp exps) env))
(else (eval (first-exp exps) env)
(eval-sequence (rest-exps exp) env))))
(define (eval-assign exp env)
(set-var-value! (assign-var exp)
(eval (assign-value exp) env)
env)
'done)
(define (eval-func exp env)
(define-variable! (func-name exp) (eval (func-value exp) env) env)
'done)
(define (apply proc args)
(cond ((primitive-proc? proc) (apply-primitive-proc proc args))
((compound-proc? proc)
(eval-sequence (proc-body proc)
(extend-env (proc-params proc)
args
(proc-env proc))))
(else (error "Unknown proc type -- APPLY" proc))))