added fresh-name fn

This commit is contained in:
John Clements 2013-05-14 11:34:17 -07:00
parent 5a158f1d19
commit fe6baa9023

View file

@ -22,6 +22,9 @@ use core::char;
use core::cmp::Equiv;
use core::local_data;
use core::str;
use core::hashmap::HashSet;
use core::rand;
use core::rand::RngUtil;
use core::to_bytes;
#[deriving(Encodable, Decodable, Eq)]
@ -559,6 +562,20 @@ pub fn gensym_ident(str : &str) -> ast::ident {
ast::new_ident(gensym(str))
}
// create a fresh name. In principle, this is just a
// gensym, but for debugging purposes, you'd like the
// resulting name to have a suggestive stringify, without
// paying the cost of guaranteeing that the name is
// truly unique. I'm going to try to strike a balance
// by using a gensym with a name that has a random number
// at the end. So, the gensym guarantees the uniqueness,
// and the int helps to avoid confusion.
pub fn fresh_name(src_name : &str) -> Name {
let num = rand::rng().gen_uint_range(0,0xffff);
gensym(fmt!("%s_%u",src_name,num))
}
/**
* All the valid words that have meaning in the Rust language.
*
@ -691,3 +708,14 @@ pub fn is_reserved_keyword(tok: &Token) -> bool {
_ => false,
}
}
#[cfg(test)]
mod test {
use super::*;
use std::io;
#[test] fn t1() {
let a = fresh_name("ghi");
io::println(fmt!("interned name: %u,\ntextual name: %s\n",
a,*interner_get(a)));
}
}