ExpressionContainer.hh 5.11 KB
#ifndef EXPRESSIONCONTAINER_HH_
#define EXPRESSIONCONTAINER_HH_

namespace AMDA {
namespace parser {

struct op_power{};
struct op_powerTen{};

struct Operator {
	explicit Operator(std::string ihmSymbol, std::string kernelSymbol) : ihmSymbol(ihmSymbol), kernelSymbol(kernelSymbol) {};

	std::string ihmSymbol = "";
	std::string kernelSymbol = "";
};

namespace Expression{

typedef std::string var;

template <typename tag> struct binop;
template <typename tag> struct funcop;
struct BinaryOperation;
struct UnaryOperation;
struct FunctionOperation;

typedef boost::variant<
	var,
	boost::recursive_wrapper<UnaryOperation>,
	boost::recursive_wrapper<BinaryOperation>,

	boost::recursive_wrapper<FunctionOperation>,

	boost::recursive_wrapper<funcop<op_power> >,
	boost::recursive_wrapper<binop<op_powerTen> >
> ExpressionContainer;

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

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

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
{
	explicit PowerTenOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("e","e"), r) { }
};

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

template <typename tag> struct binop
{
	explicit binop(const ExpressionContainer& l, const ExpressionContainer& r) : oper1(l), oper2(r) { }
	ExpressionContainer oper1, oper2;
};

template <typename tag> struct funcop
{
	explicit funcop(const ExpressionContainer& l, const ExpressionContainer& r) : oper1(l), oper2(r) { }
	ExpressionContainer oper1, oper2;
};

} /* namespace Expression */

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

#endif