diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2021-11-25 09:41:41 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2021-11-25 19:01:31 +0200 |
| commit | c04609bcdb7c7952501ddffc82fb97246ae0f6d4 (patch) | |
| tree | ffe56c8be1a218eac78733458b91563829c84f45 /src | |
| parent | adbca5c121caaa7ed041a4b1bd9f2a2ac6981af7 (diff) | |
Implement clippy lint suggestions
Diffstat (limited to 'src')
| -rw-r--r-- | src/ast.rs | 8 | ||||
| -rw-r--r-- | src/builtins.rs | 9 | ||||
| -rw-r--r-- | src/main.rs | 4 | ||||
| -rw-r--r-- | src/runtime.rs | 36 |
4 files changed, 27 insertions, 30 deletions
@@ -41,7 +41,7 @@ impl Statement { let mut parameters: Vec<Symbol> = vec![]; let mut expression_opt: Option<Rc<Expression>> = None; - while let Some(p) = inner.next() { + for p in inner { match p.as_rule() { Rule::symbol => parameters.push(p.as_span().as_str().to_owned()), Rule::expression => { @@ -61,8 +61,8 @@ impl Statement { Statement::Definition { symbol: symbol.to_owned(), - parameters: parameters, - expression: expression, + parameters, + expression, } } @@ -117,7 +117,7 @@ pub enum Expression { Binary(ExpressionInner, ExpressionInner), } -fn expression_vec_to_tuple(v: &Vec<ExpressionInner>) -> Rc<Expression> { +fn expression_vec_to_tuple(v: &[ExpressionInner]) -> Rc<Expression> { match v.len() { 1 => Rc::new(Expression::Unary(v[0].clone())), 2 => Rc::new(Expression::Binary(v[0].clone(), v[1].clone())), diff --git a/src/builtins.rs b/src/builtins.rs index 390a213..7b06864 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -1,4 +1,3 @@ -use super::ast; use super::runtime::{advance_v, Term, Value}; use super::*; use std::fmt; @@ -14,9 +13,9 @@ pub struct Builtin { impl Builtin { pub fn new(identifier: &'static str, n_arguments: usize) -> Builtin { Builtin { - identifier: identifier, + identifier, arguments: vec![], - n_arguments: n_arguments, + n_arguments, } } @@ -92,8 +91,8 @@ pub const B_INTEGER_DIVIDE: &str = "int.div"; pub const B_STRING_EQ: &str = "string.eq?"; pub const B_BOOL_TO_STRING: &str = "bool.to-string"; -pub fn try_ast_symbol_to_builtin_term(symbol: &ast::Symbol) -> Option<Term> { - let builtin = match symbol.as_str() { +pub fn try_ast_symbol_to_builtin_term(symbol: &str) -> Option<Term> { + let builtin = match symbol { "true" => return Some(make_boolean_true_function()), "false" => return Some(make_boolean_false_function()), "id" => return Some(make_identity_function()), diff --git a/src/main.rs b/src/main.rs index 4e89250..3b09791 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,7 +89,7 @@ fn main() -> Result<(), String> { println!("{}:\n{}\n", symbol, *expr); } continue; - } else if !s.ends_with(";") && s != "" && !s.starts_with("#") { + } else if !s.ends_with(';') && !s.is_empty() && !s.starts_with('#') { appending_input = true; continue; } else { @@ -110,6 +110,6 @@ fn main() -> Result<(), String> { } } } - _ => return Err("invalid subcommand".to_owned()), + _ => Err("invalid subcommand".to_owned()), } } diff --git a/src/runtime.rs b/src/runtime.rs index 07d8a70..18e8e2e 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -32,7 +32,7 @@ pub enum Term { impl Term { fn fmt_with_indent(&self, indent: usize) -> String { - let indent_str = std::iter::repeat("| ").take(indent).collect::<String>(); + let indent_str = "| ".repeat(indent); match self { Term::Lazy(symbol) => format!("{}Lazy({})", indent_str, symbol), Term::Variable(v) => format!("{}Variable({})", indent_str, v), @@ -220,7 +220,7 @@ pub fn repeatedly_reduce_term( } let (result_term, substitution_n) = - reduce_term(&symbol_table, term.clone(), bound_variable_opt, false)?; + reduce_term(symbol_table, term.clone(), bound_variable_opt, false)?; if cfg!(feature = "debug") { println!("i = {}:\n{}\n", i, result_term); @@ -241,7 +241,7 @@ pub fn repeatedly_reduce_term( } let (result_term, substitution_n) = - reduce_term(&symbol_table, term.clone(), bound_variable_opt, true)?; + reduce_term(symbol_table, term.clone(), bound_variable_opt, true)?; if cfg!(feature = "debug") { println!("i = {}:\n{}\n", i, result_term); @@ -259,7 +259,7 @@ pub fn repeatedly_reduce_term( fn process_expr_inner_unary( symbol_table: &HashMap<String, Rc<Term>>, inner: &ast::ExpressionInner, - bound_symbols: &Vec<(ast::Symbol, usize)>, + bound_symbols: &[(ast::Symbol, usize)], ) -> Rc<Term> { match inner { ast::ExpressionInner::IntegerLiteral(value) => { @@ -272,19 +272,17 @@ fn process_expr_inner_unary( let bound_symbol_opt = bound_symbols .iter() .find(|(bound_symbol, _)| bound_symbol == value); - if bound_symbol_opt.is_some() { - return Rc::new(Term::Variable(bound_symbol_opt.unwrap().1)); + + if let Some(bound_symbol) = bound_symbol_opt { + return Rc::new(Term::Variable(bound_symbol.1)); } - let table_lookup_value = symbol_table.get(value); - if table_lookup_value.is_some() { - let lookup_rc = table_lookup_value.unwrap(); - return Rc::clone(lookup_rc); + if let Some(table_lookup_value) = symbol_table.get(value) { + return Rc::clone(table_lookup_value); } - let builtin_value = builtins::try_ast_symbol_to_builtin_term(value); - if builtin_value.is_some() { - return Rc::new(builtin_value.unwrap()); + if let Some(builtin_value) = builtins::try_ast_symbol_to_builtin_term(value) { + return Rc::new(builtin_value); } Rc::new(Term::Lazy(value.clone())) @@ -300,7 +298,7 @@ fn process_expr_inner_binary( symbol_table: &HashMap<String, Rc<Term>>, lhs: &ast::ExpressionInner, rhs: &ast::ExpressionInner, - bound_symbols: &Vec<(ast::Symbol, usize)>, + bound_symbols: &[(ast::Symbol, usize)], ) -> Rc<Term> { match lhs { ast::ExpressionInner::Expression(value_rc) => { @@ -339,7 +337,7 @@ fn process_expr_inner_binary( fn process_expr( symbol_table: &HashMap<String, Rc<Term>>, expression: &ast::Expression, - bound_symbols: &Vec<(ast::Symbol, usize)>, + bound_symbols: &[(ast::Symbol, usize)], ) -> Rc<Term> { match expression { ast::Expression::Unary(inner) => { @@ -354,7 +352,7 @@ fn process_expr( pub type ProcessResult = Result<(Vec<Rc<Term>>, HashMap<String, Rc<Term>>), String>; pub fn process( - program: &ast::Program, + program: &[ast::Statement], initial_symbol_table: Option<&mut HashMap<String, Rc<Term>>>, ) -> ProcessResult { let empty_symbol_table = &mut HashMap::new(); @@ -375,7 +373,7 @@ pub fn process( .map(|param| (param.clone(), advance_v())) .collect(); - let mut term = process_expr(&symbol_table, expression, &bound_params); + let mut term = process_expr(symbol_table, expression, &bound_params); bound_params.iter().rev().for_each(|(_, v)| { term = Rc::new(Term::Abstraction(*v, Rc::clone(&term))); @@ -384,8 +382,8 @@ pub fn process( symbol_table.insert(symbol.clone(), term); } ast::Statement::Expression(expression) => { - let term = process_expr(&symbol_table, expression, &vec![]); - let result_term = repeatedly_reduce_term(&symbol_table, term, &None)?; + let term = process_expr(symbol_table, expression, &[]); + let result_term = repeatedly_reduce_term(symbol_table, term, &None)?; println!("[{}]: {}", index, result_term); output_terms.push(result_term); |
