rustleaf/eval/structs/
eval_variable.rs

1use crate::core::RustValue;
2use crate::eval::{ControlFlow, ErrorKind, EvalResult, Evaluator};
3use anyhow::anyhow;
4
5#[derive(Debug, Clone)]
6pub struct EvalVariable {
7    pub name: String,
8}
9
10#[crate::rust_value_any]
11impl RustValue for EvalVariable {
12    fn dyn_clone(&self) -> Box<dyn RustValue> {
13        Box::new(self.clone())
14    }
15    fn eval(&self, evaluator: &mut Evaluator) -> anyhow::Result<EvalResult> {
16        let result = evaluator.current_env.get(&self.name).ok_or_else(|| {
17            ControlFlow::Error(ErrorKind::SystemError(anyhow!(
18                "Undefined variable: {}",
19                self.name
20            )))
21        });
22        Ok(result)
23    }
24
25    fn str(&self) -> String {
26        self.name.clone()
27    }
28}