Last bit of clean up before building the parser.
This commit is contained in:
+1
-1
@@ -33,6 +33,6 @@ typedef struct {
|
|||||||
char* value;
|
char* value;
|
||||||
} Token;
|
} Token;
|
||||||
|
|
||||||
List* GenerateTokensFromFile(const char*);
|
List* GetTokensFromLine(char*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+1
-21
@@ -7,22 +7,9 @@ Token* CreateToken(TokenType, char*);
|
|||||||
Token* GetNextToken(char *);
|
Token* GetNextToken(char *);
|
||||||
int TokenIsNumeric(const char*, int *);
|
int TokenIsNumeric(const char*, int *);
|
||||||
|
|
||||||
List* GenerateTokensFromFile(const char* file_path) {
|
List* GetTokensFromLine(char* line) {
|
||||||
FILE *file;
|
|
||||||
int line_count = 1;
|
|
||||||
char* line = NULL;
|
|
||||||
size_t len = 0;
|
|
||||||
ssize_t bytes_read;
|
|
||||||
List* tokens = CreateList();
|
List* tokens = CreateList();
|
||||||
|
|
||||||
file = fopen(file_path, "r");
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
printf("Failed to open '%s' for reading.\n", file_path);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
while((bytes_read = getline(&line, &len, file)) != -1) {
|
|
||||||
Token* token = GetNextToken(line);
|
Token* token = GetNextToken(line);
|
||||||
|
|
||||||
while (token) {
|
while (token) {
|
||||||
@@ -34,13 +21,6 @@ List* GenerateTokensFromFile(const char* file_path) {
|
|||||||
token = GetNextToken(NULL);
|
token = GetNextToken(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
line_count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
if (line) free(line);
|
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+27
-5
@@ -10,26 +10,48 @@ typedef struct {
|
|||||||
char** parameters;
|
char** parameters;
|
||||||
} Instruction;
|
} Instruction;
|
||||||
|
|
||||||
void print_file(char*);
|
|
||||||
List* get_strings(const char*);
|
List* get_strings(const char*);
|
||||||
|
|
||||||
int main(int argc, char* args[]) {
|
int main(int argc, char* args[]) {
|
||||||
const char *input_files[argc - 1];
|
|
||||||
|
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
printf("Input files required.\n");
|
printf("Input files required.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
List* tokens = GenerateTokensFromFile(args[1]);
|
FILE *file;
|
||||||
|
int line_count = 1;
|
||||||
|
char* line = NULL;
|
||||||
|
size_t len = 0;
|
||||||
|
ssize_t bytes_read;
|
||||||
|
int line_number = 1;
|
||||||
|
|
||||||
|
file = fopen(args[1], "r");
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
printf("Failed to open '%s' for reading.\n", args[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//getline() uses realloc() for the line parameter, so there shouldn't
|
||||||
|
//be any issue with memory leaks as long as the last time this function
|
||||||
|
//is called, line gets free()ed.
|
||||||
|
while((bytes_read = getline(&line, &len, file)) != -1) {
|
||||||
|
List* tokens = GetTokensFromLine(line);
|
||||||
|
|
||||||
for (int i = 0; i < tokens->size; i++) {
|
for (int i = 0; i < tokens->size; i++) {
|
||||||
Token* t = (Token*) tokens->content[i];
|
Token* t = (Token*) tokens->content[i];
|
||||||
|
|
||||||
printf("Token Type: '%d' Value: '%s'\n", t->type, t->value);
|
printf("Token Type: '%d' Value: '%s' (%d)\n", t->type, t->value, line_number);
|
||||||
|
|
||||||
free(t->value);
|
free(t->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
DestroyList(tokens);
|
DestroyList(tokens);
|
||||||
|
|
||||||
|
line_number++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
if (line) free(line);
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,8 @@ char* PeekNextToken() {
|
|||||||
return SplitOnBasicGrammar(string, &pos);
|
return SplitOnBasicGrammar(string, &pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: this function should probably return NULL when its run out of tokens
|
||||||
|
//instead of sending back a valid pointer with memory that must be free()ed by the caller.
|
||||||
char* SplitOnBasicGrammar(char* line, unsigned long *string_index) {
|
char* SplitOnBasicGrammar(char* line, unsigned long *string_index) {
|
||||||
|
|
||||||
unsigned long length = strlen(string);
|
unsigned long length = strlen(string);
|
||||||
|
|||||||
Reference in New Issue
Block a user