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.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user