Commit 8a7bf6d4bb2d517e272d30144cbbdbdc4ce531d5
1 parent
c0bbbeb7
Exists in
master
and in
87 other branches
Fix operation priority + add some tests (2)
Showing
2 changed files
with
23 additions
and
21 deletions
Show diff stats
src/expressionParser/ParserGrammar.hh
... | ... | @@ -26,39 +26,36 @@ class ParserGrammar : public qi::grammar<It, Expression::ExpressionContainer(), |
26 | 26 | public: |
27 | 27 | ParserGrammar(AMDA::helpers::Properties& lProperties) : ParserGrammar::base_type(expr_) |
28 | 28 | { |
29 | - expr_ = or_.alias(); | |
29 | + expr_ = equal_.alias(); | |
30 | 30 | |
31 | - // Logical Operators | |
32 | - or_ = PARSERGRAMMAR_ADD_OPERATOR(OrOperation, and_, orOperator_); | |
33 | - and_ = PARSERGRAMMAR_ADD_OPERATOR(AndOperation, equal_, andOperator_); | |
34 | - equal_ = PARSERGRAMMAR_ADD_OPERATOR(EqualOperation, unequal_, equalOperator_); | |
35 | - unequal_ = PARSERGRAMMAR_ADD_OPERATOR(UnequalOperation, greaterOrEqual_, unequalOperator_); | |
36 | - greaterOrEqual_ = PARSERGRAMMAR_ADD_OPERATOR(GreaterOrEqualOperation, lowerOrEqual_, greaterOrEqualOperator_); | |
37 | - lowerOrEqual_ = PARSERGRAMMAR_ADD_OPERATOR(LowerOrEqualOperation, sum_, lowerOrEqualOperator_); | |
31 | + equal_ = PARSERGRAMMAR_ADD_OPERATOR(EqualOperation, unequal_, equalOperator_); | |
32 | + unequal_ = PARSERGRAMMAR_ADD_OPERATOR(UnequalOperation, greaterOrEqual_, unequalOperator_); | |
33 | + greaterOrEqual_ = PARSERGRAMMAR_ADD_OPERATOR(GreaterOrEqualOperation, lowerOrEqual_, greaterOrEqualOperator_); | |
34 | + lowerOrEqual_ = PARSERGRAMMAR_ADD_OPERATOR(LowerOrEqualOperation, greater_, lowerOrEqualOperator_); | |
35 | + greater_ = PARSERGRAMMAR_ADD_OPERATOR(GreaterOperation, lower_, greaterOperator_); | |
36 | + lower_ = PARSERGRAMMAR_ADD_OPERATOR(LowerOperation, or_, lowerOperator_); | |
38 | 37 | |
39 | - sum_ = fold<SumOperation>(difference_.alias())[sumOperator_ >> difference_]; | |
40 | - difference_ = fold<DifferenceOperation>(factor_.alias())[differenceOperator_ >> factor_]; | |
38 | + or_ = PARSERGRAMMAR_ADD_OPERATOR(OrOperation, sum_, orOperator_); | |
39 | + sum_ = PARSERGRAMMAR_ADD_OPERATOR(SumOperation, difference_, sumOperator_); | |
40 | + difference_ = PARSERGRAMMAR_ADD_OPERATOR(DifferenceOperation, and_, differenceOperator_); | |
41 | + and_ = PARSERGRAMMAR_ADD_OPERATOR(AndOperation, factor_, andOperator_); | |
42 | + factor_ = PARSERGRAMMAR_ADD_OPERATOR(FactorOperation, division_, factorOperator_); | |
43 | + division_ = PARSERGRAMMAR_ADD_OPERATOR(DivisionOperation, powerTen_, divisionOperator_); | |
41 | 44 | |
42 | - factor_ = fold<FactorOperation>(division_.alias())[factorOperator_ >> division_]; | |
43 | - division_ = fold<DivisionOperation>(powerTen_.alias())[divisionOperator_ >> powerTen_]; | |
44 | - powerTen_ = fold<PowerTenOperation>(plusSign_.alias())[powerTenOperator_ >> plusSign_]; | |
45 | + powerTen_ = PARSERGRAMMAR_ADD_OPERATOR(PowerTenOperation, pow_, powerTenOperator_); | |
46 | + pow_ = PARSERGRAMMAR_ADD_OPERATOR(funcop<op_power>, plusSign_, powOperator_); | |
45 | 47 | |
46 | 48 | plusSign_ = ((sumOperator_ >>-*sumOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] || |
47 | 49 | ((differenceOperator_ >>differenceOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] |minusSign_ [_val = _1]; |
48 | - minusSign_ = (((-*sumOperator_>>differenceOperator_ >>-*sumOperator_) | (-factorOperator_>>differenceOperator_>>-factorOperator_)) > pow_) [_val = boost::phoenix::construct<MinusSignOperation>(_1)] | pow_[_val = _1]; | |
50 | + minusSign_ = (((-*sumOperator_>>differenceOperator_ >>-*sumOperator_) | (-factorOperator_>>differenceOperator_>>-factorOperator_)) > not_) [_val = boost::phoenix::construct<MinusSignOperation>(_1)] | not_[_val = _1]; | |
51 | + not_ = (notOperator_ > functions_) [_val = boost::phoenix::construct<NotOperation>(_1)] | functions_[_val = _1]; | |
49 | 52 | |
50 | - // Numerical Operators | |
51 | 53 | |
52 | - pow_ = fold<funcop<op_power> >(greater_.alias())[powOperator_ >> greater_]; | |
53 | - | |
54 | - greater_= fold<GreaterOperation>(lower_.alias())[greaterOperator_ >> lower_]; | |
55 | - lower_ = fold<LowerOperation>( functions_.alias())[lowerOperator_ >> functions_]; | |
56 | 54 | |
57 | 55 | functions_ = (threeArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3,_4)] || |
58 | 56 | (twoArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3)]|| |
59 | - (oneArgsFunction>>"(">>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2)]|not_[_val=_1]; | |
57 | + (oneArgsFunction>>"(">>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2)]|const_[_val=_1]; | |
60 | 58 | // UNARY OPERATIONS |
61 | - not_ = (notOperator_ > const_) [_val = boost::phoenix::construct<NotOperation>(_1)] | const_[_val = _1]; | |
62 | 59 | |
63 | 60 | const_ = (definedConstants) [_val = boost::phoenix::construct<ConstantOperation>(_1)] | param_[_val = _1]; |
64 | 61 | param_= (definedParams >>('(' >> (spectArgs_|vectorArgs_)>>')'))[_val='$'+_1+"["+qi::_2+"]"] || | ... | ... |
test/parser/amda_test_parser.csv
... | ... | @@ -22,3 +22,8 @@ IHM Expression;Kernel Expression |
22 | 22 | 6.3712e(-9+3);(6.3712e(-9+3)) |
23 | 23 | 6.3712e-9+3;((6.3712e-9)+3) |
24 | 24 | 6.3712e-9*3;((6.3712e-9)*3) |
25 | +3*2>5-1;((3*2)>(5-1)) | |
26 | +3=2>=1;(3==(2>=1)) | |
27 | +imf;$imf | |
28 | +imf(0):$imf[0] | |
29 | +sqrt(imf(0)*imf(0)+imf(1)*imf(1)+imf(2)*imf(2));sqrt(((($imf[0]*$imf[0])+($imf[1]*$imf[1]))+($imf[2]*$imf[2]))) | ... | ... |