Refactored the scanner and updated the Token struct to store the length of its lexeme to avoid malloc()ing new strings for every token.

This commit is contained in:
2022-02-10 17:17:33 +00:00
parent 2d84019e58
commit 7bdbb9f53a
3 changed files with 55 additions and 14 deletions
+18 -1
View File
@@ -1,3 +1,4 @@
#include "scanner.h"
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
@@ -19,10 +20,26 @@ int main(int argc, char** argv) {
}
}
void Print(TokenList* list) {
char lexeme[100];
for(int i = 0; i < list->size; i++) {
printf("[Line %d] ", list->tokens[i]->line);
for(int j = 0; j < list->tokens[i]->length; j++) {
printf("%c", list->tokens[i]->lexeme[j]);
}
printf("\n");
}
}
void RunFile(const char* path) {
printf("Running '%s'\n", path);
char* contents = GetFileContents(path);
//Tokenize(contents);
TokenList* tokens = ScanTokens(contents);
Print(tokens);
DestroyTokenList(tokens);
free(contents);
}
char* GetFileContents(const char* path) {