Calc/MathParser: Conditional limit increased to 30.

This commit is contained in:
Birunthan Mohanathas 2012-01-23 12:33:17 +00:00
parent e99403ce1e
commit 017c2e9ad9
2 changed files with 22 additions and 36 deletions

View File

@ -22,7 +22,7 @@
#include "MeasureCalc.h" #include "MeasureCalc.h"
#include "MathParser.h" #include "MathParser.h"
static const int MAX_STACK_SIZE = 32; static const int MAX_STACK_SIZE = 96;
static const double M_E = 2.7182818284590452354; static const double M_E = 2.7182818284590452354;
static const double M_PI = 3.14159265358979323846; static const double M_PI = 3.14159265358979323846;
@ -84,9 +84,9 @@ enum MathTokenType
struct Operation struct Operation
{ {
void* proc; BYTE type;
BYTE prevTop; BYTE funcIndex;
OperationType type; char prevTop;
}; };
struct Function struct Function
@ -131,8 +131,8 @@ static const int FUNC_ROUND = 13;
static const int FUNC_E = 18; static const int FUNC_E = 18;
static const int FUNC_PI = 19; static const int FUNC_PI = 19;
static const Operation g_BrOp = { NULL, 0, OP_OBR }; static const Operation g_BrOp = { OP_OBR, 0, 0};
static const Operation g_NegOp = { (void*)&neg, 0, OP_FUNC_ONEARG }; static const Operation g_NegOp = { OP_FUNC_ONEARG, 17, 0 };
static const BYTE g_OpPriorities[OP_FUNC_MULTIARG + 1] = static const BYTE g_OpPriorities[OP_FUNC_MULTIARG + 1] =
{ {
@ -167,15 +167,15 @@ static const BYTE g_OpPriorities[OP_FUNC_MULTIARG + 1] =
}; };
static CharType GetCharType(WCHAR ch); static CharType GetCharType(WCHAR ch);
static int GetFunction(const WCHAR* str, size_t len, void** data); static int GetFunction(const WCHAR* str, size_t len);
static int FindSymbol(const WCHAR* str); static int FindSymbol(const WCHAR* str);
struct Parser struct Parser
{ {
Operation opStack[MAX_STACK_SIZE]; Operation opStack[MAX_STACK_SIZE];
double valStack[MAX_STACK_SIZE]; double valStack[MAX_STACK_SIZE];
int opTop; char opTop;
int valTop; char valTop;
int obrDist; int obrDist;
Parser() : opTop(0), valTop(-1), obrDist(2) { opStack[0].type = OP_OBR; } Parser() : opTop(0), valTop(-1), obrDist(2) { opStack[0].type = OP_OBR; }
@ -365,12 +365,10 @@ WCHAR* MathParser::Parse(const WCHAR* formula, CMeasureCalc* calc, double* resul
case TOK_NAME: case TOK_NAME:
{ {
Operation op; Operation op;
int funcnum, namelen = lexer.nameLen;
if (lexer.nameLen <= FUNC_MAX_LEN && if (lexer.nameLen <= FUNC_MAX_LEN &&
((funcnum = GetFunction(lexer.name, lexer.nameLen, (void**)&op.proc)) >= 0)) ((op.funcIndex = GetFunction(lexer.name, lexer.nameLen)) >= 0))
{ {
switch (funcnum) switch (op.funcIndex)
{ {
case FUNC_E: case FUNC_E:
parser.valStack[++parser.valTop] = M_E; parser.valStack[++parser.valTop] = M_E;
@ -427,7 +425,7 @@ static WCHAR* Calc(Parser& parser)
int paramcnt = parser.valTop - op.prevTop; int paramcnt = parser.valTop - op.prevTop;
parser.valTop = op.prevTop; parser.valTop = op.prevTop;
WCHAR* error = (*(MultiArgProc)op.proc)(paramcnt, &parser.valStack[parser.valTop + 1], &res); WCHAR* error = (*(MultiArgProc)g_Functions[op.funcIndex].proc)(paramcnt, &parser.valStack[parser.valTop + 1], &res);
if (error) return error; if (error) return error;
parser.valStack[++parser.valTop] = res; parser.valStack[++parser.valTop] = res;
@ -459,7 +457,7 @@ static WCHAR* Calc(Parser& parser)
} }
else if (op.type == OP_FUNC_ONEARG) else if (op.type == OP_FUNC_ONEARG)
{ {
res = (*(OneArgProc)op.proc)(right); res = (*(OneArgProc)g_Functions[op.funcIndex].proc)(right);
} }
else else
{ {
@ -785,7 +783,7 @@ bool MathParser::IsDelimiter(WCHAR ch)
return type == CH_SYMBOL || type == CH_SEPARAT; return type == CH_SYMBOL || type == CH_SEPARAT;
} }
int GetFunction(const WCHAR* str, size_t len, void** data) int GetFunction(const WCHAR* str, size_t len)
{ {
const int funcCount = sizeof(g_Functions) / sizeof(Function); const int funcCount = sizeof(g_Functions) / sizeof(Function);
for (int i = 0; i < funcCount; ++i) for (int i = 0; i < funcCount; ++i)
@ -793,7 +791,6 @@ int GetFunction(const WCHAR* str, size_t len, void** data)
if (g_Functions[i].length == len && if (g_Functions[i].length == len &&
_wcsnicmp(str, g_Functions[i].name, len) == 0) _wcsnicmp(str, g_Functions[i].name, len) == 0)
{ {
*data = g_Functions[i].proc;
return i; return i;
} }
} }

View File

@ -22,28 +22,23 @@
#define _WIN32_IE 0x0600 #define _WIN32_IE 0x0600
#define _CRTDBG_MAP_ALLOC #define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h> #include <crtdbg.h>
// WINAPI // WINAPI
#include <winsock2.h> #define WIN32_LEAN_AND_MEAN
#include <ws2tcpip.h> #include <ws2tcpip.h>
#include <windows.h> #include <windows.h>
#include <windowsx.h> #include <windowsx.h>
#include <ole2.h>
#include <gdiplus.h> #include <gdiplus.h>
#include <dwmapi.h> #include <dwmapi.h>
#include <comdef.h> #include <comdef.h>
#include <Iphlpapi.h> #include <Iphlpapi.h>
#include <commctrl.h>
#include <shellapi.h>
#include <Mmsystem.h> #include <Mmsystem.h>
#include <Shellapi.h>
#include <shlobj.h> #include <shlobj.h>
#include <shlwapi.h> #include <shlwapi.h>
#include <Wininet.h> #include <Wininet.h>
#include <winperf.h>
#include <Windns.h>
#include <Ipexport.h>
#include <Powrprof.h>
// STL // STL
#include <map> #include <map>
@ -52,22 +47,16 @@
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <list> #include <list>
#include <sstream> #include <algorithm>
#include <ctime> #include <ctime>
#include <cstdlib> #include <cstdlib>
#include <algorithm> #include <cstdio>
#include <cctype> #include <cctype>
#include <cmath>
#include <cerrno>
#include <cassert>
// RUNTIME // RUNTIME
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <io.h>
#include <stdarg.h>
#include <process.h> #include <process.h>
#include <float.h>
#endif #endif