use racket functions to represent lambdas

This commit is contained in:
Sam Caldwell 2017-03-13 14:54:01 -04:00
parent e57af91698
commit 6ee97839fa
1 changed files with 10 additions and 10 deletions

View File

@ -38,8 +38,7 @@
;; (outbound val) or ;; (outbound val) or
;; (inbound val) or ;; (inbound val) or
;; (observe val) or ;; (observe val) or
;; (closure Γ ('lambda (var ...) exp)) ;; σ Any ... -> Continue val
(struct closure (env fun) #:transparent)
;; `primop` is one of ;; `primop` is one of
;; + * / - and or not equal? null? car cdr printf ;; + * / - and or not equal? null? car cdr printf
@ -319,8 +318,13 @@
(let ([v (env-lookup Γ id)]) (let ([v (env-lookup Γ id)])
(continue v σ (list) (list)))] (continue v σ (list) (list)))]
[`(lambda (,vars ...) ,exp) [`(lambda (,vars ...) ,exp)
(define c (closure Γ e)) (define f
(continue c σ (list) (list))] (lambda (new-σ . actuals)
(define extended-env (append (map binding vars actuals) Γ))
(unless (= (length vars) (length actuals))
(error 'eval-exp "wrong number of arguments; expected ~v, got ~v" (length vars) (length actuals)))
(eval-exp exp extended-env new-σ)))
(continue f σ (list) (list))]
[`(begin ,es ...) [`(begin ,es ...)
(for-steps (void) σ (in-list es) (for-steps (void) σ (in-list es)
(lambda (v σ e) (eval-exp e Γ σ)))] (lambda (v σ e) (eval-exp e Γ σ)))]
@ -360,14 +364,10 @@
[`(,f-exp ,exps ...) [`(,f-exp ,exps ...)
(result-bind (eval-exp f-exp Γ σ) (result-bind (eval-exp f-exp Γ σ)
(lambda (f-v new-sto) (lambda (f-v new-sto)
(unless (closure? f-v) (error 'eval-exp "tried to apply non-function ~v" f-v)) (unless (procedure? f-v) (error 'eval-exp "tried to apply non-function ~v" f-v))
(result-bind (eval-exp* exps Γ σ) (result-bind (eval-exp* exps Γ σ)
(lambda (arg-vs final-sto) (lambda (arg-vs final-sto)
(match-define (closure clo-env `(lambda (,vars ...) ,body-exp)) f-v) (apply f-v final-sto arg-vs)))))]
(unless (= (length arg-vs) (length vars))
(error 'eval-exp "wrong number of arguments; expected ~v, got ~v" (length vars) (length arg-vs)))
(define new-env (append (map binding vars arg-vs) clo-env))
(eval-exp body-exp new-env final-sto)))))]
;; TODO add a predicate ;; TODO add a predicate
;; atom? ;; atom?
[x (continue x σ (list) (list))])) [x (continue x σ (list) (list))]))