Skip to content

Commit

Permalink
Spreadsheet: Fixed handling of unary minus/plus, exponentation order,…
Browse files Browse the repository at this point in the history
… and unit rule (#2099)
  • Loading branch information
eivindkv authored and wwmayer committed Jun 13, 2015
1 parent 12e4d59 commit 9d5d05a
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 270 deletions.
24 changes: 22 additions & 2 deletions src/Mod/Spreadsheet/App/Expression.cpp
Expand Up @@ -507,10 +507,9 @@ Expression * OperatorExpression::eval() const
case DIV:
output = new NumberExpression(owner, v1->getQuantity() / v2->getQuantity());
break;
case POW: {
case POW:
output = new NumberExpression(owner, v1->getQuantity().pow(v2->getQuantity()) );
break;
}
case EQ:
if (v1->getUnit() != v2->getUnit())
throw Exception("Incompatible units for + operator");
Expand Down Expand Up @@ -541,6 +540,12 @@ Expression * OperatorExpression::eval() const
throw Exception("Incompatible units for + operator");
output = new NumberExpression(owner, Quantity(v2->getValue() - v1->getValue()) < 1e-7);
break;
case NEG:
output = new NumberExpression(owner, -v1->getQuantity() );
break;
case POS:
output = new NumberExpression(owner, v1->getQuantity() );
break;
default:
assert(0);
}
Expand Down Expand Up @@ -581,6 +586,15 @@ std::string OperatorExpression::toString() const
{
std::stringstream s;

switch (op) {
case NEG:
s << "-";
break;
case POS:
s << "+";
break;
}

if (left->priority() < priority())
s << "(" << left->toString() << ")";
else
Expand Down Expand Up @@ -622,6 +636,9 @@ std::string OperatorExpression::toString() const
break;
case UNIT:
break;
case POS:
case NEG:
return s.str();
default:
assert(0);
}
Expand Down Expand Up @@ -664,6 +681,9 @@ int OperatorExpression::priority() const
return 10;
case POW:
return 10;
case NEG:
case POS:
return 15;
default:
return 0;
}
Expand Down
18 changes: 11 additions & 7 deletions src/Mod/Spreadsheet/App/Expression.h
Expand Up @@ -236,7 +236,7 @@ class SpreadsheetExport UnitExpression : public Expression {

virtual Expression * copy() const;

virtual int priority() const { return 10; }
virtual int priority() const { return 20; }

void setUnit(const Base::Quantity &_quantity);

Expand Down Expand Up @@ -270,7 +270,7 @@ class SpreadsheetExport NumberExpression : public UnitExpression {

virtual Expression * copy() const;

virtual int priority() const { return 10; }
virtual int priority() const { return 20; }

void negate();

Expand All @@ -288,7 +288,7 @@ class SpreadsheetExport ConstantExpression : public NumberExpression {

virtual Expression * copy() const;

virtual int priority() const { return 10; }
virtual int priority() const { return 20; }

std::string getName() const { return name; }

Expand Down Expand Up @@ -318,7 +318,9 @@ class SpreadsheetExport OperatorExpression : public UnitExpression {
GT,
LTE,
GTE,
UNIT
UNIT,
NEG,
POS
};
OperatorExpression(const App::DocumentObject *_owner = 0, Expression * _left = 0, Operator _op = NONE, Expression * _right = 0);

Expand Down Expand Up @@ -361,7 +363,7 @@ class SpreadsheetExport RangeExpression : public Expression {

virtual Expression * copy() const;

virtual int priority() const { return 0; }
virtual int priority() const { return 20; }

virtual void getDeps(std::set<Path> &props) const;

Expand Down Expand Up @@ -458,7 +460,7 @@ class SpreadsheetExport FunctionExpression : public UnitExpression {

virtual Expression * copy() const;

virtual int priority() const { return 10; }
virtual int priority() const { return 20; }

virtual void getDeps(std::set<Path> &props) const;

Expand Down Expand Up @@ -494,7 +496,7 @@ class SpreadsheetExport VariableExpression : public UnitExpression {

virtual Expression * copy() const;

virtual int priority() const { return 10; }
virtual int priority() const { return 20; }

virtual void getDeps(std::set<Path> &props) const;

Expand Down Expand Up @@ -535,6 +537,8 @@ class SpreadsheetExport StringExpression : public Expression {

virtual std::string getText() const { return text; }

virtual int priority() const { return 20; }

virtual Expression * copy() const;

protected:
Expand Down

0 comments on commit 9d5d05a

Please sign in to comment.