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 /** | 5 /** |
| 6 * Top level generator object for writing code and keeping track of | 6 * Top level generator object for writing code and keeping track of |
| 7 * dependencies. | 7 * dependencies. |
| 8 * | 8 * |
| 9 * Should have two compilation models, but only one implemented so far. | 9 * Should have two compilation models, but only one implemented so far. |
| 10 * | 10 * |
| (...skipping 2278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2289 | 2289 |
| 2290 _isUnaryIncrement(Expression item) { | 2290 _isUnaryIncrement(Expression item) { |
| 2291 if (item is UnaryExpression) { | 2291 if (item is UnaryExpression) { |
| 2292 UnaryExpression u = item; | 2292 UnaryExpression u = item; |
| 2293 return u.op.kind == TokenKind.INCR || u.op.kind == TokenKind.DECR; | 2293 return u.op.kind == TokenKind.INCR || u.op.kind == TokenKind.DECR; |
| 2294 } else { | 2294 } else { |
| 2295 return false; | 2295 return false; |
| 2296 } | 2296 } |
| 2297 } | 2297 } |
| 2298 | 2298 |
| 2299 String foldStrings(List<StringValue> strings) { | |
| 2300 StringBuffer buffer = new StringBuffer(); | |
| 2301 for (var part in strings) buffer.add(part.constValue.actualValue); | |
| 2302 return buffer.toString(); | |
| 2303 } | |
| 2304 | |
| 2299 visitStringConcatExpression(StringConcatExpression node) { | 2305 visitStringConcatExpression(StringConcatExpression node) { |
| 2300 var items = []; | 2306 var items = []; |
| 2307 var itemsConst = []; | |
| 2301 for (var item in node.strings) { | 2308 for (var item in node.strings) { |
| 2302 Value val = visitValue(item); | 2309 Value val = visitValue(item); |
| 2303 assert(val.type.isString); | 2310 assert(val.type.isString); |
| 2311 if (val.isConst) itemsConst.add(val); | |
| 2304 items.add(val.code); | 2312 items.add(val.code); |
| 2305 } | 2313 } |
| 2306 return new Value(world.stringType, '(${Strings.join(items, " + ")})', | 2314 if (items.length == itemsConst.length) { |
|
ngeoffray
2012/03/14 12:13:11
instead of having two lists, could you just have a
| |
| 2307 node.span); | 2315 return new StringValue(foldStrings(itemsConst), true, node.span); |
| 2316 } else { | |
| 2317 String code = '(${Strings.join(items, " + ")})'; | |
| 2318 return new Value(world.stringType, code, node.span); | |
| 2319 } | |
| 2308 } | 2320 } |
| 2309 | 2321 |
| 2310 visitStringInterpExpression(StringInterpExpression node) { | 2322 visitStringInterpExpression(StringInterpExpression node) { |
| 2311 var items = []; | 2323 var items = []; |
| 2324 var itemsConst = []; | |
| 2312 for (var item in node.pieces) { | 2325 for (var item in node.pieces) { |
| 2313 var val = visitValue(item); | 2326 var val = visitValue(item); |
| 2314 val.invoke(this, 'toString', item, Arguments.EMPTY); | 2327 bool isConst = val.isConst && val.type.isString; |
| 2328 if (!isConst) { | |
| 2329 val.invoke(this, 'toString', item, Arguments.EMPTY); | |
| 2330 } | |
| 2315 // TODO(jimhug): Ensure this solves all precedence problems. | 2331 // TODO(jimhug): Ensure this solves all precedence problems. |
| 2316 // TODO(jmesserly): We could be smarter about prefix/postfix, but we'd | 2332 // TODO(jmesserly): We could be smarter about prefix/postfix, but we'd |
| 2317 // need to know if it will compile to a ++ or to some sort of += form. | 2333 // need to know if it will compile to a ++ or to some sort of += form. |
| 2318 var code = val.code; | 2334 var code = val.code; |
| 2319 if (_expressionNeedsParens(item)) { | 2335 if (_expressionNeedsParens(item)) { |
| 2320 code = '(${code})'; | 2336 code = '(${code})'; |
| 2321 } | 2337 } |
| 2322 // No need to concat empty strings except the first. | 2338 // No need to concat empty strings except the first. |
| 2323 if (items.length == 0 || (code != "''" && code != '""')) { | 2339 if (items.length == 0 || (code != "''" && code != '""')) { |
| 2324 items.add(code); | 2340 items.add(code); |
| 2341 if (isConst) itemsConst.add(val); | |
| 2325 } | 2342 } |
| 2326 } | 2343 } |
| 2327 return new Value(world.stringType, '(${Strings.join(items, " + ")})', | 2344 if (items.length == itemsConst.length) { |
| 2328 node.span); | 2345 return new StringValue(foldStrings(itemsConst), true, node.span); |
| 2346 } else { | |
| 2347 String code = '(${Strings.join(items, " + ")})'; | |
| 2348 return new Value(world.stringType, code, node.span); | |
| 2349 } | |
| 2329 } | 2350 } |
| 2330 } | 2351 } |
| 2331 | 2352 |
| 2332 | 2353 |
| 2333 // TODO(jmesserly): move this into its own file? | 2354 // TODO(jmesserly): move this into its own file? |
| 2334 class Arguments { | 2355 class Arguments { |
| 2335 static Arguments _empty; | 2356 static Arguments _empty; |
| 2336 static Arguments get EMPTY() { | 2357 static Arguments get EMPTY() { |
| 2337 if (_empty == null) { | 2358 if (_empty == null) { |
| 2338 _empty = new Arguments(null, []); | 2359 _empty = new Arguments(null, []); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2460 return true; | 2481 return true; |
| 2461 } | 2482 } |
| 2462 | 2483 |
| 2463 } | 2484 } |
| 2464 | 2485 |
| 2465 class ReturnKind { | 2486 class ReturnKind { |
| 2466 static final int IGNORE = 1; | 2487 static final int IGNORE = 1; |
| 2467 static final int POST = 2; | 2488 static final int POST = 2; |
| 2468 static final int PRE = 3; | 2489 static final int PRE = 3; |
| 2469 } | 2490 } |
| OLD | NEW |