File: mpParserBase.cpp

package info (click to toggle)
muparserx 4.0.12-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,240 kB
  • sloc: cpp: 10,953; ansic: 28; makefile: 8
file content (1374 lines) | stat: -rw-r--r-- 40,193 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
/** \file
	\brief Implementation of the muParserX engine.

	<pre>
			   __________                                 ____  ___
	 _____  __ _\______   \_____ _______  ______ __________\   \/  /
	/     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     /
	|  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \
	|__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
		  \/                     \/           \/     \/           \_/
	Copyright (C) 2021 Ingo Berg
	All rights reserved.

	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.
	</pre>
	*/
#include "mpParserBase.h"

#include <cmath>
#include <memory>
#include <vector>
#include <sstream>

#include "utGeneric.h"
#include "mpDefines.h"
#include "mpIfThenElse.h"
#include "mpScriptTokens.h"
#include "mpOprtBinShortcut.h"

using namespace std;

MUP_NAMESPACE_START

//------------------------------------------------------------------------------
const char_type* g_sCmdCode[] = {
	_T("BRCK. OPEN       "),
	_T("BRCK. CLOSE      "),
	_T("IDX OPEN         "),
	_T("IDX CLOSE        "),
	_T("CURLY BRCK. OPEN "),
	_T("CURLY BRCK. CLOSE"),
	_T("ARG_SEP          "),
	_T("IF               "),
	_T("ELSE             "),
	_T("ENDIF            "),
	_T("JMP              "),
	_T("VAL              "),
	_T("FUNC             "),
	_T("OPRT_BIN         "),
	_T("OPRT_IFX         "),
	_T("OPRT_PFX         "),
	_T("SC_BEGIN         "),
	_T("SC_END           "),
	_T("END              "),
	_T("SCR_ENDL         "),
	_T("SCR_CMT          "),
	_T("SCR_WHILE        "),
	_T("SCR_GOTO         "),
	_T("SCR_LABEL        "),
	_T("SCR_FOR          "),
	_T("SCR_IF           "),
	_T("SCR_ELSE         "),
	_T("SCR_ELIF         "),
	_T("SCR_ENDIF        "),
	_T("SCR_FUNC         "),
	_T("UNKNOWN          "),
	nullptr };

//------------------------------------------------------------------------------
bool ParserXBase::s_bDumpStack = false;
bool ParserXBase::s_bDumpRPN = 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* ParserXBase::c_DefaultOprt[] = {
	_T("("),
	_T(")"),
	_T("["),
	_T("]"),
	_T("{"),
	_T("}"),
	_T(","),
	_T("?"),
	_T(":"),
	0 };

//------------------------------------------------------------------------------
/** \brief Default constructor. */
ParserXBase::ParserXBase()
	:m_FunDef()
	, m_PostOprtDef()
	, m_InfixOprtDef()
	, m_OprtDef()
	, m_OprtShortcutDef()
	, m_valDef()
	, m_varDef()
	, m_pParserEngine(&ParserXBase::ParseFromString)
	, m_pTokenReader()
	, m_valDynVarShadow()
	, m_sNameChars()
	, m_sOprtChars()
	, m_sInfixOprtChars()
	, m_bIsQueryingExprVar(false)
	, m_bAutoCreateVar(false)
	, m_rpn()
	, m_vStackBuffer()
{
	InitTokenReader();
}

//---------------------------------------------------------------------------
/** \brief Copy constructor.
	  \param a_Parser Reference to the other parser object

	  Implemented by calling Assign(a_Parser)
	  */
ParserXBase::ParserXBase(const ParserXBase& a_Parser)
	:m_FunDef()
	, m_PostOprtDef()
	, m_InfixOprtDef()
	, m_OprtDef()
	, m_OprtShortcutDef()
	, m_valDef()
	, m_varDef()
	, m_pParserEngine(&ParserXBase::ParseFromString)
	, m_pTokenReader()
	, m_valDynVarShadow()
	, m_sNameChars()
	, m_sOprtChars()
	, m_sInfixOprtChars()
	, m_bAutoCreateVar()
	, m_rpn()
	, m_vStackBuffer()
{
	m_pTokenReader.reset(new TokenReader(this));
	Assign(a_Parser);
}

//---------------------------------------------------------------------------
/** \brief Destructor.
	  \throw nothrow
	  */
ParserXBase::~ParserXBase()
{
	// It is important to release the stack buffer before
	// releasing the value cache. Since it may contain
	// Values referencing the cache.
	m_vStackBuffer.clear();
	m_cache.ReleaseAll();
}

//---------------------------------------------------------------------------
/** \brief Assignement operator.
	  \param a_Parser Object to copy to this.
	  \return *this
	  \throw nothrow

	  Implemented by calling Assign(a_Parser). Self assignement is suppressed.
	  */
