diff options
| -rw-r--r-- | src/formula.cpp | 4 | ||||
| -rw-r--r-- | src/formula.h | 399 | ||||
| -rw-r--r-- | src/logger.cpp | 16 | ||||
| -rw-r--r-- | src/logger.h | 11 | ||||
| -rw-r--r-- | src/stack.cpp | 1 | ||||
| -rw-r--r-- | src/stack.h | 71 | ||||
| -rw-r--r-- | src/tests/formula.cpp | 11 |
7 files changed, 503 insertions, 10 deletions
diff --git a/src/formula.cpp b/src/formula.cpp new file mode 100644 index 0000000..c32386e --- /dev/null +++ b/src/formula.cpp | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #include "formula.h" | ||
| 2 | |||
| 3 | subExceptionDef( ParseException ); | ||
| 4 | |||
diff --git a/src/formula.h b/src/formula.h new file mode 100644 index 0000000..3809508 --- /dev/null +++ b/src/formula.h | |||
| @@ -0,0 +1,399 @@ | |||
| 1 | #ifndef FORMULA_H | ||
| 2 | #define FORMULA_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | |||
| 6 | #include <math.h> | ||
| 7 | //#include "sbuffer.h" | ||
| 8 | |||
| 9 | #include "bu/stack.h" | ||
| 10 | #include "bu/exceptionbase.h" | ||
| 11 | #include "bu/hash.h" | ||
| 12 | #include "bu/fstring.h" | ||
| 13 | |||
| 14 | subExceptionDecl( ParseException ); | ||
| 15 | |||
| 16 | namespace Bu | ||
| 17 | { | ||
| 18 | /** | ||
| 19 | * Implements a very simple formula parser that allows use of variables and | ||
| 20 | * custom functions. This is based on a simple calculator-type parser that | ||
| 21 | * executes as it processes, accounting for operator precedence and | ||
| 22 | * grouping. | ||
| 23 | * | ||
| 24 | * prec = precision, a type to use for all math (except binary ops) | ||
| 25 | * bin = binary type, a type to hard cast all data to for binary ops | ||
| 26 | */ | ||
| 27 | template<typename prec, typename bin=uint32_t> | ||
| 28 | class Formula | ||
| 29 | { | ||
| 30 | public: | ||
| 31 | Formula() | ||
| 32 | { | ||
| 33 | } | ||
| 34 | |||
| 35 | virtual ~Formula() | ||
| 36 | { | ||
| 37 | } | ||
| 38 | |||
| 39 | prec run( const char *sFormula ) | ||
| 40 | { | ||
| 41 | for(;;) | ||
| 42 | { | ||
| 43 | uint8_t tNum = nextToken( &sFormula ); | ||
| 44 | if( tNum == symEOS ) | ||
| 45 | break; | ||
| 46 | else if( tNum == symSubtract ) | ||
| 47 | { | ||
| 48 | tNum = nextToken( &sFormula ); | ||
| 49 | if( tNum != symNumber ) | ||
| 50 | throw ParseException( | ||
| 51 | "Unary minus must be followed by a number, " | ||
| 52 | "variable, function, or parenthesis."); | ||
| 53 | sValue.top() = -sValue.top(); | ||
| 54 | } | ||
| 55 | else if( tNum == symNot ) | ||
| 56 | { | ||
| 57 | tNum = nextToken( &sFormula ); | ||
| 58 | if( tNum != symNumber ) | ||
| 59 | throw ParseException( | ||
| 60 | "Unary, binary not must be followed by a number, " | ||
| 61 | "variable, function, or parenthesis."); | ||
| 62 | sValue.top() = static_cast<prec>( | ||
| 63 | ~static_cast<bin>(sValue.top()) | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | else if( tNum == symOpenParen ) | ||
| 67 | { | ||
| 68 | sOper.push( tNum ); | ||
| 69 | continue; | ||
| 70 | } | ||
| 71 | |||
| 72 | oppart: uint8_t tOpr = nextToken( &sFormula ); | ||
| 73 | if( tOpr == symEOS ) | ||
| 74 | { | ||
| 75 | //printf("EOS "); | ||
| 76 | reduce(); | ||
| 77 | return sValue.top(); | ||
| 78 | break; | ||
| 79 | } | ||
| 80 | if( !sOper.isEmpty() && getPrec( sOper.top() ) > getPrec( tOpr ) ) | ||
| 81 | { | ||
| 82 | reduce(); | ||
| 83 | } | ||
| 84 | if( tOpr != symCloseParen ) | ||
| 85 | { | ||
| 86 | sOper.push( tOpr ); | ||
| 87 | } | ||
| 88 | else | ||
| 89 | { | ||
| 90 | reduce( true ); | ||
| 91 | goto oppart; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | return sValue.top(); | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | typedef Hash<Bu::FString, prec> varHash; | ||
| 99 | varHash hVars; | ||
| 100 | |||
| 101 | typedef struct Func | ||
| 102 | { | ||
| 103 | prec operator()( prec x ) | ||
| 104 | { | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | } Func; | ||
| 108 | |||
| 109 | typedef Hash<Bu::FString, Func> funcHash; | ||
| 110 | funcHash hFunc; | ||
| 111 | /* | ||
| 112 | typedef struct FuncSin : Func | ||
| 113 | { | ||
| 114 | prec operator()( prec x ) | ||
| 115 | { | ||
| 116 | return sin( x ); | ||
| 117 | } | ||
| 118 | } FuncSin; | ||
| 119 | */ | ||
| 120 | |||
| 121 | private: | ||
| 122 | enum | ||
| 123 | { | ||
| 124 | symEOS, | ||
| 125 | symAdd, | ||
| 126 | symSubtract, | ||
| 127 | symMultiply, | ||
| 128 | symDivide, | ||
| 129 | symOpenParen, | ||
| 130 | symCloseParen, | ||
| 131 | symNumber, | ||
| 132 | symVariable, | ||
| 133 | symExponent, | ||
| 134 | symModulus, | ||
| 135 | |||
| 136 | symAnd, | ||
| 137 | symOr, | ||
| 138 | symXor, | ||
| 139 | symNot | ||
| 140 | }; | ||
| 141 | |||
| 142 | typedef uint8_t symType; | ||
| 143 | |||
| 144 | Bu::Stack<symType> sOper; | ||
| 145 | Bu::Stack<prec> sValue; | ||
| 146 | |||
| 147 | private: | ||
| 148 | symType getPrec( symType nOper ) | ||
| 149 | { | ||
| 150 | switch( nOper ) | ||
| 151 | { | ||
| 152 | case symNumber: | ||
| 153 | case symVariable: | ||
| 154 | case symOpenParen: | ||
| 155 | case symCloseParen: | ||
| 156 | return 0; | ||
| 157 | |||
| 158 | case symAdd: | ||
| 159 | case symSubtract: | ||
| 160 | return 1; | ||
| 161 | |||
| 162 | case symMultiply: | ||
| 163 | case symDivide: | ||
| 164 | case symModulus: | ||
| 165 | return 2; | ||
| 166 | |||
| 167 | case symAnd: | ||
| 168 | case symOr: | ||
| 169 | case symXor: | ||
| 170 | return 2; | ||
| 171 | |||
| 172 | case symExponent: | ||
| 173 | case symNot: | ||
| 174 | return 3; | ||
| 175 | |||
| 176 | default: | ||
| 177 | return 0; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | symType nextToken( const char **sBuf ) | ||
| 182 | { | ||
| 183 | for(;;) | ||
| 184 | { | ||
| 185 | char cbuf = **sBuf; | ||
| 186 | ++(*sBuf); | ||
| 187 | switch( cbuf ) | ||
| 188 | { | ||
| 189 | case '+': | ||
| 190 | return symAdd; | ||
| 191 | |||
| 192 | case '-': | ||
| 193 | return symSubtract; | ||
| 194 | |||
| 195 | case '*': | ||
| 196 | return symMultiply; | ||
| 197 | |||
| 198 | case '/': | ||
| 199 | return symDivide; | ||
| 200 | |||
| 201 | case '^': | ||
| 202 | return symExponent; | ||
| 203 | |||
| 204 | case '%': | ||
| 205 | return symModulus; | ||
| 206 | |||
| 207 | case '(': | ||
| 208 | return symOpenParen; | ||
| 209 | |||
| 210 | case ')': | ||
| 211 | return symCloseParen; | ||
| 212 | |||
| 213 | case '|': | ||
| 214 | return symOr; | ||
| 215 | |||
| 216 | case '&': | ||
| 217 | return symAnd; | ||
| 218 | |||
| 219 | case '#': | ||
| 220 | return symXor; | ||
| 221 | |||
| 222 | case '~': | ||
| 223 | return symNot; | ||
| 224 | |||
| 225 | case ' ': | ||
| 226 | case '\t': | ||
| 227 | case '\n': | ||
| 228 | case '\r': | ||
| 229 | break; | ||
| 230 | |||
| 231 | case '\0': | ||
| 232 | return symEOS; | ||
| 233 | |||
| 234 | default: | ||
| 235 | if( cbuf == '.' || (cbuf >= '0' && cbuf <= '9') ) | ||
| 236 | { | ||
| 237 | char num[50]={cbuf}; | ||
| 238 | int nPos = 1; | ||
| 239 | bool bDot = false; | ||
| 240 | |||
| 241 | for(;;) | ||
| 242 | { | ||
| 243 | cbuf = **sBuf; | ||
| 244 | if( cbuf == '.' ) | ||
| 245 | { | ||
| 246 | if( bDot == false ) | ||
| 247 | bDot = true; | ||
| 248 | else | ||
| 249 | throw ParseException( | ||
| 250 | "Numbers cannot have more than one " | ||
| 251 | ". in them." | ||
| 252 | ); | ||
| 253 | } | ||
| 254 | if( cbuf == '.' || (cbuf >= '0' && cbuf <= '9') ) | ||
| 255 | { | ||
| 256 | num[nPos++] = cbuf; | ||
| 257 | } | ||
| 258 | else | ||
| 259 | { | ||
| 260 | num[nPos] = '\0'; | ||
| 261 | sValue.push( | ||
| 262 | static_cast<prec>( | ||
| 263 | strtod( num, NULL ) | ||
| 264 | ) | ||
| 265 | ); | ||
| 266 | return symNumber; | ||
| 267 | } | ||
| 268 | ++(*sBuf); | ||
| 269 | } | ||
| 270 | } | ||
| 271 | else if( (cbuf >= 'a' && cbuf <= 'z') || | ||
| 272 | (cbuf >= 'A' && cbuf <= 'Z') || | ||
| 273 | (cbuf == '_') ) | ||
| 274 | { | ||
| 275 | char tok[50]={cbuf}; | ||
| 276 | int nPos = 1; | ||
| 277 | |||
| 278 | for(;;) | ||
| 279 | { | ||
| 280 | cbuf = **sBuf; | ||
| 281 | if( (cbuf >= 'a' && cbuf <= 'z') || | ||
| 282 | (cbuf >= 'A' && cbuf <= 'Z') || | ||
| 283 | (cbuf >= '0' && cbuf <= '9') || | ||
| 284 | cbuf == '_' || cbuf == '.' || cbuf == ':' ) | ||
| 285 | { | ||
| 286 | tok[nPos++] = cbuf; | ||
| 287 | } | ||
| 288 | else | ||
| 289 | { | ||
| 290 | tok[nPos] = '\0'; | ||
| 291 | //printf("Checking variable \"%s\"\n", tok ); | ||
| 292 | try | ||
| 293 | { | ||
| 294 | sValue.push( hVars[tok] ); | ||
| 295 | return symNumber; | ||
| 296 | } | ||
| 297 | catch( HashException &e ) | ||
| 298 | { | ||
| 299 | throw ParseException( | ||
| 300 | "No variable named \"%s\" exists.", | ||
| 301 | tok | ||
| 302 | ); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | ++(*sBuf); | ||
| 306 | } | ||
| 307 | } | ||
| 308 | break; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | void reduce( bool bCloseParen = false ) | ||
| 314 | { | ||
| 315 | while( !sOper.isEmpty() ) | ||
| 316 | { | ||
| 317 | uint8_t nOpr = sOper.top(); | ||
| 318 | if( nOpr == symOpenParen ) | ||
| 319 | { | ||
| 320 | //printf("Found ( stopping reduction.\n"); | ||
| 321 | if( bCloseParen == true ) | ||
| 322 | sOper.pop(); | ||
| 323 | return; | ||
| 324 | } | ||
| 325 | sOper.pop(); | ||
| 326 | |||
| 327 | prec dTop = sValue.top(); | ||
| 328 | sValue.pop(); | ||
| 329 | |||
| 330 | switch( nOpr ) | ||
| 331 | { | ||
| 332 | case symAdd: | ||
| 333 | //printf("%f + %f = %f\n", sValue.top(), dTop, sValue.top()+dTop ); | ||
| 334 | sValue.top() += dTop; | ||
| 335 | break; | ||
| 336 | |||
| 337 | case symSubtract: | ||
| 338 | //printf("%f - %f = %f\n", sValue.top(), dTop, sValue.top()-dTop ); | ||
| 339 | sValue.top() -= dTop; | ||
| 340 | break; | ||
| 341 | |||
| 342 | case symMultiply: | ||
| 343 | //printf("%f * %f = %f\n", sValue.top(), dTop, sValue.top()*dTop ); | ||
| 344 | sValue.top() *= dTop; | ||
| 345 | break; | ||
| 346 | |||
| 347 | case symDivide: | ||
| 348 | //printf("%f / %f = %f\n", sValue.top(), dTop, sValue.top()/dTop ); | ||
| 349 | sValue.top() /= dTop; | ||
| 350 | break; | ||
| 351 | |||
| 352 | case symExponent: | ||
| 353 | //printf("%f ^ %f = %f\n", sValue.top(), dTop, pow(sValue.top(),dTop) ); | ||
| 354 | sValue.top() = static_cast<prec>( | ||
| 355 | pow( sValue.top(), dTop ) | ||
| 356 | ); | ||
| 357 | break; | ||
| 358 | |||
| 359 | case symModulus: | ||
| 360 | //printf("%f %% %f = %f\n", sValue.top(), dTop, fmod(sValue.top(),dTop) ); | ||
| 361 | sValue.top() = static_cast<prec>( | ||
| 362 | fmod( sValue.top(), dTop ) | ||
| 363 | ); | ||
| 364 | break; | ||
| 365 | |||
| 366 | case symOr: | ||
| 367 | sValue.top() = static_cast<prec>( | ||
| 368 | static_cast<bin>(sValue.top()) | | ||
| 369 | static_cast<bin>(dTop) | ||
| 370 | ); | ||
| 371 | break; | ||
| 372 | |||
| 373 | case symAnd: | ||
| 374 | sValue.top() = static_cast<prec>( | ||
| 375 | static_cast<bin>(sValue.top()) & | ||
| 376 | static_cast<bin>(dTop) | ||
| 377 | ); | ||
| 378 | break; | ||
| 379 | |||
| 380 | case symXor: | ||
| 381 | sValue.top() = static_cast<prec>( | ||
| 382 | static_cast<bin>(sValue.top()) ^ | ||
| 383 | static_cast<bin>(dTop) | ||
| 384 | ); | ||
| 385 | break; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | |||
| 389 | if( bCloseParen == true ) | ||
| 390 | { | ||
| 391 | throw ParseException( | ||
| 392 | "Close-paren found without matching open-paren." | ||
| 393 | ); | ||
| 394 | } | ||
| 395 | } | ||
| 396 | }; | ||
| 397 | } | ||
| 398 | |||
| 399 | #endif | ||
diff --git a/src/logger.cpp b/src/logger.cpp index 8c058d1..fe6a916 100644 --- a/src/logger.cpp +++ b/src/logger.cpp | |||
| @@ -12,7 +12,7 @@ Bu::Logger::~Logger() | |||
| 12 | { | 12 | { |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | void Bu::Logger::log( int nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...) | 15 | void Bu::Logger::log( uint32_t nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...) |
| 16 | { | 16 | { |
| 17 | if( (nLevel&nLevelMask) == 0 ) | 17 | if( (nLevel&nLevelMask) == 0 ) |
| 18 | return; | 18 | return; |
| @@ -112,12 +112,17 @@ void Bu::Logger::setFormat( const Bu::FString &str ) | |||
| 112 | //write( fileno(stdout), sLogFormat.getStr(), sLogFormat.getSize() ); | 112 | //write( fileno(stdout), sLogFormat.getStr(), sLogFormat.getSize() ); |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | void Bu::Logger::setMask( int n ) | 115 | void Bu::Logger::setMask( uint32_t n ) |
| 116 | { | 116 | { |
| 117 | nLevelMask = n; | 117 | nLevelMask = n; |
| 118 | } | 118 | } |
| 119 | 119 | ||
| 120 | void Bu::Logger::setLevel( int n ) | 120 | uint32_t Bu::Logger::getMask() |
| 121 | { | ||
| 122 | return nLevelMask; | ||
| 123 | } | ||
| 124 | |||
| 125 | void Bu::Logger::setLevel( uint32_t n ) | ||
| 121 | { | 126 | { |
| 122 | int j; | 127 | int j; |
| 123 | for( j = 31; j > 0; j-- ) | 128 | for( j = 31; j > 0; j-- ) |
| @@ -134,8 +139,8 @@ void Bu::Logger::setLevel( int n ) | |||
| 134 | } | 139 | } |
| 135 | } | 140 | } |
| 136 | 141 | ||
| 137 | void Bu::Logger::hexDump( int nLevel, const char *sFile, const char *sFunction, | 142 | void Bu::Logger::hexDump( uint32_t nLevel, const char *sFile, |
| 138 | int nLine, const void *pDataV, long nDataLen, | 143 | const char *sFunction, int nLine, const void *pDataV, long nDataLen, |
| 139 | const char *lpName ) | 144 | const char *lpName ) |
| 140 | { | 145 | { |
| 141 | log( nLevel, sFile, sFunction, nLine, "Displaying %ld bytes of %s.", nDataLen, lpName ); | 146 | log( nLevel, sFile, sFunction, nLine, "Displaying %ld bytes of %s.", nDataLen, lpName ); |
| @@ -176,3 +181,4 @@ void Bu::Logger::hexDump( int nLevel, const char *sFile, const char *sFunction, | |||
| 176 | } | 181 | } |
| 177 | log( nLevel, sFile, sFunction, nLine, sBorder.getStr() ); | 182 | log( nLevel, sFile, sFunction, nLine, sBorder.getStr() ); |
| 178 | } | 183 | } |
| 184 | |||
diff --git a/src/logger.h b/src/logger.h index f561e88..0e7d812 100644 --- a/src/logger.h +++ b/src/logger.h | |||
| @@ -69,17 +69,18 @@ namespace Bu | |||
| 69 | virtual ~Logger(); | 69 | virtual ~Logger(); |
| 70 | 70 | ||
| 71 | public: | 71 | public: |
| 72 | void log( int nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...); | 72 | void log( uint32_t nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...); |
| 73 | 73 | ||
| 74 | void setFormat( const Bu::FString &str ); | 74 | void setFormat( const Bu::FString &str ); |
| 75 | void setMask( int n ); | 75 | void setMask( uint32_t n ); |
| 76 | void setLevel( int n ); | 76 | void setLevel( uint32_t n ); |
| 77 | uint32_t getMask(); | ||
| 77 | 78 | ||
| 78 | void hexDump( int nLevel, const char *sFile, const char *sFunction, int nLine, const void *pData, long nDataLen, const char *lpName ); | 79 | void hexDump( uint32_t nLevel, const char *sFile, const char *sFunction, int nLine, const void *pData, long nDataLen, const char *lpName ); |
| 79 | 80 | ||
| 80 | private: | 81 | private: |
| 81 | Bu::FString sLogFormat; | 82 | Bu::FString sLogFormat; |
| 82 | int nLevelMask; | 83 | uint32_t nLevelMask; |
| 83 | }; | 84 | }; |
| 84 | } | 85 | } |
| 85 | 86 | ||
diff --git a/src/stack.cpp b/src/stack.cpp new file mode 100644 index 0000000..7380615 --- /dev/null +++ b/src/stack.cpp | |||
| @@ -0,0 +1 @@ | |||
| #include "stack.h" | |||
diff --git a/src/stack.h b/src/stack.h new file mode 100644 index 0000000..955332d --- /dev/null +++ b/src/stack.h | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | #ifndef BU_STACK_H | ||
| 2 | #define BU_STACK_H | ||
| 3 | |||
| 4 | #include <memory> | ||
| 5 | |||
| 6 | namespace Bu | ||
| 7 | { | ||
| 8 | template<typename value, typename valuealloc=std::allocator<value> > | ||
| 9 | class Stack | ||
| 10 | { | ||
| 11 | private: | ||
| 12 | typedef struct Chunk | ||
| 13 | { | ||
| 14 | value *pValue; | ||
| 15 | Chunk *pPrev; | ||
| 16 | } Chunk; | ||
| 17 | public: | ||
| 18 | Stack() : | ||
| 19 | pTop( NULL ) | ||
| 20 | { | ||
| 21 | } | ||
| 22 | |||
| 23 | virtual ~Stack() | ||
| 24 | { | ||
| 25 | } | ||
| 26 | |||
| 27 | void push( const value &v ) | ||
| 28 | { | ||
| 29 | Chunk *pChnk = new Chunk; | ||
| 30 | pChnk->pValue = va.allocate( 1 ); | ||
| 31 | va.construct( pChnk->pValue, v ); | ||
| 32 | pChnk->pPrev = pTop; | ||
| 33 | pTop = pChnk; | ||
| 34 | } | ||
| 35 | |||
| 36 | value &peek() | ||
| 37 | { | ||
| 38 | return *pTop->pValue; | ||
| 39 | } | ||
| 40 | |||
| 41 | value &top() | ||
| 42 | { | ||
| 43 | return *pTop->pValue; | ||
| 44 | } | ||
| 45 | |||
| 46 | value pop() | ||
| 47 | { | ||
| 48 | value ret( *pTop->pValue ); | ||
| 49 | |||
| 50 | Chunk *pChnk = pTop; | ||
| 51 | pTop = pTop->pPrev; | ||
| 52 | |||
| 53 | va.destroy( pChnk->pValue ); | ||
| 54 | va.deallocate( pChnk->pValue, 1 ); | ||
| 55 | delete pChnk; | ||
| 56 | |||
| 57 | return ret; | ||
| 58 | } | ||
| 59 | |||
| 60 | bool isEmpty() | ||
| 61 | { | ||
| 62 | return pTop == NULL; | ||
| 63 | } | ||
| 64 | |||
| 65 | private: | ||
| 66 | Chunk *pTop; | ||
| 67 | valuealloc va; | ||
| 68 | }; | ||
| 69 | } | ||
| 70 | |||
| 71 | #endif | ||
diff --git a/src/tests/formula.cpp b/src/tests/formula.cpp new file mode 100644 index 0000000..5237b87 --- /dev/null +++ b/src/tests/formula.cpp | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | #include "bu/formula.h" | ||
| 2 | |||
| 3 | int main( int argc, char *argv[] ) | ||
| 4 | { | ||
| 5 | Bu::Formula<uint32_t> uForm; | ||
| 6 | Bu::Formula<double> dForm; | ||
| 7 | |||
| 8 | printf("u: %s = %u\n", argv[1], uForm.run( argv[1] ) ); | ||
| 9 | printf("d: %s = %f\n", argv[1], dForm.run( argv[1] ) ); | ||
| 10 | } | ||
| 11 | |||
