Blame view

src/expressionParser/ExpressionPrinter.hh 7.12 KB
a7f2648e   Benjamin Renard   Parser: First imp...
1
2
3
4
5
#ifndef EXPRESSIONPRINTER_HH_
#define EXPRESSIONPRINTER_HH_

#include "ExpressionContainer.hh"

04d84599   Benjamin Renard   Parser: Add suppo...
6
7
#include <sstream>

c1f4db8e   Benjamin Renard   Add process to te...
8
9
//#define ADD_DEBUG_TAG

a7f2648e   Benjamin Renard   Parser: First imp...
10
11
12
13
namespace AMDA {
namespace parser {
namespace Expression {

47755d13   Benjamin Renard   Minor changes
14
class ExpressionPrinter : public boost::static_visitor<void>
a7f2648e   Benjamin Renard   Parser: First imp...
15
{
47755d13   Benjamin Renard   Minor changes
16
public:
a2df6c28   Benjamin Renard   Parser: Retrieve ...
17
18
19
20
	ExpressionPrinter(std::ostream& os) : _os(os), _pParamsList(NULL) {}

	ExpressionPrinter(std::ostream& os, std::vector<std::string>* pParamsList) : _os(os), _pParamsList(pParamsList) {}

c1f4db8e   Benjamin Renard   Add process to te...
21
	std::ostream& _os;
a7f2648e   Benjamin Renard   Parser: First imp...
22

a2df6c28   Benjamin Renard   Parser: Retrieve ...
23
24
	std::vector<std::string>* _pParamsList;

c1f4db8e   Benjamin Renard   Add process to te...
25
26
27
28
29
30
	void operator()(const var& v) const {
		#ifdef ADD_DEBUG_TAG
		_os << "@VAR@";
		#endif
		_os << v;
	}
a7f2648e   Benjamin Renard   Parser: First imp...
31

c1f4db8e   Benjamin Renard   Add process to te...
32
33
34
35
36
37
38
39
40
41
	void operator()(const BinaryOperation& b) const {
		#ifdef ADD_DEBUG_TAG
		_os << "@BINOP@";
		#endif
		_os << "(";
		boost::apply_visitor(*this, b.l);
		_os << b.op.kernelSymbol;
		boost::apply_visitor(*this, b.r);
		_os << ")";
	}
a7f2648e   Benjamin Renard   Parser: First imp...
42

c1f4db8e   Benjamin Renard   Add process to te...
43
44
45
46
47
48
	void operator()(const UnaryOperation& u) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@UNOP@";
		#endif
		_os << u.op.kernelSymbol;
		boost::apply_visitor(*this, u.e);
ef417f05   Benjamin Renard   Improve function ...
49
	}
c1f4db8e   Benjamin Renard   Add process to te...
50

02869c99   Benjamin Renard   Improve parse of ...
51
52
53
54
55
56
57
58
59
60
61
	void operator()(const PowerOperation& p) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@POWER@";
		#endif
		_os << "pow(";
		boost::apply_visitor(*this, p.l);
		_os << ",";
		boost::apply_visitor(*this, p.r);
		_os << ")";
	}

c1f4db8e   Benjamin Renard   Add process to te...
62
63
64
65
66
	void operator()(const FunctionOperation& f) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@FUNC@";
		#endif
		_os << f.func << "(";
e8489ddc   Benjamin Renard   Fix printer for a...
67
		bool isProcess = (!f.func.empty() && (f.func[0] == '#'));
c1f4db8e   Benjamin Renard   Add process to te...
68
69
		for (unsigned int i = 0; i < f.args.size(); ++i) {
			if (i != 0) {
e8489ddc   Benjamin Renard   Fix printer for a...
70
				_os << (isProcess ? ";" : ",");
c1f4db8e   Benjamin Renard   Add process to te...
71
72
73
74
			}
			boost::apply_visitor(*this, f.args[i]);
		}
		_os << ")";
02869c99   Benjamin Renard   Improve parse of ...
75
	}
e33239bf   Benjamin Renard   Parse parameters
76

02869c99   Benjamin Renard   Improve parse of ...
77
78
79
80
	void operator()(const ParameterDef& p) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@PARAM@";
		#endif
e8489ddc   Benjamin Renard   Fix printer for a...
81

a2df6c28   Benjamin Renard   Parser: Retrieve ...
82
		if (_pParamsList != NULL) {
aae52524   Benjamin Renard   Parser: Write res...
83
84
85
			if (std::find(_pParamsList->begin(), _pParamsList->end(), p.param) == _pParamsList->end()) {
				_pParamsList->push_back(p.param);
			}
a2df6c28   Benjamin Renard   Parser: Retrieve ...
86
87
		}

04d84599   Benjamin Renard   Parser: Add suppo...
88
89
90
91
92
93
94
		if (
			p.components.empty() ||
			((p.components.size() == 1) && (p.components[0].type == ParameterDef::ComponentType::CT_ALL)) ||
			((p.components.size() == 2) && (p.components[0].type == ParameterDef::ComponentType::CT_ALL) && (p.components[1].type == ParameterDef::ComponentType::CT_ALL)))
		{
			// All components
			_os << printParam(p.param);
e8489ddc   Benjamin Renard   Fix printer for a...
95
		}
04d84599   Benjamin Renard   Parser: Add suppo...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
		else if ((p.components.size() == 1) ||
			((p.components.size() == 2) && (p.components[1].type == ParameterDef::ComponentType::CT_ALL)))
		{
			//only first component defined
			switch (p.components[0].type) {
			case ParameterDef::ComponentType::CT_INDEX:
				_os << printParam(p.param);
				_os << printComponentIndex(p.components[0].index);
				break;
			case ParameterDef::CT_RANGE_VALUES:
			case ParameterDef::CT_RANGE_INDEXES:
				{
					std::stringstream ss;
					ss << printParam(p.param);
					_os << printSumIntoTable(p.components[0].type, ss.str(), 0, p.components[0].min, p.components[0].max);
					break;
e8489ddc   Benjamin Renard   Fix printer for a...
112
				}
04d84599   Benjamin Renard   Parser: Add suppo...
113
114
115
			default:
				//Unknown case => return parameter
				_os << printParam(p.param);
e8489ddc   Benjamin Renard   Fix printer for a...
116
			}
04d84599   Benjamin Renard   Parser: Add suppo...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
		}
		else if ((p.components.size() == 2) && (p.components[0].type == ParameterDef::ComponentType::CT_ALL))
		{
			//only second component defined
			switch (p.components[1].type) {
			case ParameterDef::ComponentType::CT_INDEX:
				{
				_os << printParamColumn(p.param, p.components[1].index);
				break;
				}
			case ParameterDef::CT_RANGE_VALUES:
			case ParameterDef::CT_RANGE_INDEXES:
				{
					std::stringstream ss;
					ss << printParam(p.param);
					_os << printSumIntoTable(p.components[1].type, ss.str(), 1, p.components[1].min, p.components[1].max);
				}
				break;
			default:
				//Unknown case => return parameter
				_os << printParam(p.param);
e8489ddc   Benjamin Renard   Fix printer for a...
138
			}
04d84599   Benjamin Renard   Parser: Add suppo...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
		}
		else {
			//two components defined
			switch (p.components[0].type) {
			case ParameterDef::ComponentType::CT_INDEX:
				{
					switch (p.components[1].type) {
					case ParameterDef::ComponentType::CT_INDEX:
						_os << printParam(p.param);
						_os << printComponentIndex(p.components[0].index);
						_os << printComponentIndex(p.components[1].index);
						break;
					case ParameterDef::CT_RANGE_VALUES:
					case ParameterDef::CT_RANGE_INDEXES:
						{
							std::stringstream ss;
							ss << printParam(p.param);
							ss << printComponentIndex(p.components[0].index);
							_os << printSumIntoTable(p.components[1].type, ss.str(), 0, p.components[1].min, p.components[1].max);
						}
						break;
					default:
						//Unknown case => return parameter
						_os << printParam(p.param);
					}
				}
				break;
			case ParameterDef::CT_RANGE_VALUES:
			case ParameterDef::CT_RANGE_INDEXES:
				{
					switch (p.components[1].type) {
					case ParameterDef::ComponentType::CT_INDEX:
						{
							std::stringstream ss;
							ss << printParamColumn(p.param,p.components[1].index);
							_os << printSumIntoTable(p.components[0].type, ss.str(), 0, p.components[0].min, p.components[0].max);
						}
						break;
					case ParameterDef::CT_RANGE_VALUES:
					case ParameterDef::CT_RANGE_INDEXES:
						if (p.components[0].type != p.components[1].type) {
							//Cannot mix range values and range indexes => return parameter
							_os << printParam(p.param);
						}
						else {
							std::stringstream ss;
							ss << printParam(p.param);
							_os << printSumIntoTable(p.components[0].type, ss.str(), 0, p.components[0].min, p.components[0].max, p.components[1].min, p.components[1].max);
						}
						break;
					default:
						//Unknown case => return parameter
						_os << printParam(p.param);
					}
				}
				break;
			default:
				//Unknown case => return parameter
				_os << printParam(p.param);
e8489ddc   Benjamin Renard   Fix printer for a...
198
199
			}
		}
04d84599   Benjamin Renard   Parser: Add suppo...
200
	}
