cs: make gensym add a counter for the printed form

Although the counter doesn't make the generated symbol any more
distinct, it's often a helpful hint.

Closes #2396
This commit is contained in:
Matthew Flatt 2018-11-21 20:49:28 -07:00
parent df88852e24
commit fd462604bd

View File

@ -1,15 +1,24 @@
(define gensym (define gensym
(case-lambda (case-lambda
[() (chez:gensym)] [() (#%gensym)]
[(s) (cond [(s) (cond
[(string? s) (chez:gensym (string->immutable-string s))] [(string? s) (#%gensym (append-gensym-counter s))]
[(symbol? s) (chez:gensym (chez:symbol->string s))] [(symbol? s) (#%gensym (append-gensym-counter (chez:symbol->string s)))]
[else (raise-argument-error [else (raise-argument-error
'gensym 'gensym
"(or/c symbol? string?)" "(or/c symbol? string?)"
s)])])) s)])]))
(define gensym-counter (box 0))
(define (append-gensym-counter s)
(let loop ()
(let ([c (#%unbox gensym-counter)])
(if (#%box-cas! gensym-counter c (add1 c))
(string-append s (number->string c))
(loop)))))
(define/who (symbol-interned? s) (define/who (symbol-interned? s)
(check who symbol? s) (check who symbol? s)
(not (gensym? s))) (not (gensym? s)))