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

Unified Diff: frog/gen.dart

Issue 9700016: Allow interpolated strings and adjacent strings to be compile-time constants. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 9 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
« no previous file with comments | « frog/analyze.dart ('k') | frog/minfrog » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: frog/gen.dart
diff --git a/frog/gen.dart b/frog/gen.dart
index a5bde90bc77d7fd22d31adc07fb0e82803a78bf3..b5b2c7d6d70312c2ea7cfc6f296b220199665f28 100644
--- a/frog/gen.dart
+++ b/frog/gen.dart
@@ -2296,22 +2296,38 @@ class MethodGenerator implements TreeVisitor, CallingContext {
}
}
+ String foldStrings(List<StringValue> strings) {
+ StringBuffer buffer = new StringBuffer();
+ for (var part in strings) buffer.add(part.constValue.actualValue);
+ return buffer.toString();
+ }
+
visitStringConcatExpression(StringConcatExpression node) {
var items = [];
+ var itemsConst = [];
for (var item in node.strings) {
Value val = visitValue(item);
assert(val.type.isString);
+ if (val.isConst) itemsConst.add(val);
items.add(val.code);
}
- return new Value(world.stringType, '(${Strings.join(items, " + ")})',
- node.span);
+ if (items.length == itemsConst.length) {
ngeoffray 2012/03/14 12:13:11 instead of having two lists, could you just have a
+ return new StringValue(foldStrings(itemsConst), true, node.span);
+ } else {
+ String code = '(${Strings.join(items, " + ")})';
+ return new Value(world.stringType, code, node.span);
+ }
}
visitStringInterpExpression(StringInterpExpression node) {
var items = [];
+ var itemsConst = [];
for (var item in node.pieces) {
var val = visitValue(item);
- val.invoke(this, 'toString', item, Arguments.EMPTY);
+ bool isConst = val.isConst && val.type.isString;
+ if (!isConst) {
+ val.invoke(this, 'toString', item, Arguments.EMPTY);
+ }
// TODO(jimhug): Ensure this solves all precedence problems.
// TODO(jmesserly): We could be smarter about prefix/postfix, but we'd
// need to know if it will compile to a ++ or to some sort of += form.
@@ -2322,10 +2338,15 @@ class MethodGenerator implements TreeVisitor, CallingContext {
// No need to concat empty strings except the first.
if (items.length == 0 || (code != "''" && code != '""')) {
items.add(code);
+ if (isConst) itemsConst.add(val);
}
}
- return new Value(world.stringType, '(${Strings.join(items, " + ")})',
- node.span);
+ if (items.length == itemsConst.length) {
+ return new StringValue(foldStrings(itemsConst), true, node.span);
+ } else {
+ String code = '(${Strings.join(items, " + ")})';
+ return new Value(world.stringType, code, node.span);
+ }
}
}
« no previous file with comments | « frog/analyze.dart ('k') | frog/minfrog » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698