Trying a more object oriented approach and created an 'Object' struct to have metadata for the values the interpreter works with.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include "token.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT] = {
|
||||
{ "(", LParen}, { ")", RParen}, { ",", Comma }, { ".", Dot },
|
||||
@@ -36,7 +38,7 @@ Token* CreateToken(const char* lexeme, void* literal, int line, TokenType type)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
token->literal = mapping_result;
|
||||
if (type != NIL) token->literal = mapping_result;
|
||||
token->lexeme = mapping_result;
|
||||
}
|
||||
|
||||
@@ -60,4 +62,32 @@ void FreeToken(Token* token) {
|
||||
if (token->type == String || token->type == Number || token->type == Identifier) free((void *) token->lexeme);
|
||||
|
||||
free(token);
|
||||
}
|
||||
|
||||
char* GetTokenStringValue(Token* token, int* length) {
|
||||
length = 0;
|
||||
|
||||
if (!token) return NULL;
|
||||
if (token->type != String) return NULL;
|
||||
|
||||
*length = strlen(token->lexeme);
|
||||
|
||||
if (length == 0) return NULL;
|
||||
|
||||
char* value = calloc(strlen(token->lexeme) + 1, sizeof(char));
|
||||
|
||||
strncpy(value, token->lexeme, *length);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
int GetTokenNumberValue(Token* token, double* value) {
|
||||
value = 0;
|
||||
|
||||
if (!token) return 0;
|
||||
if (token->type != Number) return 0;
|
||||
|
||||
*value = *((double*)token->literal);
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user