better global initialization

This commit is contained in:
2024-10-24 15:07:37 -04:00
parent c83b60a28c
commit d268725b6f

View File

@@ -14,7 +14,6 @@ use std::io::BufRead;
use std::fmt; use std::fmt;
use std::iter::Peekable; use std::iter::Peekable;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::cell::RefCell;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Type { pub enum Type {
@@ -139,7 +138,7 @@ impl Object {
} }
} }
/// evaluate the tree inside of an object if it isn't evaluated yet, returns the value /// evaluate the tree inside an object if it isn't evaluated yet, returns the value
pub fn eval(&mut self) -> Result<Value, RuntimeError> { pub fn eval(&mut self) -> Result<Value, RuntimeError> {
match self.value.clone() { match self.value.clone() {
Cache::Cached(v) => Ok(v), Cache::Cached(v) => Ok(v),
@@ -176,20 +175,27 @@ 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: HashMap::from([ global_types:
("version'".into(), Type::String) globals
]), .clone()
globals: HashMap::from([ .into_iter()
("version'".into(), Arc::new( .map(|(key, mut value)| (key, value.eval().unwrap().get_type()))
Mutex::new( .collect(),
Object::value(Value::String( globals:
format!("{} ({}/{})", globals
env!("CARGO_PKG_VERSION"), .into_iter()
env!("GIT_BRANCH"), .map(|(key, value)| (key, Arc::new(Mutex::new(value))))
env!("GIT_HASH"))), HashMap::new(), HashMap::new())))) .collect(),
]),
parser: None, parser: None,
} }
} }