3 (require (rename-in racket/base
4 (apply apply-in-underlying-scheme)
5 (eval eval-in-underlying-scheme)))
7 (provide (all-defined-out))
9 (define (self-evaluating? expr)
16 (define (variable? expr) (symbol? expr))
19 (define (eval-sequence exps env)
21 [(last-exp? exps) (eval (first-exp exps) env)]
22 [else (eval (first-exp exps) env)
23 (eval-sequence (rest-exps exps) env)]))
26 (define (last-exp? seq) (null? (cdr seq)))
27 (define (first-exp seq) (car seq))
28 (define (rest-exps seq) (cdr seq))
31 (define (make-lambda parameters body)
32 (cons 'lambda (cons parameters body)))
34 ;; environment data structures
35 (define (enclosing-environment env) (cdr env))
36 (define (first-frame env) (car env))
37 (define the-empty-environment '())
39 (define (make-frame variables values)
40 (let ([ht (make-hash)])
41 (for-each (lambda (var val)
42 (hash-set! ht var val))
47 (define (frame-variables frame)
50 (define (frame-values frame)
53 (define (add-binding-to-frame! var val frame)
54 (hash-set! frame var val))
56 ;; environment is a list of frames, most recent being the last
57 ;; one consed into the list
58 (define (extend-environment vars vals base-env)
59 (let ([frame (make-frame vars vals)])
60 (cons frame base-env)))
62 (define (lookup-variable-value var env)
63 (if (eq? env the-empty-environment)
64 (error "unbound variable: " var)
65 (let ([frame (first-frame env)])
66 (let ([value (hash-ref frame var (lambda () (lookup-variable-value var (enclosing-environment env))))])
67 (if (eq? value '*unassigned*)
68 (error "evaluating a variable that is not assigned a value -- " var)
71 (define (set-variable-value! var val env)
72 (if (eq? env the-empty-environment)
73 (error "unbound variable: " var)
74 (let ([frame (first-frame env)])
75 (if (hash-has-key? frame var)
76 (hash-set! frame var val)
77 (set-variable-value! var val (enclosing-environment env))))))
79 (define (define-variable! var val env)
80 (let ([frame (first-frame env)])
81 (if (hash-has-key? frame var)
82 (hash-set! frame var val)
83 (add-binding-to-frame! var val frame))))
85 ;; primitive procedure
86 (define (primitive-implementation proc) (cadr proc))
88 (define primitive-procedures
102 (define (primitive-procedure-names)
103 (map car primitive-procedures))
105 (define (primitive-procedure-objects)
106 (map (lambda (proc) (list 'primitive (cadr proc)))
107 primitive-procedures))
109 (define (apply-primitive-procedure proc args)
110 (apply-in-underlying-scheme
111 (primitive-implementation proc) args))
114 (define (make-environment)
116 (extend-environment (primitive-procedure-names)
117 (primitive-procedure-objects)
118 the-empty-environment)))
119 (define-variable! 'true true initial-env)
120 (define-variable! 'false false initial-env)
123 (define the-global-environment (make-environment))
126 (define (list-of-values exps env)
127 (if (no-operands? exps)
129 (cons (eval (first-operand exps) env)
130 (list-of-values (rest-operands exps) env))))
132 (define (no-operands? ops) (null? ops))
133 (define (first-operand ops) (car ops))
134 (define (rest-operands ops) (cdr ops))
136 ;; compound procedure
137 (define (make-procedure params body env)
138 (list 'procedure params (scan-out-definitions body) env))
140 (define (procedure-parameters p) (cadr p))
141 (define (procedure-body p) (scan-out-definitions (caddr p)))
142 (define (procedure-environment p) (cadddr p))
144 (define (apply procedure arguments env)
146 [`(primitive ,f ...) (apply-primitive-procedure procedure (list-of-arg-values arguments env))]
148 (let* ([params (procedure-parameters procedure)]
149 [params-value-pair (create-param-value-pair params arguments env)])
151 (procedure-body procedure)
153 (map car params-value-pair)
154 (map cdr params-value-pair)
155 (procedure-environment procedure))))]
156 [_ (error "Unknown procedure type -- APPLY" procedure)]))
158 (define (list-of-arg-values exps env)
159 (if (no-operands? exps)
161 (cons (actual-value (first-operand exps) env)
162 (list-of-arg-values (rest-operands exps) env))))
164 (define (create-param-value-pair params arguments env)
165 (if (null? arguments)
167 (let [(p (first-operand params))]
169 [`(,variable lazy) (cons (cons variable (delay-it (first-operand arguments) env))
170 (create-param-value-pair (rest-operands params)
171 (rest-operands arguments)
173 [exp (cons (cons exp (eval (first-operand arguments) env))
174 (create-param-value-pair (rest-operands params)
175 (rest-operands arguments)
186 (define (cond->if clauses)
187 (define (seq->exp actions)
188 (if (empty? (cdr actions))
193 (let ([clause (car clauses)])
195 [`(else ,action ...) (seq->exp action)]
196 [`(,pred ,action ...) `(if ,pred
198 ,(cond->if (cdr clauses)))]))))
201 (define (let->combination lexpr)
202 (match-let* ([`(let ,(? (lambda (x) (or (pair? x) (empty? x))) bindings) ,body ..1) lexpr]
203 [`((,var ,val) ...) bindings])
204 `((lambda ,var ,@body) ,@val)))
207 (define (named-let->combination lexpr)
208 (match-let* ([`(let ,(? symbol? name) ,(? (lambda (x) (or (pair? x) (empty? x))) bindings) ,body ..1) lexpr]
209 [`((,var ,val) ...) bindings])
210 `(begin (define ,name (lambda ,var ,@body))
214 (define (let*->nested-lets lexpr)
216 [`(let* (,first-binding ,rest-bindings ...) ,body ..1)
217 `(let (,first-binding) ,(let*->nested-lets `(let* ,rest-bindings ,@body)))]
218 [`(let* () ,body ..1) `(let () ,@body)]))
220 ;; internal definitions
221 (define (scan-out-definitions body)
223 [`((define ,var ,e) ..1 ,rest)
224 `((let ,(map (lambda (v) (list (car v) ''*unassigned*)) var)
225 ,@(map (lambda (v e) `(set! ,(car v) (lambda ,(cdr v) ,e))) var e)
230 (define (letrec->combination lexpr)
232 [`(letrec (,bindings ...) ,body ..1)
233 `(let ,(map (lambda (v) (list (car v) ''*unassigned*)) bindings)
234 ,@(map (lambda (binding)
235 (let ([name (car binding)]
236 [value (cadr binding)])
237 `(set! ,name ,value)))
243 (define (actual-value exp env)
244 ;(display (format "eval expr ~s~%" exp))
246 (force-it (eval exp env)))
248 (define (force-it obj)
249 ;; (display (format "~s~%" obj))
251 [`(thunk ,exp ,env) (begin
252 ;(display (format "eval's output ~s~%" obj))
254 (actual-value exp env))]
257 (define (delay-it exp env)
258 (list 'thunk exp env))
262 (define (eval exp env)
263 ;; use this display statement to visualize the recursive evaluation process
264 ;;(display (format "~s~%" exp))
266 [(? self-evaluating? exp) exp]
267 [(? variable? exp) (lookup-variable-value exp env)]
269 [`(set! ,var ,val) (set-variable-value! var (eval val env) env)]
270 [`(define ,(? (lambda (x) (not (pair? x))) var) ,b) (define-variable! var (eval b env) env)]
271 [`(define ,(? pair? var) ,b ..1) (define-variable! (car var) (eval (make-lambda (cdr var) b) env) env)]
272 [`(if ,pred ,consequent ,alternative) (if (true? (eval pred env)) (eval consequent env) (eval alternative env))]
273 [`(unless ,condition ,consequent ,alternative) (if (true? (eval condition env)) (eval alternative env) (eval consequent env))]
274 [`(lambda ,parameters ,body ..1) (make-procedure parameters body env)]
275 [`(begin ,exp ...) (eval-sequence exp env)]
276 [`(cond ,clauses ...) (eval (cond->if clauses) env)]
277 [`(let ,(? (lambda (x) (or (pair? x) (empty? x))) bindings) ,body ..1) (eval (let->combination exp) env)]
278 [`(let ,(? symbol? name) ,bindings ,body ..1) (eval (named-let->combination exp) env)]
279 [`(let* ,bindings ,body ..1) (eval (let*->nested-lets exp) env)]
280 [`(letrec ,bindings ,body ..1) (eval (letrec->combination exp) env)]
281 [`(,f ,x ...) (apply (actual-value f env)
284 [_ (error "unable to evaluate expression -- EVAL " exp)]))
288 (let loop ([input (read)]
289 [env the-global-environment])
290 (let ([output (eval input env)])
295 (let ([t1 (current-inexact-milliseconds)])
296 (eval expr the-global-environment)
297 (displayln (- (current-inexact-milliseconds) t1))))