diff --git a/src/parser/mod.rs b/src/parser/mod.rs index dd9d305..85cf281 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -98,13 +98,13 @@ impl PreToken { // Units "m" => { - let mut u = Quantity::new_rational_from_string("1").unwrap(); + let mut u = Quantity::new_rational(1f64).unwrap(); u.add_unit(BaseUnit::Meter, 1f64); Token::Number(u) }, "s" => { - let mut u = Quantity::new_rational_from_string("1").unwrap(); + let mut u = Quantity::new_rational(1f64).unwrap(); u.add_unit(BaseUnit::Second, 1f64); Token::Number(u) } diff --git a/src/quantity/quantity.rs b/src/quantity/quantity.rs index 0c6c166..2ebf01f 100644 --- a/src/quantity/quantity.rs +++ b/src/quantity/quantity.rs @@ -25,9 +25,9 @@ pub struct Quantity { impl ToString for Quantity { fn to_string(&self) -> String { - let n = self.v.to_string(); - //n.push(' '); - //n.push_str(&u.to_string()); + let mut n = self.v.to_string(); + n.push(' '); + n.push_str(&self.u.to_string()); n } } @@ -130,10 +130,9 @@ impl Quantity { } pub fn pow(&self, pwr: Quantity) -> Quantity { - if !self.unitless() { panic!() } Quantity { v: self.v.pow(pwr.v), - u: self.u.clone() + u: self.u.pow(2f64) } } } diff --git a/src/quantity/unit.rs b/src/quantity/unit.rs index 28abd1e..3a997a5 100644 --- a/src/quantity/unit.rs +++ b/src/quantity/unit.rs @@ -58,10 +58,12 @@ impl Unit { }; } - pub fn pow(&mut self, pwr: f64) { - for (_, p) in &mut self.val { + pub fn pow(&self, pwr: f64) -> Unit { + let mut u = self.clone(); + for (_, p) in &mut u.val { *p *= pwr; - } + }; + return u; } } diff --git a/src/tokens/operator.rs b/src/tokens/operator.rs index e6290b5..1af5879 100644 --- a/src/tokens/operator.rs +++ b/src/tokens/operator.rs @@ -328,19 +328,27 @@ impl Operator{ if let Token::Number(v) = args { if v.is_zero() { return Err(()); } - return Ok(Token::Number(Quantity::new_rational(1f64).unwrap()/v)); + return Ok(Token::Number( + Quantity::new_rational(1f64).unwrap()/v + )); } else { panic!(); } }, Operator::Add => { - let mut sum = Quantity::new_rational(0f64).unwrap(); - for i in args.iter() { - let j = i.as_number(); + let mut sum; + if let Token::Number(s) = args[0].as_number() { + sum = s; + } else {panic!()}; + + let mut i: usize = 1; + while i < args.len() { + let j = args[i].as_number(); if let Token::Number(v) = j { sum += v; } else { panic!(); } + i += 1; } return Ok(Token::Number(sum)); },