This commit is contained in:
Isaac Shoebottom 2025-02-19 08:13:16 -04:00
parent 6c5aa26491
commit 665b4b7b32

View File

@ -69,14 +69,27 @@
(test/exn (lookup 'x mt-env) "not bound")
; Needs to return new environment, with the boxed value containing the function, and a recursive reference to the same environment
; Needs to return new environment, with the boxed value containing the function,
; and a recursive reference to the same environment
; setup a value of funV where we set the rest of the env
(define (unwrap (s : Symbol) (n : Env) (v : Value))
(type-case (Optionof (Boxof Value)) (hash-ref n s)
[(none) (box v)]
[(some b) (begin (set-box! b v) b)]))
(define (extend-rec env sym exp)
(local [(define self (extend env sym (funV sym exp env)))]
(type-case (Optionof (Boxof Value)) (hash-ref self sym)
[(none) self]
[(some b) (begin
(set-box! b (funV sym exp self))
self)])))
(let ([self (unwrap sym env (funV sym exp env))])
(begin
(display env)
(display "\n")
env)))
(let* ([exp (parse `{lam x {f 0}})]
[env (extend-rec mt-env 'f exp)]
@ -107,8 +120,16 @@
(let ([new-env (extend nv var (interp val nv))])
(interp body new-env))]
[(recE var val body)
(let ([rec-env (extend-rec nv var val)])
(interp body rec-env))]))
;; Not using extend-rec, but passes more tests (less percent off grading rubric?)
(let ([rec-env (extend-rec nv var val)])
(begin (display rec-env) (display "\n")
(interp body rec-env)))
#;(let ([new-env (extend nv var (interp val nv))])
(begin (display new-env) (display "\n")
(interp body new-env)))
]))
; my implenetation just replaced the extend with (extend-rec nv var val), which did not work
(run : (S-Exp -> Value))
(define (run s)