Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pretty print to object mod #732

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading