Blame view

src/expressionParser/ExpressionContainer.hh 6.66 KB
a7f2648e   Benjamin Renard   Parser: First imp...
1
2
3
#ifndef EXPRESSIONCONTAINER_HH_
#define EXPRESSIONCONTAINER_HH_

04d84599   Benjamin Renard   Parser: Add suppo...
4
5
6
7
#include <cstdio>
#include <vector>
#include <boost/lexical_cast.hpp>

a7f2648e   Benjamin Renard   Parser: First imp...
8
9
10
namespace AMDA {
namespace parser {

36749bea   Benjamin Renard   Use a common cont...
11
12
13
14
15
16
struct Operator {
	explicit Operator(std::string ihmSymbol, std::string kernelSymbol) : ihmSymbol(ihmSymbol), kernelSymbol(kernelSymbol) {};

	std::string ihmSymbol = "";
	std::string kernelSymbol = "";
};
a7f2648e   Benjamin Renard   Parser: First imp...
17
18
19
20

namespace Expression{

typedef std::string var;
ef417f05   Benjamin Renard   Improve function ...
21

36749bea   Benjamin Renard   Use a common cont...
22
23
struct BinaryOperation;
struct UnaryOperation;
ef417f05   Benjamin Renard   Improve function ...
24
struct FunctionOperation;
02869c99   Benjamin Renard   Improve parse of ...
25
26
struct PowerOperation;
struct ParameterDef;
a7f2648e   Benjamin Renard   Parser: First imp...
27

ef417f05   Benjamin Renard   Improve function ...
28
29
typedef boost::variant<
	var,
36749bea   Benjamin Renard   Use a common cont...
30
31
	boost::recursive_wrapper<UnaryOperation>,
	boost::recursive_wrapper<BinaryOperation>,
02869c99   Benjamin Renard   Improve parse of ...
32
	boost::recursive_wrapper<PowerOperation>,
ef417f05   Benjamin Renard   Improve function ...
33
	boost::recursive_wrapper<FunctionOperation>,
02869c99   Benjamin Renard   Improve parse of ...
34
	boost::recursive_wrapper<ParameterDef>
47755d13   Benjamin Renard   Minor changes
35
> ExpressionContainer;
a7f2648e   Benjamin Renard   Parser: First imp...
36

ef417f05   Benjamin Renard   Improve function ...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
struct FunctionOperation
{
	explicit FunctionOperation(std::string func, const ExpressionContainer& arg1) : func(func) {
		args.push_back(arg1);
	}

	explicit FunctionOperation(std::string func, const ExpressionContainer& arg1, const ExpressionContainer& arg2) : func(func) {
		args.push_back(arg1);
		args.push_back(arg2);
	}

	explicit FunctionOperation(std::string func, const ExpressionContainer& arg1, const ExpressionContainer& arg2, const ExpressionContainer& arg3) : func(func) {
		args.push_back(arg1);
		args.push_back(arg2);
		args.push_back(arg3);
	}

	std::string func;
	std::vector<ExpressionContainer> args;
};

02869c99   Benjamin Renard   Improve parse of ...
58
59
struct ParameterDef
{
04d84599   Benjamin Renard   Parser: Add suppo...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	enum ComponentType {
		CT_INDEX,
		CT_ALL,
		CT_RANGE_VALUES,
		CT_RANGE_INDEXES
	};

	struct Component {
		ComponentType type = ComponentType::CT_ALL;
		int    index = 0;
		float min   = 0.;
		float max   = 0.;
	};

02869c99   Benjamin Renard   Improve parse of ...
74
	explicit ParameterDef(std::string param, const std::string& comp1, const std::string& comp2) : param(param) {
04d84599   Benjamin Renard   Parser: Add suppo...
75
76
		components.push_back(parseComponent(comp1));
		components.push_back(parseComponent(comp2));
02869c99   Benjamin Renard   Improve parse of ...
77
78
79
	}

	explicit ParameterDef(std::string param, const std::string& comp1) : param(param) {
04d84599   Benjamin Renard   Parser: Add suppo...
80
		components.push_back(parseComponent(comp1));
02869c99   Benjamin Renard   Improve parse of ...
81
82
83
84
	}

