Skip to content

Commit

Permalink
Merge pull request #732 from briandowns/feature/pretty_print
Browse files Browse the repository at this point in the history
add pretty print to object mod
  • Loading branch information
Jason2605 authored Jan 30, 2024
2 parents aef7bc6 + 83cb39f commit af6a577
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
24 changes: 24 additions & 0 deletions docs/docs/standard-lib/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
'
```
2 changes: 1 addition & 1 deletion src/optionals/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/optionals/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
#include "../vm/vm.h"

Value createJSONModule(DictuVM *vm);
Value stringify(DictuVM *vm, int argCount, Value *args);

#endif //dictu_json_h
21 changes: 21 additions & 0 deletions src/optionals/object/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/optionals/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "../optionals.h"
#include "../../vm/vm.h"
#include "../json.h"

Value createObjectModule(DictuVM *vm);

Expand Down

0 comments on commit af6a577

Please sign in to comment.