diff --git a/docs/docs/standard-lib/object.md b/docs/docs/standard-lib/object.md index 3c2b76e47..faa6f3869 100644 --- a/docs/docs/standard-lib/object.md +++ b/docs/docs/standard-lib/object.md @@ -61,3 +61,27 @@ This method will return a string of the object's hash value. ```cs Object.hash("Dictu"); ``` + +### Object.prettyPrint(Value, Number: indent -> Optional) + +This method will output to stdout a string representation of the given value. + +**NOTE** Strings, dicts, lists, numbers, booleans, and nil are valid values for pretty printing at this time. + +```cs +Object.prettyPrint([1, 2, 3]); + +// Output +'[1, 2, 3]' +``` + +```cs +Object.prettyPrint({"a": 1}, 4); + +// Output +' +{ + "a": 1 +} +' +``` diff --git a/src/optionals/json.c b/src/optionals/json.c index a711e7dbb..63c0691bc 100644 --- a/src/optionals/json.c +++ b/src/optionals/json.c @@ -171,7 +171,7 @@ json_value* stringifyJson(DictuVM *vm, Value value) { return NULL; } -static Value stringify(DictuVM *vm, int argCount, Value *args) { +Value stringify(DictuVM *vm, int argCount, Value *args) { if (argCount != 1 && argCount != 2) { runtimeError(vm, "stringify() takes 1 or 2 arguments (%d given).", argCount); return EMPTY_VAL; diff --git a/src/optionals/json.h b/src/optionals/json.h index 02697a793..0b9b8f328 100644 --- a/src/optionals/json.h +++ b/src/optionals/json.h @@ -7,5 +7,6 @@ #include "../vm/vm.h" Value createJSONModule(DictuVM *vm); +Value stringify(DictuVM *vm, int argCount, Value *args); #endif //dictu_json_h diff --git a/src/optionals/object/object.c b/src/optionals/object/object.c index ea5791eb1..ff5553d39 100644 --- a/src/optionals/object/object.c +++ b/src/optionals/object/object.c @@ -59,6 +59,25 @@ static Value objectHash(DictuVM *vm, int argCount, Value *args) { return OBJ_VAL(copyString(vm, (char *)str, 21)); } +static Value objectPrettyPrint(DictuVM *vm, int argCount, Value *args) { + if (argCount != 1 && argCount != 2) { + runtimeError(vm, "prettyPrint() takes 1 or arguments (%d given)", argCount); + return EMPTY_VAL; + } + + Value out = stringify(vm, argCount, args); + ObjResult *res = AS_RESULT(out); + if (res->status == ERR) { + runtimeError(vm, AS_CSTRING(res->value)); + return EMPTY_VAL; + } + + printValue(res->value); + printf("\n"); + + return NIL_VAL; +} + Value createObjectModule(DictuVM *vm) { ObjClosure *closure = compileModuleToClosure(vm, "Object", DICTU_OBJECT_SOURCE); @@ -75,6 +94,8 @@ Value createObjectModule(DictuVM *vm) { defineNative(vm, &closure->function->module->values, "getClassRef", objectGetClassRef); defineNative(vm, &closure->function->module->values, "hash", objectHash); + defineNative(vm, &closure->function->module->values, "prettyPrint", objectPrettyPrint); + pop(vm); return OBJ_VAL(closure); diff --git a/src/optionals/object/object.h b/src/optionals/object/object.h index c8384cf80..3d66276cb 100644 --- a/src/optionals/object/object.h +++ b/src/optionals/object/object.h @@ -5,6 +5,7 @@ #include "../optionals.h" #include "../../vm/vm.h" +#include "../json.h" Value createObjectModule(DictuVM *vm);