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

Unified Diff: sdk/lib/_internal/compiler/implementation/dart_backend/tree_tracer.dart

Issue 284213002: dart2dart: Logical operators and related rewrite rules in dart_tree. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Flattening of nested ifs is now iterated. Created 6 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/compiler/implementation/dart_backend/tree_tracer.dart
diff --git a/sdk/lib/_internal/compiler/implementation/dart_backend/tree_tracer.dart b/sdk/lib/_internal/compiler/implementation/dart_backend/tree_tracer.dart
index 95ef81c85dfa08e5d1fbf1897844bc252f735e6a..c636063c4279499d5ef896105b9931dac522848e 100644
--- a/sdk/lib/_internal/compiler/implementation/dart_backend/tree_tracer.dart
+++ b/sdk/lib/_internal/compiler/implementation/dart_backend/tree_tracer.dart
@@ -52,6 +52,8 @@ class BlockCollector extends Visitor {
visitConcatenateStrings(ConcatenateStrings node) {}
visitConstant(Constant node) {}
visitConditional(Conditional node) {}
+ visitLogicalOperator(LogicalOperator node) {}
+ visitNot(Not node) {}
visitLabeledStatement(LabeledStatement node) {
Block target = new Block();
@@ -186,6 +188,14 @@ class TreeTracer extends TracerUtil with Visitor {
printStatement(null, expr(node));
}
+ visitLogicalOperator(LogicalOperator node) {
+ printStatement(null, expr(node));
+ }
+
+ visitNot(Not node) {
+ printStatement(null, expr(node));
+ }
+
visitReturn(Return node) {
printStatement(null, "return ${expr(node.value)}");
}
@@ -266,10 +276,35 @@ class SubexpressionVisitor extends Visitor<String, String> {
return "${node.value}";
}
+ bool usesInfixNotation(Expression node) {
+ return node is Conditional || node is LogicalOperator;
Kevin Millikin (Google) 2014/05/19 11:36:41 The implementation doesn't seem to match the name.
asgerf 2014/05/19 13:29:07 Yeah we need proper precedence-awareness. Right no
+ }
+
String visitConditional(Conditional node) {
- return visitExpression(node.condition) + ' ? ' +
- visitExpression(node.thenExpression) + ' : ' +
- visitExpression(node.elseExpression);
+ String condition = visitExpression(node.condition);
+ String thenExpr = visitExpression(node.thenExpression);
+ String elseExpr = visitExpression(node.elseExpression);
+ return "$condition ? $thenExpr : $elseExpr";
+ }
+
+ String visitLogicalOperator(LogicalOperator node) {
+ String left = visitExpression(node.left);
+ String right = visitExpression(node.right);
+ if (usesInfixNotation(node.left)) {
+ left = "($left)";
+ }
+ if (usesInfixNotation(node.right)) {
+ right = "($right)";
+ }
+ return "$left ${node.operator} $right";
+ }
+
+ String visitNot(Not node) {
+ String operand = visitExpression(node.operand);
+ if (usesInfixNotation(node.operand)) {
+ operand = '($operand)';
+ }
+ return '!$operand';
}
// Note: There should not be statements in the context of expressions.

Powered by Google App Engine
This is Rietveld 408576698