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 new Enum.values() method #672

Merged
merged 2 commits into from
Oct 6, 2023
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
14 changes: 14 additions & 0 deletions docs/docs/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,18 @@ enum HeterogeneousEnum {
b = "string",
c = def () => 10
}
```

### Enum.values()

To get all values within an Enum you can use the `.values()` method. This will return a dictionary.

```cs
enum Test {
a = 10, // 10
b, // 1
c // 2
}

print(Test.values()); // {"c": 2, "a": 10, "b": 1}
```
2 changes: 1 addition & 1 deletion src/vm/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1977,7 +1977,7 @@ static void enumDeclaration(Compiler *compiler) {

do {
if (check(compiler, TOKEN_RIGHT_BRACE)) {
error(compiler->parser, "Trailing comma in enum declaration");
break;
}

consume(compiler, TOKEN_IDENTIFIER, "Expect enum value identifier.");
Expand Down
28 changes: 28 additions & 0 deletions src/vm/datatypes/enums.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "enums.h"

static Value values(DictuVM *vm, int argCount, Value *args) {
if (argCount != 0) {
runtimeError(vm, "values() takes 0 arguments (%d given)", argCount);
return EMPTY_VAL;
}

ObjEnum *objEnum = AS_ENUM(args[0]);
ObjDict *dict = newDict(vm);
push(vm, OBJ_VAL(dict));

for (int i = 0; i < objEnum->values.capacityMask + 1; ++i) {
if (objEnum->values.entries[i].key == NULL) {
continue;
}

dictSet(vm, dict, OBJ_VAL(objEnum->values.entries[i].key), objEnum->values.entries[i].value);
}

pop(vm);

return OBJ_VAL(dict);
}

void declareEnumMethods(DictuVM *vm) {
defineNative(vm, &vm->enumMethods, "values", values);
}
9 changes: 9 additions & 0 deletions src/vm/datatypes/enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef dictu_api_enums_h
#define dictu_api_enums_h

#include "../common.h"
#include "../util.h"

void declareEnumMethods(DictuVM *vm);

#endif //dictu_api_enums_h
1 change: 1 addition & 0 deletions src/vm/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ void collectGarbage(DictuVM *vm) {
grayTable(vm, &vm->classMethods);
grayTable(vm, &vm->instanceMethods);
grayTable(vm, &vm->resultMethods);
grayTable(vm, &vm->enumMethods);
grayCompilerRoots(vm);
grayObject(vm, (Obj *) vm->initString);
grayObject(vm, (Obj *) vm->annotationString);
Expand Down
22 changes: 20 additions & 2 deletions src/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "datatypes/class.h"
#include "datatypes/instance.h"
#include "datatypes/result/result.h"
#include "datatypes/enums.h"
#include "natives.h"
#include "../optionals/optionals.h"

Expand Down Expand Up @@ -120,6 +121,7 @@ DictuVM *dictuInitVM(bool repl, int argc, char **argv) {
initTable(&vm->classMethods);
initTable(&vm->instanceMethods);
initTable(&vm->resultMethods);
initTable(&vm->enumMethods);

vm->frames = ALLOCATE(vm, CallFrame, vm->frameCapacity);
vm->initString = copyString(vm, "init", 4);
Expand All @@ -140,6 +142,7 @@ DictuVM *dictuInitVM(bool repl, int argc, char **argv) {
declareClassMethods(vm);
declareInstanceMethods(vm);
declareResultMethods(vm);
declareEnumMethods(vm);

if (vm->repl) {
vm->replVar = copyString(vm, "_", 1);
Expand Down Expand Up @@ -168,6 +171,7 @@ void dictuFreeVM(DictuVM *vm) {
freeTable(vm, &vm->classMethods);
freeTable(vm, &vm->instanceMethods);
freeTable(vm, &vm->resultMethods);
freeTable(vm, &vm->enumMethods);
FREE_ARRAY(vm, CallFrame, vm->frames, vm->frameCapacity);
vm->initString = NULL;
vm->replVar = NULL;
Expand Down Expand Up @@ -647,14 +651,28 @@ static bool invoke(DictuVM *vm, ObjString *name, int argCount, bool unpack) {
}

case OBJ_ENUM: {
Value value;
if (tableGet(&vm->enumMethods, name, &value)) {
if (IS_NATIVE(value)) {
return callNativeMethod(vm, value, argCount);
}

push(vm, peek(vm, 0));

for (int i = 2; i <= argCount + 1; i++) {
vm->stackTop[-i] = peek(vm, i);
}

return call(vm, AS_CLOSURE(value), argCount + 1);
}

ObjEnum *enumObj = AS_ENUM(receiver);

Value value;
if (tableGet(&enumObj->values, name, &value)) {
return callValue(vm, value, argCount, false);
}

runtimeError(vm, "'%s' enum has no value '%s'.", enumObj->name->chars, name->chars);
runtimeError(vm, "Enum has no method '%s'.", name->chars);
return false;
}

Expand Down
1 change: 1 addition & 0 deletions src/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct _vm {
Table classMethods;
Table instanceMethods;
Table resultMethods;
Table enumMethods;
ObjString *initString;
ObjString *annotationString;
ObjString *replVar;
Expand Down
30 changes: 30 additions & 0 deletions tests/enum/enum.du
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@ class TestEnums < UnitTest {
this.assertEquals(HeterogeneousEnum.c, func);
this.assertEquals(HeterogeneousEnum.c(), 10);
}

testGetValues() {
enum MyEnum {
a,
b,
c
}

const values = MyEnum.values();

this.assertEquals(values["a"], 0);
this.assertEquals(values["b"], 1);
this.assertEquals(values["c"], 2);
}

testGetValuesHeterogeneousEnum() {
const func = def () => 10;

enum HeterogeneousEnum {
a = 0,
b = "string",
c = func
}

const values = HeterogeneousEnum.values();

this.assertEquals(values["a"], 0);
this.assertEquals(values["b"], "string");
this.assertEquals(values["c"], func);
}
}

TestEnums().run();