From 0b84213eb65534736bf639d3ca75a180e53aa9a6 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sat, 22 Dec 2012 11:45:15 +0200 Subject: [PATCH] Calc: Fixed handling of negative numbers without brackets "5+-1" is now valid and equal to "5+(-1)". --- Library/MathParser.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Library/MathParser.cpp b/Library/MathParser.cpp index 9be263c7..99f12bf1 100644 --- a/Library/MathParser.cpp +++ b/Library/MathParser.cpp @@ -67,6 +67,7 @@ enum CharType CH_DIGIT = 0x02, CH_SEPARAT = 0x04, CH_SYMBOL = 0x08, + CH_MINUS = 0x10, CH_FINAL = 0x7F }; @@ -598,7 +599,13 @@ MathTokenType GetNextToken(Lexer& lexer) { lexer.charType = GetCharType(*++lexer.string); } - + + if (lexer.charType == CH_MINUS) + { + // If the - sign follows a symbol, it is treated as a (negative) number. + lexer.charType = (lexer.prevToken == TOK_SYMBOL) ? CH_DIGIT : CH_SYMBOL; + } + switch (lexer.charType) { case CH_FINAL: @@ -705,8 +712,10 @@ CharType GetCharType(WCHAR ch) case L'_': return CH_LETTER; - case L'+': case L'-': + return CH_MINUS; + + case L'+': case L'/': case L'*': case L'~':