From d7d4abec592ca1ec52a6b2fe54cbbadf45037d4c Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Thu, 12 May 2011 15:27:04 -0400 Subject: [PATCH] found that the documentation for let-rec's behavior in 5.1.1 is off: the closures are installed in reverse order, but the first element is what's on the stack, not the last. --- compiler.rkt | 10 +++---- expression-structs.rkt | 4 +++ test-compiler.rkt | 62 +++++++++++++++++++++--------------------- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/compiler.rkt b/compiler.rkt index 280df36..721cd24 100644 --- a/compiler.rkt +++ b/compiler.rkt @@ -128,7 +128,7 @@ (append (build-list (length (LetRec-procs exp)) (lambda: ([i : Natural]) '?)) (drop cenv n)))) - (reverse (LetRec-procs exp))) + (LetRec-procs exp)) (drop cenv n))]) (append (apply append (map (lambda: ([lam : Lam]) @@ -1678,7 +1678,7 @@ (lambda: ([i : Natural]) '?)) (drop cenv n)))) - (reverse (LetRec-procs exp))) + (LetRec-procs exp)) (drop cenv n))] [n : Natural (length (LetRec-procs exp))] [after-body-code : Symbol (make-label 'afterBodyCode)] @@ -1700,7 +1700,7 @@ extended-cenv (append-instruction-sequences - ;; Install each of the closure shells + ;; Install each of the closure shells. (apply append-instruction-sequences (map (lambda: ([lam : Lam] [i : Natural]) @@ -1709,7 +1709,7 @@ (make-EnvLexicalReference i #f) next-linkage/expects-single)) (LetRec-procs exp) - (build-list n (lambda: ([i : Natural]) (ensure-natural (- n 1 i)))))) + (build-list n (lambda: ([i : Natural]) i)))) ;; Fix the closure maps of each (apply append-instruction-sequences @@ -1720,7 +1720,7 @@ (make-FixClosureShellMap! i (Lam-closure-map lam)))))) (LetRec-procs exp) - (build-list n (lambda: ([i : Natural]) (ensure-natural (- n 1 i)))))) + (build-list n (lambda: ([i : Natural]) i)))) ;; Compile the body (compile (LetRec-body exp) extended-cenv target letrec-linkage) diff --git a/expression-structs.rkt b/expression-structs.rkt index 461d0fa..e9de74f 100644 --- a/expression-structs.rkt +++ b/expression-structs.rkt @@ -107,6 +107,10 @@ [body : Expression] [boxes? : Boolean]) #:transparent) + +;; During evaluation, the closures corresponding to procs are expected +;; to be laid out so that stack position 0 corresponds to procs[0], +;; stack position 1 to procs[1], and so on. (define-struct: LetRec ([procs : (Listof Lam)] [body : Expression]) #:transparent) diff --git a/test-compiler.rkt b/test-compiler.rkt index 12340df..535cd86 100644 --- a/test-compiler.rkt +++ b/test-compiler.rkt @@ -387,37 +387,37 @@ ;; deriv -#;(test '(let () - (define (deriv-aux a) (list '/ (deriv a) a)) - (define (map f l) - (if (null? l) - l - (cons (f (car l)) - (map f (cdr l))))) - (define (deriv a) - (if (not (pair? a)) - (if (eq? a 'x) 1 0) - (if (eq? (car a) '+) - (cons '+ (map deriv (cdr a))) - (if (eq? (car a) '-) - (cons '- (map deriv (cdr a))) - (if (eq? (car a) '*) - (list '* - a - (cons '+ (map deriv-aux (cdr a)))) - (if (eq? (car a) '/) - (list '- - (list '/ - (deriv (cadr a)) - (caddr a)) - (list '/ - (cadr a) - (list '* - (caddr a) - (caddr a) - (deriv (caddr a))))) - 'error)))))) - (deriv '(+ (* 3 x x) (* a x x) (* b x) 5))) +(test '(let () + (define (deriv-aux a) (list '/ (deriv a) a)) + (define (map f l) + (if (null? l) + l + (cons (f (car l)) + (map f (cdr l))))) + (define (deriv a) + (if (not (pair? a)) + (if (eq? a 'x) 1 0) + (if (eq? (car a) '+) + (cons '+ (map deriv (cdr a))) + (if (eq? (car a) '-) + (cons '- (map deriv (cdr a))) + (if (eq? (car a) '*) + (list '* + a + (cons '+ (map deriv-aux (cdr a)))) + (if (eq? (car a) '/) + (list '- + (list '/ + (deriv (cadr a)) + (caddr a)) + (list '/ + (cadr a) + (list '* + (caddr a) + (caddr a) + (deriv (caddr a))))) + 'error)))))) + (deriv '(+ (* 3 x x) (* a x x) (* b x) 5))) '(+ (* (* 3 x x) (+ (/ 0 3) (/ 1 x) (/ 1 x))) (* (* a x x) (+ (/ 0 a) (/ 1 x) (/ 1 x))) (* (* b x) (+ (/ 0 b) (/ 1 x)))