From b53b359c8d5079c996e47abafdf76781c4b1afc0 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 23 Jan 2009 22:28:56 +0000 Subject: Ugh, more fixes...this time I've disallowed ending with an operator, there were just too many cases where that was causing trouble. Now it seems like everything is working correctly again. --- src/formula.h | 123 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/src/formula.h b/src/formula.h index 1fe96f9..a0996ea 100644 --- a/src/formula.h +++ b/src/formula.h @@ -59,70 +59,73 @@ namespace Bu prec run( const Bu::FString &sFormulaSrc ) { - /* - if( !sOper.isEmpty() ) - printf("sOper.isEmpty() == false!\n"); - if( !sValue.isEmpty() ) - printf("sValue.isEmpty() == false!\n"); - if( !sFunc.isEmpty() ) - printf("sFunc.isEmpty() == false!\n"); - */ - const char *sFormula = sFormulaSrc.getStr(); - for(;;) + if( sFormulaSrc.isEmpty() ) + throw FormulaException("Empty formula, nothing to do."); + try { - uint8_t tNum = nextToken( &sFormula ); - if( tNum == symEOS ) - break; - else if( tNum == symSubtract ) - { - sOper.push( symNegate ); - continue; - } - else if( tNum == symNot ) - { - sOper.push( symNot ); - continue; - } - else if( tNum == symOpenParen ) - { - sOper.push( tNum ); - continue; - } - else if( tNum == symFunction ) - { - sOper.push( symFunction ); - continue; - } - - oppart: uint8_t tOpr = nextToken( &sFormula ); - if( tOpr == symEOS ) - { - reduce(); - prec ret = sValue.top(); - sValue.clear(); - sFunc.clear(); - sOper.clear(); - return ret; - } - if( !sOper.isEmpty() && getPrec( sOper.top() ) > getPrec( tOpr ) ) - { - reduce(); - } - if( tOpr != symCloseParen ) + const char *sFormula = sFormulaSrc.getStr(); + for(;;) { - sOper.push( tOpr ); - } - else - { - reduce( true ); - goto oppart; + uint8_t tNum = nextToken( &sFormula ); + if( tNum == symSubtract ) + { + sOper.push( symNegate ); + continue; + } + else if( tNum == symNot ) + { + sOper.push( symNot ); + continue; + } + else if( tNum == symOpenParen ) + { + sOper.push( tNum ); + continue; + } + else if( tNum == symFunction ) + { + sOper.push( symFunction ); + continue; + } + else if( tNum == symEOS ) + { + throw Bu::FormulaException( + "Cannot end with an operator."); + } + + oppart: uint8_t tOpr = nextToken( &sFormula ); + if( tOpr == symEOS ) + { + reduce(); + prec ret = sValue.top(); + sValue.clear(); + sFunc.clear(); + sOper.clear(); + return ret; + } + if( !sOper.isEmpty() && getPrec( sOper.top() ) > + getPrec( tOpr ) ) + { + reduce(); + } + if( tOpr != symCloseParen ) + { + sOper.push( tOpr ); + } + else + { + reduce( true ); + goto oppart; + } } } - prec ret = sValue.top(); - sValue.clear(); - sFunc.clear(); - sOper.clear(); - return ret; + catch( ... ) + { + sValue.clear(); + sFunc.clear(); + sOper.clear(); + throw; + } } varHash hVars; -- cgit v1.2.3