rainmeter-studio/Common/MathParser.cpp

908 lines
18 KiB
C++
Raw Normal View History

/*
Copyright (C) 2011 Birunthan Mohanathas
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// Heavily based on ccalc 0.5.1 by Walery Studennikov <hqsoftware@mail.ru>
2013-06-13 14:00:24 +00:00
#include <string>
#include <stdint.h>
#include "MathParser.h"
static const double M_E = 2.7182818284590452354;
static const double M_PI = 3.14159265358979323846;
2012-12-22 10:23:36 +00:00
typedef double (*SingleArgFunction)(double arg);
typedef const WCHAR* (*MultiArgFunction)(int paramcnt, double* args, double* result);
2012-12-22 10:23:36 +00:00
enum class Operator : uint8_t
{
2012-12-22 10:23:36 +00:00
ShiftLeft,
ShiftRight,
Power,
NotEqual,
GreatorOrEqual,
LessOrEqual,
LogicalAND,
LogicalOR,
OpeningBracket,
Addition,
Subtraction,
Multiplication,
Division,
Modulo,
UNK,
BitwiseXOR,
BitwiseNOT,
BitwiseAND,
BitwiseOR,
Equal,
Greater,
Less,
Conditional,
ConditionalSeparator,
ClosingBracket,
Comma,
SingleArgFunction,
MultiArgFunction,
Invalid // Must be last.
2012-01-23 09:30:51 +00:00
};
2012-12-22 10:23:36 +00:00
enum class CharType
{
2012-12-22 10:23:36 +00:00
Unknown = 0x00,
Letter = 0x01,
Digit = 0x02,
Separator = 0x04,
Symbol = 0x08,
MinusSymbol = 0x10,
Final = 0x7F
2012-01-23 09:30:51 +00:00
};
2012-12-22 10:23:36 +00:00
enum class Token
{
2012-12-22 10:23:36 +00:00
Error,
None,
Final,
2012-12-23 13:46:42 +00:00
Operator,
Number,
2012-12-22 10:23:36 +00:00
Name
2012-01-23 09:30:51 +00:00
};
2012-01-23 09:30:51 +00:00
struct Operation
{
2012-12-22 10:23:36 +00:00
Operator type;
BYTE funcIndex;
char prevTop;
2012-01-23 09:30:51 +00:00
};
2012-01-23 09:30:51 +00:00
struct Function
{
2012-01-23 09:30:51 +00:00
WCHAR* name;
2012-12-22 10:23:36 +00:00
SingleArgFunction proc;
2012-01-23 09:30:51 +00:00
BYTE length;
};
static double frac(double x);
static double trunc2(double x);
static double rad(double deg);
static double sgn(double x);
static double neg(double x);
2012-02-20 14:49:38 +00:00
static const WCHAR* round(int paramcnt, double* args, double* result);
static Function g_Functions[] =
{
2012-01-23 09:30:51 +00:00
{ L"atan", &atan, 4 },
{ L"cos", &cos, 3 },
{ L"sin", &sin, 3 },
{ L"tan", &tan, 3 },
{ L"abs", &fabs, 3 },
{ L"exp", &exp, 3 },
{ L"ln", &log, 2 },
{ L"log", &log10, 3 },
{ L"sqrt", &sqrt, 4 },
{ L"frac", &frac, 4 },
{ L"trunc", &trunc2, 5 },
2012-01-23 09:30:51 +00:00
{ L"floor", &floor, 5 },
{ L"ceil", &ceil, 4 },
2012-12-22 10:23:36 +00:00
{ L"round", (SingleArgFunction)&round, 5 },
2012-01-23 09:30:51 +00:00
{ L"asin", &asin, 4 },
{ L"acos", &acos, 4 },
{ L"rad", &rad, 3 },
{ L"sgn", &sgn, 3 },
{ L"neg", &neg, 3 },
2013-05-31 14:28:39 +00:00
{ L"e", nullptr, 1 },
{ L"pi", nullptr, 2 }
};
2012-01-23 09:30:51 +00:00
static const int FUNC_MAX_LEN = 5;
static const int FUNC_ROUND = 13;
static const int FUNC_E = 19;
static const int FUNC_PI = 20;
static const BYTE FUNC_INVALID = UCHAR_MAX;
2012-12-22 10:23:36 +00:00
static const Operation g_BrOp = { Operator::OpeningBracket, 0, 0};
static const Operation g_NegOp = { Operator::SingleArgFunction, 18, 0 };
2012-12-22 10:23:36 +00:00
static const BYTE g_OpPriorities[Operator::Invalid] =
{
2012-12-22 10:23:36 +00:00
5, // Operator::ShiftLeft
5, // Operator::ShiftRight
5, // Operator::Power
2, // Operator::NotEqual
2, // Operator::GreatorOrEqual
2, // Operator::LessOrEqual
2, // Operator::LogicalAND
2, // Operator::LogicalOR
0, // Operator::OpeningBracket
3, // Operator::Addition
3, // Operator::Subtraction
4, // Operator::Multiplication
4, // Operator::Division
4, // Operator::Modulo
4, // Operator::UNK
5, // Operator::BitwiseXOR
5, // Operator::BitwiseNOT
5, // Operator::BitwiseAND
5, // Operator::BitwiseOR
2, // Operator::Equal
2, // Operator::Greater
2, // Operator::Less
1, // Operator::Conditional
2, // Operator::ConditionalSeparator
0, // Operator::ClosingBracket
2, // Operator::Comma
6, // Operator::SingleArgFunction
6 // Operator::MultiArgFunction
};
2012-01-23 09:30:51 +00:00
static CharType GetCharType(WCHAR ch);
static BYTE GetFunctionIndex(const WCHAR* str, BYTE len);
2012-12-22 10:23:36 +00:00
static Operator GetOperator(const WCHAR* str);
2012-01-23 09:30:51 +00:00
struct Parser
{
Operation opStack[96];
2012-12-23 13:46:42 +00:00
double numStack[64];
char opTop;
char valTop;
2012-01-23 09:30:51 +00:00
int obrDist;
2012-12-22 10:23:36 +00:00
Parser() : opTop(0), valTop(-1), obrDist(2) { opStack[0].type = Operator::OpeningBracket; }
2012-01-23 09:30:51 +00:00
};
2012-02-20 14:49:38 +00:00
static const WCHAR* CalcToObr(Parser& parser);
static const WCHAR* Calc(Parser& parser);
2012-01-23 09:30:51 +00:00
struct Lexer
{
const WCHAR* string;
const WCHAR* name;
size_t nameLen;
2012-12-23 13:46:42 +00:00
Token token;
union
{
Operator oper; // token == Token::Operator
double num; // token == Token::Number
} value;
2012-01-23 09:30:51 +00:00
CharType charType;
2012-12-23 13:46:42 +00:00
Lexer(const WCHAR* str) : string(str), name(), nameLen(), value(), token(Token::None), charType(GetCharType(*str)) {}
2012-01-23 09:30:51 +00:00
};
2012-12-22 10:23:36 +00:00
static Token GetNextToken(Lexer& lexer);
2012-01-23 09:30:51 +00:00
2012-02-20 14:49:38 +00:00
const WCHAR* eBrackets = L"Unmatched brackets";
const WCHAR* eSyntax = L"Syntax error";
const WCHAR* eInternal = L"Internal error";
const WCHAR* eExtraOp = L"Extra operation";
const WCHAR* eInfinity = L"Division by 0";
const WCHAR* eUnknFunc = L"\"%s\" is unknown";
const WCHAR* eLogicErr = L"Logical expression error";
const WCHAR* eInvPrmCnt = L"Invalid function parameter count";
2012-01-23 09:30:51 +00:00
2012-02-20 14:49:38 +00:00
const WCHAR* MathParser::Check(const WCHAR* formula)
{
int brackets = 0;
// Brackets Matching
while (*formula)
{
2012-01-23 09:30:51 +00:00
if (*formula == L'(')
{
++brackets;
}
2012-01-23 09:30:51 +00:00
else if (*formula == L')')
{
--brackets;
}
++formula;
}
2013-05-31 14:28:39 +00:00
return (brackets != 0) ? eBrackets : nullptr;
}
2012-02-20 14:49:38 +00:00
const WCHAR* MathParser::CheckedParse(const WCHAR* formula, double* result)
{
2012-02-20 14:49:38 +00:00
const WCHAR* error = Check(formula);
2012-01-23 09:30:51 +00:00
if (!error)
{
2013-06-13 14:00:24 +00:00
error = Parse(formula, result);
2012-01-23 09:30:51 +00:00
}
return error;
}
2013-06-13 14:00:24 +00:00
const WCHAR* MathParser::Parse(
const WCHAR* formula, double* result, GetValueFunc getValue, void* getValueContext)
{
2012-01-23 15:43:31 +00:00
static WCHAR errorBuffer[128];
2012-01-23 09:30:51 +00:00
if (!*formula)
{
*result = 0.0;
2013-05-31 14:28:39 +00:00
return nullptr;
}
2012-01-23 09:30:51 +00:00
Parser parser;
Lexer lexer(formula);
2012-02-20 14:49:38 +00:00
const WCHAR* error;
for (;;)
{
if ((parser.opTop == _countof(parser.opStack) - 2) ||
2012-12-23 13:46:42 +00:00
(parser.valTop == _countof(parser.numStack) - 2))
{
return eInternal;
}
2012-12-22 10:23:36 +00:00
Token token = GetNextToken(lexer);
2012-01-23 09:30:51 +00:00
--parser.obrDist;
switch (token)
{
2012-12-22 10:23:36 +00:00
case Token::Error:
return eSyntax;
2012-12-22 10:23:36 +00:00
case Token::Final:
2013-05-31 14:28:39 +00:00
if ((error = CalcToObr(parser)) != nullptr)
2012-01-23 09:30:51 +00:00
{
return error;
2012-01-23 09:30:51 +00:00
}
else if (parser.opTop != -1 || parser.valTop != 0)
{
return eInternal;
}
else
{
// Done!
2012-12-23 13:46:42 +00:00
*result = parser.numStack[0];
2013-05-31 14:28:39 +00:00
return nullptr;
2012-01-23 09:30:51 +00:00
}
break;
2012-12-23 13:46:42 +00:00
case Token::Number:
parser.numStack[++parser.valTop] = lexer.value.num;
break;
2012-12-23 13:46:42 +00:00
case Token::Operator:
switch (lexer.value.oper)
{
2012-12-22 10:23:36 +00:00
case Operator::OpeningBracket:
{
2012-01-23 09:30:51 +00:00
parser.opStack[++parser.opTop] = g_BrOp;
parser.obrDist = 2;
}
break;
2012-12-22 10:23:36 +00:00
case Operator::ClosingBracket:
{
2013-05-31 14:28:39 +00:00
if ((error = CalcToObr(parser)) != nullptr) return error;
}
break;
2012-12-22 10:23:36 +00:00
case Operator::Comma:
2012-12-23 13:46:42 +00:00
{
2013-05-31 14:28:39 +00:00
if ((error = CalcToObr(parser)) != nullptr) return error;
2012-12-22 10:23:36 +00:00
if (parser.opStack[parser.opTop].type == Operator::MultiArgFunction)
{
2012-01-23 09:30:51 +00:00
parser.opStack[++parser.opTop] = g_BrOp;
parser.obrDist = 2;
}
else
{
return eSyntax;
}
}
break;
default:
{
Operation op;
2012-12-23 13:46:42 +00:00
op.type = lexer.value.oper;
switch (op.type)
{
2012-12-22 10:23:36 +00:00
case Operator::Addition:
2012-01-23 09:30:51 +00:00
if (parser.obrDist >= 1)
{
// Goto next token
continue;
}
break;
2012-12-22 10:23:36 +00:00
case Operator::Subtraction:
2012-01-23 09:30:51 +00:00
if (parser.obrDist >= 1)
{
2012-01-23 09:30:51 +00:00
parser.opStack[++parser.opTop] = g_NegOp;
// Goto next token
continue;
}
break;
2012-12-22 10:23:36 +00:00
case Operator::Conditional:
case Operator::ConditionalSeparator:
2012-01-23 09:30:51 +00:00
parser.obrDist = 2;
break;
}
2012-12-22 10:23:36 +00:00
while (g_OpPriorities[(int)op.type] <= g_OpPriorities[(int)parser.opStack[parser.opTop].type])
{
2013-05-31 14:28:39 +00:00
if ((error = Calc(parser)) != nullptr) return error;
}
2012-01-23 09:30:51 +00:00
parser.opStack[++parser.opTop] = op;
}
break;
}
break;
2012-12-22 10:23:36 +00:00
case Token::Name:
{
Operation op;
2012-01-23 09:30:51 +00:00
if (lexer.nameLen <= FUNC_MAX_LEN &&
((op.funcIndex = GetFunctionIndex(lexer.name, lexer.nameLen)) != FUNC_INVALID))
{
switch (op.funcIndex)
{
case FUNC_E:
2012-12-23 13:46:42 +00:00
parser.numStack[++parser.valTop] = M_E;
break;
case FUNC_PI:
2012-12-23 13:46:42 +00:00
parser.numStack[++parser.valTop] = M_PI;
break;
case FUNC_ROUND:
2012-12-22 10:23:36 +00:00
op.type = Operator::MultiArgFunction;
2012-01-23 09:30:51 +00:00
op.prevTop = parser.valTop;
parser.opStack[++parser.opTop] = op;
break;
default: // Internal function
2012-12-22 10:23:36 +00:00
op.type = Operator::SingleArgFunction;
2012-01-23 09:30:51 +00:00
parser.opStack[++parser.opTop] = op;
break;
}
}
else
{
2012-01-23 09:30:51 +00:00
double dblval;
2013-06-13 14:00:24 +00:00
if (getValue && getValue(lexer.name, lexer.nameLen, &dblval, getValueContext))
{
2012-12-23 13:46:42 +00:00
parser.numStack[++parser.valTop] = dblval;
break;
}
2012-01-23 15:43:31 +00:00
std::wstring name(lexer.name, lexer.nameLen);
_snwprintf_s(errorBuffer, _TRUNCATE, eUnknFunc, name.c_str());
return errorBuffer;
}
break;
}
default:
return eSyntax;
}
}
}
2012-02-20 14:49:38 +00:00
static const WCHAR* Calc(Parser& parser)
{
double res;
2012-01-23 09:30:51 +00:00
Operation op = parser.opStack[parser.opTop--];
// Multi-argument function
2012-12-22 10:23:36 +00:00
if (op.type == Operator::Conditional)
2012-01-23 15:43:31 +00:00
{
2013-05-31 14:28:39 +00:00
return nullptr;
2012-01-23 15:43:31 +00:00
}
2012-12-22 10:23:36 +00:00
else if (op.type == Operator::MultiArgFunction)
{
2012-01-23 09:30:51 +00:00
int paramcnt = parser.valTop - op.prevTop;
2012-01-23 09:30:51 +00:00
parser.valTop = op.prevTop;
2012-12-23 13:46:42 +00:00
const WCHAR* error = (*(MultiArgFunction)g_Functions[op.funcIndex].proc)(paramcnt, &parser.numStack[parser.valTop + 1], &res);
if (error) return error;
2012-12-23 13:46:42 +00:00
parser.numStack[++parser.valTop] = res;
2013-05-31 14:28:39 +00:00
return nullptr;
}
2012-01-23 09:30:51 +00:00
else if (parser.valTop < 0)
{
return eExtraOp;
}
// Right arg
2012-12-23 13:46:42 +00:00
double right = parser.numStack[parser.valTop--];
// One arg operations
2012-12-22 10:23:36 +00:00
if (op.type == Operator::BitwiseNOT)
{
2012-01-23 15:43:31 +00:00
res = (double)(~((long long)right));
}
2012-12-22 10:23:36 +00:00
else if (op.type == Operator::SingleArgFunction)
{
2012-12-22 10:23:36 +00:00
res = (*(SingleArgFunction)g_Functions[op.funcIndex].proc)(right);
}
else
{
2012-01-23 09:30:51 +00:00
if (parser.valTop < 0)
{
return eExtraOp;
}
// Left arg
2012-12-23 13:46:42 +00:00
double left = parser.numStack[parser.valTop--];
switch (op.type)
{
2012-12-22 10:23:36 +00:00
case Operator::ShiftLeft:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left << (long long)right);
break;
2012-12-22 10:23:36 +00:00
case Operator::ShiftRight:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left >> (long long)right);
break;
2012-12-22 10:23:36 +00:00
case Operator::Power:
res = pow(left, right);
break;
2012-12-22 10:23:36 +00:00
case Operator::NotEqual:
res = left != right;
break;
2012-12-22 10:23:36 +00:00
case Operator::GreatorOrEqual:
res = left >= right;
break;
2012-12-22 10:23:36 +00:00
case Operator::LessOrEqual:
res = left <= right;
break;
2012-12-22 10:23:36 +00:00
case Operator::LogicalAND:
res = left && right;
break;
2012-12-22 10:23:36 +00:00
case Operator::LogicalOR:
res = left || right;
break;
2012-12-22 10:23:36 +00:00
case Operator::Addition:
res = left + right;
break;
2012-12-22 10:23:36 +00:00
case Operator::Subtraction:
res = left - right;
break;
2012-12-22 10:23:36 +00:00
case Operator::Multiplication:
res = left* right;
break;
2012-12-22 10:23:36 +00:00
case Operator::Division:
if (right == 0.0)
{
return eInfinity;
}
else
{
res = left / right;
}
break;
2012-12-22 10:23:36 +00:00
case Operator::Modulo:
res = fmod(left, right);
break;
2012-12-22 10:23:36 +00:00
case Operator::UNK:
if (left <= 0)
{
res = 0.0;
}
else if (right == 0.0)
{
return eInfinity;
}
else
{
res = ceil(left / right);
}
break;
2012-12-22 10:23:36 +00:00
case Operator::BitwiseXOR:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left ^ (long long)right);
break;
2012-12-22 10:23:36 +00:00
case Operator::BitwiseAND:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left & (long long)right);
break;
2012-12-22 10:23:36 +00:00
case Operator::BitwiseOR:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left | (long long)right);
break;
2012-12-22 10:23:36 +00:00
case Operator::Equal:
res = left == right;
break;
2012-12-22 10:23:36 +00:00
case Operator::Greater:
res = left > right;
break;
2012-12-22 10:23:36 +00:00
case Operator::Less:
res = left < right;
break;
2012-12-22 10:23:36 +00:00
case Operator::ConditionalSeparator:
{
2012-01-23 15:43:31 +00:00
// Needs three arguments
2012-12-22 10:23:36 +00:00
if (parser.opTop < 0 || parser.opStack[parser.opTop--].type != Operator::Conditional)
{
return eLogicErr;
}
2012-12-23 13:46:42 +00:00
res = parser.numStack[parser.valTop--] ? left : right;
}
break;
default:
return eInternal;
}
}
2012-12-23 13:46:42 +00:00
parser.numStack[++parser.valTop] = res;
2013-05-31 14:28:39 +00:00
return nullptr;
}
2012-02-20 14:49:38 +00:00
static const WCHAR* CalcToObr(Parser& parser)
{
2012-12-22 10:23:36 +00:00
while (parser.opStack[parser.opTop].type != Operator::OpeningBracket)
{
2012-02-20 14:49:38 +00:00
const WCHAR* error = Calc(parser);
if (error) return error;
}
2012-01-23 09:30:51 +00:00
--parser.opTop;
2013-05-31 14:28:39 +00:00
return nullptr;
}
2012-12-22 10:23:36 +00:00
Token GetNextToken(Lexer& lexer)
{
2012-12-22 10:23:36 +00:00
while (lexer.charType == CharType::Separator)
{
2012-01-23 09:30:51 +00:00
lexer.charType = GetCharType(*++lexer.string);
}
2012-12-22 10:23:36 +00:00
if (lexer.charType == CharType::MinusSymbol)
{
// If the - sign follows a symbol, it is treated as a (negative) number.
2012-12-23 13:47:23 +00:00
lexer.charType = CharType::Symbol;
if (lexer.token == Token::Operator &&
lexer.value.oper != Operator::OpeningBracket && // Special case for e.g. (-PI/2), (-(5)-2).
2012-12-23 13:47:23 +00:00
lexer.value.oper != Operator::ClosingBracket) // Special case for e.g. (5)-2.
{
lexer.charType = CharType::Digit;
}
}
2012-01-23 09:30:51 +00:00
switch (lexer.charType)
{
2012-12-22 10:23:36 +00:00
case CharType::Final:
{
2012-12-23 13:46:42 +00:00
lexer.token = Token::Final;
}
break;
2012-12-22 10:23:36 +00:00
case CharType::Letter:
{
2012-12-23 13:46:42 +00:00
lexer.token = Token::Name;
2012-01-23 09:30:51 +00:00
lexer.name = lexer.string;
do
{
2012-01-23 09:30:51 +00:00
lexer.charType = GetCharType(*++lexer.string);
}
2012-12-22 10:23:36 +00:00
while (lexer.charType <= CharType::Digit);
2012-12-23 15:35:04 +00:00
lexer.nameLen = lexer.string - lexer.name;
}
break;
2012-12-22 10:23:36 +00:00
case CharType::Digit:
{
2012-01-23 09:30:51 +00:00
WCHAR* newString;
if (lexer.string[0] == L'0')
{
bool valid = true;
2012-12-23 13:46:42 +00:00
int num = 0;
2012-01-23 09:30:51 +00:00
switch (lexer.string[1])
{
2012-01-23 09:30:51 +00:00
case L'x': // Hexadecimal
2012-12-23 13:46:42 +00:00
num = wcstol(lexer.string, &newString, 16);
break;
2012-01-23 09:30:51 +00:00
case L'o': // Octal
2012-12-23 13:46:42 +00:00
num = wcstol(lexer.string + 2, &newString, 8);
break;
2012-01-23 09:30:51 +00:00
case L'b': // Binary
2012-12-23 13:46:42 +00:00
num = wcstol(lexer.string + 2, &newString, 2);
break;
default:
valid = false;
break;
}
if (valid)
{
2012-01-23 09:30:51 +00:00
if (lexer.string != newString)
{
2012-12-23 13:46:42 +00:00
lexer.token = Token::Number;
lexer.value.num = num;
2012-01-23 09:30:51 +00:00
lexer.string = newString;
lexer.charType = GetCharType(*lexer.string);
}
break;
}
}
// Decimal
2012-12-23 13:46:42 +00:00
double num = wcstod(lexer.string, &newString);
2012-01-23 09:30:51 +00:00
if (lexer.string != newString)
{
2012-12-23 13:46:42 +00:00
lexer.token = Token::Number;
lexer.value.num = num;
2012-01-23 09:30:51 +00:00
lexer.string = newString;
lexer.charType = GetCharType(*lexer.string);
}
}
break;
2012-12-22 10:23:36 +00:00
case CharType::Symbol:
{
2012-12-22 10:23:36 +00:00
Operator oper = GetOperator(lexer.string);
if (oper != Operator::Invalid)
{
2012-12-23 13:46:42 +00:00
lexer.token = Token::Operator;
lexer.value.oper = oper;
2012-12-22 10:23:36 +00:00
lexer.string += ((int)oper <= (int)Operator::LogicalOR) ? 2 : 1;
2012-01-23 09:30:51 +00:00
lexer.charType = GetCharType(*lexer.string);
}
}
break;
default:
2012-12-23 13:46:42 +00:00
lexer.token = Token::Error;
break;
}
2012-12-23 13:46:42 +00:00
return lexer.token;
2012-01-23 09:30:51 +00:00
}
CharType GetCharType(WCHAR ch)
{
switch (ch)
{
case L'\0':
2012-12-22 10:23:36 +00:00
return CharType::Final;
2012-01-23 09:30:51 +00:00
case L' ':
case L'\t':
case L'\n':
2012-12-22 10:23:36 +00:00
return CharType::Separator;
2012-01-23 09:30:51 +00:00
case L'_':
2012-12-22 10:23:36 +00:00
return CharType::Letter;
2012-01-23 09:30:51 +00:00
case L'-':
2012-12-22 10:23:36 +00:00
return CharType::MinusSymbol;
case L'+':
2012-01-23 09:30:51 +00:00
case L'/':
case L'*':
case L'~':
case L'(':
case L')':
case L'<':
case L'>':
case L'%':
case L'$':
case L',':
case L'?':
case L':':
case L'=':
case L'&':
case L'|':
2012-12-22 10:23:36 +00:00
return CharType::Symbol;
2012-01-23 09:30:51 +00:00
}
2012-12-22 10:23:36 +00:00
if (iswalpha(ch)) return CharType::Letter;
if (iswdigit(ch)) return CharType::Digit;
2012-01-23 09:30:51 +00:00
2012-12-22 10:23:36 +00:00
return CharType::Unknown;
2012-01-23 09:30:51 +00:00
}
bool MathParser::IsDelimiter(WCHAR ch)
{
CharType type = GetCharType(ch);
2012-12-22 10:23:36 +00:00
return type == CharType::Symbol || type == CharType::Separator;
2012-01-23 09:30:51 +00:00
}
BYTE GetFunctionIndex(const WCHAR* str, BYTE len)
2012-01-23 09:30:51 +00:00
{
const int funcCount = sizeof(g_Functions) / sizeof(Function);
for (int i = 0; i < funcCount; ++i)
{
if (g_Functions[i].length == len &&
_wcsnicmp(str, g_Functions[i].name, len) == 0)
{
return i;
}
}
return FUNC_INVALID;
2012-01-23 09:30:51 +00:00
}
2012-12-22 10:23:36 +00:00
Operator GetOperator(const WCHAR* str)
2012-01-23 09:30:51 +00:00
{
switch (str[0])
{
2012-12-22 10:23:36 +00:00
case L'(':
return Operator::OpeningBracket;
case L'+':
return Operator::Addition;
case L'-':
return Operator::Subtraction;
case L'*':
return (str[1] == L'*') ? Operator::Power : Operator::Multiplication;
case L'/':
return Operator::Division;
case L'%':
return Operator::Modulo;
case L'$':
return Operator::UNK;
case L'^':
return Operator::BitwiseXOR;
case L'~':
return Operator::BitwiseNOT;
case L'&':
return (str[1] == L'&') ? Operator::LogicalAND : Operator::BitwiseAND;
case L'|':
return (str[1] == L'|') ? Operator::LogicalOR : Operator::BitwiseOR;
case L'=':
return Operator::Equal;
case L'>':
return (str[1] == L'>') ? Operator::ShiftRight : (str[1] == L'=') ? Operator::GreatorOrEqual : Operator::Greater;
case L'<':
return (str[1] == L'>') ? Operator::NotEqual : (str[1] == L'<') ? Operator::ShiftLeft : (str[1] == L'=') ? Operator::LessOrEqual : Operator::Less;
case L'?':
return Operator::Conditional;
case L':':
return Operator::ConditionalSeparator;
case L')':
return Operator::ClosingBracket;
case L',':
return Operator::Comma;
2012-01-23 09:30:51 +00:00
}
2012-12-22 10:23:36 +00:00
return Operator::Invalid;
}
// -----------------------------------------------------------------------------------------------
// Misc
// -----------------------------------------------------------------------------------------------
static double frac(double x)
{
double y;
return modf(x, &y);
}
static double trunc2(double x)
{
return (x >= 0.0) ? floor(x) : ceil(x);
}
static double rad(double deg)
{
return (deg / 180.0) * M_PI;
}
static double sgn(double x)
{
return (x > 0) ? 1 : (x < 0) ? -1 : 0;
}
static double neg(double x)
{
return -x;
}
// "Advanced" round function; second argument - sharpness
2012-02-20 14:49:38 +00:00
static const WCHAR* round(int paramcnt, double* args, double* result)
{
int sharpness;
if (paramcnt == 1)
{
sharpness = 0;
}
else if (paramcnt == 2)
{
sharpness = (int)args[1];
}
else
{
return eInvPrmCnt;
}
double x = args[0];
double coef;
if (sharpness < 0)
{
coef = 0.1;
sharpness = -sharpness;
}
else
{
coef = 10;
}
for (int i = 0; i < sharpness; i++) x *= coef;
x = (x + ((x >= 0) ? 0.5 : -0.5));
x = (x >= 0.0) ? floor(x) : ceil(x);
for (int i = 0; i < sharpness; i++) x /= coef;
*result = x;
2013-05-31 14:28:39 +00:00
return nullptr;
}