Boost Matheval
Evaluate mathematical expressions using Boost.Spirit
|
Parse a mathematical expression. More...
#include <matheval.hpp>
Public Member Functions | |
Parser () | |
Constructor. More... | |
~Parser () | |
Destructor. More... | |
void | parse (std::string const &expr) |
Parse the mathematical expression into an abstract syntax tree. More... | |
void | optimize () |
Perform constant folding onto the abstract syntax tree. More... | |
double | evaluate (std::map< std::string, double > const &st=std::map< std::string, double >()) |
Evaluate the abstract syntax tree for a given symbol table. More... | |
Parser () | |
Constructor. More... | |
~Parser () | |
Destructor. More... | |
void | parse (std::string const &expr) |
Parse the mathematical expression into an abstract syntax tree. More... | |
void | optimize () |
Perform constant folding onto the abstract syntax tree. More... | |
double | evaluate (std::map< std::string, double > const &st={}) |
Evaluate the abstract syntax tree for a given symbol table. More... | |
Parse a mathematical expression.
This can parse and evaluate a mathematical expression for a given symbol table using Boost.Spirit X3. The templates of Boost.Spirit are very expensive to parse and instantiate, which is why we hide it behind an opaque pointer.
The drawback of this approach is that calls can no longer be inlined and because the pointer crosses translation unit boundaries, dereferencing it can also not be optimized out at compile time. We have to rely entirely on link-time optimization which might be not as good.
The pointer to the implementation is a std::unique_ptr which makes the class not copyable but only moveable. Copying shouldn't be required but is easy to implement.
Definition at line 25 of file matheval.hpp.
matheval::Parser::Parser | ( | ) |
Constructor.
matheval::Parser::~Parser | ( | ) |
Destructor.
matheval::Parser::Parser | ( | ) |
Constructor.
matheval::Parser::~Parser | ( | ) |
Destructor.
double matheval::Parser::evaluate | ( | std::map< std::string, double > const & | st = std::map< std::string, double >() | ) |
Evaluate the abstract syntax tree for a given symbol table.
[in] | st | The symbol table |
Referenced by matheval::parse().
double matheval::Parser::evaluate | ( | std::map< std::string, double > const & | st = {} | ) |
Evaluate the abstract syntax tree for a given symbol table.
[in] | st | The symbol table |
void matheval::Parser::optimize | ( | ) |
Perform constant folding onto the abstract syntax tree.
void matheval::Parser::optimize | ( | ) |
Perform constant folding onto the abstract syntax tree.
void matheval::Parser::parse | ( | std::string const & | expr | ) |
Parse the mathematical expression into an abstract syntax tree.
[in] | expr | The expression given as a std::string |
void matheval::Parser::parse | ( | std::string const & | expr | ) |
Parse the mathematical expression into an abstract syntax tree.
[in] | expr | The expression given as a std::string |
Referenced by matheval::parse().