aboutsummaryrefslogtreecommitdiff
path: root/src/old/formula.h
diff options
context:
space:
mode:
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