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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
/*

	 _____  __ _____________ _______  ______ ___________
	/     \|  |  \____ \__  \\_  __ \/  ___// __ \_  __ \
   |  Y Y  \  |  /  |_> > __ \|  | \/\___ \\  ___/|  | \/
   |__|_|  /____/|   __(____  /__|  /____  >\___  >__|
		 \/      |__|       \/           \/     \/
   Copyright (C) 2022 Ingo Berg

	Redistribution and use in source and binary forms, with or without modification, are permitted
	provided that the following conditions are met:

	  * Redistributions of source code must retain the above copyright notice, this list of
		conditions and the following disclaimer.
	  * Redistributions in binary form must reproduce the above copyright notice, this list of
		conditions and the following disclaimer in the documentation and/or other materials provided
		with the distribution.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
	FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
	CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
	DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
	IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
	OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "muParserBase.h"
#include "muParserTemplateMagic.h"

//--- Standard includes ------------------------------------------------------------------------
#include <algorithm>
#include <cmath>
#include <memory>
#include <vector>
#include <deque>
#include <sstream>
#include <locale>
#include <cassert>
#include <cctype>

#ifdef MUP_USE_OPENMP

#include <omp.h>

#endif

#if defined(_MSC_VER)
	#pragma warning(push)
	#pragma warning(disable : 26812) 
#endif

using namespace std;

/** \file
	\brief This file contains the basic implementation of the muparser engine.
*/

namespace mu
{
	std::locale ParserBase::s_locale = std::locale(std::locale::classic(), new change_dec_sep<char_type>('.'));

	bool ParserBase::g_DbgDumpCmdCode = false;
	bool ParserBase::g_DbgDumpStack = false;

	//------------------------------------------------------------------------------
	/** \brief Identifiers for built in binary operators.

		When defining custom binary operators with #AddOprt(...) make sure not to choose
		names conflicting with these definitions.
	*/
	const char_type* ParserBase::c_DefaultOprt[] =
	{
	  _T("<="), _T(">="),  _T("!="),
	  _T("=="), _T("<"),   _T(">"),
	  _T("+"),  _T("-"),   _T("*"),
	  _T("/"),  _T("^"),   _T("&&"),
	  _T("||"), _T("="),   _T("("),
	  _T(")"),   _T("?"),  _T(":"), 0
	};

	const int ParserBase::s_MaxNumOpenMPThreads = 16;

	//------------------------------------------------------------------------------
	/** \brief Constructor.
		\param a_szFormula the formula to interpret.
		\throw ParserException if a_szFormula is nullptr.
	*/
	ParserBase::ParserBase()
		: m_pParseFormula(&ParserBase::ParseString)
		, m_vRPN()
		, m_vStringBuf()
		, m_pTokenReader()
		, m_FunDef()
		, m_PostOprtDef()
		, m_InfixOprtDef()
		, m_OprtDef()
		, m_ConstDef()
		, m_StrVarDef()
		, m_VarDef()
		, m_bBuiltInOp(true)
		, m_sNameChars()
		, m_sOprtChars()
		, m_sInfixOprtChars()
		, m_vStackBuffer()
		, m_nFinalResultIdx(0)
	{
		InitTokenReader();
	}

	//---------------------------------------------------------------------------
	/** \brief Copy constructor.

	  The parser can be safely copy constructed but the bytecode is reset during
	  copy construction.
	*/
	ParserBase::ParserBase(const ParserBase& a_Parser)
		: m_pParseFormula(&ParserBase::ParseString)
		, m_vRPN()
		, m_vStringBuf()
		, m_pTokenReader()
		, m_FunDef()
		, m_PostOprtDef()
		, m_InfixOprtDef()
		, m_OprtDef()
		, m_ConstDef()
		, m_StrVarDef()
		, m_VarDef()
		, m_bBuiltInOp(true)
		, m_sNameChars()
		, m_sOprtChars()
		, m_sInfixOprtChars()
	{
		m_pTokenReader.reset(new token_reader_type(this));
		Assign(a_Parser);
	}

	//---------------------------------------------------------------------------
	ParserBase::~ParserBase()
	{}

	//---------------------------------------------------------------------------
	/** \brief Assignment operator.

	  Implemented by calling Assign(a_Parser). Self assignment is suppressed.
	  \param a_Parser Object to copy to this.
	  \return *this
	  \throw nothrow
	*/
	ParserBase& ParserBase::operator=(const ParserBase& a_Parser)
	{
		Assign(a_Parser);
		return *this;
	}

	//---------------------------------------------------------------------------
	/** \brief Copy state of a parser object to this.

	  Clears Variables and Functions of this parser.
	  Copies the states of all internal variables.
	  Resets parse function to string parse mode.

	  \param a_Parser the source object.
	*/
	void ParserBase::Assign(const ParserBase& a_Parser)
	{
		if (&a_Parser == this)
			return;

		// Don't copy bytecode instead cause the parser to create new bytecode
		// by resetting the parse function.
		ReInit();

		m_ConstDef = a_Parser.m_ConstDef;         // Copy user define constants
		m_VarDef = a_Parser.m_VarDef;           // Copy user defined variables
		m_bBuiltInOp = a_Parser.m_bBuiltInOp;
		m_vStringBuf = a_Parser.m_vStringBuf;
		m_vStackBuffer = a_Parser.m_vStackBuffer;
		m_nFinalResultIdx = a_Parser.m_nFinalResultIdx;
		m_StrVarDef = a_Parser.m_StrVarDef;
		m_vStringVarBuf = a_Parser.m_vStringVarBuf;
		m_pTokenReader.reset(a_Parser.m_pTokenReader->Clone(this));

		// Copy function and operator callbacks
		m_FunDef = a_Parser.m_FunDef;             // Copy function definitions
		m_PostOprtDef = a_Parser.m_PostOprtDef;   // post value unary operators
		m_InfixOprtDef = a_Parser.m_InfixOprtDef; // unary operators for infix notation
		m_OprtDef = a_Parser.m_OprtDef;           // binary operators

		m_sNameChars = a_Parser.m_sNameChars;
		m_sOprtChars = a_Parser.m_sOprtChars;
		m_sInfixOprtChars = a_Parser.m_sInfixOprtChars;
	}

	//---------------------------------------------------------------------------
	/** \brief Set the decimal separator.
		\param cDecSep Decimal separator as a character value.
		\sa SetThousandsSep

		By default muparser uses the "C" locale. The decimal separator of this
		locale is overwritten by the one provided here.
	*/
	void ParserBase::SetDecSep(char_type cDecSep)
	{
		char_type cThousandsSep = std::use_facet< change_dec_sep<char_type> >(s_locale).thousands_sep();
		s_locale = std::locale(std::locale("C"), new change_dec_sep<char_type>(cDecSep, cThousandsSep));
	}

	//---------------------------------------------------------------------------
	/** \brief Sets the thousands operator.
		\param cThousandsSep The thousands separator as a character
		\sa SetDecSep

		By default muparser uses the "C" locale. The thousands separator of this
		locale is overwritten by the one provided here.
	*/
	void ParserBase::SetThousandsSep(char_type cThousandsSep)
	{
		char_type cDecSep = std::use_facet< change_dec_sep<char_type> >(s_locale).decimal_point();
		s_locale = std::locale(std::locale("C"), new change_dec_sep<char_type>(cDecSep, cThousandsSep));
	}

	//---------------------------------------------------------------------------
	/** \brief Resets the locale.

	  The default locale used "." as decimal separator, no thousands separator and
	  "," as function argument separator.
	*/
	void ParserBase::ResetLocale()
	{
		s_locale = std::locale(std::locale("C"), new change_dec_sep<char_type>('.'));
		SetArgSep(',');
	}

	//---------------------------------------------------------------------------
	/** \brief Initialize the token reader.

	  Create new token reader object and submit pointers to function, operator,
	  constant and variable definitions.

	  \post m_pTokenReader.get()!=0
	  \throw nothrow
	*/
	void ParserBase::InitTokenReader()
	{
		m_pTokenReader.reset(new token_reader_type(this));
	}

	//---------------------------------------------------------------------------
	/** \brief Reset parser to string parsing mode and clear internal buffers.

		Clear bytecode, reset the token reader.
		\throw nothrow
	*/
	void ParserBase::ReInit() const
	{
		m_pParseFormula = &ParserBase::ParseString;
		m_vStringBuf.clear();
		m_vRPN.clear();
		m_pTokenReader->ReInit();
	}


	void ParserBase::OnDetectVar(string_type* /*pExpr*/, int& /*nStart*/, int& /*nEnd*/)
	{}


