From d1af35f0f5094fb8d2bd9b098d964df72f9197e2 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Tue, 8 Mar 2011 17:27:43 -0500 Subject: [PATCH] fixed bug with lexical scoping. --- compile.rkt | 21 ++++++++++----------- lexical-env.rkt | 36 +++++++++++++++++------------------- lexical-structs.rkt | 14 ++++++++------ test-compiler.rkt | 4 ++-- 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/compile.rkt b/compile.rkt index 9255156..dc6314e 100644 --- a/compile.rkt +++ b/compile.rkt @@ -120,7 +120,8 @@ (: lexical-environment-pop-depth (CompileTimeEnvironment Linkage -> Natural)) ;; Computes how much of the environment we need to pop. (define (lexical-environment-pop-depth cenv linkage) - (cond + (length cenv) + #;(cond [(empty? cenv) 0] [else @@ -128,14 +129,12 @@ (cond [(Prefix? entry) (+ 1 (lexical-environment-pop-depth (rest cenv) linkage))] - [(FunctionExtension? entry) - (length (FunctionExtension-names entry))] - [(LocalExtension? entry) - (+ (length (LocalExtension-names entry)) - (lexical-environment-pop-depth (rest cenv) linkage))] - [(TemporaryExtension? entry) - (+ (TemporaryExtension-n entry) - (lexical-environment-pop-depth (rest cenv) linkage))]))])) + [(symbol? entry) + (cond + (+ 1 (lexical-environment-pop-depth (rest cenv) linkage)))] + [(eq? entry #f) + (+ 1 (lexical-environment-pop-depth (rest cenv) linkage))]))])) + @@ -274,9 +273,9 @@ (define (compile-lambda-body exp cenv lexical-references proc-entry) (let*: ([formals : (Listof Symbol) (Lam-parameters exp)] [extended-cenv : CompileTimeEnvironment - (extend-lexical-environment + (extend-lexical-environment/names '() - (make-FunctionExtension formals))] + formals)] [extended-cenv : CompileTimeEnvironment (lexical-references->compile-time-environment lexical-references cenv extended-cenv)]) diff --git a/lexical-env.rkt b/lexical-env.rkt index dcb9477..8521a85 100644 --- a/lexical-env.rkt +++ b/lexical-env.rkt @@ -6,6 +6,7 @@ "sets.rkt") (provide find-variable extend-lexical-environment + extend-lexical-environment/names extend-lexical-environment/placeholders collect-lexical-references lexical-references->compile-time-environment) @@ -35,25 +36,16 @@ [else (loop (rest cenv) (add1 depth))])] - [(FunctionExtension? elt) - (let: ([index : (U #f Natural) (list-index name (FunctionExtension-names elt))]) - (cond - [(boolean? index) - (loop (rest cenv) (+ depth (length (FunctionExtension-names elt))))] - [else - (make-LocalAddress (+ depth index))]))] + [(symbol? elt) + (cond + [(eq? elt name) + (make-LocalAddress depth)] + [else + (loop (rest cenv) (add1 depth))])] - [(LocalExtension? elt) - (let: ([index : (U #f Natural) (list-index name (LocalExtension-names elt))]) - (cond - [(boolean? index) - (loop (rest cenv) (+ depth (length (LocalExtension-names elt))))] - [else - (make-LocalAddress (+ depth index))]))] + [(eq? elt #f) + (loop (rest cenv) (add1 depth))]))]))) - [(TemporaryExtension? elt) - (loop (rest cenv) - (+ depth (TemporaryExtension-n elt)))]))]))) (: list-index (All (A) A (Listof A) -> (U #f Natural))) (define (list-index x l) @@ -75,12 +67,18 @@ (cons extension cenv)) + +(: extend-lexical-environment/names (CompileTimeEnvironment (Listof Symbol) -> CompileTimeEnvironment)) +(define (extend-lexical-environment/names cenv names) + (append names cenv)) + + (: extend-lexical-environment/placeholders (CompileTimeEnvironment Natural -> CompileTimeEnvironment)) ;; Add placeholders to the lexical environment (This represents what happens during procedure application.) (define (extend-lexical-environment/placeholders cenv n) - (cons (make-TemporaryExtension n) - cenv)) + (append (build-list n (lambda: ([i : Natural]) #f)) + cenv)) diff --git a/lexical-structs.rkt b/lexical-structs.rkt index dc27a49..ef01bdc 100644 --- a/lexical-structs.rkt +++ b/lexical-structs.rkt @@ -11,20 +11,22 @@ (define-struct: Prefix ([names : (Listof Symbol)]) #:transparent) -(define-struct: FunctionExtension ([names : (Listof Symbol)]) +#;(define-struct: FunctionExtension ([names : (Listof Symbol)]) #:transparent) -(define-struct: LocalExtension ([names : (Listof Symbol)]) +#;(define-struct: LocalExtension ([names : (Listof Symbol)]) #:transparent) -(define-struct: TemporaryExtension ([n : Natural]) +#;(define-struct: TemporaryExtension ([n : Natural]) #:transparent) (define-type CompileTimeEnvironmentEntry (U Prefix ;; a prefix - FunctionExtension - LocalExtension - TemporaryExtension)) + Symbol + False + #;FunctionExtension + #;LocalExtension + #;TemporaryExtension)) ;; A compile-time environment is a (listof (listof symbol)). diff --git a/test-compiler.rkt b/test-compiler.rkt index e090423..64e96a4 100644 --- a/test-compiler.rkt +++ b/test-compiler.rkt @@ -282,8 +282,8 @@ (* x acc)) 1 '(1 2 3 4 5 6 7 8 9 10))) - (* 1 2 3 4 5 6 7 8 9 10) - #:debug? #t) + (* 1 2 3 4 5 6 7 8 9 10)) +