94 lines
3.5 KiB
C
94 lines
3.5 KiB
C
#include "lexer.h"
|
|
#include "../include/error.h"
|
|
#include "../include/estr.h"
|
|
|
|
ResultType(SolsType, charptr) createSolsType(SolsTypeType in) {
|
|
SolsTypeField* ptr = malloc(sizeof(SolsTypeField) * 32);
|
|
if (ptr == NULL) {
|
|
return Error(SolsType, charptr, "Couldn't allocate memory (in createSolsType() function)");
|
|
}
|
|
SolsType type = { .type = in, .children.capacity = 32, .children.at = ptr };
|
|
return Success(SolsType, charptr, type);
|
|
}
|
|
|
|
ResultType(SolsType, charptr) copySolsType(SolsType* type) {
|
|
SolsType ret = { .type = type->type, .children.count = type->children.count, .children.capacity = type->children.capacity};
|
|
|
|
// Allocate memory
|
|
SolsTypeField* ptr = malloc(sizeof(SolsTypeField) * type->children.capacity);
|
|
if (ptr == NULL) {
|
|
return Error(SolsType, charptr, "Couldn't allocate memory (in copySolsType() function)");
|
|
}
|
|
ret.children.at = ptr;
|
|
|
|
// Deep copy values
|
|
for (size_t i = 0; i < type->children.count; i++) {
|
|
// Copy the SolsType value
|
|
ResultType(SolsType, charptr) copied = copySolsType(&type->children.at[i].type);
|
|
if (copied.error) {
|
|
Estr err = CREATE_ESTR(copied.as.error);
|
|
APPEND_ESTR(err, " (in addChildToSolsType() function)");
|
|
return Error(SolsType, charptr, err.str);
|
|
}
|
|
ret.children.at[i].type = copied.as.success;
|
|
|
|
// Copy the name
|
|
if (type->children.at[i].name == NULL) {
|
|
ret.children.at[i].name = NULL;
|
|
} else {
|
|
ret.children.at[i].name = malloc(strlen(type->children.at[i].name) + 1);
|
|
if (ret.children.at[i].name == NULL) {
|
|
return Error(SolsType, charptr, "Couldn't allocate memory (in copySolsType() function)");
|
|
}
|
|
strcpy(ret.children.at[i].name, type->children.at[i].name);
|
|
}
|
|
}
|
|
return Success(SolsType, charptr, ret);
|
|
}
|
|
|
|
ResultType(Nothing, charptr) addChildToSolsType(SolsType* type, SolsType child, const char* name) {
|
|
if (type->children.capacity < type->children.count + 1) {
|
|
type->children.capacity *= 2;
|
|
SolsTypeField* ptr = realloc(type->children.at, sizeof(SolsTypeField) * type->children.capacity);
|
|
if (ptr == NULL) {
|
|
return Error(Nothing, charptr, "Couldn't allocate memory (in addChildToSolsType() function)");
|
|
}
|
|
type->children.at = ptr;
|
|
}
|
|
ResultType(SolsType, charptr) copied = copySolsType(&child);
|
|
if (copied.error) {
|
|
Estr err = CREATE_ESTR(copied.as.error);
|
|
APPEND_ESTR(err, " (in addChildToSolsType() function)");
|
|
return Error(Nothing, charptr, err.str);
|
|
}
|
|
type->children.at[type->children.count].type = copied.as.success;
|
|
if (name == NULL) {
|
|
type->children.at[type->children.count].name = NULL;
|
|
} else {
|
|
type->children.at[type->children.count].name = malloc(strlen(name) + 1);
|
|
strcpy(type->children.at[type->children.count].name, name);
|
|
}
|
|
type->children.count++;
|
|
|
|
return Success(Nothing, charptr, {});
|
|
}
|
|
|
|
void freeSolsType(SolsType* type) {
|
|
for (size_t i = 0; i < type->children.count; i++) {
|
|
// Free the name
|
|
if (type->children.at[i].name != NULL) {
|
|
free(type->children.at[i].name);
|
|
}
|
|
|
|
// Free the child SolsTypes
|
|
freeSolsType(&type->children.at[i].type);
|
|
}
|
|
// Free the field itself
|
|
free(type->children.at);
|
|
type->children.at = NULL;
|
|
|
|
// Set count and capacity to zero
|
|
type->children.count = 0;
|
|
type->children.capacity = 0;
|
|
}
|