Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 class Printer implements NodeVisitor { | 5 class Printer implements NodeVisitor { |
| 6 final bool shouldCompressOutput; | 6 final bool shouldCompressOutput; |
| 7 leg.Compiler compiler; | 7 leg.Compiler compiler; |
| 8 leg.CodeBuffer outBuffer; | 8 leg.CodeBuffer outBuffer; |
| 9 int indentLevel = 0; | 9 int indentLevel = 0; |
| 10 bool inForInit = false; | 10 bool inForInit = false; |
| (...skipping 930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 941 static int nthLetter(int n) { | 941 static int nthLetter(int n) { |
| 942 return (n < 26) ? charCodes.$a + n : charCodes.$A + n - 26; | 942 return (n < 26) ? charCodes.$a + n : charCodes.$A + n - 26; |
| 943 } | 943 } |
| 944 | 944 |
| 945 String declareName(String oldName) { | 945 String declareName(String oldName) { |
| 946 const LETTERS = 52; | 946 const LETTERS = 52; |
| 947 const DIGITS = 10; | 947 const DIGITS = 10; |
| 948 if (maps.isEmpty) return oldName; | 948 if (maps.isEmpty) return oldName; |
| 949 | 949 |
| 950 String newName; | 950 String newName; |
| 951 int n = nameNumber; | 951 do { |
| 952 if (n < LETTERS) { | 952 int n = nameNumber; |
| 953 // Start naming variables a, b, c, ..., z, A, B, C, ..., Z. | 953 if (n < LETTERS) { |
| 954 newName = new String.fromCharCodes([nthLetter(n)]); | 954 // Start naming variables a, b, c, ..., z, A, B, C, ..., Z. |
| 955 } else { | 955 newName = new String.fromCharCodes([nthLetter(n)]); |
| 956 // Then name variables a0, a1, a2, ..., a9, b0, b1, ..., Z9, aa0, aa1, ... | 956 } else { |
| 957 // For all functions with fewer than 500 locals this is just as compact | 957 // Then name variables a0, a1, ..., a9, b0, b1, ..., Z9, aa0, aa1, ... |
| 958 // as using aa, ab, etc. but avoids clashes with keywords. | 958 // For all functions with fewer than 500 locals this is just as compact |
| 959 n -= LETTERS; | 959 // as using aa, ab, etc. but avoids clashes with keywords. |
| 960 int digit = n % DIGITS; | 960 n -= LETTERS; |
| 961 n ~/= DIGITS; | 961 int digit = n % DIGITS; |
| 962 int alphaChars = 1; | 962 n ~/= DIGITS; |
| 963 int nameSpaceSize = LETTERS; | 963 int alphaChars = 1; |
| 964 // Find out whether we should use the 1-character namespace (size 52), the | 964 int nameSpaceSize = LETTERS; |
| 965 // 2-character namespace (size 52*52), etc. | 965 // Find out whether we should use the 1-character namespace (size 52), |
| 966 while (n >= nameSpaceSize) { | 966 // the 2-character namespace (size 52*52), etc. |
| 967 n -= nameSpaceSize; | 967 while (n >= nameSpaceSize) { |
| 968 alphaChars++; | 968 n -= nameSpaceSize; |
| 969 nameSpaceSize *= LETTERS; | 969 alphaChars++; |
| 970 nameSpaceSize *= LETTERS; | |
| 971 } | |
| 972 var codes = <int>[]; | |
| 973 for (var i = 0; i < alphaChars; i++) { | |
| 974 nameSpaceSize ~/= LETTERS; | |
| 975 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); | |
| 976 } | |
| 977 codes.add(charCodes.$0 + digit); | |
| 978 newName = new String.fromCharCodes(codes); | |
| 970 } | 979 } |
| 971 var codes = <int>[]; | 980 assert(const RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); |
| 972 for (var i = 0; i < alphaChars; i++) { | 981 nameNumber++; |
| 973 nameSpaceSize ~/= LETTERS; | 982 } while (isReserved(newName)); |
| 974 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); | |
| 975 } | |
| 976 codes.add(charCodes.$0 + digit); | |
| 977 newName = new String.fromCharCodes(codes); | |
| 978 } | |
| 979 assert(const RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); | |
| 980 nameNumber++; | |
| 981 maps.last()[oldName] = newName; | 983 maps.last()[oldName] = newName; |
| 982 return newName; | 984 return newName; |
| 983 } | 985 } |
| 986 | |
| 987 // We only have names here that are single-letter or end with a digit, | |
| 988 // since those are the ones we can generate. | |
|
floitsch
2012/10/25 08:42:40
assert this.
erikcorry
2012/10/25 09:09:28
Done.
| |
| 989 bool isReserved(String name) { | |
| 990 return name == "I"; // Minified version of Isolate. | |
|
floitsch
2012/10/25 12:58:38
Is there a way to get this set from the namer?
erikcorry
2012/12/06 09:38:07
Done.
| |
| 991 } | |
| 984 } | 992 } |
| OLD | NEW |