Boost Matheval
Evaluate mathematical expressions using Boost.Spirit
matheval::Parser Class Reference

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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Parser() [1/2]

matheval::Parser::Parser ( )

Constructor.

◆ ~Parser() [1/2]

matheval::Parser::~Parser ( )

Destructor.

◆ Parser() [2/2]

matheval::Parser::Parser ( )

Constructor.

◆ ~Parser() [2/2]

matheval::Parser::~Parser ( )

Destructor.

Member Function Documentation

◆ evaluate() [1/2]

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.

Parameters
[in]stThe symbol table

Referenced by matheval::parse().

◆ evaluate() [2/2]

double matheval::Parser::evaluate ( std::map< std::string, double > const &  st = {})

Evaluate the abstract syntax tree for a given symbol table.

Parameters
[in]stThe symbol table

◆ optimize() [1/2]

void matheval::Parser::optimize ( )

Perform constant folding onto the abstract syntax tree.

◆ optimize() [2/2]

void matheval::Parser::optimize ( )

Perform constant folding onto the abstract syntax tree.

◆ parse() [1/2]

void matheval::Parser::parse ( std::string const &  expr)

Parse the mathematical expression into an abstract syntax tree.

Parameters
[in]exprThe expression given as a std::string

◆ parse() [2/2]

void matheval::Parser::parse ( std::string const &  expr)

Parse the mathematical expression into an abstract syntax tree.

Parameters
[in]exprThe expression given as a std::string

Referenced by matheval::parse().


The documentation for this class was generated from the following file: