From d29fa2b9b08d8762edfc1db0ecbc6eac05338dad Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Thu, 24 Oct 2024 15:18:58 -0400 Subject: [PATCH] easier global adding --- src/lib.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3f92a81..25f7857 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -175,29 +175,29 @@ pub struct Runtime<'a, R: BufRead> { impl<'a, R: BufRead> Runtime<'a, R> { pub fn new(reader: R) -> Self { - let globals: HashMap = HashMap::from([ - ("version'".into(), Object::value(Value::String( - format!("{} ({}/{})", - env!("CARGO_PKG_VERSION"), - env!("GIT_BRANCH"), - env!("GIT_HASH"))), HashMap::new(), HashMap::new())) - ]); - Self { tokenizer: Tokenizer::new(reader).peekable(), - global_types: - globals - .clone() - .into_iter() - .map(|(key, mut value)| (key, value.eval().unwrap().get_type())) - .collect(), - globals: - globals - .into_iter() - .map(|(key, value)| (key, Arc::new(Mutex::new(value)))) - .collect(), + global_types: HashMap::new(), + globals: HashMap::new(), parser: None, - } + }.add_global("version'", Value::String( + format!("{} ({}/{})", + env!("CARGO_PKG_VERSION"), + env!("GIT_BRANCH"), + env!("GIT_HASH") + ) + )) + } + + pub fn add_global(mut self, name: &str, value: Value) -> Self { + self.global_types.insert(name.to_string(), value.get_type()); + self.globals.insert(name.to_string(), + Arc::new(Mutex::new(Object::value( + value, + HashMap::new(), + HashMap::new())))); + + self } pub fn values(&'a mut self) -> impl Iterator> + 'a {