From 0ed0d077148718c037181570355919081f0160df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 14 Feb 2011 20:43:26 +0100 Subject: [PATCH] Tokenizer::simplifyCalculations: basic handling of bitwise operators --- lib/tokenize.cpp | 32 ++++++++++++++++++++++++++++---- test/testsimplifytokens.cpp | 2 ++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9398867d5..be64337b5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7071,10 +7071,11 @@ bool Tokenizer::simplifyCalculations() { // (1-2) - while (Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% [],);=<>+-*/]") || - Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/]") || - Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% <<|>>") || - Token::Match(tok, "<< %num% [+-*/] %num% <<")) + while (Token::Match(tok, "[[,(=<>+-*|&^] %num% [+-*/] %num% [],);=<>+-*/|&^]") || + Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/|&^]") || + Token::Match(tok, "[[,(=<>+-*|&^] %num% [+-*/] %num% <<|>>") || + Token::Match(tok, "<< %num% [+-*/] %num% <<") || + Token::Match(tok, "[(,[] %num% [|&^] %num% [];,);]")) { tok = tok->next(); @@ -7082,6 +7083,29 @@ bool Tokenizer::simplifyCalculations() if (Token::simpleMatch(tok->next(), "/ 0")) continue; + // & | ^ + if (Token::Match(tok->next(), "[&|^]")) + { + std::string result; + const std::string first(tok->str()); + const std::string second(tok->strAt(2)); + const char op = tok->next()->str()[0]; + if (op == '&') + result = MathLib::toString(MathLib::toLongNumber(first) & MathLib::toLongNumber(second)); + else if (op == '|') + result = MathLib::toString(MathLib::toLongNumber(first) | MathLib::toLongNumber(second)); + else if (op == '^') + result = MathLib::toString(MathLib::toLongNumber(first) ^ MathLib::toLongNumber(second)); + + if (!result.empty()) + { + ret = true; + tok->str(result); + Token::eraseTokens(tok, tok->tokAt(3)); + continue; + } + } + // + and - are calculated after * and / if (Token::Match(tok->next(), "[+-/]")) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index ac191e618..a4c507cd4 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -2591,6 +2591,8 @@ private: ASSERT_EQUALS("a [ 0 ]", tok(code)); } + ASSERT_EQUALS("a [ 4 ] ;", tok("a[1+3|4];")); + ASSERT_EQUALS("x = 1 + 2 * y ;", tok("x=1+2*y;")); ASSERT_EQUALS("x = 7 ;", tok("x=1+2*3;")); ASSERT_EQUALS("x = 47185 ;", tok("x=(65536*72/100);"));