diff --git a/src/ast.rs b/src/ast.rs index 4b7d959..2fe5ea8 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -288,7 +288,7 @@ impl Expr { Expression { type_: None, expr: self, - span: span, + span, } } } diff --git a/src/emit.rs b/src/emit.rs index 88f8719..670527a 100644 --- a/src/emit.rs +++ b/src/emit.rs @@ -66,7 +66,7 @@ pub fn emit(script: &ast::Script, module_name: &str, options: &Options) -> Vec HashMap Result<()> { let script_dir = path.parent().expect("Script path has no parent"); for data in &mut script.data { for values in &mut data.data { - match values { - ast::DataValues::File { - ref path, - ref mut data, - } => { - let mut full_path = script_dir.to_path_buf(); - full_path.push(path); - File::open(&full_path) - .map_err(|e| { - anyhow!("Failed to load data from {}: {}", full_path.display(), e) - })? - .read_to_end(data)?; - } - _ => (), + if let ast::DataValues::File { + ref path, + ref mut data, + } = values + { + let mut full_path = script_dir.to_path_buf(); + full_path.push(path); + File::open(&full_path) + .map_err(|e| { + anyhow!("Failed to load data from {}: {}", full_path.display(), e) + })? + .read_to_end(data)?; } } } diff --git a/src/lib.rs b/src/lib.rs index 5512412..39d9e59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,10 +20,7 @@ pub struct Options { impl Options { pub fn with_debug(self) -> Self { - Options { - debug: true, - ..self - } + Options { debug: true } } } @@ -36,7 +33,7 @@ pub fn compile_file>(path: P, options: Options) -> Result } pub fn compile_str(input: &str, path: &Path, options: Options) -> Result> { - let mut script = match parser::parse(&input) { + let mut script = match parser::parse(input) { Ok(script) => script, Err(_) => bail!("Parse failed"), }; @@ -44,7 +41,7 @@ pub fn compile_str(input: &str, path: &Path, options: Options) -> Result includes::resolve_includes(&mut script, path)?; constfold::fold_script(&mut script); - if let Err(_) = typecheck::tc_script(&mut script, &input) { + if typecheck::tc_script(&mut script, input).is_err() { bail!("Type check failed"); } let wasm = emit::emit( diff --git a/src/parser.rs b/src/parser.rs index 77e6698..7525879 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -183,7 +183,8 @@ fn lexer() -> impl Parser, Error = Simple> { u64::from_str_radix(&n, 16).map_err(|err| Simple::custom(span, err.to_string())) }) .or(text::int(10).try_map(|n: String, span: Span| { - u64::from_str_radix(&n, 10).map_err(|err| Simple::custom(span, err.to_string())) + n.parse::() + .map_err(|err| Simple::custom(span, err.to_string())) })) .boxed(); @@ -358,7 +359,7 @@ fn script_parser() -> impl Parser> + C let branch = just(Token::Branch) .ignore_then(identifier) - .map(|label| ast::Expr::Branch(label)); + .map(ast::Expr::Branch); let branch_if = just(Token::BranchIf) .ignore_then(expression.clone()) @@ -377,7 +378,7 @@ fn script_parser() -> impl Parser> + C .or(just(Token::Inline).to(ast::LetType::Inline))) .or_not(), ) - .then(identifier.clone()) + .then(identifier) .then(just(Token::Ctrl(':')).ignore_then(type_parser()).or_not()) .then( just(Token::Op("=".to_string())) @@ -394,7 +395,6 @@ fn script_parser() -> impl Parser> + C .boxed(); let assign = identifier - .clone() .then_ignore(just(Token::Op("=".to_string()))) .then(expression.clone()) .map(|(name, value)| ast::Expr::Assign { @@ -422,7 +422,6 @@ fn script_parser() -> impl Parser> + C .boxed(); let function_call = identifier - .clone() .then( expression .clone() @@ -676,7 +675,7 @@ fn script_parser() -> impl Parser> + C }) .boxed(); - let op_first = op_bit + op_bit .clone() .then( just(Token::Op("<|".to_string())) @@ -691,9 +690,7 @@ fn script_parser() -> impl Parser> + C } .with_span(span) }) - .boxed(); - - op_first + .boxed() }); expression_out = Some(expression.clone()); @@ -718,7 +715,7 @@ fn script_parser() -> impl Parser> + C } ast::Expr::Block { statements: statements.into_iter().map(|(expr, _)| expr).collect(), - final_expression: final_expression.map(|e| Box::new(e)), + final_expression: final_expression.map(Box::new), } .with_span(span) }) @@ -740,7 +737,7 @@ fn script_parser() -> impl Parser> + C let import_global = just(Token::Global) .ignore_then(just(Token::Mut).or_not()) - .then(identifier.clone()) + .then(identifier) .then_ignore(just(Token::Ctrl(':'))) .then(type_parser()) .map(|((mut_opt, name), type_)| ast::ImportType::Variable { @@ -751,7 +748,7 @@ fn script_parser() -> impl Parser> + C .boxed(); let import_function = just(Token::Fn) - .ignore_then(identifier.clone()) + .ignore_then(identifier) .then( type_parser() .separated_by(just(Token::Ctrl(','))) @@ -783,7 +780,6 @@ fn script_parser() -> impl Parser> + C .boxed(); let parameter = identifier - .clone() .then_ignore(just(Token::Ctrl(':'))) .then(type_parser()) .boxed(); @@ -792,7 +788,7 @@ fn script_parser() -> impl Parser> + C .or_not() .then(just(Token::Ident("start".to_string())).or_not()) .then_ignore(just(Token::Fn)) - .then(identifier.clone()) + .then(identifier) .then( parameter .separated_by(just(Token::Ctrl(','))) @@ -820,7 +816,7 @@ fn script_parser() -> impl Parser> + C let global = just(Token::Global) .ignore_then(just(Token::Mut).or_not()) - .then(identifier.clone()) + .then(identifier) .then(just(Token::Ctrl(':')).ignore_then(type_parser()).or_not()) .then(just(Token::Op("=".to_string())).ignore_then(expression.clone())) .then_ignore(just(Token::Ctrl(';'))) @@ -850,7 +846,7 @@ fn script_parser() -> impl Parser> + C ) .map(|(type_, values)| ast::DataValues::Array { type_, values }); - let data_string = string.clone().map(|s| ast::DataValues::String(s)); + let data_string = string.clone().map(ast::DataValues::String); let data_file = just(Token::Ident("file".to_string())) .ignore_then( diff --git a/src/typecheck.rs b/src/typecheck.rs index e8d8186..d16d2f8 100644 --- a/src/typecheck.rs +++ b/src/typecheck.rs @@ -160,7 +160,7 @@ pub fn tc_script(script: &mut ast::Script, source: &str) -> Result<()> { context.locals.locals[index].index = Some((locals_start + id) as u32); } - f.locals = std::mem::replace(&mut context.locals, ast::Locals::default()); + f.locals = std::mem::take(&mut context.locals); if f.body.type_ != f.type_ { result = type_mismatch(f.type_, &f.span, f.body.type_, &f.body.span, source); @@ -333,7 +333,7 @@ fn type_mismatch( "Expected type {:?}...", type1 .map(|t| format!("{:?}", t)) - .unwrap_or("void".to_string()) + .unwrap_or_else(|| "void".to_string()) )) .with_color(Color::Yellow), ) @@ -343,7 +343,7 @@ fn type_mismatch( "...but found type {}", type2 .map(|t| format!("{:?}", t)) - .unwrap_or("void".to_string()) + .unwrap_or_else(|| "void".to_string()) )) .with_color(Color::Red), ) @@ -406,7 +406,7 @@ fn missing_label(span: &Span, source: &str) -> Result<()> { .finish() .eprint(Source::from(source)) .unwrap(); - return Err(()); + Err(()) } fn tc_expression(context: &mut Context, expr: &mut ast::Expression) -> Result<()> {