From 013dc07b5d3e28c77b0efc51d18f4ffc371fa382 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 7 Oct 2024 20:52:36 +0530 Subject: fmt and clippy --- src/eval/builtins.rs | 24 +++++++----------------- src/eval/mod.rs | 33 ++++++++++++++++----------------- 2 files changed, 23 insertions(+), 34 deletions(-) (limited to 'src/eval') diff --git a/src/eval/builtins.rs b/src/eval/builtins.rs index 7820a8e..7d10a86 100644 --- a/src/eval/builtins.rs +++ b/src/eval/builtins.rs @@ -48,11 +48,8 @@ builtins! { fn print(ctx: &mut Context, args: &[ast::Expr]) -> Result { for arg in args { let val = ctx.eval_expr(arg)?; - let mut default_stream =Box::new(std::io::stdout()) as Box ; - let stream = ctx - .output_stream - .as_mut() - .unwrap_or(&mut default_stream); + let mut default_stream = Box::new(std::io::stdout()) as Box; + let stream = ctx.output_stream.as_mut().unwrap_or(&mut default_stream); write!(stream, "{val}").unwrap(); } Ok(Value::Unit) @@ -157,9 +154,9 @@ fn kind(ctx: &mut Context, args: &[ast::Expr]) -> Result { fn member(ctx: &mut Context, args: &[ast::Expr]) -> Result { let [list_expr, element_expr] = get_args::<2>(args)?; - let list = ctx.eval_expr(&list_expr)?; + let list = ctx.eval_expr(list_expr)?; let v = list.as_list()?; - let element = ctx.eval_expr(&element_expr)?; + let element = ctx.eval_expr(element_expr)?; v.iter() .any(|item| item == &element) .wrap(Value::Boolean) @@ -174,7 +171,7 @@ fn push(ctx: &mut Context, args: &[ast::Expr]) -> Result { lhs ))); }; - let element = ctx.eval_expr(&rhs)?; + let element = ctx.eval_expr(rhs)?; let variable = ctx.lookup_mut(ident)?; variable.mutate(|v| match &mut v.value { Value::List(l) => { @@ -195,9 +192,7 @@ fn pop(ctx: &mut Context, args: &[ast::Expr]) -> Result { }; let variable = ctx.lookup_mut(ident)?; variable.mutate(|v| match &mut v.value { - Value::List(l) => l - .pop() - .ok_or_else(|| Error::ArrayOutOfBounds { idx: 0, len: 0 }), + Value::List(l) => l.pop().ok_or(Error::ArrayOutOfBounds { idx: 0, len: 0 }), _ => Err(v.ty().expected([ast::Type::List])), }) } @@ -211,12 +206,7 @@ fn isempty(ctx: &mut Context, args: &[ast::Expr]) -> Result { .is_empty() .wrap(Value::Boolean) .wrap(Ok), - ast::Type::String => v - .as_str() - .unwrap() - .is_empty() - .wrap(Value::Boolean) - .wrap(Ok), + ast::Type::String => v.as_str().unwrap().is_empty().wrap(Value::Boolean).wrap(Ok), _ => Err(v.ty().expected([ast::Type::List])), } } diff --git a/src/eval/mod.rs b/src/eval/mod.rs index c4460c0..9455704 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -474,11 +474,11 @@ impl<'s> Context<'s> { ast::Expr::Unit => Ok(Value::Unit), ast::Expr::Lit(lit) => self.eval_lit(lit), ast::Expr::Ident(ident) => self.lookup(ident).map(Variable::value).cloned(), - ast::Expr::Bin(lhs, op, rhs) => self.eval_bin(&*lhs, *op, &*rhs), - ast::Expr::Unary(expr, op) => self.eval_unary(&*expr, *op), - ast::Expr::Call(call) => self.eval_call(&*call), - ast::Expr::List(list) => self.eval_list(&*list), - ast::Expr::Index(target, idx) => self.eval_index(&*target, &*idx), + ast::Expr::Bin(lhs, op, rhs) => self.eval_bin(lhs, *op, rhs), + ast::Expr::Unary(expr, op) => self.eval_unary(expr, *op), + ast::Expr::Call(call) => self.eval_call(call), + ast::Expr::List(list) => self.eval_list(list), + ast::Expr::Index(target, idx) => self.eval_index(target, idx), ast::Expr::If(if_expr) => self.eval_if(if_expr), ast::Expr::Block(block) => self.eval_block(block), ast::Expr::Node => self.eval_node(), @@ -609,7 +609,7 @@ impl<'s> Context<'s> { match op { ast::LogicOp::Or => { if l_value { - return Ok(l); + Ok(l) } else { let r = self.eval_expr(rhs)?; l.or(&r) @@ -617,7 +617,7 @@ impl<'s> Context<'s> { } ast::LogicOp::And => { if !l_value { - return Ok(l); + Ok(l) } else { let r = self.eval_expr(rhs)?; l.and(&r) @@ -644,7 +644,7 @@ impl<'s> Context<'s> { } fn eval_call(&mut self, call: &ast::Call) -> Result { - ((&*builtins::BUILTINS) + ((*builtins::BUILTINS) .get(call.function.as_str()) .ok_or_else(|| Error::FailedLookup(call.function.to_owned()))?)( self, @@ -675,7 +675,7 @@ impl<'s> Context<'s> { fn eval_declaration(&mut self, decl: &ast::Declaration) -> Result { let initial_value = match decl.init.as_ref() { - Some(init) => Some(self.eval_expr(&*init)?), + Some(init) => Some(self.eval_expr(init)?), None => None, }; let variable = self.bind(&decl.name, decl.ty)?; @@ -775,7 +775,7 @@ pub fn evaluate(file: &str, program: &str, language: tree_sitter::Language) -> R let tree = parser.parse(file, None).unwrap(); - let program = ast::Program::new().from_str(program).unwrap(); + let program = ast::Program::new().with_file(program).unwrap(); let mut ctx = Context::new(language) .with_input(file.to_owned()) .with_tree(tree) @@ -796,7 +796,7 @@ mod test { let mut parser = tree_sitter::Parser::new(); let _ = parser.set_language(&language); let tree = parser.parse(file, None).unwrap(); - let program = ast::Program::new().from_str(program).unwrap(); + let program = ast::Program::new().with_file(program).unwrap(); let mut output = Vec::new(); let mut ctx = Context::new(language) @@ -983,7 +983,7 @@ mod test { print(a); } ", - expect!["[5]"] + expect!["[5]"], ); gen_test( @@ -993,7 +993,7 @@ mod test { print(length(a)); } ", - expect!["3"] + expect!["3"], ); gen_test( @@ -1005,7 +1005,7 @@ mod test { print(member(a, 6)); } "#, - expect!["true, false"] + expect!["true, false"], ); gen_test( @@ -1029,7 +1029,7 @@ mod test { [5, 4, 3] [5, 4] [5] - "#]] + "#]], ); gen_test( @@ -1044,8 +1044,7 @@ mod test { expect![[r#" false true - "#]] + "#]], ); - } } -- cgit v1.2.3