Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jeremyko/expression-tree-with-user-functions

c++ expression tree library
https://github.com/jeremyko/expression-tree-with-user-functions

expression-tree

Last synced: 4 days ago
JSON representation

c++ expression tree library

Awesome Lists containing this project

README

        

# c++ expression tree library

http://jeremyko.blogspot.kr/2014/03/c-expression-tree.html

# Usage

## compile
```
mkdir build
cd build
cmake ..
make
```

## general arithmetic
```cpp
ExpressionTree expTree;
expression_result* pExpressionRslt;
bool bRslt = false;

bRslt = expTree.SetInfixExpression("1 * -2 ");
EXPECT_TRUE(bRslt);
if (bRslt)
{
bRslt = expTree.EvaluateExpression();
EXPECT_TRUE(bRslt);
pExpressionRslt = expTree.GetResult();
cout << "expTree.EvaluateExpression returns :"
<< pExpressionRslt->nResultLong << "\n";
EXPECT_EQ(-2, pExpressionRslt->nResultLong);
}
```
## custom user functions
```cpp
//see user_functions.h, user_functions.cpp
//-----------------------------------------------------
bRslt = expTree.SetInfixExpression("SumInt4(SumInt4(1,1+1,1+1,2), 1+1, 1+1, 1+1)");
EXPECT_TRUE(bRslt);
bRslt = expTree.EvaluateExpression();
EXPECT_TRUE(bRslt);
pExpressionRslt = expTree.GetResult();
cout << "expTree.EvaluateExpression returns :"
<< pExpressionRslt->nResultLong << "\n";
EXPECT_EQ(13, pExpressionRslt->nResultLong);

//-----------------------------------------------------
bRslt = expTree.SetInfixExpression("GetBool('1'='1')");
EXPECT_TRUE(bRslt);
bRslt = expTree.EvaluateExpression();
EXPECT_TRUE(bRslt);

//-----------------------------------------------------
bRslt = expTree.SetInfixExpression("StrCat('ab','cd')=StrCat('a','b')+StrCat('c','d') ");
EXPECT_TRUE(bRslt);
bRslt = expTree.EvaluateExpression();
EXPECT_TRUE(bRslt);

//-----------------------------------------------------
bRslt = expTree.SetInfixExpression("StrCat3('1','2',StrCat3('a','b',StrCat3('X','Y','Z')) )");
EXPECT_TRUE(bRslt);
bRslt = expTree.EvaluateExpression();
EXPECT_TRUE(bRslt);
EXPECT_EQ(4, expTree.GetDepth());
pExpressionRslt = expTree.GetResult();
cout << "expTree.EvaluateExpression returns :" << pExpressionRslt->strResult << "\n";
EXPECT_STREQ("12abXYZ", pExpressionRslt->strResult);
```

## using placeholder
```cpp
// placeholder identifier is :$ph1, :$ph2, .... :$ph50 (max)
bRslt = expTree.SetInfixExpression ( ":$ph1+:$ph2" );
EXPECT_TRUE ( bRslt );
if ( bRslt )
{
expTree.SetNumberLongValueOfPlaceHolder ( 1, 1 );
expTree.SetNumberLongValueOfPlaceHolder ( 2, 2 );
bRslt = expTree.EvaluateExpression ( );
EXPECT_TRUE ( bRslt );
pExpressionRslt = expTree.GetResult ( );
cout << "expTree.EvaluateExpression returns :" << pExpressionRslt->nResultLong << "\n";
EXPECT_EQ ( 3, pExpressionRslt->nResultLong );

//-----------------------------------------------------
//there's no rebuild tree
expTree.SetNumberFloatValueOfPlaceHolder ( 1, 2.0f );
expTree.SetNumberFloatValueOfPlaceHolder ( 2, 3.0f );
bRslt = expTree.EvaluateExpression ( );
EXPECT_TRUE ( bRslt );
pExpressionRslt = expTree.GetResult ( );
cout << "expTree.EvaluateExpression returns :" << pExpressionRslt->nResultFloat << "\n";
EXPECT_FLOAT_EQ ( 5.0f, pExpressionRslt->nResultFloat );

//-----------------------------------------------------
expTree.SetStringValueOfPlaceHolder ( 1, "A" );
expTree.SetStringValueOfPlaceHolder ( 2, "B" );
bRslt = expTree.EvaluateExpression ( );
EXPECT_TRUE ( bRslt );
pExpressionRslt = expTree.GetResult ( );
cout << "expTree.EvaluateExpression returns :" << pExpressionRslt->strResult << "\n";
EXPECT_STREQ ( "AB", pExpressionRslt->strResult );
}
```