forked from ground/ground
updated fileio and collections libs
rewrote fileio lib to use structs and methods. i also updated collections lib to fix bugs and be more consistent
This commit is contained in:
@@ -25,9 +25,9 @@ GroundValue appendToListStruct(GroundScope* scope, List args) {
|
||||
ERROR("A field called \"memSize\" was not found", "FieldNotFound");
|
||||
}
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
@@ -48,7 +48,7 @@ GroundValue appendToListStruct(GroundScope* scope, List args) {
|
||||
|
||||
items = realloc(items, newSize);
|
||||
if (items == NULL) {
|
||||
ERROR("Failed to allocate memory when increasing list size!", "MemoryAllocationFailed");
|
||||
ERROR("Failed to allocate memory when increasing list size!", "AllocFail");
|
||||
}
|
||||
|
||||
ptrField->value.data.intVal = (int64_t)items;
|
||||
@@ -61,7 +61,7 @@ GroundValue appendToListStruct(GroundScope* scope, List args) {
|
||||
|
||||
GroundValue listStructAt(GroundScope* scope, List args) {
|
||||
|
||||
uint64_t index = args.values[0].data.intVal;
|
||||
int64_t index = args.values[0].data.intVal;
|
||||
if (index < 0) {
|
||||
ERROR("Attempt to access list at negative index", "OutOfBounds");
|
||||
}
|
||||
@@ -77,9 +77,9 @@ GroundValue listStructAt(GroundScope* scope, List args) {
|
||||
ERROR(buffer, "OutOfBounds");
|
||||
}
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
@@ -102,9 +102,9 @@ GroundValue clearListStruct(GroundScope* scope, List args) {
|
||||
ERROR("A field called \"memSize\" was not found", "FieldNotFound");
|
||||
}
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
|
||||
GroundVariable* capacityField = groundFindVariable(scope, "capacity");
|
||||
@@ -114,7 +114,7 @@ GroundValue clearListStruct(GroundScope* scope, List args) {
|
||||
|
||||
GroundValue* newBuffer = calloc(STARTING_ELEMENTS, sizeof(GroundValue));
|
||||
if (newBuffer == NULL) {
|
||||
ERROR("Failed to allocate memory when clearing list!", "MemoryAllocationFailed");
|
||||
ERROR("Failed to allocate memory when clearing list!", "AllocFail");
|
||||
}
|
||||
|
||||
sizeField->value.data.intVal = 0;
|
||||
@@ -127,7 +127,7 @@ GroundValue clearListStruct(GroundScope* scope, List args) {
|
||||
|
||||
GroundValue insertIntoListStruct(GroundScope* scope, List args) {
|
||||
GroundValue value = args.values[0];
|
||||
uint64_t index = args.values[1].data.intVal;
|
||||
int64_t index = args.values[1].data.intVal;
|
||||
if (index < 0) {
|
||||
ERROR("Attempt to insert element into list at negative index", "OutOfBounds");
|
||||
}
|
||||
@@ -143,9 +143,9 @@ GroundValue insertIntoListStruct(GroundScope* scope, List args) {
|
||||
ERROR("A field called \"memSize\" was not found", "FieldNotFound");
|
||||
}
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
@@ -172,7 +172,7 @@ GroundValue insertIntoListStruct(GroundScope* scope, List args) {
|
||||
// allocate new buffer
|
||||
GroundValue* newBuffer = calloc(capacity, sizeof(GroundValue));
|
||||
if (newBuffer == NULL) {
|
||||
ERROR("Failed to allocate memory when increasing list size!", "MemoryAllocationFailed");
|
||||
ERROR("Failed to allocate memory when increasing list size!", "AllocFail");
|
||||
}
|
||||
ptrField->value.data.intVal = (int64_t)newBuffer;
|
||||
memSizeField->value.data.intVal = sizeof(GroundValue) * capacity;
|
||||
@@ -192,7 +192,7 @@ GroundValue insertIntoListStruct(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
GroundValue listStructDelete(GroundScope* scope, List args) {
|
||||
uint64_t index = args.values[0].data.intVal;
|
||||
int64_t index = args.values[0].data.intVal;
|
||||
if (index < 0) {
|
||||
ERROR("Attempt to remove element into list at negative index", "OutOfBounds");
|
||||
}
|
||||
@@ -206,12 +206,12 @@ GroundValue listStructDelete(GroundScope* scope, List args) {
|
||||
|
||||
GroundVariable* memSizeField = groundFindVariable(scope, "memSize");
|
||||
if (memSizeField == NULL) {
|
||||
ERROR("A field called \"memSize\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"memSizvoid initLists(GroundScope* scope);e\" was not found", "FieldNotFound");
|
||||
}
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
@@ -242,7 +242,7 @@ GroundValue listStructDelete(GroundScope* scope, List args) {
|
||||
// allocate new buffer
|
||||
GroundValue* newBuffer = calloc(capacity, sizeof(GroundValue));
|
||||
if (newBuffer == NULL) {
|
||||
ERROR("Failed to allocate memory when decreasing list size!", "MemoryAllocationFailed");
|
||||
ERROR("Failed to allocate memory when decreasing list size!", "AllocFail");
|
||||
}
|
||||
ptrField->value.data.intVal = (int64_t)newBuffer;
|
||||
memSizeField->value.data.intVal = sizeof(GroundValue) * capacity;
|
||||
@@ -264,7 +264,7 @@ GroundValue listStructDelete(GroundScope* scope, List args) {
|
||||
|
||||
GroundValue listStructSet(GroundScope* scope, List args) {
|
||||
GroundValue value = args.values[0];
|
||||
uint64_t index = args.values[1].data.intVal;
|
||||
int64_t index = args.values[1].data.intVal;
|
||||
if (index < 0) {
|
||||
ERROR("Attempt to set element in list at negative index", "OutOfBounds");
|
||||
}
|
||||
@@ -275,9 +275,9 @@ GroundValue listStructSet(GroundScope* scope, List args) {
|
||||
}
|
||||
int64_t size = sizeField->value.data.intVal;
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
@@ -338,13 +338,13 @@ GroundValue listStructContains(GroundScope* scope, List args) {
|
||||
}
|
||||
int64_t size = sizeField->value.data.intVal;
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
for (uint64_t i = 0; i < size; i++) {
|
||||
for (int64_t i = 0; i < size; i++) {
|
||||
if (areGroundValuesEqual(items[i], targetValue)) {
|
||||
return groundCreateValue(BOOL, true);
|
||||
}
|
||||
@@ -366,15 +366,15 @@ GroundValue reverseListStruct(GroundScope* scope, List args) {
|
||||
}
|
||||
int64_t capacity = capacityField->value.data.intVal;
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
GroundValue* newBuffer = calloc(capacity, sizeof(GroundValue));
|
||||
if (newBuffer == NULL) {
|
||||
ERROR("Failed to allocate memory when reversing list!", "MemoryAllocationFailed");
|
||||
ERROR("Failed to allocate memory when reversing list!", "AllocFail");
|
||||
}
|
||||
|
||||
int z = 0;
|
||||
@@ -397,13 +397,13 @@ GroundValue findListStruct(GroundScope* scope, List args) {
|
||||
}
|
||||
int64_t size = sizeField->value.data.intVal;
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
for (uint64_t i = 0; i < size; i++) {
|
||||
for (int64_t i = 0; i < size; i++) {
|
||||
if (areGroundValuesEqual(items[i], targetValue)) {
|
||||
return groundCreateValue(INT, i);
|
||||
}
|
||||
@@ -421,9 +421,9 @@ GroundValue reserveListStruct(GroundScope* scope, List args) {
|
||||
}
|
||||
int64_t capacity = capacityField->value.data.intVal;
|
||||
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "private_ptr");
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"private_ptr\" was not found", "FieldNotFound");
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
|
||||
|
||||
@@ -439,7 +439,7 @@ GroundValue reserveListStruct(GroundScope* scope, List args) {
|
||||
capacityField->value.data.intVal = amount;
|
||||
items = realloc(items, sizeof(GroundValue) * amount);
|
||||
if (items == NULL) {
|
||||
ERROR("Failed to allocate memory when reserving list space!", "MemoryAllocationFailed");
|
||||
ERROR("Failed to allocate memory when reserving list space!", "AllocFail");
|
||||
}
|
||||
|
||||
memSizeField->value.data.intVal = sizeof(GroundValue) * amount;
|
||||
@@ -448,26 +448,32 @@ GroundValue reserveListStruct(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
GroundValue listStructConstructor(GroundScope* scope, List args) {
|
||||
GroundValue value = groundCreateValue(CUSTOM, &listStruct);
|
||||
|
||||
int64_t startingCapacity = args.values[0].data.intVal;
|
||||
if (startingCapacity < 1) {
|
||||
ERROR("List can't be less than 1 element in capacity on initialization!", "OutOfBounds");
|
||||
}
|
||||
|
||||
return createList(startingCapacity);
|
||||
}
|
||||
|
||||
GroundValue createList(int64_t initialCapacity) {
|
||||
GroundValue value = groundCreateValue(CUSTOM, &listStruct);
|
||||
|
||||
|
||||
|
||||
GroundObjectField *sizeField = groundFindField(*value.data.customVal, "size");
|
||||
GroundObjectField *capacityField = groundFindField(*value.data.customVal, "capacity");
|
||||
GroundObjectField *memSizeField = groundFindField(*value.data.customVal, "memSize");
|
||||
GroundObjectField *ptrField = groundFindField(*value.data.customVal, "private_ptr");
|
||||
GroundObjectField *ptrField = groundFindField(*value.data.customVal, "ptr");
|
||||
|
||||
GroundValue* items = calloc(STARTING_ELEMENTS, sizeof(GroundValue));
|
||||
if (items == NULL) {
|
||||
ERROR("Failed to allocate memory while creating list!", "MemoryAllocationFailed");
|
||||
ERROR("Failed to allocate memory while creating list!", "AllocFail");
|
||||
}
|
||||
|
||||
sizeField->value.data.intVal = 0;
|
||||
capacityField->value.data.intVal = startingCapacity;
|
||||
memSizeField->value.data.intVal = sizeof(GroundValue) * startingCapacity;
|
||||
capacityField->value.data.intVal = initialCapacity;
|
||||
memSizeField->value.data.intVal = sizeof(GroundValue) * initialCapacity;
|
||||
ptrField->value.data.intVal = (int64_t)items;
|
||||
|
||||
value.type = CUSTOM;
|
||||
@@ -480,7 +486,7 @@ void initLists(GroundScope* scope) {
|
||||
groundAddFieldToStruct(&listStruct, "size", groundCreateValue(INT, 0)); // number of elements
|
||||
groundAddFieldToStruct(&listStruct, "memSize", groundCreateValue(INT, sizeof(GroundValue) * STARTING_ELEMENTS)); // number of bytes allocated
|
||||
groundAddFieldToStruct(&listStruct, "capacity", groundCreateValue(INT, STARTING_ELEMENTS)); // number of elements that can fit in the currently allocated space
|
||||
groundAddFieldToStruct(&listStruct, "private_ptr", groundCreateValue(INT, 0)); // pointer to internal list struct
|
||||
groundAddFieldToStruct(&listStruct, "ptr", groundCreateValue(INT, 0)); // pointer to internal list struct
|
||||
|
||||
groundAddFunctionToStruct(&listStruct, "init", listStructConstructor, INT, 1, INT, "startingCapacity"); // init struct (ground constructor)
|
||||
groundAddFunctionToStruct(&listStruct, "append", appendToListStruct, INT, 1, ANY, "value"); // append item to end of list
|
||||
|
||||
Reference in New Issue
Block a user