rainmeter-studio/Library/MathParser.cpp

844 lines
16 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;
typedef double (*OneArgProc)(double arg);
typedef const WCHAR* (*MultiArgProc)(int paramcnt, double* args, double* result);
2012-01-23 09:30:51 +00:00
enum OperationType
{
OP_SHL,
OP_SHR,
OP_POW,
OP_LOGIC_NEQ,
OP_LOGIC_GEQ,
OP_LOGIC_LEQ,
OP_LOGIC_AND,
OP_LOGIC_OR,
OP_OBR,
OP_ADD,
OP_SUB,
OP_MUL,
OP_DIV,
OP_MOD,
OP_UNK,
OP_XOR,
OP_NOT,
OP_AND,
OP_OR,
OP_EQU,
OP_GREATER,
OP_SMALLER,
OP_LOGIC,
OP_LOGIC_SEP,
OP_CBR,
OP_COMMA,
OP_FUNC_ONEARG, // Special
OP_FUNC_MULTIARG // Special
2012-01-23 09:30:51 +00:00
};
2012-01-23 09:30:51 +00:00
enum CharType
{
2012-01-23 09:30:51 +00:00
CH_UNKNOWN = 0x00,
CH_LETTER = 0x01,
CH_DIGIT = 0x02,
CH_SEPARAT = 0x04,
CH_SYMBOL = 0x08,
CH_FINAL = 0x7F
2012-01-23 09:30:51 +00:00
};
2012-01-23 09:30:51 +00:00
enum MathTokenType
{
TOK_ERROR,
TOK_NONE,
TOK_FINAL,
TOK_FLOAT,
TOK_SYMBOL,
TOK_NAME
2012-01-23 09:30:51 +00:00
};
2012-01-23 09:30:51 +00:00
struct Operation
{
BYTE 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;
OneArgProc proc;
2012-01-23 09:30:51 +00:00
BYTE length;
};
static double neg(double x);
static double frac(double x);
static double trunc(double x);
static double sgn(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 },
{ L"round", (OneArgProc)&round, 5 },
2012-01-23 09:30:51 +00:00
{ L"asin", &asin, 4 },
{ L"acos", &acos, 4 },
{ L"sgn", &sgn, 4 },
{ L"neg", &neg, 4 },
{ 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 = 18;
static const int FUNC_PI = 19;
static const BYTE FUNC_INVALID = UCHAR_MAX;
static const Operation g_BrOp = { OP_OBR, 0, 0};
static const Operation g_NegOp = { OP_FUNC_ONEARG, 17, 0 };
2012-01-23 09:30:51 +00:00
static const BYTE g_OpPriorities[OP_FUNC_MULTIARG + 1] =
{
5, // OP_SHL
5, // OP_SHR
5, // OP_POW
2, // OP_LOGIC_NEQ
2, // OP_LOGIC_GEQ
2, // OP_LOGIC_LEQ
2, // OP_LOGIC_AND
2, // OP_LOGIC_OR
0, // OP_OBR
3, // OP_ADD
3, // OP_SUB
4, // OP_MUL
4, // OP_DIV
4, // OP_MOD
4, // OP_UNK
5, // OP_XOR
5, // OP_NOT
5, // OP_AND
5, // OP_OR
2, // OP_EQU
2, // OP_GREATER
2, // OP_SMALLER
1, // OP_LOGIC
2, // OP_LOGIC_SEP
0, // OP_CBR
2, // OP_COMMA
6, // OP_FUNC_ONEARG
6 // OP_FUNC_MULTIARG
};
2012-01-23 09:30:51 +00:00
static CharType GetCharType(WCHAR ch);
static BYTE GetFunctionIndex(const WCHAR* str, BYTE len);
2012-01-23 09:30:51 +00:00
static int FindSymbol(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-01-23 09:30:51 +00:00
Parser() : opTop(0), valTop(-1), obrDist(2) { opStack[0].type = OP_OBR; }
};
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;
MathTokenType prevToken;
CharType charType;
2012-01-23 09:30:51 +00:00
Lexer(const WCHAR* str) : string(str), name(), nameLen(), extValue(), intValue(), prevToken(TOK_NONE), charType(GetCharType(*str)) {}
};
static MathTokenType GetNextToken(Lexer& lexer);
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-01-23 09:30:51 +00:00
MathTokenType token = GetNextToken(lexer);
--parser.obrDist;
switch (token)
{
case TOK_ERROR:
return eSyntax;
case TOK_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;
case TOK_FLOAT:
2012-01-23 09:30:51 +00:00
parser.valStack[++parser.valTop] = lexer.extValue;
break;
case TOK_SYMBOL:
2012-01-23 09:30:51 +00:00
switch (lexer.intValue)
{
2012-01-23 09:30:51 +00:00
case OP_OBR:
{
2012-01-23 09:30:51 +00:00
parser.opStack[++parser.opTop] = g_BrOp;
parser.obrDist = 2;
}
break;
2012-01-23 09:30:51 +00:00
case OP_CBR:
{
2012-01-23 09:30:51 +00:00
if ((error = CalcToObr(parser)) != NULL) return error;
}
break;
2012-01-23 09:30:51 +00:00
case OP_COMMA:
{
2012-01-23 09:30:51 +00:00
if ((error = CalcToObr(parser)) != NULL) return error;
2012-01-23 09:30:51 +00:00
if (parser.opStack[parser.opTop].type == OP_FUNC_MULTIARG)
{
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-01-23 09:30:51 +00:00
op.type = (OperationType)lexer.intValue;
switch (op.type)
{
case OP_ADD:
2012-01-23 09:30:51 +00:00
if (parser.obrDist >= 1)
{
// Goto next token
continue;
}
break;
case OP_SUB:
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;
case OP_LOGIC:
case OP_LOGIC_SEP:
2012-01-23 09:30:51 +00:00
parser.obrDist = 2;
break;
}
2012-01-23 09:30:51 +00:00
while (g_OpPriorities[op.type] <= g_OpPriorities[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-01-23 09:30:51 +00:00
case TOK_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:
op.type = OP_FUNC_MULTIARG;
2012-01-23 09:30:51 +00:00
op.prevTop = parser.valTop;
parser.opStack[++parser.opTop] = op;
break;
default: // Internal function
op.type = OP_FUNC_ONEARG;
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-01-23 15:43:31 +00:00
if (op.type == OP_LOGIC)
{
return NULL;
}
else if (op.type == OP_FUNC_MULTIARG)
{
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;
const WCHAR* error = (*(MultiArgProc)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
if (op.type == OP_NOT)
{
2012-01-23 15:43:31 +00:00
res = (double)(~((long long)right));
}
else if (op.type == OP_FUNC_ONEARG)
{
res = (*(OneArgProc)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)
{
case OP_SHL:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left << (long long)right);
break;
case OP_SHR:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left >> (long long)right);
break;
case OP_POW:
res = pow(left, right);
break;
case OP_LOGIC_NEQ:
res = left != right;
break;
case OP_LOGIC_GEQ:
res = left >= right;
break;
case OP_LOGIC_LEQ:
res = left <= right;
break;
case OP_LOGIC_AND:
res = left && right;
break;
case OP_LOGIC_OR:
res = left || right;
break;
case OP_ADD:
res = left + right;
break;
case OP_SUB:
res = left - right;
break;
case OP_MUL:
res = left* right;
break;
case OP_DIV:
if (right == 0.0)
{
return eInfinity;
}
else
{
res = left / right;
}
break;
case OP_MOD:
res = fmod(left, right);
break;
case OP_UNK:
if (left <= 0)
{
res = 0.0;
}
else if (right == 0.0)
{
return eInfinity;
}
else
{
res = ceil(left / right);
}
break;
case OP_XOR:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left ^ (long long)right);
break;
case OP_AND:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left & (long long)right);
break;
case OP_OR:
2012-01-23 15:43:31 +00:00
res = (double)((long long)left | (long long)right);
break;
case OP_EQU:
res = left == right;
break;
case OP_GREATER:
res = left > right;
break;
case OP_SMALLER:
res = left < right;
break;
case OP_LOGIC_SEP:
{
2012-01-23 15:43:31 +00:00
// Needs three arguments
2012-01-23 09:30:51 +00:00
if (parser.opTop < 0 || parser.opStack[parser.opTop--].type != OP_LOGIC)
{
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-01-23 09:30:51 +00:00
while (parser.opStack[parser.opTop].type != OP_OBR)
{
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-01-23 09:30:51 +00:00
MathTokenType GetNextToken(Lexer& lexer)
{
MathTokenType result = TOK_ERROR;
2012-01-23 09:30:51 +00:00
while (lexer.charType == CH_SEPARAT)
{
2012-01-23 09:30:51 +00:00
lexer.charType = GetCharType(*++lexer.string);
}
2012-01-23 09:30:51 +00:00
switch (lexer.charType)
{
case CH_FINAL:
{
result = TOK_FINAL;
}
break;
case CH_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-01-23 09:30:51 +00:00
while (lexer.charType <= CH_DIGIT);
2012-01-23 09:30:51 +00:00
lexer.nameLen = lexer.string - lexer.name;
result = TOK_NAME;
}
break;
case CH_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;
result = TOK_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);
result = TOK_FLOAT;
}
}
break;
case CH_SYMBOL:
{
2012-01-23 09:30:51 +00:00
int sym = FindSymbol(lexer.string);
if (sym >= 0)
{
2012-01-23 09:30:51 +00:00
lexer.string += (sym <= OP_LOGIC_OR) ? 2 : 1;
2012-01-23 09:30:51 +00:00
lexer.charType = GetCharType(*lexer.string);
lexer.intValue = sym;
result = TOK_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':
return CH_FINAL;
case L' ':
case L'\t':
case L'\n':
return CH_SEPARAT;
case L'_':
return CH_LETTER;
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'=':
case L'&':
case L'|':
return CH_SYMBOL;
}
if (iswalpha(ch)) return CH_LETTER;
if (iswdigit(ch)) return CH_DIGIT;
return CH_UNKNOWN;
}
bool MathParser::IsDelimiter(WCHAR ch)
{
CharType type = GetCharType(ch);
return type == CH_SYMBOL || type == CH_SEPARAT;
}
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
}
int FindSymbol(const WCHAR* str)
{
switch (str[0])
{
2012-01-23 15:43:31 +00:00
case L'(': return OP_OBR;
2012-01-23 09:30:51 +00:00
case L'+': return OP_ADD;
case L'-': return OP_SUB;
case L'*': return (str[1] == L'*') ? OP_POW : OP_MUL;
case L'/': return OP_DIV;
case L'%': return OP_MOD;
case L'$': return OP_UNK;
case L'^': return OP_XOR;
case L'~': return OP_NOT;
case L'&': return (str[1] == L'&') ? OP_LOGIC_AND : OP_AND;
case L'|': return (str[1] == L'|') ? OP_LOGIC_OR : OP_OR;
case L'=': return OP_EQU;
case L'>': return (str[1] == L'>') ? OP_SHR : (str[1] == L'=') ? OP_LOGIC_GEQ : OP_GREATER;
case L'<': return (str[1] == L'>') ? OP_LOGIC_NEQ : (str[1] == L'<') ? OP_SHL : (str[1] == L'=') ? OP_LOGIC_LEQ : OP_SMALLER;
case L'?': return OP_LOGIC;
case L':': return OP_LOGIC_SEP;
case L')': return OP_CBR;
case L',': return OP_COMMA;
}
return -1;
}
// -----------------------------------------------------------------------------------------------
// 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 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;
}