From 8656ba7d0cafc96bda6a6727dd6286ece3d9e3f7 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 5 Jan 2022 22:51:56 +0000 Subject: [PATCH] Fixed a bug where the last character of a line would be lost when generating a list of string tokens. --- src/list.c | 8 ++++---- src/main.c | 28 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/list.c b/src/list.c index 661d819..b10b953 100644 --- a/src/list.c +++ b/src/list.c @@ -24,7 +24,7 @@ List* CreateList() { } int AddListItem(const char *value, List* list) { - if (list == NULL) return -1; + if (!list) return -1; if (list->capacity < list->size + 1) { void* ptr = realloc(list->root, sizeof(char*) * list->capacity * 2); @@ -44,7 +44,7 @@ int AddListItem(const char *value, List* list) { if (!item) { fprintf(stderr, "Failed to malloc() new memory for '%s' (length: %lu bytes).\n", value, valueLength); - return -1;; + return -1; } strncpy(item, value, valueLength); @@ -56,7 +56,7 @@ int AddListItem(const char *value, List* list) { } void PrintList(const List* list) { - if (list == NULL) return; + if (!list) return; const List *current = list; @@ -68,7 +68,7 @@ void PrintList(const List* list) { } void DestroyList(List* list) { - if (list == NULL) return; + if (!list) return; for(int i = 0; i < list->size; i++) { free(list->root[i]); diff --git a/src/main.c b/src/main.c index 7f8c8d0..428665b 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,11 @@ #include #include "../includes/list.h" +typedef struct { + char *opcode; + unsigned char count; + char** parameters; +} Instruction; void print_file(char*); List* get_strings(const char*); @@ -51,8 +56,25 @@ void print_file(char* file_path) { while((bytes_read = getline(&line, &len, file)) != -1) { List* list = get_strings(line); + + if (!list) continue; + + Instruction *inst = malloc(sizeof(Instruction)); + inst->parameters = malloc(sizeof(char*) * 2); - PrintList(list); + 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]); + } + + free(inst->parameters); + free(inst); + + //PrintList(list); DestroyList(list); } @@ -82,6 +104,10 @@ List* get_strings(const char* line) { if (line[i] == ';' || i + 1 == lineLength) { if (index > 0) { + if (line[i] != ';') { + currentWord[index] = line[i]; + index++; + } currentWord[index] = '\0'; AddListItem(currentWord, list); }