Permalink
Browse files

Use new Value->Type() api for pretty printer

  • Loading branch information...
1 parent b293400 commit 8f9ed29155638d06207247deb11cf4a59abb2eb4 @creationix creationix committed Mar 21, 2012
Showing with 79 additions and 70 deletions.
  1. +79 −70 src/cio.cc
View
@@ -20,89 +20,98 @@ using namespace candor;
// Print a value to a fd
static void printValue(FILE* fd, Value* value, bool shallow) {
- if (value->Is<String>()) {
- String* string = value->ToString();
- fprintf(fd, "\033[1;32m\"\033[0;32m%.*s\033[1;32m\"\033[0m", string->Length(), string->Value());
- return;
- }
- if (value->Is<Number>()) {
- String* string = value->ToString();
- fprintf(fd, "\033[0;34m%.*s\033[0m", string->Length(), string->Value());
- return;
- }
- if (value->Is<Boolean>()) {
- String* string = value->ToString();
- fprintf(fd, "\033[0;33m%.*s\033[0m", string->Length(), string->Value());
- return;
- }
- if (value->Is<Function>()) {
- fprintf(fd, "\033[0;36mfunction: %p\033[0m", value);
- return;
- }
+ switch (value->Type()) {
- if (value->Is<Object>()) {
- if (shallow) {
- fprintf(fd, "\033[0;35mobject: %p\033[0m", value);
+ case Value::kString: {
+ String* string = value->ToString();
+ fprintf(fd, "\033[1;32m\"\033[0;32m%.*s\033[1;32m\"\033[0m", string->Length(), string->Value());
return;
}
- Object* obj = value->As<Object>();
- Array* keys = obj->Keys();
- int64_t length = keys->Length();
- int64_t i = 0;
- fprintf(fd, "\033[1;37m{\033[0m ");
- while (i < length) {
- Value* key = keys->Get(i);
- if (key->Is<String>()) {
- String* string = key->ToString();
- fprintf(fd, "%.*s", string->Length(), string->Value());
- } else {
- fprintf(fd, "[");
- printValue(fd, key, true);
- fprintf(fd, "]");
- }
- fprintf(fd, "\033[1;37m:\033[0m ");
- printValue(fd, obj->Get(key), true);
- if (i < length - 1) {
- fprintf(fd, "\033[1;37m,\033[0m ");
- }
- i++;
+ case Value::kNumber: {
+ String* string = value->ToString();
+ fprintf(fd, "\033[0;34m%.*s\033[0m", string->Length(), string->Value());
+ return;
}
- fprintf(fd, " \033[1;37m}\033[0m");
- return;
- }
- if (value->Is<Array>()) {
- if (shallow) {
- fprintf(fd, "\033[0;36marray: %p\033[0m", value);
+ case Value::kBoolean: {
+ String* string = value->ToString();
+ fprintf(fd, "\033[0;33m%.*s\033[0m", string->Length(), string->Value());
return;
}
- Array* array = value->As<Array>();
- int64_t length = array->Length();
- int64_t i = 0;
- fprintf(fd, "[ ");
- while (i < length) {
- printValue(fd, array->Get(i), true);
- if (i < length - 1) {
- fprintf(fd, ", ");
+
+ case Value::kFunction: {
+ fprintf(fd, "\033[0;36mfunction: %p\033[0m", value);
+ break;
+ }
+
+ case Value::kObject: {
+ if (shallow) {
+ fprintf(fd, "\033[0;35mobject: %p\033[0m", value);
+ return;
}
- i++;
+ Object* obj = value->As<Object>();
+ Array* keys = obj->Keys();
+ int64_t length = keys->Length();
+ int64_t i = 0;
+ fprintf(fd, "\033[1;37m{\033[0m ");
+ while (i < length) {
+ Value* key = keys->Get(i);
+ if (key->Is<String>()) {
+ String* string = key->ToString();
+ fprintf(fd, "%.*s", string->Length(), string->Value());
+ } else {
+ fprintf(fd, "[");
+ printValue(fd, key, true);
+ fprintf(fd, "]");
+ }
+
+ fprintf(fd, "\033[1;37m:\033[0m ");
+ printValue(fd, obj->Get(key), true);
+ if (i < length - 1) {
+ fprintf(fd, "\033[1;37m,\033[0m ");
+ }
+ i++;
+ }
+ fprintf(fd, " \033[1;37m}\033[0m");
+ return;
}
- fprintf(fd, " ]");
- return;
- }
- if (value->Is<CData>()) {
- fprintf(fd, "\033[0;31mcdata: %p\033[0m", value);
- return;
- }
+ case Value::kArray: {
+ if (shallow) {
+ fprintf(fd, "\033[0;36marray: %p\033[0m", value);
+ return;
+ }
+ Array* array = value->As<Array>();
+ int64_t length = array->Length();
+ int64_t i = 0;
+ fprintf(fd, "[ ");
+ while (i < length) {
+ printValue(fd, array->Get(i), true);
+ if (i < length - 1) {
+ fprintf(fd, ", ");
+ }
+ i++;
+ }
+ fprintf(fd, " ]");
+ return;
+ }
- if (value->Is<Nil>()) {
- fprintf(fd, "\033[1;30mnil\033[0m");
- return;
- }
+ case Value::kCData: {
+ fprintf(fd, "\033[0;31mcdata: %p\033[0m", value);
+ return;
+ }
+
+ case Value::kNil: {
+ fprintf(fd, "\033[1;30mnil\033[0m");
+ return;
+ }
- assert(false);
+ case Value::kNone: {
+ fprintf(fd, "\033[1,31mnone\033[0m");
+ return;
+ }
+ }
}
static Value* Print(uint32_t argc, Arguments& argv) {

0 comments on commit 8f9ed29

Please sign in to comment.