rainmeter-studio/Library/MathParser.cpp

896 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>
#include "StdAfx.h"
2012-01-23 07:21:03 +00:00
#include "MeasureCalc.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,
Float,
Symbol,
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 trunc(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", &trunc, 5 },
{ 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 },
2012-01-23 09:30:51 +00:00
{ L"e", NULL, 1 },
2012-02-20 14:49:38 +00:00
{ L"pi", NULL, 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];
double valStack[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;
double extValue;
int intValue;
2012-12-22 10:23:36 +00:00
Token prevToken;
2012-01-23 09:30:51 +00:00
CharType charType;
2012-12-22 10:23:36 +00:00
Lexer(const WCHAR* str) : string(str), name(), nameLen(), extValue(), intValue(), prevToken(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;
}
return (brackets != 0) ? eBrackets : NULL;
}
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)
{
error = Parse(formula, NULL, result);
}
return error;
}
2012-02-20 14:49:38 +00:00
const WCHAR* MathParser::Parse(const WCHAR* formula, CMeasureCalc* calc, double* result)
{
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;
return NULL;
}
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) ||
(parser.valTop == _countof(parser.valStack) - 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:
2012-01-23 09:30:51 +00:00
if ((error = CalcToObr(parser)) != NULL)
{
return error;
2012-01-23 09:30:51 +00:00
}
else if (parser.opTop != -1 || parser.valTop != 0)
{
return eInternal;
}
else
{
// Done!
*result = parser.valStack[0];
return NULL;
}
break;
2012-12-22 10:23:36 +00:00
case Token::Float:
2012-01-23 09:30:51 +00:00
parser.valStack[++parser.valTop] = lexer.extValue;
break;
2012-12-22 10:23:36 +00:00
case Token::Symbol:
2012-01-23 09:30:51 +00:00
switch (lexer.intValue)
{
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:
{
2012-01-23 09:30:51 +00:00
if ((error = CalcToObr(parser)) != NULL) return error;
}
break;
2012-12-22 10:23:36 +00:00
case Operator::Comma:
{
2012-01-23 09:30:51 +00:00
if ((error = CalcToObr(parser)) != NULL) 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-22 10:23:36 +00:00
op.type = (Operator)lexer.intValue;
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])
{
2012-01-23 09:30:51 +00:00
if ((error = Calc(parser)) != NULL) 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-01-23 09:30:51 +00:00
parser.valStack[++parser.valTop] = M_E;
break;
case FUNC_PI:
2012-01-23 09:30:51 +00:00
parser.valStack[++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;
if (calc && calc->GetMeasureValue(lexer.name, lexer.nameLen, &dblval))
{
2012-01-23 09:30:51 +00:00
parser.valStack[++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
{
return NULL;
}
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-22 10:23:36 +00:00
const WCHAR* error = (*(MultiArgFunction)g_Functions[op.funcIndex].proc)(paramcnt, &parser.valStack[parser.valTop + 1], &res);
if (error) return error;
2012-01-23 09:30:51 +00:00
parser.valStack[++parser.valTop] = res;
return NULL;
}
2012-01-23 09:30:51 +00:00
else if (parser.valTop < 0)
{
return eExtraOp;
}
// Right arg
2012-01-23 09:30:51 +00:00
double right = parser.valStack[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-01-23 09:30:51 +00:00
double left = parser.valStack[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-01-23 15:43:31 +00:00
res = parser.valStack[parser.valTop--] ? left : right;
}
break;
default:
return eInternal;
}
}
2012-01-23 09:30:51 +00:00
parser.valStack[++parser.valTop] = res;
return NULL;
}
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;
return NULL;
}
2012-12-22 10:23:36 +00:00
Token GetNextToken(Lexer& lexer)
{
2012-12-22 10:23:36 +00:00
Token result = Token::Error;
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-22 10:23:36 +00:00
lexer.charType = (lexer.prevToken == Token::Symbol) ? CharType::Digit : CharType::Symbol;
}
2012-01-23 09:30:51 +00:00
switch (lexer.charType)
{
2012-12-22 10:23:36 +00:00
case CharType::Final:
{
2012-12-22 10:23:36 +00:00
result = Token::Final;
}
break;
2012-12-22 10:23:36 +00:00
case CharType::Letter:
{
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-01-23 09:30:51 +00:00
lexer.nameLen = lexer.string - lexer.name;
2012-12-22 10:23:36 +00:00
result = Token::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-01-23 09:30:51 +00:00
switch (lexer.string[1])
{
2012-01-23 09:30:51 +00:00
case L'x': // Hexadecimal
lexer.intValue = wcstol(lexer.string, &newString, 16);
break;
2012-01-23 09:30:51 +00:00
case L'o': // Octal
lexer.intValue = wcstol(lexer.string + 2, &newString, 8);
break;
2012-01-23 09:30:51 +00:00
case L'b': // Binary
lexer.intValue = 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-01-23 09:30:51 +00:00
lexer.string = newString;
lexer.charType = GetCharType(*lexer.string);
lexer.extValue = lexer.intValue;
2012-12-22 10:23:36 +00:00
result = Token::Float;
}
break;
}
}
// Decimal
2012-01-23 09:30:51 +00:00
lexer.extValue = wcstod(lexer.string, &newString);
if (lexer.string != newString)
{
2012-01-23 09:30:51 +00:00
lexer.string = newString;
lexer.charType = GetCharType(*lexer.string);
2012-12-22 10:23:36 +00:00
result = Token::Float;
}
}
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-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);
2012-12-22 10:23:36 +00:00
lexer.intValue = (int)oper;
result = Token::Symbol;
}
}
break;
default:
break;
}
2012-01-23 09:30:51 +00:00
return lexer.prevToken = result;
}
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 trunc(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;
return NULL;
}