add memoize function

This commit is contained in:
Yuichi Nishiwaki 2014-07-19 18:26:03 +09:00
parent 03cc21953f
commit 1297ef9fb8
1 changed files with 31 additions and 34 deletions

View File

@ -40,19 +40,29 @@
(proc expr) (proc expr)
expr))))) expr)))))
(define (memoize f)
"memoize on a symbol"
(define cache (make-dictionary))
(lambda (sym)
(if (dictionary-has? cache sym)
(dictionary-ref cache sym)
(begin
(define val (f sym))
(dictionary-set! cache sym val)
val))))
(define (make-syntactic-closure env free form) (define (make-syntactic-closure env free form)
(define cache (make-dictionary))
(define resolve
(memoize
(lambda (sym)
(make-identifier sym env))))
(walk (walk
(lambda (sym) (lambda (sym)
(if (memq sym free) (if (memq sym free)
sym sym
(if (dictionary-has? cache sym) (resolve sym)))
(dictionary-ref cache sym)
(begin
(define id (make-identifier sym env))
(dictionary-set! cache sym id)
id))))
form)) form))
(define (close-syntax form env) (define (close-syntax form env)
@ -73,15 +83,10 @@
(define (er-macro-transformer f) (define (er-macro-transformer f)
(lambda (expr use-env mac-env) (lambda (expr use-env mac-env)
(define cache (make-dictionary)) (define rename
(memoize
(define (rename sym) (lambda (sym)
(if (dictionary-has? cache sym) (make-identifier sym mac-env))))
(dictionary-ref cache sym)
(begin
(define id (make-identifier sym mac-env))
(dictionary-set! cache sym id)
id)))
(define (compare x y) (define (compare x y)
(if (not (symbol? x)) (if (not (symbol? x))
@ -95,27 +100,19 @@
(define (ir-macro-transformer f) (define (ir-macro-transformer f)
(lambda (expr use-env mac-env) (lambda (expr use-env mac-env)
(define icache (make-dictionary))
(define icache* (make-dictionary)) (define icache* (make-dictionary))
(define (inject sym) (define inject
(if (dictionary-has? icache sym) (memoize
(dictionary-ref icache sym) (lambda (sym)
(begin (define id (make-identifier sym use-env))
(define id (make-identifier sym use-env)) (dictionary-set! icache* id sym)
(dictionary-set! icache sym id) id)))
(dictionary-set! icache* id sym)
id)))
(define rcache (make-dictionary)) (define rename
(memoize
(define (rename sym) (lambda (sym)
(if (dictionary-has? rcache sym) (make-identifier sym mac-env))))
(dictionary-ref rcache sym)
(begin
(define id (make-identifier sym mac-env))
(dictionary-set! rcache sym id)
id)))
(define (uninject sym) (define (uninject sym)
(if (dictionary-has? icache* sym) (if (dictionary-has? icache* sym)