forked from ground/ground
fixing bugs with lists and strings
This commit is contained in:
@@ -43,7 +43,7 @@ GroundValue appendToListStruct(GroundScope* scope, List args) {
|
||||
capacity *= 2;
|
||||
capacityField->value.data.intVal = capacity;
|
||||
|
||||
uint64_t newSize = sizeof(GroundValue) * capacity;
|
||||
int64_t newSize = sizeof(GroundValue) * capacity;
|
||||
memSizeField->value.data.intVal = newSize;
|
||||
|
||||
items = realloc(items, newSize);
|
||||
@@ -87,6 +87,18 @@ GroundValue listStructAt(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
GroundValue listStructToString(GroundScope* scope, List args) {
|
||||
GroundVariable* sizeField = groundFindVariable(scope, "size");
|
||||
if (sizeField == NULL) {
|
||||
ERROR("A field called \"size\" was not found", "FieldNotFound");
|
||||
}
|
||||
int64_t size = sizeField->value.data.intVal;
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
return groundCreateValue(STRING, "TODO");
|
||||
}
|
||||
|
||||
@@ -221,7 +233,6 @@ GroundValue listStructDelete(GroundScope* scope, List args) {
|
||||
}
|
||||
int64_t capacity = capacityField->value.data.intVal;
|
||||
|
||||
printf("size = %ld, index = %ld\n", sizeField->value.data.intVal, index);
|
||||
if (sizeField->value.data.intVal < index) {
|
||||
char buffer[512];
|
||||
sprintf(buffer, "Attempt to delete element at index %ld when list is of size %ld", index, sizeField->value.data.intVal);
|
||||
@@ -478,6 +489,7 @@ GroundValue listStructConstructor(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
GroundValue destroyListStruct(GroundScope* scope, List args) {
|
||||
printf("destroying\n");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
@@ -487,6 +499,37 @@ GroundValue destroyListStruct(GroundScope* scope, List args) {
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
GroundValue duplicateListStruct(GroundScope* scope, List args) {
|
||||
GroundObject* self = args.values[0].data.customVal;
|
||||
|
||||
GroundObjectField *sizeField = groundFindField(*self, "size");
|
||||
GroundObjectField *capacityField = groundFindField(*self, "capacity");
|
||||
GroundObjectField *memSizeField = groundFindField(*self, "memSize");
|
||||
GroundObjectField *ptrField = groundFindField(*self, "ptr");
|
||||
|
||||
GroundValue newSelf = groundCreateValue(CUSTOM, &listStruct);
|
||||
newSelf.type = CUSTOM;
|
||||
|
||||
GroundObjectField *newSizeField = groundFindField(*newSelf.data.customVal, "size");
|
||||
GroundObjectField *newCapacityField = groundFindField(*newSelf.data.customVal, "capacity");
|
||||
GroundObjectField *newMemSizeField = groundFindField(*newSelf.data.customVal, "memSize");
|
||||
GroundObjectField *newPtrField = groundFindField(*newSelf.data.customVal, "ptr");
|
||||
|
||||
groundAddValueToScope(scope, "size", sizeField->value);
|
||||
groundAddValueToScope(scope, "capacity", capacityField->value);
|
||||
groundAddValueToScope(scope, "memSize", memSizeField->value);
|
||||
|
||||
GroundValue* newPtr = calloc(newCapacityField->value.data.intVal, sizeof(GroundValue));
|
||||
if (!newPtr) {
|
||||
ERROR("Failed to allocate memory while calling copy constructor of List!", "AllocFail");
|
||||
}
|
||||
|
||||
memcpy(newPtr, (GroundValue*)ptrField->value.data.intVal, newMemSizeField->value.data.intVal);
|
||||
newPtrField->value.data.intVal = (int64_t)newPtr;
|
||||
|
||||
return newSelf;
|
||||
}
|
||||
|
||||
void initLists(GroundScope* scope) {
|
||||
listStruct = groundCreateStruct();
|
||||
groundAddFieldToStruct(&listStruct, "size", groundCreateValue(INT, 0)); // number of elements
|
||||
@@ -507,8 +550,9 @@ void initLists(GroundScope* scope) {
|
||||
groundAddFunctionToStruct(&listStruct, "find", findListStruct, INT, 1, ANY, "value"); // return index of value in list, if not found, returns -1
|
||||
groundAddFunctionToStruct(&listStruct, "reserve", reserveListStruct, BOOL, 1, INT, "amount"); // ensure list capacity >= amount. returns true if the list's capacity was expanded
|
||||
|
||||
|
||||
groundAddFunctionToStruct(&listStruct, "destructor", destroyListStruct, ANY,0);
|
||||
groundAddFunctionToStruct(&listStruct, "duplicator", duplicateListStruct, CUSTOM, 1, CUSTOM, "self");
|
||||
groundAddNativeFunction(scope, "newList", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
|
||||
groundAddNativeFunction(scope, "List_SOLS_CONSTRUCTOR", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
|
||||
groundAddNativeFunction(scope, "string_SOLS_AS", listStructToString, STRING, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user