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/ast.rs | 2 +- src/eval/builtins.rs | 24 ++++++------------- src/eval/mod.rs | 33 +++++++++++++------------- src/parser.rs | 67 +++++++++++++++++++++++----------------------------- 4 files changed, 54 insertions(+), 72 deletions(-) (limited to 'src') diff --git a/src/ast.rs b/src/ast.rs index 7e83b3d..ec5f953 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -10,7 +10,7 @@ impl Program { } } - pub fn from_str(mut self, i: &str) -> Result> { + pub fn with_file(mut self, i: &str) -> Result> { use nom::Finish; let (remaining_input, stanzas) = crate::parser::parse_file(i).finish()?; assert!(remaining_input.trim().is_empty(), "{remaining_input}"); 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 - "#]] + "#]], ); - } } diff --git a/src/parser.rs b/src/parser.rs index 8b04307..0719f93 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -13,20 +13,21 @@ use nom::{ use crate::ast::*; use crate::string::parse_string; -fn ws<'a, F: 'a, O, E>(inner: F) -> impl FnMut(&'a str) -> IResult<&'a str, O, E> +fn ws<'a, F, O, E>(inner: F) -> impl FnMut(&'a str) -> IResult<&'a str, O, E> where F: FnMut(&'a str) -> IResult<&'a str, O, E>, + F: 'a, E: ParseError<&'a str>, { delimited(multispace0, inner, multispace0) } // TODO use this -fn _parse_comment<'a>(i: &'a str) -> IResult<&'a str, ()> { +fn _parse_comment(i: &str) -> IResult<&str, ()> { value((), pair(tag("//"), is_not("\n\r")))(i) } -fn parse_unit<'a>(i: &'a str) -> IResult<&'a str, ()> { +fn parse_unit(i: &str) -> IResult<&str, ()> { let open = char('('); let close = char(')'); let unit = tuple((open, close)); @@ -39,7 +40,7 @@ fn parse_bool(i: &str) -> IResult<&str, bool> { alt((t, f)).parse(i) } -fn parse_int<'a>(i: &'a str) -> IResult<&'a str, i128> { +fn parse_int(i: &str) -> IResult<&str, i128> { map(recognize(many1(one_of("0123456789"))), |s: &str| { s.parse::().unwrap() })(i) @@ -57,7 +58,7 @@ fn parse_ident(i: &str) -> IResult<&str, Identifier> { map(parse_name, str::to_owned)(i) } -fn parse_lit<'a>(i: &'a str) -> IResult<&'a str, Literal> { +fn parse_lit(i: &str) -> IResult<&str, Literal> { alt(( map(parse_string, Literal::Str), map(parse_int, Literal::Int), @@ -118,28 +119,28 @@ where }) } -fn parse_assign<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_assign(i: &str) -> IResult<&str, Expr> { let op = map(parse_assign_op, BinOp::Assign); let recursive = parse_binary(parse_atom, op, parse_assign); let base = parse_union; alt((recursive, base)).parse(i) } -fn parse_union<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_union(i: &str) -> IResult<&str, Expr> { let op = parse_op("||", BinOp::Logic(LogicOp::Or)); let recursive = parse_binary(parse_intersection, op, parse_union); let base = parse_intersection; alt((recursive, base)).parse(i) } -fn parse_intersection<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_intersection(i: &str) -> IResult<&str, Expr> { let op = parse_op("&&", BinOp::Logic(LogicOp::And)); let recursive = parse_binary(parse_negated, op, parse_intersection); let base = parse_negated; alt((recursive, base)).parse(i) } -fn parse_negated<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_negated(i: &str) -> IResult<&str, Expr> { let op = parse_op("!", UnaryOp::Not); let recursive = map(tuple((op, parse_rel)), |(op, expr)| { Expr::Unary(expr.boxed(), op) @@ -148,14 +149,14 @@ fn parse_negated<'a>(i: &'a str) -> IResult<&'a str, Expr> { alt((recursive, base)).parse(i) } -fn parse_rel<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_rel(i: &str) -> IResult<&str, Expr> { let op = map(parse_cmp_op, BinOp::Cmp); let recursive = parse_binary(parse_sum, op, parse_rel); let base = parse_sum; alt((recursive, base)).parse(i) } -fn parse_sum<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_sum(i: &str) -> IResult<&str, Expr> { let add = parse_op("+", BinOp::Arith(ArithOp::Add)); let sub = parse_op("-", BinOp::Arith(ArithOp::Sub)); let op = alt((add, sub)); @@ -164,7 +165,7 @@ fn parse_sum<'a>(i: &'a str) -> IResult<&'a str, Expr> { alt((recursive, base)).parse(i) } -fn parse_mul<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_mul(i: &str) -> IResult<&str, Expr> { let mul = parse_op("*", BinOp::Arith(ArithOp::Mul)); let div = parse_op("/", BinOp::Arith(ArithOp::Div)); let mod_ = parse_op("%", BinOp::Arith(ArithOp::Mod)); @@ -174,7 +175,7 @@ fn parse_mul<'a>(i: &'a str) -> IResult<&'a str, Expr> { alt((recursive, base)).parse(i) } -fn parse_field_or_index<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_field_or_index(i: &str) -> IResult<&str, Expr> { enum FieldOrIndex { Field(String), Index(Expr), @@ -200,14 +201,14 @@ fn parse_field_or_index<'a>(i: &'a str) -> IResult<&'a str, Expr> { )(i) } -fn parse_list<'a>(i: &'a str) -> IResult<&'a str, List> { +fn parse_list(i: &str) -> IResult<&str, List> { let open = ws(char('[')); let items = separated_list0(char(','), parse_expr); let close = ws(char(']')); map(tuple((open, items, close)), |(_, items, _)| List { items }).parse(i) } -fn parse_atom<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_atom(i: &str) -> IResult<&str, Expr> { let inner = alt(( map(tag("node"), |_| Expr::Node), map(parse_block, Expr::Block), @@ -221,7 +222,7 @@ fn parse_atom<'a>(i: &'a str) -> IResult<&'a str, Expr> { ws(inner).parse(i) } -fn parse_call<'a>(i: &'a str) -> IResult<&'a str, Call> { +fn parse_call(i: &str) -> IResult<&str, Call> { let ident = parse_ident; let open = ws(char('(')); let args = separated_list0(char(','), parse_expr); @@ -236,14 +237,14 @@ fn parse_call<'a>(i: &'a str) -> IResult<&'a str, Call> { .parse(i) } -fn parse_block<'a>(i: &'a str) -> IResult<&'a str, Block> { +fn parse_block(i: &str) -> IResult<&str, Block> { let open = ws(char('{')); let statements = map(many0(parse_statement), |body| Block { body }); let close = ws(char('}')); delimited(open, statements, close).parse(i) } -fn parse_if<'a>(i: &'a str) -> IResult<&'a str, IfExpr> { +fn parse_if(i: &str) -> IResult<&str, IfExpr> { let if_ = delimited(multispace0, tag("if"), multispace1); let open = char('('); @@ -265,15 +266,15 @@ fn parse_if<'a>(i: &'a str) -> IResult<&'a str, IfExpr> { )(i) } -fn parse_expr<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_expr(i: &str) -> IResult<&str, Expr> { parse_assign.parse(i) } -fn parse_bare<'a>(i: &'a str) -> IResult<&'a str, Expr> { +fn parse_bare(i: &str) -> IResult<&str, Expr> { parse_expr(i) } -fn parse_type<'a>(i: &'a str) -> IResult<&'a str, Type> { +fn parse_type(i: &str) -> IResult<&str, Type> { let int = value(Type::Integer, tag("int")); let string = value(Type::String, tag("string")); let bool_ = value(Type::Boolean, tag("bool")); @@ -281,7 +282,7 @@ fn parse_type<'a>(i: &'a str) -> IResult<&'a str, Type> { alt((int, string, bool_, list)).parse(i) } -fn parse_declaration<'a>(i: &'a str) -> IResult<&'a str, Declaration> { +fn parse_declaration(i: &str) -> IResult<&str, Declaration> { let ty = parse_type; let name = parse_ident; let op = ws(char('=')); @@ -292,7 +293,7 @@ fn parse_declaration<'a>(i: &'a str) -> IResult<&'a str, Declaration> { )(i) } -fn parse_statement<'a>(i: &'a str) -> IResult<&'a str, Statement> { +fn parse_statement(i: &str) -> IResult<&str, Statement> { let semicolon = ws(char(';')); let inner = alt(( map(parse_declaration, Statement::Declaration), @@ -364,38 +365,30 @@ fn parse_statement<'a>(i: &'a str) -> IResult<&'a str, Statement> { // }; // } -fn parse_modifier<'a>(i: &str) -> IResult<&str, Modifier> { +fn parse_modifier(i: &str) -> IResult<&str, Modifier> { let pre = value(Modifier::Enter, tag("enter")); let post = value(Modifier::Leave, tag("leave")); map(opt(alt((pre, post))), Option::unwrap_or_default)(i) } -fn parse_pattern<'a>(i: &str) -> IResult<&str, Pattern> { +fn parse_pattern(i: &str) -> IResult<&str, Pattern> { let begin = value(Pattern::Begin, ws(tag("BEGIN"))); let end = value(Pattern::End, ws(tag("END"))); ws(alt((begin, end, parse_tree_pattern))).parse(i) } -// fn parse_node_pattern<'a>(i: &str) -> IResult<&str, Pattern> { -// map( -// tuple((parse_modifier, multispace0, parse_ident)), -// |(modifier, _, kind)| Pattern::Node(NodePattern { modifier, kind }), -// ) -// .parse(i) -// } - -fn parse_tree_pattern<'a>(i: &str) -> IResult<&str, Pattern> { +fn parse_tree_pattern(i: &str) -> IResult<&str, Pattern> { let parse_matcher = alt((parse_tree_atom, parse_tree_list)); tuple((parse_modifier, multispace0, parse_matcher)) .map(|(modifier, _, matcher)| Pattern::Tree { modifier, matcher }) .parse(i) } -fn parse_tree_atom<'a>(i: &str) -> IResult<&str, TreePattern> { +fn parse_tree_atom(i: &str) -> IResult<&str, TreePattern> { parse_ident.map(TreePattern::Atom).parse(i) } -fn parse_tree_list<'a>(i: &str) -> IResult<&str, TreePattern> { +fn parse_tree_list(i: &str) -> IResult<&str, TreePattern> { let open = terminated(char('('), multispace0); let close = preceded(multispace0, char(')')); let list = separated_list1(multispace1, alt((parse_tree_atom, parse_tree_list))); @@ -404,7 +397,7 @@ fn parse_tree_list<'a>(i: &str) -> IResult<&str, TreePattern> { .parse(i) } -pub fn parse_stanza<'a>(i: &str) -> IResult<&str, Stanza> { +pub fn parse_stanza(i: &str) -> IResult<&str, Stanza> { map( tuple((parse_pattern, parse_block)), |(pattern, statements)| Stanza { -- cgit v1.2.3