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