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:
2022-03-08 22:08:06 +00:00
parent 2d587e75b1
commit e802c57a93
4 changed files with 63 additions and 12 deletions
+31 -1
View File
@@ -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;
}