2012-01-22 20:28:05 +00:00
|
|
|
/*
|
|
|
|
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
|
2012-01-23 06:36:15 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-01-22 20:28:05 +00:00
|
|
|
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"
|
2012-01-22 20:28:05 +00:00
|
|
|
#include "MathParser.h"
|
|
|
|
|
|
|
|
static const double M_E = 2.7182818284590452354;
|
|
|
|
static const double M_PI = 3.14159265358979323846;
|
|
|
|
|
|
|
|
typedef double (*OneArgProc)(double arg);
|
2012-02-20 17:27:00 +00:00
|
|
|
typedef const WCHAR* (*MultiArgProc)(int paramcnt, double* args, double* result);
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
enum OperationType
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
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-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
enum CharType
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
CH_UNKNOWN = 0x00,
|
2012-01-22 20:28:05 +00:00
|
|
|
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-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
enum MathTokenType
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
TOK_ERROR,
|
|
|
|
TOK_NONE,
|
|
|
|
TOK_FINAL,
|
|
|
|
TOK_FLOAT,
|
|
|
|
TOK_SYMBOL,
|
|
|
|
TOK_NAME
|
2012-01-23 09:30:51 +00:00
|
|
|
};
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
struct Operation
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 12:33:17 +00:00
|
|
|
BYTE type;
|
|
|
|
BYTE funcIndex;
|
|
|
|
char prevTop;
|
2012-01-23 09:30:51 +00:00
|
|
|
};
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
struct Function
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
WCHAR* name;
|
2012-01-27 15:36:46 +00:00
|
|
|
OneArgProc proc;
|
2012-01-23 09:30:51 +00:00
|
|
|
BYTE length;
|
|
|
|
};
|
2012-01-22 20:28:05 +00:00
|
|
|
|
|
|
|
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);
|
2012-01-22 20:28:05 +00:00
|
|
|
|
|
|
|
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-01-27 15:36:46 +00:00
|
|
|
{ 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-22 20:28:05 +00:00
|
|
|
};
|
|
|
|
|
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;
|
2012-01-27 15:36:46 +00:00
|
|
|
static const BYTE FUNC_INVALID = UCHAR_MAX;
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 12:33:17 +00:00
|
|
|
static const Operation g_BrOp = { OP_OBR, 0, 0};
|
|
|
|
static const Operation g_NegOp = { OP_FUNC_ONEARG, 17, 0 };
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
static const BYTE g_OpPriorities[OP_FUNC_MULTIARG + 1] =
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
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);
|
2012-01-27 15:36:46 +00:00
|
|
|
static BYTE GetFunctionIndex(const WCHAR* str, BYTE len);
|
2012-01-23 09:30:51 +00:00
|
|
|
static int FindSymbol(const WCHAR* str);
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
struct Parser
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-25 16:00:49 +00:00
|
|
|
Operation opStack[96];
|
|
|
|
double valStack[64];
|
2012-01-23 12:33:17 +00:00
|
|
|
char opTop;
|
|
|
|
char valTop;
|
2012-01-23 09:30:51 +00:00
|
|
|
int obrDist;
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
Parser() : opTop(0), valTop(-1), obrDist(2) { opStack[0].type = OP_OBR; }
|
|
|
|
};
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-02-20 14:49:38 +00:00
|
|
|
static const WCHAR* CalcToObr(Parser& parser);
|
|
|
|
static const WCHAR* Calc(Parser& parser);
|
2012-01-22 20:28:05 +00:00
|
|
|
|
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-22 20:28:05 +00:00
|
|
|
|
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)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-25 16:00:49 +00:00
|
|
|
int brackets = 0;
|
2012-01-22 20:28:05 +00:00
|
|
|
|
|
|
|
// Brackets Matching
|
|
|
|
while (*formula)
|
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
if (*formula == L'(')
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-25 16:00:49 +00:00
|
|
|
++brackets;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
2012-01-23 09:30:51 +00:00
|
|
|
else if (*formula == L')')
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-25 16:00:49 +00:00
|
|
|
--brackets;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
++formula;
|
|
|
|
}
|
|
|
|
|
2012-01-25 16:00:49 +00:00
|
|
|
return (brackets != 0) ? eBrackets : NULL;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 14:49:38 +00:00
|
|
|
const WCHAR* MathParser::CheckedParse(const WCHAR* formula, double* result)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
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-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 14:49:38 +00:00
|
|
|
const WCHAR* MathParser::Parse(const WCHAR* formula, CMeasureCalc* calc, double* result)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 15:43:31 +00:00
|
|
|
static WCHAR errorBuffer[128];
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
if (!*formula)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
*result = 0.0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
Parser parser;
|
|
|
|
Lexer lexer(formula);
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-02-20 14:49:38 +00:00
|
|
|
const WCHAR* error;
|
2012-01-22 20:28:05 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2012-01-25 16:00:49 +00:00
|
|
|
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;
|
2012-01-22 20:28:05 +00:00
|
|
|
switch (token)
|
|
|
|
{
|
|
|
|
case TOK_ERROR:
|
|
|
|
return eSyntax;
|
|
|
|
|
|
|
|
case TOK_FINAL:
|
2012-01-23 09:30:51 +00:00
|
|
|
if ((error = CalcToObr(parser)) != NULL)
|
|
|
|
{
|
2012-01-22 20:28:05 +00:00
|
|
|
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-01-22 20:28:05 +00:00
|
|
|
|
|
|
|
case TOK_FLOAT:
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.valStack[++parser.valTop] = lexer.extValue;
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TOK_SYMBOL:
|
2012-01-23 09:30:51 +00:00
|
|
|
switch (lexer.intValue)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
case OP_OBR:
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.opStack[++parser.opTop] = g_BrOp;
|
|
|
|
parser.obrDist = 2;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
case OP_CBR:
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
if ((error = CalcToObr(parser)) != NULL) return error;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
case OP_COMMA:
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
if ((error = CalcToObr(parser)) != NULL) return error;
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
if (parser.opStack[parser.opTop].type == OP_FUNC_MULTIARG)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.opStack[++parser.opTop] = g_BrOp;
|
|
|
|
parser.obrDist = 2;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return eSyntax;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
Operation op;
|
2012-01-23 09:30:51 +00:00
|
|
|
op.type = (OperationType)lexer.intValue;
|
2012-01-22 20:28:05 +00:00
|
|
|
switch (op.type)
|
|
|
|
{
|
|
|
|
case OP_ADD:
|
2012-01-23 09:30:51 +00:00
|
|
|
if (parser.obrDist >= 1)
|
|
|
|
{
|
|
|
|
// Goto next token
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OP_SUB:
|
2012-01-23 09:30:51 +00:00
|
|
|
if (parser.obrDist >= 1)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.opStack[++parser.opTop] = g_NegOp;
|
|
|
|
|
|
|
|
// Goto next token
|
|
|
|
continue;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OP_LOGIC:
|
|
|
|
case OP_LOGIC_SEP:
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.obrDist = 2;
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
while (g_OpPriorities[op.type] <= g_OpPriorities[parser.opStack[parser.opTop].type])
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
if ((error = Calc(parser)) != NULL) return error;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.opStack[++parser.opTop] = op;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
case TOK_NAME:
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
Operation op;
|
2012-01-23 09:30:51 +00:00
|
|
|
if (lexer.nameLen <= FUNC_MAX_LEN &&
|
2012-01-27 15:36:46 +00:00
|
|
|
((op.funcIndex = GetFunctionIndex(lexer.name, lexer.nameLen)) != FUNC_INVALID))
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 12:33:17 +00:00
|
|
|
switch (op.funcIndex)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
case FUNC_E:
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.valStack[++parser.valTop] = M_E;
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FUNC_PI:
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.valStack[++parser.valTop] = M_PI;
|
2012-01-22 20:28:05 +00:00
|
|
|
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;
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: // Internal function
|
|
|
|
op.type = OP_FUNC_ONEARG;
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.opStack[++parser.opTop] = op;
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
double dblval;
|
|
|
|
if (calc && calc->GetMeasureValue(lexer.name, lexer.nameLen, &dblval))
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.valStack[++parser.valTop] = dblval;
|
2012-01-22 20:28:05 +00:00
|
|
|
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;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return eSyntax;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 14:49:38 +00:00
|
|
|
static const WCHAR* Calc(Parser& parser)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
double res;
|
2012-01-23 09:30:51 +00:00
|
|
|
Operation op = parser.opStack[parser.opTop--];
|
2012-01-22 20:28:05 +00:00
|
|
|
|
|
|
|
// 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-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
int paramcnt = parser.valTop - op.prevTop;
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.valTop = op.prevTop;
|
2012-02-20 17:27:00 +00:00
|
|
|
const WCHAR* error = (*(MultiArgProc)g_Functions[op.funcIndex].proc)(paramcnt, &parser.valStack[parser.valTop + 1], &res);
|
2012-01-22 20:28:05 +00:00
|
|
|
if (error) return error;
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.valStack[++parser.valTop] = res;
|
2012-01-22 20:28:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-23 09:30:51 +00:00
|
|
|
else if (parser.valTop < 0)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
return eExtraOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right arg
|
2012-01-23 09:30:51 +00:00
|
|
|
double right = parser.valStack[parser.valTop--];
|
2012-01-22 20:28:05 +00:00
|
|
|
|
|
|
|
// One arg operations
|
|
|
|
if (op.type == OP_NOT)
|
|
|
|
{
|
2012-01-23 15:43:31 +00:00
|
|
|
res = (double)(~((long long)right));
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
else if (op.type == OP_FUNC_ONEARG)
|
|
|
|
{
|
2012-01-23 12:33:17 +00:00
|
|
|
res = (*(OneArgProc)g_Functions[op.funcIndex].proc)(right);
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
if (parser.valTop < 0)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
return eExtraOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Left arg
|
2012-01-23 09:30:51 +00:00
|
|
|
double left = parser.valStack[parser.valTop--];
|
2012-01-22 20:28:05 +00:00
|
|
|
switch (op.type)
|
|
|
|
{
|
|
|
|
case OP_SHL:
|
2012-01-23 15:43:31 +00:00
|
|
|
res = (double)((long long)left << (long long)right);
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OP_SHR:
|
2012-01-23 15:43:31 +00:00
|
|
|
res = (double)((long long)left >> (long long)right);
|
2012-01-22 20:28:05 +00:00
|
|
|
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);
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OP_AND:
|
2012-01-23 15:43:31 +00:00
|
|
|
res = (double)((long long)left & (long long)right);
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OP_OR:
|
2012-01-23 15:43:31 +00:00
|
|
|
res = (double)((long long)left | (long long)right);
|
2012-01-22 20:28:05 +00:00
|
|
|
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)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
return eLogicErr;
|
|
|
|
}
|
2012-01-23 15:43:31 +00:00
|
|
|
res = parser.valStack[parser.valTop--] ? left : right;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return eInternal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
parser.valStack[++parser.valTop] = res;
|
2012-01-22 20:28:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-20 14:49:38 +00:00
|
|
|
static const WCHAR* CalcToObr(Parser& parser)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
while (parser.opStack[parser.opTop].type != OP_OBR)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-02-20 14:49:38 +00:00
|
|
|
const WCHAR* error = Calc(parser);
|
2012-01-22 20:28:05 +00:00
|
|
|
if (error) return error;
|
|
|
|
}
|
2012-01-23 09:30:51 +00:00
|
|
|
--parser.opTop;
|
2012-01-22 20:28:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
MathTokenType GetNextToken(Lexer& lexer)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
MathTokenType result = TOK_ERROR;
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
while (lexer.charType == CH_SEPARAT)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
lexer.charType = GetCharType(*++lexer.string);
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
switch (lexer.charType)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
case CH_FINAL:
|
|
|
|
{
|
|
|
|
result = TOK_FINAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CH_LETTER:
|
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
lexer.name = lexer.string;
|
2012-01-22 20:28:05 +00:00
|
|
|
do
|
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
lexer.charType = GetCharType(*++lexer.string);
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
2012-01-23 09:30:51 +00:00
|
|
|
while (lexer.charType <= CH_DIGIT);
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
lexer.nameLen = lexer.string - lexer.name;
|
2012-01-22 20:28:05 +00:00
|
|
|
result = TOK_NAME;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CH_DIGIT:
|
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
WCHAR* newString;
|
|
|
|
if (lexer.string[0] == L'0')
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
bool valid = true;
|
2012-01-23 09:30:51 +00:00
|
|
|
switch (lexer.string[1])
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
case L'x': // Hexadecimal
|
|
|
|
lexer.intValue = wcstol(lexer.string, &newString, 16);
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
case L'o': // Octal
|
|
|
|
lexer.intValue = wcstol(lexer.string + 2, &newString, 8);
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
case L'b': // Binary
|
|
|
|
lexer.intValue = wcstol(lexer.string + 2, &newString, 2);
|
2012-01-22 20:28:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid)
|
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
if (lexer.string != newString)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
lexer.string = newString;
|
|
|
|
lexer.charType = GetCharType(*lexer.string);
|
|
|
|
lexer.extValue = lexer.intValue;
|
2012-01-22 20:28:05 +00:00
|
|
|
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-22 20:28:05 +00:00
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
lexer.string = newString;
|
|
|
|
lexer.charType = GetCharType(*lexer.string);
|
2012-01-22 20:28:05 +00:00
|
|
|
result = TOK_FLOAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CH_SYMBOL:
|
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
int sym = FindSymbol(lexer.string);
|
2012-01-22 20:28:05 +00:00
|
|
|
if (sym >= 0)
|
|
|
|
{
|
2012-01-23 09:30:51 +00:00
|
|
|
lexer.string += (sym <= OP_LOGIC_OR) ? 2 : 1;
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
lexer.charType = GetCharType(*lexer.string);
|
|
|
|
lexer.intValue = sym;
|
2012-01-22 20:28:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-27 15:36:46 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 15:36:46 +00:00
|
|
|
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;
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------
|
|
|
|
// 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)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|