Commit ef417f051f3fbb2b364de260496e40fd421c3d23

Authored by Benjamin Renard
1 parent 36749bea

Improve function container

src/expressionParser/ExpressionContainer.hh
... ... @@ -6,9 +6,6 @@ namespace parser {
6 6  
7 7 struct op_power{};
8 8 struct op_powerTen{};
9   -struct fun_three{};
10   -struct fun_two{};
11   -struct fun_one{};
12 9  
13 10 struct Operator {
14 11 explicit Operator(std::string ihmSymbol, std::string kernelSymbol) : ihmSymbol(ihmSymbol), kernelSymbol(kernelSymbol) {};
... ... @@ -20,24 +17,45 @@ struct Operator {
20 17 namespace Expression{
21 18  
22 19 typedef std::string var;
  20 +
23 21 template <typename tag> struct binop;
24   -template <typename tag> struct comop;
25 22 template <typename tag> struct funcop;
26   -template <typename tag> struct triop;
27   -template <typename tag> struct forop;
28 23 struct BinaryOperation;
29 24 struct UnaryOperation;
  25 +struct FunctionOperation;
30 26  
31   -typedef boost::variant<var,
  27 +typedef boost::variant<
  28 + var,
32 29 boost::recursive_wrapper<UnaryOperation>,
33 30 boost::recursive_wrapper<BinaryOperation>,
  31 +
  32 + boost::recursive_wrapper<FunctionOperation>,
  33 +
34 34 boost::recursive_wrapper<funcop<op_power> >,
35   - boost::recursive_wrapper<binop<op_powerTen> >,
36   - boost::recursive_wrapper<binop<fun_one> >,
37   - boost::recursive_wrapper<triop<fun_two> >,
38   - boost::recursive_wrapper<forop<fun_three> >
  35 + boost::recursive_wrapper<binop<op_powerTen> >
39 36 > ExpressionContainer;
40 37  
  38 +struct FunctionOperation
  39 +{
  40 + explicit FunctionOperation(std::string func, const ExpressionContainer& arg1) : func(func) {
  41 + args.push_back(arg1);
  42 + }
  43 +
  44 + explicit FunctionOperation(std::string func, const ExpressionContainer& arg1, const ExpressionContainer& arg2) : func(func) {
  45 + args.push_back(arg1);
  46 + args.push_back(arg2);
  47 + }
  48 +
  49 + explicit FunctionOperation(std::string func, const ExpressionContainer& arg1, const ExpressionContainer& arg2, const ExpressionContainer& arg3) : func(func) {
  50 + args.push_back(arg1);
  51 + args.push_back(arg2);
  52 + args.push_back(arg3);
  53 + }
  54 +
  55 + std::string func;
  56 + std::vector<ExpressionContainer> args;
  57 +};
  58 +
41 59 struct UnaryOperation
42 60 {
43 61 explicit UnaryOperation(Operator op, const ExpressionContainer& e) : op(op), e(e) {}
... ... @@ -144,38 +162,12 @@ template &lt;typename tag&gt; struct binop
144 162 ExpressionContainer oper1, oper2;
145 163 };
146 164  
147   -template <typename tag> struct comop
148   -{
149   - explicit comop(const ExpressionContainer& l, const ExpressionContainer& r) : oper1(l), oper2(r) { }
150   - ExpressionContainer oper1, oper2;
151   -};
152   -
153 165 template <typename tag> struct funcop
154 166 {
155 167 explicit funcop(const ExpressionContainer& l, const ExpressionContainer& r) : oper1(l), oper2(r) { }
156 168 ExpressionContainer oper1, oper2;
157 169 };
158 170  
159   -template <typename tag> struct triop
160   -{
161   - explicit triop(const ExpressionContainer& functionId,
162   - const ExpressionContainer& l
163   - , const ExpressionContainer& r)
164   - : oper1(functionId), oper2(l), oper3(r) { }
165   -ExpressionContainer oper1, oper2, oper3;
166   -};
167   -
168   -template <typename tag> struct forop
169   -{
170   - explicit forop(const ExpressionContainer& functionId,
171   - const ExpressionContainer& l,
172   - const ExpressionContainer& m,
173   - const ExpressionContainer& r)
174   - : oper1(functionId), oper2(l), oper3(m),oper4(r) { }
175   -ExpressionContainer oper1, oper2, oper3,oper4;
176   -};
177   -
178   -
179 171 } /* namespace Expression */
180 172  
181 173 } /* namespace parser */
... ...
src/expressionParser/ExpressionPrinter.hh
... ... @@ -19,17 +19,24 @@ public:
19 19 void operator()(const BinaryOperation& b) const { print(b.op.kernelSymbol, b.l, b.r); }
20 20 void operator()(const UnaryOperation& u) const{ printUnique(u.op.kernelSymbol,u.e); }
21 21  
  22 + void operator()(const FunctionOperation& f) const{
  23 + _os << f.func << "(";
  24 + for (unsigned int i = 0; i < f.args.size(); ++i) {
  25 + if (i != 0) {
  26 + _os << ",";
  27 + }
  28 + boost::apply_visitor(*this, f.args[i]);
  29 + }
  30 + _os << ")";
  31 + }
  32 +
22 33 // Logical
23 34  
24 35 // Power Math operators
25   - void operator()(const funcop<op_power>& b) const { printFunc("pow", b.oper1, b.oper2); }
  36 + void operator()(const funcop<op_power>& b) const { _os << "power"; }
26 37 void operator()(const binop<op_powerTen>& b) const { printPowerTen("e", b.oper1, b.oper2); }
27 38  
28 39 // Functions
29   - void operator()(const forop<fun_three>& b) const { printFunctionThree(b.oper1, b.oper2, b.oper3, b.oper4); }
30   - void operator()(const triop<fun_two>& b) const { printFunctionTwo(b.oper1, b.oper2, b.oper3); }
31   - void operator()(const binop<fun_one>& b) const { printFunctionOne(b.oper1, b.oper2); }
32   -
33 40 // printer
34 41 void print(const std::string& op, const ExpressionContainer& l, const ExpressionContainer& r) const
35 42 {
... ... @@ -52,46 +59,6 @@ public:
52 59 boost::apply_visitor(*this, r);
53 60 }
54 61  
55   - void printFunc(const std::string& op, const ExpressionContainer& l, const ExpressionContainer& r) const
56   - {
57   - _os << op;
58   - _os << "(";
59   - boost::apply_visitor(*this, l);
60   - _os << ",";
61   - boost::apply_visitor(*this, r);
62   - _os << ")";
63   - }
64   -
65   - void printFunctionThree(const ExpressionContainer& functionId, const ExpressionContainer& l, const ExpressionContainer& m, const ExpressionContainer& r) const
66   - {
67   - boost::apply_visitor(*this, functionId);
68   - _os << "(";
69   - boost::apply_visitor(*this, l);
70   - _os << ',';
71   - boost::apply_visitor(*this, m);
72   - _os << ',';
73   - boost::apply_visitor(*this, r);
74   - _os << ")";
75   - }
76   -
77   - void printFunctionTwo(const ExpressionContainer& functionId, const ExpressionContainer& l, const ExpressionContainer& r) const
78   - {
79   - boost::apply_visitor(*this, functionId);
80   - _os << "(";
81   - boost::apply_visitor(*this, l);
82   - _os << ',';
83   - boost::apply_visitor(*this, r);
84   - _os << ")";
85   - }
86   -
87   - void printFunctionOne(const ExpressionContainer& functionId, const ExpressionContainer& l) const
88   - {
89   - boost::apply_visitor(*this, functionId);
90   - _os << "(";
91   - boost::apply_visitor(*this, l);
92   - _os << ")";
93   - }
94   -
95 62 };
96 63  
97 64 std::ostream& operator<<(std::ostream& os, const ExpressionContainer& e)
... ...
src/expressionParser/ParserGrammar.hh
... ... @@ -50,9 +50,9 @@ public:
50 50 greater_= fold<GreaterOperation>(lower_.alias())[greaterOperator_ >> lower_];
51 51 lower_ = fold<LowerOperation>( functions_.alias())[lowerOperator_ >> functions_];
52 52  
53   - functions_ = (threeArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<Expression::forop <fun_three>>(_1,_2,_3,_4)] ||
54   - (twoArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<Expression::triop <fun_two>>(_1,_2,_3)]||
55   - (oneArgsFunction>>"(">>funArgs_>>")")[_val= boost::phoenix::construct<Expression::binop <fun_one>>(_1,_2)]|not_[_val=_1];
  53 + functions_ = (threeArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3,_4)] ||
  54 + (twoArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3)]||
  55 + (oneArgsFunction>>"(">>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2)]|not_[_val=_1];
56 56 // UNARY OPERATIONS
57 57 not_ = (notOperator_ > const_) [_val = boost::phoenix::construct<NotOperation>(_1)] | const_[_val = _1];
58 58  
... ...