From cdda4d413e80ffc129f2109580dfde82d22035b5 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 20 Jun 2018 20:35:19 +0200 Subject: [PATCH] Solve most compiler warnings #52 --- rustfmt.toml | 1 + src/mango/ast_full/node/assignment.rs | 6 +-- src/mango/ast_full/node/unary_operation.rs | 6 +-- src/mango/ast_full/terminal/literal.rs | 4 +- src/mango/io/util.rs | 4 +- src/mango/lexing/code_lexer.rs | 58 +++++++++++----------- src/mango/lexing/combi_lexer.rs | 20 +------- src/mango/lexing/string_lexer.rs | 10 ++-- src/mango/lexing/util/lex_all.rs | 9 ++-- src/mango/lexing/util/mod.rs | 2 + src/mango/lexing/util/test_util.rs | 19 +++++++ src/mango/token/tokens/association.rs | 4 +- src/mango/token/tokens/block.rs | 5 +- src/mango/towasm/control/block.rs | 6 +-- src/mango/towasm/control/repeat.rs | 6 +-- src/mango/towasm/numeric/arithmetic.rs | 24 ++------- src/mango/towasm/numeric/logic.rs | 24 ++------- src/mango/towasm/scope/function.rs | 25 ++-------- src/mango/towasm/scope/module.rs | 6 +-- src/mango/towasm/tests.rs | 34 ++++--------- src/mango/towasm/values/assign.rs | 6 +-- src/mango/towasm/values/localvar.rs | 10 +--- src/mango/util/codeparts/keyword.rs | 5 +- src/mango/util/codeparts/operator.rs | 5 +- src/mango/util/errors/code_problem.rs | 15 ++---- src/mango/util/errors/collector.rs | 26 ++-------- src/mango/util/format/strings.rs | 5 +- src/mango/util/numtype/eqfloat.rs | 10 +--- src/mango/util/parsetxt/int.rs | 7 ++- src/mango/util/parsetxt/real.rs | 21 ++------ src/mango/util/strslice/slice.rs | 5 +- src/mango/util/strtype/msg.rs | 4 +- src/mango/util/strtype/name.rs | 30 +++-------- 33 files changed, 120 insertions(+), 302 deletions(-) create mode 100644 src/mango/lexing/util/test_util.rs diff --git a/rustfmt.toml b/rustfmt.toml index 44148a2d..eaf1d122 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1,2 @@ reorder_imports = true +max_width = 140 diff --git a/src/mango/ast_full/node/assignment.rs b/src/mango/ast_full/node/assignment.rs index 558d6d3a..4b060303 100644 --- a/src/mango/ast_full/node/assignment.rs +++ b/src/mango/ast_full/node/assignment.rs @@ -21,11 +21,7 @@ impl AssignmentAST { impl ToText for AssignmentAST { fn to_text(&self) -> String { - return format!( - "{0:} = ({1:})", - self.assignee.to_text(), - self.value.to_text() - ); + return format!("{0:} = ({1:})", self.assignee.to_text(), self.value.to_text()); } } diff --git a/src/mango/ast_full/node/unary_operation.rs b/src/mango/ast_full/node/unary_operation.rs index 751105a3..36fc77b1 100644 --- a/src/mango/ast_full/node/unary_operation.rs +++ b/src/mango/ast_full/node/unary_operation.rs @@ -22,11 +22,7 @@ impl UnaryOperationAST { impl ToText for UnaryOperationAST { fn to_text(&self) -> String { - return format!( - "({0:} {1:})", - self.operator.to_text(), - self.subject.to_text() - ); + return format!("({0:} {1:})", self.operator.to_text(), self.subject.to_text()); } } diff --git a/src/mango/ast_full/terminal/literal.rs b/src/mango/ast_full/terminal/literal.rs index b585461d..74d907e8 100644 --- a/src/mango/ast_full/terminal/literal.rs +++ b/src/mango/ast_full/terminal/literal.rs @@ -31,9 +31,7 @@ pub struct StringLiteralAST { impl FloatLiteralAST { pub fn new(value: f64) -> Self { - FloatLiteralAST { - value: f64eq::new(value), - } + FloatLiteralAST { value: f64eq::new(value) } } } diff --git a/src/mango/io/util.rs b/src/mango/io/util.rs index 258d0e8c..e70c250e 100644 --- a/src/mango/io/util.rs +++ b/src/mango/io/util.rs @@ -9,9 +9,7 @@ pub struct RegexCache { impl RegexCache { // Not public to prevent having more than one instance. fn new() -> Self { - RegexCache { - cache: HashMap::new(), - } + RegexCache { cache: HashMap::new() } } pub fn make_or_get(&mut self, subpattern: &str) -> &Regex { diff --git a/src/mango/lexing/code_lexer.rs b/src/mango/lexing/code_lexer.rs index 18adb6ee..8134247f 100644 --- a/src/mango/lexing/code_lexer.rs +++ b/src/mango/lexing/code_lexer.rs @@ -1,8 +1,6 @@ use mango::io::typ::Reader; use mango::io::typ::ReaderResult::*; use mango::lexing::string_lexer::StringLexer; -use mango::lexing::typ::Lexer; -use mango::lexing::typ::MaybeToken; use mango::lexing::typ::SubLexer; use mango::lexing::typ::SubLexerResult; use mango::token::special::UnlexableToken; @@ -17,7 +15,6 @@ use mango::token::tokens::ParenthesisCloseToken; use mango::token::tokens::ParenthesisOpenToken; use mango::token::tokens::StartBlockToken; use mango::token::Tokens; -use mango::util::collection::Queue; use mango::util::strslice::char_ops::CharOps; use mango::util::strslice::charsliceto; @@ -72,8 +69,6 @@ impl SubLexer for CodeLexer { fn lex_pass(&mut self, reader: &mut Box) -> SubLexerResult { use self::SubLexerResult::*; - // TODO: put all these match results inline - // End of line continuation if let Match(_) = reader.matches(r"\.\.\.") { // Line continuation has no token, it just continues on the next line, ignoring indents (for now). @@ -83,8 +78,7 @@ impl SubLexer for CodeLexer { // The rest of this line is unparsable. if let Match(word) = reader.matches("[^\\n]*\\n\\r?") { // This is a new line, so there may be indents. - return self - .token_and_indents(reader, Tokens::Unlexable(UnlexableToken::new(word))); + return self.token_and_indents(reader, Tokens::Unlexable(UnlexableToken::new(word))); } else { // TODO: I don't know yet how to deal with '...' followed by end-of-file panic!() @@ -95,24 +89,16 @@ impl SubLexer for CodeLexer { if let Match(_) = reader.matches("\\n\\r?") { // Newline WITHOUT line continuation. // This is a new line, so there may be indents. - return self.token_and_indents( - reader, - Tokens::EndStatement(EndStatementToken::new_end_line()), - ); + return self.token_and_indents(reader, Tokens::EndStatement(EndStatementToken::new_end_line())); } // End of statement if let Match(_) = reader.matches(";") { // Semicolon, which ends a statement. if let Match(_) = reader.matches("\\n\\r?") { // If semicolon is followed by a newline, it is redundant. Deal with indents (but ignore the newline itself). - return self.token_and_indents( - reader, - Tokens::EndStatement(EndStatementToken::new_semicolon()), - ); + return self.token_and_indents(reader, Tokens::EndStatement(EndStatementToken::new_semicolon())); } else { - return SubLexerResult::single(Tokens::EndStatement( - EndStatementToken::new_semicolon(), - )); + return SubLexerResult::single(Tokens::EndStatement(EndStatementToken::new_semicolon())); } } // @@ -124,9 +110,7 @@ impl SubLexer for CodeLexer { if let Ok(keyword) = KeywordToken::from_str(word.clone()) { return SubLexerResult::single(Tokens::Keyword(keyword)); } - return SubLexerResult::single(Tokens::Identifier( - IdentifierToken::from_str(word).unwrap(), - )); + return SubLexerResult::single(Tokens::Identifier(IdentifierToken::from_str(word).unwrap())); } // Literal if let Match(_) = reader.matches("[a-z]?\"") { @@ -146,22 +130,16 @@ impl SubLexer for CodeLexer { debug_assert!(token.chars().last().unwrap() == '='); if token.char_len() > 1 { match AssociationToken::from_str(charsliceto(token, -1)) { - Ok(association) => { - return SubLexerResult::single((Tokens::Association(association))) - } + Ok(association) => return SubLexerResult::single(Tokens::Association(association)), Err(msg) => panic!(format!("Invalid association prefix: {}", msg)), } } else { - return SubLexerResult::single( - (Tokens::Association(AssociationToken::from_unprefixed())), - ); + return SubLexerResult::single(Tokens::Association(AssociationToken::from_unprefixed())); } } // Operator (after association) if let Match(token) = reader.matches(OperatorToken::subpattern()) { - return SubLexerResult::single(Tokens::Operator( - OperatorToken::from_str(&token).unwrap(), - )); + return SubLexerResult::single(Tokens::Operator(OperatorToken::from_str(&token).unwrap())); } // Grouping symbols if let Match(_) = reader.matches(r"\(") { @@ -190,3 +168,23 @@ impl SubLexer for CodeLexer { }; } } + +#[cfg(test)] +mod tests { + use mango::lexing::util::test_util::assert_text_to_tokens; + use mango::token::tokens::EndStatementToken; + use mango::token::tokens::KeywordToken; + use mango::token::Tokens; + + #[test] + fn test_lexing_individual() { + assert_text_to_tokens( + "if", + vec![ + Tokens::Keyword(KeywordToken::from_str("if".to_owned()).unwrap()), + Tokens::EndStatement(EndStatementToken::new_end_line()), + ], + ); + // todo: more + } +} diff --git a/src/mango/lexing/combi_lexer.rs b/src/mango/lexing/combi_lexer.rs index f7fcb8f7..09d6a520 100644 --- a/src/mango/lexing/combi_lexer.rs +++ b/src/mango/lexing/combi_lexer.rs @@ -77,19 +77,13 @@ mod tests { use mango::token::tokens::KeywordToken; use mango::token::tokens::LiteralToken; use mango::token::tokens::OperatorToken; - use mango::token::tokens::ParenthesisCloseToken; - use mango::token::tokens::ParenthesisOpenToken; use mango::token::tokens::StartBlockToken; use mango::token::Tokens; use mango::util::encdec::to_text::ToText; - use std::cell::RefCell; - use std::rc::Rc; fn assert_text_to_tokens(text: &str, tokens: Vec) { let expected = LexList::from_tokens(tokens); - let actual = lex_all(&mut CombiLexer::new(Box::new(StringReader::new( - text.to_owned(), - )))); + let actual = lex_all(&mut CombiLexer::new(Box::new(StringReader::new(text.to_owned())))); assert_eq!( expected, actual, @@ -99,18 +93,6 @@ mod tests { ); } - #[test] - fn test_lexing_individual() { - assert_text_to_tokens( - "if", - vec![ - Tokens::Keyword(KeywordToken::from_str("if".to_owned()).unwrap()), - Tokens::EndStatement(EndStatementToken::new_end_line()), - ], - ); - // todo: more - } - #[test] fn test_lexing_combined() { assert_text_to_tokens( diff --git a/src/mango/lexing/string_lexer.rs b/src/mango/lexing/string_lexer.rs index dbc68034..270bced4 100644 --- a/src/mango/lexing/string_lexer.rs +++ b/src/mango/lexing/string_lexer.rs @@ -5,6 +5,7 @@ use mango::lexing::typ::SubLexerResult; use mango::token::tokens::LiteralToken; use mango::token::Tokens; +#[allow(dead_code)] // TODO: TMP pub enum StringType { SingleQuotedInline, DoubleQuotedInline, @@ -13,6 +14,7 @@ pub enum StringType { /// Lexes a string literal token. // Starts after the opening quote and expected to consume until closing quote. +#[allow(dead_code)] // TODO: TMP pub struct StringLexer { typ: StringType, } @@ -32,13 +34,9 @@ impl SubLexer for StringLexer { // TODO: doesn't handle escaping etc at all now // TODO: this is going to have a problem if `matches` automatically eats whitespace match reader.matches("[^\"\\n]*") { - Match(value) => { - return SubLexerResult::single(Tokens::Literal(LiteralToken::string(value))) - } + Match(value) => return SubLexerResult::single(Tokens::Literal(LiteralToken::string(value))), NoMatch() => panic!("failed to parse string"), // This can't really go wrong since empty pattern matches - EOF() => { - return SubLexerResult::single(Tokens::Literal(LiteralToken::string("".to_owned()))) - } // Unclosed string literal, let code parser deal with it + EOF() => return SubLexerResult::single(Tokens::Literal(LiteralToken::string("".to_owned()))), // Unclosed string literal, let code parser deal with it } } } diff --git a/src/mango/lexing/util/lex_all.rs b/src/mango/lexing/util/lex_all.rs index e10557a8..a6aee685 100644 --- a/src/mango/lexing/util/lex_all.rs +++ b/src/mango/lexing/util/lex_all.rs @@ -14,6 +14,7 @@ impl LexList { LexList { tokens } } + #[allow(unused)] pub fn from_reader(lexer: &mut Lexer) -> Self { lex_all(lexer) } @@ -21,11 +22,7 @@ impl LexList { impl ToText for LexList { fn to_text(&self) -> String { - self.tokens - .iter() - .map(|token| token.to_text()) - .collect::>() - .join(" ") + self.tokens.iter().map(|token| token.to_text()).collect::>().join(" ") } } @@ -35,5 +32,5 @@ pub fn lex_all(lexer: &mut Lexer) -> LexList { list.push(token) } list.shrink_to_fit(); - LexList { tokens: list } + LexList::from_tokens(list) } diff --git a/src/mango/lexing/util/mod.rs b/src/mango/lexing/util/mod.rs index 52be7fa1..37351b8a 100644 --- a/src/mango/lexing/util/mod.rs +++ b/src/mango/lexing/util/mod.rs @@ -1 +1,3 @@ pub mod lex_all; + +pub mod test_util; diff --git a/src/mango/lexing/util/test_util.rs b/src/mango/lexing/util/test_util.rs new file mode 100644 index 00000000..aa5c0ece --- /dev/null +++ b/src/mango/lexing/util/test_util.rs @@ -0,0 +1,19 @@ +use mango::io::fortest::stringreader::StringReader; +use mango::lexing::combi_lexer::CombiLexer; +use mango::lexing::util::lex_all::lex_all; +use mango::lexing::util::lex_all::LexList; +use mango::token::Tokens; +use mango::util::encdec::to_text::ToText; + +#[allow(dead_code)] +pub fn assert_text_to_tokens(text: &str, tokens: Vec) { + let expected = LexList::from_tokens(tokens); + let actual = lex_all(&mut CombiLexer::new(Box::new(StringReader::new(text.to_owned())))); + assert_eq!( + expected, + actual, + "\nexpected:\n{}\nactual:\n{}", + expected.to_text(), + actual.to_text(), + ); +} diff --git a/src/mango/token/tokens/association.rs b/src/mango/token/tokens/association.rs index a5be9028..4f086166 100644 --- a/src/mango/token/tokens/association.rs +++ b/src/mango/token/tokens/association.rs @@ -12,9 +12,7 @@ pub struct AssociationToken { impl AssociationToken { pub fn from_unprefixed() -> Self { - AssociationToken { - symbol: Option::None, - } + AssociationToken { symbol: Option::None } } pub fn from_str>(symbol_txt: S) -> Result { diff --git a/src/mango/token/tokens/block.rs b/src/mango/token/tokens/block.rs index 64a3041f..c538cf2a 100644 --- a/src/mango/token/tokens/block.rs +++ b/src/mango/token/tokens/block.rs @@ -20,10 +20,7 @@ impl StartBlockToken { impl EndBlockToken { pub fn new(is_dedent: bool, is_end_keyword: bool) -> Self { assert!(is_dedent || is_end_keyword); - EndBlockToken { - is_dedent, - is_end_keyword, - } + EndBlockToken { is_dedent, is_end_keyword } } } diff --git a/src/mango/towasm/control/block.rs b/src/mango/towasm/control/block.rs index 63ac5a06..770555aa 100644 --- a/src/mango/towasm/control/block.rs +++ b/src/mango/towasm/control/block.rs @@ -64,11 +64,7 @@ impl Block { impl Wasm for Block { fn as_wat(&self) -> String { - format!( - "(block {0:}\n{1:}\n) ;; block {0:}", - self.name.as_wat(), - self.group.as_wat() - ) + format!("(block {0:}\n{1:}\n) ;; block {0:}", self.name.as_wat(), self.group.as_wat()) } fn write_wasm(&self, file: &mut File) -> io::Result<()> { diff --git a/src/mango/towasm/control/repeat.rs b/src/mango/towasm/control/repeat.rs index 3672a981..594d974c 100644 --- a/src/mango/towasm/control/repeat.rs +++ b/src/mango/towasm/control/repeat.rs @@ -34,11 +34,7 @@ impl Loop { impl Wasm for Loop { fn as_wat(&self) -> String { - format!( - "loop {0:}\n{1:}\nend ;; loop {0:}", - self.name.as_wat(), - self.group.as_wat() - ) + format!("loop {0:}\n{1:}\nend ;; loop {0:}", self.name.as_wat(), self.group.as_wat()) } fn write_wasm(&self, file: &mut File) -> io::Result<()> { diff --git a/src/mango/towasm/numeric/arithmetic.rs b/src/mango/towasm/numeric/arithmetic.rs index 3b2fcede..7faac2bd 100644 --- a/src/mango/towasm/numeric/arithmetic.rs +++ b/src/mango/towasm/numeric/arithmetic.rs @@ -13,10 +13,7 @@ pub struct Add { impl Add { pub fn new(left: Box, right: Box) -> Box { assert!(left.typ() == right.typ()); - Box::new(Add { - left: left, - right: right, - }) + Box::new(Add { left: left, right: right }) } pub fn typ(&self) -> &Type { @@ -26,12 +23,7 @@ impl Add { impl Wasm for Add { fn as_wat(&self) -> String { - format!( - "{}\n{}\n{}.add", - self.left.as_wat(), - self.right.as_wat(), - self.typ().as_wat(), - ) + format!("{}\n{}\n{}.add", self.left.as_wat(), self.right.as_wat(), self.typ().as_wat(),) } fn write_wasm(&self, file: &mut File) -> io::Result<()> { @@ -54,10 +46,7 @@ pub struct Mul { impl Mul { pub fn new(left: Box, right: Box) -> Box { assert!(left.typ() == right.typ()); - Box::new(Mul { - left: left, - right: right, - }) + Box::new(Mul { left: left, right: right }) } pub fn typ(&self) -> &Type { @@ -67,12 +56,7 @@ impl Mul { impl Wasm for Mul { fn as_wat(&self) -> String { - format!( - "{}\n{}\n{}.mul", - self.left.as_wat(), - self.right.as_wat(), - self.typ().as_wat(), - ) + format!("{}\n{}\n{}.mul", self.left.as_wat(), self.right.as_wat(), self.typ().as_wat(),) } fn write_wasm(&self, file: &mut File) -> io::Result<()> { diff --git a/src/mango/towasm/numeric/logic.rs b/src/mango/towasm/numeric/logic.rs index eeb5bd51..92fa2d99 100644 --- a/src/mango/towasm/numeric/logic.rs +++ b/src/mango/towasm/numeric/logic.rs @@ -13,21 +13,13 @@ pub struct Gt { impl Gt { pub fn new(left: Box, right: Box) -> Box { assert!(left.typ() == right.typ()); - Box::new(Gt { - left: left, - right: right, - }) + Box::new(Gt { left: left, right: right }) } } impl Wasm for Gt { fn as_wat(&self) -> String { - format!( - "{}\n{}\n{}.gt_s", - self.left.as_wat(), - self.right.as_wat(), - self.typ().as_wat(), - ) + format!("{}\n{}\n{}.gt_s", self.left.as_wat(), self.right.as_wat(), self.typ().as_wat(),) } fn write_wasm(&self, file: &mut File) -> io::Result<()> { @@ -50,10 +42,7 @@ pub struct Lt { impl Lt { pub fn new(left: Box, right: Box) -> Box { assert!(left.typ() == right.typ()); - Box::new(Lt { - left: left, - right: right, - }) + Box::new(Lt { left: left, right: right }) } pub fn typ(&self) -> &Type { @@ -63,12 +52,7 @@ impl Lt { impl Wasm for Lt { fn as_wat(&self) -> String { - format!( - "{}\n{}\n{}.lt_s", - self.left.as_wat(), - self.right.as_wat(), - self.typ().as_wat(), - ) + format!("{}\n{}\n{}.lt_s", self.left.as_wat(), self.right.as_wat(), self.typ().as_wat(),) } fn write_wasm(&self, file: &mut File) -> io::Result<()> { diff --git a/src/mango/towasm/scope/function.rs b/src/mango/towasm/scope/function.rs index 3847c4f9..61c7690c 100644 --- a/src/mango/towasm/scope/function.rs +++ b/src/mango/towasm/scope/function.rs @@ -75,11 +75,7 @@ pub struct FunctionSignature { impl FunctionSignature { pub fn new(name: Rc, parameters: Vec>, results: Vec>) -> Self { assert!(results.len() <= 1); // - FunctionSignature { - name, - parameters, - results, - } + FunctionSignature { name, parameters, results } } } @@ -89,16 +85,8 @@ impl Wasm for FunctionSignature { "func {} (export \"{}\") {} {}", self.name.as_wat(), self.name.pure_name(), - self.parameters - .iter() - .map(|func| func.as_wat()) - .collect::>() - .join("\n"), - self.results - .iter() - .map(|func| func.as_wat()) - .collect::>() - .join("\n") + self.parameters.iter().map(|func| func.as_wat()).collect::>().join("\n"), + self.results.iter().map(|func| func.as_wat()).collect::>().join("\n") ) } @@ -114,12 +102,7 @@ pub struct Function { impl Function { // This uses group, so it has a label, but this isn't final... It might be useless. - pub fn new( - name: Rc, - parameters: Vec>, - results: Vec>, - statements_gen: F, - ) -> Box + pub fn new(name: Rc, parameters: Vec>, results: Vec>, statements_gen: F) -> Box where F: FnOnce(Label) -> Vec>, { diff --git a/src/mango/towasm/scope/module.rs b/src/mango/towasm/scope/module.rs index 95bcc155..c46cbe50 100644 --- a/src/mango/towasm/scope/module.rs +++ b/src/mango/towasm/scope/module.rs @@ -18,11 +18,7 @@ impl Wasm for Module { fn as_wat(&self) -> String { format!( "(module\n{}\n) ;; module", - self.functions - .iter() - .map(|func| func.as_wat()) - .collect::>() - .join("\n") + self.functions.iter().map(|func| func.as_wat()).collect::>().join("\n") ) } diff --git a/src/mango/towasm/tests.rs b/src/mango/towasm/tests.rs index 0bf72f76..f5941bde 100644 --- a/src/mango/towasm/tests.rs +++ b/src/mango/towasm/tests.rs @@ -26,37 +26,23 @@ fn test_example_1() { vec![param_n], vec![Output::new(Type::Int32)], |func_label: Label| { - let fac_result_decl = - DeclareLocal::new(Name::new("fac_result".to_owned()).unwrap(), Type::Int32); + let fac_result_decl = DeclareLocal::new(Name::new("fac_result".to_owned()).unwrap(), Type::Int32); let fac_result = fac_result_decl.local(); - let loop_condition_decl = - DeclareLocal::new(Name::new("loop_condition".to_owned()).unwrap(), Type::Bool); + let loop_condition_decl = DeclareLocal::new(Name::new("loop_condition".to_owned()).unwrap(), Type::Bool); let loop_condition = loop_condition_decl.local(); vec![ // Function body fac_result_decl, loop_condition_decl, Assign::new(fac_result.clone(), Const::new(Type::Int32, Value::Int(1))), - Loop::new_named( - Name::new("fac_loop".to_owned()).unwrap(), - |loop_label: Label| { - vec![ - Assign::new( - fac_result.clone(), - Mul::new(fac_result.get(), var_n.get()), - ), - Assign::new( - loop_condition.clone(), - Gt::new(var_n.get(), Const::new(Type::Int32, Value::Int(2))), - ), - Assign::new( - var_n.clone(), - Add::new(var_n.get(), Const::new(Type::Int32, Value::Int(-1))), - ), - BranchIf::new(loop_condition.get(), loop_label), - ] - }, - ), + Loop::new_named(Name::new("fac_loop".to_owned()).unwrap(), |loop_label: Label| { + vec![ + Assign::new(fac_result.clone(), Mul::new(fac_result.get(), var_n.get())), + Assign::new(loop_condition.clone(), Gt::new(var_n.get(), Const::new(Type::Int32, Value::Int(2)))), + Assign::new(var_n.clone(), Add::new(var_n.get(), Const::new(Type::Int32, Value::Int(-1)))), + BranchIf::new(loop_condition.get(), loop_label), + ] + }), Return::new(func_label, fac_result.get()), ] }, diff --git a/src/mango/towasm/values/assign.rs b/src/mango/towasm/values/assign.rs index 2044fefc..b4fd64bb 100644 --- a/src/mango/towasm/values/assign.rs +++ b/src/mango/towasm/values/assign.rs @@ -18,11 +18,7 @@ impl Assign { impl Wasm for Assign { fn as_wat(&self) -> String { - format!( - "{}\nset_local {}", - self.value.as_wat(), - self.assignee.as_wat() - ) + format!("{}\nset_local {}", self.value.as_wat(), self.assignee.as_wat()) // set_local $fac_result } diff --git a/src/mango/towasm/values/localvar.rs b/src/mango/towasm/values/localvar.rs index 6af061bc..47536f1d 100644 --- a/src/mango/towasm/values/localvar.rs +++ b/src/mango/towasm/values/localvar.rs @@ -40,11 +40,7 @@ impl DeclareLocal { impl Wasm for DeclareLocal { fn as_wat(&self) -> String { - format!( - "(local {} {})", - self.local.name().as_wat(), - self.local.typ().as_wat() - ) + format!("(local {} {})", self.local.name().as_wat(), self.local.typ().as_wat()) } fn write_wasm(&self, file: &mut File) -> io::Result<()> { @@ -69,9 +65,7 @@ pub struct Local { impl Local { pub fn get(&self) -> Box { Box::new(GetLocal { - local: Local { - inner: self.inner.clone(), - }, + local: Local { inner: self.inner.clone() }, }) } diff --git a/src/mango/util/codeparts/keyword.rs b/src/mango/util/codeparts/keyword.rs index f3f176d4..92605849 100644 --- a/src/mango/util/codeparts/keyword.rs +++ b/src/mango/util/codeparts/keyword.rs @@ -166,10 +166,7 @@ impl Keyword { "xor" => Ok(Reserved("xor".to_owned())), "yield" => Ok(Reserved("yield".to_owned())), - _ => Err(Msg::from_valid(&format!( - "Unknown keywords: '{}'", - ssymbol_txt - ))), + _ => Err(Msg::from_valid(&format!("Unknown keywords: '{}'", ssymbol_txt))), } } } diff --git a/src/mango/util/codeparts/operator.rs b/src/mango/util/codeparts/operator.rs index b001bfda..625331ca 100644 --- a/src/mango/util/codeparts/operator.rs +++ b/src/mango/util/codeparts/operator.rs @@ -37,10 +37,7 @@ impl Symbol { ">=" => Ok(GE), "!" => Ok(Exclamation), "?" => Ok(Question), - _ => Err(Msg::from_valid(&format!( - "Unknown symbol: '{}'", - ssymbol_txt - ))), + _ => Err(Msg::from_valid(&format!("Unknown symbol: '{}'", ssymbol_txt))), } } diff --git a/src/mango/util/errors/code_problem.rs b/src/mango/util/errors/code_problem.rs index 6919824a..f28d20a5 100644 --- a/src/mango/util/errors/code_problem.rs +++ b/src/mango/util/errors/code_problem.rs @@ -109,17 +109,8 @@ mod tests { #[test] fn test_new_problem() { - CodeProblem::error( - Msg::copy_new("test problem").unwrap(), - Context::new("test context".to_string()), - ); - CodeProblem::warning( - Msg::copy_new("test problem").unwrap(), - Context::new("test context".to_string()), - ); - CodeProblem::debug( - Msg::copy_new("test problem").unwrap(), - Context::new("test context".to_string()), - ); + CodeProblem::error(Msg::copy_new("test problem").unwrap(), Context::new("test context".to_string())); + CodeProblem::warning(Msg::copy_new("test problem").unwrap(), Context::new("test context".to_string())); + CodeProblem::debug(Msg::copy_new("test problem").unwrap(), Context::new("test context".to_string())); } } diff --git a/src/mango/util/errors/collector.rs b/src/mango/util/errors/collector.rs index 619ed139..86a18828 100644 --- a/src/mango/util/errors/collector.rs +++ b/src/mango/util/errors/collector.rs @@ -49,33 +49,17 @@ mod tests { #[test] fn test_iter_collector() { let mut collector = ProblemCollector::new(); - collector.error( - Msg::copy_new("test problem").unwrap(), - Context::new("test context".to_string()), - ); + collector.error(Msg::copy_new("test problem").unwrap(), Context::new("test context".to_string())); let cnt = collector.into_iter().count(); assert_eq!(1, cnt, "No item in ProblemCollector"); - assert_eq!( - cnt, - collector.into_iter().count(), - "Failed to iterate over ProblemCollector twice" - ) + assert_eq!(cnt, collector.into_iter().count(), "Failed to iterate over ProblemCollector twice") } #[test] fn test_new_problem() { let mut collector = ProblemCollector::new(); - collector.error( - Msg::copy_new("test problem").unwrap(), - Context::new("test context".to_string()), - ); - collector.warning( - Msg::copy_new("test problem").unwrap(), - Context::new("test context".to_string()), - ); - collector.debug( - Msg::copy_new("test problem").unwrap(), - Context::new("test context".to_string()), - ); + collector.error(Msg::copy_new("test problem").unwrap(), Context::new("test context".to_string())); + collector.warning(Msg::copy_new("test problem").unwrap(), Context::new("test context".to_string())); + collector.debug(Msg::copy_new("test problem").unwrap(), Context::new("test context".to_string())); } } diff --git a/src/mango/util/format/strings.rs b/src/mango/util/format/strings.rs index 7161c5a9..bcacf3ee 100644 --- a/src/mango/util/format/strings.rs +++ b/src/mango/util/format/strings.rs @@ -26,10 +26,7 @@ mod tests { assert_eq!("\"hello\\nworld\"", to_double_quoted_str("hello\nworld")); assert_eq!("\"hello\\\\ world\"", to_double_quoted_str("hello\\ world")); assert_eq!("\"hello\\\"world\"", to_double_quoted_str("hello\"world")); - assert_eq!( - "\"\\\"\\\"\\\"\\n\\\\\"", - to_double_quoted_str("\"\"\"\n\\") - ); + assert_eq!("\"\\\"\\\"\\\"\\n\\\\\"", to_double_quoted_str("\"\"\"\n\\")); assert_eq!("\"\\\\n\"", to_double_quoted_str("\\n")); assert_eq!("\"\\\\\\n\"", to_double_quoted_str("\\\n")); } diff --git a/src/mango/util/numtype/eqfloat.rs b/src/mango/util/numtype/eqfloat.rs index df09318d..eb570d45 100644 --- a/src/mango/util/numtype/eqfloat.rs +++ b/src/mango/util/numtype/eqfloat.rs @@ -165,14 +165,8 @@ mod tests { assert_eq!(get_hash(f64eq::new(PI)), get_hash(f64eq::new(PI))); assert_ne!(get_hash(f64eq::new(42.)), get_hash(f64eq::new(-42.))); assert_eq!(get_hash(f64eq::new(0.)), get_hash(f64eq::new(-0.))); - assert_eq!( - get_hash(f64eq::new(INFINITY)), - get_hash(f64eq::new(INFINITY)) - ); - assert_ne!( - get_hash(f64eq::new(INFINITY)), - get_hash(f64eq::new(NEG_INFINITY)) - ); + assert_eq!(get_hash(f64eq::new(INFINITY)), get_hash(f64eq::new(INFINITY))); + assert_ne!(get_hash(f64eq::new(INFINITY)), get_hash(f64eq::new(NEG_INFINITY))); assert_ne!(get_hash(f64eq::new(42.)), get_hash(f64eq::new(NAN))); assert_ne!(get_hash(f64eq::new(NAN)), get_hash(f64eq::new(42.))); assert_eq!(get_hash(f64eq::new(NAN)), get_hash(f64eq::new(NAN))); diff --git a/src/mango/util/parsetxt/int.rs b/src/mango/util/parsetxt/int.rs index 38bdb248..89911ecf 100644 --- a/src/mango/util/parsetxt/int.rs +++ b/src/mango/util/parsetxt/int.rs @@ -18,10 +18,7 @@ pub fn int_pattern() -> &'static str { /// Convert a String that matches [int_pattern] to an i64 integer. Overflow is possible. pub fn parse_int>(text: S) -> Result { let text = text.into(); - match Regex::new(&format!("^{}$", int_pattern())) - .unwrap() - .captures(&text) - { + match Regex::new(&format!("^{}$", int_pattern())).unwrap().captures(&text) { None => return Err(IntParseFailReason::Invalid), Some(captures) => { // // Sign @@ -78,6 +75,7 @@ mod tests { assert_eq!(9, parse_int("09").unwrap()); } + #[test] fn test_invalid_b10_ints() { assert!(parse_int("0x9").is_err()); assert!(parse_int("A").is_err()); @@ -86,6 +84,7 @@ mod tests { // TODO: over/underflow } + #[test] fn test_parse_based_ints() { // TODO: not implemented yet } diff --git a/src/mango/util/parsetxt/real.rs b/src/mango/util/parsetxt/real.rs index b99925ff..20e805c7 100644 --- a/src/mango/util/parsetxt/real.rs +++ b/src/mango/util/parsetxt/real.rs @@ -23,10 +23,7 @@ pub fn real_pattern() -> &'static str { /// Convert a String that matches [real_pattern] to an f64 real. Overflow and loss of precision is possible. pub fn parse_real>(text: S) -> Result { let text = text.into(); - match Regex::new(&format!("^{}$", real_pattern())) - .unwrap() - .captures(&text) - { + match Regex::new(&format!("^{}$", real_pattern())).unwrap().captures(&text) { None => return Err(RealParseFailReason::Invalid), Some(captures) => { let multiplier = captures @@ -43,11 +40,7 @@ pub fn parse_real>(text: S) -> Result } Some(exponent_match) => { // This real is in exponential notation - let exponent = exponent_match - .as_str() - .without_char(&'_') - .parse::() - .unwrap(); + let exponent = exponent_match.as_str().without_char(&'_').parse::().unwrap(); // TODO: is there a numerically smarter way to do this? return Ok(10f64.powf(exponent) * multiplier); } @@ -80,17 +73,11 @@ mod tests { assert!(close(-0.1, parse_real("-.1e0").unwrap())); assert!(close(-1., parse_real("-1.e0").unwrap())); assert!(close(42., parse_real("42.0e+0").unwrap())); - assert!(close( - 12345.6789, - parse_real("1_2_3_4_5.6_7_8_9e0").unwrap() - )); + assert!(close(12345.6789, parse_real("1_2_3_4_5.6_7_8_9e0").unwrap())); assert!(close(0.42, parse_real("42.0e-2").unwrap())); assert!(close(-0.001, parse_real("-.1e-2").unwrap())); assert!(close(-0.01, parse_real("-1.e-2").unwrap())); - assert!(close( - 123.456789, - parse_real("1_2_3_4_5.6_7_8_9e-2").unwrap() - )); + assert!(close(123.456789, parse_real("1_2_3_4_5.6_7_8_9e-2").unwrap())); assert!(close(42.0, parse_real("42.0e-0_0_0").unwrap())); } diff --git a/src/mango/util/strslice/slice.rs b/src/mango/util/strslice/slice.rs index 60ab23a8..35fb70da 100644 --- a/src/mango/util/strslice/slice.rs +++ b/src/mango/util/strslice/slice.rs @@ -23,10 +23,7 @@ pub fn charslice>(text: S, start: isize, end: isize) -> String { "charslice: if 'end' is negative, the magnitude may not exceed the length" ); let new_end = (charcount as isize + end) as usize; - assert!( - new_end >= from, - "charslice: 'start' may not be before 'end' (end was negative)" - ); + assert!(new_end >= from, "charslice: 'start' may not be before 'end' (end was negative)"); length = new_end - from; } else { assert!( diff --git a/src/mango/util/strtype/msg.rs b/src/mango/util/strtype/msg.rs index 4f20b0ae..d2e2f28b 100644 --- a/src/mango/util/strtype/msg.rs +++ b/src/mango/util/strtype/msg.rs @@ -34,9 +34,7 @@ impl StrType for Msg { fn validate(msg: &str) -> Result<(), Msg> { if !VALID_MESSAGE.is_match(&msg.to_string()) { // Make sure this is a valid string, otherwise it causes an infinite loop making error messages for it! - return Err(Msg::from_valid( - "Messages should consist of printable text.", - )); + return Err(Msg::from_valid("Messages should consist of printable text.")); } Ok(()) } diff --git a/src/mango/util/strtype/name.rs b/src/mango/util/strtype/name.rs index 5940158a..23533c4c 100644 --- a/src/mango/util/strtype/name.rs +++ b/src/mango/util/strtype/name.rs @@ -8,8 +8,7 @@ use string_interner::StringInterner; const VALID_IDENTIFIER_SUBPATTERN: &'static str = r"[a-zA-Z_][a-zA-Z0-9_]*"; lazy_static! { - static ref VALID_IDENTIFIER: Regex = - Regex::new(&format!("{}{}{}", r"^", VALID_IDENTIFIER_SUBPATTERN, r"$")).unwrap(); + static ref VALID_IDENTIFIER: Regex = Regex::new(&format!("{}{}{}", r"^", VALID_IDENTIFIER_SUBPATTERN, r"$")).unwrap(); } // TODO: this alias just for https://github.com/rust-lang-nursery/rustfmt/issues/2610 @@ -33,12 +32,7 @@ impl Name { pub fn value(&self) -> String { // Unwrap only fails if another thread panicked while locking, which shouldn't happen. // todo: I want this to return &str but that'd need the interner to be borrowed longer - INTERNER - .lock() - .unwrap() - .resolve(self.name_id) - .unwrap() - .to_string() + INTERNER.lock().unwrap().resolve(self.name_id).unwrap().to_string() } /// Generate an eager subpattern to match names, that can be composed in a regular expression. @@ -50,11 +44,7 @@ impl Name { impl fmt::Display for Name { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Use interner directly instead of .value(), because that creates a copy - write!( - f, - "{}", - INTERNER.lock().unwrap().resolve(self.name_id).unwrap() - ) + write!(f, "{}", INTERNER.lock().unwrap().resolve(self.name_id).unwrap()) } } @@ -73,9 +63,7 @@ impl StrType for Name { fn validate(name: &str) -> Result<(), Msg> { match name.chars().next() { Some(chr) => if chr.is_digit(10) { - return Err(Msg::from_valid( - "Identifier names may not start with a digit.", - )); + return Err(Msg::from_valid("Identifier names may not start with a digit.")); }, None => return Ok(()), // empty string } @@ -169,13 +157,7 @@ mod tests { #[test] fn test_name_interning() { - assert_eq!( - Name::copy_new("Hello").unwrap(), - Name::copy_new("Hello").unwrap() - ); - assert_ne!( - Name::copy_new("Hello").unwrap(), - Name::copy_new("Goodbye").unwrap() - ); + assert_eq!(Name::copy_new("Hello").unwrap(), Name::copy_new("Hello").unwrap()); + assert_ne!(Name::copy_new("Hello").unwrap(), Name::copy_new("Goodbye").unwrap()); } }