SXEval 1.0.3
A generic s-expression interpreter library.
Loading...
Searching...
No Matches
sxeval::SXEval< T > Class Template Reference

The SXEval class is used to evaluate s-expressions. More...

#include <SXEval.hpp>

Public Member Functions

 SXEval ()
 Default constructor.
 
template<typename OP >
void registerOperation ()
 Register an operation.
 
void build (const std::string &expression, const resolveVariable_t< T > &resolveVariable=resolveVariable_t< T >(), const resolveEncapsulated_t< T > &resolveEncapsulated=resolveEncapsulated_t< T >())
 Build the expression tree from a s-expression.
 
void build (const std::string &expression, const resolveEncapsulated_t< T > &resolveEncapsulated=resolveEncapsulated_t< T >(), const resolveVariable_t< T > &resolveVariable=resolveVariable_t< T >())
 Build the expression tree from a s-expression.
 
T execute () const
 Compute the result of the expression tree.
 
T interpret (const std::string &expression, const resolveVariable_t< T > &resolveVariable=resolveVariable_t< T >(), const resolveEncapsulated_t< T > &resolveEncapsulated=resolveEncapsulated_t< T >()) const
 Compute the result of an expression without building its tree.
 
T interpret (const std::string &expression, const resolveEncapsulated_t< T > &resolveEncapsulated=resolveEncapsulated_t< T >(), const resolveVariable_t< T > &resolveVariable=resolveVariable_t< T >()) const
 Compute the result of an expression without building its tree.
 
std::string toString () const
 Convert the expression tree to a string representation.
 

Detailed Description

template<typename T>
class sxeval::SXEval< T >

The SXEval class is used to evaluate s-expressions.

It allows user-custom operatinos as well as user-owned variables and encapsulated variables.

It can build an expression tree from a s-expression and evaluate it. It can also compute the result of an expression without building its tree, which is much slower than the evaluation but faster than building the tree. This latter method is useful for quick/unique evaluations without the overhead of building a tree while the former is useful for repeated evaluations of the same expression.

Template Parameters
TThe type of the values handle by the s-expression. The supported types are int, signed char, short int, long int, unsigned int, unsigned char, unsigned long int, float, double and long double.

Constructor & Destructor Documentation

◆ SXEval()

template<typename T >
sxeval::SXEval< T >::SXEval ( )
inline

Default constructor.

Member Function Documentation

◆ build() [1/2]

template<typename T >
void sxeval::SXEval< T >::build ( const std::string &  expression,
const resolveEncapsulated_t< T > &  resolveEncapsulated = resolveEncapsulated_t<T>(),
const resolveVariable_t< T > &  resolveVariable = resolveVariable_t<T>() 
)
inline

Build the expression tree from a s-expression.

Parameters
expressionThe expression to parse.
resolveEncapsulatedA function to resolve encapsulated variable names to their getter functions. The function should take a string as input and return a function that returns the variable's value when called. If the encapsulated variable is not found, an exception should be thrown.
resolveVariableA function to resolve variable names to their values. The function should take a string as input and return a reference to the variable's value. If the variable is not found, an exception should be thrown.
Exceptions
std::runtime_errorif a variable cannot be resolved or if the expression is invalid.
Note
This is an overload of the build() method that allows specifying the resolveEncapsulated function first, which can be useful if the resolveVariable function is not needed.

◆ build() [2/2]

template<typename T >
void sxeval::SXEval< T >::build ( const std::string &  expression,
const resolveVariable_t< T > &  resolveVariable = resolveVariable_t<T>(),
const resolveEncapsulated_t< T > &  resolveEncapsulated = resolveEncapsulated_t<T>() 
)

Build the expression tree from a s-expression.

Parameters
expressionThe expression to parse.
resolveVariableA function to resolve variable names to their values. The function should take a string as input and return a reference to the variable's value. If the variable is not found, an exception should be thrown.
resolveEncapsulatedA function to resolve encapsulated variable names to their getter functions. The function should take a string as input and return a function that returns the variable's value when called. If the encapsulated variable is not found, an exception should be thrown.
Exceptions
std::runtime_errorif a variable cannot be resolved or if the expression is invalid.

◆ execute()

template<typename T >
T sxeval::SXEval< T >::execute ( ) const

Compute the result of the expression tree.

Returns
The result of the computation.
Exceptions
std::runtime_errorif no operations have been registered or if the evaluation fails.
Note
build() must have been called before calling this method.

◆ interpret() [1/2]

template<typename T >
T sxeval::SXEval< T >::interpret ( const std::string &  expression,
const resolveEncapsulated_t< T > &  resolveEncapsulated = resolveEncapsulated_t<T>(),
const resolveVariable_t< T > &  resolveVariable = resolveVariable_t<T>() 
) const
inline

Compute the result of an expression without building its tree.

Parameters
expressionThe expression to compute.
resolveEncapsulatedA function to resolve encapsulated variable names to their getter functions. The function should take a string as input and return a function that returns the variable's value when called. If the encapsulated variable is not found, an exception should be thrown.
resolveVariableA function to resolve variable names to their values. The function should take a string as input and return a reference to the variable's value. If the variable is not found, an exception should be thrown.
Returns
The result of the computation.
Exceptions
std::runtime_errorif a variable cannot be resolved or if the expression is invalid.
Note
This method does not build the expression tree, it only computes the result of the expression. It is useful for quick/unique evaluations without the overhead of building a tree.
This is an overload of the interpret() method that allows specifying the resolveEncapsulated function first, which can be useful if the resolveVariable function is not needed.

◆ interpret() [2/2]

template<typename T >
T sxeval::SXEval< T >::interpret ( const std::string &  expression,
const resolveVariable_t< T > &  resolveVariable = resolveVariable_t<T>(),
const resolveEncapsulated_t< T > &  resolveEncapsulated = resolveEncapsulated_t<T>() 
) const

Compute the result of an expression without building its tree.

Parameters
expressionThe expression to compute.
resolveVariableA function to resolve variable names to their values. The function should take a string as input and return a reference to the variable's value. If the variable is not found, an exception should be thrown.
resolveEncapsulatedA function to resolve encapsulated variable names to their getter functions. The function should take a string as input and return a function that returns the variable's value when called. If the encapsulated variable is not found, an exception should be thrown.
Returns
The result of the computation.
Exceptions
std::runtime_errorif a variable cannot be resolved or if the expression is invalid.
Note
This method does not build the expression tree, it only computes the result of the expression. It is useful for quick/unique evaluations without the overhead of building a tree.

◆ registerOperation()

template<typename T >
template<typename OP >
void sxeval::SXEval< T >::registerOperation ( )
inline

Register an operation.

Template Parameters
OPThe operation to register. The operation must inherit from sxeval::AOperation<T>.

◆ toString()

template<typename T >
std::string sxeval::SXEval< T >::toString ( ) const

Convert the expression tree to a string representation.

Returns
A string representation of the expression tree.

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