| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/gn/operators.h" | 5 #include "tools/gn/operators.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "tools/gn/err.h" | 8 #include "tools/gn/err.h" |
| 9 #include "tools/gn/parse_tree.h" | 9 #include "tools/gn/parse_tree.h" |
| 10 #include "tools/gn/scope.h" | 10 #include "tools/gn/scope.h" |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 } | 409 } |
| 410 | 410 |
| 411 // Binary ---------------------------------------------------------------------- | 411 // Binary ---------------------------------------------------------------------- |
| 412 | 412 |
| 413 Value ExecuteOr(Scope* scope, | 413 Value ExecuteOr(Scope* scope, |
| 414 const BinaryOpNode* op_node, | 414 const BinaryOpNode* op_node, |
| 415 const Value& left, | 415 const Value& left, |
| 416 const Value& right, | 416 const Value& right, |
| 417 Err* err) { | 417 Err* err) { |
| 418 if (left.type() != Value::BOOLEAN) { | 418 if (left.type() != Value::BOOLEAN) { |
| 419 *err = Err(left, "Left side of || operator is not a boolean."); | 419 *err = Err(op_node->left(), "Left side of || operator is not a boolean.", |
| 420 err->AppendRange(op_node->GetRange()); | 420 "Type is \"" + std::string(Value::DescribeType(left.type())) + |
| 421 "\" instead."); |
| 422 return Value(); |
| 421 } else if (right.type() != Value::BOOLEAN) { | 423 } else if (right.type() != Value::BOOLEAN) { |
| 422 *err = Err(right, "Right side of || operator is not a boolean."); | 424 *err = Err(op_node->right(), "Right side of || operator is not a boolean.", |
| 423 err->AppendRange(op_node->GetRange()); | 425 "Type is \"" + std::string(Value::DescribeType(right.type())) + |
| 426 "\" instead."); |
| 427 return Value(); |
| 424 } | 428 } |
| 425 return Value(op_node, left.boolean_value() || right.boolean_value()); | 429 return Value(op_node, left.boolean_value() || right.boolean_value()); |
| 426 } | 430 } |
| 427 | 431 |
| 428 Value ExecuteAnd(Scope* scope, | 432 Value ExecuteAnd(Scope* scope, |
| 429 const BinaryOpNode* op_node, | 433 const BinaryOpNode* op_node, |
| 430 const Value& left, | 434 const Value& left, |
| 431 const Value& right, | 435 const Value& right, |
| 432 Err* err) { | 436 Err* err) { |
| 433 if (left.type() != Value::BOOLEAN) { | 437 if (left.type() != Value::BOOLEAN) { |
| 434 *err = Err(left, "Left side of && operator is not a boolean."); | 438 *err = Err(op_node->left(), "Left side of && operator is not a boolean.", |
| 435 err->AppendRange(op_node->GetRange()); | 439 "Type is \"" + std::string(Value::DescribeType(left.type())) + |
| 440 "\" instead."); |
| 441 return Value(); |
| 436 } else if (right.type() != Value::BOOLEAN) { | 442 } else if (right.type() != Value::BOOLEAN) { |
| 437 *err = Err(right, "Right side of && operator is not a boolean."); | 443 *err = Err(op_node->right(), "Right side of && operator is not a boolean.", |
| 438 err->AppendRange(op_node->GetRange()); | 444 "Type is \"" + std::string(Value::DescribeType(right.type())) + |
| 445 "\" instead."); |
| 446 return Value(); |
| 439 } | 447 } |
| 440 return Value(op_node, left.boolean_value() && right.boolean_value()); | 448 return Value(op_node, left.boolean_value() && right.boolean_value()); |
| 441 } | 449 } |
| 442 | 450 |
| 443 } // namespace | 451 } // namespace |
| 444 | 452 |
| 445 // ---------------------------------------------------------------------------- | 453 // ---------------------------------------------------------------------------- |
| 446 | 454 |
| 447 bool IsUnaryOperator(const Token& token) { | 455 bool IsUnaryOperator(const Token& token) { |
| 448 return token.type() == Token::BANG; | 456 return token.type() == Token::BANG; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 return token.type() == Token::RIGHT_BRACE; | 488 return token.type() == Token::RIGHT_BRACE; |
| 481 } | 489 } |
| 482 | 490 |
| 483 Value ExecuteUnaryOperator(Scope* scope, | 491 Value ExecuteUnaryOperator(Scope* scope, |
| 484 const UnaryOpNode* op_node, | 492 const UnaryOpNode* op_node, |
| 485 const Value& expr, | 493 const Value& expr, |
| 486 Err* err) { | 494 Err* err) { |
| 487 DCHECK(op_node->op().type() == Token::BANG); | 495 DCHECK(op_node->op().type() == Token::BANG); |
| 488 | 496 |
| 489 if (expr.type() != Value::BOOLEAN) { | 497 if (expr.type() != Value::BOOLEAN) { |
| 490 *err = Err(expr, "Operand of ! operator is not a boolean."); | 498 *err = Err(op_node, "Operand of ! operator is not a boolean.", |
| 491 err->AppendRange(op_node->GetRange()); | 499 "Type is \"" + std::string(Value::DescribeType(expr.type())) + |
| 500 "\" instead."); |
| 492 return Value(); | 501 return Value(); |
| 493 } | 502 } |
| 494 // TODO(scottmg): Why no unary minus? | 503 // TODO(scottmg): Why no unary minus? |
| 495 return Value(op_node, !expr.boolean_value()); | 504 return Value(op_node, !expr.boolean_value()); |
| 496 } | 505 } |
| 497 | 506 |
| 498 Value ExecuteBinaryOperator(Scope* scope, | 507 Value ExecuteBinaryOperator(Scope* scope, |
| 499 const BinaryOpNode* op_node, | 508 const BinaryOpNode* op_node, |
| 500 const ParseNode* left, | 509 const ParseNode* left, |
| 501 const ParseNode* right, | 510 const ParseNode* right, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 return ExecuteLess(scope, op_node, left_value, right_value, err); | 588 return ExecuteLess(scope, op_node, left_value, right_value, err); |
| 580 | 589 |
| 581 // ||, &&. | 590 // ||, &&. |
| 582 if (op.type() == Token::BOOLEAN_OR) | 591 if (op.type() == Token::BOOLEAN_OR) |
| 583 return ExecuteOr(scope, op_node, left_value, right_value, err); | 592 return ExecuteOr(scope, op_node, left_value, right_value, err); |
| 584 if (op.type() == Token::BOOLEAN_AND) | 593 if (op.type() == Token::BOOLEAN_AND) |
| 585 return ExecuteAnd(scope, op_node, left_value, right_value, err); | 594 return ExecuteAnd(scope, op_node, left_value, right_value, err); |
| 586 | 595 |
| 587 return Value(); | 596 return Value(); |
| 588 } | 597 } |
| OLD | NEW |