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
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
class Expression::Term : public SingleThreadedReferenceCountedObject
{
public:
Term() {}
virtual ~Term() {}
virtual Type getType() const noexcept = 0;
virtual Term* clone() const = 0;
virtual ReferenceCountedObjectPtr<Term> resolve (const Scope&, int recursionDepth) = 0;
virtual String toString() const = 0;
virtual double toDouble() const { return 0; }
virtual int getInputIndexFor (const Term*) const { return -1; }
virtual int getOperatorPrecedence() const { return 0; }
virtual int getNumInputs() const { return 0; }
virtual Term* getInput (int) const { return nullptr; }
virtual ReferenceCountedObjectPtr<Term> negated();
virtual ReferenceCountedObjectPtr<Term> createTermToEvaluateInput (const Scope&, const Term* /*inputTerm*/,
double /*overallTarget*/, Term* /*topLevelTerm*/) const
{
jassertfalse;
return ReferenceCountedObjectPtr<Term>();
}
virtual String getName() const
{
jassertfalse; // You shouldn't call this for an expression that's not actually a function!
return {};
}
virtual void renameSymbol (const Symbol& oldSymbol, const String& newName, const Scope& scope, int recursionDepth)
{
for (int i = getNumInputs(); --i >= 0;)
getInput (i)->renameSymbol (oldSymbol, newName, scope, recursionDepth);
}
class SymbolVisitor
{
public:
virtual ~SymbolVisitor() {}
virtual void useSymbol (const Symbol&) = 0;
};
virtual void visitAllSymbols (SymbolVisitor& visitor, const Scope& scope, int recursionDepth)
{
for (int i = getNumInputs(); --i >= 0;)
getInput(i)->visitAllSymbols (visitor, scope, recursionDepth);
}
private:
JUCE_DECLARE_NON_COPYABLE (Term)
};
//==============================================================================
struct Expression::Helpers
{
using TermPtr = ReferenceCountedObjectPtr<Term>;
static void checkRecursionDepth (int depth)
{
if (depth > 256)
throw EvaluationError ("Recursive symbol references");
}
friend class Expression::Term;
//==============================================================================
/** An exception that can be thrown by Expression::evaluate(). */
class EvaluationError : public std::exception
{
public:
EvaluationError (const String& desc) : description (desc)
{
DBG ("Expression::EvaluationError: " + description);
}
String description;
};
//==============================================================================
class Constant : public Term
{
public:
Constant (double val, bool resolutionTarget)
: value (val), isResolutionTarget (resolutionTarget) {}
Type getType() const noexcept { return constantType; }
Term* clone() const { return new Constant (value, isResolutionTarget); }
TermPtr resolve (const Scope&, int) { return *this; }
double toDouble() const { return value; }
TermPtr negated() { return *new Constant (-value, isResolutionTarget); }
String toString() const
{
String s (value);
if (isResolutionTarget)
s = "@" + s;
return s;
}
double value;
bool isResolutionTarget;
};
//==============================================================================
class BinaryTerm : public Term
{
public:
BinaryTerm (TermPtr l, TermPtr r) : left (std::move (l)), right (std::move (r))
{
jassert (left != nullptr && right != nullptr);
}
int getInputIndexFor (const Term* possibleInput) const
{
return possibleInput == left ? 0 : (possibleInput == right ? 1 : -1);
}
Type getType() const noexcept { return operatorType; }
int getNumInputs() const { return 2; }
Term* getInput (int index) const { return index == 0 ? left.get() : (index == 1 ? right.get() : nullptr); }
virtual double performFunction (double left, double right) const = 0;
virtual void writeOperator (String& dest) const = 0;
TermPtr resolve (const Scope& scope, int recursionDepth)
{
return *new Constant (performFunction (left ->resolve (scope, recursionDepth)->toDouble(),
right->resolve (scope, recursionDepth)->toDouble()), false);
}
String toString() const
{
String s;
auto ourPrecendence = getOperatorPrecedence();
if (left->getOperatorPrecedence() > ourPrecendence)
s << '(' << left->toString() << ')';
else
s = left->toString();
writeOperator (s);
if (right->getOperatorPrecedence() >= ourPrecendence)
s << '(' << right->toString() << ')';
else
s << right->toString();
return s;
}
protected:
const TermPtr left, right;
TermPtr createDestinationTerm (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
{
jassert (input == left || input == right);
if (input != left && input != right)
return {};
if (auto dest = findDestinationFor (topLevelTerm, this))
return dest->createTermToEvaluateInput (scope, this, overallTarget, topLevelTerm);
return *new Constant (overallTarget, false);
}
};
//==============================================================================
class SymbolTerm : public Term
{
public:
explicit SymbolTerm (const String& sym) : symbol (sym) {}
TermPtr resolve (const Scope& scope, int recursionDepth)
{
checkRecursionDepth (recursionDepth);
return scope.getSymbolValue (symbol).term->resolve (scope, recursionDepth + 1);
}
Type getType() const noexcept { return symbolType; }
Term* clone() const { return new SymbolTerm (symbol); }
String toString() const { return symbol; }
String getName() const { return symbol; }
void visitAllSymbols (SymbolVisitor& visitor, const Scope& scope, int recursionDepth)
{
checkRecursionDepth (recursionDepth);
visitor.useSymbol (Symbol (scope.getScopeUID(), symbol));
scope.getSymbolValue (symbol).term->visitAllSymbols (visitor, scope, recursionDepth + 1);
}
void renameSymbol (const Symbol& oldSymbol, const String& newName, const Scope& scope, int /*recursionDepth*/)
{
if (oldSymbol.symbolName == symbol && scope.getScopeUID() == oldSymbol.scopeUID)
symbol = newName;
}
String symbol;
};
//==============================================================================
class Function : public Term
{
public:
explicit Function (const String& name) : functionName (name) {}
Function (const String& name, const Array<Expression>& params)
: functionName (name), parameters (params)
{}
Type getType() const noexcept { return functionType; }
Term* clone() const { return new Function (functionName, parameters); }
int getNumInputs() const { return parameters.size(); }
Term* getInput (int i) const { return parameters.getReference(i).term.get(); }
String getName() const { return functionName; }
TermPtr resolve (const Scope& scope, int recursionDepth)
{
checkRecursionDepth (recursionDepth);
double result = 0;
auto numParams = parameters.size();
if (numParams > 0)
{
HeapBlock<double> params (numParams);
for (int i = 0; i < numParams; ++i)
params[i] = parameters.getReference(i).term->resolve (scope, recursionDepth + 1)->toDouble();
result = scope.evaluateFunction (functionName, params, numParams);
}
else
{
result = scope.evaluateFunction (functionName, nullptr, 0);
}
return *new Constant (result, false);
}
int getInputIndexFor (const Term* possibleInput) const
{
for (int i = 0; i < parameters.size(); ++i)
if (parameters.getReference(i).term == possibleInput)
return i;
return -1;
}
String toString() const
{
if (parameters.size() == 0)
return functionName + "()";
String s (functionName + " (");
for (int i = 0; i < parameters.size(); ++i)
{
s << parameters.getReference(i).term->toString();
if (i < parameters.size() - 1)
s << ", ";
}
s << ')';
return s;
}
const String functionName;
Array<Expression> parameters;
};
//==============================================================================
class DotOperator : public BinaryTerm
{
public:
DotOperator (SymbolTerm* l, TermPtr r) : BinaryTerm (TermPtr (l), r) {}
TermPtr resolve (const Scope& scope, int recursionDepth)
{
checkRecursionDepth (recursionDepth);
EvaluationVisitor visitor (right, recursionDepth + 1);
scope.visitRelativeScope (getSymbol()->symbol, visitor);
return visitor.output;
}
Term* clone() const { return new DotOperator (getSymbol(), *right); }
String getName() const { return "."; }
int getOperatorPrecedence() const { return 1; }
void writeOperator (String& dest) const { dest << '.'; }
double performFunction (double, double) const { return 0.0; }
void visitAllSymbols (SymbolVisitor& visitor, const Scope& scope, int recursionDepth)
{
checkRecursionDepth (recursionDepth);
visitor.useSymbol (Symbol (scope.getScopeUID(), getSymbol()->symbol));
SymbolVisitingVisitor v (right, visitor, recursionDepth + 1);
try
{
scope.visitRelativeScope (getSymbol()->symbol, v);
}
catch (...) {}
}
void renameSymbol (const Symbol& oldSymbol, const String& newName, const Scope& scope, int recursionDepth)
{
checkRecursionDepth (recursionDepth);
getSymbol()->renameSymbol (oldSymbol, newName, scope, recursionDepth);
SymbolRenamingVisitor visitor (right, oldSymbol, newName, recursionDepth + 1);
try
{
scope.visitRelativeScope (getSymbol()->symbol, visitor);
}
catch (...) {}
}
private:
//==============================================================================
class EvaluationVisitor : public Scope::Visitor
{
public:
EvaluationVisitor (const TermPtr& t, const int recursion)
: input (t), output (t), recursionCount (recursion) {}
void visit (const Scope& scope) { output = input->resolve (scope, recursionCount); }
const TermPtr input;
TermPtr output;
const int recursionCount;
private:
JUCE_DECLARE_NON_COPYABLE (EvaluationVisitor)
};
class SymbolVisitingVisitor : public Scope::Visitor
{
public:
SymbolVisitingVisitor (const TermPtr& t, SymbolVisitor& v, const int recursion)
: input (t), visitor (v), recursionCount (recursion) {}
void visit (const Scope& scope) { input->visitAllSymbols (visitor, scope, recursionCount); }
private:
const TermPtr input;
SymbolVisitor& visitor;
const int recursionCount;
JUCE_DECLARE_NON_COPYABLE (SymbolVisitingVisitor)
};
class SymbolRenamingVisitor : public Scope::Visitor
{
public:
SymbolRenamingVisitor (const TermPtr& t, const Expression::Symbol& symbol_, const String& newName_, const int recursionCount_)
: input (t), symbol (symbol_), newName (newName_), recursionCount (recursionCount_) {}
void visit (const Scope& scope) { input->renameSymbol (symbol, newName, scope, recursionCount); }
private:
const TermPtr input;
const Symbol& symbol;
const String newName;
const int recursionCount;
JUCE_DECLARE_NON_COPYABLE (SymbolRenamingVisitor)
};
SymbolTerm* getSymbol() const { return static_cast<SymbolTerm*> (left.get()); }
JUCE_DECLARE_NON_COPYABLE (DotOperator)
};
//==============================================================================
class Negate : public Term
{
public:
explicit Negate (const TermPtr& t) : input (t)
{
jassert (t != nullptr);
}
Type getType() const noexcept { return operatorType; }
int getInputIndexFor (const Term* possibleInput) const { return possibleInput == input ? 0 : -1; }
int getNumInputs() const { return 1; }
Term* getInput (int index) const { return index == 0 ? input.get() : nullptr; }
Term* clone() const { return new Negate (*input->clone()); }
TermPtr resolve (const Scope& scope, int recursionDepth)
{
return *new Constant (-input->resolve (scope, recursionDepth)->toDouble(), false);
}
String getName() const { return "-"; }
TermPtr negated() { return input; }
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* t, double overallTarget, Term* topLevelTerm) const
{
ignoreUnused (t);
jassert (t == input);
const Term* const dest = findDestinationFor (topLevelTerm, this);
return *new Negate (dest == nullptr ? TermPtr (*new Constant (overallTarget, false))
: dest->createTermToEvaluateInput (scope, this, overallTarget, topLevelTerm));
}
String toString() const
{
if (input->getOperatorPrecedence() > 0)
return "-(" + input->toString() + ")";
return "-" + input->toString();
}
private:
const TermPtr input;
};
//==============================================================================
class Add : public BinaryTerm
{
public:
Add (TermPtr l, TermPtr r) : BinaryTerm (l, r) {}
Term* clone() const { return new Add (*left->clone(), *right->clone()); }
double performFunction (double lhs, double rhs) const { return lhs + rhs; }
int getOperatorPrecedence() const { return 3; }
String getName() const { return "+"; }
void writeOperator (String& dest) const { dest << " + "; }
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
{
if (auto newDest = createDestinationTerm (scope, input, overallTarget, topLevelTerm))
return *new Subtract (newDest, *(input == left ? right : left)->clone());
return {};
}
private:
JUCE_DECLARE_NON_COPYABLE (Add)
};
//==============================================================================
class Subtract : public BinaryTerm
{
public:
Subtract (TermPtr l, TermPtr r) : BinaryTerm (l, r) {}
Term* clone() const { return new Subtract (*left->clone(), *right->clone()); }
double performFunction (double lhs, double rhs) const { return lhs - rhs; }
int getOperatorPrecedence() const { return 3; }
String getName() const { return "-"; }
void writeOperator (String& dest) const { dest << " - "; }
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
{
if (auto newDest = createDestinationTerm (scope, input, overallTarget, topLevelTerm))
{
if (input == left)
return *new Add (*newDest, *right->clone());
return *new Subtract (*left->clone(), *newDest);
}
return {};
}
private:
JUCE_DECLARE_NON_COPYABLE (Subtract)
};
//==============================================================================
class Multiply : public BinaryTerm
{
public:
Multiply (TermPtr l, TermPtr r) : BinaryTerm (l, r) {}
Term* clone() const { return new Multiply (*left->clone(), *right->clone()); }
double performFunction (double lhs, double rhs) const { return lhs * rhs; }
String getName() const { return "*"; }
void writeOperator (String& dest) const { dest << " * "; }
int getOperatorPrecedence() const { return 2; }
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
{
if (auto newDest = createDestinationTerm (scope, input, overallTarget, topLevelTerm))
return *new Divide (newDest, *(input == left ? right : left)->clone());
return {};
}
JUCE_DECLARE_NON_COPYABLE (Multiply)
};
//==============================================================================
class Divide : public BinaryTerm
{
public:
Divide (TermPtr l, TermPtr r) : BinaryTerm (l, r) {}
Term* clone() const { return new Divide (*left->clone(), *right->clone()); }
double performFunction (double lhs, double rhs) const { return lhs / rhs; }
String getName() const { return "/"; }
void writeOperator (String& dest) const { dest << " / "; }
int getOperatorPrecedence() const { return 2; }
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
{
auto newDest = createDestinationTerm (scope, input, overallTarget, topLevelTerm);
if (newDest == nullptr)
return {};
if (input == left)
return *new Multiply (*newDest, *right->clone());
return *new Divide (*left->clone(), *newDest);
}
JUCE_DECLARE_NON_COPYABLE (Divide)
};
//==============================================================================
static Term* findDestinationFor (Term* const topLevel, const Term* const inputTerm)
{
const int inputIndex = topLevel->getInputIndexFor (inputTerm);
if (inputIndex >= 0)
return topLevel;
for (int i = topLevel->getNumInputs(); --i >= 0;)
{
Term* const t = findDestinationFor (topLevel->getInput (i), inputTerm);
if (t != nullptr)
return t;
}
return nullptr;
}
static Constant* findTermToAdjust (Term* const term, const bool mustBeFlagged)
{
jassert (term != nullptr);
if (term->getType() == constantType)
{
Constant* const c = static_cast<Constant*> (term);
if (c->isResolutionTarget || ! mustBeFlagged)
return c;
}
if (term->getType() == functionType)
return nullptr;
const int numIns = term->getNumInputs();
for (int i = 0; i < numIns; ++i)
{
Term* const input = term->getInput (i);
if (input->getType() == constantType)
{
Constant* const c = static_cast<Constant*> (input);
if (c->isResolutionTarget || ! mustBeFlagged)
return c;
}
}
for (int i = 0; i < numIns; ++i)
if (auto c = findTermToAdjust (term->getInput (i), mustBeFlagged))
return c;
return nullptr;
}
static bool containsAnySymbols (const Term& t)
{
if (t.getType() == Expression::symbolType)
return true;
for (int i = t.getNumInputs(); --i >= 0;)
if (containsAnySymbols (*t.getInput (i)))
return true;
return false;
}
//==============================================================================
class SymbolCheckVisitor : public Term::SymbolVisitor
{
public:
SymbolCheckVisitor (const Symbol& s) : symbol (s) {}
void useSymbol (const Symbol& s) { wasFound = wasFound || s == symbol; }
bool wasFound = false;
private:
const Symbol& symbol;
JUCE_DECLARE_NON_COPYABLE (SymbolCheckVisitor)
};
//==============================================================================
class SymbolListVisitor : public Term::SymbolVisitor
{
public:
SymbolListVisitor (Array<Symbol>& list_) : list (list_) {}
void useSymbol (const Symbol& s) { list.addIfNotAlreadyThere (s); }
private:
Array<Symbol>& list;
JUCE_DECLARE_NON_COPYABLE (SymbolListVisitor)
};
//==============================================================================
class Parser
{
public:
//==============================================================================
Parser (String::CharPointerType& stringToParse) : text (stringToParse)
{
}
TermPtr readUpToComma()
{
if (text.isEmpty())
return *new Constant (0.0, false);
auto e = readExpression();
if (e == nullptr || ((! readOperator (",")) && ! text.isEmpty()))
return parseError ("Syntax error: \"" + String (text) + "\"");
return e;
}
String error;
private:
String::CharPointerType& text;
TermPtr parseError (const String& message)
{
if (error.isEmpty())
error = message;
return {};
}
//==============================================================================
static inline bool isDecimalDigit (const juce_wchar c) noexcept
{
return c >= '0' && c <= '9';
}
bool readChar (const juce_wchar required) noexcept
{
if (*text == required)
{
++text;
return true;
}
return false;
}
bool readOperator (const char* ops, char* const opType = nullptr) noexcept
{
text = text.findEndOfWhitespace();
while (*ops != 0)
{
if (readChar ((juce_wchar) (uint8) *ops))
{
if (opType != nullptr)
*opType = *ops;
return true;
}
++ops;
}
return false;
}
bool readIdentifier (String& identifier) noexcept
{
text = text.findEndOfWhitespace();
auto t = text;
int numChars = 0;
if (t.isLetter() || *t == '_')
{
++t;
++numChars;
while (t.isLetterOrDigit() || *t == '_')
{
++t;
++numChars;
}
}
if (numChars > 0)
{
identifier = String (text, (size_t) numChars);
text = t;
return true;
}
return false;
}
Term* readNumber() noexcept
{
text = text.findEndOfWhitespace();
auto t = text;
bool isResolutionTarget = (*t == '@');
if (isResolutionTarget)
{
++t;
t = t.findEndOfWhitespace();
text = t;
}
if (*t == '-')
{
++t;
t = t.findEndOfWhitespace();
}
if (isDecimalDigit (*t) || (*t == '.' && isDecimalDigit (t[1])))
return new Constant (CharacterFunctions::readDoubleValue (text), isResolutionTarget);
return nullptr;
}
TermPtr readExpression()
{
auto lhs = readMultiplyOrDivideExpression();
char opType;
while (lhs != nullptr && readOperator ("+-", &opType))
{
auto rhs = readMultiplyOrDivideExpression();
if (rhs == nullptr)
return parseError ("Expected expression after \"" + String::charToString ((juce_wchar) (uint8) opType) + "\"");
if (opType == '+')
lhs = *new Add (lhs, rhs);
else
lhs = *new Subtract (lhs, rhs);
}
return lhs;
}
TermPtr readMultiplyOrDivideExpression()
{
auto lhs = readUnaryExpression();
char opType;
while (lhs != nullptr && readOperator ("*/", &opType))
{
TermPtr rhs (readUnaryExpression());
if (rhs == nullptr)
return parseError ("Expected expression after \"" + String::charToString ((juce_wchar) (uint8) opType) + "\"");
if (opType == '*')
lhs = *new Multiply (lhs, rhs);
else
lhs = *new Divide (lhs, rhs);
}
return lhs;
}
TermPtr readUnaryExpression()
{
char opType;
if (readOperator ("+-", &opType))
{
TermPtr e (readUnaryExpression());
if (e == nullptr)
return parseError ("Expected expression after \"" + String::charToString ((juce_wchar) (uint8) opType) + "\"");
if (opType == '-')
e = e->negated();
return e;
}
return readPrimaryExpression();
}
TermPtr readPrimaryExpression()
{
if (auto e = readParenthesisedExpression())
return e;
if (auto e = readNumber())
return e;
return readSymbolOrFunction();
}
TermPtr readSymbolOrFunction()
{
String identifier;
if (readIdentifier (identifier))
{
if (readOperator ("(")) // method call...
{
auto f = new Function (identifier);
std::unique_ptr<Term> func (f); // (can't use std::unique_ptr<Function> in MSVC)
auto param = readExpression();
if (param == nullptr)
{
if (readOperator (")"))
return TermPtr (func.release());
return parseError ("Expected parameters after \"" + identifier + " (\"");
}
f->parameters.add (Expression (param.get()));
while (readOperator (","))
{
param = readExpression();
if (param == nullptr)
return parseError ("Expected expression after \",\"");
f->parameters.add (Expression (param.get()));
}
if (readOperator (")"))
return TermPtr (func.release());
return parseError ("Expected \")\"");
}
if (readOperator ("."))
{
TermPtr rhs (readSymbolOrFunction());
if (rhs == nullptr)
return parseError ("Expected symbol or function after \".\"");
if (identifier == "this")
return rhs;
return *new DotOperator (new SymbolTerm (identifier), rhs);
}
// just a symbol..
jassert (identifier.trim() == identifier);
return *new SymbolTerm (identifier);
}
return {};
}
TermPtr readParenthesisedExpression()
{
if (! readOperator ("("))
return {};
auto e = readExpression();
if (e == nullptr || ! readOperator (")"))
return {};
return e;
}
JUCE_DECLARE_NON_COPYABLE (Parser)
};
};
//==============================================================================
Expression::Expression()
: term (new Expression::Helpers::Constant (0, false))
{
}
Expression::~Expression()
{
}
Expression::Expression (Term* t) : term (t)
{
jassert (term != nullptr);
}
Expression::Expression (const double constant)
: term (new Expression::Helpers::Constant (constant, false))
{
}
Expression::Expression (const Expression& other)
: term (other.term)
{
}
Expression& Expression::operator= (const Expression& other)
{
term = other.term;
return *this;
}
Expression::Expression (Expression&& other) noexcept
: term (std::move (other.term))
{
}
Expression& Expression::operator= (Expression&& other) noexcept
{
term = std::move (other.term);
return *this;
}
Expression::Expression (const String& stringToParse, String& parseError)
{
auto text = stringToParse.getCharPointer();
Helpers::Parser parser (text);
term = parser.readUpToComma();
parseError = parser.error;
}
Expression Expression::parse (String::CharPointerType& stringToParse, String& parseError)
{
Helpers::Parser parser (stringToParse);
Expression e (parser.readUpToComma().get());
parseError = parser.error;
return e;
}
double Expression::evaluate() const
{
return evaluate (Expression::Scope());
}
double Expression::evaluate (const Expression::Scope& scope) const
{
String err;
return evaluate (scope, err);
}
double Expression::evaluate (const Scope& scope, String& evaluationError) const
{
try
{
return term->resolve (scope, 0)->toDouble();
}
catch (Helpers::EvaluationError& e)
{
evaluationError = e.description;
}
return 0;
}
Expression Expression::operator+ (const Expression& other) const { return Expression (new Helpers::Add (term, other.term)); }
Expression Expression::operator- (const Expression& other) const { return Expression (new Helpers::Subtract (term, other.term)); }
Expression Expression::operator* (const Expression& other) const { return Expression (new Helpers::Multiply (term, other.term)); }
Expression Expression::operator/ (const Expression& other) const { return Expression (new Helpers::Divide (term, other.term)); }
Expression Expression::operator-() const { return Expression (term->negated().get()); }
Expression Expression::symbol (const String& symbol) { return Expression (new Helpers::SymbolTerm (symbol)); }
Expression Expression::function (const String& functionName, const Array<Expression>& parameters)
{
return Expression (new Helpers::Function (functionName, parameters));
}
Expression Expression::adjustedToGiveNewResult (const double targetValue, const Expression::Scope& scope) const
{
std::unique_ptr<Term> newTerm (term->clone());
auto termToAdjust = Helpers::findTermToAdjust (newTerm.get(), true);
if (termToAdjust == nullptr)
termToAdjust = Helpers::findTermToAdjust (newTerm.get(), false);
if (termToAdjust == nullptr)
{
newTerm.reset (new Helpers::Add (*newTerm.release(), *new Helpers::Constant (0, false)));
termToAdjust = Helpers::findTermToAdjust (newTerm.get(), false);
}
jassert (termToAdjust != nullptr);
if (const Term* parent = Helpers::findDestinationFor (newTerm.get(), termToAdjust))
{
if (Helpers::TermPtr reverseTerm = parent->createTermToEvaluateInput (scope, termToAdjust, targetValue, newTerm.get()))
termToAdjust->value = Expression (reverseTerm.get()).evaluate (scope);
else
return Expression (targetValue);
}
else
{
termToAdjust->value = targetValue;
}
return Expression (newTerm.release());
}
Expression Expression::withRenamedSymbol (const Expression::Symbol& oldSymbol, const String& newName, const Scope& scope) const
{
jassert (newName.toLowerCase().containsOnly ("abcdefghijklmnopqrstuvwxyz0123456789_"));
if (oldSymbol.symbolName == newName)
return *this;
Expression e (term->clone());
e.term->renameSymbol (oldSymbol, newName, scope, 0);
return e;
}
bool Expression::referencesSymbol (const Expression::Symbol& symbolToCheck, const Scope& scope) const
{
Helpers::SymbolCheckVisitor visitor (symbolToCheck);
try
{
term->visitAllSymbols (visitor, scope, 0);
}
catch (Helpers::EvaluationError&)
{}
return visitor.wasFound;
}
void Expression::findReferencedSymbols (Array<Symbol>& results, const Scope& scope) const
{
try
{
Helpers::SymbolListVisitor visitor (results);
term->visitAllSymbols (visitor, scope, 0);
}
catch (Helpers::EvaluationError&)
{}
}
String Expression::toString() const { return term->toString(); }
bool Expression::usesAnySymbols() const { return Helpers::containsAnySymbols (*term); }
Expression::Type Expression::getType() const noexcept { return term->getType(); }
String Expression::getSymbolOrFunction() const { return term->getName(); }
int Expression::getNumInputs() const { return term->getNumInputs(); }
Expression Expression::getInput (int index) const { return Expression (term->getInput (index)); }
//==============================================================================
ReferenceCountedObjectPtr<Expression::Term> Expression::Term::negated()
{
return *new Helpers::Negate (*this);
}
//==============================================================================
Expression::Symbol::Symbol (const String& scope, const String& symbol)
: scopeUID (scope), symbolName (symbol)
{
}
bool Expression::Symbol::operator== (const Symbol& other) const noexcept
{
return symbolName == other.symbolName && scopeUID == other.scopeUID;
}
bool Expression::Symbol::operator!= (const Symbol& other) const noexcept
{
return ! operator== (other);
}
//==============================================================================
Expression::Scope::Scope() {}
Expression::Scope::~Scope() {}
Expression Expression::Scope::getSymbolValue (const String& symbol) const
{
if (symbol.isNotEmpty())
throw Helpers::EvaluationError ("Unknown symbol: " + symbol);
return Expression();
}
double Expression::Scope::evaluateFunction (const String& functionName, const double* parameters, int numParams) const
{
if (numParams > 0)
{
if (functionName == "min")
{
double v = parameters[0];
for (int i = 1; i < numParams; ++i)
v = jmin (v, parameters[i]);
return v;
}
if (functionName == "max")
{
double v = parameters[0];
for (int i = 1; i < numParams; ++i)
v = jmax (v, parameters[i]);
return v;
}
if (numParams == 1)
{
if (functionName == "sin") return std::sin (parameters[0]);
if (functionName == "cos") return std::cos (parameters[0]);
if (functionName == "tan") return std::tan (parameters[0]);
if (functionName == "abs") return std::abs (parameters[0]);
}
}
throw Helpers::EvaluationError ("Unknown function: \"" + functionName + "\"");
}
void Expression::Scope::visitRelativeScope (const String& scopeName, Visitor&) const
{
throw Helpers::EvaluationError ("Unknown symbol: " + scopeName);
}
String Expression::Scope::getScopeUID() const
{
return {};
}
} // namespace juce
|