From 08acc6af229d4c454405fb9bc7c8c97528e1557a Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 30 Dec 2021 15:21:09 -0600 Subject: [PATCH] First stage of splitting up a source line complete. Next is tokenizing the list of string being generated. --- .gitignore | 1 + includes/list.h | 1 + misc/test.asm | 4 +-- src/list.c | 7 ++-- src/main.c | 93 ++++++++++++++++++++----------------------------- 5 files changed, 46 insertions(+), 60 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fe4a28d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +assm \ No newline at end of file diff --git a/includes/list.h b/includes/list.h index 3db4453..dcff1f7 100644 --- a/includes/list.h +++ b/includes/list.h @@ -3,6 +3,7 @@ #include #include +#include #define LISTDEFAULTSIZE 4 diff --git a/misc/test.asm b/misc/test.asm index 3f6f3f9..65f83a5 100644 --- a/misc/test.asm +++ b/misc/test.asm @@ -1,6 +1,6 @@ ; comments are ignored ; This is a very simple test file for an assemblier. mov r1, 5 ; move the immediate value 5 into r1 -add r1, 5 ; add 5 into r1 +add r1,5 ; add 5 into r1 int 21 ; maybe that will call some string drawing BIOS-like routine -ret ; return from 'call' if there were such a thing \ No newline at end of file +ret \ No newline at end of file diff --git a/src/list.c b/src/list.c index f5c8256..fa5fbbf 100644 --- a/src/list.c +++ b/src/list.c @@ -1,7 +1,4 @@ #include "../includes/list.h" -#include -#include -#include List* CreateList() { List *new = malloc(sizeof(List)); @@ -13,6 +10,7 @@ List* CreateList() { } int AddListItem(char *value, List* list) { + if (list == NULL) return -1; if (list->capacity < list->size + 1) { list->root = realloc(list->root, sizeof(char*) * list->capacity * 2); @@ -31,6 +29,8 @@ int AddListItem(char *value, List* list) { } void PrintList(List* list) { + if (list == NULL) return; + List *current = list; printf("Size: %d; Capacity: %d\n", list->size, list->capacity); @@ -41,6 +41,7 @@ void PrintList(List* list) { } void DestroyList(List* list) { + if (list == NULL) return; for(int i = 0; i < list->size; i++) { free(list->root[i]); diff --git a/src/main.c b/src/main.c index 1446042..7f8c8d0 100644 --- a/src/main.c +++ b/src/main.c @@ -3,14 +3,9 @@ #include #include "../includes/list.h" -typedef struct opcode { - char op[255]; - char param1[255]; - char param2[255]; -} Opcode; void print_file(char*); -Opcode* get_opcode(char*); +List* get_strings(const char*); int main(int argc, char* args[]) { const char *input_files[argc - 1]; @@ -25,20 +20,20 @@ int main(int argc, char* args[]) { // print_file(args[i]); // } - //print_file(args[1]); - char str[16]; - List *list = CreateList(); + print_file(args[1]); + // char str[16]; + // List *list = CreateList(); - for (int i = 0; i < 64; i++) { - sprintf(str, "%d", i); - AddListItem(str, list); + // for (int i = 0; i < 64; i++) { + // sprintf(str, "%d", i); + // AddListItem(str, list); - if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity); - } + // if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity); + // } - PrintList(list); + // PrintList(list); - free(list); + // free(list); } void print_file(char* file_path) { @@ -55,59 +50,47 @@ void print_file(char* file_path) { } while((bytes_read = getline(&line, &len, file)) != -1) { - //printf("%s", line); - - unsigned short int count = 0; - char* c; - char* opcode; - char* param1; - char* param2; - - //for(c = line; *c != '\0'; c++) { - Opcode* op = get_opcode(line); - //} - //printf("\n"); - if (strcmp(op->op, "NA") == 0) continue; - - printf("%s %s, %s\n", op->op, op->param1, op->param2); + List* list = get_strings(line); + + PrintList(list); + DestroyList(list); } - printf("\n"); - fclose(file); if (line) free(line); } -Opcode* get_opcode(char* line) { - Opcode *op = malloc(sizeof(Opcode)); - strcpy(op->op, "NA"); +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. - if (strlen(line) == 0 ) return op; - if (line[0] == ';') return op; + List *list = CreateList(); + unsigned long lineLength = strlen(line); + char* currentWord = malloc(lineLength + 1); + int index = 0; - int i = 0; - int count = 0; - - for (char* c = line; *c != '\0'; c++) { - if (*c == ';') break; - if (*c == ' ') { - i = 0; - count++; + for (int i = 0; i < lineLength; i++) { + if (line[i] == ' ' || line[i] == ',') { + if (index > 0) { + currentWord[index] = '\0'; + AddListItem(currentWord, list); + } + index = 0; continue; } - if (*c == ',') { - i = 0; - continue; + if (line[i] == ';' || i + 1 == lineLength) { + if (index > 0) { + currentWord[index] = '\0'; + AddListItem(currentWord, list); + } + break; } - if (count == 0) op->op[i] = *c; - else if (count == 1) op->param1[i] = *c; - else if (count == 2) op->param2[i] = *c; - - i++; + currentWord[index] = line[i]; + index++; } - return op; + return list; } \ No newline at end of file