summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-01-25 21:21:51 -0500
committerDavid Thompson <dthompson2@worcester.edu>2023-06-08 08:14:41 -0400
commit03b9dc11fab48521929dfceffea4dd077467a9b2 (patch)
tree23cc0d97b2384ad8f93b2e5d03c85b49706eca77
parent1285411d6a0c67c676b321fc028453794e7d6641 (diff)
Fix hoisting.
-rw-r--r--chickadee/graphics/seagull.scm33
1 files changed, 26 insertions, 7 deletions
diff --git a/chickadee/graphics/seagull.scm b/chickadee/graphics/seagull.scm
index e46ebf8..677e02d 100644
--- a/chickadee/graphics/seagull.scm
+++ b/chickadee/graphics/seagull.scm
@@ -651,19 +651,38 @@
(compose-envs predicate-env consequent-env alternate-env)))
(define (hoist:let names exps body)
- (define-values (exps* exps-env) (hoist:list exps))
+ (define-values (exps* exps-env)
+ (hoist:list exps))
(define-values (body* body-env)
(hoist-functions body))
- (values `(let ,(map list names exps*) ,body*)
- (compose-envs exps-env body-env)))
+ ;; Remove all lambda bindings...
+ (define bindings
+ (filter-map (lambda (name exp)
+ (match exp
+ (('lambda _ _)
+ #f)
+ (_ (list name exp))))
+ names exps*))
+ ;; ...and add them to the top-level environment.
+ (define env*
+ (fold (lambda (name exp env)
+ (match exp
+ (('lambda _ _)
+ (extend-env name exp env))
+ (_ env)))
+ (compose-envs exps-env body-env)
+ names exps*))
+ ;; If there are no bindings left, remove the 'let'.
+ (values (if (null? bindings)
+ body*
+ `(let ,bindings ,body*))
+ env*))
(define (hoist:lambda params body)
- (define var (unique-identifier))
(define-values (body* body-env)
(hoist-functions body))
- (define lambda* `(lambda ,params ,body*))
- (values `(var ,var #f)
- (extend-env var lambda* body-env)))
+ (values `(lambda ,params ,body*)
+ body-env))
(define (hoist:values exps)
(define-values (exps* exp-env)