| Recursive Operator Demo Sample Code
void RecursiveDemo( void )
{
char theExpression[ 128 ];
LZObjectHandle hTheExpression = 0;
long lDefineOp = 0;
long lDefineOpA = 0;
long lDefineOpB = 0;
lzCreatePublicContext( "RecurseContext" );
lzSetContext( "RecurseContext", false );
///////////////////////////////////
// Direct Recursion
///////////////////////////////////
// Forward declare...
lDefineOp = lzDefineExpOp( kGlobalScope,
"fact",
"prefix",
"number",
"number x",
"sin",
NULL );
if ( lDefineOp )
{
cout << "Failed to load recursive operator.\r\n";
cout << lzGetLastErrorText() << "\r\n\n";
goto RecursiveDemoExit;
}
// Copy in the expression...
strcpy( theExpression, "if (x>0) then return (x * fact( x-1 )); else return 1; end" );
// Complete the definition...
lDefineOp = lzDefineExpOpStr( kGlobalScope,
"fact",
theExpression );
if ( lDefineOp )
{
cout << "Failed to load recursive operator.\r\n";
cout << lzGetLastErrorText() << "\r\n\n";
goto RecursiveDemoExit;
}
///////////////////////////////////
// Indirect Recursion
///////////////////////////////////
// Forward declare...
lDefineOpA = lzDefineExpOp( kGlobalScope,
"facta",
"prefix",
"number",
"number x",
"sin",
NULL );
if ( lDefineOpA )
{
cout << "Failed to load recursive operator.\r\n";
cout << lzGetLastErrorText() << "\r\n\n";
goto RecursiveDemoExit;
}
lDefineOpB = lzDefineExpOp( kGlobalScope,
"factb",
"prefix",
"number",
"number x",
"sin",
NULL );
if ( lDefineOpB )
{
cout << "Failed to load recursive operator.\r\n";
cout << lzGetLastErrorText() << "\r\n\n";
goto RecursiveDemoExit;
}
// Copy in the 'A' expression...
strcpy( theExpression, "if (x>0) then return (x * factb( x-1 )); else return 1; end" );
// Complete the definition...
lDefineOpA = lzDefineExpOpStr( kGlobalScope,
"facta",
theExpression );
if ( lDefineOpA )
{
cout << "Failed to load recursive operator.\r\n";
cout << lzGetLastErrorText() << "\r\n\n";
goto RecursiveDemoExit;
}
// Copy in the 'B' expression...
strcpy( theExpression, "if (x>0) then return (x * facta( x-1 )); else return 1; end" );
// Complete the definition...
lDefineOpB = lzDefineExpOpStr( kGlobalScope,
"factb",
theExpression );
if ( lDefineOpB )
{
cout << "Failed to load recursive operator.\r\n";
cout << lzGetLastErrorText() << "\r\n\n";
goto RecursiveDemoExit;
}
// Test direct recursion...
strcpy( theExpression, "fact( 6 )" );
if ( hTheExpression = lzParseExp( theExpression ) )
{
cout << " " << lzEvalExpHndlToStr( hTheExpression ) << "\r\n";
}
if ( lzGetLastErrorID() )
cout << lzGetLastErrorText() << "\r\n\n";
cout.flush();
// Test indirect recursion...
strcpy( theExpression, "facta( 6 )" );
if ( hTheExpression = lzParseExp( theExpression ) )
{
cout << " " << lzEvalExpHndlToStr( hTheExpression ) << "\r\n";
}
if ( lzGetLastErrorID() )
cout << lzGetLastErrorText() << "\r\n\n";
cout.flush();
RecursiveDemoExit:
lzDeleteOp( kGlobalScope, "fact" );
lzDeleteOp( kGlobalScope, "facta" );
lzDeleteOp( kGlobalScope, "factb" );
lzDestroyPublicContext( "RecurseContext" );
return;
}
|