diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cexpr.rs | 43 | ||||
-rw-r--r-- | src/lib.rs | 2 |
2 files changed, 22 insertions, 23 deletions
diff --git a/src/cexpr.rs b/src/cexpr.rs index 357caff..5a171bd 100644 --- a/src/cexpr.rs +++ b/src/cexpr.rs @@ -5,6 +5,7 @@ */ use crate::sexpr::SExpr; +use crate::Result; use std::fmt; #[derive(Debug, Clone, PartialEq, PartialOrd)] @@ -46,7 +47,7 @@ pub enum CNumber { } impl CNumber { - pub fn to_sexpr(&self) -> Result<SExpr, String> { + pub fn to_sexpr(&self) -> Result<SExpr> { match self { CNumber::Integer(v) => Ok(SExpr::SInteger(*v)), CNumber::Rational(a, b) => Ok(SExpr::SList(vec![ @@ -123,7 +124,7 @@ fn compute_factorial(n: i64) -> i64 { } impl CExpr { - pub fn from_sexpr(sexpr: &SExpr) -> Result<CExpr, String> { + pub fn from_sexpr(sexpr: &SExpr) -> Result<CExpr> { // not all cases are handled; some atoms are covered trivialy match sexpr { SExpr::SNull => Err("null not handled".to_string()), @@ -141,7 +142,7 @@ impl CExpr { } } - pub fn from_sexpr_list(list: &Vec<SExpr>) -> Result<CExpr, String> { + pub fn from_sexpr_list(list: &Vec<SExpr>) -> Result<CExpr> { use CExpr::*; use CNumber::*; use SExpr::*; @@ -200,20 +201,16 @@ impl CExpr { } }, // TODO: how to make range unbounded? or less bounded? - ("+", 2..=5000) => { - CExpr::new_sum(rest.iter().map(|v| CExpr::from_sexpr(v)).collect::<Result< - Vec<CExpr>, - String, - >>( - )?) - } - ("*", 2..=5000) => { - CExpr::new_product(rest.iter().map(|v| CExpr::from_sexpr(v)).collect::<Result< - Vec<CExpr>, - String, - >>( - )?) - } + ("+", 2..=5000) => CExpr::new_sum( + rest.iter() + .map(|v| CExpr::from_sexpr(v)) + .collect::<Result<Vec<CExpr>>>()?, + ), + ("*", 2..=5000) => CExpr::new_product( + rest.iter() + .map(|v| CExpr::from_sexpr(v)) + .collect::<Result<Vec<CExpr>>>()?, + ), ("-", 2) => { let a = CExpr::from_sexpr(&rest[0])?; let b = CExpr::from_sexpr(&rest[1])?; @@ -236,7 +233,7 @@ impl CExpr { } } - pub fn new_sum(list: Vec<CExpr>) -> Result<CExpr, String> { + pub fn new_sum(list: Vec<CExpr>) -> Result<CExpr> { use CExpr::*; use CNumber::*; let (numeric, mut rest): (Vec<CExpr>, Vec<CExpr>) = list @@ -273,7 +270,7 @@ impl CExpr { } } - pub fn new_product(list: Vec<CExpr>) -> Result<CExpr, String> { + pub fn new_product(list: Vec<CExpr>) -> Result<CExpr> { use CExpr::*; use CNumber::*; let (numeric, mut rest): (Vec<CExpr>, Vec<CExpr>) = list @@ -310,7 +307,7 @@ impl CExpr { } } - pub fn to_sexpr(&self) -> Result<SExpr, String> { + pub fn to_sexpr(&self) -> Result<SExpr> { match self { CExpr::Symbol(s) => Ok(SExpr::SIdentifier(s.to_string())), CExpr::Number(n) => n.to_sexpr(), @@ -319,7 +316,7 @@ impl CExpr { let mut list = l .iter() .map(|v| v.to_sexpr()) - .collect::<Result<Vec<SExpr>, String>>()?; + .collect::<Result<Vec<SExpr>>>()?; list.insert(0, SExpr::SIdentifier("+".to_string())); if let Some(num) = n { list.insert(1, num.to_sexpr()?); @@ -330,7 +327,7 @@ impl CExpr { let mut list = l .iter() .map(|v| v.to_sexpr()) - .collect::<Result<Vec<SExpr>, String>>()?; + .collect::<Result<Vec<SExpr>>>()?; list.insert(0, SExpr::SIdentifier("*".to_string())); if let Some(num) = n { list.insert(1, num.to_sexpr()?); @@ -353,7 +350,7 @@ impl CExpr { } } - pub fn from_str(raw: &str) -> Result<CExpr, String> { + pub fn from_str(raw: &str) -> Result<CExpr> { let ast = SExpr::from_str(raw)?; CExpr::from_sexpr(&ast) } @@ -7,6 +7,8 @@ mod sexpr; pub use cexpr::{CExpr, CNumber}; pub use sexpr::{sexpr_parse_file, SExpr}; +pub type Result<T, E = String> = std::result::Result<T, E>; + pub fn repl(_verbose: bool) { let stdin = io::stdin(); let mut stdout = io::stdout(); |