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);
|
ScanToken(tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
AddToTokenList(CreateToken(NULL, line, EndOF), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, EndOF), tokens);
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
@@ -102,54 +102,54 @@ void ScanToken(TokenList* tokens) {
|
|||||||
|
|
||||||
switch (*c) {
|
switch (*c) {
|
||||||
case '(':
|
case '(':
|
||||||
AddToTokenList(CreateToken(NULL, line, LParen), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, LParen), tokens);
|
||||||
break;
|
break;
|
||||||
case ')':
|
case ')':
|
||||||
AddToTokenList(CreateToken(NULL, line, RParen), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, RParen), tokens);
|
||||||
break;
|
break;
|
||||||
case '{':
|
case '{':
|
||||||
AddToTokenList(CreateToken(NULL, line, LBrace), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, LBrace), tokens);
|
||||||
break;
|
break;
|
||||||
case '}':
|
case '}':
|
||||||
AddToTokenList(CreateToken(NULL, line, RBrace), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, RBrace), tokens);
|
||||||
break;
|
break;
|
||||||
case ',':
|
case ',':
|
||||||
AddToTokenList(CreateToken(NULL, line, Comma), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, Comma), tokens);
|
||||||
break;
|
break;
|
||||||
case '.':
|
case '.':
|
||||||
AddToTokenList(CreateToken(NULL, line, Dot), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, Dot), tokens);
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
AddToTokenList(CreateToken(NULL, line, Minus), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, Minus), tokens);
|
||||||
break;
|
break;
|
||||||
case '+':
|
case '+':
|
||||||
AddToTokenList(CreateToken(NULL, line, Plus), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, Plus), tokens);
|
||||||
break;
|
break;
|
||||||
case ';':
|
case ';':
|
||||||
AddToTokenList(CreateToken(NULL, line, Semicolon), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, Semicolon), tokens);
|
||||||
break;
|
break;
|
||||||
case '*':
|
case '*':
|
||||||
AddToTokenList(CreateToken(NULL, line, Star), tokens);
|
AddToTokenList(CreateToken(NULL, NULL, line, Star), tokens);
|
||||||
break;
|
break;
|
||||||
case '!':
|
case '!':
|
||||||
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Bang_Equal), tokens);
|
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, NULL, line, Bang_Equal), tokens);
|
||||||
else AddToTokenList(CreateToken(NULL, line, Bang), tokens);
|
else AddToTokenList(CreateToken(NULL, NULL, line, Bang), tokens);
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Equal_Equal), tokens);
|
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, NULL, line, Equal_Equal), tokens);
|
||||||
else AddToTokenList(CreateToken(NULL, line, Equal), tokens);
|
else AddToTokenList(CreateToken(NULL, NULL, line, Equal), tokens);
|
||||||
break;
|
break;
|
||||||
case '<':
|
case '<':
|
||||||
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Less_Equal), tokens);
|
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, NULL, line, Less_Equal), tokens);
|
||||||
else AddToTokenList(CreateToken(NULL, line, Less), tokens);
|
else AddToTokenList(CreateToken(NULL, NULL, line, Less), tokens);
|
||||||
break;
|
break;
|
||||||
case '>':
|
case '>':
|
||||||
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Greater_Equal), tokens);
|
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, NULL, line, Greater_Equal), tokens);
|
||||||
else AddToTokenList(CreateToken(NULL, line, Greater), tokens);
|
else AddToTokenList(CreateToken(NULL, NULL, line, Greater), tokens);
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
if (ScannerMatch('/')) while(ScannerPeek() != '\n' && !ScannerAtEnd()) { AdvanceScanner(); }
|
if (ScannerMatch('/')) while(ScannerPeek() != '\n' && !ScannerAtEnd()) { AdvanceScanner(); }
|
||||||
else AddToTokenList(CreateToken(NULL, line, Slash), tokens);
|
else AddToTokenList(CreateToken(NULL, NULL, line, Slash), tokens);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
@@ -221,7 +221,7 @@ void ParseString(TokenList *list) {
|
|||||||
|
|
||||||
snprintf(lexeme, current - start, "%s", &source_code[start]);
|
snprintf(lexeme, current - start, "%s", &source_code[start]);
|
||||||
|
|
||||||
AddToTokenList(CreateToken(lexeme, line, String), list);
|
AddToTokenList(CreateToken(lexeme, lexeme, line, String), list);
|
||||||
|
|
||||||
AdvanceScanner(); // The closing ".
|
AdvanceScanner(); // The closing ".
|
||||||
}
|
}
|
||||||
@@ -243,8 +243,9 @@ void ParseNumber(TokenList* list) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
snprintf(lexeme, current - start + 1, "%s", &source_code[start]);
|
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() {
|
char PeekNext() {
|
||||||
@@ -265,9 +266,9 @@ void ParseIdentifier(TokenList * list) {
|
|||||||
if (result) {
|
if (result) {
|
||||||
free(lexeme);
|
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) {
|
int IsAlpha(char c) {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT] = {
|
|||||||
|
|
||||||
const char* GetLexemeMapping(TokenType);
|
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));
|
Token* token = calloc(1, sizeof(Token));
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
@@ -23,7 +23,10 @@ Token* CreateToken(const char* lexeme, int line, TokenType type) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lexeme) token->lexeme = lexeme;
|
if (lexeme) {
|
||||||
|
token->literal = literal;
|
||||||
|
token->lexeme = lexeme;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
const char* mapping_result = GetLexemeMapping(type);
|
const char* mapping_result = GetLexemeMapping(type);
|
||||||
|
|
||||||
@@ -33,6 +36,7 @@ Token* CreateToken(const char* lexeme, int line, TokenType type) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
token->literal = mapping_result;
|
||||||
token->lexeme = mapping_result;
|
token->lexeme = mapping_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,13 +42,14 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
TokenType type;
|
TokenType type;
|
||||||
const char* lexeme;
|
const char* lexeme;
|
||||||
|
const void* literal;
|
||||||
int line;
|
int line;
|
||||||
} Token;
|
} Token;
|
||||||
|
|
||||||
extern KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT];
|
extern KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT];
|
||||||
|
|
||||||
//If the first parameter is NULL, the token creation will attempt to infer the lexeme from the TokenType.
|
//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*);
|
void FreeToken(Token*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user