Blame view

src/expressionParser/ExpressionPrinter.hh 6.53 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:
c1f4db8e   Benjamin Renard   Add process to te...
17
18
	ExpressionPrinter(std::ostream& os) : _os(os) {}
	std::ostream& _os;
a7f2648e   Benjamin Renard   Parser: First imp...
19

c1f4db8e   Benjamin Renard   Add process to te...
20
21
22
23
24
25
	void operator()(const var& v) const {
		#ifdef ADD_DEBUG_TAG
		_os << "@VAR@";
		#endif
		_os << v;
	}
a7f2648e   Benjamin Renard   Parser: First imp...
26

c1f4db8e   Benjamin Renard   Add process to te...
27
28
29
30
31
32
33
34
35
36
	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...
37

c1f4db8e   Benjamin Renard   Add process to te...
38
39
40
41
42
43
	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 ...
44
	}
c1f4db8e   Benjamin Renard   Add process to te...
45

02869c99   Benjamin Renard   Improve parse of ...
46
47
48
49
50
51
52
53
54
55
56
	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...
57
58
59
60
61
	void operator()(const FunctionOperation& f) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@FUNC@";
		#endif
		_os << f.func << "(";
e8489ddc   Benjamin Renard   Fix printer for a...
62
		bool isProcess = (!f.func.empty() && (f.func[0] == '#'));
c1f4db8e   Benjamin Renard   Add process to te...
63
64
		for (unsigned int i = 0; i < f.args.size(); ++i) {
			if (i != 0) {
e8489ddc   Benjamin Renard   Fix printer for a...
65
				_os << (isProcess ? ";" : ",");
c1f4db8e   Benjamin Renard   Add process to te...
66
67
68
69
			}
			boost::apply_visitor(*this, f.args[i]);
		}
		_os << ")";
02869c99   Benjamin Renard   Improve parse of ...
70
	}
e33239bf   Benjamin Renard   Parse parameters
71

02869c99   Benjamin Renard   Improve parse of ...
72
73
74
75
	void operator()(const ParameterDef& p) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@PARAM@";
		#endif
e8489ddc   Benjamin Renard   Fix printer for a...
76

04d84599   Benjamin Renard   Parser: Add suppo...
77
78
79
80
81
82
83
		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...
84
		}
04d84599   Benjamin Renard   Parser: Add suppo...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
		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...
101
				}
04d84599   Benjamin Renard   Parser: Add suppo...
102
103
104
			default:
				//Unknown case => return parameter
				_os << printParam(p.param);
e8489ddc   Benjamin Renard   Fix printer for a...
105
			}
04d84599   Benjamin Renard   Parser: Add suppo...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
		}
		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...
127
			}
04d84599   Benjamin Renard   Parser: Add suppo...
128
129
130
131
132
133
134
135
136
137
138
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
		}
		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...
187
188
			}
		}
04d84599   Benjamin Renard   Parser: Add suppo...
189
	}
e8489ddc   Benjamin Renard   Fix printer for a...
190

04d84599   Benjamin Renard   Parser: Add suppo...
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
236
237
238
239
240
241
242
243
244
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 ...
245
	}
a7f2648e   Benjamin Renard   Parser: First imp...
246
247
};

47755d13   Benjamin Renard   Minor changes
248
std::ostream& operator<<(std::ostream& os, const ExpressionContainer& e)
a7f2648e   Benjamin Renard   Parser: First imp...
249
{
47755d13   Benjamin Renard   Minor changes
250
	boost::apply_visitor(ExpressionPrinter(os), e);
a7f2648e   Benjamin Renard   Parser: First imp...
251
252
253
254
255
256
257
258
259
	return os;
}
    
} /* namespace Expression */
} /* namespace parser */
} /* namespace AMDA */


#endif