| 14 | } |
| 15 | |
| 16 | bool MathParser::addexpr(Expression& expr) |
| 17 | { |
| 18 | if (!multexpr(expr)) |
| 19 | return false; |
| 20 | |
| 21 | while (true) |
| 22 | { |
| 23 | NodeType type; |
| 24 | |
| 25 | if (match(TokenType::Plus)) |
| 26 | type = NodeType::Add; |
| 27 | else if (match(TokenType::Dash)) |
| 28 | type = NodeType::Subtract; |
| 29 | else |
| 30 | return true; |
| 31 | |
| 32 | if (!multexpr(expr)) |
| 33 | { |
| 34 | setError("Expected expression following '" + curToken().sval() + "'."); |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | NodePtr right = expr.popNode(); |
| 39 | NodePtr left = expr.popNode(); |
| 40 | |
| 41 | ConstValueNode *leftVal = dynamic_cast<ConstValueNode *>(left.get()); |
| 42 | ConstValueNode *rightVal = dynamic_cast<ConstValueNode *>(right.get()); |
| 43 | if (leftVal && rightVal) |
| 44 | { |
| 45 | double v = (type == NodeType::Add) ? |
| 46 | leftVal->value() + rightVal->value() : |
| 47 | leftVal->value() - rightVal->value(); |
| 48 | expr.pushNode(NodePtr(new ConstValueNode(v))); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | if (left->isBool() || right->isBool()) |
| 53 | { |
| 54 | setError("Can't apply '" + curToken().sval() + "' to " |
| 55 | "logical expression."); |
| 56 | return false; |
| 57 | } |
| 58 | expr.pushNode(NodePtr(new BinMathNode(type, std::move(left), std::move(right)))); |
| 59 | } |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | bool MathParser::multexpr(Expression& expr) |
| 65 | { |