	explicit ParameterDef(std::string param) : param(param) {}

04d84599   Benjamin Renard   Parser: Add suppo...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	Component parseComponent(const std::string& comp) {
		Component res;
		if (comp.compare("*") == 0) {
			res.type = ComponentType::CT_ALL;
		}
		else if (comp.find("range[") == 0) {
			if (std::sscanf((char*)comp.c_str(), "range[%f,%f]", &res.min, &res.max) == 2) {
				res.type = ComponentType::CT_RANGE_VALUES;
			}
			else {
				res.type = ComponentType::CT_ALL;
			}
		}
		else if (comp.find("indexes[") == 0) {
			int min = 0, max = 0;
			if (std::sscanf((char*)comp.c_str(), "indexes[%d,%d]", &min, &max) == 2) {
				res.type = ComponentType::CT_RANGE_INDEXES;
				res.min = min;
				res.max = max;
			}
			else {
				res.type = ComponentType::CT_ALL;
			}
		}
		else {
			try {
				res.index = boost::lexical_cast<int>(comp.c_str());
				res.type = ComponentType::CT_INDEX;
			} catch( boost::bad_lexical_cast const& ) {
				res.type = ComponentType::CT_ALL;
			}
		}

		return res;
	}

02869c99   Benjamin Renard   Improve parse of ...
121
	std::string param;
04d84599   Benjamin Renard   Parser: Add suppo...
122
	std::vector<Component> components;
02869c99   Benjamin Renard   Improve parse of ...
123
124
};

36749bea   Benjamin Renard   Use a common cont...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
struct UnaryOperation
{
	explicit UnaryOperation(Operator op, const ExpressionContainer& e) : op(op), e(e) {}
	Operator op;
	ExpressionContainer e;
};

struct BinaryOperation
{
	explicit BinaryOperation(const ExpressionContainer& l, Operator op, const ExpressionContainer& r) : l(l), op(op), r(r) { }
	ExpressionContainer l;
	Operator op;
	ExpressionContainer r;
};

02869c99   Benjamin Renard   Improve parse of ...
140
141
142
143
144
145
146
struct PowerOperation
{
	explicit PowerOperation(const ExpressionContainer& l, const ExpressionContainer& r) : l(l), r(r) {}
	ExpressionContainer l;
	ExpressionContainer r;
};

36749bea   Benjamin Renard   Use a common cont...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
struct NotOperation : UnaryOperation
{
	explicit NotOperation(const ExpressionContainer& e) : UnaryOperation(Operator("!","!"), e) {}
};

struct MinusSignOperation : UnaryOperation
{
	explicit MinusSignOperation(const ExpressionContainer& e) : UnaryOperation(Operator("-","-"), e) {}
};

struct PlusSignOperation : UnaryOperation
{
	explicit PlusSignOperation(const ExpressionContainer& e) : UnaryOperation(Operator("+","+"), e) {}
};

struct ConstantOperation : UnaryOperation
{
	explicit ConstantOperation(const ExpressionContainer& e) : UnaryOperation(Operator("@",""), e) {}
};

struct PowerTenOperation : BinaryOperation
{
c0bbbeb7   Benjamin Renard   Fix some bug with...
169
	explicit PowerTenOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("e","e"), r) { }
36749bea   Benjamin Renard   Use a common cont...
170
171
};

82a9bc9e   Benjamin Renard   Support d operator
172
173
174
175
176
struct PowerTenDOperation : BinaryOperation
{
	explicit PowerTenDOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("d","e"), r) { }
};

36749bea   Benjamin Renard   Use a common cont...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
struct SumOperation : BinaryOperation
{
	explicit SumOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("+","+"), r) { }
};

struct DifferenceOperation : BinaryOperation
{
	explicit DifferenceOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("-","-"), r) { }
};

struct FactorOperation : BinaryOperation
{
	explicit FactorOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("*","*"), r) { }
};

struct DivisionOperation : BinaryOperation
{
	explicit DivisionOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("/","/"), r) { }
};

struct EqualOperation : BinaryOperation
{
	explicit EqualOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("=","=="), r) { }
};

struct UnequalOperation : BinaryOperation
{
	explicit UnequalOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("!=","!="), r) { }
};

struct GreaterOperation : BinaryOperation
{
	explicit GreaterOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator(">",">"), r) { }
};

struct GreaterOrEqualOperation : BinaryOperation
{
	explicit GreaterOrEqualOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator(">=",">="), r) { }
};

struct LowerOperation : BinaryOperation
{
	explicit LowerOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("<","<"), r) { }
};

struct LowerOrEqualOperation : BinaryOperation
{
	explicit LowerOrEqualOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("<=","<="), r) { }
};

struct AndOperation : BinaryOperation
{
	explicit AndOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("&","&&"), r) { }
};

struct OrOperation : BinaryOperation
{
	explicit OrOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("|","||"), r) { }
};
a7f2648e   Benjamin Renard   Parser: First imp...
236

a7f2648e   Benjamin Renard   Parser: First imp...
237
238
239
240
241
242
} /* namespace Expression */

} /* namespace parser */
} /* namespace AMDA */

#endif