First stage of splitting up a source line complete. Next is tokenizing the list of string being generated.

This commit is contained in:
2021-12-30 15:21:09 -06:00
parent 4be3a4a7ea
commit 08acc6af22
5 changed files with 46 additions and 60 deletions
+1
View File
@@ -0,0 +1 @@
assm
+1
View File
@@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#define LISTDEFAULTSIZE 4 #define LISTDEFAULTSIZE 4
+2 -2
View File
@@ -1,6 +1,6 @@
; comments are ignored ; comments are ignored
; This is a very simple test file for an assemblier. ; This is a very simple test file for an assemblier.
mov r1, 5 ; move the immediate value 5 into r1 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 int 21 ; maybe that will call some string drawing BIOS-like routine
ret ; return from 'call' if there were such a thing ret
+4 -3
View File
@@ -1,7 +1,4 @@
#include "../includes/list.h" #include "../includes/list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
List* CreateList() { List* CreateList() {
List *new = malloc(sizeof(List)); List *new = malloc(sizeof(List));
@@ -13,6 +10,7 @@ List* CreateList() {
} }
int AddListItem(char *value, List* list) { int AddListItem(char *value, List* list) {
if (list == NULL) return -1;
if (list->capacity < list->size + 1) { if (list->capacity < list->size + 1) {
list->root = realloc(list->root, sizeof(char*) * list->capacity * 2); list->root = realloc(list->root, sizeof(char*) * list->capacity * 2);
@@ -31,6 +29,8 @@ int AddListItem(char *value, List* list) {
} }
void PrintList(List* list) { void PrintList(List* list) {
if (list == NULL) return;
List *current = list; List *current = list;
printf("Size: %d; Capacity: %d\n", list->size, list->capacity); printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
@@ -41,6 +41,7 @@ void PrintList(List* list) {
} }
void DestroyList(List* list) { void DestroyList(List* list) {
if (list == NULL) return;
for(int i = 0; i < list->size; i++) { for(int i = 0; i < list->size; i++) {
free(list->root[i]); free(list->root[i]);
+38 -55
View File
@@ -3,14 +3,9 @@
#include <string.h> #include <string.h>
#include "../includes/list.h" #include "../includes/list.h"
typedef struct opcode {
char op[255];
char param1[255];
char param2[255];
} Opcode;
void print_file(char*); void print_file(char*);
Opcode* get_opcode(char*); List* get_strings(const char*);
int main(int argc, char* args[]) { int main(int argc, char* args[]) {
const char *input_files[argc - 1]; const char *input_files[argc - 1];
@@ -25,20 +20,20 @@ int main(int argc, char* args[]) {
// print_file(args[i]); // print_file(args[i]);
// } // }
//print_file(args[1]); print_file(args[1]);
char str[16]; // char str[16];
List *list = CreateList(); // List *list = CreateList();
for (int i = 0; i < 64; i++) { // for (int i = 0; i < 64; i++) {
sprintf(str, "%d", i); // sprintf(str, "%d", i);
AddListItem(str, list); // 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) { void print_file(char* file_path) {
@@ -55,59 +50,47 @@ void print_file(char* file_path) {
} }
while((bytes_read = getline(&line, &len, file)) != -1) { while((bytes_read = getline(&line, &len, file)) != -1) {
//printf("%s", line); List* list = get_strings(line);
unsigned short int count = 0; PrintList(list);
char* c; DestroyList(list);
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);
} }
printf("\n");
fclose(file); fclose(file);
if (line) free(line); if (line) free(line);
} }
Opcode* get_opcode(char* line) { List* get_strings(const char* line) {
Opcode *op = malloc(sizeof(Opcode)); if (strlen(line) == 0 ) return NULL;
strcpy(op->op, "NA"); 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; List *list = CreateList();
if (line[0] == ';') return op; unsigned long lineLength = strlen(line);
char* currentWord = malloc(lineLength + 1);
int index = 0;
int i = 0; for (int i = 0; i < lineLength; i++) {
int count = 0; if (line[i] == ' ' || line[i] == ',') {
if (index > 0) {
for (char* c = line; *c != '\0'; c++) { currentWord[index] = '\0';
if (*c == ';') break; AddListItem(currentWord, list);
if (*c == ' ') { }
i = 0; index = 0;
count++;
continue; continue;
} }
if (*c == ',') { if (line[i] == ';' || i + 1 == lineLength) {
i = 0; if (index > 0) {
continue; currentWord[index] = '\0';
AddListItem(currentWord, list);
}
break;
} }
if (count == 0) op->op[i] = *c; currentWord[index] = line[i];
else if (count == 1) op->param1[i] = *c; index++;
else if (count == 2) op->param2[i] = *c;
i++;
} }
return op; return list;
} }