Fixed a bug where the last character of a line would be lost when generating a list of string tokens.

This commit is contained in:
2022-01-05 22:51:56 +00:00
parent 0b46ac2f54
commit 8656ba7d0c
2 changed files with 31 additions and 5 deletions
+4 -4
View File
@@ -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]);
+27 -1
View File
@@ -3,6 +3,11 @@
#include <string.h>
#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);
}