remove return statements

This commit is contained in:
Pyther99 2021-07-13 19:27:33 +02:00 committed by finngaertner2@gmx.de
parent 2ccee2ccd2
commit 3ee5e00b11
6 changed files with 40 additions and 42 deletions

View File

@ -21,7 +21,7 @@ pub fn evaluate(ast: &AstNode) -> Result<Number, String> {
///
/// All return values of this function are hard-coded.
pub fn factorial(input: d128) -> d128 {
return lookup_factorial(input.into());
lookup_factorial(input.into())
}
/// Returns the square root of a [`struct@d128`]
@ -31,7 +31,7 @@ pub fn sqrt(input: d128) -> d128 {
for _ in 0..10 {
n = (n + input/n) * half;
}
return n
n
}
/// Returns the cube root of a [`struct@d128`]
@ -43,7 +43,7 @@ pub fn cbrt(input: d128) -> d128 {
let z2 = n*n;
n = n - ((n*z2 - input) / (three*z2));
}
return n
n
}
/// Returns the sine of a [`struct@d128`]
@ -72,19 +72,19 @@ pub fn sin(mut input: d128) -> d128 {
result += neg_one.pow(i) * (input.pow(calc_result) / factorial(calc_result));
}
return negative_correction * result;
negative_correction * result
}
/// Returns the cosine of a [`struct@d128`]
pub fn cos(input: d128) -> d128 {
let half_pi = d128!(1.570796326794896619231321691639751);
return sin(half_pi - input);
sin(half_pi - input)
}
/// Returns the tangent of a [`struct@d128`]
pub fn tan(input: d128) -> d128 {
return sin(input) / cos(input);
sin(input) / cos(input)
}
/// Evaluate an [`AstNode`] into a [`Number`]
@ -112,41 +112,41 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
Cbrt => {
if child_answer.unit.category() == UnitType::NoType {
let result = cbrt(child_answer.value);
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
} else {
return Err("log() only accepts UnitType::NoType".to_string())
Err("log() only accepts UnitType::NoType".to_string())
}
},
Sqrt => {
if child_answer.unit.category() == UnitType::NoType {
let result = sqrt(child_answer.value);
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
} else {
return Err("log() only accepts UnitType::NoType".to_string())
Err("log() only accepts UnitType::NoType".to_string())
}
},
Log => {
if child_answer.unit.category() == UnitType::NoType {
let result = child_answer.value.log10();
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
} else {
return Err("log() only accepts UnitType::NoType".to_string())
Err("log() only accepts UnitType::NoType".to_string())
}
},
Ln => {
if child_answer.unit.category() == UnitType::NoType {
let result = child_answer.value.ln();
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
} else {
return Err("ln() only accepts UnitType::NoType".to_string())
Err("ln() only accepts UnitType::NoType".to_string())
}
},
Exp => {
if child_answer.unit.category() == UnitType::NoType {
let result = child_answer.value.exp(child_answer.value);
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
} else {
return Err("exp() only accepts UnitType::NoType".to_string())
Err("exp() only accepts UnitType::NoType".to_string())
}
},
Round => {
@ -155,37 +155,37 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
let rounding_change = result - child_answer.value;
// If the result was rounded down by 0.5, correct by +1
if rounding_change == d128!(-0.5) { result += d128!(1); }
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
},
Ceil => {
let mut result = child_answer.value.quantize(d128!(1));
let rounding_change = result - child_answer.value;
if rounding_change.is_negative() { result += d128!(1); }
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
},
Floor => {
let mut result = child_answer.value.quantize(d128!(1));
let rounding_change = result - child_answer.value;
if !rounding_change.is_negative() { result -= d128!(1); }
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
},
Abs => {
let mut result = child_answer.value.abs();
let rounding_change = result - child_answer.value;
if rounding_change == d128!(-0.5) { result += d128!(1); }
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
},
Sin => {
let result = sin(child_answer.value);
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
},
Cos => {
let result = cos(child_answer.value);
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
},
Tan => {
let result = tan(child_answer.value);
return Ok(Number::new(result, child_answer.unit))
Ok(Number::new(result, child_answer.unit))
},
}
}
@ -201,7 +201,7 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
},
Token::Paren => {
let child_node = children.get(0).ok_or("Paren has no child[0]")?;
return evaluate_node(child_node)
evaluate_node(child_node)
},
Token::UnaryOperator(operator) => {
let child_node = children.get(0).ok_or(format!("Token {:?} has no child[0]", token))?;
@ -241,18 +241,18 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
if let Token::Unit(right_unit) = right_child.token {
let left = evaluate_node(left_child)?;
let result = convert(left, right_unit)?;
return Ok(result)
Ok(result)
} else {
return Err("Right side of To operator needs to be a unit".to_string())
Err("Right side of To operator needs to be a unit".to_string())
}
},
Of => {
let left = evaluate_node(left_child)?;
let right = evaluate_node(right_child)?;
if left.unit == Unit::NoUnit {
return Ok(Number::new(left.value * right.value, right.unit))
Ok(Number::new(left.value * right.value, right.unit))
} else {
return Err("Left side of the Of operator must be NoUnit".to_string())
Err("Left side of the Of operator must be NoUnit".to_string())
}
},
}

