@@ -22,6 +22,9 @@ use core::char;
22
22
use core:: cmp:: Equiv ;
23
23
use core:: local_data;
24
24
use core:: str;
25
+ use core:: hashmap:: HashSet ;
26
+ use core:: rand;
27
+ use core:: rand:: RngUtil ;
25
28
use core:: to_bytes;
26
29
27
30
#[ deriving( Encodable , Decodable , Eq ) ]
@@ -559,6 +562,20 @@ pub fn gensym_ident(str : &str) -> ast::ident {
559
562
ast:: new_ident ( gensym ( str) )
560
563
}
561
564
565
+
566
+ // create a fresh name. In principle, this is just a
567
+ // gensym, but for debugging purposes, you'd like the
568
+ // resulting name to have a suggestive stringify, without
569
+ // paying the cost of guaranteeing that the name is
570
+ // truly unique. I'm going to try to strike a balance
571
+ // by using a gensym with a name that has a random number
572
+ // at the end. So, the gensym guarantees the uniqueness,
573
+ // and the int helps to avoid confusion.
574
+ pub fn fresh_name ( src_name : & str ) -> Name {
575
+ let num = rand:: rng ( ) . gen_uint_range ( 0 , 0xffff ) ;
576
+ gensym ( fmt ! ( "%s_%u" , src_name, num) )
577
+ }
578
+
562
579
/**
563
580
* All the valid words that have meaning in the Rust language.
564
581
*
@@ -691,3 +708,14 @@ pub fn is_reserved_keyword(tok: &Token) -> bool {
691
708
_ => false ,
692
709
}
693
710
}
711
+
712
+ #[ cfg( test) ]
713
+ mod test {
714
+ use super :: * ;
715
+ use std:: io;
716
+ #[ test] fn t1 ( ) {
717
+ let a = fresh_name ( "ghi" ) ;
718
+ io:: println ( fmt ! ( "interned name: %u,\n textual name: %s\n " ,
719
+ a, * interner_get( a) ) ) ;
720
+ }
721
+ }
0 commit comments