The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_EXPRESSION_H_INCLUDED
  24. #define JUCE_EXPRESSION_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. A class for dynamically evaluating simple numeric expressions.
  28. This class can parse a simple C-style string expression involving floating point
  29. numbers, named symbols and functions. The basic arithmetic operations of +, -, *, /
  30. are supported, as well as parentheses, and any alphanumeric identifiers are
  31. assumed to be named symbols which will be resolved when the expression is
  32. evaluated.
  33. Expressions which use identifiers and functions require a subclass of
  34. Expression::Scope to be supplied when evaluating them, and this object
  35. is expected to be able to resolve the symbol names and perform the functions that
  36. are used.
  37. */
  38. class JUCE_API Expression
  39. {
  40. public:
  41. //==============================================================================
  42. /** Creates a simple expression with a value of 0. */
  43. Expression();
  44. /** Destructor. */
  45. ~Expression();
  46. /** Creates a copy of an expression. */
  47. Expression (const Expression&);
  48. /** Copies another expression. */
  49. Expression& operator= (const Expression&);
  50. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  51. Expression (Expression&&) noexcept;
  52. Expression& operator= (Expression&&) noexcept;
  53. #endif
  54. /** Creates a simple expression with a specified constant value. */
  55. explicit Expression (double constant);
  56. /** Attempts to create an expression by parsing a string.
  57. Any errors are returned in the parseError argument provided.
  58. */
  59. Expression (const String& stringToParse, String& parseError);
  60. /** Returns a string version of the expression. */
  61. String toString() const;
  62. /** Returns an expression which is an addition operation of two existing expressions. */
  63. Expression operator+ (const Expression&) const;
  64. /** Returns an expression which is a subtraction operation of two existing expressions. */
  65. Expression operator- (const Expression&) const;
  66. /** Returns an expression which is a multiplication operation of two existing expressions. */
  67. Expression operator* (const Expression&) const;
  68. /** Returns an expression which is a division operation of two existing expressions. */
  69. Expression operator/ (const Expression&) const;
  70. /** Returns an expression which performs a negation operation on an existing expression. */
  71. Expression operator-() const;
  72. /** Returns an Expression which is an identifier reference. */
  73. static Expression symbol (const String& symbol);
  74. /** Returns an Expression which is a function call. */
  75. static Expression function (const String& functionName, const Array<Expression>& parameters);
  76. /** Returns an Expression which parses a string from a character pointer, and updates the pointer
  77. to indicate where it finished.
  78. The pointer is incremented so that on return, it indicates the character that follows
  79. the end of the expression that was parsed.
  80. If there's a syntax error in parsing, the parseError argument will be set
  81. to a description of the problem.
  82. */
  83. static Expression parse (String::CharPointerType& stringToParse, String& parseError);
  84. //==============================================================================
  85. /** When evaluating an Expression object, this class is used to resolve symbols and
  86. perform functions that the expression uses.
  87. */
  88. class JUCE_API Scope
  89. {
  90. public:
  91. Scope();
  92. virtual ~Scope();
  93. /** Returns some kind of globally unique ID that identifies this scope. */
  94. virtual String getScopeUID() const;
  95. /** Returns the value of a symbol.
  96. If the symbol is unknown, this can throw an Expression::EvaluationError exception.
  97. The member value is set to the part of the symbol that followed the dot, if there is
  98. one, e.g. for "foo.bar", symbol = "foo" and member = "bar".
  99. @throws Expression::EvaluationError
  100. */
  101. virtual Expression getSymbolValue (const String& symbol) const;
  102. /** Executes a named function.
  103. If the function name is unknown, this can throw an Expression::EvaluationError exception.
  104. @throws Expression::EvaluationError
  105. */
  106. virtual double evaluateFunction (const String& functionName,
  107. const double* parameters, int numParameters) const;
  108. /** Used as a callback by the Scope::visitRelativeScope() method.
  109. You should never create an instance of this class yourself, it's used by the
  110. expression evaluation code.
  111. */
  112. class Visitor
  113. {
  114. public:
  115. virtual ~Visitor() {}
  116. virtual void visit (const Scope&) = 0;
  117. };
  118. /** Creates a Scope object for a named scope, and then calls a visitor
  119. to do some kind of processing with this new scope.
  120. If the name is valid, this method must create a suitable (temporary) Scope
  121. object to represent it, and must call the Visitor::visit() method with this
  122. new scope.
  123. */
  124. virtual void visitRelativeScope (const String& scopeName, Visitor& visitor) const;
  125. };
  126. /** Evaluates this expression, without using a Scope.
  127. Without a Scope, no symbols can be used, and only basic functions such as sin, cos, tan,
  128. min, max are available.
  129. To find out about any errors during evaluation, use the other version of this method which
  130. takes a String parameter.
  131. */
  132. double evaluate() const;
  133. /** Evaluates this expression, providing a scope that should be able to evaluate any symbols
  134. or functions that it uses.
  135. To find out about any errors during evaluation, use the other version of this method which
  136. takes a String parameter.
  137. */
  138. double evaluate (const Scope& scope) const;
  139. /** Evaluates this expression, providing a scope that should be able to evaluate any symbols
  140. or functions that it uses.
  141. */
  142. double evaluate (const Scope& scope, String& evaluationError) const;
  143. /** Attempts to return an expression which is a copy of this one, but with a constant adjusted
  144. to make the expression resolve to a target value.
  145. E.g. if the expression is "x + 10" and x is 5, then asking for a target value of 8 will return
  146. the expression "x + 3". Obviously some expressions can't be reversed in this way, in which
  147. case they might just be adjusted by adding a constant to the original expression.
  148. @throws Expression::EvaluationError
  149. */
  150. Expression adjustedToGiveNewResult (double targetValue, const Scope& scope) const;
  151. /** Represents a symbol that is used in an Expression. */
  152. struct Symbol
  153. {
  154. Symbol (const String& scopeUID, const String& symbolName);
  155. bool operator== (const Symbol&) const noexcept;
  156. bool operator!= (const Symbol&) const noexcept;
  157. String scopeUID; /**< The unique ID of the Scope that contains this symbol. */
  158. String symbolName; /**< The name of the symbol. */
  159. };
  160. /** Returns a copy of this expression in which all instances of a given symbol have been renamed. */
  161. Expression withRenamedSymbol (const Symbol& oldSymbol, const String& newName, const Scope& scope) const;
  162. /** Returns true if this expression makes use of the specified symbol.
  163. If a suitable scope is supplied, the search will dereference and recursively check
  164. all symbols, so that it can be determined whether this expression relies on the given
  165. symbol at any level in its evaluation. If the scope parameter is null, this just checks
  166. whether the expression contains any direct references to the symbol.
  167. @throws Expression::EvaluationError
  168. */
  169. bool referencesSymbol (const Symbol& symbol, const Scope& scope) const;
  170. /** Returns true if this expression contains any symbols. */
  171. bool usesAnySymbols() const;
  172. /** Returns a list of all symbols that may be needed to resolve this expression in the given scope. */
  173. void findReferencedSymbols (Array<Symbol>& results, const Scope& scope) const;
  174. //==============================================================================
  175. /** Expression type.
  176. @see Expression::getType()
  177. */
  178. enum Type
  179. {
  180. constantType,
  181. functionType,
  182. operatorType,
  183. symbolType
  184. };
  185. /** Returns the type of this expression. */
  186. Type getType() const noexcept;
  187. /** If this expression is a symbol, function or operator, this returns its identifier. */
  188. String getSymbolOrFunction() const;
  189. /** Returns the number of inputs to this expression.
  190. @see getInput
  191. */
  192. int getNumInputs() const;
  193. /** Retrieves one of the inputs to this expression.
  194. @see getNumInputs
  195. */
  196. Expression getInput (int index) const;
  197. private:
  198. //==============================================================================
  199. class Term;
  200. struct Helpers;
  201. friend class Term;
  202. friend struct Helpers;
  203. friend struct ContainerDeletePolicy<Term>;
  204. friend class ReferenceCountedObjectPtr<Term>;
  205. ReferenceCountedObjectPtr<Term> term;
  206. explicit Expression (Term*);
  207. };
  208. #endif // JUCE_EXPRESSION_H_INCLUDED