#include "SolsToken.h" #include "SolsLiteral.h" #include "../include/error.h" #include #include ResultType(SolsToken, charptr) createSolsToken(SolsTokenType type, ...) { va_list args; va_start(args, type); SolsToken token = { .type = type }; if (type == STT_IDENTIFIER) { char* name = va_arg(args, char*); if (name == NULL) { return Error(SolsToken, charptr, "String passed is NULL (in createSolsToken() function)"); } token.as.idName = malloc(strlen(name) + 1); if (token.as.idName == NULL) { return Error(SolsToken, charptr, "Couldn't allocate memory (in createSolsToken() function)"); } strcpy(token.as.idName, name); } if (type == STT_KW_GROUND) { char* ground = va_arg(args, char*); if (ground == NULL) { return Error(SolsToken, charptr, "String passed is NULL (in createSolsToken() function)"); } token.as.inlineGround = malloc(strlen(ground) + 1); if (token.as.inlineGround == NULL) { return Error(SolsToken, charptr, "Couldn't allocate memory (in createSolsToken() function)"); } strcpy(token.as.inlineGround, ground); } if (type == STT_LITERAL) { token.as.literal = va_arg(args, SolsLiteral); } if (type == STT_TYPE) { token.as.type = va_arg(args, SolsType); } va_end(args); return Success(SolsToken, charptr, token); } void freeSolsToken(SolsToken* token) { if (token->type == STT_IDENTIFIER && token->as.idName != NULL) { free(token->as.idName); } if (token->type == STT_KW_GROUND && token->as.inlineGround != NULL) { free(token->as.inlineGround); } if (token->type == STT_LITERAL) { freeSolsLiteral(&token->as.literal); } if (token->type == STT_TYPE) { freeSolsType(&token->as.type); } } ResultType(SolsTokens, charptr) createSolsTokens() { SolsTokens tokens = { .at = malloc(sizeof(SolsToken) * 32), .capacity = 32, .count = 0 }; if (tokens.at == NULL) { return Error(SolsTokens, charptr, "Failed to allocate memory (in createSolsTokens() function)"); } return Success(SolsTokens, charptr, tokens); } ResultType(voidptr, charptr) addTokenToSolsTokens(SolsTokens* tokens, SolsToken token) { if (tokens->capacity < tokens->count + 1) { tokens->capacity *= 2; SolsToken* tmp = realloc(tokens->at, sizeof(SolsToken) * tokens->capacity); if (tmp == NULL) { return Error(voidptr, charptr, "Failed to allocate memory (in addTokenToSolsTokens() function)"); } } tokens->at[tokens->count] = token; tokens->count++; return Success(voidptr, charptr, NULL); }