forked from ground/cground
Add lists (MEMORY ERRORS)
This commit is contained in:
29
src/types.c
29
src/types.c
@@ -37,6 +37,13 @@ GroundValue createBoolGroundValue(bool in) {
|
||||
return gv;
|
||||
}
|
||||
|
||||
GroundValue createListGroundValue(List in) {
|
||||
GroundValue gv;
|
||||
gv.data.listVal = in;
|
||||
gv.type = LIST;
|
||||
return gv;
|
||||
}
|
||||
|
||||
void printGroundValue(GroundValue* gv) {
|
||||
switch (gv->type) {
|
||||
case INT: {
|
||||
@@ -74,6 +81,13 @@ void freeGroundValue(GroundValue* gv) {
|
||||
if (gv->type == STRING && gv->data.stringVal != NULL) {
|
||||
free(gv->data.stringVal);
|
||||
}
|
||||
if (gv->type == LIST && gv->data.listVal.values != NULL) {
|
||||
List* list = &gv->data.listVal;
|
||||
for (int i = 0; i < list->size; i++) {
|
||||
freeGroundValue(&list->values[i]);
|
||||
}
|
||||
free(list->values);
|
||||
}
|
||||
}
|
||||
|
||||
GroundArg createValueGroundArg(GroundValue value) {
|
||||
@@ -159,6 +173,19 @@ GroundInstruction copyGroundInstruction(const GroundInstruction* inst) {
|
||||
if (inst->args.args[i].value.value.type == STRING) {
|
||||
newInst.args.args[i].value.value.data.stringVal = strdup(inst->args.args[i].value.value.data.stringVal);
|
||||
}
|
||||
if (inst->args.args[i].value.value.type == LIST) {
|
||||
List* origList = &inst->args.args[i].value.value.data.listVal;
|
||||
List newList = createList();
|
||||
for (int j = 0; j < origList->size; j++) {
|
||||
GroundValue copiedValue = origList->values[j];
|
||||
if (copiedValue.type == STRING) {
|
||||
copiedValue.data.stringVal = strdup(copiedValue.data.stringVal);
|
||||
}
|
||||
// Note: this doesn't handle nested lists - you'd need full recursion for that
|
||||
appendToList(&newList, copiedValue);
|
||||
}
|
||||
newInst.args.args[i].value.value.data.listVal = newList;
|
||||
}
|
||||
} else {
|
||||
newInst.args.args[i].value.refName = strdup(inst->args.args[i].value.refName);
|
||||
}
|
||||
@@ -323,7 +350,7 @@ void appendToList(List* list, GroundValue value) {
|
||||
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));
|
||||
GroundValue* ptr = realloc(list->values, (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);
|
||||
|
||||
Reference in New Issue
Block a user