Fixed #622 (Tokenizer: Calculations are wrong)

This commit is contained in:
danmar 2009-08-28 12:13:46 +02:00
parent 465c450247
commit bb2bda0be4
3 changed files with 16 additions and 1 deletions

View File

@ -3100,6 +3100,15 @@ bool Tokenizer::simplifyCalculations()
if (Token::simpleMatch(tok->next(), "/ 0")) if (Token::simpleMatch(tok->next(), "/ 0"))
continue; continue;
// + and - are calculated after *
if (Token::Match(tok->next(),"[+-]"))
{
if (tok->previous()->str() == "*")
continue;
if (Token::simpleMatch(tok->tokAt(3), "*"))
continue;
}
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), *(tok->strAt(1)))); tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), *(tok->strAt(1))));
Token::eraseTokens(tok, tok->tokAt(3)); Token::eraseTokens(tok, tok->tokAt(3));

View File

@ -1204,6 +1204,9 @@ private:
const char code[] = "a[10+10-10-10]"; const char code[] = "a[10+10-10-10]";
ASSERT_EQUALS("a [ 0 ]", tok(code)); ASSERT_EQUALS("a [ 0 ]", tok(code));
} }
ASSERT_EQUALS("x = 1 + 2 * y ;", tok("x=1+2*y;"));
ASSERT_EQUALS("x = 7 ;", tok("x=1+2*3;"));
} }

View File

@ -17,7 +17,8 @@
*/ */
#ifndef testsuiteH
#define testsuiteH
#include <sstream> #include <sstream>
#include "../src/errorlogger.h" #include "../src/errorlogger.h"
@ -63,3 +64,5 @@ public:
#define TODO_ASSERT_EQUALS( EXPECTED , ACTUAL ) if (EXPECTED==ACTUAL) assertEquals(__FILE__, __LINE__, "TODO assertion", "The assertion succeeded") #define TODO_ASSERT_EQUALS( EXPECTED , ACTUAL ) if (EXPECTED==ACTUAL) assertEquals(__FILE__, __LINE__, "TODO assertion", "The assertion succeeded")
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance; } #define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance; }
#endif