| 120 | } |
| 121 | |
| 122 | bool MathParser::uminus(Expression& expr) |
| 123 | { |
| 124 | if (!match(TokenType::Dash)) |
| 125 | return primary(expr); |
| 126 | |
| 127 | if (!primary(expr)) |
| 128 | { |
| 129 | setError("Expecting expression following '-'."); |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | NodePtr sub = expr.popNode(); // Pop the primary. |
| 134 | assert(sub.get()); |
| 135 | ConstValueNode *constnode = dynamic_cast<ConstValueNode *>(sub.get()); |
| 136 | if (constnode) |
| 137 | { |
| 138 | double v = -(constnode->value()); |
| 139 | expr.pushNode(NodePtr(new ConstValueNode(v))); |
| 140 | } |
| 141 | else |
| 142 | expr.pushNode(NodePtr(new UnMathNode(NodeType::Negative, std::move(sub)))); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | bool MathParser::primary(Expression& expr) |
| 147 | { |