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 <stdlib.h>
#include <string.h>
#define LISTDEFAULTSIZE 4
+2 -2
View File
@@ -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
ret
+4 -3
View File
@@ -1,7 +1,4 @@
#include "../includes/list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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]);
+38 -55
View File
@@ -3,14 +3,9 @@
#include <string.h>
#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;
}