OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project 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 "src/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/ast-literal-reindexer.h" | 9 #include "src/ast/ast-literal-reindexer.h" |
10 #include "src/ast/scopeinfo.h" | 10 #include "src/ast/scopeinfo.h" |
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 } | 544 } |
545 | 545 |
546 | 546 |
547 bool ParserTraits::ShortcutNumericLiteralBinaryExpression( | 547 bool ParserTraits::ShortcutNumericLiteralBinaryExpression( |
548 Expression** x, Expression* y, Token::Value op, int pos, | 548 Expression** x, Expression* y, Token::Value op, int pos, |
549 AstNodeFactory* factory) { | 549 AstNodeFactory* factory) { |
550 if ((*x)->AsLiteral() && (*x)->AsLiteral()->raw_value()->IsNumber() && | 550 if ((*x)->AsLiteral() && (*x)->AsLiteral()->raw_value()->IsNumber() && |
551 y->AsLiteral() && y->AsLiteral()->raw_value()->IsNumber()) { | 551 y->AsLiteral() && y->AsLiteral()->raw_value()->IsNumber()) { |
552 double x_val = (*x)->AsLiteral()->raw_value()->AsNumber(); | 552 double x_val = (*x)->AsLiteral()->raw_value()->AsNumber(); |
553 double y_val = y->AsLiteral()->raw_value()->AsNumber(); | 553 double y_val = y->AsLiteral()->raw_value()->AsNumber(); |
| 554 bool x_has_dot = (*x)->AsLiteral()->raw_value()->ContainsDot(); |
| 555 bool y_has_dot = y->AsLiteral()->raw_value()->ContainsDot(); |
| 556 bool has_dot = x_has_dot || y_has_dot; |
554 switch (op) { | 557 switch (op) { |
555 case Token::ADD: | 558 case Token::ADD: |
556 *x = factory->NewNumberLiteral(x_val + y_val, pos); | 559 *x = factory->NewNumberLiteral(x_val + y_val, pos, has_dot); |
557 return true; | 560 return true; |
558 case Token::SUB: | 561 case Token::SUB: |
559 *x = factory->NewNumberLiteral(x_val - y_val, pos); | 562 *x = factory->NewNumberLiteral(x_val - y_val, pos, has_dot); |
560 return true; | 563 return true; |
561 case Token::MUL: | 564 case Token::MUL: |
562 *x = factory->NewNumberLiteral(x_val * y_val, pos); | 565 *x = factory->NewNumberLiteral(x_val * y_val, pos, has_dot); |
563 return true; | 566 return true; |
564 case Token::DIV: | 567 case Token::DIV: |
565 *x = factory->NewNumberLiteral(x_val / y_val, pos); | 568 *x = factory->NewNumberLiteral(x_val / y_val, pos, has_dot); |
566 return true; | 569 return true; |
567 case Token::BIT_OR: { | 570 case Token::BIT_OR: { |
568 int value = DoubleToInt32(x_val) | DoubleToInt32(y_val); | 571 int value = DoubleToInt32(x_val) | DoubleToInt32(y_val); |
569 *x = factory->NewNumberLiteral(value, pos); | 572 *x = factory->NewNumberLiteral(value, pos, has_dot); |
570 return true; | 573 return true; |
571 } | 574 } |
572 case Token::BIT_AND: { | 575 case Token::BIT_AND: { |
573 int value = DoubleToInt32(x_val) & DoubleToInt32(y_val); | 576 int value = DoubleToInt32(x_val) & DoubleToInt32(y_val); |
574 *x = factory->NewNumberLiteral(value, pos); | 577 *x = factory->NewNumberLiteral(value, pos, has_dot); |
575 return true; | 578 return true; |
576 } | 579 } |
577 case Token::BIT_XOR: { | 580 case Token::BIT_XOR: { |
578 int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val); | 581 int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val); |
579 *x = factory->NewNumberLiteral(value, pos); | 582 *x = factory->NewNumberLiteral(value, pos, has_dot); |
580 return true; | 583 return true; |
581 } | 584 } |
582 case Token::SHL: { | 585 case Token::SHL: { |
583 int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f); | 586 int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f); |
584 *x = factory->NewNumberLiteral(value, pos); | 587 *x = factory->NewNumberLiteral(value, pos, has_dot); |
585 return true; | 588 return true; |
586 } | 589 } |
587 case Token::SHR: { | 590 case Token::SHR: { |
588 uint32_t shift = DoubleToInt32(y_val) & 0x1f; | 591 uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
589 uint32_t value = DoubleToUint32(x_val) >> shift; | 592 uint32_t value = DoubleToUint32(x_val) >> shift; |
590 *x = factory->NewNumberLiteral(value, pos); | 593 *x = factory->NewNumberLiteral(value, pos, has_dot); |
591 return true; | 594 return true; |
592 } | 595 } |
593 case Token::SAR: { | 596 case Token::SAR: { |
594 uint32_t shift = DoubleToInt32(y_val) & 0x1f; | 597 uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
595 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift); | 598 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift); |
596 *x = factory->NewNumberLiteral(value, pos); | 599 *x = factory->NewNumberLiteral(value, pos, has_dot); |
597 return true; | 600 return true; |
598 } | 601 } |
599 default: | 602 default: |
600 break; | 603 break; |
601 } | 604 } |
602 } | 605 } |
603 return false; | 606 return false; |
604 } | 607 } |
605 | 608 |
606 | 609 |
607 Expression* ParserTraits::BuildUnaryExpression(Expression* expression, | 610 Expression* ParserTraits::BuildUnaryExpression(Expression* expression, |
608 Token::Value op, int pos, | 611 Token::Value op, int pos, |
609 AstNodeFactory* factory) { | 612 AstNodeFactory* factory) { |
610 DCHECK(expression != NULL); | 613 DCHECK(expression != NULL); |
611 if (expression->IsLiteral()) { | 614 if (expression->IsLiteral()) { |
612 const AstValue* literal = expression->AsLiteral()->raw_value(); | 615 const AstValue* literal = expression->AsLiteral()->raw_value(); |
613 if (op == Token::NOT) { | 616 if (op == Token::NOT) { |
614 // Convert the literal to a boolean condition and negate it. | 617 // Convert the literal to a boolean condition and negate it. |
615 bool condition = literal->BooleanValue(); | 618 bool condition = literal->BooleanValue(); |
616 return factory->NewBooleanLiteral(!condition, pos); | 619 return factory->NewBooleanLiteral(!condition, pos); |
617 } else if (literal->IsNumber()) { | 620 } else if (literal->IsNumber()) { |
618 // Compute some expressions involving only number literals. | 621 // Compute some expressions involving only number literals. |
619 double value = literal->AsNumber(); | 622 double value = literal->AsNumber(); |
| 623 bool has_dot = literal->ContainsDot(); |
620 switch (op) { | 624 switch (op) { |
621 case Token::ADD: | 625 case Token::ADD: |
622 return expression; | 626 return expression; |
623 case Token::SUB: | 627 case Token::SUB: |
624 return factory->NewNumberLiteral(-value, pos); | 628 return factory->NewNumberLiteral(-value, pos, has_dot); |
625 case Token::BIT_NOT: | 629 case Token::BIT_NOT: |
626 return factory->NewNumberLiteral(~DoubleToInt32(value), pos); | 630 return factory->NewNumberLiteral(~DoubleToInt32(value), pos, has_dot); |
627 default: | 631 default: |
628 break; | 632 break; |
629 } | 633 } |
630 } | 634 } |
631 } | 635 } |
632 // Desugar '+foo' => 'foo*1' | 636 // Desugar '+foo' => 'foo*1' |
633 if (op == Token::ADD) { | 637 if (op == Token::ADD) { |
634 return factory->NewBinaryOperation( | 638 return factory->NewBinaryOperation( |
635 Token::MUL, expression, factory->NewNumberLiteral(1, pos, true), pos); | 639 Token::MUL, expression, factory->NewNumberLiteral(1, pos, true), pos); |
636 } | 640 } |
(...skipping 5824 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6461 } | 6465 } |
6462 | 6466 |
6463 | 6467 |
6464 void Parser::RaiseLanguageMode(LanguageMode mode) { | 6468 void Parser::RaiseLanguageMode(LanguageMode mode) { |
6465 SetLanguageMode(scope_, | 6469 SetLanguageMode(scope_, |
6466 static_cast<LanguageMode>(scope_->language_mode() | mode)); | 6470 static_cast<LanguageMode>(scope_->language_mode() | mode)); |
6467 } | 6471 } |
6468 | 6472 |
6469 } // namespace internal | 6473 } // namespace internal |
6470 } // namespace v8 | 6474 } // namespace v8 |
OLD | NEW |