ExpressionPrinter.hh 2.63 KB
#ifndef EXPRESSIONPRINTER_HH_
#define EXPRESSIONPRINTER_HH_

#include "ExpressionContainer.hh"

//#define ADD_DEBUG_TAG

namespace AMDA {
namespace parser {
namespace Expression {

class ExpressionPrinter : public boost::static_visitor<void>
{
public:
	ExpressionPrinter(std::ostream& os) : _os(os) {}
	std::ostream& _os;

	void operator()(const var& v) const {
		#ifdef ADD_DEBUG_TAG
		_os << "@VAR@";
		#endif
		_os << v;
	}

	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 << ")";
	}

	void operator()(const UnaryOperation& u) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@UNOP@";
		#endif
		_os << u.op.kernelSymbol;
		boost::apply_visitor(*this, u.e);
	}

	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 << ")";
	}

	void operator()(const FunctionOperation& f) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@FUNC@";
		#endif
		_os << f.func << "(";
		bool isProcess = (!f.func.empty() && (f.func[0] == '#'));
		for (unsigned int i = 0; i < f.args.size(); ++i) {
			if (i != 0) {
				_os << (isProcess ? ";" : ",");
			}
			boost::apply_visitor(*this, f.args[i]);
		}
		_os << ")";
	}

	void operator()(const ParameterDef& p) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@PARAM@";
		#endif

		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;
		_os << "$";
		_os << p.param;
		_os << suffix;
		_os << components;
	}
};

std::ostream& operator<<(std::ostream& os, const ExpressionContainer& e)
{
	boost::apply_visitor(ExpressionPrinter(os), e);
	return os;
}
    
} /* namespace Expression */
} /* namespace parser */
} /* namespace AMDA */


#endif