	/** \brief Returns a copy of the bytecode of the current expression.
	*/
	const ParserByteCode& ParserBase::GetByteCode() const
	{
		// If a variable factory is defined the bytecode may contain references to implicitely 
		// created variables. 
//		if (m_pTokenReader->HasVarCreator())
//			Error(ecBYTECODE_IMPORT_EXPORT_DISABLED);

		return m_vRPN;
	}


	/** \brief Restore a previously saved bytecode. */
	void ParserBase::SetByteCode(const ParserByteCode& a_ByteCode)
	{
		// If a variable factory is defined the bytecode may contain references to dynamically
		// created variables. 
//		if (m_pTokenReader->HasVarCreator())
//			Error(ecBYTECODE_IMPORT_EXPORT_DISABLED);

		m_vRPN = a_ByteCode;

		// restore expression environment
		string_type expr;
		std::tie(expr, m_vStringBuf) = a_ByteCode.RestoreEnvironment();
		m_pTokenReader->SetFormula(expr);

		m_pParseFormula = &ParserBase::ParseCmdCode;
	}


	/** \brief Returns the version of muparser.
		\param eInfo A flag indicating whether the full version info should be
					 returned or not.

	  Format is as follows: "MAJOR.MINOR (COMPILER_FLAGS)" The COMPILER_FLAGS
	  are returned only if eInfo==pviFULL.
	*/
	string_type ParserBase::GetVersion(EParserVersionInfo eInfo) const
	{
		stringstream_type ss;

		ss << ParserVersion;

		if (eInfo == pviFULL)
		{
			ss << _T(" (") << ParserVersionDate;
			ss << std::dec << _T("; ") << sizeof(void*) * 8 << _T("BIT");

#ifdef _DEBUG
			ss << _T("; DEBUG");
#else 
			ss << _T("; RELEASE");
#endif

#ifdef _UNICODE
			ss << _T("; UNICODE");
#else
#ifdef _MBCS
			ss << _T("; MBCS");
#else
			ss << _T("; ASCII");
#endif
#endif

#ifdef MUP_USE_OPENMP
			ss << _T("; OPENMP");
#endif

			ss << _T(")");
		}

		return ss.str();
	}

	//---------------------------------------------------------------------------
	/** \brief Add a value parsing function.

		When parsing an expression muParser tries to detect values in the expression
		string using different valident callbacks. Thus it's possible to parse
		for hex values, binary values and floating point values.
	*/
	void ParserBase::AddValIdent(identfun_type a_pCallback)
	{
		m_pTokenReader->AddValIdent(a_pCallback);
	}

	//---------------------------------------------------------------------------
	/** \brief Set a function that can create variable pointer for unknown expression variables.
		\param a_pFactory A pointer to the variable factory.
		\param pUserData A user defined context pointer.
	*/
	void ParserBase::SetVarFactory(facfun_type a_pFactory, void* pUserData)
	{
		m_pTokenReader->SetVarCreator(a_pFactory, pUserData);
	}

	//---------------------------------------------------------------------------
	/** \brief Add a function or operator callback to the parser. */
	void ParserBase::AddCallback(
		const string_type& a_strName,
		const ParserCallback& a_Callback,
		funmap_type& a_Storage,
		const char_type* a_szCharSet)
	{
		if (!a_Callback.IsValid())
			Error(ecINVALID_FUN_PTR);

		const funmap_type* pFunMap = &a_Storage;

		// Check for conflicting operator or function names
		if (pFunMap != &m_FunDef && m_FunDef.find(a_strName) != m_FunDef.end())
			Error(ecNAME_CONFLICT, -1, a_strName);

		if (pFunMap != &m_PostOprtDef && m_PostOprtDef.find(a_strName) != m_PostOprtDef.end())
			Error(ecNAME_CONFLICT, -1, a_strName);

		if (pFunMap != &m_InfixOprtDef && pFunMap != &m_OprtDef && m_InfixOprtDef.find(a_strName) != m_InfixOprtDef.end())
			Error(ecNAME_CONFLICT, -1, a_strName);

		if (pFunMap != &m_InfixOprtDef && pFunMap != &m_OprtDef && m_OprtDef.find(a_strName) != m_OprtDef.end())
			Error(ecNAME_CONFLICT, -1, a_strName);

		CheckOprt(a_strName, a_Callback, a_szCharSet);
		a_Storage[a_strName] = a_Callback;
		ReInit();
	}

	//---------------------------------------------------------------------------
	/** \brief Check if a name contains invalid characters.

		\throw ParserException if the name contains invalid characters.
	*/
	void ParserBase::CheckOprt(const string_type& a_sName,
		const ParserCallback& a_Callback,
		const string_type& a_szCharSet) const
	{
		if (!a_sName.length() ||
			(a_sName.find_first_not_of(a_szCharSet) != string_type::npos) ||
			(a_sName[0] >= '0' && a_sName[0] <= '9'))
		{
			switch (a_Callback.GetCode())
			{
			case cmOPRT_POSTFIX: Error(ecINVALID_POSTFIX_IDENT, -1, a_sName); break;
			case cmOPRT_INFIX:   Error(ecINVALID_INFIX_IDENT, -1, a_sName); break;
			default:             Error(ecINVALID_NAME, -1, a_sName);
			}
		}
	}


	/** \brief Check if a name contains invalid characters.
		\throw ParserException if the name contains invalid characters.
	*/
	void ParserBase::CheckName(const string_type& a_sName, const string_type& a_szCharSet) const
	{
		if (!a_sName.length() ||
			(a_sName.find_first_not_of(a_szCharSet) != string_type::npos) ||
			(a_sName[0] >= '0' && a_sName[0] <= '9'))
		{
			Error(ecINVALID_NAME);
		}
	}

	/** \brief Set the formula.
		\param a_strFormula Formula as string_type
		\throw ParserException in case of syntax errors.

		Triggers first time calculation thus the creation of the bytecode and
		scanning of used variables.
	*/
	void ParserBase::SetExpr(const string_type& a_sExpr)
	{
		// Check locale compatibility
		if (m_pTokenReader->GetArgSep() == std::use_facet<numpunct<char_type> >(s_locale).decimal_point())
			Error(ecLOCALE);

		// Check maximum allowed expression length. An arbitrary value small enough so i can debug expressions sent to me
		if (a_sExpr.length() >= MaxLenExpression)
			Error(ecEXPRESSION_TOO_LONG, 0, a_sExpr);

		m_pTokenReader->SetFormula(a_sExpr + _T(" "));
		ReInit();
	}

	//---------------------------------------------------------------------------
	/** \brief Get the default symbols used for the built in operators.
		\sa c_DefaultOprt
	*/
	const char_type** ParserBase::GetOprtDef() const
	{
		return (const char_type**)(&c_DefaultOprt[0]);
	}

	//---------------------------------------------------------------------------
	/** \brief Define the set of valid characters to be used in names of
			   functions, variables, constants.
	*/
	void ParserBase::DefineNameChars(const char_type* a_szCharset)
	{
		m_sNameChars = a_szCharset;
	}

	//---------------------------------------------------------------------------
	/** \brief Define the set of valid characters to be used in names of
			   binary operators and postfix operators.
	*/
	void ParserBase::DefineOprtChars(const char_type* a_szCharset)
	{
		m_sOprtChars = a_szCharset;
	}

	//---------------------------------------------------------------------------
	/** \brief Define the set of valid characters to be used in names of
			   infix operators.
	*/
	void ParserBase::DefineInfixOprtChars(const char_type* a_szCharset)
	{
		m_sInfixOprtChars = a_szCharset;
	}

	//---------------------------------------------------------------------------
	/** \brief Virtual function that defines the characters allowed in name identifiers.
		\sa #ValidOprtChars, #ValidPrefixOprtChars
	*/
	const char_type* ParserBase::ValidNameChars() const
	{
		MUP_ASSERT(m_sNameChars.size());
		return m_sNameChars.c_str();
	}

	//---------------------------------------------------------------------------
	/** \brief Virtual function that defines the characters allowed in operator definitions.
		\sa #ValidNameChars, #ValidPrefixOprtChars
	*/
	const char_type* ParserBase::ValidOprtChars() const
	{
		MUP_ASSERT(m_sOprtChars.size());
		return m_sOprtChars.c_str();
	}

	//---------------------------------------------------------------------------
	/** \brief Virtual function that defines the characters allowed in infix operator definitions.
		\sa #ValidNameChars, #ValidOprtChars
	*/
	const char_type* ParserBase::ValidInfixOprtChars() const
	{
		MUP_ASSERT(m_sInfixOprtChars.size());
		return m_sInfixOprtChars.c_str();
	}

