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
|
/** \example example.cpp
This is example code showing you how to use muparserx.
<pre>
__________ ____ ___
_____ __ _\______ \_____ _______ ______ __________\ \/ /
/ \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ /
| Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \
|__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \
\/ \/ \/ \/ \_/
Copyright (C) 2022 Ingo Berg, et al.
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>
*/
//---------------------------------------------------------------------------
//
// muparserx
//
// example.cpp - Demonstrates how to use muparserx
//
//---------------------------------------------------------------------------
/** \brief This macro will enable mathematical constants like M_PI. */
#define _USE_MATH_DEFINES
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <limits>
#include <string>
#include <typeinfo>
//--- muparserx framework -------------------------------------------------------------------------
#include "mpParser.h"
#include "mpDefines.h"
#include "mpTest.h"
//--- other includes ------------------------------------------------------------------------------
#include "timer.h"
using namespace std;
using namespace mup;
const string_type sPrompt = _T("muparserx> ");
//-------------------------------------------------------------------------------------------------
// The following classes will be used to list muParserX variables, constants
// from this console application
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
class FunPrint : public ICallback
{
public:
FunPrint() : ICallback(cmFUNC, _T("print"), 1)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* a_pArg, int /*a_iArgc*/)
{
console() << a_pArg[0].Get()->ToString() << _T("\n");
*ret = (float_type)0.0;
}
virtual const char_type* GetDesc() const
{
return _T("");
}
virtual IToken* Clone() const
{
return new FunPrint(*this);
}
}; // class FunPrint
//-------------------------------------------------------------------------------------------------
class FunTest0 : public ICallback
{
public:
FunTest0() : ICallback(cmFUNC, _T("test0"), 0)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* /*a_pArg*/, int /*a_iArgc*/)
{
*ret = (float_type)0.0;
}
virtual const char_type* GetDesc() const
{
return _T("");
}
virtual IToken* Clone() const
{
return new FunTest0(*this);
}
}; // class FunTest0
//-------------------------------------------------------------------------------------------------
class FunListVar : public ICallback
{
public:
FunListVar() : ICallback(cmFUNC, _T("list_var"), 0)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* /*a_pArg*/, int /*a_iArgc*/)
{
ParserXBase& parser = *GetParent();
console() << _T("\nParser variables:\n");
console() << _T("-----------------\n");
// Query the used variables (must be done after calc)
var_maptype vmap = parser.GetVar();
if (!vmap.size())
{
console() << _T("Expression does not contain variables\n");
}
else
{
var_maptype::iterator item = vmap.begin();
for (; item != vmap.end(); ++item)
{
// You can dump the token into a stream via the "<<" operator
console() << _T(" ") << item->first << _T(" = ") << *(item->second)/* << _T("\n")*/;
// If you need more specific information cast the token to a variable object
Variable& v = (Variable&)(*(item->second));
console() << _T(" (type=\"") << v.GetType() << _T("\"; ptr=0x") << hex << v.GetPtr() << _T(")\n");
}
}
*ret = (float_type)vmap.size();
}
virtual const char_type* GetDesc() const
{
return _T("list_var() - List all variables of the parser bound to this function and returns the number of defined variables.");
}
virtual IToken* Clone() const
{
return new FunListVar(*this);
}
}; // class FunListVar
//-------------------------------------------------------------------------------------------------
class FunListConst : public ICallback
{
public:
FunListConst() : ICallback(cmFUNC, _T("list_const"), 0)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* /*a_pArg*/, int /*a_iArgc*/)
{
ParserXBase& parser = *GetParent();
console() << _T("\nParser constants:\n");
console() << _T("-----------------\n");
val_maptype cmap = parser.GetConst();
if (!cmap.size())
{
console() << _T("No constants defined\n");
}
else
{
val_maptype::iterator item = cmap.begin();
for (; item != cmap.end(); ++item)
console() << _T(" ") << item->first << _T(" = ") << (Value&)(*(item->second)) << _T("\n");
}
*ret = (float_type)cmap.size();
}
virtual const char_type* GetDesc() const
{
return _T("list_const() - List all constants of the parser bound to this function and returns the number of defined constants.");
}
virtual IToken* Clone() const
{
return new FunListConst(*this);
}
}; // class FunListConst
//-------------------------------------------------------------------------------------------------
class FunBenchmark : public ICallback
{
public:
FunBenchmark() : ICallback(cmFUNC, _T("bench"), 0)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* /*a_pArg*/, int /*a_iArgc*/)
{
char outstr[200];
time_t t = time(nullptr);
struct tm newtime;
#ifdef _MSC_VER
errno_t err = localtime_s(&newtime, &t);
if (err != 0)
return;
#else
auto* r = localtime_r(&t, &newtime);
if (!r)
return;
#endif
#ifdef _DEBUG
strftime(outstr, sizeof(outstr), "Result_%Y%m%d_%H%M%S_dbg.txt", &newtime);
#else
strftime(outstr, sizeof(outstr), "Result_%Y%m%d_%H%M%S_release.txt", &newtime);
#endif
const char_type* sExpr[] = {
_T("sin(a)"),
_T("cos(a)"),
_T("tan(a)"),
_T("sqrt(a)"),
_T("(a+b)*3"),
_T("a^2+b^2"),
_T("a^3+b^3"),
_T("a^4+b^4"),
_T("a^5+b^5"),
_T("a*2+b*2"),
_T("-(b^1.1)"),
_T("a + b * c"),
_T("a * b + c"),
_T("a+b*(a+b)"),
_T("(1+b)*(-3)"),
_T("e^log(7*a)"),
_T("10^log(3+b)"),
_T("a+b-e*pi/5^6"),
_T("a^b/e*pi-5+6"),
_T("sin(a)+sin(b)"),
_T("(cos(2.41)/b)"),
_T("-(sin(pi+a)+1)"),
_T("a-(e^(log(7+b)))"),
_T("sin(((a-a)+b)+a)"),
_T("((0.09/a)+2.58)-1.67"),
_T("abs(sin(sqrt(a^2+b^2))*255)"),
_T("abs(sin(sqrt(a*a+b*b))*255)"),
_T("cos(0.90-((cos(b)/2.89)/e)/a)"),
_T("(1*(2*(3*(4*(5*(6*(a+b)))))))"),
_T("abs(sin(sqrt(a^2.1+b^2.1))*255)"),
_T("(1*(2*(3*(4*(5*(6*(7*(a+b))))))))"),
_T("1/(a*sqrt(2*pi))*e^(-0.5*((b-a)/a)^2)"),
_T("1+2-3*4/5^6*(2*(1-5+(3*7^9)*(4+6*7-3)))+12"),
_T("1+b-3*4/5^6*(2*(1-5+(3*7^9)*(4+6*7-3)))+12*a"),
_T("(b+1)*(b+2)*(b+3)*(b+4)*(b+5)*(b+6)*(b+7)*(b+8)*(b+9)*(b+10)*(b+11)*(b+12)"),
_T("(a/((((b+(((e*(((((pi*((((3.45*((pi+a)+pi))+b)+b)*a))+0.68)+e)+a)/a))+a)+b))+b)*a)-pi))"),
_T("(((-9))-e/(((((((pi-(((-7)+(-3)/4/e))))/(((-5))-2)-((pi+(-0))*(sqrt((e+e))*(-8))*(((-pi)+(-pi)-(-9)*(6*5))/(-e)-e))/2)/((((sqrt(2/(-e)+6)-(4-2))+((5/(-2))/(1*(-pi)+3))/8)*pi*((pi/((-2)/(-6)*1*(-1))*(-6)+(-e)))))/((e+(-2)+(-e)*((((-3)*9+(-e)))+(-9)))))))-((((e-7+(((5/pi-(3/1+pi)))))/e)/(-5))/(sqrt((((((1+(-7))))+((((-e)*(-e)))-8))*(-5)/((-e)))*(-6)-((((((-2)-(-9)-(-e)-1)/3))))/(sqrt((8+(e-((-6))+(9*(-9))))*(((3+2-8))*(7+6+(-5))+((0/(-e)*(-pi))+7)))+(((((-e)/e/e)+((-6)*5)*e+(3+(-5)/pi))))+pi))/sqrt((((9))+((((pi))-8+2))+pi))/e*4)*((-5)/(((-pi))*(sqrt(e)))))-(((((((-e)*(e)-pi))/4+(pi)*(-9)))))))+(-pi)"),
0 };
ParserX parser;
Value a((float_type)1.0);
Value b((float_type)2.0);
Value c((float_type)3.0);
parser.DefineVar(_T("a"), Variable(&a));
parser.DefineVar(_T("b"), Variable(&b));
parser.DefineVar(_T("c"), Variable(&c));
parser.DefineConst(_T("pi"), (float_type)M_PI);
parser.DefineConst(_T("e"), (float_type)M_E);
FILE* pFile;
#ifdef _MSC_VER
err = fopen_s(&pFile, outstr, "w");
if (err != 0)
return;
#else
pFile = fopen(outstr, "w");
if (!pFile)
return;
#endif
int iCount = 400000;
#ifdef _DEBUG
string_type sMode = _T("# debug mode\n");
#else
string_type sMode = _T("# release mode\n");
#endif
#if !defined MUP_USE_WIDE_STRING
fprintf(pFile, "%s; muParserX V%s\n", sMode.c_str(), ParserXBase::GetVersion().c_str());
fprintf(pFile, "\"Eqn no.\", \"number\", \"result\", \"time in ms\", \"eval per second\", \"expr\"\n");
printf("%s", sMode.c_str());
printf("\"Eqn no.\", \"number\", \"result\", \"time in ms\", \"eval per second\", \"expr\"\n");
#else
fwprintf(pFile, _T("%s; muParserX V%s\n"), sMode.c_str(), ParserXBase::GetVersion().c_str());
fwprintf(pFile, _T("\"Eqn no.\", \"number\", \"result\", \"time in ms\", \"eval per second\", \"expr\"\n"));
wprintf(_T("%s"), sMode.c_str());
wprintf(_T("\"Eqn no.\", \"number\", \"result\", \"time in ms\", \"eval per second\", \"expr\"\n"));
#endif
double avg_eval_per_sec = 0;
int ct = 0;
for (int i = 0; sExpr[i]; ++i)
{
ct++;
StartTimer();
Value val;
parser.SetExpr(sExpr[i]);
// implicitely create reverse polish notation
parser.Eval();
for (int n = 0; n < iCount; ++n)
{
val = parser.Eval();
}
double diff = StopTimer();
double eval_per_sec = (double)iCount * 1000.0 / diff;
avg_eval_per_sec += eval_per_sec;
#if !defined MUP_USE_WIDE_STRING
fprintf(pFile, "Eqn_%d, %d, %lf, %lf, %lf, %s\n", i, iCount, (double)val.GetFloat(), diff, eval_per_sec, sExpr[i]);
printf("Eqn_%d, %d, %lf, %lf, %lf, %s\n", i, iCount, (double)val.GetFloat(), diff, eval_per_sec, sExpr[i]);
#else
fwprintf(pFile, _T("Eqn_%d, %d, %lf, %lf, %lf, %s\n"), i, iCount, (double)val.GetFloat(), diff, eval_per_sec, sExpr[i]);
wprintf(_T("Eqn_%d, %d, %lf, %lf, %lf, %s\n"), i, iCount, (double)val.GetFloat(), diff, eval_per_sec, sExpr[i]);
#endif
}
avg_eval_per_sec /= (double)ct;
fprintf(pFile, "# Eval per s: %ld", (long)avg_eval_per_sec);
fflush(pFile);
*ret = (float_type)avg_eval_per_sec;
}
virtual const char_type* GetDesc() const
{
return _T("bench() - Perform a benchmark with a set of standard functions.");
}
virtual IToken* Clone() const
{
return new FunBenchmark(*this);
}
}; // class FunBenchmark
//-------------------------------------------------------------------------------------------------
class FunListFunctions : public ICallback
{
public:
FunListFunctions() : ICallback(cmFUNC, _T("list_fun"), 0)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* /*a_pArg*/, int /*a_iArgc*/)
{
ParserXBase& parser = *GetParent();
console() << _T("\nParser functions:\n");
console() << _T("----------------\n");
fun_maptype fmap = parser.GetFunDef();
if (!fmap.size())
{
console() << _T("No functions defined\n");
}
else
{
val_maptype::iterator item = fmap.begin();
for (; item != fmap.end(); ++item)
{
ICallback* pFun = (ICallback*)item->second.Get();
console() << pFun->GetDesc() << _T("\n");
}
}
*ret = (float_type)fmap.size();
}
virtual const char_type* GetDesc() const
{
return _T("list_fun() - List all parser functions and returns the total number of defined functions.");
}
virtual IToken* Clone() const
{
return new FunListFunctions(*this);
}
}; // class FunListFunctions
//-------------------------------------------------------------------------------------------------
class FunEnableOptimizer : public ICallback
{
public:
FunEnableOptimizer() : ICallback(cmFUNC, _T("enable_optimizer"), 1)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* a_pArg, int /*a_iArgc*/)
{
ParserXBase& parser = *GetParent();
parser.EnableOptimizer(a_pArg[0]->GetBool());
*ret = a_pArg[0]->GetBool();
}
virtual const char_type* GetDesc() const
{
return _T("enable_optimizer(bool) - Enables the parsers built in expression optimizer.");
}
virtual IToken* Clone() const
{
return new FunEnableOptimizer(*this);
}
}; // class FunListFunctions
//-------------------------------------------------------------------------------------------------
class FunSelfTest : public ICallback
{
public:
FunSelfTest() : ICallback(cmFUNC, _T("test"), 0)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* /*a_pArg*/, int /*a_iArgc*/)
{
ParserXBase::EnableDebugDump(0, 0);
ParserTester pt;
pt.Run();
*ret = (float_type)0.0;
}
virtual const char_type* GetDesc() const
{
return _T("test() - Runs the unit test of muparserx.");
}
virtual IToken* Clone() const
{
return new FunSelfTest(*this);
}
}; // class FunSelfTest
//-------------------------------------------------------------------------------------------------
class FunEnableDebugDump : public ICallback
{
public:
FunEnableDebugDump() : ICallback(cmFUNC, _T("debug"), 2)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* a_pArg, int /*a_iArgc*/)
{
ParserXBase::EnableDebugDump(a_pArg[0]->GetBool(), a_pArg[1]->GetBool());
if (a_pArg[0]->GetBool())
{
console() << _T("Bytecode output activated.\n");
}
else
{
console() << _T("Bytecode output deactivated.\n");
}
if (a_pArg[1]->GetBool())
{
console() << _T("Stack output activated.\n");
}
else
{
console() << _T("Stack output deactivated.\n");
}
*ret = (float_type)0.0;
}
virtual const char_type* GetDesc() const
{
return _T("debug(bDumpRPN, bDumpStack) - Enable dumping of RPN and stack content.");
}
virtual IToken* Clone() const
{
return new FunEnableDebugDump(*this);
}
}; // class FunEnableDebugDump
#if defined(MUP_USE_WIDE_STRING)
//-------------------------------------------------------------------------------------------------
class FunLang : public ICallback
{
public:
FunLang() : ICallback(cmFUNC, _T("lang"), 1)
{}
virtual void Eval(ptr_val_type& ret, const ptr_val_type* a_pArg, int /*a_iArgc*/)
{
string_type sID = a_pArg[0]->GetString();
if (sID == _T("de"))
{
ParserX::ResetErrorMessageProvider(new mup::ParserMessageProviderGerman);
}
else if (sID == _T("en"))
{
ParserX::ResetErrorMessageProvider(new mup::ParserMessageProviderEnglish);
}
else
{
console() << _T("Invalid language ID\n");
}
*ret = (float_type)0.0;
}
virtual const char_type* GetDesc() const
{
return _T("lang(sLang) - Set the language of error messages (i.e. \"de\" or \"en\").");
}
virtual IToken* Clone() const
{
return new FunLang(*this);
}
}; // class FunLang
#endif // #if defined(MUP_USE_WIDE_STRING)
//---------------------------------------------------------------------------
void Splash()
{
console() << _T("-------------------------------------------------------------------------\n");
console() << _T(" __________ ____ ___\n");
console() << _T(" _____ __ _\\______ \\_____ _______ ______ __________\\ \\/ /\n");
console() << _T(" / \\| | \\ ___/\\__ \\\\_ __ \\/ ___// __ \\_ __ \\ / \n");
console() << _T(" | Y Y \\ | / | / __ \\| | \\/\\___ \\\\ ___/| | \\/ \\ \n");
console() << _T(" |__|_| /____/|____| (____ /__| /____ >\\___ >__| /___/\\ \\\n");
console() << _T(" \\/ \\/ \\/ \\/ \\_/\n");
console() << _T(" Version ") << ParserXBase::GetVersion() << _T("\n");
console() << _T(" Copyright (C) 2022 Ingo Berg");
console() << _T("\n\n");
console() << _T("-------------------------------------------------------------------------\n\n");
console() << _T("Build configuration:\n\n");
#if defined(_DEBUG)
console() << _T("- DEBUG build\n");
#else
console() << _T("- RELEASE build\n");
#endif
#if defined(MUP_USE_WIDE_STRING)
console() << _T("- wide string build\n");
#else
console() << _T("- ascii build\n");
#endif
#if defined (__clang__)
console() << _T("- compiled with clang Version ") << __clang_version__ << _T("\n");
#elif defined (__GNUC__)
console() << _T("- compiled with GCC Version ") << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__ << _T("\n");
#elif defined(_MSC_VER)
console() << _T("- compiled with MSC Version ") << _MSC_VER << _T("\n");
#endif
console() << _T("- IEEE 754 (IEC 559) is ") << ((std::numeric_limits<float_type>::is_iec559) ? _T("available") : _T(" NOT AVAILABLE")) << _T("\n");
console() << _T("- ") << sizeof(void*) * 8 << _T(" bit\n");
console() << _T("- Floating point type is \"") << typeid(float_type).name()
<< _T("\" (") << std::numeric_limits<float_type>::digits10 << _T(" Digits)")
<< _T("\n");
console() << _T("\n");
}
//---------------------------------------------------------------------------
void SelfTest()
{
console() << _T("-------------------------------------------------------------------------\n\n");
console() << _T("Running test suite:\n\n");
ParserTester pt;
pt.Run();
console() << _T("-------------------------------------------------------------------------\n\n");
console() << _T("Special parser functions:\n");
console() << _T(" list_var() - list parser variables and return the number of variables.\n");
console() << _T(" list_fun() - list parser functions and return the number of functions\n");
console() << _T(" list_const() - list all numeric parser constants\n");
console() << _T("Command line commands:\n");
console() << _T(" exprvar - list all variables found in the last expression\n");
console() << _T(" rpn - Dump reverse polish notation of the current expression\n");
console() << _T(" quit - exits the parser\n");
console() << _T("Constants:\n");
console() << _T(" \"e\" 2.718281828459045235360287\n");
console() << _T(" \"pi\" 3.141592653589793238462643\n");
console() << _T("-------------------------------------------------------------------------\n\n");
}
//---------------------------------------------------------------------------
void ListExprVar(ParserXBase& parser)
{
console() << _T("\nVariables found in : \"") << parser.GetExpr() << _T("\"\n");
console() << _T("-----------------------------\n");
// Query the used variables (must be done after calc)
var_maptype vmap = parser.GetExprVar();
if (!vmap.size())
{
console() << _T("Expression does not contain variables\n");
}
else
{
var_maptype::iterator item = vmap.begin();
for (; item != vmap.end(); ++item)
console() << _T(" ") << item->first << _T(" = ") << (Variable&)(*(item->second)) << _T("\n");
}
}
//---------------------------------------------------------------------------
/** \brief Check for external keywords.
*/
int CheckKeywords(const char_type* a_szLine, ParserXBase& a_Parser)
{
string_type sLine(a_szLine);
if (sLine == _T("quit"))
{
return -1;
}
else if (sLine == _T("exprvar"))
{
ListExprVar(a_Parser);
return 1;
}
else if (sLine == _T("rpn"))
{
a_Parser.DumpRPN();
return 1;
}
return 0;
}
//---------------------------------------------------------------------------
void Calc()
{
ParserX parser(pckALL_NON_COMPLEX);
// ParserX parser(pckALL_COMPLEX);
// Create an array variable
Value arr1(3, 0);
arr1.At(0) = (float_type)1.0;
arr1.At(1) = (float_type)2.0;
arr1.At(2) = (float_type)3.0;
Value arr2(3, 0);
arr2.At(0) = (float_type)4.0;
arr2.At(1) = (float_type)3.0;
arr2.At(2) = (float_type)2.0;
Value arr3(4, 0);
arr3.At(0) = (float_type)1.0;
arr3.At(1) = (float_type)2.0;
arr3.At(2) = (float_type)3.0;
arr3.At(3) = (float_type)4.0;
Value arr4(3, 0);
arr4.At(0) = (float_type)4.0;
arr4.At(1) = false;
arr4.At(2) = _T("hallo");
// Create a 3x3 matrix with zero elements
Value m1(3, 3, 0);
m1.At(0, 0) = 1.0;
m1.At(1, 1) = 1.0;
m1.At(2, 2) = 1.0;
Value m2(3, 3, 0);
m2.At(0, 0) = 1.0;
m2.At(0, 1) = 2.0;
m2.At(0, 2) = 3.0;
m2.At(1, 0) = 4.0;
m2.At(1, 1) = 5.0;
m2.At(1, 2) = 6.0;
m2.At(2, 0) = 7.0;
m2.At(2, 1) = 8.0;
m2.At(2, 2) = 9.0;
Value val[5];
val[0] = (float_type)1.1;
val[1] = 1.0;
val[2] = false;
val[3] = _T("Hello");
val[4] = _T("World");
Value fVal[3];
fVal[0] = (int_type)1;
fVal[1] = (float_type)2.22;
fVal[2] = (float_type)3.33;
Value sVal[3];
sVal[0] = _T("hello");
sVal[1] = _T("world");
sVal[2] = _T("test");
Value cVal[3];
cVal[0] = mup::cmplx_type(1, 1);
cVal[1] = mup::cmplx_type(2, 2);
cVal[2] = mup::cmplx_type(3, 3);
Value ans;
parser.DefineVar(_T("ans"), Variable(&ans));
// some tests for vectors
parser.DefineVar(_T("va"), Variable(&arr1));
parser.DefineVar(_T("vb"), Variable(&arr2));
parser.DefineVar(_T("vc"), Variable(&arr3));
parser.DefineVar(_T("vd"), Variable(&arr4));
parser.DefineVar(_T("m1"), Variable(&m1));
parser.DefineVar(_T("m2"), Variable(&m2));
parser.DefineVar(_T("a"), Variable(&fVal[0]));
parser.DefineVar(_T("b"), Variable(&fVal[1]));
parser.DefineVar(_T("c"), Variable(&fVal[2]));
parser.DefineVar(_T("ca"), Variable(&cVal[0]));
parser.DefineVar(_T("cb"), Variable(&cVal[1]));
parser.DefineVar(_T("cc"), Variable(&cVal[2]));
parser.DefineVar(_T("sa"), Variable(&sVal[0]));
parser.DefineVar(_T("sb"), Variable(&sVal[1]));
parser.DefineConst(_T("organisation"), _T("whatever"));
// Add functions for inspecting the parser properties
parser.DefineFun(new FunListVar);
parser.DefineFun(new FunListFunctions);
parser.DefineFun(new FunListConst);
parser.DefineFun(new FunBenchmark);
parser.DefineFun(new FunEnableOptimizer);
parser.DefineFun(new FunSelfTest);
parser.DefineFun(new FunEnableDebugDump);
parser.DefineFun(new FunTest0);
parser.DefineFun(new FunPrint);
#if defined(MUP_USE_WIDE_STRING)
parser.DefineFun(new FunLang);
#endif
parser.EnableAutoCreateVar(true);
#ifdef _DEBUG
// ParserXBase::EnableDebugDump(1, 0);
#endif
Value x = 1.0;
Value y = std::complex<double>(0, 1);
parser.DefineVar(_T("x"), Variable(&x));
parser.DefineVar(_T("y"), Variable(&y));
for (;;)
{
try
{
console() << sPrompt;
string_type sLine;
std::getline(mup::console_in(), sLine);
if (sLine == _T("dbg"))
{
sLine = _T("{?{{{{:44");
mup::console() << sLine << endl;
}
switch (CheckKeywords(sLine.c_str(), parser))
{
case 0: break;
case 1: continue;
case -1: return;
}
parser.SetExpr(sLine);
// The returned result is of type Value, value is a Variant like
// type that can be either a boolean an integer or a floating point value
ans = parser.Eval();
{
// Value supports C++ streaming like this:
console() << _T("Result (type: '") << ans.GetType() << _T("'):\n");
console() << _T("ans = ") << ans << _T("\n");
/*
// Or if you need the specific type use this:
switch (ans.GetType())
{
case 's': { std::string s = ans.GetString(); console() << s << " (string)" << "\n"; } break;
case 'i': { int_type i = ans.GetInteger(); console() << i << " (int)" << "\n"; } break;
case 'f': { float_type f = ans.GetFloat(); console() << f << " (float)" << "\n"; } break;
case 'c': { std::complex<float_type> c = ans.GetComplex(); console() << c << " (complex)" << "\n"; } break;
case 'b': break;
}
*/
}
}
catch (ParserError& e)
{
if (e.GetPos() != -1)
{
string_type sMarker;
sMarker.insert(0, sPrompt.size() + e.GetPos(), ' ');
sMarker += _T("^\n");
console() << sMarker;
}
console() << e.GetMsg() << _T(" (Errc: ") << std::dec << e.GetCode() << _T(")") << _T("\n\n");
//if (e.GetContext().Ident.length())
// console() << _T("Ident.: ") << e.GetContext().Ident << _T("\n");
//if (e.GetToken().length())
// console() << _T("Token: \"") << e.GetToken() << _T("\"\n");
} // try / catch
} // for (;;)
}
//---------------------------------------------------------------------------
int main(int /*argc*/, char** /*argv*/)
{
Splash();
SelfTest();
#if defined(MUP_USE_WIDE_STRING)
// Internationalization requires UNICODE as translations do contain non ASCII
// Characters.
//ParserX::ResetErrorMessageProvider(new mup::ParserMessageProviderGerman);
#endif
try
{
Calc();
}
catch (ParserError& e)
{
// Only erros raised during the initialization will end up here
// expression related errors are treated in Calc()
console() << _T("Initialization error: ") << e.GetMsg() << endl;
}
catch (std::runtime_error&)
{
console() << _T("aborting...") << endl;
}
#ifdef MUP_LEAKAGE_REPORT
IToken::LeakageReport();
#endif
return 0;
}
|