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