	//---------------------------------------------------------------------------
	/** \brief Add a user defined operator.
		\post Will reset the Parser to string parsing mode.
	*/
	void ParserBase::DefinePostfixOprt(const string_type& a_sName, fun_type1 a_pFun, bool a_bAllowOpt)
	{
		if (a_sName.length() > MaxLenIdentifier)
			Error(ecIDENTIFIER_TOO_LONG);

		AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, prPOSTFIX, cmOPRT_POSTFIX), m_PostOprtDef, ValidOprtChars());
	}

	//---------------------------------------------------------------------------
	/** \brief Initialize user defined functions.

	  Calls the virtual functions InitFun(), InitConst() and InitOprt().
	*/
	void ParserBase::Init()
	{
		InitCharSets();
		InitFun();
		InitConst();
		InitOprt();
	}

	//---------------------------------------------------------------------------
	/** \brief Add a user defined operator.
		\post Will reset the Parser to string parsing mode.
		\param [in] a_sName  operator Identifier
		\param [in] a_pFun  Operator callback function
		\param [in] a_iPrec  Operator Precedence (default=prSIGN)
		\param [in] a_bAllowOpt  True if operator is volatile (default=false)
		\sa EPrec
	*/
	void ParserBase::DefineInfixOprt(const string_type& a_sName, fun_type1 a_pFun, int a_iPrec, bool a_bAllowOpt)
	{
		if (a_sName.length() > MaxLenIdentifier)
			Error(ecIDENTIFIER_TOO_LONG);

		AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, a_iPrec, cmOPRT_INFIX), m_InfixOprtDef, ValidInfixOprtChars());
	}


	//---------------------------------------------------------------------------
	/** \brief Define a binary operator.
		\param [in] a_sName The identifier of the operator.
		\param [in] a_pFun Pointer to the callback function.
		\param [in] a_iPrec Precedence of the operator.
		\param [in] a_eAssociativity The associativity of the operator.
		\param [in] a_bAllowOpt If this is true the operator may be optimized away.

		Adds a new Binary operator the the parser instance.
	*/
	void ParserBase::DefineOprt(const string_type& a_sName, fun_type2 a_pFun, unsigned a_iPrec, EOprtAssociativity a_eAssociativity, bool a_bAllowOpt)
	{
		if (a_sName.length() > MaxLenIdentifier)
			Error(ecIDENTIFIER_TOO_LONG);

		// Check for conflicts with built in operator names
		for (int i = 0; m_bBuiltInOp && i < cmENDIF; ++i)
		{
			if (a_sName == string_type(c_DefaultOprt[i]))
			{
				Error(ecBUILTIN_OVERLOAD, -1, a_sName);
			}
		}

		AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, a_iPrec, a_eAssociativity), m_OprtDef, ValidOprtChars());
	}

	//---------------------------------------------------------------------------
	/** \brief Define a new string constant.
		\param [in] a_strName The name of the constant.
		\param [in] a_strVal the value of the constant.
	*/
	void ParserBase::DefineStrConst(const string_type& a_strName, const string_type& a_strVal)
	{
		// Test if a constant with that names already exists
		if (m_StrVarDef.find(a_strName) != m_StrVarDef.end())
			Error(ecNAME_CONFLICT);

		CheckName(a_strName, ValidNameChars());

		m_vStringVarBuf.push_back(a_strVal);                // Store variable string in internal buffer
		m_StrVarDef[a_strName] = m_vStringVarBuf.size() - 1;  // bind buffer index to variable name

		ReInit();
	}

	//---------------------------------------------------------------------------
	/** \brief Add a user defined variable.
		\param [in] a_sName the variable name
		\param [in] a_pVar A pointer to the variable value.
		\post Will reset the Parser to string parsing mode.
		\throw ParserException in case the name contains invalid signs or a_pVar is nullptr.
	*/
	void ParserBase::DefineVar(const string_type& a_sName, value_type* a_pVar)
	{
		if (a_pVar == 0)
			Error(ecINVALID_VAR_PTR);

		if (a_sName.length() > MaxLenIdentifier)
			Error(ecIDENTIFIER_TOO_LONG);

		// Test if a constant with that names already exists
		if (m_ConstDef.find(a_sName) != m_ConstDef.end())
			Error(ecNAME_CONFLICT);

		CheckName(a_sName, ValidNameChars());
		m_VarDef[a_sName] = a_pVar;
		ReInit();
	}

	//---------------------------------------------------------------------------
	/** \brief Add a user defined constant.
		\param [in] a_sName The name of the constant.
		\param [in] a_fVal the value of the constant.
		\post Will reset the Parser to string parsing mode.
		\throw ParserException in case the name contains invalid signs.
	*/
	void ParserBase::DefineConst(const string_type& a_sName, value_type a_fVal)
	{
		if (a_sName.length() > MaxLenIdentifier)
			Error(ecIDENTIFIER_TOO_LONG);

		CheckName(a_sName, ValidNameChars());
		m_ConstDef[a_sName] = a_fVal;
		ReInit();
	}

	//---------------------------------------------------------------------------
	/** \brief Get operator priority.
		\throw ParserException if a_Oprt is no operator code
	*/
	int ParserBase::GetOprtPrecedence(const token_type& a_Tok) const
	{
		switch (a_Tok.GetCode())
		{
			// built in operators
		case cmEND:      return -5;
		case cmARG_SEP:  return -4;
		case cmASSIGN:   return -1;
		case cmELSE:
		case cmIF:       return  0;
		case cmLAND:     return  prLAND;
		case cmLOR:      return  prLOR;
		case cmLT:
		case cmGT:
		case cmLE:
		case cmGE:
		case cmNEQ:
		case cmEQ:       return  prCMP;
		case cmADD:
		case cmSUB:      return  prADD_SUB;
		case cmMUL:
		case cmDIV:      return  prMUL_DIV;
		case cmPOW:      return  prPOW;

		// user defined binary operators
		case cmOPRT_INFIX:
		case cmOPRT_BIN: return a_Tok.GetPri();
		default:  
			throw exception_type(ecINTERNAL_ERROR, 5, _T(""));
		}
	}

	//---------------------------------------------------------------------------
	/** \brief Get operator priority.
		\throw ParserException if a_Oprt is no operator code
	*/
	EOprtAssociativity ParserBase::GetOprtAssociativity(const token_type& a_Tok) const
	{
		switch (a_Tok.GetCode())
		{
		case cmASSIGN:
		case cmLAND:
		case cmLOR:
		case cmLT:
		case cmGT:
		case cmLE:
		case cmGE:
		case cmNEQ:
		case cmEQ:
		case cmADD:
		case cmSUB:
		case cmMUL:
		case cmDIV:      return oaLEFT;
		case cmPOW:      return oaRIGHT;
		case cmOPRT_BIN: return a_Tok.GetAssociativity();
		default:         return oaNONE;
		}
	}

	//---------------------------------------------------------------------------
	/** \brief Return a map containing the used variables only. */
	const varmap_type& ParserBase::GetUsedVar() const
	{
		try
		{
			m_pTokenReader->IgnoreUndefVar(true);
			CreateRPN(); // try to create bytecode, but don't use it for any further calculations since it
						 // may contain references to nonexisting variables.
			m_pParseFormula = &ParserBase::ParseString;
			m_pTokenReader->IgnoreUndefVar(false);
		}
		catch (exception_type& /*e*/)
		{
			// Make sure to stay in string parse mode, don't call ReInit()
			// because it deletes the array with the used variables
			m_pParseFormula = &ParserBase::ParseString;
			m_pTokenReader->IgnoreUndefVar(false);
			throw;
		}

		return m_pTokenReader->GetUsedVar();
	}

	//---------------------------------------------------------------------------
	/** \brief Return a map containing the used variables only. */
	const varmap_type& ParserBase::GetVar() const
	{
		return m_VarDef;
	}

	//---------------------------------------------------------------------------
	/** \brief Return a map containing all parser constants. */
	const valmap_type& ParserBase::GetConst() const
	{
		return m_ConstDef;
	}

	//---------------------------------------------------------------------------
	/** \brief Return prototypes of all parser functions.
		\return #m_FunDef
		\sa FunProt
		\throw nothrow

		The return type is a map of the public type #funmap_type containing the prototype
		definitions for all numerical parser functions. String functions are not part of
		this map. The Prototype definition is encapsulated in objects of the class FunProt
		one per parser function each associated with function names via a map construct.
	*/
	const funmap_type& ParserBase::GetFunDef() const
	{
		return m_FunDef;
	}

	//---------------------------------------------------------------------------
	/** \brief Retrieve the formula. */
	const string_type& ParserBase::GetExpr() const
	{
		return m_pTokenReader->GetExpr();
	}

	//---------------------------------------------------------------------------
	/** \brief Execute a function that takes a single string argument.
		\param a_FunTok Function token.
		\throw exception_type If the function token is not a string function
	*/
	ParserBase::token_type ParserBase::ApplyStrFunc(
		const token_type& a_FunTok,
		const std::vector<token_type>& a_vArg) const
	{
		if (a_vArg.back().GetCode() != cmSTRING)
			Error(ecSTRING_EXPECTED, m_pTokenReader->GetPos(), a_FunTok.GetAsString());

		token_type  valTok;
		generic_callable_type pFunc = a_FunTok.GetFuncAddr();
		MUP_ASSERT(pFunc);

		try
		{
			// Check function arguments; write dummy value into valtok to represent the result
			switch (a_FunTok.GetArgCount())
			{
			case 0: valTok.SetVal(1); a_vArg[0].GetAsString();  break;
			case 1: valTok.SetVal(1); a_vArg[1].GetAsString();  a_vArg[0].GetVal();  break;
			case 2: valTok.SetVal(1); a_vArg[2].GetAsString();  a_vArg[1].GetVal();  a_vArg[0].GetVal();  break;
			case 3: valTok.SetVal(1); a_vArg[3].GetAsString();  a_vArg[2].GetVal();  a_vArg[1].GetVal();  a_vArg[0].GetVal();  break;
			case 4: valTok.SetVal(1); a_vArg[4].GetAsString();  a_vArg[3].GetVal();  a_vArg[2].GetVal();  a_vArg[1].GetVal();  a_vArg[0].GetVal();  break;
			case 5: valTok.SetVal(1); a_vArg[5].GetAsString();  a_vArg[4].GetVal();  a_vArg[3].GetVal();  a_vArg[2].GetVal();  a_vArg[1].GetVal(); a_vArg[0].GetVal(); break;
			default: Error(ecINTERNAL_ERROR);
			}
		}
		catch (ParserError&)
		{
			Error(ecVAL_EXPECTED, m_pTokenReader->GetPos(), a_FunTok.GetAsString());
		}

		// string functions won't be optimized
		m_vRPN.AddStrFun(pFunc, a_FunTok.GetArgCount(), a_vArg.back().GetIdx());

		// Push dummy value representing the function result to the stack
		return valTok;
	}

	//---------------------------------------------------------------------------
	/** \brief Apply a function token.
		\param iArgCount Number of Arguments actually gathered used only for multiarg functions.
		\post The result is pushed to the value stack
		\post The function token is removed from the stack
		\throw exception_type if Argument count does not match function requirements.
	*/
	void ParserBase::ApplyFunc(std::stack<token_type>& a_stOpt, std::stack<token_type>& a_stVal, int a_iArgCount) const
	{
		MUP_ASSERT(m_pTokenReader.get());

		// Operator stack empty or does not contain tokens with callback functions
		if (a_stOpt.empty() || a_stOpt.top().GetFuncAddr() == 0)
			return;

		token_type funTok = a_stOpt.top();
		a_stOpt.pop();
		MUP_ASSERT(funTok.GetFuncAddr() != nullptr);

		// Binary operators must rely on their internal operator number
		// since counting of operators relies on commas for function arguments
		// binary operators do not have commas in their expression
		int iArgCount = (funTok.GetCode() == cmOPRT_BIN) ? funTok.GetArgCount() : a_iArgCount;

		// determine how many parameters the function needs. To remember iArgCount includes the 
		// string parameter whilst GetArgCount() counts only numeric parameters.
		int iArgRequired = funTok.GetArgCount() + ((funTok.GetType() == tpSTR) ? 1 : 0);

		// That's the number of numerical parameters
		int iArgNumerical = iArgCount - ((funTok.GetType() == tpSTR) ? 1 : 0);

		if (funTok.GetCode() == cmFUNC_STR && iArgCount - iArgNumerical > 1)
			Error(ecINTERNAL_ERROR);

		if (funTok.GetArgCount() >= 0 && iArgCount > iArgRequired)
			Error(ecTOO_MANY_PARAMS, m_pTokenReader->GetPos() - 1, funTok.GetAsString());

		if (funTok.GetCode() != cmOPRT_BIN && iArgCount < iArgRequired)
			Error(ecTOO_FEW_PARAMS, m_pTokenReader->GetPos() - 1, funTok.GetAsString());

		if (funTok.GetCode() == cmFUNC_STR && iArgCount > iArgRequired)
			Error(ecTOO_MANY_PARAMS, m_pTokenReader->GetPos() - 1, funTok.GetAsString());

		// Collect the numeric function arguments from the value stack and store them
		// in a vector
		std::vector<token_type> stArg;
		for (int i = 0; i < iArgNumerical; ++i)
		{
			if (a_stVal.empty())
				Error(ecINTERNAL_ERROR, m_pTokenReader->GetPos(), funTok.GetAsString());

			stArg.push_back(a_stVal.top());
			a_stVal.pop();

			if (stArg.back().GetType() == tpSTR && funTok.GetType() != tpSTR)
				Error(ecVAL_EXPECTED, m_pTokenReader->GetPos(), funTok.GetAsString());
		}

		switch (funTok.GetCode())
		{
		case  cmFUNC_STR:
			if (a_stVal.empty())
				Error(ecINTERNAL_ERROR, m_pTokenReader->GetPos(), funTok.GetAsString());

			stArg.push_back(a_stVal.top());
			a_stVal.pop();

			if (stArg.back().GetType() == tpSTR && funTok.GetType() != tpSTR)
				Error(ecVAL_EXPECTED, m_pTokenReader->GetPos(), funTok.GetAsString());

			ApplyStrFunc(funTok, stArg);
			break;

		case  cmFUNC_BULK:
			m_vRPN.AddBulkFun(funTok.GetFuncAddr(), (int)stArg.size());
			break;

		case  cmOPRT_BIN:
		case  cmOPRT_POSTFIX:
		case  cmOPRT_INFIX:
		case  cmFUNC:
			if (funTok.GetArgCount() == -1 && iArgCount == 0)
				Error(ecTOO_FEW_PARAMS, m_pTokenReader->GetPos(), funTok.GetAsString());

			m_vRPN.AddFun(funTok.GetFuncAddr(), (funTok.GetArgCount() == -1) ? -iArgNumerical : iArgNumerical, funTok.IsOptimizable());
			break;
		default:
			break;
		}

		// Push dummy value representing the function result to the stack
		token_type token;
		token.SetVal(1);
		a_stVal.push(token);
	}

	//---------------------------------------------------------------------------
	void ParserBase::ApplyIfElse(std::stack<token_type>& a_stOpt, std::stack<token_type>& a_stVal) const
	{
		// Check if there is an if Else clause to be calculated
		while (a_stOpt.size() && a_stOpt.top().GetCode() == cmELSE)
		{
			MUP_ASSERT(!a_stOpt.empty())
			token_type opElse = a_stOpt.top();
			a_stOpt.pop();

			// Take the value associated with the else branch from the value stack
			MUP_ASSERT(!a_stVal.empty());
			token_type vVal2 = a_stVal.top();
			if (vVal2.GetType() != tpDBL)
				Error(ecUNEXPECTED_STR, m_pTokenReader->GetPos());
			
			a_stVal.pop();

			// it then else is a ternary operator Pop all three values from the value s
			// tack and just return the right value
			MUP_ASSERT(!a_stVal.empty());
			token_type vVal1 = a_stVal.top();
			if (vVal1.GetType() != tpDBL)
				Error(ecUNEXPECTED_STR, m_pTokenReader->GetPos());

			a_stVal.pop();

			MUP_ASSERT(!a_stVal.empty());
			token_type vExpr = a_stVal.top();
			a_stVal.pop();

			a_stVal.push((vExpr.GetVal() != 0) ? vVal1 : vVal2);

			token_type opIf = a_stOpt.top();
			a_stOpt.pop();

			MUP_ASSERT(opElse.GetCode() == cmELSE);

			if (opIf.GetCode() != cmIF)
				Error(ecMISPLACED_COLON, m_pTokenReader->GetPos());

			m_vRPN.AddIfElse(cmENDIF);
		} // while pending if-else-clause found
	}

	//---------------------------------------------------------------------------
	/** \brief Performs the necessary steps to write code for
			   the execution of binary operators into the bytecode.
	*/
	void ParserBase::ApplyBinOprt(std::stack<token_type>& a_stOpt, std::stack<token_type>& a_stVal) const
	{
		// is it a user defined binary operator?
		if (a_stOpt.top().GetCode() == cmOPRT_BIN)
		{
			ApplyFunc(a_stOpt, a_stVal, 2);
		}
		else
		{
			if (a_stVal.size() < 2)
				Error(ecINTERNAL_ERROR, m_pTokenReader->GetPos(), _T("ApplyBinOprt: not enough values in value stack!"));

			token_type valTok1 = a_stVal.top();
			a_stVal.pop();

			token_type valTok2 = a_stVal.top();
			a_stVal.pop();

			token_type optTok = a_stOpt.top();
			a_stOpt.pop();

			token_type resTok;

			if (valTok1.GetType() != valTok2.GetType() ||
				(valTok1.GetType() == tpSTR && valTok2.GetType() == tpSTR))
				Error(ecOPRT_TYPE_CONFLICT, m_pTokenReader->GetPos(), optTok.GetAsString());

			if (optTok.GetCode() == cmASSIGN)
			{
				if (valTok2.GetCode() != cmVAR)
					Error(ecUNEXPECTED_OPERATOR, -1, _T("="));

				m_vRPN.AddAssignOp(valTok2.GetVar());
			}
			else
				m_vRPN.AddOp(optTok.GetCode());

			resTok.SetVal(1);
			a_stVal.push(resTok);
		}
	}

	//---------------------------------------------------------------------------
	/** \brief Apply a binary operator.
		\param a_stOpt The operator stack
		\param a_stVal The value stack
	*/
	void ParserBase::ApplyRemainingOprt(std::stack<token_type>& stOpt, std::stack<token_type>& stVal) const
	{
		while (stOpt.size() &&
			stOpt.top().GetCode() != cmBO &&
			stOpt.top().GetCode() != cmIF)
		{
			token_type tok = stOpt.top();
			switch (tok.GetCode())
			{
			case cmOPRT_INFIX:
			case cmOPRT_BIN:
			case cmLE:
			case cmGE:
			case cmNEQ:
			case cmEQ:
			case cmLT:
			case cmGT:
			case cmADD:
			case cmSUB:
			case cmMUL:
			case cmDIV:
			case cmPOW:
			case cmLAND:
			case cmLOR:
			case cmASSIGN:
				if (stOpt.top().GetCode() == cmOPRT_INFIX)
					ApplyFunc(stOpt, stVal, 1);
				else
					ApplyBinOprt(stOpt, stVal);
				break;

			case cmELSE:
				ApplyIfElse(stOpt, stVal);
				break;

			default:
				Error(ecINTERNAL_ERROR);
			}
		}
	}

	//---------------------------------------------------------------------------
	/** \brief Parse the command code.
		\sa ParseString(...)

		Command code contains precalculated stack positions of the values and the
		associated operators. The Stack is filled beginning from index one the
		value at index zero is not used at all.
	*/
	value_type ParserBase::ParseCmdCode() const
	{
		return ParseCmdCodeBulk(0, 0);
	}

	value_type ParserBase::ParseCmdCodeShort() const
	{
		const SToken *const tok = m_vRPN.GetBase();
		value_type buf;

		switch (tok->Cmd)
		{
		case cmVAL:		
			return tok->Val.data2;

		case cmVAR:		
			return *tok->Val.ptr;

		case cmVARMUL:	
			return *tok->Val.ptr * tok->Val.data + tok->Val.data2;

		case cmVARPOW2: 
			buf = *(tok->Val.ptr);
			return buf * buf;

		case  cmVARPOW3: 				
			buf = *(tok->Val.ptr);
			return buf * buf * buf;

		case  cmVARPOW4: 				
			buf = *(tok->Val.ptr);
			return buf * buf * buf * buf;

		// numerical function without any argument
		case cmFUNC:
			return tok->Fun.cb.call_fun<0>();

		// String function without a numerical argument
		case cmFUNC_STR:
			return tok->Fun.cb.call_strfun<1>(m_vStringBuf[0].c_str());

		default:
			throw ParserError(ecINTERNAL_ERROR);
		}
	}

	//---------------------------------------------------------------------------
	/** \brief Evaluate the RPN.
		\param nOffset The offset added to variable addresses (for bulk mode)
		\param nThreadID OpenMP Thread id of the calling thread
	*/
	value_type ParserBase::ParseCmdCodeBulk(int nOffset, int nThreadID) const
	{
		assert(nThreadID <= s_MaxNumOpenMPThreads);

		// Note: The check for nOffset==0 and nThreadID here is not necessary but 
		//       brings a minor performance gain when not in bulk mode.
		value_type *stack = ((nOffset == 0) && (nThreadID == 0)) ? &m_vStackBuffer[0] : &m_vStackBuffer[nThreadID * (m_vStackBuffer.size() / s_MaxNumOpenMPThreads)];
		value_type buf;
		int sidx(0);
		for (const SToken* pTok = m_vRPN.GetBase(); pTok->Cmd != cmEND; ++pTok)
		{
			switch (pTok->Cmd)
			{
			// built in binary operators
			case  cmLE:   --sidx; stack[sidx] = stack[sidx] <= stack[sidx + 1]; continue;
			case  cmGE:   --sidx; stack[sidx] = stack[sidx] >= stack[sidx + 1]; continue;
			case  cmNEQ:  --sidx; stack[sidx] = stack[sidx] != stack[sidx + 1]; continue;
			case  cmEQ:   --sidx; stack[sidx] = stack[sidx] == stack[sidx + 1]; continue;
			case  cmLT:   --sidx; stack[sidx] = stack[sidx] < stack[sidx + 1];  continue;
			case  cmGT:   --sidx; stack[sidx] = stack[sidx] > stack[sidx + 1];  continue;
			case  cmADD:  --sidx; stack[sidx] += stack[1 + sidx]; continue;
			case  cmSUB:  --sidx; stack[sidx] -= stack[1 + sidx]; continue;
			case  cmMUL:  --sidx; stack[sidx] *= stack[1 + sidx]; continue;
			case  cmDIV:  --sidx;
				stack[sidx] /= stack[1 + sidx];
				continue;

			case  cmPOW:
				--sidx; stack[sidx] = MathImpl<value_type>::Pow(stack[sidx], stack[1 + sidx]);
				continue;

			case  cmLAND: --sidx; stack[sidx] = stack[sidx] && stack[sidx + 1]; continue;
			case  cmLOR:  --sidx; stack[sidx] = stack[sidx] || stack[sidx + 1]; continue;

			case  cmASSIGN:
				// Bugfix for Bulkmode:
				// for details see:
				//    https://groups.google.com/forum/embed/?place=forum/muparser-dev&showsearch=true&showpopout=true&showtabs=false&parenturl=http://muparser.beltoforion.de/mup_forum.html&afterlogin&pli=1#!topic/muparser-dev/szgatgoHTws
				--sidx; 
				stack[sidx] = *(pTok->Oprt.ptr + nOffset) = stack[sidx + 1]; 
				continue;
				// original code:
				//--sidx; Stack[sidx] = *pTok->Oprt.ptr = Stack[sidx+1]; continue;

			case  cmIF:
				if (stack[sidx--] == 0)
				{
					MUP_ASSERT(sidx >= 0);
					pTok += pTok->Oprt.offset;
				}
				continue;

			case  cmELSE:
				pTok += pTok->Oprt.offset;
				continue;

			case  cmENDIF:
				continue;

				// value and variable tokens
			case  cmVAR:    stack[++sidx] = *(pTok->Val.ptr + nOffset);  continue;
			case  cmVAL:    stack[++sidx] = pTok->Val.data2;  continue;

			case  cmVARPOW2: buf = *(pTok->Val.ptr + nOffset);
				stack[++sidx] = buf * buf;
				continue;

			case  cmVARPOW3: buf = *(pTok->Val.ptr + nOffset);
				stack[++sidx] = buf * buf * buf;
				continue;

			case  cmVARPOW4: buf = *(pTok->Val.ptr + nOffset);
				stack[++sidx] = buf * buf * buf * buf;
				continue;

			case  cmVARMUL:  
				stack[++sidx] = *(pTok->Val.ptr + nOffset) * pTok->Val.data + pTok->Val.data2;
				continue;

				// Next is treatment of numeric functions
			case  cmFUNC:
			{
				int iArgCount = pTok->Fun.argc;

				// switch according to argument count
				switch (iArgCount)
				{
				case 0: sidx += 1; stack[sidx] = pTok->Fun.cb.call_fun<0 >(); continue;
				case 1:            stack[sidx] = pTok->Fun.cb.call_fun<1 >(stack[sidx]);   continue;
				case 2: sidx -= 1; stack[sidx] = pTok->Fun.cb.call_fun<2 >(stack[sidx], stack[sidx + 1]); continue;
				case 3: sidx -= 2; stack[sidx] = pTok->Fun.cb.call_fun<3 >(stack[sidx], stack[sidx + 1], stack[sidx + 2]); continue;
				case 4: sidx -= 3; stack[sidx] = pTok->Fun.cb.call_fun<4 >(stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3]); continue;
				case 5: sidx -= 4; stack[sidx] = pTok->Fun.cb.call_fun<5 >(stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4]); continue;
				case 6: sidx -= 5; stack[sidx] = pTok->Fun.cb.call_fun<6 >(stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5]); continue;
				case 7: sidx -= 6; stack[sidx] = pTok->Fun.cb.call_fun<7 >(stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5], stack[sidx + 6]); continue;
				case 8: sidx -= 7; stack[sidx] = pTok->Fun.cb.call_fun<8 >(stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5], stack[sidx + 6], stack[sidx + 7]); continue;
				case 9: sidx -= 8; stack[sidx] = pTok->Fun.cb.call_fun<9 >(stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5], stack[sidx + 6], stack[sidx + 7], stack[sidx + 8]); continue;
				case 10:sidx -= 9; stack[sidx] = pTok->Fun.cb.call_fun<10>(stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5], stack[sidx + 6], stack[sidx + 7], stack[sidx + 8], stack[sidx + 9]); continue;
				default:
					// function with variable arguments store the number as a negative value
					if (iArgCount > 0)
						Error(ecINTERNAL_ERROR, -1);

					sidx -= -iArgCount - 1;

					// <ibg 2020-06-08> From oss-fuzz. Happend when Multiarg functions and if-then-else are used incorrectly.
					// Expressions where this was observed:
					//		sum(0?1,2,3,4,5:6)			-> fixed
					//		avg(0>3?4:(""),0^3?4:(""))
					//
					// The final result normally lieas at position 1. If sixd is smaller there is something wrong.
					if (sidx <= 0)
						Error(ecINTERNAL_ERROR, -1);
					// </ibg>

					stack[sidx] = pTok->Fun.cb.call_multfun(&stack[sidx], -iArgCount);
					continue;
				}
			}

			// Next is treatment of string functions
			case  cmFUNC_STR:
			{
				sidx -= pTok->Fun.argc - 1;

				// The index of the string argument in the string table
				int iIdxStack = pTok->Fun.idx;
				if (iIdxStack < 0 || iIdxStack >= (int)m_vStringBuf.size())
					Error(ecINTERNAL_ERROR, m_pTokenReader->GetPos());

				switch (pTok->Fun.argc)  // switch according to argument count
				{
				case 0: stack[sidx] = pTok->Fun.cb.call_strfun<1>(m_vStringBuf[iIdxStack].c_str()); continue;
				case 1: stack[sidx] = pTok->Fun.cb.call_strfun<2>(m_vStringBuf[iIdxStack].c_str(), stack[sidx]); continue;
				case 2: stack[sidx] = pTok->Fun.cb.call_strfun<3>(m_vStringBuf[iIdxStack].c_str(), stack[sidx], stack[sidx + 1]); continue;
				case 3: stack[sidx] = pTok->Fun.cb.call_strfun<4>(m_vStringBuf[iIdxStack].c_str(), stack[sidx], stack[sidx + 1], stack[sidx + 2]); continue;
				case 4: stack[sidx] = pTok->Fun.cb.call_strfun<5>(m_vStringBuf[iIdxStack].c_str(), stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3]); continue;
				case 5: stack[sidx] = pTok->Fun.cb.call_strfun<6>(m_vStringBuf[iIdxStack].c_str(), stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4]); continue;
				}

				continue;
			}

			case  cmFUNC_BULK:
			{
				int iArgCount = pTok->Fun.argc;

				// switch according to argument count
				switch (iArgCount)
				{
				case 0: sidx += 1; stack[sidx] = pTok->Fun.cb.call_bulkfun<0 >(nOffset, nThreadID); continue;
				case 1:            stack[sidx] = pTok->Fun.cb.call_bulkfun<1 >(nOffset, nThreadID, stack[sidx]); continue;
				case 2: sidx -= 1; stack[sidx] = pTok->Fun.cb.call_bulkfun<2 >(nOffset, nThreadID, stack[sidx], stack[sidx + 1]); continue;
				case 3: sidx -= 2; stack[sidx] = pTok->Fun.cb.call_bulkfun<3 >(nOffset, nThreadID, stack[sidx], stack[sidx + 1], stack[sidx + 2]); continue;
				case 4: sidx -= 3; stack[sidx] = pTok->Fun.cb.call_bulkfun<4 >(nOffset, nThreadID, stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3]); continue;
				case 5: sidx -= 4; stack[sidx] = pTok->Fun.cb.call_bulkfun<5 >(nOffset, nThreadID, stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4]); continue;
				case 6: sidx -= 5; stack[sidx] = pTok->Fun.cb.call_bulkfun<6 >(nOffset, nThreadID, stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5]); continue;
				case 7: sidx -= 6; stack[sidx] = pTok->Fun.cb.call_bulkfun<7 >(nOffset, nThreadID, stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5], stack[sidx + 6]); continue;
				case 8: sidx -= 7; stack[sidx] = pTok->Fun.cb.call_bulkfun<8 >(nOffset, nThreadID, stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5], stack[sidx + 6], stack[sidx + 7]); continue;
				case 9: sidx -= 8; stack[sidx] = pTok->Fun.cb.call_bulkfun<9 >(nOffset, nThreadID, stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5], stack[sidx + 6], stack[sidx + 7], stack[sidx + 8]); continue;
				case 10:sidx -= 9; stack[sidx] = pTok->Fun.cb.call_bulkfun<10>(nOffset, nThreadID, stack[sidx], stack[sidx + 1], stack[sidx + 2], stack[sidx + 3], stack[sidx + 4], stack[sidx + 5], stack[sidx + 6], stack[sidx + 7], stack[sidx + 8], stack[sidx + 9]); continue;
				default:
					throw exception_type(ecINTERNAL_ERROR, 2, _T(""));
				}
			}

			default:
				throw exception_type(ecINTERNAL_ERROR, 3, _T(""));
			} // switch CmdCode
		} // for all bytecode tokens

		return stack[m_nFinalResultIdx];
	}

	//---------------------------------------------------------------------------
	void ParserBase::CreateRPN() const
	{
		if (!m_pTokenReader->GetExpr().length())
			Error(ecUNEXPECTED_EOF, 0);

		std::stack<token_type> stOpt, stVal;
		std::stack<int> stArgCount;
		token_type opta, opt;  // for storing operators
		token_type val, tval;  // for storing value
		int ifElseCounter = 0;

		ReInit();

		// The outermost counter counts the number of separated items
		// such as in "a=10,b=20,c=c+a"
		stArgCount.push(1);

		for (;;)
		{
			opt = m_pTokenReader->ReadNextToken();

			switch (opt.GetCode())
			{
			//
			// Next three are different kind of value entries
			//
			case cmSTRING:
				if (stOpt.empty())
					Error(ecSTR_RESULT, m_pTokenReader->GetPos(), opt.GetAsString());

				opt.SetIdx((int)m_vStringBuf.size());      // Assign buffer index to token 
				stVal.push(opt);
				m_vStringBuf.push_back(opt.GetAsString()); // Store string in internal buffer
				break;

			case cmVAR:
				stVal.push(opt);
				m_vRPN.AddVar(static_cast<value_type*>(opt.GetVar()));
				break;

			case cmVAL:
				stVal.push(opt);
				m_vRPN.AddVal(opt.GetVal());
				break;

			case cmELSE:
				if (stArgCount.empty())
					Error(ecMISPLACED_COLON, m_pTokenReader->GetPos());

				if (stArgCount.top() > 1)
					Error(ecUNEXPECTED_ARG_SEP, m_pTokenReader->GetPos());

				stArgCount.pop();

				ifElseCounter--;
				if (ifElseCounter < 0)
					Error(ecMISPLACED_COLON, m_pTokenReader->GetPos());

				ApplyRemainingOprt(stOpt, stVal);
				m_vRPN.AddIfElse(cmELSE);
				stOpt.push(opt);
				break;

			case cmARG_SEP:
				if (!stOpt.empty() && stOpt.top().GetCode() == cmIF)
					Error(ecUNEXPECTED_ARG_SEP, m_pTokenReader->GetPos());

				if (stArgCount.empty())
					Error(ecUNEXPECTED_ARG_SEP, m_pTokenReader->GetPos());

				++stArgCount.top();
				// Falls through.
				// intentional (no break!)

			case cmEND:
				ApplyRemainingOprt(stOpt, stVal);
				break;

			case cmBC:
			{
				// The argument count for parameterless functions is zero
				// by default an opening bracket sets parameter count to 1
				// in preparation of arguments to come. If the last token
				// was an opening bracket we know better...
				if (opta.GetCode() == cmBO)
					--stArgCount.top();

				ApplyRemainingOprt(stOpt, stVal);

				// Check if the bracket content has been evaluated completely
				if (stOpt.size() && stOpt.top().GetCode() == cmBO)
				{
					// if opt is ")" and opta is "(" the bracket has been evaluated, now its time to check
					// if there is either a function or a sign pending
					// neither the opening nor the closing bracket will be pushed back to
					// the operator stack
					// Check if a function is standing in front of the opening bracket, 
					// if yes evaluate it afterwards check for infix operators
					MUP_ASSERT(stArgCount.size());
					int iArgCount = stArgCount.top();
					stArgCount.pop();

					stOpt.pop(); // Take opening bracket from stack

					if (iArgCount > 1 && (stOpt.size() == 0 ||
						(stOpt.top().GetCode() != cmFUNC &&
							stOpt.top().GetCode() != cmFUNC_BULK &&
							stOpt.top().GetCode() != cmFUNC_STR)))
						Error(ecUNEXPECTED_ARG, m_pTokenReader->GetPos());

					// The opening bracket was popped from the stack now check if there
					// was a function before this bracket
					if (stOpt.size() &&
						stOpt.top().GetCode() != cmOPRT_INFIX &&
						stOpt.top().GetCode() != cmOPRT_BIN &&
						stOpt.top().GetFuncAddr() != 0)
					{
						ApplyFunc(stOpt, stVal, iArgCount);
					}
				}
			} // if bracket content is evaluated
			break;

			//
			// Next are the binary operator entries
			//
			case cmIF:
				ifElseCounter++;
				stArgCount.push(1);
				// Falls through.
				// intentional (no break!)

			case cmLAND:
			case cmLOR:
			case cmLT:
			case cmGT:
			case cmLE:
			case cmGE:
			case cmNEQ:
			case cmEQ:
			case cmADD:
			case cmSUB:
			case cmMUL:
			case cmDIV:
			case cmPOW:
			case cmASSIGN:
			case cmOPRT_BIN:

				// A binary operator (user defined or built in) has been found. 
				while (
					stOpt.size() &&
					stOpt.top().GetCode() != cmBO &&
					stOpt.top().GetCode() != cmELSE &&
					stOpt.top().GetCode() != cmIF)
				{
					int nPrec1 = GetOprtPrecedence(stOpt.top()),
						nPrec2 = GetOprtPrecedence(opt);

					if (stOpt.top().GetCode() == opt.GetCode())
					{

						// Deal with operator associativity
						EOprtAssociativity eOprtAsct = GetOprtAssociativity(opt);
						if ((eOprtAsct == oaRIGHT && (nPrec1 <= nPrec2)) ||
							(eOprtAsct == oaLEFT && (nPrec1 < nPrec2)))
						{
							break;
						}
					}
					else if (nPrec1 < nPrec2)
					{
						// In case the operators are not equal the precedence decides alone...
						break;
					}

					if (stOpt.top().GetCode() == cmOPRT_INFIX)
						ApplyFunc(stOpt, stVal, 1);
					else
						ApplyBinOprt(stOpt, stVal);
				} // while ( ... )

				if (opt.GetCode() == cmIF)
					m_vRPN.AddIfElse(opt.GetCode());

				// The operator can't be evaluated right now, push back to the operator stack
				stOpt.push(opt);
				break;

				//
				// Last section contains functions and operators implicitly mapped to functions
				//
			case cmBO:
				stArgCount.push(1);
				stOpt.push(opt);
				break;

			case cmOPRT_INFIX:
			case cmFUNC:
			case cmFUNC_BULK:
			case cmFUNC_STR:
				stOpt.push(opt);
				break;

			case cmOPRT_POSTFIX:
				stOpt.push(opt);
				ApplyFunc(stOpt, stVal, 1);  // this is the postfix operator
				break;

			default:  Error(ecINTERNAL_ERROR, 3);
			} // end of switch operator-token

			opta = opt;

			if (opt.GetCode() == cmEND)
			{
				m_vRPN.Finalize();
				break;
			}

			if (ParserBase::g_DbgDumpStack)
			{
				StackDump(stVal, stOpt);
				m_vRPN.AsciiDump();
			}

//			if (ParserBase::g_DbgDumpCmdCode)
				//m_vRPN.AsciiDump();
		} // while (true)

		if (ParserBase::g_DbgDumpCmdCode)
			m_vRPN.AsciiDump();

		if (ifElseCounter > 0)
			Error(ecMISSING_ELSE_CLAUSE);

		// get the last value (= final result) from the stack
		MUP_ASSERT(stArgCount.size() == 1);
		m_nFinalResultIdx = stArgCount.top();
		if (m_nFinalResultIdx == 0)
			Error(ecINTERNAL_ERROR, 9);

		if (stVal.size() == 0)
			Error(ecEMPTY_EXPRESSION);

		// 2020-09-17; fix for https://oss-fuzz.com/testcase-detail/5758791700971520
		// I don't need the value stack any more. Destructively check if all values in the value 
		// stack represent floating point values
		while (stVal.size())
		{
			if (stVal.top().GetType() != tpDBL)
				Error(ecSTR_RESULT);

			stVal.pop();
		}

		m_vStackBuffer.resize(m_vRPN.GetMaxStackSize() * s_MaxNumOpenMPThreads);
	}

	//---------------------------------------------------------------------------
	/** \brief One of the two main parse functions.
		\sa ParseCmdCode(...)

	  Parse expression from input string. Perform syntax checking and create
	  bytecode. After parsing the string and creating the bytecode the function
	  pointer #m_pParseFormula will be changed to the second parse routine the
	  uses bytecode instead of string parsing.
	*/
	value_type ParserBase::ParseString() const
	{
		try
		{
			CreateRPN();

			if (m_vRPN.GetSize() == 2)
			{
				m_vRPN.StoreEnvironment(m_pTokenReader->GetExpr(), m_vStringBuf);
				m_pParseFormula = &ParserBase::ParseCmdCodeShort;
				m_vStackBuffer[1] = (this->*m_pParseFormula)();
				return m_vStackBuffer[1];
			}
			else
			{
				m_vRPN.StoreEnvironment(m_pTokenReader->GetExpr(), m_vStringBuf);
				m_pParseFormula = &ParserBase::ParseCmdCode;
				return (this->*m_pParseFormula)();
			}
		}
		catch (ParserError& exc)
		{
			exc.SetFormula(m_pTokenReader->GetExpr());
			throw;
		}
	}

	//---------------------------------------------------------------------------
	/** \brief Create an error containing the parse error position.

	  This function will create an Parser Exception object containing the error text and
	  its position.

	  \param a_iErrc [in] The error code of type #EErrorCodes.
	  \param a_iPos [in] The position where the error was detected.
	  \param a_strTok [in] The token string representation associated with the error.
	  \throw ParserException always throws that's the only purpose of this function.
	*/
	void  ParserBase::Error(EErrorCodes a_iErrc, int a_iPos, const string_type& a_sTok) const
	{
		throw exception_type(a_iErrc, a_sTok, m_pTokenReader->GetExpr(), a_iPos);
	}

	//------------------------------------------------------------------------------
	/** \brief Clear all user defined variables.
		\throw nothrow

		Resets the parser to string parsing mode by calling #ReInit.
	*/
	void ParserBase::ClearVar()
	{
		m_VarDef.clear();
		ReInit();
	}

	//------------------------------------------------------------------------------
	/** \brief Remove a variable from internal storage.
		\throw nothrow

		Removes a variable if it exists. If the Variable does not exist nothing will be done.
	*/
	void ParserBase::RemoveVar(const string_type& a_strVarName)
	{
		varmap_type::iterator item = m_VarDef.find(a_strVarName);
		if (item != m_VarDef.end())
		{
			m_VarDef.erase(item);
			ReInit();
		}
	}

	//------------------------------------------------------------------------------
	/** \brief Clear all functions.
		\post Resets the parser to string parsing mode.
		\throw nothrow
	*/
	void ParserBase::ClearFun()
	{
		m_FunDef.clear();
		ReInit();
	}

	//------------------------------------------------------------------------------
	/** \brief Clear all user defined constants.

		Both numeric and string constants will be removed from the internal storage.
		\post Resets the parser to string parsing mode.
		\throw nothrow
	*/
	void ParserBase::ClearConst()
	{
		m_ConstDef.clear();
		m_StrVarDef.clear();
		ReInit();
	}

	//------------------------------------------------------------------------------
	/** \brief Clear all user defined postfix operators.
		\post Resets the parser to string parsing mode.
		\throw nothrow
	*/
	void ParserBase::ClearPostfixOprt()
	{
		m_PostOprtDef.clear();
		ReInit();
	}

	//------------------------------------------------------------------------------
	/** \brief Clear all user defined binary operators.
		\post Resets the parser to string parsing mode.
		\throw nothrow
	*/
	void ParserBase::ClearOprt()
	{
		m_OprtDef.clear();
		ReInit();
	}

	//------------------------------------------------------------------------------
	/** \brief Clear the user defined Prefix operators.
		\post Resets the parser to string parser mode.
		\throw nothrow
	*/
	void ParserBase::ClearInfixOprt()
	{
		m_InfixOprtDef.clear();
		ReInit();
	}

	//------------------------------------------------------------------------------
	/** \brief Enable or disable the formula optimization feature.
		\post Resets the parser to string parser mode.
		\throw nothrow
	*/
	void ParserBase::EnableOptimizer(bool a_bIsOn)
	{
		m_vRPN.EnableOptimizer(a_bIsOn);
		ReInit();
	}

	//---------------------------------------------------------------------------
	/** \brief Enable the dumping of bytecode and stack content on the console.
		\param bDumpCmd Flag to enable dumping of the current bytecode to the console.
		\param bDumpStack Flag to enable dumping of the stack content is written to the console.

	   This function is for debug purposes only!
	*/
	void ParserBase::EnableDebugDump(bool bDumpCmd, bool bDumpStack)
	{
		ParserBase::g_DbgDumpCmdCode = bDumpCmd;
		ParserBase::g_DbgDumpStack = bDumpStack;
	}

	//------------------------------------------------------------------------------
	/** \brief Enable or disable the built in binary operators.
		\throw nothrow
		\sa m_bBuiltInOp, ReInit()

	  If you disable the built in binary operators there will be no binary operators
	  defined. Thus you must add them manually one by one. It is not possible to
	  disable built in operators selectively. This function will Reinitialize the
	  parser by calling ReInit().
	*/
	void ParserBase::EnableBuiltInOprt(bool a_bIsOn)
	{
		m_bBuiltInOp = a_bIsOn;
		ReInit();
	}

	//------------------------------------------------------------------------------
	/** \brief Query status of built in variables.
		\return #m_bBuiltInOp; true if built in operators are enabled.
		\throw nothrow
	*/
	bool ParserBase::HasBuiltInOprt() const
	{
		return m_bBuiltInOp;
	}

	//------------------------------------------------------------------------------
	/** \brief Get the argument separator character.
	*/
	char_type ParserBase::GetArgSep() const
	{
		return m_pTokenReader->GetArgSep();
	}

	//------------------------------------------------------------------------------
	/** \brief Set argument separator.
		\param cArgSep the argument separator character.
	*/
	void ParserBase::SetArgSep(char_type cArgSep)
	{
		m_pTokenReader->SetArgSep(cArgSep);
	}

	//------------------------------------------------------------------------------
	/** \brief Dump stack content.

		This function is used for debugging only.
	*/
	void ParserBase::StackDump(const std::stack<token_type>& a_stVal, const std::stack<token_type>& a_stOprt) const
	{
		std::stack<token_type> stOprt(a_stOprt);
		std::stack<token_type> stVal(a_stVal);

		mu::console() << _T("\nValue stack:\n");
		while (!stVal.empty())
		{
			token_type val = stVal.top();
			stVal.pop();

			if (val.GetType() == tpSTR)
				mu::console() << _T(" \"") << val.GetAsString() << _T("\" ");
			else
				mu::console() << _T(" ") << val.GetVal() << _T(" ");
		}
		mu::console() << "\nOperator stack:\n";

		while (!stOprt.empty())
		{
			if (stOprt.top().GetCode() <= cmASSIGN)
			{
				mu::console() << _T("OPRT_INTRNL \"")
					<< ParserBase::c_DefaultOprt[stOprt.top().GetCode()]
					<< _T("\" \n");
			}
			else
			{
				switch (stOprt.top().GetCode())
				{
				case cmVAR:   mu::console() << _T("VAR\n");  break;
				case cmVAL:   mu::console() << _T("VAL\n");  break;
				case cmFUNC:
					mu::console()
						<< _T("FUNC \"")
						<< stOprt.top().GetAsString()
						<< _T("\"\n");
					break;

				case cmFUNC_BULK:
					mu::console()
						<< _T("FUNC_BULK \"")
						<< stOprt.top().GetAsString()
						<< _T("\"\n");
					break;

				case cmOPRT_INFIX:
					mu::console() << _T("OPRT_INFIX \"")
						<< stOprt.top().GetAsString()
						<< _T("\"\n");
					break;

				case cmOPRT_BIN:
					mu::console() << _T("OPRT_BIN \"")
						<< stOprt.top().GetAsString()
						<< _T("\"\n");
					break;

				case cmFUNC_STR: mu::console() << _T("FUNC_STR\n");       break;
				case cmEND:      mu::console() << _T("END\n");            break;
				case cmUNKNOWN:  mu::console() << _T("UNKNOWN\n");        break;
				case cmBO:       mu::console() << _T("BRACKET \"(\"\n");  break;
				case cmBC:       mu::console() << _T("BRACKET \")\"\n");  break;
				case cmIF:       mu::console() << _T("IF\n");  break;
				case cmELSE:     mu::console() << _T("ELSE\n");  break;
				case cmENDIF:    mu::console() << _T("ENDIF\n");  break;
				default:         mu::console() << stOprt.top().GetCode() << _T(" ");  break;
				}
			}
			stOprt.pop();
		}

		mu::console() << dec << endl;
	}

	/** \brief Calculate the result.

	  A note on const correctness:
	  I consider it important that Calc is a const function.
	  Due to caching operations Calc changes only the state of internal variables with one exception
	  m_UsedVar this is reset during string parsing and accessible from the outside. Instead of making
	  Calc non const GetUsedVar is non const because it explicitly calls Eval() forcing this update.

	  \pre A formula must be set.
	  \pre Variables must have been set (if needed)

	  \sa #m_pParseFormula
	  \return The evaluation result
	  \throw ParseException if no Formula is set or in case of any other error related to the formula.
	*/
	value_type ParserBase::Eval() const
	{
		return (this->*m_pParseFormula)();
	}

	//------------------------------------------------------------------------------
	/** \brief Evaluate an expression containing comma separated subexpressions
		\param [out] nStackSize The total number of results available
		\return Pointer to the array containing all expression results

		This member function can be used to retrieve all results of an expression
		made up of multiple comma separated subexpressions (i.e. "x+y,sin(x),cos(y)")
	*/
	value_type* ParserBase::Eval(int& nStackSize) const
	{
		if (m_vRPN.GetSize() > 0)
		{
			ParseCmdCode();
		}
		else
		{
			ParseString();
		}

		nStackSize = m_nFinalResultIdx;

		// (for historic reasons the stack starts at position 1)
		return &m_vStackBuffer[1];
	}

	//---------------------------------------------------------------------------
	/** \brief Return the number of results on the calculation stack.

	  If the expression contains comma separated subexpressions (i.e. "sin(y), x+y").
	  There may be more than one return value. This function returns the number of
	  available results.
	*/
	int ParserBase::GetNumResults() const
	{
		return m_nFinalResultIdx;
	}

	//---------------------------------------------------------------------------
	void ParserBase::Eval(value_type* results, int nBulkSize)
	{
		CreateRPN();

		int i = 0;

#ifdef MUP_USE_OPENMP
		//#define DEBUG_OMP_STUFF
#ifdef DEBUG_OMP_STUFF
		int* pThread = new int[nBulkSize];
		int* pIdx = new int[nBulkSize];
#endif

		int nMaxThreads = std::min(omp_get_max_threads(), s_MaxNumOpenMPThreads);
		int nThreadID = 0;

#ifdef DEBUG_OMP_STUFF
		int ct = 0;
#endif
		omp_set_num_threads(nMaxThreads);

		const int chunkSize = std::max(nBulkSize/nMaxThreads, 1);
#pragma omp parallel for schedule(static, chunkSize) private(nThreadID)
		for (i = 0; i < nBulkSize; ++i)
		{
			nThreadID = omp_get_thread_num();
			results[i] = ParseCmdCodeBulk(i, nThreadID);

#ifdef DEBUG_OMP_STUFF
#pragma omp critical
			{
				pThread[ct] = nThreadID;
				pIdx[ct] = i;
				ct++;
			}
#endif
		}

#ifdef DEBUG_OMP_STUFF
		FILE* pFile = fopen("bulk_dbg.txt", "w");
		for (i = 0; i < nBulkSize; ++i)
		{
			fprintf(pFile, "idx: %d  thread: %d \n", pIdx[i], pThread[i]);
		}

		delete[] pIdx;
		delete[] pThread;

		fclose(pFile);
#endif

#else
		for (i = 0; i < nBulkSize; ++i)
		{
			results[i] = ParseCmdCodeBulk(i, 0);
		}
#endif

	}
} // namespace mu

#if defined(_MSC_VER)
	#pragma warning(pop)
#endif