List stuff

This commit is contained in:
2025-11-28 09:23:43 +11:00
parent 99e93bdfb8
commit 0b917a6702
3 changed files with 213 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
#include "types.h"
#include <inttypes.h>
#include <stdlib.h>
GroundValue createIntGroundValue(int64_t in) {
GroundValue gv;
@@ -309,3 +310,56 @@ void printGroundInstruction(GroundInstruction* gi) {
printf(" ");
}
}
List createList() {
List list;
list.size = 0;
list.values = malloc(sizeof(GroundValue));
return list;
}
void appendToList(List* list, GroundValue value) {
if (list == NULL) {
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
exit(EXIT_FAILURE);
}
GroundValue* ptr = realloc(list, (list->size + 1) * sizeof(GroundValue));
if (ptr == NULL) {
printf("There was an error allocating memory for a list.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
exit(EXIT_FAILURE);
}
list->size++;
list->values = ptr;
list->values[list->size - 1] = value;
}
ListAccess getListAt(List* list, int idx) {
if (list == NULL) {
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
exit(EXIT_FAILURE);
}
if (idx < list->size) {
ListAccess retval;
retval.value = &list->values[idx];
retval.status = LIST_OKAY;
return retval;
} else {
ListAccess retval;
retval.value = NULL;
retval.status = LIST_OUT_OF_BOUNDS;
return retval;
}
}
ListAccessStatus setListAt(List* list, int idx, GroundValue value) {
if (list == NULL) {
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
exit(EXIT_FAILURE);
}
if (idx < list->size) {
list->values[idx] = value;
return LIST_OKAY;
} else {
return LIST_OUT_OF_BOUNDS;
}
}