Newer
Older

Michal Kravcenko
committed
/**
* DESCRIPTION OF THE FILE
*
* @author Michal Kravčenko
* @date 4.9.18 -
*/
#include "ExprtkWrapper.h"
ExprtkWrapper::ExprtkWrapper( std::string expression_string ) {
this->expression_str = expression_string;

Michal Kravcenko
committed
this->symbol_table = new symbol_table_t( );
this->symbol_table->add_variable("x", this->x);
this->symbol_table->add_variable("y", this->y);
this->symbol_table->add_variable("z", this->z);
this->symbol_table->add_variable("t", this->t);
this->symbol_table->add_variable("f", this->z);

Michal Kravcenko
committed
this->expression = new expression_t( );
this->expression->register_symbol_table( *this->symbol_table );
this->parser = new parser_t( );
parser->compile(this->expression_str, *this->expression );

Michal Kravcenko
committed
}
ExprtkWrapper::~ExprtkWrapper() {

Michal Kravcenko
committed
if( this->expression ){
delete this->expression;
this->expression = nullptr;
}
if( this->symbol_table ){
delete this->symbol_table;
this->symbol_table = nullptr;
}
if( this->parser ){
delete this->parser;
this->parser = nullptr;
}

Michal Kravcenko
committed
}
double ExprtkWrapper::eval(double x1, double x2, double x3, double x4) {
this->x = x1;
this->y = x2;
this->z = x3;
this->t = x4;
return this->expression->value();
}
double ExprtkWrapper::eval(std::vector<double> &p) {
if(p.size() > 0){
this->x = p[0];
}
if(p.size() > 1){
this->y = p[1];
}
if(p.size() > 2){
this->z = p[2];
}
if(p.size() > 3){
this->t = p[3];
}
double result = this->expression->value();
return result;