From 4ee7c0f2970accc0f49eeac7fb2a0359b159c8ae Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sat, 25 Oct 2014 16:51:52 -0500 Subject: rust: add step7_quote. Refactor with type constructors. --- rust/src/env.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'rust/src/env.rs') diff --git a/rust/src/env.rs b/rust/src/env.rs index 3c98015..730010a 100644 --- a/rust/src/env.rs +++ b/rust/src/env.rs @@ -1,11 +1,12 @@ +#![allow(dead_code)] + use std::rc::Rc; use std::cell::RefCell; use std::collections::HashMap; use std::fmt; -use types::{MalVal,MalRet,Nil,Sym,List}; +use types::{MalVal,MalRet,Sym,List,_nil,list}; -#[allow(dead_code)] struct EnvType { data: HashMap, outer: Option, @@ -13,12 +14,10 @@ struct EnvType { pub type Env = Rc>; -#[allow(dead_code)] pub fn env_new(outer: Option) -> Env { Rc::new(RefCell::new(EnvType{data: HashMap::new(), outer: outer})) } -#[allow(dead_code)] pub fn env_bind(env: &Env, mbinds: MalVal, mexprs: MalVal) -> Result { @@ -46,7 +45,7 @@ pub fn env_bind(env: &Env, match **sym { Sym(ref s) => { let rest = exprs.slice(i-1,exprs.len()).to_vec(); - env_set(env, s.clone(), Rc::new(List(rest))); + env_set(env, s.clone(), list(rest)); } _ => return Err("& bind to non-symbol".to_string()), } @@ -60,7 +59,6 @@ pub fn env_bind(env: &Env, } } -#[allow(dead_code)] pub fn env_find(env: Env, key: String) -> Option { if env.borrow().data.contains_key(&key) { Some(env) @@ -72,7 +70,6 @@ pub fn env_find(env: Env, key: String) -> Option { } } -#[allow(dead_code)] pub fn env_root(env: &Env) -> Env { match env.borrow().outer { Some(ref ei) => env_root(ei), @@ -80,18 +77,16 @@ pub fn env_root(env: &Env) -> Env { } } -#[allow(dead_code)] pub fn env_set(env: &Env, key: String, val: MalVal) { env.borrow_mut().data.insert(key, val.clone()); } -#[allow(dead_code)] pub fn env_get(env: Env, key: String) -> MalRet { match env_find(env, key.clone()) { Some(e) => { match e.borrow().data.find_copy(&key) { Some(v) => Ok(v), - None => Ok(Rc::new(Nil)), + None => Ok(_nil()), } }, None => Err("'".to_string() + key + "' not found".to_string()), -- cgit v1.2.3