The Scanner has been refactored and some responsibilities were pulled from it. Confirmed working, the Parser still needs to be touched on however.

This commit is contained in:
2022-03-02 20:52:40 +00:00
parent 1b33468343
commit e20ccb8cfb
7 changed files with 238 additions and 177 deletions
+51
View File
@@ -0,0 +1,51 @@
#include "token.h"
KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT] = {
{ "(", LParen}, { ")", RParen}, { ",", Comma }, { ".", Dot },
{ "-", Minus }, { "+", Plus }, { ";", Semicolon }, { "/", Slash}, { "*", Star },
{ "!", Bang }, { "!=", Bang_Equal }, { "=", Equal }, { "==", Equal_Equal },
{ ">", Greater }, { ">=", Greater_Equal }, { "<", Less }, { "<=", Less_Equal },
{ "and", AND }, { "class", CLASS }, { "else", ELSE },
{ "false", FALSE }, { "for", FOR }, { "fun", FUN },
{ "if", IF }, { "nil", NIL }, { "or", OR },
{ "print", PRINT }, { "return", RETURN }, { "super", SUPER },
{ "this", THIS }, { "true", TRUE }, { "var", VAR }, { "while", WHILE },
{ "", EndOF }
};
const char* GetLexemeMapping(TokenType);
Token* CreateToken(const char* lexeme, int line, TokenType type) {
Token* token = calloc(1, sizeof(Token));
if (!token) {
fprintf(stderr, "Failed to calloc token. %s", strerror(errno));
return NULL;
}
if (lexeme) token->lexeme = lexeme;
else {
const char* mapping_result = GetLexemeMapping(type);
if (!mapping_result) {
fprintf(stderr, "Failed to get the mapping for %s\n", lexeme);
free(token);
return NULL;
}
token->lexeme = mapping_result;
}
token->line = line;
token->type = type;
return token;
}
const char* GetLexemeMapping(TokenType type) {
for (int i = 0; i < TOKENTYPE_MAPPINGS_COUNT; i++) {
if (TokenTypeMappings[i].type == type) return TokenTypeMappings[i].lexeme;
}
return NULL;
}