OLD | NEW |
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 code_generator; | 5 library code_generator; |
6 | 6 |
7 import 'glue.dart'; | 7 import 'glue.dart'; |
8 | 8 |
9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; | 9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; |
10 import '../../js/js.dart' as js; | 10 import '../../js/js.dart' as js; |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 @override | 418 @override |
419 void visitExpressionStatement(tree_ir.ExpressionStatement node) { | 419 void visitExpressionStatement(tree_ir.ExpressionStatement node) { |
420 accumulator.add(new js.ExpressionStatement( | 420 accumulator.add(new js.ExpressionStatement( |
421 visitExpression(node.expression))); | 421 visitExpression(node.expression))); |
422 visitStatement(node.next); | 422 visitStatement(node.next); |
423 } | 423 } |
424 | 424 |
425 @override | 425 @override |
426 void visitIf(tree_ir.If node) { | 426 void visitIf(tree_ir.If node) { |
427 accumulator.add(new js.If(visitExpression(node.condition), | 427 accumulator.add(new js.If(visitExpression(node.condition), |
428 buildBody(node.thenStatement), | 428 buildBodyStatement(node.thenStatement), |
429 buildBody(node.elseStatement))); | 429 buildBodyStatement(node.elseStatement))); |
430 } | 430 } |
431 | 431 |
432 @override | 432 @override |
433 void visitLabeledStatement(tree_ir.LabeledStatement node) { | 433 void visitLabeledStatement(tree_ir.LabeledStatement node) { |
434 accumulator.add(buildLabeled(() => buildBody(node.body), | 434 accumulator.add(buildLabeled(() => buildBodyStatement(node.body), |
435 node.label, | 435 node.label, |
436 node.next)); | 436 node.next)); |
437 visitStatement(node.next); | 437 visitStatement(node.next); |
438 } | 438 } |
439 | 439 |
440 js.Statement buildLabeled(js.Statement buildBody(), | 440 js.Statement buildLabeled(js.Statement buildBody(), |
441 tree_ir.Label label, | 441 tree_ir.Label label, |
442 tree_ir.Statement fallthroughStatement) { | 442 tree_ir.Statement fallthroughStatement) { |
443 tree_ir.Statement savedFallthrough = fallthrough; | 443 tree_ir.Statement savedFallthrough = fallthrough; |
444 fallthrough = fallthroughStatement; | 444 fallthrough = fallthroughStatement; |
(...skipping 24 matching lines...) Expand all Loading... |
469 if (accumulator.length == 0) { | 469 if (accumulator.length == 0) { |
470 return new js.EmptyStatement(); | 470 return new js.EmptyStatement(); |
471 } | 471 } |
472 if (accumulator.length == 1) { | 472 if (accumulator.length == 1) { |
473 return accumulator.single; | 473 return accumulator.single; |
474 } | 474 } |
475 return new js.Block(accumulator); | 475 return new js.Block(accumulator); |
476 } | 476 } |
477 | 477 |
478 /// Builds a nested statement. | 478 /// Builds a nested statement. |
479 js.Statement buildBody(tree_ir.Statement statement) { | 479 js.Statement buildBodyStatement(tree_ir.Statement statement) { |
480 List<js.Statement> savedAccumulator = accumulator; | 480 List<js.Statement> savedAccumulator = accumulator; |
481 accumulator = new List<js.Statement>(); | 481 accumulator = <js.Statement>[]; |
482 visitStatement(statement); | 482 visitStatement(statement); |
483 js.Statement result = _bodyAsStatement(); | 483 js.Statement result = _bodyAsStatement(); |
484 accumulator = savedAccumulator; | 484 accumulator = savedAccumulator; |
485 return result; | 485 return result; |
486 } | 486 } |
487 | 487 |
| 488 js.Block buildBodyBlock(tree_ir.Statement statement) { |
| 489 List<js.Statement> savedAccumulator = accumulator; |
| 490 accumulator = <js.Statement>[]; |
| 491 visitStatement(statement); |
| 492 js.Statement result = new js.Block(accumulator); |
| 493 accumulator = savedAccumulator; |
| 494 return result; |
| 495 } |
| 496 |
488 js.Statement buildWhile(js.Expression condition, | 497 js.Statement buildWhile(js.Expression condition, |
489 tree_ir.Statement body, | 498 tree_ir.Statement body, |
490 tree_ir.Label label, | 499 tree_ir.Label label, |
491 tree_ir.Statement fallthroughStatement) { | 500 tree_ir.Statement fallthroughStatement) { |
492 return buildLabeled(() => new js.While(condition, buildBody(body)), | 501 return buildLabeled(() => new js.While(condition, buildBodyStatement(body)), |
493 label, | 502 label, |
494 fallthroughStatement); | 503 fallthroughStatement); |
495 } | 504 } |
496 | 505 |
497 @override | 506 @override |
498 void visitWhileCondition(tree_ir.WhileCondition node) { | 507 void visitWhileCondition(tree_ir.WhileCondition node) { |
499 accumulator.add( | 508 accumulator.add( |
500 buildWhile(visitExpression(node.condition), | 509 buildWhile(visitExpression(node.condition), |
501 node.body, | 510 node.body, |
502 node.label, | 511 node.label, |
503 node)); | 512 node)); |
504 visitStatement(node.next); | 513 visitStatement(node.next); |
505 } | 514 } |
506 | 515 |
507 @override | 516 @override |
508 void visitWhileTrue(tree_ir.WhileTrue node) { | 517 void visitWhileTrue(tree_ir.WhileTrue node) { |
509 accumulator.add( | 518 accumulator.add( |
510 buildWhile(new js.LiteralBool(true), node.body, node.label, node)); | 519 buildWhile(new js.LiteralBool(true), node.body, node.label, node)); |
511 } | 520 } |
512 | 521 |
513 @override | 522 @override |
514 void visitReturn(tree_ir.Return node) { | 523 void visitReturn(tree_ir.Return node) { |
515 accumulator.add(new js.Return(visitExpression(node.value))); | 524 accumulator.add(new js.Return(visitExpression(node.value))); |
516 } | 525 } |
517 | 526 |
518 @override | 527 @override |
519 void visitTry(tree_ir.Try node) { | 528 void visitTry(tree_ir.Try node) { |
520 // TODO(kmillikin): implement TryStatement. | 529 js.Block tryBlock = buildBodyBlock(node.tryBody); |
521 return giveup(node); | 530 tree_ir.Variable exceptionVariable = node.catchParameters.first; |
| 531 js.VariableDeclaration exceptionParameter = |
| 532 new js.VariableDeclaration(getVariableName(exceptionVariable)); |
| 533 js.Block catchBlock = buildBodyBlock(node.catchBody); |
| 534 js.Catch catchPart = new js.Catch(exceptionParameter, catchBlock); |
| 535 accumulator.add(new js.Try(tryBlock, catchPart, null)); |
522 } | 536 } |
523 | 537 |
524 @override | 538 @override |
525 js.Expression visitCreateBox(tree_ir.CreateBox node) { | 539 js.Expression visitCreateBox(tree_ir.CreateBox node) { |
526 return new js.ObjectInitializer([]); | 540 return new js.ObjectInitializer([]); |
527 } | 541 } |
528 | 542 |
529 @override | 543 @override |
530 js.Expression visitCreateInstance(tree_ir.CreateInstance node) { | 544 js.Expression visitCreateInstance(tree_ir.CreateInstance node) { |
531 ClassElement cls = node.classElement; | 545 ClassElement cls = node.classElement; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 | 640 |
627 @override | 641 @override |
628 visitVariableDeclaration(tree_ir.VariableDeclaration node) { | 642 visitVariableDeclaration(tree_ir.VariableDeclaration node) { |
629 return errorUnsupportedNode(node); | 643 return errorUnsupportedNode(node); |
630 } | 644 } |
631 | 645 |
632 errorUnsupportedNode(tree_ir.DartSpecificNode node) { | 646 errorUnsupportedNode(tree_ir.DartSpecificNode node) { |
633 throw "Unsupported node in JS backend: $node"; | 647 throw "Unsupported node in JS backend: $node"; |
634 } | 648 } |
635 } | 649 } |
OLD | NEW |