summaryrefslogtreecommitdiff
path: root/src/formula.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/formula.h')
-rw-r--r--src/formula.h77
1 files changed, 0 insertions, 77 deletions
diff --git a/src/formula.h b/src/formula.h
deleted file mode 100644
index 939eb09..0000000
--- a/src/formula.h
+++ /dev/null
@@ -1,77 +0,0 @@
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