Further refactoring done. The Parser runs without crashing, however it's output is still to be checked and verified.
This commit is contained in:
@@ -8,13 +8,13 @@ int current; //points to the character currently being considered.
|
||||
int length;
|
||||
int line = 1;
|
||||
|
||||
const char* SAdvance(void);
|
||||
int SIsAtEnd(void);
|
||||
const char* AdvanceScanner(void);
|
||||
int ScannerAtEnd(void);
|
||||
void ScanToken(TokenList*);
|
||||
TokenList* CreateList(void);
|
||||
int AddToTokenList(Token*, TokenList*);
|
||||
int SMatch(char);
|
||||
char SPeek(void);
|
||||
int ScannerMatch(char);
|
||||
char ScannerPeek(void);
|
||||
char PeekNext(void);
|
||||
void ParseString(TokenList *);
|
||||
void ParseNumber(TokenList *);
|
||||
@@ -32,7 +32,7 @@ TokenList* ScanTokens(const char* source) {
|
||||
|
||||
TokenList* tokens = CreateList();
|
||||
|
||||
while(!SIsAtEnd()) {
|
||||
while(!ScannerAtEnd()) {
|
||||
start = current;
|
||||
ScanToken(tokens);
|
||||
}
|
||||
@@ -68,11 +68,7 @@ void DestroyTokenList(TokenList* list) {
|
||||
if (!list) return;
|
||||
|
||||
for(int i = 0; i < list->size; i++) {
|
||||
//Only free objects that required allocation, i.e. not in the TokenTypeMappings
|
||||
if (list->tokens[i]->type == String || list->tokens[i]->type == Number || list->tokens[i]->type == Identifier)
|
||||
free((void *) list->tokens[i]->lexeme);
|
||||
|
||||
free(list->tokens[i]);
|
||||
FreeToken(list->tokens[i]);
|
||||
}
|
||||
|
||||
free(list->tokens);
|
||||
@@ -102,7 +98,7 @@ int AddToTokenList(Token* token, TokenList* list) {
|
||||
}
|
||||
|
||||
void ScanToken(TokenList* tokens) {
|
||||
const char* c = SAdvance();
|
||||
const char* c = AdvanceScanner();
|
||||
|
||||
switch (*c) {
|
||||
case '(':
|
||||
@@ -136,23 +132,23 @@ void ScanToken(TokenList* tokens) {
|
||||
AddToTokenList(CreateToken(NULL, line, Star), tokens);
|
||||
break;
|
||||
case '!':
|
||||
if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Bang_Equal), tokens);
|
||||
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Bang_Equal), tokens);
|
||||
else AddToTokenList(CreateToken(NULL, line, Bang), tokens);
|
||||
break;
|
||||
case '=':
|
||||
if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Equal_Equal), tokens);
|
||||
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Equal_Equal), tokens);
|
||||
else AddToTokenList(CreateToken(NULL, line, Equal), tokens);
|
||||
break;
|
||||
case '<':
|
||||
if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Less_Equal), tokens);
|
||||
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Less_Equal), tokens);
|
||||
else AddToTokenList(CreateToken(NULL, line, Less), tokens);
|
||||
break;
|
||||
case '>':
|
||||
if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Greater_Equal), tokens);
|
||||
if (ScannerMatch('=')) AddToTokenList(CreateToken(NULL, line, Greater_Equal), tokens);
|
||||
else AddToTokenList(CreateToken(NULL, line, Greater), tokens);
|
||||
break;
|
||||
case '/':
|
||||
if (SMatch('/')) while(SPeek() != '\n' && !SIsAtEnd()) { SAdvance(); }
|
||||
if (ScannerMatch('/')) while(ScannerPeek() != '\n' && !ScannerAtEnd()) { AdvanceScanner(); }
|
||||
else AddToTokenList(CreateToken(NULL, line, Slash), tokens);
|
||||
|
||||
break;
|
||||
@@ -180,16 +176,16 @@ void ScanToken(TokenList* tokens) {
|
||||
}
|
||||
}
|
||||
|
||||
int SIsAtEnd() {
|
||||
int ScannerAtEnd() {
|
||||
return current >= length;
|
||||
}
|
||||
|
||||
const char* SAdvance() {
|
||||
const char* AdvanceScanner() {
|
||||
return &source_code[current++];
|
||||
}
|
||||
|
||||
int SMatch(char expected) {
|
||||
if (SIsAtEnd()) return 0;
|
||||
int ScannerMatch(char expected) {
|
||||
if (ScannerAtEnd()) return 0;
|
||||
if (source_code[current] != expected) return 0;
|
||||
|
||||
current++;
|
||||
@@ -197,8 +193,8 @@ int SMatch(char expected) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
char SPeek() {
|
||||
if (SIsAtEnd()) return '\0';
|
||||
char ScannerPeek() {
|
||||
if (ScannerAtEnd()) return '\0';
|
||||
|
||||
return source_code[current];
|
||||
}
|
||||
@@ -206,12 +202,12 @@ char SPeek() {
|
||||
void ParseString(TokenList *list) {
|
||||
start = current; //The start is currently pointing to the first double quote so we need to move it
|
||||
//to the next (first character) of the string literal.
|
||||
while(SPeek() != '"' && !SIsAtEnd()) {
|
||||
if (SPeek() == '\n') line++;
|
||||
SAdvance();
|
||||
while(ScannerPeek() != '"' && !ScannerAtEnd()) {
|
||||
if (ScannerPeek() == '\n') line++;
|
||||
AdvanceScanner();
|
||||
}
|
||||
|
||||
if (SIsAtEnd()) {
|
||||
if (ScannerAtEnd()) {
|
||||
fprintf(stderr, "Unterminated string.\n");
|
||||
return;
|
||||
}
|
||||
@@ -227,16 +223,16 @@ void ParseString(TokenList *list) {
|
||||
|
||||
AddToTokenList(CreateToken(lexeme, line, String), list);
|
||||
|
||||
SAdvance(); // The closing ".
|
||||
AdvanceScanner(); // The closing ".
|
||||
}
|
||||
|
||||
void ParseNumber(TokenList* list) {
|
||||
while(isdigit(SPeek())) SAdvance();
|
||||
while(isdigit(ScannerPeek())) AdvanceScanner();
|
||||
|
||||
if (SPeek() == '.' && isdigit(PeekNext())) {
|
||||
SAdvance();
|
||||
if (ScannerPeek() == '.' && isdigit(PeekNext())) {
|
||||
AdvanceScanner();
|
||||
|
||||
while(isdigit(SPeek())) SAdvance();
|
||||
while(isdigit(ScannerPeek())) AdvanceScanner();
|
||||
}
|
||||
|
||||
char* lexeme = calloc(current - start + 2, sizeof(char));
|
||||
@@ -258,12 +254,12 @@ char PeekNext() {
|
||||
}
|
||||
|
||||
void ParseIdentifier(TokenList * list) {
|
||||
while(IsAlpha(SPeek())) SAdvance();
|
||||
while(IsAlpha(ScannerPeek())) AdvanceScanner();
|
||||
|
||||
char* lexeme = calloc(current - start + 2, sizeof(char));
|
||||
|
||||
snprintf(lexeme, current - start + 1, "%s", &source_code[start]);
|
||||
printf("%s\n", lexeme);
|
||||
|
||||
KeyValuePair* result = Get(lexeme);
|
||||
|
||||
if (result) {
|
||||
|
||||
Reference in New Issue
Block a user