aboutsummaryrefslogtreecommitdiff
path: root/src/formula.h
blob: 939eb092f057134269c30ca774970a44ff8e49aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef FORMULA_H
#define FORMULA_H

#include <stdint.h>

#include <math.h>
#include <stack>
#include "sbuffer.h"

#include "exceptionbase.h"
#include "hash.h"

subExceptionDecl( ParseException );

/**
 * Implements a very simple formula parser that allows use of variables and
 * custom functions.  This is based on a simple calculator-type parser that
 * executes as it processes, accounting for operator precedence and grouping.
 */
class Formula
{
public:
	Formula();
	virtual ~Formula();

	double run( char *sFormula );

	typedef Hash<std::string, double> varHash;
	varHash hVars;

	typedef struct Func
	{
		double operator()( double x )
		{
			return 0.0;
		}
	} Func;

	typedef Hash<std::string, Func> funcHash;
	funcHash hFunc;

	typedef struct FuncSin : Func
	{
		double operator()( double x )
		{
			return sin( x );
		}
	} FuncSin;

private:
	enum
	{
		symEOS,
		symAdd,
		symSubtract,
		symMultiply,
		symDivide,
		symOpenParen,
		symCloseParen,
		symNumber,
		symVariable,
		symExponent,
		symModulus
	};

	typedef uint8_t symType;

	std::stack<symType> sOper;
	std::stack<double> sValue;

private:
	symType getPrec( symType nOper );
	symType nextToken( char **sBuf );
	void reduce( bool bCloseParen = false );
};

#endif