From 205f995b648e96b5b4f4df603b21439b564a4e76 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Wed, 24 Jan 2024 14:52:56 -0700 Subject: [PATCH 1/5] add pretty print to object mod Signed-off-by: Brian Downs --- src/optionals/json.c | 2 +- src/optionals/json.h | 1 + src/optionals/object/object.c | 16 ++++++++++++++++ src/optionals/object/object.h | 1 + tests/object/import.du | 1 + tests/object/prettyPrint.du | 30 ++++++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/object/prettyPrint.du 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..471a1923f 100644 --- a/src/optionals/object/object.c +++ b/src/optionals/object/object.c @@ -59,7 +59,21 @@ 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; + } + + return OBJ_VAL(stringify(vm, argCount, args)); +} + Value createObjectModule(DictuVM *vm) { + ObjString *name = copyString(vm, "Object", 6); + push(vm, OBJ_VAL(name)); + ObjModule *module = newModule(vm, name); + push(vm, OBJ_VAL(module)); + ObjClosure *closure = compileModuleToClosure(vm, "Object", DICTU_OBJECT_SOURCE); if (closure == NULL) { @@ -75,6 +89,8 @@ Value createObjectModule(DictuVM *vm) { defineNative(vm, &closure->function->module->values, "getClassRef", objectGetClassRef); defineNative(vm, &closure->function->module->values, "hash", objectHash); + defineNative(vm, &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); diff --git a/tests/object/import.du b/tests/object/import.du index 3780daa58..7b2f11606 100644 --- a/tests/object/import.du +++ b/tests/object/import.du @@ -6,3 +6,4 @@ import "createFrom.du"; import "hash.du"; +import "prettyPrint"; diff --git a/tests/object/prettyPrint.du b/tests/object/prettyPrint.du new file mode 100644 index 000000000..9f999459a --- /dev/null +++ b/tests/object/prettyPrint.du @@ -0,0 +1,30 @@ +/** + * prettyPrint.du + * + * Testing the Object.prettyPrint method + */ +from UnitTest import UnitTest; + +import Object; + +class Test {} + +class TestObjectPrettyPrint < UnitTest { + testObjectPrettyPrint() { + this.assertEquals(Object.prettyPrint(1).unwrap(), "1"); + this.assertEquals(Object.prettyPrint("1").unwrap(), '"1"'); + this.assertEquals(Object.prettyPrint([1, 2, 3]).unwrap(), '[1, 2, 3]'); + this.assertEquals(Object.prettyPrint({"a": 1}).unwrap(), '{"a": 1}'); + this.assertEquals(Object.prettyPrint(nil).unwrap(), 'null'); + this.assertEquals(Object.prettyPrint(true).unwrap(), 'true'); + this.assertEquals(Object.prettyPrint(false).unwrap(), 'false'); + this.assertError(Object.prettyPrint(Test())); + } + + testObjectPrettyPrintWithIndent() { + this.assertEquals(Object.prettyPrint([1, 2, 3], 2).unwrap(), '[\n 1,\n 2,\n 3\n]'); + this.assertEquals(Object.prettyPrint({"a": 1}, 2).unwrap(), '{\n "a": 1\n}'); + } +} + +TestObjectPrettyPrint().run(); From b38fef9160cf750dd3b63e7e2a094b196e720027 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Wed, 24 Jan 2024 16:33:42 -0700 Subject: [PATCH 2/5] add docs, fix test typo Signed-off-by: Brian Downs --- docs/docs/standard-lib/object.md | 24 ++++++++++++++++++++++++ tests/object/import.du | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/docs/standard-lib/object.md b/docs/docs/standard-lib/object.md index dc5cbaf9c..fd0441206 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, indent -> Optional) -> Result\ + +This method will return a string representation of the given value. + +**NOTE** Strings, dicts, lists, numbers, and nil are valid values for pretty printing at this time. + +```cs +Object.prettyPrint([1, 2, 3]).unwrap(); + +// Output +'[1, 2, 3]' +``` + +```cs +Object.prettyPrint({"a": 1}, 4).unwrap() + +// Output +' +{ + "a": 1 +} +' +``` diff --git a/tests/object/import.du b/tests/object/import.du index 7b2f11606..30c20717d 100644 --- a/tests/object/import.du +++ b/tests/object/import.du @@ -6,4 +6,4 @@ import "createFrom.du"; import "hash.du"; -import "prettyPrint"; +import "prettyPrint.du"; From ac680bc1ec1461728414b0f33b7d0ab4591ba2cc Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sun, 28 Jan 2024 22:53:07 -0700 Subject: [PATCH 3/5] resolve pr comments Signed-off-by: Brian Downs --- docs/docs/standard-lib/object.md | 6 +++--- src/optionals/object/object.c | 7 +------ 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/docs/docs/standard-lib/object.md b/docs/docs/standard-lib/object.md index fd0441206..c1d754f97 100644 --- a/docs/docs/standard-lib/object.md +++ b/docs/docs/standard-lib/object.md @@ -62,11 +62,11 @@ This method will return a string of the object's hash value. Object.hash("Dictu"); ``` -### Object.prettyPrint(Value, indent -> Optional) -> Result\ +### Object.prettyPrint(Value, Number: indent -> Optional) -> Result\ This method will return a string representation of the given value. -**NOTE** Strings, dicts, lists, numbers, and nil are valid values for pretty printing at this time. +**NOTE** Strings, dicts, lists, numbers, booleans, and nil are valid values for pretty printing at this time. ```cs Object.prettyPrint([1, 2, 3]).unwrap(); @@ -76,7 +76,7 @@ Object.prettyPrint([1, 2, 3]).unwrap(); ``` ```cs -Object.prettyPrint({"a": 1}, 4).unwrap() +Object.prettyPrint({"a": 1}, 4).unwrap(); // Output ' diff --git a/src/optionals/object/object.c b/src/optionals/object/object.c index 471a1923f..17f5b5865 100644 --- a/src/optionals/object/object.c +++ b/src/optionals/object/object.c @@ -69,11 +69,6 @@ static Value objectPrettyPrint(DictuVM *vm, int argCount, Value *args) { } Value createObjectModule(DictuVM *vm) { - ObjString *name = copyString(vm, "Object", 6); - push(vm, OBJ_VAL(name)); - ObjModule *module = newModule(vm, name); - push(vm, OBJ_VAL(module)); - ObjClosure *closure = compileModuleToClosure(vm, "Object", DICTU_OBJECT_SOURCE); if (closure == NULL) { @@ -89,7 +84,7 @@ Value createObjectModule(DictuVM *vm) { defineNative(vm, &closure->function->module->values, "getClassRef", objectGetClassRef); defineNative(vm, &closure->function->module->values, "hash", objectHash); - defineNative(vm, &module->values, "prettyPrint", objectPrettyPrint); + defineNative(vm, &closure->function->module->values, "prettyPrint", objectPrettyPrint); pop(vm); From 7c15a8ab082d194b2e2d98e24240c696e98a39df Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 29 Jan 2024 16:22:58 -0700 Subject: [PATCH 4/5] update prettyPrint implementation Signed-off-by: Brian Downs --- src/optionals/object/object.c | 12 +++++++++++- tests/object/import.du | 1 - tests/object/prettyPrint.du | 30 ------------------------------ 3 files changed, 11 insertions(+), 32 deletions(-) delete mode 100644 tests/object/prettyPrint.du diff --git a/src/optionals/object/object.c b/src/optionals/object/object.c index 17f5b5865..ff5553d39 100644 --- a/src/optionals/object/object.c +++ b/src/optionals/object/object.c @@ -65,7 +65,17 @@ static Value objectPrettyPrint(DictuVM *vm, int argCount, Value *args) { return EMPTY_VAL; } - return OBJ_VAL(stringify(vm, argCount, args)); + 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) { diff --git a/tests/object/import.du b/tests/object/import.du index 30c20717d..3780daa58 100644 --- a/tests/object/import.du +++ b/tests/object/import.du @@ -6,4 +6,3 @@ import "createFrom.du"; import "hash.du"; -import "prettyPrint.du"; diff --git a/tests/object/prettyPrint.du b/tests/object/prettyPrint.du deleted file mode 100644 index 9f999459a..000000000 --- a/tests/object/prettyPrint.du +++ /dev/null @@ -1,30 +0,0 @@ -/** - * prettyPrint.du - * - * Testing the Object.prettyPrint method - */ -from UnitTest import UnitTest; - -import Object; - -class Test {} - -class TestObjectPrettyPrint < UnitTest { - testObjectPrettyPrint() { - this.assertEquals(Object.prettyPrint(1).unwrap(), "1"); - this.assertEquals(Object.prettyPrint("1").unwrap(), '"1"'); - this.assertEquals(Object.prettyPrint([1, 2, 3]).unwrap(), '[1, 2, 3]'); - this.assertEquals(Object.prettyPrint({"a": 1}).unwrap(), '{"a": 1}'); - this.assertEquals(Object.prettyPrint(nil).unwrap(), 'null'); - this.assertEquals(Object.prettyPrint(true).unwrap(), 'true'); - this.assertEquals(Object.prettyPrint(false).unwrap(), 'false'); - this.assertError(Object.prettyPrint(Test())); - } - - testObjectPrettyPrintWithIndent() { - this.assertEquals(Object.prettyPrint([1, 2, 3], 2).unwrap(), '[\n 1,\n 2,\n 3\n]'); - this.assertEquals(Object.prettyPrint({"a": 1}, 2).unwrap(), '{\n "a": 1\n}'); - } -} - -TestObjectPrettyPrint().run(); From b99930acffa043cb680ec5e9d59aaa81cc0fbf29 Mon Sep 17 00:00:00 2001 From: Jason_000 Date: Tue, 30 Jan 2024 09:47:07 +0000 Subject: [PATCH 5/5] Update documentation for prettyPrint --- docs/docs/standard-lib/object.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/docs/standard-lib/object.md b/docs/docs/standard-lib/object.md index c1d754f97..cf3ced1f8 100644 --- a/docs/docs/standard-lib/object.md +++ b/docs/docs/standard-lib/object.md @@ -62,21 +62,21 @@ This method will return a string of the object's hash value. Object.hash("Dictu"); ``` -### Object.prettyPrint(Value, Number: indent -> Optional) -> Result\ +### Object.prettyPrint(Value, Number: indent -> Optional) -This method will return a string representation of the given value. +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]).unwrap(); +Object.prettyPrint([1, 2, 3]); // Output '[1, 2, 3]' ``` ```cs -Object.prettyPrint({"a": 1}, 4).unwrap(); +Object.prettyPrint({"a": 1}, 4); // Output '