From 5ea8079090588ab056b871a4e07a08d123b088e9 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 3 Mar 2022 19:39:12 +0000 Subject: [PATCH] Updated the token to more reflect what was implemented in the book. Maybe not the right way to handle it but perhaps this setup will come in handy in later chapters. --- scanner.c | 49 +++++++++++++++++++++++++------------------------ token.c | 8 ++++++-- token.h | 3 ++- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/scanner.c b/scanner.c index 3b86882..d73e205 100644 --- a/scanner.c +++ b/scanner.c @@ -37,7 +37,7 @@ TokenList* ScanTokens(const char* source) { ScanToken(tokens); } - AddToTokenList(CreateToken(NULL, line, EndOF), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, EndOF), tokens); return tokens; } @@ -102,54 +102,54 @@ void ScanToken(TokenList* tokens) { switch (*c) { case '(': - AddToTokenList(CreateToken(NULL, line, LParen), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, LParen), tokens); break; case ')': - AddToTokenList(CreateToken(NULL, line, RParen), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, RParen), tokens); break; case '{': - AddToTokenList(CreateToken(NULL, line, LBrace), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, LBrace), tokens); break; case '}': - AddToTokenList(CreateToken(NULL, line, RBrace), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, RBrace), tokens); break; case ',': - AddToTokenList(CreateToken(NULL, line, Comma), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, Comma), tokens); break; case '.': - AddToTokenList(CreateToken(NULL, line, Dot), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, Dot), tokens); break; case '-': - AddToTokenList(CreateToken(NULL, line, Minus), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, Minus), tokens); break; case '+': - AddToTokenList(CreateToken(NULL, line, Plus), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, Plus), tokens); break; case ';': - AddToTokenList(CreateToken(NULL, line, Semicolon), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, Semicolon), tokens); break; case '*': - AddToTokenList(CreateToken(NULL, line, Star), tokens); + AddToTokenList(CreateToken(NULL, NULL, line, Star), tokens); break; case '!': - if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Bang_Equal), tokens); - else AddToTokenList(CreateToken(NULL, line, Bang), tokens); + if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, NULL, line, Bang_Equal), tokens); + else AddToTokenList(CreateToken(NULL, NULL, line, Bang), tokens); break; case '=': - if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Equal_Equal), tokens); - else AddToTokenList(CreateToken(NULL, line, Equal), tokens); + if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, NULL, line, Equal_Equal), tokens); + else AddToTokenList(CreateToken(NULL, NULL, line, Equal), tokens); break; case '<': - if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Less_Equal), tokens); - else AddToTokenList(CreateToken(NULL, line, Less), tokens); + if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, NULL, line, Less_Equal), tokens); + else AddToTokenList(CreateToken(NULL, NULL, line, Less), tokens); break; case '>': - if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Greater_Equal), tokens); - else AddToTokenList(CreateToken(NULL, line, Greater), tokens); + if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, NULL, line, Greater_Equal), tokens); + else AddToTokenList(CreateToken(NULL, NULL, line, Greater), tokens); break; case '/': if (ScannerMatch('/')) while(ScannerPeek() != '\n' && !ScannerAtEnd()) { AdvanceScanner(); } - else AddToTokenList(CreateToken(NULL, line, Slash), tokens); + else AddToTokenList(CreateToken(NULL, NULL, line, Slash), tokens); break; case '"': @@ -221,7 +221,7 @@ void ParseString(TokenList *list) { snprintf(lexeme, current - start, "%s", &source_code[start]); - AddToTokenList(CreateToken(lexeme, line, String), list); + AddToTokenList(CreateToken(lexeme, lexeme, line, String), list); AdvanceScanner(); // The closing ". } @@ -243,8 +243,9 @@ void ParseNumber(TokenList* list) { } snprintf(lexeme, current - start + 1, "%s", &source_code[start]); + double value = atof(lexeme); //Will this reference become stale on return? - AddToTokenList(CreateToken(lexeme, line, Number), list); + AddToTokenList(CreateToken(lexeme, &value, line, Number), list); } char PeekNext() { @@ -265,9 +266,9 @@ void ParseIdentifier(TokenList * list) { if (result) { free(lexeme); - AddToTokenList(CreateToken(NULL, line, result->type), list); + AddToTokenList(CreateToken(NULL, NULL, line, result->type), list); } - else AddToTokenList(CreateToken(lexeme, line, Identifier), list); + else AddToTokenList(CreateToken(lexeme, lexeme, line, Identifier), list); } int IsAlpha(char c) { diff --git a/token.c b/token.c index a09922d..dd37351 100644 --- a/token.c +++ b/token.c @@ -15,7 +15,7 @@ KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT] = { const char* GetLexemeMapping(TokenType); -Token* CreateToken(const char* lexeme, int line, TokenType type) { +Token* CreateToken(const char* lexeme, void* literal, int line, TokenType type) { Token* token = calloc(1, sizeof(Token)); if (!token) { @@ -23,7 +23,10 @@ Token* CreateToken(const char* lexeme, int line, TokenType type) { return NULL; } - if (lexeme) token->lexeme = lexeme; + if (lexeme) { + token->literal = literal; + token->lexeme = lexeme; + } else { const char* mapping_result = GetLexemeMapping(type); @@ -33,6 +36,7 @@ Token* CreateToken(const char* lexeme, int line, TokenType type) { return NULL; } + token->literal = mapping_result; token->lexeme = mapping_result; } diff --git a/token.h b/token.h index a9ead21..f16d9a6 100644 --- a/token.h +++ b/token.h @@ -42,13 +42,14 @@ typedef struct { typedef struct { TokenType type; const char* lexeme; + const void* literal; int line; } Token; extern KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT]; //If the first parameter is NULL, the token creation will attempt to infer the lexeme from the TokenType. -Token* CreateToken(const char*, int, TokenType); +Token* CreateToken(const char*, void*, int, TokenType); void FreeToken(Token*); #endif \ No newline at end of file