Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/jeremyko/expression-tree-with-user-functions
- Owner: jeremyko
- License: mit
- Created: 2014-03-13T09:55:24.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-11-30T05:48:27.000Z (almost 6 years ago)
- Last Synced: 2024-10-12T04:05:59.607Z (about 1 month ago)
- Topics: expression-tree
- Language: C++
- Size: 556 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 );
}
```