Cosmetics

This commit is contained in:
Birunthan Mohanathas 2012-12-23 15:46:42 +02:00
parent 24ef81dee8
commit 536bde48d1

View File

@ -77,8 +77,8 @@ enum class Token
Error, Error,
None, None,
Final, Final,
Float, Operator,
Symbol, Number,
Name Name
}; };
@ -176,7 +176,7 @@ static Operator GetOperator(const WCHAR* str);
struct Parser struct Parser
{ {
Operation opStack[96]; Operation opStack[96];
double valStack[64]; double numStack[64];
char opTop; char opTop;
char valTop; char valTop;
int obrDist; int obrDist;
@ -192,12 +192,17 @@ struct Lexer
const WCHAR* string; const WCHAR* string;
const WCHAR* name; const WCHAR* name;
size_t nameLen; size_t nameLen;
double extValue;
int intValue; Token token;
Token prevToken; union
{
Operator oper; // token == Token::Operator
double num; // token == Token::Number
} value;
CharType charType; CharType charType;
Lexer(const WCHAR* str) : string(str), name(), nameLen(), extValue(), intValue(), prevToken(Token::None), charType(GetCharType(*str)) {} Lexer(const WCHAR* str) : string(str), name(), nameLen(), value(), token(Token::None), charType(GetCharType(*str)) {}
}; };
static Token GetNextToken(Lexer& lexer); static Token GetNextToken(Lexer& lexer);
@ -259,7 +264,7 @@ const WCHAR* MathParser::Parse(const WCHAR* formula, CMeasureCalc* calc, double*
for (;;) for (;;)
{ {
if ((parser.opTop == _countof(parser.opStack) - 2) || if ((parser.opTop == _countof(parser.opStack) - 2) ||
(parser.valTop == _countof(parser.valStack) - 2)) (parser.valTop == _countof(parser.numStack) - 2))
{ {
return eInternal; return eInternal;
} }
@ -283,17 +288,17 @@ const WCHAR* MathParser::Parse(const WCHAR* formula, CMeasureCalc* calc, double*
else else
{ {
// Done! // Done!
*result = parser.valStack[0]; *result = parser.numStack[0];
return NULL; return NULL;
} }
break; break;
case Token::Float: case Token::Number:
parser.valStack[++parser.valTop] = lexer.extValue; parser.numStack[++parser.valTop] = lexer.value.num;
break; break;
case Token::Symbol: case Token::Operator:
switch (lexer.intValue) switch (lexer.value.oper)
{ {
case Operator::OpeningBracket: case Operator::OpeningBracket:
{ {
@ -327,7 +332,7 @@ const WCHAR* MathParser::Parse(const WCHAR* formula, CMeasureCalc* calc, double*
default: default:
{ {
Operation op; Operation op;
op.type = (Operator)lexer.intValue; op.type = lexer.value.oper;
switch (op.type) switch (op.type)
{ {
case Operator::Addition: case Operator::Addition:
@ -373,11 +378,11 @@ const WCHAR* MathParser::Parse(const WCHAR* formula, CMeasureCalc* calc, double*
switch (op.funcIndex) switch (op.funcIndex)
{ {
case FUNC_E: case FUNC_E:
parser.valStack[++parser.valTop] = M_E; parser.numStack[++parser.valTop] = M_E;
break; break;
case FUNC_PI: case FUNC_PI:
parser.valStack[++parser.valTop] = M_PI; parser.numStack[++parser.valTop] = M_PI;
break; break;
case FUNC_ROUND: case FUNC_ROUND:
@ -397,7 +402,7 @@ const WCHAR* MathParser::Parse(const WCHAR* formula, CMeasureCalc* calc, double*
double dblval; double dblval;
if (calc && calc->GetMeasureValue(lexer.name, lexer.nameLen, &dblval)) if (calc && calc->GetMeasureValue(lexer.name, lexer.nameLen, &dblval))
{ {
parser.valStack[++parser.valTop] = dblval; parser.numStack[++parser.valTop] = dblval;
break; break;
} }
@ -429,10 +434,10 @@ static const WCHAR* Calc(Parser& parser)
int paramcnt = parser.valTop - op.prevTop; int paramcnt = parser.valTop - op.prevTop;
parser.valTop = op.prevTop; parser.valTop = op.prevTop;
const WCHAR* error = (*(MultiArgFunction)g_Functions[op.funcIndex].proc)(paramcnt, &parser.valStack[parser.valTop + 1], &res); const WCHAR* error = (*(MultiArgFunction)g_Functions[op.funcIndex].proc)(paramcnt, &parser.numStack[parser.valTop + 1], &res);
if (error) return error; if (error) return error;
parser.valStack[++parser.valTop] = res; parser.numStack[++parser.valTop] = res;
return NULL; return NULL;
} }
else if (parser.valTop < 0) else if (parser.valTop < 0)
@ -441,7 +446,7 @@ static const WCHAR* Calc(Parser& parser)
} }
// Right arg // Right arg
double right = parser.valStack[parser.valTop--]; double right = parser.numStack[parser.valTop--];
// One arg operations // One arg operations
if (op.type == Operator::BitwiseNOT) if (op.type == Operator::BitwiseNOT)
@ -460,7 +465,7 @@ static const WCHAR* Calc(Parser& parser)
} }
// Left arg // Left arg
double left = parser.valStack[parser.valTop--]; double left = parser.numStack[parser.valTop--];
switch (op.type) switch (op.type)
{ {
case Operator::ShiftLeft: case Operator::ShiftLeft:
@ -568,7 +573,7 @@ static const WCHAR* Calc(Parser& parser)
{ {
return eLogicErr; return eLogicErr;
} }
res = parser.valStack[parser.valTop--] ? left : right; res = parser.numStack[parser.valTop--] ? left : right;
} }
break; break;
@ -577,7 +582,7 @@ static const WCHAR* Calc(Parser& parser)
} }
} }
parser.valStack[++parser.valTop] = res; parser.numStack[++parser.valTop] = res;
return NULL; return NULL;
} }
@ -594,8 +599,6 @@ static const WCHAR* CalcToObr(Parser& parser)
Token GetNextToken(Lexer& lexer) Token GetNextToken(Lexer& lexer)
{ {
Token result = Token::Error;
while (lexer.charType == CharType::Separator) while (lexer.charType == CharType::Separator)
{ {
lexer.charType = GetCharType(*++lexer.string); lexer.charType = GetCharType(*++lexer.string);
@ -604,28 +607,27 @@ Token GetNextToken(Lexer& lexer)
if (lexer.charType == CharType::MinusSymbol) if (lexer.charType == CharType::MinusSymbol)
{ {
// If the - sign follows a symbol, it is treated as a (negative) number. // If the - sign follows a symbol, it is treated as a (negative) number.
lexer.charType = (lexer.prevToken == Token::Symbol) ? CharType::Digit : CharType::Symbol; lexer.charType = (lexer.token == Token::Operator) ? CharType::Digit : CharType::Symbol;
} }
switch (lexer.charType) switch (lexer.charType)
{ {
case CharType::Final: case CharType::Final:
{ {
result = Token::Final; lexer.token = Token::Final;
} }
break; break;
case CharType::Letter: case CharType::Letter:
{ {
lexer.token = Token::Name;
lexer.name = lexer.string; lexer.name = lexer.string;
lexer.nameLen = lexer.string - lexer.name;
do do
{ {
lexer.charType = GetCharType(*++lexer.string); lexer.charType = GetCharType(*++lexer.string);
} }
while (lexer.charType <= CharType::Digit); while (lexer.charType <= CharType::Digit);
lexer.nameLen = lexer.string - lexer.name;
result = Token::Name;
} }
break; break;
@ -635,18 +637,19 @@ Token GetNextToken(Lexer& lexer)
if (lexer.string[0] == L'0') if (lexer.string[0] == L'0')
{ {
bool valid = true; bool valid = true;
int num = 0;
switch (lexer.string[1]) switch (lexer.string[1])
{ {
case L'x': // Hexadecimal case L'x': // Hexadecimal
lexer.intValue = wcstol(lexer.string, &newString, 16); num = wcstol(lexer.string, &newString, 16);
break; break;
case L'o': // Octal case L'o': // Octal
lexer.intValue = wcstol(lexer.string + 2, &newString, 8); num = wcstol(lexer.string + 2, &newString, 8);
break; break;
case L'b': // Binary case L'b': // Binary
lexer.intValue = wcstol(lexer.string + 2, &newString, 2); num = wcstol(lexer.string + 2, &newString, 2);
break; break;
default: default:
@ -658,22 +661,23 @@ Token GetNextToken(Lexer& lexer)
{ {
if (lexer.string != newString) if (lexer.string != newString)
{ {
lexer.token = Token::Number;
lexer.value.num = num;
lexer.string = newString; lexer.string = newString;
lexer.charType = GetCharType(*lexer.string); lexer.charType = GetCharType(*lexer.string);
lexer.extValue = lexer.intValue;
result = Token::Float;
} }
break; break;
} }
} }
// Decimal // Decimal
lexer.extValue = wcstod(lexer.string, &newString); double num = wcstod(lexer.string, &newString);
if (lexer.string != newString) if (lexer.string != newString)
{ {
lexer.token = Token::Number;
lexer.value.num = num;
lexer.string = newString; lexer.string = newString;
lexer.charType = GetCharType(*lexer.string); lexer.charType = GetCharType(*lexer.string);
result = Token::Float;
} }
} }
break; break;
@ -683,19 +687,20 @@ Token GetNextToken(Lexer& lexer)
Operator oper = GetOperator(lexer.string); Operator oper = GetOperator(lexer.string);
if (oper != Operator::Invalid) if (oper != Operator::Invalid)
{ {
lexer.token = Token::Operator;
lexer.value.oper = oper;
lexer.string += ((int)oper <= (int)Operator::LogicalOR) ? 2 : 1; lexer.string += ((int)oper <= (int)Operator::LogicalOR) ? 2 : 1;
lexer.charType = GetCharType(*lexer.string); lexer.charType = GetCharType(*lexer.string);
lexer.intValue = (int)oper;
result = Token::Symbol;
} }
} }
break; break;
default: default:
lexer.token = Token::Error;
break; break;
} }
return lexer.prevToken = result;
return lexer.token;
} }
CharType GetCharType(WCHAR ch) CharType GetCharType(WCHAR ch)