Added Peek() and Match() as well as a bugged '!=' detection.

This commit is contained in:
2022-02-14 17:06:30 +00:00
parent 7bdbb9f53a
commit db54e8f888
2 changed files with 50 additions and 6 deletions
+5
View File
@@ -24,6 +24,11 @@ void Print(TokenList* list) {
char lexeme[100];
for(int i = 0; i < list->size; i++) {
if (list->tokens[i]->type == EndOF) {
printf("EOF\n");\
return;
}
printf("[Line %d] ", list->tokens[i]->line);
for(int j = 0; j < list->tokens[i]->length; j++) {
printf("%c", list->tokens[i]->lexeme[j]);
+45 -6
View File
@@ -11,9 +11,12 @@ int length;
int line = 1;
const char* Advance(void);
int IsAtend(void);
int IsAtEnd(void);
void ScanToken(TokenList*);
TokenList* CreateList(void);
int AddTokenToList(TokenType, const char*, int, TokenList*);
int Match(char);
char Peek(void);
TokenList* ScanTokens(const char* source) {
if (!source) return NULL;
@@ -25,12 +28,14 @@ TokenList* ScanTokens(const char* source) {
TokenList* tokens = CreateList();
while(!IsAtend()) {
while(!IsAtEnd()) {
start = current;
ScanToken(tokens);
}
//Add EOF token and return list once that's set up.
AddTokenToList(EndOF, NULL, 0, tokens);
return tokens;
}
@@ -79,10 +84,11 @@ int AddTokenToList(TokenType type, const char* lexeme, int length, TokenList* to
token->lexeme = lexeme;
token->type = type;
token->length = length; //Set the length of the lexeme (which is just a pointer into the complete soure listing).
token->length = length; //Set the length of the lexeme (which is just a pointer into the complete source listing).
token->line = line;
if ((tokens->size + 1) > tokens->capacity) {
void* new_ptr = realloc(tokens->tokens, sizeof(Token*) * tokens->capacity * 2);//calloc(tokens->capacity * 2, sizeof(Token*));
void* new_ptr = realloc(tokens->tokens, sizeof(Token*) * tokens->capacity * 2);
if (!new_ptr) {
fprintf(stderr, "Failed to realloc TokenList to size %d.\n", tokens->capacity * 2);
@@ -101,7 +107,7 @@ int AddTokenToList(TokenType type, const char* lexeme, int length, TokenList* to
void ScanToken(TokenList* tokens) {
const char* c = Advance();
switch (*c) {
case '(':
AddTokenToList(LParen, c, 1, tokens);
@@ -133,13 +139,46 @@ void ScanToken(TokenList* tokens) {
case '*':
AddTokenToList(Star, c, 1, tokens);
break;
case '!':
if (Match('=')) AddTokenToList(Bang_Equal, "!=", 2, tokens);
else AddTokenToList(Bang, c, 1, tokens);
case '/':
if (Match('/')) {
while(Peek() != '\n' && !IsAtEnd()) Advance();
}
else {
AddTokenToList(Slash, c, 1, tokens);
}
break;
case ' ':
case '\r':
case '\t':
break; //Ignore whitespace
case '\n':
line++;
break;
}
}
int IsAtend() {
int IsAtEnd() {
return current >= length;
}
const char* Advance() {
return &source_code[current++];
}
int Match(char expected) {
if (IsAtEnd()) return 0;
if (source_code[current] != expected) return 0;
current++;
return 1;
}
char Peek() {
if (IsAtEnd()) return '\0';
return source_code[current];
}