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