ParserXBase& ParserXBase::operator=(const ParserXBase& a_Parser)
{
	Assign(a_Parser);
	return *this;
}

//---------------------------------------------------------------------------
/** \brief Copy state of a parser object to this.
	  \param a_Parser the source object.

	  Clears Variables and Functions of this parser.
	  Copies the states of all internal variables.
	  Resets parse function to string parse mode.
	  */
void ParserXBase::Assign(const ParserXBase& ref)
{
	if (&ref == this)
		return;

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

	m_pTokenReader.reset(ref.m_pTokenReader->Clone(this));

	m_OprtDef = ref.m_OprtDef;
	m_OprtShortcutDef = ref.m_OprtShortcutDef;
	m_FunDef = ref.m_FunDef;
	m_PostOprtDef = ref.m_PostOprtDef;
	m_InfixOprtDef = ref.m_InfixOprtDef;
	m_valDef = ref.m_valDef;
	m_valDynVarShadow = ref.m_valDynVarShadow;
	m_varDef = ref.m_varDef;             // Copy user defined variables

	// Copy charsets
	m_sNameChars = ref.m_sNameChars;
	m_sOprtChars = ref.m_sOprtChars;
	m_sInfixOprtChars = ref.m_sInfixOprtChars;

	m_bAutoCreateVar = ref.m_bAutoCreateVar;

	// Things that should not be copied:
	// - m_vStackBuffer
	// - m_cache
	// - m_rpn
}

//---------------------------------------------------------------------------
/** \brief Evaluate the expression.
	  \pre A formula must be set.
	  \pre Variables must have been set (if needed)
	  \sa SetExpr
	  \return The evaluation result
	  \throw ParseException if no Formula is set or in case of any other error related to the formula.

	  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 GetExprVar is non const because it explicitely calls Eval() forcing this update.
	  */
const IValue& ParserXBase::Eval() const
{
	return (this->*m_pParserEngine)();
}

//---------------------------------------------------------------------------
/** \brief Return the strings of all Operator identifiers.
	  \return Returns a pointer to the c_DefaultOprt array of const char *.
	  \throw nothrow

	  GetOprt is a const function returning a pinter to an array of const char pointers.
	  */
const char_type** ParserXBase::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 ParserXBase::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.
			 \param a_szCharset A string containing all characters that can be used
			 in operator identifiers.
			 */
void ParserXBase::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.
			 \param a_szCharset A string containing all characters that can be used
			 in infix operator identifiers.
			 */
