Skip to content

Commit

Permalink
feat: ✨ enviroment can get_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
curryyzheng committed Sep 16, 2020
1 parent 0aa9aa7 commit 6dc8bb7
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/environment.rs
Expand Up @@ -4,7 +4,7 @@ use crate::interpreter::scheme;
use crate::interpreter::Interpreter;
use crate::interpreter::RealNumberInternalTrait;
use crate::interpreter::Value;
use cell::{Ref, RefCell, RefVal};
use cell::{Ref, RefCell, RefMut, RefVal};
use std::collections::HashMap;
use std::rc::Rc;

Expand All @@ -18,6 +18,9 @@ pub trait IEnvironment<R: RealNumberInternalTrait>: std::fmt::Debug + Clone + Pa
where
Self: Sized;
fn get(&self, name: &str) -> Option<Ref<Value<R, Self>>>
where
Self: Sized;
fn get_mut(&self, name: &str) -> Option<RefMut<Value<R, Self>>>
where
Self: Sized;
fn set(&self, name: &str, value: Value<R, Self>) -> Result<(), SchemeError>
Expand Down Expand Up @@ -66,6 +69,18 @@ impl<R: RealNumberInternalTrait> IEnvironment<R> for StandardEnv<R> {
}
}
}
fn get_mut(&self, name: &str) -> Option<RefMut<Value<R, Self>>> {
if self.definitions.borrow().contains_key(name) {
Some(RefMut::map(self.definitions.borrow_mut(), |definitions| {
definitions.get_mut(name).unwrap()
}))
} else {
match &self.parent {
Some(parent) => parent.get_mut(name),
None => None,
}
}
}

fn set(&self, name: &str, value: Value<R, Self>) -> Result<(), SchemeError> {
match self.definitions.borrow_mut().get_mut(name) {
Expand Down Expand Up @@ -106,3 +121,16 @@ fn iter_envs() -> Result<(), SchemeError> {

Ok(())
}
#[test]
fn get_mut() -> Result<(), SchemeError> {
use crate::interpreter::Number;
use std::ops::Deref;
let env = StandardEnv::<f32>::new();
env.define("x".to_string(), Value::Number(Number::Integer(1)));
*env.get_mut("x").unwrap() = Value::Number(Number::Integer(2));
assert_eq!(
env.get("x").unwrap().deref(),
&Value::Number(Number::Integer(2))
);
Ok(())
}

0 comments on commit 6dc8bb7

Please sign in to comment.