diff --git a/src/compiler/compile.rs b/src/compiler/compile.rs index 828a291..e5a20bc 100644 --- a/src/compiler/compile.rs +++ b/src/compiler/compile.rs @@ -30,7 +30,7 @@ pub fn compile(program: String) -> Result<(), Box> { .first_element_child() .ok_or(InvalidProgram)? .children() - .find(|node| util::tag_name(&node) == "main") + .find(|node| util::tag_name(node) == "main") .ok_or(MissingChild("program", "main"))?; let main_ast = Instruction::from_children(main)?; diff --git a/src/instruction.rs b/src/instruction.rs index 42d5b30..43b0fdd 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -46,7 +46,7 @@ impl Instruction { Ok(match util::tag_name(&node).as_str() { "value" => Instruction::Value( node.attribute("variable") - .and_then(|a| Some(String::from(a))) + .map(String::from) .ok_or(MissingAttribute("value", "variable"))?, ), "assign" => Instruction::Assign( diff --git a/src/interpreter/run.rs b/src/interpreter/run.rs index f04fe58..15ae322 100644 --- a/src/interpreter/run.rs +++ b/src/interpreter/run.rs @@ -234,7 +234,7 @@ impl RunUtil for Instruction { pub trait Run { fn run(&self, ctx: &mut Context, globals: &Context) -> Result, Box>; fn run_all( - ins: &Vec, + ins: &[Instruction], ctx: &mut Context, globals: &Context, ) -> Result>, Box>; @@ -242,7 +242,7 @@ pub trait Run { impl Run for Instruction { fn run_all( - ins: &Vec, + ins: &[Instruction], ctx: &mut Context, globals: &Context, ) -> Result>, Box> { @@ -250,11 +250,11 @@ impl Run for Instruction { } fn run(&self, ctx: &mut Context, globals: &Context) -> Result, Box> { - Ok(if let None = ctx.value(&String::from("__return")) { + Ok(if ctx.value(&String::from("__return")).is_none() { match self { Instruction::Value(key) => { Some(match ctx.value(key).ok_or(UnknownVariable(key.clone()))? { - Value::Array(vecrc) => Value::Array(Rc::clone(&vecrc)), + Value::Array(vecrc) => Value::Array(Rc::clone(vecrc)), val => val.clone(), }) } @@ -388,7 +388,7 @@ impl Run for Instruction { let vals: Vec = Instruction::run_all(args, ctx, globals)?.ok_or(InvalidValue("call"))?; let fct_val = ctx - .value(&fct_name) + .value(fct_name) .ok_or(UnknownVariable(fct_name.clone()))?; if let Value::Function(f) = fct_val { let mut local = ctx.clone(); @@ -511,7 +511,7 @@ pub fn run(program: String) -> Result<(), Box> { .first_element_child() .ok_or(InvalidProgram)? .children() - .find(|node| util::tag_name(&node) == "main") + .find(|node| util::tag_name(node) == "main") .ok_or(MissingChild("program", "main"))?; let main_ast = Instruction::from_children(main)?; @@ -519,7 +519,7 @@ pub fn run(program: String) -> Result<(), Box> { .first_element_child() .ok_or(InvalidProgram)? .children() - .filter(|node| node.tag_name().name() == String::from("function")); + .filter(|node| node.tag_name().name() == "function"); for fun in functions { ctx.assign( diff --git a/src/interpreter/stl.rs b/src/interpreter/stl.rs index a5887e5..439b49b 100644 --- a/src/interpreter/stl.rs +++ b/src/interpreter/stl.rs @@ -62,7 +62,7 @@ fn print_line(vals: Vec) -> Result, Box> { } fn input(vals: Vec) -> Result, Box> { - if vals.len() == 0 { + if vals.is_empty() { let mut line = String::new(); stdin().read_line(&mut line)?; line.pop(); @@ -181,7 +181,7 @@ fn array_length(vals: Vec) -> Result, Box> { fn to_ascii(vals: Vec) -> Result, Box> { if vals.len() == 1 { if let Value::Integer(i) = &vals[0] { - if &0 <= i && i <= &255 { + if (0..=255).contains(i) { Ok(Some(Value::String(String::from_utf8(vec![*i as u8])?))) } else { Err(InvalidArgument("to-ascii", "integer").into()) @@ -211,12 +211,9 @@ fn from_ascii(vals: Vec) -> Result, Box> { } fn get_args(vals: Vec) -> Result, Box> { - if vals.len() == 0 { + if vals.is_empty() { Ok(Some(Value::Array(Rc::new(RefCell::new( - std::env::args() - .skip(1) - .map(|arg| Value::String(arg)) - .collect(), + std::env::args().skip(1).map(Value::String).collect(), ))))) } else { Err(BadArgumentCount("get-args", vals.len(), 0).into()) @@ -233,7 +230,7 @@ fn write_file(vals: Vec) -> Result, Box> { .append(Value::to_bool(&vals[2])) .open(path) { - if let Ok(_) = write!(file, "{}", contents) { + if write!(file, "{}", contents).is_ok() { Ok(None) } else { Err(InaccessibleFile(path.clone()).into()) diff --git a/src/interpreter/value.rs b/src/interpreter/value.rs index 5939ba6..fdd9c6c 100644 --- a/src/interpreter/value.rs +++ b/src/interpreter/value.rs @@ -27,7 +27,7 @@ impl Function { } self.args .iter() - .zip(args.into_iter()) + .zip(args) .for_each(|(p, a)| ctx.assign(p.clone(), a)); for i in self.ins.iter() { i.run(ctx, globals)?; @@ -37,20 +37,22 @@ impl Function { pub fn from(fun: &Node<'_, '_>) -> Result> { Ok(Function { - args: util::find_node(&fun, "arguments") + args: util::find_node(fun, "arguments") .ok_or(MissingChild("call", "arguments"))? .children() .filter(Node::is_element) - .map(|n| n.attribute("name").and_then(|s| Some(String::from(s)))) + .map(|n| n.attribute("name").map(String::from)) .collect::>>() .ok_or(Unnamed("argument"))?, ins: Instruction::from_children( - util::find_node(&fun, "body").ok_or(MissingChild("call", "body"))?, + util::find_node(fun, "body").ok_or(MissingChild("call", "body"))?, )?, }) } } +type StdFn = fn(Vec) -> Result, Box>; + #[derive(Clone, Debug)] pub enum Value { Integer(i64), @@ -58,7 +60,7 @@ pub enum Value { String(String), Array(Rc>>), Function(Function), - StdFunction(fn(Vec) -> Result, Box>), + StdFunction(StdFn), } impl Value { @@ -66,7 +68,7 @@ impl Value { match self { Value::Integer(i) => *i != 0, Value::Real(f) => *f != 0.0, - Value::String(s) => s.len() != 0, + Value::String(s) => !s.is_empty(), Value::Array(v) => v.borrow().len() != 0, _ => true, }