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 backend_ast_nodes; | 5 library backend_ast_nodes; |
6 | 6 |
7 import '../constants/values.dart' as values; | 7 import '../constants/values.dart' as values; |
8 import '../dart_types.dart' as types; | 8 import '../dart_types.dart' as types; |
9 import '../elements/elements.dart' as elements; | 9 import '../elements/elements.dart' as elements; |
10 import '../tree/tree.dart' as tree; | 10 import '../tree/tree.dart' as tree; |
(...skipping 729 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 if (first) { | 740 if (first) { |
741 first = false; | 741 first = false; |
742 } else { | 742 } else { |
743 write(separator); | 743 write(separator); |
744 } | 744 } |
745 callback(x); | 745 callback(x); |
746 } | 746 } |
747 } | 747 } |
748 | 748 |
749 void writeOperator(String operator) { | 749 void writeOperator(String operator) { |
750 write(" "); // TODO(sigurdm,kmillikin): Minimize use of whitespace. | 750 write(" "); |
751 write(operator); | 751 write(operator); |
752 write(" "); | 752 write(" "); |
753 } | 753 } |
754 | 754 |
755 /// Unfolds singleton blocks and returns the inner statement. | 755 /// Unfolds singleton blocks and returns the inner statement. |
756 /// If an empty block is found, the [EmptyStatement] is returned instead. | 756 /// If an empty block is found, the [EmptyStatement] is returned instead. |
757 Statement unfoldBlocks(Statement stmt) { | 757 Statement unfoldBlocks(Statement stmt) { |
758 while (stmt is Block && stmt.statements.length == 1) { | 758 while (stmt is Block && stmt.statements.length == 1) { |
759 Statement inner = (stmt as Block).statements[0]; | 759 Statement inner = (stmt as Block).statements[0]; |
760 if (definesVariable(inner)) { | 760 if (definesVariable(inner)) { |
(...skipping 19 matching lines...) Expand all Loading... |
780 /// Prints the expression [e]. | 780 /// Prints the expression [e]. |
781 void writeExpression(Expression e) { | 781 void writeExpression(Expression e) { |
782 writeExp(e, EXPRESSION); | 782 writeExp(e, EXPRESSION); |
783 } | 783 } |
784 | 784 |
785 /// Prints [e] as an expression with precedence of at least [minPrecedence], | 785 /// Prints [e] as an expression with precedence of at least [minPrecedence], |
786 /// using parentheses if necessary to raise the precedence level. | 786 /// using parentheses if necessary to raise the precedence level. |
787 /// Abusing terminology slightly, the function accepts a [Receiver] which | 787 /// Abusing terminology slightly, the function accepts a [Receiver] which |
788 /// may also be the [SuperReceiver] object. | 788 /// may also be the [SuperReceiver] object. |
789 void writeExp(Receiver e, int minPrecedence, {beginStmt:false}) { | 789 void writeExp(Receiver e, int minPrecedence, {beginStmt:false}) { |
790 // TODO(kmillikin,sigurdm): it might be faster to use a Visitor. | |
791 void withPrecedence(int actual, void action()) { | 790 void withPrecedence(int actual, void action()) { |
792 if (actual < minPrecedence) { | 791 if (actual < minPrecedence) { |
793 write("("); | 792 write("("); |
794 beginStmt = false; | 793 beginStmt = false; |
795 action(); | 794 action(); |
796 write(")"); | 795 write(")"); |
797 } else { | 796 } else { |
798 action(); | 797 action(); |
799 } | 798 } |
800 } | 799 } |
(...skipping 12 matching lines...) Expand all Loading... |
813 write('('); | 812 write('('); |
814 } | 813 } |
815 if (e.returnType != null) { | 814 if (e.returnType != null) { |
816 writeType(e.returnType); | 815 writeType(e.returnType); |
817 write(' '); | 816 write(' '); |
818 } | 817 } |
819 if (e.name != null) { | 818 if (e.name != null) { |
820 write(e.name); | 819 write(e.name); |
821 } | 820 } |
822 writeParameters(e.parameters); | 821 writeParameters(e.parameters); |
823 // TODO(sigurdm,kmillikin): Print {} for "return null;" | |
824 if (stmt is Return) { | 822 if (stmt is Return) { |
825 write('=> '); | 823 write('=> '); |
826 writeExp(stmt.expression, EXPRESSION); | 824 writeExp(stmt.expression, EXPRESSION); |
827 } else { | 825 } else { |
828 writeBlock(stmt); | 826 writeBlock(stmt); |
829 } | 827 } |
830 if (needParen) { | 828 if (needParen) { |
831 write(')'); | 829 write(')'); |
832 } | 830 } |
833 }); | 831 }); |
(...skipping 22 matching lines...) Expand all Loading... |
856 write('-1/0.0'); | 854 write('-1/0.0'); |
857 }); | 855 }); |
858 } else if (v.isNaN) { | 856 } else if (v.isNaN) { |
859 withPrecedence(MULTIPLICATIVE, () { | 857 withPrecedence(MULTIPLICATIVE, () { |
860 write('0/0.0'); | 858 write('0/0.0'); |
861 }); | 859 }); |
862 } else { | 860 } else { |
863 write(v.toString()); | 861 write(v.toString()); |
864 } | 862 } |
865 } else { | 863 } else { |
866 // TODO(sigurdm): Use [ConstExp] to generate valid code for any | |
867 // constant. | |
868 write(e.value.unparse()); | 864 write(e.value.unparse()); |
869 } | 865 } |
870 } else if (e is LiteralList) { | 866 } else if (e is LiteralList) { |
871 if (e.isConst) { | 867 if (e.isConst) { |
872 write(' const '); | 868 write(' const '); |
873 } | 869 } |
874 if (e.typeArgument != null) { | 870 if (e.typeArgument != null) { |
875 write('<'); | 871 write('<'); |
876 writeType(e.typeArgument); | 872 writeType(e.typeArgument); |
877 write('>'); | 873 write('>'); |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1303 if (type.typeArguments != null && type.typeArguments.length > 0) { | 1299 if (type.typeArguments != null && type.typeArguments.length > 0) { |
1304 write('<'); | 1300 write('<'); |
1305 writeEach(',', type.typeArguments, writeType); | 1301 writeEach(',', type.typeArguments, writeType); |
1306 write('>'); | 1302 write('>'); |
1307 } | 1303 } |
1308 } | 1304 } |
1309 | 1305 |
1310 /// A list of string quotings that the printer may use to quote strings. | 1306 /// A list of string quotings that the printer may use to quote strings. |
1311 // Ignore multiline quotings for now. Would need to make sure that no | 1307 // Ignore multiline quotings for now. Would need to make sure that no |
1312 // newline (potentially prefixed by whitespace) follows the quoting. | 1308 // newline (potentially prefixed by whitespace) follows the quoting. |
1313 // TODO(sigurdm,kmillikin): Include multiline quotation schemes. | |
1314 static const _QUOTINGS = const <tree.StringQuoting>[ | 1309 static const _QUOTINGS = const <tree.StringQuoting>[ |
1315 const tree.StringQuoting(characters.$DQ, raw: false, leftQuoteLength: 1), | 1310 const tree.StringQuoting(characters.$DQ, raw: false, leftQuoteLength: 1), |
1316 const tree.StringQuoting(characters.$DQ, raw: true, leftQuoteLength: 1), | 1311 const tree.StringQuoting(characters.$DQ, raw: true, leftQuoteLength: 1), |
1317 const tree.StringQuoting(characters.$SQ, raw: false, leftQuoteLength: 1), | 1312 const tree.StringQuoting(characters.$SQ, raw: false, leftQuoteLength: 1), |
1318 const tree.StringQuoting(characters.$SQ, raw: true, leftQuoteLength: 1), | 1313 const tree.StringQuoting(characters.$SQ, raw: true, leftQuoteLength: 1), |
1319 ]; | 1314 ]; |
1320 | 1315 |
1321 static StringLiteralOutput analyzeStringLiteral(Expression node) { | 1316 static StringLiteralOutput analyzeStringLiteral(Expression node) { |
1322 // TODO(sigurdm,kmillikin): This might be a bit too expensive. Benchmark. | |
1323 // Flatten the StringConcat tree. | 1317 // Flatten the StringConcat tree. |
1324 List parts = []; // Expression or int (char node) | 1318 List parts = []; // Expression or int (char node) |
1325 void collectParts(Expression e) { | 1319 void collectParts(Expression e) { |
1326 if (e is StringConcat) { | 1320 if (e is StringConcat) { |
1327 e.expressions.forEach(collectParts); | 1321 e.expressions.forEach(collectParts); |
1328 } else if (e is Literal && e.value.isString) { | 1322 } else if (e is Literal && e.value.isString) { |
1329 for (int char in e.value.primitiveValue) { | 1323 for (int char in e.value.primitiveValue) { |
1330 parts.add(char); | 1324 parts.add(char); |
1331 } | 1325 } |
1332 } else { | 1326 } else { |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1569 final StringChunk previous; | 1563 final StringChunk previous; |
1570 final tree.StringQuoting quoting; | 1564 final tree.StringQuoting quoting; |
1571 num cost; | 1565 num cost; |
1572 | 1566 |
1573 OpenStringChunk(this.previous, this.quoting, this.cost); | 1567 OpenStringChunk(this.previous, this.quoting, this.cost); |
1574 | 1568 |
1575 StringChunk end(int endIndex) { | 1569 StringChunk end(int endIndex) { |
1576 return new StringChunk(previous, quoting, endIndex); | 1570 return new StringChunk(previous, quoting, endIndex); |
1577 } | 1571 } |
1578 } | 1572 } |
OLD | NEW |