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;
|
capacity *= 2;
|
||||||
capacityField->value.data.intVal = capacity;
|
capacityField->value.data.intVal = capacity;
|
||||||
|
|
||||||
uint64_t newSize = sizeof(GroundValue) * capacity;
|
int64_t newSize = sizeof(GroundValue) * capacity;
|
||||||
memSizeField->value.data.intVal = newSize;
|
memSizeField->value.data.intVal = newSize;
|
||||||
|
|
||||||
items = realloc(items, newSize);
|
items = realloc(items, newSize);
|
||||||
@@ -87,6 +87,18 @@ GroundValue listStructAt(GroundScope* scope, List args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GroundValue listStructToString(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");
|
return groundCreateValue(STRING, "TODO");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,7 +233,6 @@ GroundValue listStructDelete(GroundScope* scope, List args) {
|
|||||||
}
|
}
|
||||||
int64_t capacity = capacityField->value.data.intVal;
|
int64_t capacity = capacityField->value.data.intVal;
|
||||||
|
|
||||||
printf("size = %ld, index = %ld\n", sizeField->value.data.intVal, index);
|
|
||||||
if (sizeField->value.data.intVal < index) {
|
if (sizeField->value.data.intVal < index) {
|
||||||
char buffer[512];
|
char buffer[512];
|
||||||
sprintf(buffer, "Attempt to delete element at index %ld when list is of size %ld", index, sizeField->value.data.intVal);
|
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) {
|
GroundValue destroyListStruct(GroundScope* scope, List args) {
|
||||||
|
printf("destroying\n");
|
||||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||||
if (ptrField == NULL) {
|
if (ptrField == NULL) {
|
||||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||||
@@ -487,6 +499,37 @@ GroundValue destroyListStruct(GroundScope* scope, List args) {
|
|||||||
return groundCreateValue(NONE);
|
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) {
|
void initLists(GroundScope* scope) {
|
||||||
listStruct = groundCreateStruct();
|
listStruct = groundCreateStruct();
|
||||||
groundAddFieldToStruct(&listStruct, "size", groundCreateValue(INT, 0)); // number of elements
|
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, "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, "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, "destructor", destroyListStruct, ANY,0);
|
||||||
|
groundAddFunctionToStruct(&listStruct, "duplicator", duplicateListStruct, CUSTOM, 1, CUSTOM, "self");
|
||||||
groundAddNativeFunction(scope, "newList", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
|
groundAddNativeFunction(scope, "newList", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
|
||||||
groundAddNativeFunction(scope, "List_SOLS_CONSTRUCTOR", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
|
groundAddNativeFunction(scope, "List_SOLS_CONSTRUCTOR", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
|
||||||
|
groundAddNativeFunction(scope, "string_SOLS_AS", listStructToString, STRING, 0);
|
||||||
}
|
}
|
||||||
@@ -64,16 +64,18 @@ GroundValue stringSubstring(GroundScope* scope, List args) {
|
|||||||
ERROR("End can't be less than start when getting substring!", "EndBeforeStart");
|
ERROR("End can't be less than start when getting substring!", "EndBeforeStart");
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t inputLen = strlen(string);
|
size_t inputLen = strlen(string);
|
||||||
|
|
||||||
if (start >= inputLen)
|
if (start >= inputLen) {
|
||||||
ERROR("Start is outside string!", "OutOfBounds");
|
ERROR("Start is outside string!", "OutOfBounds");
|
||||||
else if (end >= inputLen || end < 0)
|
} else if (end >= inputLen || end < 0) {
|
||||||
ERROR("End is outside string!", "OutOfBounds");
|
ERROR("End is outside string!", "OutOfBounds");
|
||||||
|
}
|
||||||
|
|
||||||
char* buffer = malloc(inputLen + 1);
|
char* buffer = malloc(inputLen + 1);
|
||||||
if (!buffer)
|
if (!buffer) {
|
||||||
ERROR("Failed to allocate memory while getting substring of string!", "AllocFail");
|
ERROR("Failed to allocate memory while getting substring of string!", "AllocFail");
|
||||||
|
}
|
||||||
|
|
||||||
// string_Substring("Hello!", 1, 3) -> "ell"
|
// string_Substring("Hello!", 1, 3) -> "ell"
|
||||||
// amount = 3 - 1 + 1 = 3
|
// amount = 3 - 1 + 1 = 3
|
||||||
@@ -106,8 +108,9 @@ GroundValue stringFind(GroundScope* scope, List args) {
|
|||||||
char* needle = args.values[1].data.stringVal;
|
char* needle = args.values[1].data.stringVal;
|
||||||
|
|
||||||
char* result = strstr(haystack, needle);
|
char* result = strstr(haystack, needle);
|
||||||
if (!result)
|
if (!result) {
|
||||||
return groundCreateValue(INT, (int64_t)-1);
|
return groundCreateValue(INT, (int64_t)-1);
|
||||||
|
}
|
||||||
|
|
||||||
return groundCreateValue(INT, result - haystack);
|
return groundCreateValue(INT, result - haystack);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user