Blame view

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

#include "ExpressionContainer.hh"

c1f4db8e   Benjamin Renard   Add process to te...
6
7
//#define ADD_DEBUG_TAG

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

47755d13   Benjamin Renard   Minor changes
12
class ExpressionPrinter : public boost::static_visitor<void>
a7f2648e   Benjamin Renard   Parser: First imp...
13
{
47755d13   Benjamin Renard   Minor changes
14
public:
c1f4db8e   Benjamin Renard   Add process to te...
15
16
	ExpressionPrinter(std::ostream& os) : _os(os) {}
	std::ostream& _os;
a7f2648e   Benjamin Renard   Parser: First imp...
17

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

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

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

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

02869c99   Benjamin Renard   Improve parse of ...
70
71
72
73
	void operator()(const ParameterDef& p) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@PARAM@";
		#endif
e8489ddc   Benjamin Renard   Fix printer for a...
74
75
76
77
78
79
80
81
82
83
84
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

		std::string components = "";
		std::string prefix = "";
		std::string suffix = "";

		if ((p.components.size() == 1) && (p.components[0].compare("*") != 0)) {
			components += "[";
			components += p.components[0];
			components += "]";
		}
		else if (p.components.size() == 2) {
			if (p.components[0].compare("*") == 0) {
				if (p.components[1].compare("*") != 0) {
					// Extract column
					prefix = "(";
					suffix  = ").getColumn(";
					suffix += p.components[1];
					suffix += ")";
				}
				else {
					// *,* => no components defined
				}
			}
			else if (p.components[1].compare("*") == 0) {
				components += "[";
				components += p.components[0];
				components += "]";
			}
			else {
				components += "[";
				components += p.components[0];
				components += "][";
				components += p.components[1];
				components += "]";
			}
		}

		_os << prefix;
02869c99   Benjamin Renard   Improve parse of ...
112
113
		_os << "$";
		_os << p.param;
e8489ddc   Benjamin Renard   Fix printer for a...
114
115
		_os << suffix;
		_os << components;
02869c99   Benjamin Renard   Improve parse of ...
116
	}
a7f2648e   Benjamin Renard   Parser: First imp...
117
118
};

47755d13   Benjamin Renard   Minor changes
119
std::ostream& operator<<(std::ostream& os, const ExpressionContainer& e)
a7f2648e   Benjamin Renard   Parser: First imp...
120
{
47755d13   Benjamin Renard   Minor changes
121
	boost::apply_visitor(ExpressionPrinter(os), e);
a7f2648e   Benjamin Renard   Parser: First imp...
122
123
124
125
126
127
128
129
130
	return os;
}
    
} /* namespace Expression */
} /* namespace parser */
} /* namespace AMDA */


#endif