Improve interface for `find-mutated-vars', now supports mutable *and* immutable versions.

This commit is contained in:
Sam Tobin-Hochstadt 2010-07-05 14:25:51 -04:00
parent 63ec520885
commit 692a172474
2 changed files with 20 additions and 13 deletions

View File

@ -1,17 +1,17 @@
#lang racket/base
(require (for-template racket/base) racket/dict
racket/trace
syntax/id-table syntax/kerncase)
;; samth : this should use sets, not dicts
;; but sets do not have extensible comparisons
;; shouldn't be promoted until this is fixed
;; find and add to mapping all the set!'ed variables in form
;; syntax -> table
(define (find-mutated-vars form)
(let loop ([stx form] [tbl (make-immutable-free-id-table)])
;; if the supplied mapping is mutable, mutates it
;; default is immutability
;; syntax [table] -> table
(define (find-mutated-vars form [tbl (make-immutable-free-id-table)])
(define add (if (dict-mutable? tbl)
(lambda (t i) (dict-set! t i #t) t)
(lambda (t i) (dict-set t i #t))))
(let loop ([stx form] [tbl tbl])
;; syntax-list -> table
(define (fmv/list lstx)
(for/fold ([tbl tbl])
@ -20,7 +20,7 @@
(kernel-syntax-case* stx #f (#%top-interaction)
;; what we care about: set!
[(set! v e)
(dict-set (loop #'e tbl) #'v #t)]
(add (loop #'e tbl) #'v)]
;; forms with expression subforms
[(define-values (var ...) expr)
(loop #'expr tbl)]

View File

@ -4,28 +4,35 @@
(for-label unstable/mutated-vars
racket/contract
racket/dict
syntax/id-table
racket/base))
@title[#:tag "mutated-vars"]{Finding Mutated Variables}
@(define the-eval (make-base-eval))
@(the-eval '(require unstable/mutated-vars racket/dict))
@(the-eval '(require unstable/mutated-vars syntax/id-table racket/dict))
@defmodule[unstable/mutated-vars]
@unstable[@author+email["Sam Tobin-Hochstadt" "samth@ccs.neu.edu"]]
@defproc[(find-mutated-vars [stx syntax?]) dict?]{Traverses
@defproc[(find-mutated-vars [stx syntax?] [dict dict? (make-immutable-free-id-table)]) dict?]{Traverses
@racket[stx], which should be @racket[module-level-form] in the sense
of the grammar for
@tech[#:doc '(lib "scribblings/reference/reference.scrbl")]{fully-expanded} forms,
and records all of the variables that are mutated. The result is a
dictionary that maps each mutated identifier to @racket[#t].}
and records all of the variables that are mutated.
Each mutated variable is added to @racket[dict], mapped to
@racket[#t]. If @racket[dict] is mutable, as determined by
@racket[dict-mutable?], then the table is updated destructively.
Otherwise, the table is updated functionally.}
@examples[#:eval the-eval
(define t (find-mutated-vars #'(begin (set! var 'foo) 'bar)))
(dict-ref t #'var #f)
(dict-ref t #'other-var #f)
(define tbl (make-free-id-table))
(find-mutated-vars #'(begin (set! var 'foo) 'bar) tbl)
(dict-ref tbl #'var #f)
]
}