Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy lints #23

Merged
merged 5 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand All @@ -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`]
Expand All @@ -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`]
Expand Down Expand Up @@ -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`]
Expand All @@ -93,7 +93,7 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
let children = &ast_node.children;
match token {
Token::Number(number) => {
Ok(Number::new(number.clone(), Unit::NoUnit))
Ok(Number::new(*number, Unit::NoUnit))
},
Token::Constant(constant) => {
match constant {
Expand All @@ -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 => {
Expand All @@ -155,44 +155,44 @@ 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))
},
}
}
Token::Unit(unit) => {
let child_node = children.get(0).ok_or("Unit has no child[0]")?;
let child_answer = evaluate_node(child_node)?;
Ok(Number::new(child_answer.value, unit.clone()))
Ok(Number::new(child_answer.value, *unit))
},
Token::Negative => {
let child_node = children.get(0).ok_or("Negative has no child[0]")?;
Expand All @@ -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))?;
Expand Down Expand Up @@ -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())
}
},
}
Expand Down
14 changes: 7 additions & 7 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ fn read_word_plain(chars: &mut Peekable<Graphemes>) -> String {
break;
}
}
return word;
word
}

/// Read next as a word, otherwise return empty string.
/// Leading whitespace is ignored. A trailing digit may be included.
fn read_word(first_c: &str, lexer: &mut Lexer) -> String {
let chars = &mut lexer.chars;
let mut word = first_c.trim().to_owned();
if word == "" {
if word.is_empty() {
// skip whitespace
while let Some(current_char) = chars.peek() {
if current_char.trim().is_empty() {
Expand All @@ -71,7 +71,7 @@ fn read_word(first_c: &str, lexer: &mut Lexer) -> String {
break;
}
}
if word != "" {
if !word.is_empty() {
match *chars.peek().unwrap_or(&"") {
"2" | "²" => {
word += "2";
Expand All @@ -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> {
Expand Down Expand Up @@ -596,7 +596,7 @@ fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> {
}
};
lexer.tokens.push(token);
return Ok(());
Ok(())
}

struct Lexer<'a> {
Expand Down Expand Up @@ -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() {
return Err("Input was empty".to_string());
}

let mut token_index = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,27 +232,27 @@ pub fn eval(input: &str, allow_trailing_operators: bool, default_degree: Unit, v
match lexer::lex(input, allow_trailing_operators, default_degree) {
Ok(tokens) => {
let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32;
if verbose == true { println!("Lexed TokenVector: {:?}", tokens); }
if verbose { println!("Lexed TokenVector: {:?}", tokens); }

let parse_start = Instant::now();
match parser::parse(&tokens) {
Ok(ast) => {
let parse_time = Instant::now().duration_since(parse_start).as_nanos() as f32;
if verbose == true { println!("Parsed AstNode: {:#?}", ast); }
if verbose { println!("Parsed AstNode: {:#?}", ast); }

let eval_start = Instant::now();
match evaluator::evaluate(&ast) {
Ok(answer) => {
let eval_time = Instant::now().duration_since(eval_start).as_nanos() as f32;

if verbose == true {
if verbose {
println!("Evaluated value: {} {:?}", answer.value, answer.unit);
println!("\u{23f1} {:.3}ms lexing", lex_time/1000.0/1000.0);
println!("\u{23f1} {:.3}ms parsing", parse_time/1000.0/1000.0);
println!("\u{23f1} {:.3}ms evaluation", eval_time/1000.0/1000.0);
}

return Ok(answer)
Ok(answer)
},
Err(e) => Err(format!("Eval error: {}", e)),
}
Expand Down
6 changes: 3 additions & 3 deletions src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -1037,5 +1037,5 @@ pub fn lookup_factorial(n: i32) -> d128 {
999 => d128!(4.023872600770937735437024339230041E+2564),
1000 => d128!(4.023872600770937735437024339230041E+2567),
_ => d128!("NaN"),
};
}
}
8 changes: 3 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl AstNode {
pub fn new(token: Token) -> AstNode {
AstNode {
children: Vec::new(),
token: token,
token,
}
}
}
Expand Down Expand Up @@ -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)
}
}

Expand Down
18 changes: 8 additions & 10 deletions src/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -494,7 +494,7 @@ pub fn to_ideal_joule_unit(number: Number) -> Number {
/// - If you multiply [`ElectricCurrent`] with [`Resistance`], the result has a unit of [`Voltage`]
/// - If you multiply [`Power`] with [`Time`], the result has a unit of [`Energy`]
pub fn multiply(left: Number, right: Number) -> Result<Number, String> {
Ok(actual_multiply(left, right, false)?)
actual_multiply(left, right, false)
}

fn actual_multiply(left: Number, right: Number, swapped: bool) -> Result<Number, String> {
Expand Down Expand Up @@ -544,15 +544,13 @@ fn actual_multiply(left: Number, right: Number, swapped: bool) -> Result<Number,
// 1 watt * 1 second = 1 joule
let result = (left.value * left.unit.weight()) * (right.value * right.unit.weight() / Unit::Second.weight());
match right.unit {
Second => return Ok(to_ideal_joule_unit(Number::new(result, Joule))),
_ => return Ok(to_ideal_unit(Number::new(result, Joule))),
};
} else {
if swapped == true {
Err(format!("Cannot multiply {:?} and {:?}", right.unit, left.unit))
} else {
actual_multiply(right, left, true)
Second => Ok(to_ideal_joule_unit(Number::new(result, Joule))),
_ => Ok(to_ideal_unit(Number::new(result, Joule))),
}
} else if swapped {
Err(format!("Cannot multiply {:?} and {:?}", right.unit, left.unit))
} else {
actual_multiply(right, left, true)
}
}

Expand Down