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.

252 lines
10KB

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