aboutsummaryrefslogtreecommitdiff
path: root/src/formula.h
blob: 1eccd367d3d87c0635bfed5369f6764f5bc43439 (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
#ifndef FORMULA_H
#define FORMULA_H

#include <stdint.h>

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

#include "exceptionbase.h"

subExceptionDecl( ParseException );

/**
 *
 */
class Formula
{
public:
	Formula();
	virtual ~Formula();

	double run( const char *sFormula );

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

	typedef struct Token
	{
		Token() {}
		Token( uint8_t nSym ) : nSym( nSym ) { }
		Token( uint8_t nSym, double dNum ) : nSym( nSym ) { val.num = dNum; }
		uint8_t nSym;
		union 
		{
			double num;
		} val;
	} Token;

	std::stack<Token> sToken;
	Token tLook;
	int nState;
	SBuffer sBuf;

private:
	void push();
	void state();
	Token nextToken();
	void printToken( Token &tok );
};

#endif