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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/dart_backend/dart_codegen.dart

Issue 318723002: dart2dart: Restore loops with conditions and updates in backend. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: For loops and update expressions Created 6 years, 6 months 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) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 library dart_codegen; 5 library dart_codegen;
6 6
7 import 'dart_tree.dart' as tree; 7 import 'dart_tree.dart' as tree;
8 import 'dart_printer.dart'; 8 import 'dart_printer.dart';
9 import 'dart_tree_printer.dart' show TreePrinter; 9 import 'dart_tree_printer.dart' show TreePrinter;
10 import '../tree/tree.dart' as frontend; 10 import '../tree/tree.dart' as frontend;
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 // Fall through to break target 205 // Fall through to break target
206 } else if (fall is tree.Break && fall.target == stmt.target) { 206 } else if (fall is tree.Break && fall.target == stmt.target) {
207 // Fall through to equivalent break 207 // Fall through to equivalent break
208 } else { 208 } else {
209 usedLabels.add(stmt.target); 209 usedLabels.add(stmt.target);
210 statementBuffer.add(new Break(stmt.target.name)); 210 statementBuffer.add(new Break(stmt.target.name));
211 } 211 }
212 } 212 }
213 213
214 void visitContinue(tree.Continue stmt) { 214 void visitContinue(tree.Continue stmt) {
215 statementBuffer.add(new Continue(stmt.target.name)); 215 tree.Statement fall = fallthrough;
216 if (stmt.target.binding == fall) {
217 // Fall through to continue target
218 } else if (fall is tree.Continue && fall.target == stmt.target) {
219 // Fall through to equivalent continue
220 } else {
221 usedLabels.add(stmt.target);
222 statementBuffer.add(new Continue(stmt.target.name));
223 }
216 } 224 }
217 225
218 void visitIf(tree.If stmt) { 226 void visitIf(tree.If stmt) {
219 Expression condition = visitExpression(stmt.condition); 227 Expression condition = visitExpression(stmt.condition);
220 List<Statement> savedBuffer = statementBuffer; 228 List<Statement> savedBuffer = statementBuffer;
221 List<Statement> thenBuffer = statementBuffer = <Statement>[]; 229 List<Statement> thenBuffer = statementBuffer = <Statement>[];
222 visitStatement(stmt.thenStatement); 230 visitStatement(stmt.thenStatement);
223 List<Statement> elseBuffer = statementBuffer = <Statement>[]; 231 List<Statement> elseBuffer = statementBuffer = <Statement>[];
224 visitStatement(stmt.elseStatement); 232 visitStatement(stmt.elseStatement);
225 savedBuffer.add( 233 savedBuffer.add(
226 new If(condition, new Block(thenBuffer), new Block(elseBuffer))); 234 new If(condition, new Block(thenBuffer), new Block(elseBuffer)));
227 statementBuffer = savedBuffer; 235 statementBuffer = savedBuffer;
228 } 236 }
229 237
230 void visitWhile(tree.While stmt) { 238 void visitWhileTrue(tree.WhileTrue stmt) {
231 Expression condition = new Literal(new dart2js.BoolConstant(true)); 239 List<Expression> updates = stmt.updates.map(visitExpression)
240 .toList(growable:false);
241
232 List<Statement> savedBuffer = statementBuffer; 242 List<Statement> savedBuffer = statementBuffer;
243 tree.Statement savedFallthrough = fallthrough;
233 statementBuffer = <Statement>[]; 244 statementBuffer = <Statement>[];
234 tree.Statement savedFallthrough = fallthrough; 245 fallthrough = stmt;
235 fallthrough = stmt.body; 246
236 visitStatement(stmt.body); 247 visitStatement(stmt.body);
237 savedBuffer.add( 248 Statement body = new Block(statementBuffer);
238 new LabeledStatement( 249 Statement statement = new For(null, null, updates, body);
239 stmt.label.name, 250 if (usedLabels.remove(stmt.label.name)) {
240 new While(condition, new Block(statementBuffer)))); 251 statement = new LabeledStatement(stmt.label.name, statement);
252 }
253 savedBuffer.add(statement);
254
241 statementBuffer = savedBuffer; 255 statementBuffer = savedBuffer;
242 fallthrough = savedFallthrough; 256 fallthrough = savedFallthrough;
243 } 257 }
244 258
259 void visitWhileCondition(tree.WhileCondition stmt) {
260 Expression condition = visitExpression(stmt.condition);
261 List<Expression> updates = stmt.updates.map(visitExpression)
262 .toList(growable:false);
263
264 List<Statement> savedBuffer = statementBuffer;
265 tree.Statement savedFallthrough = fallthrough;
266 statementBuffer = <Statement>[];
267 fallthrough = stmt;
268
269 visitStatement(stmt.body);
270 Statement body = new Block(statementBuffer);
271 Statement statement;
272 if (updates.isEmpty) {
273 // while(E) is the same as for(;E;), but the former is nicer
274 statement = new While(condition, body);
275 } else {
276 statement = new For(null, condition, updates, body);
277 }
278 if (usedLabels.remove(stmt.label.name)) {
279 statement = new LabeledStatement(stmt.label.name, statement);
280 }
281 savedBuffer.add(statement);
282
283 statementBuffer = savedBuffer;
284 fallthrough = savedFallthrough;
285
286 visitStatement(stmt.next);
287 }
288
245 Expression visitConstant(tree.Constant exp) { 289 Expression visitConstant(tree.Constant exp) {
246 return emitConstant(exp.value); 290 return emitConstant(exp.value);
247 } 291 }
248 292
249 Expression visitLiteralList(tree.LiteralList exp) { 293 Expression visitLiteralList(tree.LiteralList exp) {
250 return new LiteralList( 294 return new LiteralList(
251 exp.values.map(visitExpression).toList(growable: false)); 295 exp.values.map(visitExpression).toList(growable: false));
252 } 296 }
253 297
254 Expression visitLiteralMap(tree.LiteralMap exp) { 298 Expression visitLiteralMap(tree.LiteralMap exp) {
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 if (constant is dart2js.ConstructedConstant && constant.isLiteralSymbol) { 456 if (constant is dart2js.ConstructedConstant && constant.isLiteralSymbol) {
413 dart2js.StringConstant nameConstant = constant.fields[0]; 457 dart2js.StringConstant nameConstant = constant.fields[0];
414 String nameString = nameConstant.value.slowToString(); 458 String nameString = nameConstant.value.slowToString();
415 return new LiteralSymbol(nameString); 459 return new LiteralSymbol(nameString);
416 } else { 460 } else {
417 throw "Unsupported constant: $constant"; 461 throw "Unsupported constant: $constant";
418 } 462 }
419 } 463 }
420 } 464 }
421 } 465 }
466
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698