diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/formula.h | 123 |
1 files 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 | |||
| 59 | 59 | ||
| 60 | prec run( const Bu::FString &sFormulaSrc ) | 60 | prec run( const Bu::FString &sFormulaSrc ) |
| 61 | { | 61 | { |
| 62 | /* | 62 | if( sFormulaSrc.isEmpty() ) |
| 63 | if( !sOper.isEmpty() ) | 63 | throw FormulaException("Empty formula, nothing to do."); |
| 64 | printf("sOper.isEmpty() == false!\n"); | 64 | try |
| 65 | if( !sValue.isEmpty() ) | ||
| 66 | printf("sValue.isEmpty() == false!\n"); | ||
| 67 | if( !sFunc.isEmpty() ) | ||
| 68 | printf("sFunc.isEmpty() == false!\n"); | ||
| 69 | */ | ||
| 70 | const char *sFormula = sFormulaSrc.getStr(); | ||
| 71 | for(;;) | ||
| 72 | { | 65 | { |
| 73 | uint8_t tNum = nextToken( &sFormula ); | 66 | const char *sFormula = sFormulaSrc.getStr(); |
| 74 | if( tNum == symEOS ) | 67 | for(;;) |
| 75 | break; | ||
| 76 | else if( tNum == symSubtract ) | ||
| 77 | { | ||
| 78 | sOper.push( symNegate ); | ||
| 79 | continue; | ||
| 80 | } | ||
| 81 | else if( tNum == symNot ) | ||
| 82 | { | ||
| 83 | sOper.push( symNot ); | ||
| 84 | continue; | ||
| 85 | } | ||
| 86 | else if( tNum == symOpenParen ) | ||
| 87 | { | ||
| 88 | sOper.push( tNum ); | ||
| 89 | continue; | ||
| 90 | } | ||
| 91 | else if( tNum == symFunction ) | ||
| 92 | { | ||
| 93 | sOper.push( symFunction ); | ||
| 94 | continue; | ||
| 95 | } | ||
| 96 | |||
| 97 | oppart: uint8_t tOpr = nextToken( &sFormula ); | ||
| 98 | if( tOpr == symEOS ) | ||
| 99 | { | ||
| 100 | reduce(); | ||
| 101 | prec ret = sValue.top(); | ||
| 102 | sValue.clear(); | ||
| 103 | sFunc.clear(); | ||
| 104 | sOper.clear(); | ||
| 105 | return ret; | ||
| 106 | } | ||
| 107 | if( !sOper.isEmpty() && getPrec( sOper.top() ) > getPrec( tOpr ) ) | ||
| 108 | { | ||
| 109 | reduce(); | ||
| 110 | } | ||
| 111 | if( tOpr != symCloseParen ) | ||
| 112 | { | 68 | { |
| 113 | sOper.push( tOpr ); | 69 | uint8_t tNum = nextToken( &sFormula ); |
| 114 | } | 70 | if( tNum == symSubtract ) |
| 115 | else | 71 | { |
| 116 | { | 72 | sOper.push( symNegate ); |
| 117 | reduce( true ); | 73 | continue; |
| 118 | goto oppart; | 74 | } |
| 75 | else if( tNum == symNot ) | ||
| 76 | { | ||
| 77 | sOper.push( symNot ); | ||
| 78 | continue; | ||
| 79 | } | ||
| 80 | else if( tNum == symOpenParen ) | ||
| 81 | { | ||
| 82 | sOper.push( tNum ); | ||
| 83 | continue; | ||
| 84 | } | ||
| 85 | else if( tNum == symFunction ) | ||
| 86 | { | ||
| 87 | sOper.push( symFunction ); | ||
| 88 | continue; | ||
| 89 | } | ||
| 90 | else if( tNum == symEOS ) | ||
| 91 | { | ||
| 92 | throw Bu::FormulaException( | ||
| 93 | "Cannot end with an operator."); | ||
| 94 | } | ||
| 95 | |||
| 96 | oppart: uint8_t tOpr = nextToken( &sFormula ); | ||
| 97 | if( tOpr == symEOS ) | ||
| 98 | { | ||
| 99 | reduce(); | ||
| 100 | prec ret = sValue.top(); | ||
| 101 | sValue.clear(); | ||
| 102 | sFunc.clear(); | ||
| 103 | sOper.clear(); | ||
| 104 | return ret; | ||
| 105 | } | ||
| 106 | if( !sOper.isEmpty() && getPrec( sOper.top() ) > | ||
| 107 | getPrec( tOpr ) ) | ||
| 108 | { | ||
| 109 | reduce(); | ||
| 110 | } | ||
| 111 | if( tOpr != symCloseParen ) | ||
| 112 | { | ||
| 113 | sOper.push( tOpr ); | ||
| 114 | } | ||
| 115 | else | ||
| 116 | { | ||
| 117 | reduce( true ); | ||
| 118 | goto oppart; | ||
| 119 | } | ||
| 119 | } | 120 | } |
| 120 | } | 121 | } |
| 121 | prec ret = sValue.top(); | 122 | catch( ... ) |
| 122 | sValue.clear(); | 123 | { |
| 123 | sFunc.clear(); | 124 | sValue.clear(); |
| 124 | sOper.clear(); | 125 | sFunc.clear(); |
| 125 | return ret; | 126 | sOper.clear(); |
| 127 | throw; | ||
| 128 | } | ||
| 126 | } | 129 | } |
| 127 | 130 | ||
| 128 | varHash hVars; | 131 | varHash hVars; |
