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

Unified Diff: frog/value.dart

Issue 9121025: cleanup to Value - fix for StringEscapesTest (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 11 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
Index: frog/value.dart
diff --git a/frog/value.dart b/frog/value.dart
index 7febe6af4ebf0c2b8c27c80c137463dc069a3baa..f1943b504d694951992372473adec98408487424 100644
--- a/frog/value.dart
+++ b/frog/value.dart
@@ -506,8 +506,70 @@ function \$assert_${toType.name}(x) {
return _resolveMember(context, 'noSuchMethod', node).invoke(
context, node, this, new Arguments(null, noSuchArgs));
}
+
+
+ static Value fromBool(bool value, SourceSpan span) {
+ return new EvaluatedValue(world.nonNullBool, value, value.toString(),
+ span);
+ }
+
+ static Value fromInt(int value, SourceSpan span) {
+ final strValue = value.toString();
+ assert(strValue.indexOf('.') == -1);
+ return new EvaluatedValue(world.numType, value, strValue, span);
+ }
+
+ static Value fromDouble(double value, SourceSpan span) {
+ var strValue = value.toString();
+ // Ensure that string version looks different from int
+ if (strValue.indexOf('.') == -1 && strValue.indexOf('e') == -1) {
+ strValue = strValue + '.0';
+ }
+ return new EvaluatedValue(world.numType, value, strValue, span);
+ }
+
+ static Value fromString(String value, SourceSpan span) {
+ // TODO(jimhug): This could be much more efficient
Jennifer Messerly 2012/01/09 20:16:26 one thing that might help: we could change StringB
jimhug 2012/01/09 21:19:05 Good thought. Let me know what you learn! On 2012
+ StringBuffer buf = new StringBuffer();
+ buf.add('"');
+ for (int i=0; i < value.length; i++) {
+ var ch = value.charCodeAt(i);
+ switch (ch) {
+ case 9/*'\t'*/: buf.add(@'\t'); break;
+ case 10/*'\n'*/: buf.add(@'\n'); break;
+ case 13/*'\r'*/: buf.add(@'\r'); break;
+ case 34/*"*/: buf.add(@'\"'); break;
+ case 92/*\*/: buf.add(@'\\'); break;
+ default:
+ if (ch >= 32 && ch <= 126) {
+ buf.add(value[i]);
+ } else {
+ final hex = ch.toRadixString(16);
+ switch (hex.length) {
+ case 1: buf.add(@'\x0'); buf.add(hex); break;
+ case 2: buf.add(@'\x'); buf.add(hex); break;
+ case 3: buf.add(@'\u0'); buf.add(hex); break;
+ case 4: buf.add(@'\u'); buf.add(hex); break;
+ default:
+ world.internalError(
+ 'unicode values greater than 2 bytes not implemented');
+ break;
+ }
+ }
+ break;
+ }
+ }
+ buf.add('"');
+
+ return new EvaluatedValue(world.stringType, value, buf.toString(), span);
+ }
+
+ static Value fromNull(SourceSpan span) {
+ return new EvaluatedValue(world.varType, null, 'null', span);
+ }
}
+
// TODO(jmesserly): the subtypes of Value require a lot of type checks and
// downcasts to use; can we make that cleaner? (search for ".dynamic")
@@ -537,8 +599,7 @@ class EvaluatedValue extends Value {
: super(type, code, span, false);
static String codeWithComments(String canonicalCode, SourceSpan span) {
- return (span != null && span.text != canonicalCode)
- ? '$canonicalCode/*${_escapeForComment(span.text)}*/' : canonicalCode;
+ return canonicalCode;
Jennifer Messerly 2012/01/09 20:16:26 nice simplification
}
}
« frog/utils.dart ('K') | « frog/utils.dart ('k') | tests/language/language.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698