Setup the assembler to use the new file reading code.

This commit is contained in:
2022-04-28 22:24:10 -05:00
parent 1968590654
commit 1900b504e7
6 changed files with 60 additions and 33 deletions
+3 -4
View File
@@ -5,16 +5,15 @@
int ReadAllString(const char* path, char** content_ptr, size_t* bytes_read) {
if (!path || !content_ptr || !bytes_read) return 0;
FILE *file;
file = fopen(path, "rb");
FILE *file = fopen(path, "rb");
if (!file) {
fprintf(stderr, "Failed to open '%s'. %s.\n", path, strerror(errno));
return 0;
}
char *content = NULL, *temp;
size_t used, capacity, read;
char *content = NULL, *temp = NULL;
size_t used = 0, capacity = 0, read = 0;
while(1) {
if (used + FUTIL_READ_SIZE + 1 > capacity) {
+1 -1
View File
@@ -25,7 +25,7 @@ List* CreateList() {
int AddListItem(const void *value, size_t size, List* list) {
if (!list) return -1;
if (!value) return -1;
if (size == 0) return - 1;
if (size == 0) return -1;
if (list->capacity < list->size + 1) {
void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2);
+15 -27
View File
@@ -1,43 +1,31 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include "../includes/list.h"
#include "../includes/lexer.h"
#include "../includes/parser.h"
typedef struct {
char *opcode;
unsigned char count;
char** parameters;
} Instruction;
#include "../includes/futil.h"
List* get_strings(const char*);
int main(int argc, char* args[]) {
if (argc == 1) {
printf("Input files required.\n");
return -1;
printf("Usage: assm <file1.asm>\n");
return EX_USAGE;
}
FILE *file;
int line_count = 1;
char* line = NULL;
size_t len = 0;
ssize_t bytes_read;
char* source_code;
size_t bytes_read;
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
char* token = strtok(source_code, "\n");
int line_number = 1;
file = fopen(args[1], "r");
while(token != NULL) {
if (!file) {
printf("Failed to open '%s' for reading.\n", args[1]);
return 1;
}
//getline() uses realloc() for the line parameter, so there shouldn't
//be any issue with memory leaks as long as the last time this function
//is called, line gets free()ed.
while((bytes_read = getline(&line, &len, file)) != -1) {
List* tokens = GetTokensFromLine(line);
List* tokens = GetTokensFromLine(token);
if (line_number == 1 || line_number == 2) {
ParseTokens(tokens);
@@ -53,10 +41,10 @@ int main(int argc, char* args[]) {
DestroyList(tokens);
token = strtok(NULL, "\n");
line_number++;
}
fclose(file);
if (line) free(line);
free(source_code);
}