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.

ExpressionProgram.h 4.4KB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef LEPTON_EXPRESSION_PROGRAM_H_
  2. #define LEPTON_EXPRESSION_PROGRAM_H_
  3. /* -------------------------------------------------------------------------- *
  4. * Lepton *
  5. * -------------------------------------------------------------------------- *
  6. * This is part of the Lepton expression parser originating from *
  7. * Simbios, the NIH National Center for Physics-Based Simulation of *
  8. * Biological Structures at Stanford, funded under the NIH Roadmap for *
  9. * Medical Research, grant U54 GM072970. See https://simtk.org. *
  10. * *
  11. * Portions copyright (c) 2009 Stanford University and the Authors. *
  12. * Authors: Peter Eastman *
  13. * Contributors: *
  14. * *
  15. * Permission is hereby granted, free of charge, to any person obtaining a *
  16. * copy of this software and associated documentation files (the "Software"), *
  17. * to deal in the Software without restriction, including without limitation *
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  19. * and/or sell copies of the Software, and to permit persons to whom the *
  20. * Software is furnished to do so, subject to the following conditions: *
  21. * *
  22. * The above copyright notice and this permission notice shall be included in *
  23. * all copies or substantial portions of the Software. *
  24. * *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  28. * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  29. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  30. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  31. * USE OR OTHER DEALINGS IN THE SOFTWARE. *
  32. * -------------------------------------------------------------------------- */
  33. #include "ExpressionTreeNode.h"
  34. #include "windowsIncludes.h"
  35. #include <map>
  36. #include <string>
  37. #include <vector>
  38. namespace Lepton
  39. {
  40. class ParsedExpression;
  41. /**
  42. * An ExpressionProgram is a linear sequence of Operations for evaluating an expression. The evaluation
  43. * is done with a stack. The arguments to each Operation are first taken off the stack in order, then it is
  44. * evaluated and the result is pushed back onto the stack. At the end, the stack contains a single value,
  45. * which is the value of the expression.
  46. *
  47. * An ExpressionProgram is created by calling createProgram() on a ParsedExpression.
  48. */
  49. class LEPTON_EXPORT ExpressionProgram
  50. {
  51. public:
  52. ExpressionProgram();
  53. ExpressionProgram(const ExpressionProgram& program);
  54. ~ExpressionProgram();
  55. ExpressionProgram& operator=(const ExpressionProgram& program);
  56. /**
  57. * Get the number of Operations that make up this program.
  58. */
  59. int getNumOperations() const;
  60. /**
  61. * Get an Operation in this program.
  62. */
  63. const Operation& getOperation(int index) const;
  64. /**
  65. * Get the size of the stack needed to execute this program. This is the largest number of elements present
  66. * on the stack at any point during evaluation.
  67. */
  68. int getStackSize() const;
  69. /**
  70. * Evaluate the expression. If the expression involves any variables, this method will throw an exception.
  71. */
  72. double evaluate() const;
  73. /**
  74. * Evaluate the expression.
  75. *
  76. * @param variables a map specifying the values of all variables that appear in the expression. If any
  77. * variable appears in the expression but is not included in this map, an exception
  78. * will be thrown.
  79. */
  80. double evaluate(const std::map<std::string, double>& variables) const;
  81. private:
  82. friend class ParsedExpression;
  83. ExpressionProgram(const ParsedExpression& expression);
  84. void buildProgram(const ExpressionTreeNode& node);
  85. std::vector<Operation*> operations;
  86. int maxArgs, stackSize;
  87. };
  88. } // namespace Lepton
  89. #endif /*LEPTON_EXPRESSION_PROGRAM_H_*/