e8489ddc   Benjamin Renard   Fix printer for a...
201

04d84599   Benjamin Renard   Parser: Add suppo...
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
private:
	std::string printParam(std::string name) const {
		std::stringstream ss;
		ss << "$";
		ss << name;
		return ss.str();
	}

	std::string printParamColumn(std::string name, int col) const {
		std::stringstream ss;
		ss << "(";
		ss << printParam(name);
		ss << ").getColumn(";
		ss << col;
		ss << ")";
		return ss.str();
	}

	std::string printComponentIndex(int index) const {
		std::stringstream ss;
		ss << "[";
		ss << index;
		ss << "]";
		return ss.str();
	}

	

	std::string printSumIntoTable(ParameterDef::ComponentType type, std::string expression, int relatedDim1, float min, float max, float min2 = NAN, float max2 = NAN) const {
		std::stringstream ss;
		bool forceInt = false;
		if (type == ParameterDef::ComponentType::CT_RANGE_INDEXES) {
			ss << "#sum_into_table_indexes";
			forceInt = true;
		}
		else {
			ss << "#sum_into_table_range";
		}
		ss << "(";
		ss << expression;
		ss << ";";
		ss << relatedDim1;
		ss << ";";
		ss << (forceInt ? (int)min : min);
		ss << ";";
		ss << (forceInt ? (int)max : max);
		if (!std::isnan(min2) && !std::isnan(max2)) {
			ss << ";";
			ss << (forceInt ? (int)min2 : min2);
			ss << ";";
			ss << (forceInt ? (int)max2 : max2);
		}
		ss << ")";
		return ss.str();
02869c99   Benjamin Renard   Improve parse of ...
256
	}
a7f2648e   Benjamin Renard   Parser: First imp...
257
258
};

47755d13   Benjamin Renard   Minor changes
259
std::ostream& operator<<(std::ostream& os, const ExpressionContainer& e)
a7f2648e   Benjamin Renard   Parser: First imp...
260
{
47755d13   Benjamin Renard   Minor changes
261
	boost::apply_visitor(ExpressionPrinter(os), e);
a7f2648e   Benjamin Renard   Parser: First imp...
262
263
	return os;
}
a2df6c28   Benjamin Renard   Parser: Retrieve ...
264
265
266
267
268
269

void getExpressionInfo(const ExpressionContainer& e, std::string& kernelExpression, std::vector<std::string>& paramsList) {
	std::stringstream ss;
        boost::apply_visitor(ExpressionPrinter(ss, &paramsList), e);
	kernelExpression = ss.str();
}
a7f2648e   Benjamin Renard   Parser: First imp...
270
271
272
273
274
275
276
    
} /* namespace Expression */
} /* namespace parser */
} /* namespace AMDA */


#endif