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
+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);
}