void ParserXBase::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* ParserXBase::ValidNameChars() const
{
	MUP_VERIFY(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* ParserXBase::ValidOprtChars() const
{
	MUP_VERIFY(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* ParserXBase::ValidInfixOprtChars() const
{
	MUP_VERIFY(m_sInfixOprtChars.size());
	return m_sInfixOprtChars.c_str();
}

//---------------------------------------------------------------------------
/** \brief Initialize the token reader.
	  \post m_pTokenReader.Get()!=0
	  \throw nothrow

	  Create new token reader object and submit pointers to function, operator,
	  constant and variable definitions.
	  */
void ParserXBase::InitTokenReader()
{
	m_pTokenReader.reset(new TokenReader(this));
}

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

	  Resets the token reader.
	  */
void ParserXBase::ReInit() const
{
	m_pParserEngine = &ParserXBase::ParseFromString;
	m_pTokenReader->ReInit();
	m_rpn.Reset();
	m_vStackBuffer.clear();
	m_nPos = 0;
}

//---------------------------------------------------------------------------
/** \brief Adds a new package to the parser.

	The parser becomes the owner of the package pointer and is responsible for
	its deletion.
	*/
void ParserXBase::AddPackage(IPackage* p)
{
	p->AddToParser(this);
}

//---------------------------------------------------------------------------
/** \brief Add a value reader object to muParserX.
	  \param a_pReader Pointer to the value reader object.
	  */
void ParserXBase::AddValueReader(IValueReader* a_pReader)
{
	m_pTokenReader->AddValueReader(a_pReader);
}

//---------------------------------------------------------------------------
/** \brief Check if a given name contains invalid characters.
	  \param a_strName The name to check
	  \param a_szCharSet The characterset
	  \throw ParserException if the name contains invalid charakters.
	  */
void ParserXBase::CheckName(const string_type& a_strName,
	const string_type& a_szCharSet) const
{
	if (!a_strName.length() ||
		(a_strName.find_first_not_of(a_szCharSet) != string_type::npos) ||
		(a_strName[0] >= (char_type)'0' && a_strName[0] <= (char_type)'9'))
	{
		Error(ecINVALID_NAME);
	}
}

//---------------------------------------------------------------------------
/** \brief Set the mathematical expression.
	  \param a_sExpr String with the expression
	  \throw ParserException in case of syntax errors.

	  Triggers first time calculation thus the creation of the bytecode and
	  scanning of used variables.
	  */
void ParserXBase::SetExpr(const string_type& a_sExpr)
{
	m_pTokenReader->SetExpr(a_sExpr);
	ReInit();
}

//---------------------------------------------------------------------------
/** \brief Add a user defined variable.
	  \param a_sName The variable name
	  \param a_Var The variable to be added to muParserX
	  */
void ParserXBase::DefineVar(const string_type& ident, const Variable& var)
{
	CheckName(ident, ValidNameChars());

	CheckForEntityExistence(ident, ecVARIABLE_DEFINED);

	m_varDef[ident] = ptr_tok_type(var.Clone());
}

void ParserXBase::CheckForEntityExistence(const string_type& ident, EErrorCodes error_code)
{
	if (IsVarDefined(ident) ||
		IsConstDefined(ident) ||
		IsFunDefined(ident) ||
		IsOprtDefined(ident) ||
		IsPostfixOprtDefined(ident) ||
		IsInfixOprtDefined(ident))
		throw ParserError(ErrorContext(error_code, 0, ident));
}

//---------------------------------------------------------------------------
/** \brief Define a parser Constant.
		\param a_sName The name of the constant
		\param a_Val Const reference to the constants value

		Parser constants are handed over by const reference as opposed to variables
		which are handed over by reference. Consequently the parser can not change
		their value.
		*/
void ParserXBase::DefineConst(const string_type& ident, const Value& val)
{
	CheckName(ident, ValidNameChars());

	CheckForEntityExistence(ident, ecCONSTANT_DEFINED);

	m_valDef[ident] = ptr_tok_type(val.Clone());
}

//---------------------------------------------------------------------------
/** \brief Add a callback object to the parser.
		\param a_pFunc Pointer to the intance of a parser callback object
		representing the function.
		\sa GetFunDef, functions

		The parser takes ownership over the callback object.
		*/
void ParserXBase::DefineFun(const ptr_cal_type& fun)
{
	if (IsFunDefined(fun->GetIdent()))
		throw ParserError(ErrorContext(ecFUNOPRT_DEFINED, 0, fun->GetIdent()));

	fun->SetParent(this);
	m_FunDef[fun->GetIdent()] = ptr_tok_type(fun->Clone());
}

//---------------------------------------------------------------------------
/** \brief Define a binary operator.
		\param a_pCallback Pointer to the callback object
		*/
void ParserXBase::DefineOprt(const TokenPtr<IOprtBin>& oprt)
{
	if (IsOprtDefined(oprt->GetIdent()))
		throw ParserError(ErrorContext(ecFUNOPRT_DEFINED, 0, oprt->GetIdent()));

	oprt->SetParent(this);
	m_OprtDef[oprt->GetIdent()] = ptr_tok_type(oprt->Clone());
}

//---------------------------------------------------------------------------
/** \brief Define a short circuit operator.
		\param a_pCallback Pointer to the callback object
		*/
void ParserXBase::DefineOprt(const TokenPtr<IOprtBinShortcut> &oprt)
{
	if (IsOprtDefined(oprt->GetIdent()))
		throw ParserError(ErrorContext(ecFUNOPRT_DEFINED, 0, oprt->GetIdent()));

	//oprt->SetParent(this);
	m_OprtShortcutDef[oprt->GetIdent()] = ptr_tok_type(oprt->Clone());
}

//---------------------------------------------------------------------------
/** \brief Add a user defined operator.
	  \post Will reset the Parser to string parsing mode.
	  \param a_pOprt Pointer to a unary postfix operator object. The parser will
	  become the new owner of this object hence will destroy it.
	  */
void ParserXBase::DefinePostfixOprt(const TokenPtr<IOprtPostfix>& oprt)
{
	if (IsPostfixOprtDefined(oprt->GetIdent()))
		throw ParserError(ErrorContext(ecFUNOPRT_DEFINED, 0, oprt->GetIdent()));

	// Operator is not added yet, add it.
	oprt->SetParent(this);
	m_PostOprtDef[oprt->GetIdent()] = ptr_tok_type(oprt->Clone());
}

//---------------------------------------------------------------------------
/** \brief Add a user defined operator.
	\param a_pOprt Pointer to a unary postfix operator object. The parser will
		   become the new owner of this object hence will destroy it.
*/
void ParserXBase::DefineInfixOprt(const TokenPtr<IOprtInfix>& oprt)
{
	if (IsInfixOprtDefined(oprt->GetIdent()))
		throw ParserError(ErrorContext(ecFUNOPRT_DEFINED, 0, oprt->GetIdent()));

	// Function is not added yet, add it.
	oprt->SetParent(this);
	m_InfixOprtDef[oprt->GetIdent()] = ptr_tok_type(oprt->Clone());
}

//---------------------------------------------------------------------------
void ParserXBase::RemoveVar(const string_type& ident)
{
	m_varDef.erase(ident);
	ReInit();
}

//---------------------------------------------------------------------------
void ParserXBase::RemoveConst(const string_type& ident)
{
	m_valDef.erase(ident);
	ReInit();
}

//---------------------------------------------------------------------------
void ParserXBase::RemoveFun(const string_type& ident)
{
	m_FunDef.erase(ident);
	ReInit();
}

//---------------------------------------------------------------------------
void ParserXBase::RemoveOprt(const string_type& ident)
{
	m_OprtDef.erase(ident);
	m_OprtShortcutDef.erase(ident);
	ReInit();
}

//---------------------------------------------------------------------------
void ParserXBase::RemovePostfixOprt(const string_type& ident)
{
	m_PostOprtDef.erase(ident);
	ReInit();
}

//---------------------------------------------------------------------------
void ParserXBase::RemoveInfixOprt(const string_type& ident)
{
	m_InfixOprtDef.erase(ident);
	ReInit();
}

//---------------------------------------------------------------------------
bool ParserXBase::IsVarDefined(const string_type& ident) const
{
	return m_varDef.find(ident) != m_varDef.end();
}

//---------------------------------------------------------------------------
bool ParserXBase::IsConstDefined(const string_type& ident) const
{
	return m_valDef.find(ident) != m_valDef.end();
}

//---------------------------------------------------------------------------
bool ParserXBase::IsFunDefined(const string_type& ident) const
{
	return m_FunDef.find(ident) != m_FunDef.end();
}

//---------------------------------------------------------------------------
bool ParserXBase::IsOprtDefined(const string_type& ident) const
{
	return m_OprtDef.find(ident) != m_OprtDef.end() || m_OprtShortcutDef.find(ident) != m_OprtShortcutDef.end();
}

//---------------------------------------------------------------------------
bool ParserXBase::IsPostfixOprtDefined(const string_type& ident) const
{
	return m_PostOprtDef.find(ident) != m_PostOprtDef.end();
}

//---------------------------------------------------------------------------
bool ParserXBase::IsInfixOprtDefined(const string_type& ident) const
{
	return m_InfixOprtDef.find(ident) != m_InfixOprtDef.end();
}

//---------------------------------------------------------------------------
/** \brief Return a map containing the used variables only. */
const var_maptype& ParserXBase::GetExprVar() const
{
	utils::scoped_setter<bool> guard(m_bIsQueryingExprVar, true);

	// Create RPN,  but do not compute the result or switch to RPN
	// parsing mode. The expression may contain yet to be defined variables.
	CreateRPN();
	return m_pTokenReader->GetUsedVar();
}

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

//---------------------------------------------------------------------------
/** \brief Return a map containing all parser constants. */
const val_maptype& ParserXBase::GetConst() const
{
	return m_valDef;
}

//---------------------------------------------------------------------------
/** \brief Return prototypes of all parser functions.
	  \return #m_FunDef
	  \sa FunProt, functions
	  \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 fun_maptype& ParserXBase::GetFunDef() const
{
	return m_FunDef;
}

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

//---------------------------------------------------------------------------
/** \brief Get the version number of muParserX.
	  \return A string containing the version number of muParserX.
	  */
string_type ParserXBase::GetVersion()
{
	return MUP_PARSER_VERSION;
}

//---------------------------------------------------------------------------
void ParserXBase::ApplyRemainingOprt(Stack<ptr_tok_type>& stOpt) const

{
	while (stOpt.size() &&
		stOpt.top()->GetCode() != cmBO &&
		stOpt.top()->GetCode() != cmIO &&
		stOpt.top()->GetCode() != cmCBO &&
		stOpt.top()->GetCode() != cmIF)
	{
		ptr_tok_type& op = stOpt.top();
		switch (op->GetCode())
		{
		case  cmOPRT_INFIX:
		case  cmOPRT_BIN:      ApplyFunc(stOpt, 2);   break;
		case  cmSHORTCUT_END:  ApplyOprtShortcut(stOpt);    break;
		case  cmELSE:          ApplyIfElse(stOpt);    break;
		default:               Error(ecINTERNAL_ERROR);
		} // switch operator token type
	} // While operator stack not empty
}

//---------------------------------------------------------------------------
/** \brief Simulates the call of a parser function with its corresponding arguments.
	  \param a_stOpt The operator stack
	  \param a_stVal The value stack
	  \param a_iArgCount The number of function arguments
	  */
void ParserXBase::ApplyFunc(Stack<ptr_tok_type>& a_stOpt,
	int a_iArgCount) const
{
	if (a_stOpt.empty())
		return;

	ptr_tok_type tok = a_stOpt.pop();
	ICallback* pFun = tok->AsICallback();

	int iArgCount = (pFun->GetArgc() >= 0) ? pFun->GetArgc() : a_iArgCount;
	pFun->SetNumArgsPresent(iArgCount);

	m_nPos -= (iArgCount - 1);
	m_rpn.Add(tok);
}

void ParserXBase::ApplyOprtShortcut(Stack<ptr_tok_type> &a_stOpt) const
{
	if (a_stOpt.empty())
		return;

	ptr_tok_type tok = a_stOpt.pop();
	m_nPos -= 1;
	m_rpn.Add(tok);
}

//---------------------------------------------------------------------------
/** \brief Simulates the effect of the execution of an if-then-else block.
*/
void ParserXBase::ApplyIfElse(Stack<ptr_tok_type>& a_stOpt) const
{
	while (a_stOpt.size() && a_stOpt.top()->GetCode() == cmELSE)
	{
		MUP_VERIFY(a_stOpt.size() > 0);
		MUP_VERIFY(m_nPos >= 3);
		MUP_VERIFY(a_stOpt.top()->GetCode() == cmELSE);

		ptr_tok_type opElse = a_stOpt.pop();
		ptr_tok_type opIf = a_stOpt.pop();
		MUP_VERIFY(opElse->GetCode() == cmELSE)
		
		if (opIf->GetCode() != cmIF)
		{
			ErrorContext err;
			err.Expr = m_pTokenReader->GetExpr();
			err.Errc = ecMISPLACED_COLON;
			err.Pos = m_pTokenReader->GetPos();
			throw ParserError(err);
		}

		// If then else hat 3 argumente und erzeugt einen rückgabewert (3-1=2)
		m_nPos -= 2;
		m_rpn.Add(ptr_tok_type(new TokenIfThenElse(cmENDIF)));
	}
}

//---------------------------------------------------------------------------
void ParserXBase::DumpRPN() const
{
	m_rpn.AsciiDump();
}

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

	// The Stacks take the ownership over the tokens
	Stack<ptr_tok_type> stOpt;
	Stack<int>  stArgCount;
	Stack<int>  stIdxCount;
	ptr_tok_type pTok, pTokPrev;
	Value val;

	ReInit();

	for (;;)
	{
		pTokPrev = pTok;
		pTok = m_pTokenReader->ReadNextToken();

#if defined(MUP_DUMP_TOKENS)
		console() << pTok->AsciiDump() << endl;
#endif

		ECmdCode eCmd = pTok->GetCode();
		switch (eCmd)
		{
		case  cmVAL:
			m_nPos++;
			m_rpn.Add(pTok);
			break;

		case  cmCBC:
		case  cmIC:
		{
			ECmdCode eStarter = (ECmdCode)(eCmd - 1);
			MUP_VERIFY(eStarter == cmCBO || eStarter == cmIO);

			// 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 (pTokPrev.Get() != nullptr && pTokPrev->GetCode() == eStarter)
				--stArgCount.top();

			ApplyRemainingOprt(stOpt);

			// if opt is "]" and opta is "[" the bracket content has been evaluated.
			// Now check whether there is an index operator on the stack.
			if (stOpt.size() && stOpt.top()->GetCode() == eStarter)
			{
				//
				// Find out how many dimensions were used in the index operator.
				//
				int iArgc = stArgCount.pop();
				stOpt.pop(); // Take opening bracket from stack

				ICallback* pOprtIndex = pTok->AsICallback();
				MUP_VERIFY(pOprtIndex != nullptr);

				pOprtIndex->SetNumArgsPresent(iArgc);
				m_rpn.Add(pOprtIndex);

				// If this is an index operator there must be something else in the register (the variable to index)
				MUP_VERIFY(eCmd != cmIC || m_nPos >= (int)iArgc + 1);

				// Reduce the index into the value registers accordingly
				m_nPos -= iArgc;

				if (eCmd == cmCBC)
				{
					++m_nPos;
				}
			} // if opening index bracket is on top of operator stack
		}
		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 (pTokPrev.Get() != nullptr && pTokPrev->GetCode() == cmBO)
				--stArgCount.top();

			ApplyRemainingOprt(stOpt);

			// if opt is ")" and opta is "(" the bracket content 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 so evaluate it afterwards to apply an infix operator.
			if (stOpt.size() && stOpt.top()->GetCode() == cmBO)
			{
				//
				// Here is the stuff to evaluate a function token
				//
				int iArgc = stArgCount.pop();

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

				if ((stOpt.top()->GetCode() != cmFUNC) && (stOpt.top()->GetCode() != cmOPRT_INFIX))
					break;

				ICallback* pFun = stOpt.top()->AsICallback();

				if (pFun->GetArgc() != -1 && iArgc > pFun->GetArgc())
					Error(ecTOO_MANY_PARAMS, pTok->GetExprPos(), pFun);

				if (iArgc < pFun->GetArgc())
					Error(ecTOO_FEW_PARAMS, pTok->GetExprPos(), pFun);

				// Apply function, if present
				if (stOpt.size() &&
					stOpt.top()->GetCode() != cmOPRT_INFIX &&
					stOpt.top()->GetCode() != cmOPRT_BIN)
				{
					ApplyFunc(stOpt, iArgc);
				}
			}
		}
		break;

		case  cmELSE:
			ApplyRemainingOprt(stOpt);
			m_rpn.Add(pTok);
			stOpt.push(pTok);
			break;

		case  cmSCRIPT_NEWLINE:
			ApplyRemainingOprt(stOpt);
			m_rpn.AddNewline(pTok, m_nPos);
			stOpt.clear();
			m_nPos = 0;
			break;

		case  cmARG_SEP:
			if (stArgCount.empty())
				Error(ecUNEXPECTED_COMMA, m_pTokenReader->GetPos() - 1);

			++stArgCount.top();

			ApplyRemainingOprt(stOpt);
			break;

		case  cmEOE:
			ApplyRemainingOprt(stOpt);
			m_rpn.Finalize();
			break;

		case  cmIF:
		case  cmOPRT_BIN:
		case  cmSHORTCUT_BEGIN:
		{
			while (stOpt.size() &&
				stOpt.top()->GetCode() != cmBO &&
				stOpt.top()->GetCode() != cmIO &&
				stOpt.top()->GetCode() != cmCBO &&
				stOpt.top()->GetCode() != cmELSE &&
				stOpt.top()->GetCode() != cmIF)
			{
				IToken* pOprt1 = stOpt.top().Get();
				IToken* pOprt2 = pTok.Get();
				MUP_VERIFY(pOprt1 != nullptr && pOprt2 != nullptr);
				MUP_VERIFY(pOprt1->AsIPrecedence() && pOprt2->AsIPrecedence());

				int nPrec1 = pOprt1->AsIPrecedence()->GetPri(),
					nPrec2 = pOprt2->AsIPrecedence()->GetPri();

				if (pOprt1->GetCode() == pOprt2->GetCode())
				{
					// Deal with operator associativity
					EOprtAsct eOprtAsct = pOprt1->AsIPrecedence()->GetAssociativity();
					if ((eOprtAsct == oaRIGHT && (nPrec1 <= nPrec2)) ||
						(eOprtAsct == oaLEFT && (nPrec1 < nPrec2)))
					{
						break;
					}
				}
				else if (nPrec1 < nPrec2)
				{
					break;
				}
				
				if (pOprt1->GetCode() == cmSHORTCUT_END) 
				{
					ApplyOprtShortcut(stOpt);
					break;
				} 
				// apply the operator now
				// (binary operators are identic to functions with two arguments)
				ApplyFunc(stOpt, 2);
			} // while ( ... )

			if (pTok->GetCode() == cmIF || pTok->GetCode() == cmSHORTCUT_BEGIN)
				m_rpn.Add(pTok);

			if (pTok->GetCode() == cmSHORTCUT_BEGIN)
			{
				if(pTok->AsIPrecedence()->GetPri() == prLOGIC_OR)
				{
					stOpt.push(ptr_tok_type(new OprtShortcutLogicOrEnd));
				}
				else
				{
					stOpt.push(ptr_tok_type(new OprtShortcutLogicAndEnd));
				}
			} 
			else 
			{
				stOpt.push(pTok);
			}
		}
		break;

		//
		//  Postfix Operators
		//
		case  cmOPRT_POSTFIX:
			MUP_VERIFY(m_nPos);
			m_rpn.Add(pTok);
			break;

		case  cmCBO:
		case  cmIO:
		case  cmBO:
			stOpt.push(pTok);
			stArgCount.push(1);
			break;

			//
			// Functions
			//
		case  cmOPRT_INFIX:
		case  cmFUNC:
		{
			ICallback* pFunc = pTok->AsICallback();
			MUP_VERIFY(pFunc != nullptr);
			stOpt.push(pTok);
		}
		break;

		default:
			Error(ecINTERNAL_ERROR);
		} // switch Code

		if (ParserXBase::s_bDumpStack)
		{
			StackDump(stOpt);
		}

		if (pTok->GetCode() == cmEOE)
			break;
	} // for (all tokens)

	if (ParserXBase::s_bDumpRPN)
	{
		m_rpn.AsciiDump();
	}

	if (m_nPos > 1)
	{
		Error(ecUNEXPECTED_COMMA, -1);
	}
}

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

	  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.
	  */
const IValue& ParserXBase::ParseFromString() const
{
	CreateRPN();

	// Umsachalten auf RPN
	m_vStackBuffer.assign(m_rpn.GetRequiredStackSize(), ptr_val_type());
	for (std::size_t i = 0; i < m_vStackBuffer.size(); ++i)
	{
		Value* pValue = new Value;
		pValue->BindToCache(&m_cache);
		m_vStackBuffer[i].Reset(pValue);
	}

	m_pParserEngine = &ParserXBase::ParseFromRPN;

	return (this->*m_pParserEngine)();
}

//---------------------------------------------------------------------------
const IValue& ParserXBase::ParseFromRPN() const
{
	ptr_val_type* pStack = &m_vStackBuffer[0];
	if (m_rpn.GetSize() == 0)
	{
		// Passiert bei leeren strings oder solchen, die nur Leerzeichen enthalten
		ErrorContext err;
		err.Expr = m_pTokenReader->GetExpr();
		err.Errc = ecUNEXPECTED_EOF;
		err.Pos = 0;
		throw ParserError(err);
	}

	const ptr_tok_type* pRPN = &(m_rpn.GetData()[0]);

	int sidx = -1;
	std::size_t lenRPN = m_rpn.GetSize();
	for (std::size_t i = 0; i < lenRPN; ++i)
	{
		IToken* pTok = pRPN[i].Get();
		ECmdCode eCode = pTok->GetCode();

		switch (eCode)
		{
		case cmSCRIPT_NEWLINE:
			sidx = -1;
			continue;

		case cmVAL:
		{
			IValue* pVal = static_cast<IValue*>(pTok);

			sidx++;
			MUP_VERIFY(sidx < (int)m_vStackBuffer.size());
			if (pVal->IsVariable())
			{
				pStack[sidx].Reset(pVal);
			}
			else
			{
				ptr_val_type& val = pStack[sidx];
				if (val->IsVariable())
					val.Reset(m_cache.CreateFromCache());

				*val = *(static_cast<IValue*>(pTok));
			}
		}
		continue;

		case  cmIC:
		{
			ICallback* pIdxOprt = static_cast<ICallback*>(pTok);
			int nArgs = pIdxOprt->GetArgsPresent();
			sidx -= nArgs - 1;
			MUP_VERIFY(sidx >= 0);

			ptr_val_type& idx = pStack[sidx];   // Pointer to the first index
			ptr_val_type& val = pStack[--sidx];   // Pointer to the variable or value beeing indexed
			pIdxOprt->Eval(val, &idx, nArgs);
		}
		continue;

		case cmCBC:
		case cmOPRT_POSTFIX:
		case cmFUNC:
		case cmOPRT_BIN:
		case cmOPRT_INFIX:
		{
			ICallback* pFun = static_cast<ICallback*>(pTok);
			int nArgs = pFun->GetArgsPresent();
			sidx -= nArgs - 1;

			// most likely cause: Comma in if-then-else sum(false?1,0,0:3)
			if (sidx < 0)
			{
				ErrorContext err;
				err.Expr = m_pTokenReader->GetExpr();
				err.Errc = ecUNEXPECTED_COMMA;
				err.Pos = m_pTokenReader->GetPos();
				throw ParserError(err);
			}

			ptr_val_type& val = pStack[sidx];
			try
			{
				if (val->IsVariable())
				{
					ptr_val_type buf(m_cache.CreateFromCache());
					pFun->Eval(buf, &val, nArgs);
					val = buf;
				}
				else
				{
					pFun->Eval(val, &val, nArgs);
				}
			}
			catch (ParserError& exc)
			{
				// <ibg 20130131> Not too happy about that:
				// Multiarg functions may throw specific error codes when evaluating.
				// These codes would be converted to ecEVAL here. I omit the conversion
				// for certain handpicked errors. (The reason this catch block exists is
				// that not all exceptions contain proper metadata when thrown out of
				// a function.)
				if (exc.GetCode() == ecTOO_FEW_PARAMS ||
					exc.GetCode() == ecDOMAIN_ERROR ||
					exc.GetCode() == ecOVERFLOW ||
					exc.GetCode() == ecINVALID_NUMBER_OF_PARAMETERS ||
					exc.GetCode() == ecASSIGNEMENT_TO_VALUE)
				{
					exc.GetContext().Pos = pFun->GetExprPos();
					throw;
				}
				// </ibg>
				else
				{
					ErrorContext err;
					err.Expr = m_pTokenReader->GetExpr();
					err.Ident = pFun->GetIdent();
					err.Errc = ecEVAL;
					err.Pos = pFun->GetExprPos();
					err.Hint = exc.GetMsg();
					throw ParserError(err);
				}
			}
			catch (MatrixError& /*exc*/)
			{
				ErrorContext err;
				err.Expr = m_pTokenReader->GetExpr();
				err.Ident = pFun->GetIdent();
				err.Errc = ecMATRIX_DIMENSION_MISMATCH;
				err.Pos = pFun->GetExprPos();
				throw ParserError(err);
			}
		}
		continue;

		case cmIF:
			MUP_VERIFY(sidx >= 0);
			if (pStack[sidx--]->GetBool() == false)
				i += static_cast<TokenIfThenElse*>(pTok)->GetOffset();
			continue;

		case cmELSE:
		case cmJMP:
			i += static_cast<TokenIfThenElse*>(pTok)->GetOffset();
			continue;

		case cmENDIF:
			continue;

		case cmSHORTCUT_BEGIN:
			if (pTok->AsIPrecedence()->GetPri() == prLOGIC_OR)
			{
				// occur short circuit feature
				if (pStack[sidx]->GetBool() == true) 
				{
					i += static_cast<IOprtBinShortcut*>(pTok)->GetOffset();
				} else {
					// pop stack ,becuase this value had used
					--sidx;
				}
			}
			else // logic and
			{
				// occur short circuit feature
				if (pStack[sidx]->GetBool() == false) 
				{
					i += static_cast<IOprtBinShortcut*>(pTok)->GetOffset();
				} else {
					// pop stack ,becuase this value had used
					--sidx;
				}
			}
			continue;

		case cmSHORTCUT_END:
			continue;

		default:
			Error(ecINTERNAL_ERROR);
		} // switch token
	} // for all RPN tokens

	return *pStack[0];
}

//---------------------------------------------------------------------------
void  ParserXBase::Error(EErrorCodes a_iErrc, int a_iPos, const IToken* a_pTok) const
{
	ErrorContext err;
	err.Errc = a_iErrc;
	err.Pos = a_iPos;
	err.Expr = m_pTokenReader->GetExpr();
	err.Ident = (a_pTok) ? a_pTok->GetIdent() : _T("");
	throw ParserError(err);
}

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

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

//------------------------------------------------------------------------------
/** \brief Clear the expression.
	  \throw nothrow

	  Clear the expression and existing bytecode.
	  */
void ParserXBase::ClearExpr()
{
	m_pTokenReader->SetExpr(_T(""));
	ReInit();
}

//------------------------------------------------------------------------------
/** \brief Clear all function definitions.
	  \throw nothrow
	  */
void ParserXBase::ClearFun()
{
	m_FunDef.clear();
	ReInit();
}

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

	  Both numeric and string constants will be removed from the internal storage.
	  */
void ParserXBase::ClearConst()
{
	m_valDef.clear();
	ReInit();
}

//------------------------------------------------------------------------------
/** \brief Clear all user defined postfix operators.
	  \throw nothrow
	  */
void ParserXBase::ClearPostfixOprt()
{
	m_PostOprtDef.clear();
	ReInit();
}

//------------------------------------------------------------------------------
/** \brief Clear all user defined binary operators.
	  \throw nothrow
	  */
void ParserXBase::ClearOprt()
{
	m_OprtDef.clear();
	m_OprtShortcutDef.clear();
	ReInit();
}

//------------------------------------------------------------------------------
/** \brief Clear the user defined Prefix operators.
	  \throw nothrow
	  */
void ParserXBase::ClearInfixOprt()
{
	m_InfixOprtDef.clear();
	ReInit();
}

//------------------------------------------------------------------------------
void ParserXBase::EnableAutoCreateVar(bool bStat)
{
	m_bAutoCreateVar = bStat;
}

//------------------------------------------------------------------------------
void ParserXBase::EnableOptimizer(bool bStat)
{
	m_rpn.EnableOptimizer(bStat);
}

//---------------------------------------------------------------------------
/** \brief Enable the dumping of bytecode amd 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 ParserXBase::EnableDebugDump(bool bDumpRPN, bool bDumpStack)
{
	ParserXBase::s_bDumpRPN = bDumpRPN;
	ParserXBase::s_bDumpStack = bDumpStack;
}

//------------------------------------------------------------------------------
bool ParserXBase::IsAutoCreateVarEnabled() const
{
	return m_bAutoCreateVar;
}

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

	  This function is used for debugging only.
	  */
void ParserXBase::StackDump(const Stack<ptr_tok_type>& a_stOprt) const
{
	using std::cout;
	Stack<ptr_tok_type>  stOprt(a_stOprt);

	string_type sInfo = _T("StackDump>  ");
	console() << sInfo;

	if (stOprt.empty())
		console() << _T("\n") << sInfo << _T("Operator stack is empty.\n");
	else
		console() << _T("\n") << sInfo << _T("Operator stack:\n");

	while (!stOprt.empty())
	{
		ptr_tok_type tok = stOprt.pop();
		console() << sInfo << _T(" ") << g_sCmdCode[tok->GetCode()] << _T(" \"") << tok->GetIdent() << _T("\" \n");
	}

	console() << endl;
}
} // namespace mu