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

Side by Side Diff: frog/leg/compile_time_constants.dart

Issue 9706018: Make string interpolations 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 Constant implements Hashable { 5 class Constant implements Hashable {
6 const Constant(); 6 const Constant();
7 7
8 bool isNull() => false; 8 bool isNull() => false;
9 bool isBool() => false; 9 bool isBool() => false;
10 bool isTrue() => false; 10 bool isTrue() => false;
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 key.writeJsCode(buffer, handler); 326 key.writeJsCode(buffer, handler);
327 buffer.add(": "); 327 buffer.add(": ");
328 Constant value = values[i]; 328 Constant value = values[i];
329 // TODO(floitsch): share this code with the ListConstant and 329 // TODO(floitsch): share this code with the ListConstant and
330 // ConstructedConstant. 330 // ConstructedConstant.
331 if (value.isObject()) { 331 if (value.isObject()) {
332 String name = handler.getNameForConstant(value); 332 String name = handler.getNameForConstant(value);
333 buffer.add("$isolatePrototype.$name"); 333 buffer.add("$isolatePrototype.$name");
334 } else { 334 } else {
335 value.writeJsCode(buffer, handler); 335 value.writeJsCode(buffer, handler);
336 } 336 }
337 } 337 }
338 buffer.add("}"); 338 buffer.add("}");
339 } 339 }
340 340
341 void badFieldCountError() { 341 void badFieldCountError() {
342 handler.compiler.internalError( 342 handler.compiler.internalError(
343 "Compiler and ConstantMap disagree on number of fields."); 343 "Compiler and ConstantMap disagree on number of fields.");
344 } 344 }
345 345
346 ClassElement classElement = type.element; 346 ClassElement classElement = type.element;
347 buffer.add("new "); 347 buffer.add("new ");
348 buffer.add(handler.getJsConstructor(classElement)); 348 buffer.add(handler.getJsConstructor(classElement));
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 } 761 }
762 762
763 Constant visitLiteralNull(LiteralNull node) { 763 Constant visitLiteralNull(LiteralNull node) {
764 return new NullConstant(); 764 return new NullConstant();
765 } 765 }
766 766
767 Constant visitLiteralString(LiteralString node) { 767 Constant visitLiteralString(LiteralString node) {
768 return new StringConstant(node.dartString); 768 return new StringConstant(node.dartString);
769 } 769 }
770 770
771 Constant visitStringJuxtapostion(StringJuxtaposition node) {
772 if (node.dartString !== null) {
ngeoffray 2012/03/15 08:09:41 !node.isInterpolation
Lasse Reichstein Nielsen 2012/03/15 08:39:09 For consistency :)
773 return new StringConstant(node.dartString);
774 }
775 StringConstant left = evaluate(node.left);
776 StringConstant right = evaluate(node.right);
777 return new StringConstant(new DartString.concat(left.value, right.value));
778 }
779
780 Constant visitStringInterpolation(StringInterpolation node) {
781 StringConstant initialString = evaluate(node.string);
782 DartString accumulator = initialString.value;
783 for (StringInterpolationPart part in node.parts) {
784 Constant expression = evaluate(part.expression);
785 DartString expressionString;
786 if (expression.isNum() || expression.isBool()) {
787 Object value = expression.value;
788 expressionString = new DartString.literal(value.toString());
789 } else if (expression.isString()) {
790 expressionString = expression.value;
791 } else {
792 error(part.expression);
793 }
794 accumulator = new DartString.concat(accumulator, expressionString);
795 StringConstant partString = evaluate(part.string);
796 accumulator = new DartString.concat(accumulator, partString.value);
797 };
ngeoffray 2012/03/15 08:09:41 extra ';'
Lasse Reichstein Nielsen 2012/03/15 08:39:09 well spotted.
798 return new StringConstant(accumulator);
799 }
800
771 // TODO(floitsch): provide better error-messages. 801 // TODO(floitsch): provide better error-messages.
772 Constant visitSend(Send send) { 802 Constant visitSend(Send send) {
773 Element element = elements[send]; 803 Element element = elements[send];
774 if (Elements.isStaticOrTopLevelField(element)) { 804 if (Elements.isStaticOrTopLevelField(element)) {
775 if (element.modifiers === null || 805 if (element.modifiers === null ||
776 !element.modifiers.isFinal()) { 806 !element.modifiers.isFinal()) {
777 error(send); 807 error(send);
778 } 808 }
779 return constantHandler.compileVariable(element); 809 return constantHandler.compileVariable(element);
780 } else if (send.isPrefix) { 810 } else if (send.isPrefix) {
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 return constant; 1045 return constant;
1016 } 1046 }
1017 1047
1018 error(Node node) { 1048 error(Node node) {
1019 // TODO(floitsch): get the list of constants that are currently compiled 1049 // TODO(floitsch): get the list of constants that are currently compiled
1020 // and present some kind of stack-trace. 1050 // and present some kind of stack-trace.
1021 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; 1051 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT;
1022 compiler.reportError(node, new CompileTimeConstantError(kind, const [])); 1052 compiler.reportError(node, new CompileTimeConstantError(kind, const []));
1023 } 1053 }
1024 } 1054 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698