View File

@ -46,7 +46,7 @@ fn read_word_plain(chars: &mut Peekable<Graphemes>) -> String {
break;
}
}
return word;
word
}
/// Read next as a word, otherwise return empty string.
@ -84,7 +84,7 @@ fn read_word(first_c: &str, lexer: &mut Lexer) -> String {
_ => {},
}
}
return word;
word
}
fn parse_token(c: &str, lexer: &mut Lexer) -> Result<(), String> {
@ -596,7 +596,7 @@ fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> {
}
};
lexer.tokens.push(token);
return Ok(());
Ok(())
}
struct Lexer<'a> {
@ -645,8 +645,8 @@ pub fn lex(input: &str, remove_trailing_operator: bool, default_degree: Unit) ->
}
}
if tokens.len() == 0 {
return Err(format!("Input was empty"))
if tokens.is_empty() {
Err("Input was empty".to_string())
}
let mut token_index = 0;

View File

@ -252,7 +252,7 @@ pub fn eval(input: &str, allow_trailing_operators: bool, default_degree: Unit, v
println!("\u{23f1} {:.3}ms evaluation", eval_time/1000.0/1000.0);
}
return Ok(answer)
Ok(answer)
},
Err(e) => Err(format!("Eval error: {}", e)),
}

View File

@ -4,7 +4,7 @@ use decimal::d128;
/// Returns the corresponding [`d128`] of a [`NamedNumber`]
pub fn lookup_named_number(named_number: &NamedNumber) -> d128 {
return match named_number {
match named_number {
Hundred => d128!(100),
Thousand => d128!(1000),
Million => d128!(1000000),
@ -34,7 +34,7 @@ pub fn lookup_named_number(named_number: &NamedNumber) -> d128 {
/// Returns the factorial of an `i32` as a [`struct@d128`]
pub fn lookup_factorial(n: i32) -> d128 {
return match n {
match n {
0 => d128!(1),
1 => d128!(1),
2 => d128!(2),
@ -1037,5 +1037,5 @@ pub fn lookup_factorial(n: i32) -> d128 {
999 => d128!(4.023872600770937735437024339230041E+2564),
1000 => d128!(4.023872600770937735437024339230041E+2567),
_ => d128!("NaN"),
};
}
}

View File

@ -253,11 +253,9 @@ pub fn parse_level_5(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), S
let (right_node, next_pos) = parse_level_6(tokens, pos + 1)?;
let mut new_node = AstNode::new(Token::Negative);
new_node.children.push(right_node);
return Ok((new_node, next_pos));
Ok((new_node, next_pos))
},
_ => {
return Ok(parse_level_6(tokens, pos)?);
}
_ => parse_level_6(tokens, pos)
}
}

View File

@ -288,7 +288,7 @@ fn get_inverted_millivolt_weight() -> d128 {
///
/// This is not sufficient for [`Temperature`] units.
pub fn get_conversion_factor(unit: Unit, to_unit: Unit) -> d128 {
return unit.weight() / to_unit.weight();
unit.weight() / to_unit.weight()
}
/// Convert a [`Number`] to a specified [`Unit`].