diff --git a/src/lexer/SolsToken.c b/src/lexer/SolsToken.c index e69de29..339641a 100644 --- a/src/lexer/SolsToken.c +++ b/src/lexer/SolsToken.c @@ -0,0 +1,48 @@ +#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); +} diff --git a/src/lexer/SolsToken.h b/src/lexer/SolsToken.h index a664659..d9ea064 100644 --- a/src/lexer/SolsToken.h +++ b/src/lexer/SolsToken.h @@ -3,11 +3,15 @@ #include "SolsType.h" #include "SolsLiteral.h" +#include "../include/error.h" +#include typedef enum SolsTokenType { STT_IDENTIFIER, STT_LITERAL, STT_TYPE, STT_OPEN_CURLY, STT_CLOSE_CURLY, STT_OPEN_PAREN, STT_CLOSE_PAREN, STT_OP_ADD, STT_OP_SUB, STT_OP_MUL, STT_OP_DIV, STT_OP_SET, STT_OP_GREATER, STT_OP_LESSER, STT_OP_EQUAL, STT_OP_INEQUAL, STT_OP_EQGREATER, STT_OP_EQLESSER, STT_KW_DEF, STT_KW_STRUCT, STT_KW_PUTS, STT_KW_GROUND } SolsTokenType; +typedef char* charptr; + // Represents a token lexed by the lex() function. // Most token types exclusively use the .type field, however some tokens require storing // more data, inside the .as union. @@ -26,6 +30,13 @@ typedef struct SolsToken { } as; } SolsToken; +Result(SolsToken, charptr); + +// Creates a SolsToken. If the type passed in is STT_LITERAL, STT_TYPE, STT_IDENTIFIER or +// STT_KW_GROUND, the function expects another argument, corresponding to the data type +// the token holds. See the SolsToken struct for more information. +ResultType(SolsToken, charptr) createSolsToken(SolsTokenType type, ...); + // Represents a Solstice program, seperated into tokens. // .at is a pointer to the tokens // .count is how many tokens are currently being stored