Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: runtime/vm/parser.cc

Issue 11410086: Use iterator, moveNext(), current. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Address comments. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/parser.h" 5 #include "vm/parser.h"
6 6
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/compiler.h" 9 #include "vm/compiler.h"
10 #include "vm/compiler_stats.h" 10 #include "vm/compiler_stats.h"
(...skipping 5810 matching lines...) Expand 10 before | Expand all | Expand 10 after
5821 // where T is the type of the for loop variable. However, the type error 5821 // where T is the type of the for loop variable. However, the type error
5822 // would refer to the compiler generated iterator and could confuse the user. 5822 // would refer to the compiler generated iterator and could confuse the user.
5823 // It is better to leave the iterator untyped and postpone the type error 5823 // It is better to leave the iterator untyped and postpone the type error
5824 // until the loop variable is assigned to. 5824 // until the loop variable is assigned to.
5825 const AbstractType& iterator_type = Type::ZoneHandle(Type::DynamicType()); 5825 const AbstractType& iterator_type = Type::ZoneHandle(Type::DynamicType());
5826 LocalVariable* iterator_var = 5826 LocalVariable* iterator_var =
5827 new LocalVariable(collection_pos, iterator_name, iterator_type); 5827 new LocalVariable(collection_pos, iterator_name, iterator_type);
5828 current_block_->scope->AddVariable(iterator_var); 5828 current_block_->scope->AddVariable(iterator_var);
5829 5829
5830 // Generate initialization of iterator variable. 5830 // Generate initialization of iterator variable.
5831 const String& iterator_method_name = 5831 const String& iterator_getter_name =
5832 String::ZoneHandle(Symbols::GetIterator()); 5832 String::ZoneHandle(Symbols::GetIterator());
5833 ArgumentListNode* no_args = new ArgumentListNode(collection_pos); 5833 ArgumentListNode* no_args = new ArgumentListNode(collection_pos);
5834 AstNode* get_iterator = new InstanceCallNode( 5834 AstNode* get_iterator = new InstanceGetterNode(
5835 collection_pos, collection_expr, iterator_method_name, no_args); 5835 collection_pos, collection_expr, iterator_getter_name);
5836 AstNode* iterator_init = 5836 AstNode* iterator_init =
5837 new StoreLocalNode(collection_pos, iterator_var, get_iterator); 5837 new StoreLocalNode(collection_pos, iterator_var, get_iterator);
5838 current_block_->statements->Add(iterator_init); 5838 current_block_->statements->Add(iterator_init);
5839 5839
5840 // Generate while loop condition. 5840 // Generate while loop condition.
5841 AstNode* iterator_has_next = new InstanceGetterNode( 5841 AstNode* iterator_moveNext = new InstanceCallNode(
5842 collection_pos, 5842 collection_pos,
5843 new LoadLocalNode(collection_pos, iterator_var), 5843 new LoadLocalNode(collection_pos, iterator_var),
5844 String::ZoneHandle(Symbols::HasNext())); 5844 String::ZoneHandle(Symbols::MoveNext()),
5845 no_args);
5845 5846
5846 // Parse the for loop body. Ideally, we would use ParseNestedStatement() 5847 // Parse the for loop body. Ideally, we would use ParseNestedStatement()
5847 // here, but that does not work well because we have to insert an implicit 5848 // here, but that does not work well because we have to insert an implicit
5848 // variable assignment and potentially a variable declaration in the 5849 // variable assignment and potentially a variable declaration in the
5849 // loop body. 5850 // loop body.
5850 OpenLoopBlock(); 5851 OpenLoopBlock();
5851 current_block_->scope->AddLabel(label); 5852 current_block_->scope->AddLabel(label);
5852 5853
5853 AstNode* iterator_next = new InstanceCallNode( 5854 AstNode* iterator_current = new InstanceGetterNode(
5854 collection_pos, 5855 collection_pos,
5855 new LoadLocalNode(collection_pos, iterator_var), 5856 new LoadLocalNode(collection_pos, iterator_var),
5856 String::ZoneHandle(Symbols::Next()), 5857 String::ZoneHandle(Symbols::Current()));
5857 no_args);
5858 5858
5859 // Generate assignment of next iterator value to loop variable. 5859 // Generate assignment of next iterator value to loop variable.
5860 AstNode* loop_var_assignment = NULL; 5860 AstNode* loop_var_assignment = NULL;
5861 if (loop_var != NULL) { 5861 if (loop_var != NULL) {
5862 // The for loop declares a new variable. Add it to the loop body scope. 5862 // The for loop declares a new variable. Add it to the loop body scope.
5863 current_block_->scope->AddVariable(loop_var); 5863 current_block_->scope->AddVariable(loop_var);
5864 loop_var_assignment = 5864 loop_var_assignment =
5865 new StoreLocalNode(loop_var_pos, loop_var, iterator_next); 5865 new StoreLocalNode(loop_var_pos, loop_var, iterator_current);
5866 } else { 5866 } else {
5867 AstNode* loop_var_primary = 5867 AstNode* loop_var_primary =
5868 ResolveIdent(loop_var_pos, *loop_var_name, false); 5868 ResolveIdent(loop_var_pos, *loop_var_name, false);
5869 ASSERT(!loop_var_primary->IsPrimaryNode()); 5869 ASSERT(!loop_var_primary->IsPrimaryNode());
5870 loop_var_assignment = 5870 loop_var_assignment =
5871 CreateAssignmentNode(loop_var_primary, iterator_next); 5871 CreateAssignmentNode(loop_var_primary, iterator_current);
5872 if (loop_var_assignment == NULL) { 5872 if (loop_var_assignment == NULL) {
5873 ErrorMsg(loop_var_pos, "variable or field '%s' is not assignable", 5873 ErrorMsg(loop_var_pos, "variable or field '%s' is not assignable",
5874 loop_var_name->ToCString()); 5874 loop_var_name->ToCString());
5875 } 5875 }
5876 } 5876 }
5877 current_block_->statements->Add(loop_var_assignment); 5877 current_block_->statements->Add(loop_var_assignment);
5878 5878
5879 // Now parse the for-in loop statement or block. 5879 // Now parse the for-in loop statement or block.
5880 if (CurrentToken() == Token::kLBRACE) { 5880 if (CurrentToken() == Token::kLBRACE) {
5881 ConsumeToken(); 5881 ConsumeToken();
5882 ParseStatementSequence(); 5882 ParseStatementSequence();
5883 ExpectToken(Token::kRBRACE); 5883 ExpectToken(Token::kRBRACE);
5884 } else { 5884 } else {
5885 AstNode* statement = ParseStatement(); 5885 AstNode* statement = ParseStatement();
5886 if (statement != NULL) { 5886 if (statement != NULL) {
5887 current_block_->statements->Add(statement); 5887 current_block_->statements->Add(statement);
5888 } 5888 }
5889 } 5889 }
5890 5890
5891 SequenceNode* for_loop_statement = CloseBlock(); 5891 SequenceNode* for_loop_statement = CloseBlock();
5892 5892
5893 AstNode* while_statement = 5893 AstNode* while_statement =
5894 new WhileNode(forin_pos, label, iterator_has_next, for_loop_statement); 5894 new WhileNode(forin_pos, label, iterator_moveNext, for_loop_statement);
5895 current_block_->statements->Add(while_statement); 5895 current_block_->statements->Add(while_statement);
5896 5896
5897 return CloseBlock(); // Implicit block around while loop. 5897 return CloseBlock(); // Implicit block around while loop.
5898 } 5898 }
5899 5899
5900 5900
5901 AstNode* Parser::ParseForStatement(String* label_name) { 5901 AstNode* Parser::ParseForStatement(String* label_name) {
5902 TRACE_PARSER("ParseForStatement"); 5902 TRACE_PARSER("ParseForStatement");
5903 const intptr_t for_pos = TokenPos(); 5903 const intptr_t for_pos = TokenPos();
5904 ConsumeToken(); 5904 ConsumeToken();
(...skipping 4111 matching lines...) Expand 10 before | Expand all | Expand 10 after
10016 void Parser::SkipQualIdent() { 10016 void Parser::SkipQualIdent() {
10017 ASSERT(IsIdentifier()); 10017 ASSERT(IsIdentifier());
10018 ConsumeToken(); 10018 ConsumeToken();
10019 if (CurrentToken() == Token::kPERIOD) { 10019 if (CurrentToken() == Token::kPERIOD) {
10020 ConsumeToken(); // Consume the kPERIOD token. 10020 ConsumeToken(); // Consume the kPERIOD token.
10021 ExpectIdentifier("identifier expected after '.'"); 10021 ExpectIdentifier("identifier expected after '.'");
10022 } 10022 }
10023 } 10023 }
10024 10024
10025 } // namespace dart 10025 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698