easier global adding
This commit is contained in:
40
src/lib.rs
40
src/lib.rs
@@ -175,29 +175,29 @@ pub struct Runtime<'a, R: BufRead> {
|
|||||||
|
|
||||||
impl<'a, R: BufRead> Runtime<'a, R> {
|
impl<'a, R: BufRead> Runtime<'a, R> {
|
||||||
pub fn new(reader: R) -> Self {
|
pub fn new(reader: R) -> Self {
|
||||||
let globals: HashMap<String, Object> = HashMap::from([
|
|
||||||
("version'".into(), Object::value(Value::String(
|
|
||||||
format!("{} ({}/{})",
|
|
||||||
env!("CARGO_PKG_VERSION"),
|
|
||||||
env!("GIT_BRANCH"),
|
|
||||||
env!("GIT_HASH"))), HashMap::new(), HashMap::new()))
|
|
||||||
]);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
tokenizer: Tokenizer::new(reader).peekable(),
|
tokenizer: Tokenizer::new(reader).peekable(),
|
||||||
global_types:
|
global_types: HashMap::new(),
|
||||||
globals
|
globals: HashMap::new(),
|
||||||
.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(),
|
|
||||||
parser: None,
|
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<Item = Result<Value, RuntimeError>> + 'a {
|
pub fn values(&'a mut self) -> impl Iterator<Item = Result<Value, RuntimeError>> + 'a {
|
||||||
|
|||||||
Reference in New Issue
Block a user