added solstice support to the collections lib

This commit is contained in:
2026-04-12 09:47:27 +10:00
parent f6c59a61d3
commit aa0c71a47b
5 changed files with 48 additions and 11 deletions

View File

@@ -5,6 +5,6 @@
#include "hashmap.h"
void ground_init(GroundScope* scope) {
groundAddValueToScope(scope, "List", createListStruct());
groundAddValueToScope(scope, "Hashmap", createHashmapStruct());
initLists(scope);
initHashmaps(scope);
}

View File

@@ -6,6 +6,8 @@
#include <uthash.h>
#include <stdio.h>
GroundStruct hashmapStruct = {};
GroundValue hashmapStructSet(GroundScope* scope, List args) {
char* key = args.values[0].data.stringVal;
GroundValue value = args.values[1];
@@ -117,8 +119,14 @@ GroundValue hashmapStructRemoveIfPresent(GroundScope* scope, List args) {
return groundCreateValue(BOOL, true);
}
GroundValue createHashmapStruct() {
GroundStruct hashmapStruct = groundCreateStruct();
GroundValue hashmapStructConstructor(GroundScope* scope, List args) {
GroundValue value = groundCreateValue(CUSTOM, &hashmapStruct);
value.type = CUSTOM;
return value;
}
void initHashmaps(GroundScope* scope) {
hashmapStruct = groundCreateStruct();
groundAddFieldToStruct(&hashmapStruct, "private_ptr", groundCreateValue(INT, 0));
@@ -128,5 +136,6 @@ GroundValue createHashmapStruct() {
groundAddFunctionToStruct(&hashmapStruct, "remove", hashmapStructRemove, INT, 1, STRING, "key"); // remove a key from the hashmap, throws KeyNotFound if the key does not exist
groundAddFunctionToStruct(&hashmapStruct, "removeIfPresent", hashmapStructRemoveIfPresent, BOOL, 1, STRING, "key"); // remove a key from the hashmap, returns true if the key was removed or false if it didn't exist
return groundCreateValue(STRUCTVAL, hashmapStruct);
groundAddNativeFunction(scope, "newHashmap", hashmapStructConstructor, CUSTOM, 0);
groundAddNativeFunction(scope, "Hashmap_SOLS_CONSTRUCTOR", hashmapStructConstructor, CUSTOM, 0);
}

View File

@@ -11,6 +11,6 @@ typedef struct {
UT_hash_handle hh;
} HashmapItem;
GroundValue createHashmapStruct();
void initHashmaps(GroundScope* scope);
#endif

View File

@@ -8,6 +8,7 @@
#include <string.h>
const uint8_t STARTING_ELEMENTS = 2;
GroundStruct listStruct = {};
GroundValue appendToListStruct(GroundScope* scope, List args) {
@@ -446,16 +447,42 @@ GroundValue reserveListStruct(GroundScope* scope, List args) {
return groundCreateValue(BOOL, true);
}
GroundValue createListStruct() {
GroundStruct listStruct = groundCreateStruct();
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");
}
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");
GroundValue* items = calloc(STARTING_ELEMENTS, sizeof(GroundValue));
if (items == NULL) {
ERROR("Failed to allocate memory while creating list!", "MemoryAllocationFailed");
}
sizeField->value.data.intVal = 0;
capacityField->value.data.intVal = startingCapacity;
memSizeField->value.data.intVal = sizeof(GroundValue) * startingCapacity;
ptrField->value.data.intVal = (int64_t)items;
value.type = CUSTOM;
return value;
}
void initLists(GroundScope* scope) {
listStruct = groundCreateStruct();
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, items)); // pointer to internal list struct
groundAddFieldToStruct(&listStruct, "private_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
groundAddFunctionToStruct(&listStruct, "insert", insertIntoListStruct, INT, 2, ANY, "value", INT, "index"); // insert value at index
groundAddFunctionToStruct(&listStruct, "remove", listStructDelete, INT, 1, INT, "index"); // delete value at index
@@ -468,5 +495,6 @@ GroundValue createListStruct() {
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
return groundCreateValue(STRUCTVAL, listStruct);
groundAddNativeFunction(scope, "newList", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
groundAddNativeFunction(scope, "List_SOLS_CONSTRUCTOR", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
}

View File

@@ -6,6 +6,6 @@
extern const uint8_t STARTING_ELEMENTS;
GroundValue createListStruct();
void initLists(GroundScope* scope);
#endif