Cleaned up the tokenizer and setting the stage for more granular token types.
This commit is contained in:
+63
-64
@@ -2,7 +2,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../includes/list.h"
|
||||
#include "../includes/hash_table.h"
|
||||
#include "../includes/tokenizer.h"
|
||||
|
||||
typedef struct {
|
||||
@@ -56,83 +55,83 @@ int main(int argc, char* args[]) {
|
||||
//free(list);
|
||||
}
|
||||
|
||||
void print_file(char* file_path) {
|
||||
FILE *file;
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
ssize_t bytes_read;
|
||||
// void print_file(char* file_path) {
|
||||
// FILE *file;
|
||||
// char* line = NULL;
|
||||
// size_t len = 0;
|
||||
// ssize_t bytes_read;
|
||||
|
||||
file = fopen(file_path, "r");
|
||||
// file = fopen(file_path, "r");
|
||||
|
||||
if (!file) {
|
||||
printf("Failed to open '%s' for reading.\n", file_path);
|
||||
return;
|
||||
}
|
||||
// if (!file) {
|
||||
// printf("Failed to open '%s' for reading.\n", file_path);
|
||||
// return;
|
||||
// }
|
||||
|
||||
while((bytes_read = getline(&line, &len, file)) != -1) {
|
||||
List* list = get_strings(line);
|
||||
// while((bytes_read = getline(&line, &len, file)) != -1) {
|
||||
// List* list = get_strings(line);
|
||||
|
||||
if (!list) continue;
|
||||
if (list->size == 0) continue;
|
||||
// if (!list) continue;
|
||||
// if (list->size == 0) continue;
|
||||
|
||||
// Instruction *inst = malloc(sizeof(Instruction));
|
||||
// inst->parameters = malloc(sizeof(char*) * 2);
|
||||
// // Instruction *inst = malloc(sizeof(Instruction));
|
||||
// // inst->parameters = malloc(sizeof(char*) * 2);
|
||||
|
||||
// if (strcmp(list->root[0], "mov") == 0) {
|
||||
// inst->opcode = "mov";
|
||||
// inst->count = 2;
|
||||
// inst->parameters[0] = list->root[1];
|
||||
// inst->parameters[1] = list->root[2];
|
||||
// // if (strcmp(list->root[0], "mov") == 0) {
|
||||
// // inst->opcode = "mov";
|
||||
// // inst->count = 2;
|
||||
// // inst->parameters[0] = list->root[1];
|
||||
// // inst->parameters[1] = list->root[2];
|
||||
|
||||
// printf("Instruction '%s' with %d params ('%s', '%s')\n", inst->opcode, inst->count, inst->parameters[0], inst->parameters[1]);
|
||||
// }
|
||||
// // printf("Instruction '%s' with %d params ('%s', '%s')\n", inst->opcode, inst->count, inst->parameters[0], inst->parameters[1]);
|
||||
// // }
|
||||
|
||||
// free(inst->parameters);
|
||||
// free(inst);
|
||||
// // free(inst->parameters);
|
||||
// // free(inst);
|
||||
|
||||
//PrintList(list);
|
||||
DestroyList(list);
|
||||
}
|
||||
// //PrintList(list);
|
||||
// DestroyList(list);
|
||||
// }
|
||||
|
||||
fclose(file);
|
||||
// fclose(file);
|
||||
|
||||
if (line) free(line);
|
||||
}
|
||||
// if (line) free(line);
|
||||
// }
|
||||
|
||||
List* get_strings(const char* line) {
|
||||
if (strlen(line) == 0 ) return NULL;
|
||||
if (line[0] == ';') return NULL; //If the line starts with a semi-colon its just a comment, so ignore it.
|
||||
// List* get_strings(const char* line) {
|
||||
// if (strlen(line) == 0 ) return NULL;
|
||||
// if (line[0] == ';') return NULL; //If the line starts with a semi-colon its just a comment, so ignore it.
|
||||
|
||||
List *list = CreateList();
|
||||
unsigned long lineLength = strlen(line);
|
||||
char* currentWord = malloc(lineLength + 1);
|
||||
int index = 0;
|
||||
// List *list = CreateList();
|
||||
// unsigned long lineLength = strlen(line);
|
||||
// char* currentWord = malloc(lineLength + 1);
|
||||
// int index = 0;
|
||||
|
||||
for (int i = 0; i < lineLength; i++) {
|
||||
if (line[i] == ' ' || line[i] == ',') {
|
||||
if (index > 0) {
|
||||
currentWord[index] = '\0';
|
||||
PushListItem(currentWord, strlen(currentWord) + 1, list);
|
||||
}
|
||||
index = 0;
|
||||
continue;
|
||||
}
|
||||
// for (int i = 0; i < lineLength; i++) {
|
||||
// if (line[i] == ' ' || line[i] == ',') {
|
||||
// if (index > 0) {
|
||||
// currentWord[index] = '\0';
|
||||
// PushListItem(currentWord, strlen(currentWord) + 1, list);
|
||||
// }
|
||||
// index = 0;
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if (line[i] == ';' || i + 1 == lineLength) {
|
||||
if (index > 0) {
|
||||
if (line[i] != ';') {
|
||||
currentWord[index] = line[i];
|
||||
index++;
|
||||
}
|
||||
currentWord[index] = '\0';
|
||||
PushListItem(currentWord, strlen(currentWord) + 1, list);
|
||||
}
|
||||
break;
|
||||
}
|
||||
// if (line[i] == ';' || i + 1 == lineLength) {
|
||||
// if (index > 0) {
|
||||
// if (line[i] != ';') {
|
||||
// currentWord[index] = line[i];
|
||||
// index++;
|
||||
// }
|
||||
// currentWord[index] = '\0';
|
||||
// PushListItem(currentWord, strlen(currentWord) + 1, list);
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
|
||||
currentWord[index] = line[i];
|
||||
index++;
|
||||
}
|
||||
// currentWord[index] = line[i];
|
||||
// index++;
|
||||
// }
|
||||
|
||||
return list;
|
||||
}
|
||||
// return list;
|
||||
// }
|
||||
Reference in New Issue
Block a user