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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user