diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-02-19 05:15:23 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-02-19 05:15:23 +0000 |
commit | c208b397b798910df4ad129fb13d562b4450034e (patch) | |
tree | dbc04e831163022e09c7a001b887ba3d7f82f7bd /src/formula.h | |
parent | 89eeeff54f0b3ce30be5b046fc3899fdeb5ebb40 (diff) | |
download | libbu++-c208b397b798910df4ad129fb13d562b4450034e.tar.gz libbu++-c208b397b798910df4ad129fb13d562b4450034e.tar.bz2 libbu++-c208b397b798910df4ad129fb13d562b4450034e.tar.xz libbu++-c208b397b798910df4ad129fb13d562b4450034e.zip |
The formula system works just fine, just no functions yet, but I don't need them
for quite a while.
Diffstat (limited to '')
-rw-r--r-- | src/formula.h | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/src/formula.h b/src/formula.h index 1eccd36..939eb09 100644 --- a/src/formula.h +++ b/src/formula.h | |||
@@ -3,15 +3,19 @@ | |||
3 | 3 | ||
4 | #include <stdint.h> | 4 | #include <stdint.h> |
5 | 5 | ||
6 | #include <math.h> | ||
6 | #include <stack> | 7 | #include <stack> |
7 | #include "sbuffer.h" | 8 | #include "sbuffer.h" |
8 | 9 | ||
9 | #include "exceptionbase.h" | 10 | #include "exceptionbase.h" |
11 | #include "hash.h" | ||
10 | 12 | ||
11 | subExceptionDecl( ParseException ); | 13 | subExceptionDecl( ParseException ); |
12 | 14 | ||
13 | /** | 15 | /** |
14 | * | 16 | * Implements a very simple formula parser that allows use of variables and |
17 | * custom functions. This is based on a simple calculator-type parser that | ||
18 | * executes as it processes, accounting for operator precedence and grouping. | ||
15 | */ | 19 | */ |
16 | class Formula | 20 | class Formula |
17 | { | 21 | { |
@@ -19,7 +23,29 @@ public: | |||
19 | Formula(); | 23 | Formula(); |
20 | virtual ~Formula(); | 24 | virtual ~Formula(); |
21 | 25 | ||
22 | double run( const char *sFormula ); | 26 | double run( char *sFormula ); |
27 | |||
28 | typedef Hash<std::string, double> varHash; | ||
29 | varHash hVars; | ||
30 | |||
31 | typedef struct Func | ||
32 | { | ||
33 | double operator()( double x ) | ||
34 | { | ||
35 | return 0.0; | ||
36 | } | ||
37 | } Func; | ||
38 | |||
39 | typedef Hash<std::string, Func> funcHash; | ||
40 | funcHash hFunc; | ||
41 | |||
42 | typedef struct FuncSin : Func | ||
43 | { | ||
44 | double operator()( double x ) | ||
45 | { | ||
46 | return sin( x ); | ||
47 | } | ||
48 | } FuncSin; | ||
23 | 49 | ||
24 | private: | 50 | private: |
25 | enum | 51 | enum |
@@ -32,31 +58,20 @@ private: | |||
32 | symOpenParen, | 58 | symOpenParen, |
33 | symCloseParen, | 59 | symCloseParen, |
34 | symNumber, | 60 | symNumber, |
35 | symVariable | 61 | symVariable, |
62 | symExponent, | ||
63 | symModulus | ||
36 | }; | 64 | }; |
37 | 65 | ||
38 | typedef struct Token | 66 | typedef uint8_t symType; |
39 | { | ||
40 | Token() {} | ||
41 | Token( uint8_t nSym ) : nSym( nSym ) { } | ||
42 | Token( uint8_t nSym, double dNum ) : nSym( nSym ) { val.num = dNum; } | ||
43 | uint8_t nSym; | ||
44 | union | ||
45 | { | ||
46 | double num; | ||
47 | } val; | ||
48 | } Token; | ||
49 | 67 | ||
50 | std::stack<Token> sToken; | 68 | std::stack<symType> sOper; |
51 | Token tLook; | 69 | std::stack<double> sValue; |
52 | int nState; | ||
53 | SBuffer sBuf; | ||
54 | 70 | ||
55 | private: | 71 | private: |
56 | void push(); | 72 | symType getPrec( symType nOper ); |
57 | void state(); | 73 | symType nextToken( char **sBuf ); |
58 | Token nextToken(); | 74 | void reduce( bool bCloseParen = false ); |
59 | void printToken( Token &tok ); | ||
60 | }; | 75 | }; |
61 | 76 | ||
62 | #endif | 77 | #endif |