aboutsummaryrefslogtreecommitdiff
path: root/src/old/formula.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-04-03 03:49:53 +0000
committerMike Buland <eichlan@xagasoft.com>2007-04-03 03:49:53 +0000
commitf4c20290509d7ed3a8fd5304577e7a4cc0b9d974 (patch)
tree13cdf64f7cf134f397a7165b7a3fe0807e37026b /src/old/formula.h
parent74d4c8cd27334fc7204d5a8773deb3d424565778 (diff)
downloadlibbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.gz
libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.bz2
libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.xz
libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.zip
Ok, no code is left in src, it's all in src/old. We'll gradually move code back
into src as it's fixed and re-org'd. This includes tests, which, I may write a unit test system into libbu++ just to make my life easier.
Diffstat (limited to 'src/old/formula.h')
-rw-r--r--src/old/formula.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/old/formula.h b/src/old/formula.h
new file mode 100644
index 0000000..939eb09
--- /dev/null
+++ b/src/old/formula.h
@@ -0,0 +1,77 @@
1#ifndef FORMULA_H
2#define FORMULA_H
3
4#include <stdint.h>
5
6#include <math.h>
7#include <stack>
8#include "sbuffer.h"
9
10#include "exceptionbase.h"
11#include "hash.h"
12
13subExceptionDecl( ParseException );
14
15/**
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.
19 */
20class Formula
21{
22public:
23 Formula();
24 virtual ~Formula();
25
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;
49
50private:
51 enum
52 {
53 symEOS,
54 symAdd,
55 symSubtract,
56 symMultiply,
57 symDivide,
58 symOpenParen,
59 symCloseParen,
60 symNumber,
61 symVariable,
62 symExponent,
63 symModulus
64 };
65
66 typedef uint8_t symType;
67
68 std::stack<symType> sOper;
69 std::stack<double> sValue;
70
71private:
72 symType getPrec( symType nOper );
73 symType nextToken( char **sBuf );
74 void reduce( bool bCloseParen = false );
75};
76
77#endif